VirtualBox

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

Last change on this file since 57444 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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