VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-ut-core.cpp@ 56290

Last change on this file since 56290 was 56290, checked in by vboxsync, 10 years ago

IPRT: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: asn1-ut-core.cpp 56290 2015-06-09 14:01:31Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, Generic Core Type.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include "internal/iprt.h"
31#include <iprt/asn1.h>
32
33#include <iprt/alloca.h>
34#include <iprt/bignum.h>
35#include <iprt/ctype.h>
36#include <iprt/err.h>
37#include <iprt/string.h>
38#include <iprt/uni.h>
39
40#include <iprt/formats/asn1.h>
41
42
43/*
44 * ASN.1 Core - Special methods (for all applications of RTASN1CORE).
45 */
46
47RTDECL(int) RTAsn1Core_SetTagAndFlags(PRTASN1CORE pAsn1Core, uint32_t uTag, uint8_t fClass)
48{
49 if (!(pAsn1Core->fFlags & RTASN1CORE_F_TAG_IMPLICIT))
50 {
51 pAsn1Core->fRealClass = pAsn1Core->fClass;
52 pAsn1Core->uRealTag = pAsn1Core->uTag;
53 Assert(pAsn1Core->uRealTag == pAsn1Core->uTag);
54 pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
55 }
56 pAsn1Core->uTag = uTag;
57 pAsn1Core->fClass = fClass;
58 return VINF_SUCCESS;
59}
60
61
62RTDECL(int) RTAsn1Core_ChangeTag(PRTASN1CORE pAsn1Core, uint32_t uTag)
63{
64 if (!(pAsn1Core->fFlags & RTASN1CORE_F_TAG_IMPLICIT))
65 pAsn1Core->uTag = uTag;
66 pAsn1Core->uRealTag = uTag;
67 return VINF_SUCCESS;
68}
69
70
71RTDECL(void) RTAsn1Core_ResetImplict(PRTASN1CORE pThis)
72{
73 AssertPtr(pThis);
74 if (pThis->fFlags & RTASN1CORE_F_TAG_IMPLICIT)
75 {
76 pThis->fFlags &= ~RTASN1CORE_F_TAG_IMPLICIT;
77 pThis->uTag = pThis->uRealTag;
78 pThis->fClass = pThis->fRealClass;
79 }
80}
81
82
83RTDECL(int) RTAsn1Core_InitEx(PRTASN1CORE pAsn1Core, uint32_t uTag, uint8_t fClass, PCRTASN1COREVTABLE pOps, uint32_t fFlags)
84{
85 pAsn1Core->uTag = uTag;
86 pAsn1Core->fClass = fClass;
87 pAsn1Core->uRealTag = uTag;
88 pAsn1Core->fRealClass = fClass;
89 pAsn1Core->cbHdr = 0;
90 pAsn1Core->cb = 0;
91 pAsn1Core->fFlags = fFlags;
92 pAsn1Core->uData.pv = NULL;
93 pAsn1Core->pOps = pOps;
94 return VINF_SUCCESS;
95}
96
97
98RTDECL(int) RTAsn1Core_InitDefault(PRTASN1CORE pAsn1Core, uint32_t uTag, uint8_t fClass)
99{
100 return RTAsn1Core_InitEx(pAsn1Core, uTag, fClass, NULL, RTASN1CORE_F_DEFAULT);
101}
102
103
104static int rtAsn1Core_CloneEx(PRTASN1CORE pThis, PCRTASN1CORE pSrc, PCRTASN1ALLOCATORVTABLE pAllocator, bool fCopyContent)
105{
106 Assert(RTASN1CORE_IS_PRESENT(pSrc));
107 pThis->uTag = pSrc->uTag;
108 pThis->fClass = pSrc->fClass;
109 pThis->uRealTag = pSrc->uRealTag;
110 pThis->fRealClass = pSrc->fRealClass;
111 pThis->cbHdr = pSrc->cbHdr;
112 pThis->fFlags = pSrc->fFlags & ~(RTASN1CORE_F_ALLOCATED_CONTENT | RTASN1CORE_F_DECODED_CONTENT);
113 pThis->pOps = pSrc->pOps;
114 pThis->cb = 0;
115 pThis->uData.pv = NULL;
116 if (pSrc->cb)
117 {
118 if (!fCopyContent)
119 pThis->cb = pSrc->cb;
120 else
121 {
122 int rc = RTAsn1ContentDup(pThis, pSrc->uData.pv, pSrc->cb, pAllocator);
123 if (RT_FAILURE(rc))
124 {
125 RT_ZERO(*pThis);
126 return rc;
127 }
128 Assert(pThis->cb == pSrc->cb);
129 AssertPtr(pThis->uData.pv);
130 }
131 }
132 return VINF_SUCCESS;
133}
134
135
136RTDECL(int) RTAsn1Core_CloneContent(PRTASN1CORE pThis, PCRTASN1CORE pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
137{
138 return rtAsn1Core_CloneEx(pThis, pSrc, pAllocator, true /*fConpyContent*/);
139}
140
141
142RTDECL(int) RTAsn1Core_CloneNoContent(PRTASN1CORE pThis, PCRTASN1CORE pSrc)
143{
144 return rtAsn1Core_CloneEx(pThis, pSrc, NULL, false /*fConpyContent*/);
145}
146
147
148RTDECL(int) RTAsn1Core_CompareEx(PCRTASN1CORE pLeft, PCRTASN1CORE pRight, bool fIgnoreTagAndClass)
149{
150 int iDiff;
151 if (RTASN1CORE_IS_PRESENT(pLeft))
152 {
153 if (RTASN1CORE_IS_PRESENT(pRight))
154 {
155 iDiff = memcmp(pLeft->uData.pv, pRight->uData.pv, RT_MIN(pLeft->cb, pRight->cb));
156 if (!iDiff)
157 {
158 if (pLeft->cb != pRight->cb)
159 iDiff = pLeft->cb < pRight->cb ? -1 : 1;
160 else if (!fIgnoreTagAndClass)
161 {
162 if (pLeft->uTag != pRight->uTag)
163 iDiff = pLeft->uTag < pRight->uTag ? -1 : 1;
164 else if (pLeft->fClass != pRight->fClass)
165 iDiff = pLeft->fClass < pRight->fClass ? -1 : 1;
166 }
167 }
168 else
169 iDiff = iDiff < 0 ? -1 : 1;
170 }
171 else
172 iDiff = -1;
173 }
174 else
175 iDiff = 0 - (int)RTASN1CORE_IS_PRESENT(pRight);
176 return iDiff;
177}
178
179
180
181/*
182 * ASN.1 Core - Standard Methods.
183 *
184 * Note! Children of the ASN.1 Core doesn't normally call these, they are for
185 * when RTASN1CORE is used as a member type.
186 */
187
188RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1Core_Vtable =
189{
190 "RTAsn1Core",
191 sizeof(RTASN1CORE),
192 UINT8_MAX,
193 UINT8_MAX,
194 0,
195 RTAsn1Core_Delete,
196 RTAsn1Core_Enum,
197 (PFNRTASN1COREVTCLONE)RTAsn1Core_Clone,
198 (PFNRTASN1COREVTCOMPARE)RTAsn1Core_Compare,
199 (PFNRTASN1COREVTCHECKSANITY)RTAsn1Core_CheckSanity,
200 NULL,
201 NULL
202};
203
204
205RTDECL(int) RTAsn1Core_Init(PRTASN1CORE pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
206{
207 return RTAsn1Core_InitEx(pThis, 0, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_PRIMITIVE,
208 &g_RTAsn1Core_Vtable, RTASN1CORE_F_PRESENT);
209}
210
211
212RTDECL(int) RTAsn1Core_Clone(PRTASN1CORE pThis, PCRTASN1CORE pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
213{
214 int rc;
215 RT_ZERO(*pThis);
216 if (RTASN1CORE_IS_PRESENT(pSrc))
217 {
218 Assert(pSrc->pOps == &g_RTAsn1Core_Vtable);
219
220 rc = RTAsn1Core_CloneContent(pThis, pSrc, pAllocator);
221 }
222 else
223 rc = VINF_SUCCESS;
224 return rc;
225}
226
227
228RTDECL(void) RTAsn1Core_Delete(PRTASN1CORE pThis)
229{
230 if (pThis && RTASN1CORE_IS_PRESENT(pThis))
231 {
232 Assert(pThis->pOps == &g_RTAsn1Core_Vtable);
233
234 RTAsn1ContentFree(pThis);
235 RT_ZERO(*pThis);
236 }
237}
238
239
240RTDECL(int) RTAsn1Core_Enum(PRTASN1CORE pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
241{
242 /* We have no children to enumerate. */
243 Assert(pThis && (!RTASN1CORE_IS_PRESENT(pThis) || pThis->pOps == &g_RTAsn1Core_Vtable));
244 NOREF(pThis);
245 NOREF(pfnCallback);
246 NOREF(uDepth);
247 NOREF(pvUser);
248 return VINF_SUCCESS;
249}
250
251
252RTDECL(int) RTAsn1Core_Compare(PCRTASN1CORE pLeft, PCRTASN1CORE pRight)
253{
254 Assert(pLeft && (!RTASN1CORE_IS_PRESENT(pLeft) || pLeft->pOps == &g_RTAsn1Core_Vtable));
255 Assert(pRight && (!RTASN1CORE_IS_PRESENT(pRight) || pRight->pOps == &g_RTAsn1Core_Vtable));
256
257 return RTAsn1Core_CompareEx(pLeft, pRight, false /*fIgnoreTagAndClass*/);
258}
259
260
261RTDECL(int) RTAsn1Core_CheckSanity(PCRTASN1CORE pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
262{
263 /* We can only check that it's present. */
264 if (!RTAsn1Core_IsPresent(pThis))
265 return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (RTASN1CORE).", pszErrorTag);
266 return VINF_SUCCESS;
267}
268
269
270/*
271 * Generate code for the associated collection types.
272 */
273#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-core-template.h"
274#include <iprt/asn1-generator-internal-header.h>
275#include <iprt/asn1-generator-core.h>
276#include <iprt/asn1-generator-init.h>
277#include <iprt/asn1-generator-sanity.h>
278
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette