VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-ut-integer.cpp@ 53528

Last change on this file since 53528 was 51770, checked in by vboxsync, 11 years ago

Merged in iprt++ dev branch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.7 KB
Line 
1/* $Id: asn1-ut-integer.cpp 51770 2014-07-01 18:14:02Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, INTEGER Type.
4 */
5
6/*
7 * Copyright (C) 2006-2014 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/bignum.h>
34#include <iprt/err.h>
35#include <iprt/string.h>
36
37#include <iprt/formats/asn1.h>
38
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43/** Fixed on-byte constants for small numbers.
44 * Good for structure version values and such. */
45static const uint8_t g_abSmall[] =
46{
47 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
48 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
49};
50
51
52
53/*
54 * ASN.1 INTEGER - Special Methods.
55 */
56
57
58/**
59 * Updates the native value we keep in RTASN1INTEGER::uValue.
60 *
61 * @param pThis The integer.
62 */
63static void rtAsn1Integer_UpdateNativeValue(PRTASN1INTEGER pThis)
64{
65 uint32_t offLast = pThis->Asn1Core.cb - 1;
66 switch (pThis->Asn1Core.cb)
67 {
68 default:
69 case 8: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 7] << 56;
70 case 7: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 6] << 48;
71 case 6: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 5] << 40;
72 case 5: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 4] << 32;
73 case 4: pThis->uValue.u |= (uint32_t)pThis->Asn1Core.uData.pu8[offLast - 3] << 24;
74 case 3: pThis->uValue.u |= (uint32_t)pThis->Asn1Core.uData.pu8[offLast - 2] << 16;
75 case 2: pThis->uValue.u |= (uint16_t)pThis->Asn1Core.uData.pu8[offLast - 1] << 8;
76 case 1: pThis->uValue.u |= pThis->Asn1Core.uData.pu8[offLast];
77 }
78}
79
80
81RTDECL(int) RTAsn1Integer_InitU64(PRTASN1INTEGER pThis, uint64_t uValue, PCRTASN1ALLOCATORVTABLE pAllocator)
82{
83 /*
84 * Initialize the core and the native value.
85 */
86 RTAsn1Core_InitEx(&pThis->Asn1Core,
87 ASN1_TAG_INTEGER,
88 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
89 &g_RTAsn1Integer_Vtable,
90 RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
91 pThis->uValue.u = uValue;
92
93 /*
94 * Use one of the constants if possible.
95 */
96 if (uValue < RT_ELEMENTS(g_abSmall))
97 {
98 pThis->Asn1Core.cb = 1;
99 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[0];
100 }
101 else
102 {
103 /*
104 * Need to turn uValue into a big endian number without any
105 * unnecessary leading zero bytes.
106 */
107 /* Figure the size. */
108 uint32_t cb = 0;
109 if (uValue <= UINT32_MAX)
110 {
111 if (uValue <= UINT16_MAX)
112 {
113 if (uValue <= UINT8_MAX)
114 cb = 1;
115 else
116 cb = 2;
117 }
118 else
119 {
120 if (uValue <= UINT32_C(0xffffff))
121 cb = 3;
122 else
123 cb = 4;
124 }
125 }
126 else
127 {
128 if (uValue <= UINT64_C(0x0000FfffFfffFfff))
129 {
130 if (uValue <= UINT64_C(0x000000ffFfffFfff))
131 cb = 5;
132 else
133 cb = 6;
134 }
135 else
136 {
137 if (uValue <= UINT64_C(0x00ffFfffFfffFfff))
138 cb = 7;
139 else
140 cb = 8;
141 }
142 }
143
144 /* Allocate space. */
145 int rc = RTAsn1ContentAllocZ(&pThis->Asn1Core, cb, pAllocator);
146 if (RT_FAILURE(rc))
147 {
148 RT_ZERO(*pThis);
149 return rc;
150 }
151
152 /* Serialize the number in MSB order. */
153 uint8_t *pb = (uint8_t *)pThis->Asn1Core.uData.pu8;
154 while (cb-- > 0)
155 {
156 pb[cb] = (uint8_t)uValue;
157 uValue >>= 8;
158 }
159 Assert(uValue == 0);
160 }
161 return VINF_SUCCESS;
162}
163
164
165RTDECL(int) RTAsn1Integer_InitDefault(PRTASN1INTEGER pThis, uint64_t uValue, PCRTASN1ALLOCATORVTABLE pAllocator)
166{
167 int rc = RTAsn1Integer_InitU64(pThis, uValue, pAllocator);
168 if (RT_SUCCESS(rc))
169 {
170 pThis->Asn1Core.fFlags &= ~RTASN1CORE_F_PRESENT;
171 pThis->Asn1Core.fFlags |= RTASN1CORE_F_DEFAULT;
172 }
173 return rc;
174}
175
176
177RTDECL(int32_t) RTAsn1Integer_UnsignedLastBit(PCRTASN1INTEGER pThis)
178{
179 AssertReturn(pThis->Asn1Core.fFlags, -1);
180 uint8_t const *pb = pThis->Asn1Core.uData.pu8;
181 AssertReturn(pb, -1);
182 uint32_t cb = pThis->Asn1Core.cb;
183 AssertReturn(pThis->Asn1Core.cb < (uint32_t)INT32_MAX / 8, -1);
184
185 while (cb-- > 0)
186 {
187 uint8_t b = *pb++;
188 if (b)
189 {
190 int32_t iRet = cb * 8;
191 if (b & 0x80) iRet += 7;
192 else if (b & 0x40) iRet += 6;
193 else if (b & 0x20) iRet += 5;
194 else if (b & 0x10) iRet += 4;
195 else if (b & 0x08) iRet += 3;
196 else if (b & 0x04) iRet += 2;
197 else if (b & 0x02) iRet += 1;
198 else Assert(b == 0x01);
199 return iRet;
200 }
201 }
202 return -1;
203}
204
205
206RTDECL(int) RTAsn1Integer_UnsignedCompare(PCRTASN1INTEGER pLeft, PCRTASN1INTEGER pRight)
207{
208 Assert(pLeft && (!RTAsn1Integer_IsPresent(pLeft) || pLeft->Asn1Core.pOps == &g_RTAsn1Integer_Vtable));
209 Assert(pRight && (!RTAsn1Integer_IsPresent(pRight) || pRight->Asn1Core.pOps == &g_RTAsn1Integer_Vtable));
210
211 int iDiff;
212 if (RTAsn1Integer_IsPresent(pLeft))
213 {
214 if (RTAsn1Integer_IsPresent(pRight))
215 {
216 if ( pLeft->Asn1Core.cb > 8
217 || pRight->Asn1Core.cb > 8)
218 {
219 uint32_t iLeft = RTAsn1Integer_UnsignedLastBit(pLeft);
220 uint32_t iRight = RTAsn1Integer_UnsignedLastBit(pRight);
221 if (iLeft != iRight)
222 return iLeft < iRight ? -1 : 1;
223
224 uint32_t i = iLeft / 8;
225 if (i > 8)
226 {
227 uint8_t const *pbLeft = &pLeft->Asn1Core.uData.pu8[pLeft->Asn1Core.cb - i - 1];
228 uint8_t const *pbRight = &pRight->Asn1Core.uData.pu8[pRight->Asn1Core.cb - i - 1];
229 for (;;)
230 {
231 if (*pbLeft != *pbRight)
232 return *pbLeft < *pbRight ? -1 : 1;
233 if (--i <= 8)
234 break;
235 pbLeft++;
236 pbRight++;
237 }
238 }
239 }
240
241 if (pLeft->uValue.u == pRight->uValue.u)
242 iDiff = 0;
243 else
244 iDiff = pLeft->uValue.u < pRight->uValue.u ? -1 : 1;
245 }
246 else
247 iDiff = -1;
248 }
249 else
250 iDiff = 0 - (int)RTAsn1Integer_IsPresent(pRight);
251 return iDiff;
252}
253
254
255RTDECL(int) RTAsn1Integer_UnsignedCompareWithU64(PCRTASN1INTEGER pThis, uint64_t u64Const)
256{
257 int iDiff;
258 if (RTAsn1Integer_IsPresent(pThis))
259 {
260 if (pThis->Asn1Core.cb > 8)
261 {
262 int32_t iLast = RTAsn1Integer_UnsignedLastBit(pThis);
263 if (iLast >= 64)
264 return 1;
265 }
266
267 if (pThis->uValue.u == u64Const)
268 iDiff = 0;
269 else
270 iDiff = pThis->uValue.u < u64Const ? -1 : 1;
271 }
272 else
273 iDiff = 1;
274 return iDiff;
275}
276
277
278RTDECL(int) RTAsn1Integer_UnsignedCompareWithU32(PCRTASN1INTEGER pThis, uint32_t u32Const)
279{
280 int iDiff;
281 if (RTAsn1Integer_IsPresent(pThis))
282 {
283 if (pThis->Asn1Core.cb > 8)
284 {
285 int32_t iLast = RTAsn1Integer_UnsignedLastBit(pThis);
286 if (iLast >= 32)
287 return 1;
288 }
289
290 if (pThis->uValue.u == u32Const)
291 iDiff = 0;
292 else
293 iDiff = pThis->uValue.u < u32Const ? -1 : 1;
294 }
295 else
296 iDiff = 1;
297 return iDiff;
298}
299
300
301RTDECL(int) RTAsn1Integer_ToBigNum(PCRTASN1INTEGER pThis, PRTBIGNUM pBigNum, uint32_t fBigNumInit)
302{
303 AssertReturn(!(fBigNumInit & ~( RTBIGNUMINIT_F_SENSITIVE | RTBIGNUMINIT_F_UNSIGNED | RTBIGNUMINIT_F_SIGNED
304 | RTBIGNUMINIT_F_ENDIAN_LITTLE | RTBIGNUMINIT_F_ENDIAN_BIG)),
305 VERR_INVALID_PARAMETER);
306 AssertReturn(RTAsn1Integer_IsPresent(pThis), VERR_INVALID_PARAMETER);
307
308 if (!(fBigNumInit & (RTBIGNUMINIT_F_UNSIGNED | RTBIGNUMINIT_F_SIGNED)))
309 fBigNumInit |= RTBIGNUMINIT_F_SIGNED;
310
311 if (!(fBigNumInit & (RTBIGNUMINIT_F_ENDIAN_BIG | RTBIGNUMINIT_F_ENDIAN_LITTLE)))
312 fBigNumInit |= RTBIGNUMINIT_F_ENDIAN_BIG;
313
314 return RTBigNumInit(pBigNum, fBigNumInit, pThis->Asn1Core.uData.pv, pThis->Asn1Core.cb);
315}
316
317
318RTDECL(int) RTAsn1Integer_FromBigNum(PRTASN1INTEGER pThis, PCRTBIGNUM pBigNum, PCRTASN1ALLOCATORVTABLE pAllocator)
319{
320 AssertPtr(pThis); AssertPtr(pBigNum); AssertPtr(pAllocator);
321
322 /* Be nice and auto init the object. */
323 if (!RTAsn1Integer_IsPresent(pThis))
324 RTAsn1Integer_Init(pThis, NULL);
325
326 uint32_t cb = RTBigNumByteWidth(pBigNum); Assert(cb > 0);
327 int rc = RTAsn1ContentReallocZ(&pThis->Asn1Core, cb, pAllocator);
328 if (RT_SUCCESS(rc))
329 {
330 Assert(cb == pThis->Asn1Core.cb);
331 rc = RTBigNumToBytesBigEndian(pBigNum, (void *)pThis->Asn1Core.uData.pv, cb);
332 if (RT_SUCCESS(rc))
333 rtAsn1Integer_UpdateNativeValue(pThis);
334 }
335 return rc;
336}
337
338
339
340/*
341 * ASN.1 INTEGER - Standard Methods.
342 */
343
344RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1Integer_Vtable =
345{
346 "RTAsn1Integer",
347 sizeof(RTASN1INTEGER),
348 ASN1_TAG_INTEGER,
349 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
350 0,
351 (PFNRTASN1COREVTDTOR)RTAsn1Integer_Delete,
352 NULL,
353 (PFNRTASN1COREVTCLONE)RTAsn1Integer_Clone,
354 (PFNRTASN1COREVTCOMPARE)RTAsn1Integer_Compare,
355 (PFNRTASN1COREVTCHECKSANITY)RTAsn1Integer_CheckSanity,
356 NULL,
357 NULL
358};
359
360
361RTDECL(int) RTAsn1Integer_Init(PRTASN1INTEGER pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
362{
363 RTAsn1Core_InitEx(&pThis->Asn1Core,
364 ASN1_TAG_INTEGER,
365 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
366 &g_RTAsn1Integer_Vtable,
367 RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
368 pThis->uValue.u = 1;
369 pThis->Asn1Core.cb = 1;
370 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[0];
371 return VINF_SUCCESS;
372}
373
374
375RTDECL(int) RTAsn1Integer_Clone(PRTASN1INTEGER pThis, PCRTASN1INTEGER pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
376{
377 AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
378 RT_ZERO(*pThis);
379 if (RTAsn1Integer_IsPresent(pSrc))
380 {
381 AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1Integer_Vtable, VERR_INTERNAL_ERROR_3);
382
383 int rc;
384 if ( pSrc->Asn1Core.cb != 1
385 || pSrc->uValue.u >= RT_ELEMENTS(g_abSmall))
386 {
387 /* Value is too large, copy it. */
388 rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
389 if (RT_FAILURE(rc))
390 return rc;
391 }
392 else
393 {
394 /* Use one of the const values. */
395 rc = RTAsn1Core_CloneNoContent(&pThis->Asn1Core, &pSrc->Asn1Core);
396 if (RT_FAILURE(rc))
397 return rc;
398 Assert(g_abSmall[pSrc->uValue.u] == pSrc->uValue.u);
399 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[pSrc->uValue.u];
400 }
401 pThis->uValue.u = pSrc->uValue.u;
402 }
403 return VINF_SUCCESS;
404}
405
406
407RTDECL(void) RTAsn1Integer_Delete(PRTASN1INTEGER pThis)
408{
409 if ( pThis
410 && RTAsn1Integer_IsPresent(pThis))
411 {
412 Assert(pThis->Asn1Core.pOps == &g_RTAsn1Integer_Vtable);
413
414 RTAsn1ContentFree(&pThis->Asn1Core);
415 RT_ZERO(*pThis);
416 }
417}
418
419
420RTDECL(int) RTAsn1Integer_Enum(PRTASN1INTEGER pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
421{
422 Assert(pThis && (!RTAsn1Integer_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1Integer_Vtable));
423
424 /* No children to enumerate. */
425 return VINF_SUCCESS;
426}
427
428
429RTDECL(int) RTAsn1Integer_Compare(PCRTASN1INTEGER pLeft, PCRTASN1INTEGER pRight)
430{
431 return RTAsn1Integer_UnsignedCompare(pLeft, pRight);
432}
433
434
435RTDECL(int) RTAsn1Integer_CheckSanity(PCRTASN1INTEGER pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
436{
437 if (RT_UNLIKELY(!RTAsn1Integer_IsPresent(pThis)))
438 return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (INTEGER).", pszErrorTag);
439 return VINF_SUCCESS;
440}
441
442
443/*
444 * Generate code for the associated collection types.
445 */
446#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-integer-template.h"
447#include <iprt/asn1-generator-internal-header.h>
448#include <iprt/asn1-generator-core.h>
449#include <iprt/asn1-generator-init.h>
450#include <iprt/asn1-generator-sanity.h>
451
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