1 | /* $Id: asn1-ut-objid.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, OBJECT IDENTIFIER Type.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/asn1.h>
|
---|
33 |
|
---|
34 | #include <iprt/alloca.h>
|
---|
35 | #include <iprt/bignum.h>
|
---|
36 | #include <iprt/ctype.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/uni.h>
|
---|
40 |
|
---|
41 | #include <iprt/formats/asn1.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*********************************************************************************************************************************
|
---|
45 | * Global Variables *
|
---|
46 | *********************************************************************************************************************************/
|
---|
47 | static char const g_szDefault[] = "2.16.840.1.113894";
|
---|
48 | static uint32_t const g_auDefault[] = { 2, 16, 840, 1, 113894 };
|
---|
49 | static uint8_t const g_abDefault[] =
|
---|
50 | {
|
---|
51 | 2*40 + 16, 0x80 | (840 >> 7), 840 & 0x7f, 1, 0x80 | (113894 >> 14), 0x80 | ((113894 >> 7) & 0x7f), 113894 & 0x7f
|
---|
52 | };
|
---|
53 |
|
---|
54 |
|
---|
55 | /*********************************************************************************************************************************
|
---|
56 | * Internal Functions *
|
---|
57 | *********************************************************************************************************************************/
|
---|
58 | DECLHIDDEN(int) rtAsn1ObjId_InternalFormatComponent(uint32_t uValue, char **ppszObjId, size_t *pcbObjId); /* asn1-ut-objid.cpp */
|
---|
59 | /** @todo check if we really need this. */
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * ASN.1 OBJECT IDENTIFIER - Special Methods.
|
---|
65 | */
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Encodes the ASN.1 byte sequence for a set of components.
|
---|
69 | *
|
---|
70 | * @returns IPRT status code.
|
---|
71 | * @param cComponents The number of components. Must be at least two.
|
---|
72 | * @param pauComponents The components array.
|
---|
73 | * @param pbEncoded The output buffer.
|
---|
74 | * @param pcbEncoded On input, this holds the size of the output buffer.
|
---|
75 | * On successful return it's the encoded size in bytes.
|
---|
76 | */
|
---|
77 | static int rtAsn1ObjId_EncodeComponents(uint32_t cComponents, uint32_t const *pauComponents,
|
---|
78 | uint8_t *pbEncoded, uint32_t *pcbEncoded)
|
---|
79 | {
|
---|
80 | uint8_t *pbCur = pbEncoded;
|
---|
81 | uint32_t cbLeft = *pcbEncoded;
|
---|
82 |
|
---|
83 | /* The first two componets are encoded together to save a byte, so the loop
|
---|
84 | organization is a little special. */
|
---|
85 | AssertReturn(cComponents >= 2, VERR_ASN1_INTERNAL_ERROR_1);
|
---|
86 | AssertReturn(pauComponents[0] <= 2, VERR_ASN1_INTERNAL_ERROR_1);
|
---|
87 | AssertReturn(pauComponents[1] <= (pauComponents[0] < 2 ? 39 : UINT32_MAX - 80), VERR_ASN1_INTERNAL_ERROR_1);
|
---|
88 | uint32_t i = 1;
|
---|
89 | uint32_t uValue = pauComponents[0] * 40 + pauComponents[1];
|
---|
90 |
|
---|
91 | for (;;)
|
---|
92 | {
|
---|
93 | if (uValue < 0x80)
|
---|
94 | {
|
---|
95 | if (RT_UNLIKELY(cbLeft < 1))
|
---|
96 | return VERR_BUFFER_OVERFLOW;
|
---|
97 | cbLeft -= 1;
|
---|
98 | *pbCur++ = (uint8_t)uValue;
|
---|
99 | }
|
---|
100 | else if (uValue < 0x4000)
|
---|
101 | {
|
---|
102 | if (RT_UNLIKELY(cbLeft < 2))
|
---|
103 | return VERR_BUFFER_OVERFLOW;
|
---|
104 | cbLeft -= 2;
|
---|
105 | pbCur[0] = (uValue >> 7) | 0x80;
|
---|
106 | pbCur[1] = uValue & 0x7f;
|
---|
107 | pbCur += 2;
|
---|
108 | }
|
---|
109 | else if (uValue < 0x200000)
|
---|
110 | {
|
---|
111 | if (RT_UNLIKELY(cbLeft < 3))
|
---|
112 | return VERR_BUFFER_OVERFLOW;
|
---|
113 | cbLeft -= 3;
|
---|
114 | pbCur[0] = (uValue >> 14) | 0x80;
|
---|
115 | pbCur[1] = ((uValue >> 7) & 0x7f) | 0x80;
|
---|
116 | pbCur[2] = uValue & 0x7f;
|
---|
117 | pbCur += 3;
|
---|
118 | }
|
---|
119 | else if (uValue < 0x10000000)
|
---|
120 | {
|
---|
121 | if (RT_UNLIKELY(cbLeft < 4))
|
---|
122 | return VERR_BUFFER_OVERFLOW;
|
---|
123 | cbLeft -= 4;
|
---|
124 | pbCur[0] = (uValue >> 21) | 0x80;
|
---|
125 | pbCur[1] = ((uValue >> 14) & 0x7f) | 0x80;
|
---|
126 | pbCur[2] = ((uValue >> 7) & 0x7f) | 0x80;
|
---|
127 | pbCur[3] = uValue & 0x7f;
|
---|
128 | pbCur += 4;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | if (RT_UNLIKELY(cbLeft < 5))
|
---|
133 | return VERR_BUFFER_OVERFLOW;
|
---|
134 | cbLeft -= 5;
|
---|
135 | pbCur[0] = (uValue >> 28) | 0x80;
|
---|
136 | pbCur[1] = ((uValue >> 21) & 0x7f) | 0x80;
|
---|
137 | pbCur[2] = ((uValue >> 14) & 0x7f) | 0x80;
|
---|
138 | pbCur[3] = ((uValue >> 7) & 0x7f) | 0x80;
|
---|
139 | pbCur[4] = uValue & 0x7f;
|
---|
140 | pbCur += 5;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* Advance / return. */
|
---|
144 | i++;
|
---|
145 | if (i >= cComponents)
|
---|
146 | {
|
---|
147 | *pcbEncoded = (uint32_t)(pbCur - pbEncoded);
|
---|
148 | return VINF_SUCCESS;
|
---|
149 | }
|
---|
150 | uValue = pauComponents[i];
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(int) RTAsn1ObjId_InitFromString(PRTASN1OBJID pThis, const char *pszObjId, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
156 | {
|
---|
157 | RT_ZERO(*pThis);
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Check the string, counting the number of components and checking their validity.
|
---|
161 | */
|
---|
162 | size_t cbObjId = strlen(pszObjId) + 1;
|
---|
163 | AssertReturn(cbObjId < sizeof(pThis->szObjId), VERR_ASN1_OBJID_TOO_LONG_STRING_FORM);
|
---|
164 |
|
---|
165 | const char *psz = pszObjId;
|
---|
166 |
|
---|
167 | /* Special checking of the first component. It has only three valid values: 0,1,2. */
|
---|
168 | char ch = *psz++;
|
---|
169 | if (RT_UNLIKELY(ch < '0' || ch > '2'))
|
---|
170 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
171 | char const chFirst = ch;
|
---|
172 | ch = *psz++;
|
---|
173 | if (RT_UNLIKELY(ch != '.'))
|
---|
174 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
175 |
|
---|
176 | /* The 2nd component. It the first is 0 or 1, it has a max of 39. */
|
---|
177 | uint32_t cComponents = 1;
|
---|
178 | if (chFirst < '2')
|
---|
179 | {
|
---|
180 | ch = *psz++;
|
---|
181 | if (*psz == '.')
|
---|
182 | {
|
---|
183 | if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
|
---|
184 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
185 | }
|
---|
186 | else
|
---|
187 | {
|
---|
188 | if (RT_UNLIKELY(ch < '0' || ch > '3'))
|
---|
189 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
190 | ch = *psz++;
|
---|
191 | if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
|
---|
192 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
193 | if (*psz != '.')
|
---|
194 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
195 | }
|
---|
196 | cComponents++;
|
---|
197 | }
|
---|
198 | else
|
---|
199 | psz--;
|
---|
200 |
|
---|
201 | /* Subsequent components have max values of UINT32_MAX - 80. */
|
---|
202 | while ((ch = *psz++) != '\0')
|
---|
203 | {
|
---|
204 | if (RT_UNLIKELY(ch != '.'))
|
---|
205 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
206 | const char *pszStart = psz;
|
---|
207 |
|
---|
208 | /* Special treatment of the first digit. Need to make sure it isn't an
|
---|
209 | unnecessary leading 0. */
|
---|
210 | ch = *psz++;
|
---|
211 | if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
|
---|
212 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
213 | if (RT_UNLIKELY(ch == '0' && RT_C_IS_DIGIT(*psz)))
|
---|
214 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
215 |
|
---|
216 | /* The rest of the digits. */
|
---|
217 | while ((ch = *psz) != '.' && ch != '\0')
|
---|
218 | {
|
---|
219 | if (RT_UNLIKELY(!RT_C_IS_DIGIT(ch)))
|
---|
220 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
221 | psz++;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /* Check the value range. */
|
---|
225 | if (RT_UNLIKELY(psz - pszStart >= 9))
|
---|
226 | if ( psz - pszStart > 9
|
---|
227 | || strncmp(pszStart, "4294967216", 9) >= 0) /* 2^32 - 80 */
|
---|
228 | return VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
229 |
|
---|
230 | cComponents++;
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (RT_UNLIKELY(cComponents >= 128))
|
---|
234 | return VERR_ASN1_OBJID_TOO_MANY_COMPONENTS;
|
---|
235 | pThis->cComponents = (uint8_t)cComponents;
|
---|
236 |
|
---|
237 | /*
|
---|
238 | * Find space for the component array, either at the unused end of szObjId
|
---|
239 | * or on the heap.
|
---|
240 | */
|
---|
241 | int rc;
|
---|
242 | RTAsn1MemInitAllocation(&pThis->Allocation, pAllocator);
|
---|
243 | #if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
|
---|
244 | size_t cbLeft = sizeof(pThis->szObjId) - cbObjId;
|
---|
245 | if (cbLeft >= cComponents * sizeof(uint32_t))
|
---|
246 | {
|
---|
247 | pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - cComponents * sizeof(uint32_t)];
|
---|
248 | cbLeft -= cComponents * sizeof(uint32_t);
|
---|
249 | rc = VINF_SUCCESS;
|
---|
250 | }
|
---|
251 | else
|
---|
252 | #endif
|
---|
253 | rc = RTAsn1MemAllocZ(&pThis->Allocation, (void **)&pThis->pauComponents, cComponents * sizeof(uint32_t));
|
---|
254 | if (RT_SUCCESS(rc))
|
---|
255 | {
|
---|
256 | /*
|
---|
257 | * Fill the elements array.
|
---|
258 | */
|
---|
259 | uint32_t *pauComponents = (uint32_t *)pThis->pauComponents;
|
---|
260 | rc = VINF_SUCCESS;
|
---|
261 | psz = pszObjId;
|
---|
262 | for (uint32_t i = 0; i < cComponents; i++)
|
---|
263 | {
|
---|
264 | uint32_t uValue = 0;
|
---|
265 | rc = RTStrToUInt32Ex(psz, (char **)&psz, 10, &uValue);
|
---|
266 | if (rc == VWRN_TRAILING_CHARS)
|
---|
267 | {
|
---|
268 | pauComponents[i] = uValue;
|
---|
269 | AssertBreakStmt(*psz == '.', rc = VERR_TRAILING_CHARS);
|
---|
270 | psz++;
|
---|
271 | }
|
---|
272 | else if (rc == VINF_SUCCESS)
|
---|
273 | {
|
---|
274 | pauComponents[i] = uValue;
|
---|
275 | Assert(*psz == '\0');
|
---|
276 | }
|
---|
277 | else if (RT_FAILURE(rc))
|
---|
278 | break;
|
---|
279 | else
|
---|
280 | {
|
---|
281 | rc = -rc;
|
---|
282 | break;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | if (rc == VINF_SUCCESS && *psz == '\0')
|
---|
286 | {
|
---|
287 | /*
|
---|
288 | * Initialize the core structure before we start on the encoded bytes.
|
---|
289 | */
|
---|
290 | RTAsn1Core_InitEx(&pThis->Asn1Core,
|
---|
291 | ASN1_TAG_OID,
|
---|
292 | ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
|
---|
293 | &g_RTAsn1ObjId_Vtable,
|
---|
294 | RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
|
---|
295 |
|
---|
296 | /*
|
---|
297 | * Encode the value into the string buffer. This will NOT overflow
|
---|
298 | * because the string representation is much less efficient than the
|
---|
299 | * binary ASN.1 representation (base-10 + separators vs. base-128).
|
---|
300 | */
|
---|
301 | pThis->Asn1Core.cb = (uint32_t)cbObjId;
|
---|
302 | rc = rtAsn1ObjId_EncodeComponents(cComponents, pThis->pauComponents,
|
---|
303 | (uint8_t *)&pThis->szObjId[0], &pThis->Asn1Core.cb);
|
---|
304 | if (RT_SUCCESS(rc))
|
---|
305 | {
|
---|
306 | /*
|
---|
307 | * Now, find a place for the encoded bytes. There might be
|
---|
308 | * enough room left in the szObjId for it if we're lucky.
|
---|
309 | */
|
---|
310 | #if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
|
---|
311 | if (pThis->Asn1Core.cb >= cbLeft)
|
---|
312 | pThis->Asn1Core.uData.pv = memmove(&pThis->szObjId[cbObjId], &pThis->szObjId[0], pThis->Asn1Core.cb);
|
---|
313 | else
|
---|
314 | #endif
|
---|
315 | rc = RTAsn1ContentDup(&pThis->Asn1Core, pThis->szObjId, pThis->Asn1Core.cb, pAllocator);
|
---|
316 | if (RT_SUCCESS(rc))
|
---|
317 | {
|
---|
318 | /*
|
---|
319 | * Finally, copy the dotted string.
|
---|
320 | */
|
---|
321 | memcpy(pThis->szObjId, pszObjId, cbObjId);
|
---|
322 | return VINF_SUCCESS;
|
---|
323 | }
|
---|
324 | }
|
---|
325 | else
|
---|
326 | {
|
---|
327 | AssertMsgFailed(("%Rrc\n", rc));
|
---|
328 | rc = VERR_ASN1_INTERNAL_ERROR_3;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | else
|
---|
332 | rc = VERR_ASN1_OBJID_INVALID_DOTTED_STRING;
|
---|
333 | }
|
---|
334 | RT_ZERO(*pThis);
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | RTDECL(int) RTAsn1ObjId_CompareWithString(PCRTASN1OBJID pThis, const char *pszRight)
|
---|
340 | {
|
---|
341 | return strcmp(pThis->szObjId, pszRight);
|
---|
342 | }
|
---|
343 |
|
---|
344 |
|
---|
345 | RTDECL(bool) RTAsn1ObjId_StartsWith(PCRTASN1OBJID pThis, const char *pszStartsWith)
|
---|
346 | {
|
---|
347 | size_t cchStartsWith = strlen(pszStartsWith);
|
---|
348 | return !strncmp(pThis->szObjId, pszStartsWith, cchStartsWith)
|
---|
349 | && ( pszStartsWith[cchStartsWith] == '.'
|
---|
350 | || pszStartsWith[cchStartsWith] == '\0');
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | RTDECL(uint8_t) RTAsn1ObjIdCountComponents(PCRTASN1OBJID pThis)
|
---|
355 | {
|
---|
356 | return pThis->cComponents;
|
---|
357 | }
|
---|
358 |
|
---|
359 |
|
---|
360 | RTDECL(uint32_t) RTAsn1ObjIdGetComponentsAsUInt32(PCRTASN1OBJID pThis, uint8_t iComponent)
|
---|
361 | {
|
---|
362 | if (iComponent < pThis->cComponents)
|
---|
363 | return pThis->pauComponents[iComponent];
|
---|
364 | return UINT32_MAX;
|
---|
365 | }
|
---|
366 |
|
---|
367 |
|
---|
368 | RTDECL(uint32_t) RTAsn1ObjIdGetLastComponentsAsUInt32(PCRTASN1OBJID pThis)
|
---|
369 | {
|
---|
370 | return pThis->pauComponents[pThis->cComponents - 1];
|
---|
371 | }
|
---|
372 |
|
---|
373 |
|
---|
374 | /*
|
---|
375 | * ASN.1 OBJECT IDENTIFIER - Standard Methods.
|
---|
376 | */
|
---|
377 |
|
---|
378 | RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1ObjId_Vtable =
|
---|
379 | {
|
---|
380 | "RTAsn1ObjId",
|
---|
381 | sizeof(RTASN1OBJID),
|
---|
382 | ASN1_TAG_OID,
|
---|
383 | ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
|
---|
384 | 0,
|
---|
385 | (PFNRTASN1COREVTDTOR)RTAsn1ObjId_Delete,
|
---|
386 | NULL,
|
---|
387 | (PFNRTASN1COREVTCLONE)RTAsn1ObjId_Clone,
|
---|
388 | (PFNRTASN1COREVTCOMPARE)RTAsn1ObjId_Compare,
|
---|
389 | (PFNRTASN1COREVTCHECKSANITY)RTAsn1ObjId_CheckSanity,
|
---|
390 | NULL,
|
---|
391 | NULL
|
---|
392 | };
|
---|
393 |
|
---|
394 |
|
---|
395 | RTDECL(int) RTAsn1ObjId_Init(PRTASN1OBJID pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
396 | {
|
---|
397 | RT_NOREF_PV(pAllocator);
|
---|
398 | RTAsn1Core_InitEx(&pThis->Asn1Core,
|
---|
399 | ASN1_TAG_OID,
|
---|
400 | ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
|
---|
401 | &g_RTAsn1ObjId_Vtable,
|
---|
402 | RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
|
---|
403 | pThis->Asn1Core.cb = sizeof(g_abDefault);
|
---|
404 | pThis->Asn1Core.uData.pv = (void *)&g_abDefault[0];
|
---|
405 | pThis->cComponents = RT_ELEMENTS(g_auDefault);
|
---|
406 | pThis->pauComponents = g_auDefault;
|
---|
407 | AssertCompile(sizeof(g_szDefault) <= sizeof(pThis->szObjId));
|
---|
408 | memcpy(pThis->szObjId, g_szDefault, sizeof(g_szDefault));
|
---|
409 | return VINF_SUCCESS;
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | RTDECL(int) RTAsn1ObjId_Clone(PRTASN1OBJID pThis, PCRTASN1OBJID pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
414 | {
|
---|
415 | AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
|
---|
416 | RT_ZERO(*pThis);
|
---|
417 | if (RTAsn1ObjId_IsPresent(pSrc))
|
---|
418 | {
|
---|
419 | AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1ObjId_Vtable, VERR_INTERNAL_ERROR_3);
|
---|
420 |
|
---|
421 | /* Copy the dotted string representation. */
|
---|
422 | size_t cbObjId = strlen(pSrc->szObjId) + 1;
|
---|
423 | AssertReturn(cbObjId <= sizeof(pThis->szObjId), VERR_INTERNAL_ERROR_5);
|
---|
424 | memcpy(pThis->szObjId, pSrc->szObjId, cbObjId);
|
---|
425 |
|
---|
426 | /* Copy the integer component array. Try fit it in the unused space of
|
---|
427 | the dotted object string buffer. We place it at the end of the
|
---|
428 | buffer as that is simple alignment wise and avoid wasting bytes that
|
---|
429 | could be used to sequueze in the content bytes (see below). */
|
---|
430 | int rc;
|
---|
431 | RTAsn1MemInitAllocation(&pThis->Allocation, pAllocator);
|
---|
432 | pThis->cComponents = pSrc->cComponents;
|
---|
433 | #if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
|
---|
434 | size_t cbLeft = sizeof(pThis->szObjId);
|
---|
435 | if (pSrc->cComponents * sizeof(uint32_t) <= cbLeft)
|
---|
436 | {
|
---|
437 | pThis->pauComponents = (uint32_t *)&pThis->szObjId[sizeof(pThis->szObjId) - pSrc->cComponents * sizeof(uint32_t)];
|
---|
438 | memcpy((uint32_t *)pThis->pauComponents, pSrc->pauComponents, pSrc->cComponents * sizeof(uint32_t));
|
---|
439 | cbLeft -= pSrc->cComponents * sizeof(uint32_t);
|
---|
440 | rc = VINF_SUCCESS;
|
---|
441 | }
|
---|
442 | else
|
---|
443 | #endif
|
---|
444 | {
|
---|
445 | rc = RTAsn1MemDup(&pThis->Allocation, (void **)&pThis->pauComponents, pSrc->pauComponents,
|
---|
446 | pSrc->cComponents * sizeof(uint32_t));
|
---|
447 | }
|
---|
448 | if (RT_SUCCESS(rc))
|
---|
449 | {
|
---|
450 | /* See if we can fit the content value into the szObjId as well.
|
---|
451 | It will follow immediately after the string as the component
|
---|
452 | array is the end of the string buffer, when present. */
|
---|
453 | #if 0 /** @todo breaks with arrays of ObjIds or structs containing them. They get resized and repositioned in memory, thus invalidating the pointer. Add recall-pointers callback, or just waste memory? Or maybe make all arrays pointer-arrays? */
|
---|
454 | uint32_t cbContent = pSrc->Asn1Core.cb;
|
---|
455 | if (cbContent <= cbLeft)
|
---|
456 | {
|
---|
457 | rc = RTAsn1Core_CloneNoContent(&pThis->Asn1Core, &pSrc->Asn1Core);
|
---|
458 | if (RT_SUCCESS(rc))
|
---|
459 | {
|
---|
460 | pThis->Asn1Core.uData.pv = memcpy(&pThis->szObjId[cbObjId], pSrc->Asn1Core.uData.pv, cbContent);
|
---|
461 | return VINF_SUCCESS;
|
---|
462 | }
|
---|
463 | }
|
---|
464 | else
|
---|
465 | #endif
|
---|
466 | {
|
---|
467 | rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
|
---|
468 | if (RT_SUCCESS(rc))
|
---|
469 | return VINF_SUCCESS;
|
---|
470 | }
|
---|
471 | }
|
---|
472 |
|
---|
473 | /* failed, clean up. */
|
---|
474 | if (pThis->Allocation.cbAllocated)
|
---|
475 | RTAsn1MemFree(&pThis->Allocation, (uint32_t *)pThis->pauComponents);
|
---|
476 | RT_ZERO(*pThis);
|
---|
477 | return rc;
|
---|
478 | }
|
---|
479 | return VINF_SUCCESS;
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | RTDECL(void) RTAsn1ObjId_Delete(PRTASN1OBJID pThis)
|
---|
484 | {
|
---|
485 | if ( pThis
|
---|
486 | && RTAsn1ObjId_IsPresent(pThis))
|
---|
487 | {
|
---|
488 | Assert(pThis->Asn1Core.pOps == &g_RTAsn1ObjId_Vtable);
|
---|
489 |
|
---|
490 | if (pThis->Allocation.cbAllocated)
|
---|
491 | RTAsn1MemFree(&pThis->Allocation, (uint32_t *)pThis->pauComponents);
|
---|
492 | RTAsn1ContentFree(&pThis->Asn1Core);
|
---|
493 | RT_ZERO(*pThis);
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | RTDECL(int) RTAsn1ObjId_Enum(PRTASN1OBJID pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
|
---|
499 | {
|
---|
500 | RT_NOREF_PV(pThis); RT_NOREF_PV(pfnCallback); RT_NOREF_PV(uDepth); RT_NOREF_PV(pvUser);
|
---|
501 | Assert(pThis && (!RTAsn1ObjId_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1ObjId_Vtable));
|
---|
502 |
|
---|
503 | /* No children to enumerate. */
|
---|
504 | return VINF_SUCCESS;
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | RTDECL(int) RTAsn1ObjId_Compare(PCRTASN1OBJID pLeft, PCRTASN1OBJID pRight)
|
---|
509 | {
|
---|
510 | if (RTAsn1ObjId_IsPresent(pLeft))
|
---|
511 | {
|
---|
512 | if (RTAsn1ObjId_IsPresent(pRight))
|
---|
513 | {
|
---|
514 | uint8_t cComponents = RT_MIN(pLeft->cComponents, pRight->cComponents);
|
---|
515 | for (uint32_t i = 0; i < cComponents; i++)
|
---|
516 | if (pLeft->pauComponents[i] != pRight->pauComponents[i])
|
---|
517 | return pLeft->pauComponents[i] < pRight->pauComponents[i] ? -1 : 1;
|
---|
518 |
|
---|
519 | if (pLeft->cComponents == pRight->cComponents)
|
---|
520 | return 0;
|
---|
521 | return pLeft->cComponents < pRight->cComponents ? -1 : 1;
|
---|
522 | }
|
---|
523 | return 1;
|
---|
524 | }
|
---|
525 | return 0 - (int)RTAsn1ObjId_IsPresent(pRight);
|
---|
526 | }
|
---|
527 |
|
---|
528 |
|
---|
529 | RTDECL(int) RTAsn1ObjId_CheckSanity(PCRTASN1OBJID pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
|
---|
530 | {
|
---|
531 | RT_NOREF_PV(fFlags);
|
---|
532 | if (RT_UNLIKELY(!RTAsn1ObjId_IsPresent(pThis)))
|
---|
533 | return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (OBJID).", pszErrorTag);
|
---|
534 | return VINF_SUCCESS;
|
---|
535 | }
|
---|
536 |
|
---|
537 |
|
---|
538 | /*
|
---|
539 | * Generate code for the associated collection types.
|
---|
540 | */
|
---|
541 | #define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-objid-template.h"
|
---|
542 | #include <iprt/asn1-generator-internal-header.h>
|
---|
543 | #include <iprt/asn1-generator-core.h>
|
---|
544 | #include <iprt/asn1-generator-init.h>
|
---|
545 | #include <iprt/asn1-generator-sanity.h>
|
---|
546 |
|
---|