VirtualBox

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

Last change on this file since 95596 was 95584, checked in by vboxsync, 2 years ago

IPRT/asn1: Made the first parameter of RTAsn1Integer_ToString const. bugref:8691

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.8 KB
Line 
1/* $Id: asn1-ut-integer.cpp 95584 2022-07-10 14:09:21Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, INTEGER Type.
4 */
5
6/*
7 * Copyright (C) 2006-2022 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: AssertBreak(pThis->Asn1Core.cb > 8); /* paranoia */ RT_FALL_THRU();
70 case 8: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 7] << 56; RT_FALL_THRU();
71 case 7: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 6] << 48; RT_FALL_THRU();
72 case 6: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 5] << 40; RT_FALL_THRU();
73 case 5: pThis->uValue.u |= (uint64_t)pThis->Asn1Core.uData.pu8[offLast - 4] << 32; RT_FALL_THRU();
74 case 4: pThis->uValue.u |= (uint32_t)pThis->Asn1Core.uData.pu8[offLast - 3] << 24; RT_FALL_THRU();
75 case 3: pThis->uValue.u |= (uint32_t)pThis->Asn1Core.uData.pu8[offLast - 2] << 16; RT_FALL_THRU();
76 case 2: pThis->uValue.u |= (uint16_t)pThis->Asn1Core.uData.pu8[offLast - 1] << 8; RT_FALL_THRU();
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 if ((int32_t)iLeft < 0)
225 return 0; /* Both are all zeros. */
226
227 uint32_t i = iLeft / 8;
228 if (i > 8)
229 {
230 uint8_t const *pbLeft = &pLeft->Asn1Core.uData.pu8[pLeft->Asn1Core.cb - i - 1];
231 uint8_t const *pbRight = &pRight->Asn1Core.uData.pu8[pRight->Asn1Core.cb - i - 1];
232 for (;;)
233 {
234 if (*pbLeft != *pbRight)
235 return *pbLeft < *pbRight ? -1 : 1;
236 if (--i <= 8)
237 break;
238 pbLeft++;
239 pbRight++;
240 }
241 }
242 }
243
244 if (pLeft->uValue.u == pRight->uValue.u)
245 iDiff = 0;
246 else
247 iDiff = pLeft->uValue.u < pRight->uValue.u ? -1 : 1;
248 }
249 else
250 iDiff = -1;
251 }
252 else
253 iDiff = 0 - (int)RTAsn1Integer_IsPresent(pRight);
254 return iDiff;
255}
256
257
258RTDECL(int) RTAsn1Integer_UnsignedCompareWithU64(PCRTASN1INTEGER pThis, uint64_t u64Const)
259{
260 int iDiff;
261 if (RTAsn1Integer_IsPresent(pThis))
262 {
263 if (pThis->Asn1Core.cb > 8)
264 {
265 int32_t iLast = RTAsn1Integer_UnsignedLastBit(pThis);
266 if (iLast >= 64)
267 return 1;
268 }
269
270 if (pThis->uValue.u == u64Const)
271 iDiff = 0;
272 else
273 iDiff = pThis->uValue.u < u64Const ? -1 : 1;
274 }
275 else
276 iDiff = 1;
277 return iDiff;
278}
279
280
281RTDECL(int) RTAsn1Integer_UnsignedCompareWithU32(PCRTASN1INTEGER pThis, uint32_t u32Const)
282{
283 int iDiff;
284 if (RTAsn1Integer_IsPresent(pThis))
285 {
286 if (pThis->Asn1Core.cb > 8)
287 {
288 int32_t iLast = RTAsn1Integer_UnsignedLastBit(pThis);
289 if (iLast >= 32)
290 return 1;
291 }
292
293 if (pThis->uValue.u == u32Const)
294 iDiff = 0;
295 else
296 iDiff = pThis->uValue.u < u32Const ? -1 : 1;
297 }
298 else
299 iDiff = 1;
300 return iDiff;
301}
302
303
304RTDECL(int) RTAsn1Integer_ToBigNum(PCRTASN1INTEGER pThis, PRTBIGNUM pBigNum, uint32_t fBigNumInit)
305{
306 AssertReturn(!(fBigNumInit & ~( RTBIGNUMINIT_F_SENSITIVE | RTBIGNUMINIT_F_UNSIGNED | RTBIGNUMINIT_F_SIGNED
307 | RTBIGNUMINIT_F_ENDIAN_LITTLE | RTBIGNUMINIT_F_ENDIAN_BIG)),
308 VERR_INVALID_PARAMETER);
309 AssertReturn(RTAsn1Integer_IsPresent(pThis), VERR_INVALID_PARAMETER);
310
311 if (!(fBigNumInit & (RTBIGNUMINIT_F_UNSIGNED | RTBIGNUMINIT_F_SIGNED)))
312 fBigNumInit |= RTBIGNUMINIT_F_SIGNED;
313
314 if (!(fBigNumInit & (RTBIGNUMINIT_F_ENDIAN_BIG | RTBIGNUMINIT_F_ENDIAN_LITTLE)))
315 fBigNumInit |= RTBIGNUMINIT_F_ENDIAN_BIG;
316
317 return RTBigNumInit(pBigNum, fBigNumInit, pThis->Asn1Core.uData.pv, pThis->Asn1Core.cb);
318}
319
320
321RTDECL(int) RTAsn1Integer_FromBigNum(PRTASN1INTEGER pThis, PCRTBIGNUM pBigNum, PCRTASN1ALLOCATORVTABLE pAllocator)
322{
323 AssertPtr(pThis); AssertPtr(pBigNum); AssertPtr(pAllocator);
324
325 /* Be nice and auto init the object. */
326 if (!RTAsn1Integer_IsPresent(pThis))
327 RTAsn1Integer_Init(pThis, NULL);
328
329 uint32_t cb = RTBigNumByteWidth(pBigNum); Assert(cb > 0);
330 int rc = RTAsn1ContentReallocZ(&pThis->Asn1Core, cb, pAllocator);
331 if (RT_SUCCESS(rc))
332 {
333 Assert(cb == pThis->Asn1Core.cb);
334 rc = RTBigNumToBytesBigEndian(pBigNum, (void *)pThis->Asn1Core.uData.pv, cb);
335 if (RT_SUCCESS(rc))
336 rtAsn1Integer_UpdateNativeValue(pThis);
337 }
338 return rc;
339}
340
341
342RTDECL(int) RTAsn1Integer_ToString(PCRTASN1INTEGER pThis, char *pszBuf, size_t cbBuf, uint32_t fFlags, size_t *pcbActual)
343{
344 AssertReturn(RTAsn1Integer_IsPresent(pThis), VERR_INVALID_PARAMETER);
345 AssertReturn(fFlags == 0, VERR_INVALID_FLAGS);
346
347 /*
348 * We only do hex conversions via this API.
349 * Currently we consider all numbers to be unsigned.
350 */
351 /** @todo Signed ASN.1 INTEGER. */
352 int rc;
353 size_t cbActual;
354 if (pThis->Asn1Core.cb <= 8)
355 {
356 cbActual = 2 + pThis->Asn1Core.cb*2 + 1;
357 if (cbActual <= cbBuf)
358 {
359 ssize_t cchFormat = RTStrFormatU64(pszBuf, cbBuf, pThis->uValue.u, 16, (int)cbActual - 1 /*cchWidth*/, 0,
360 RTSTR_F_SPECIAL | RTSTR_F_ZEROPAD);
361 rc = VINF_SUCCESS;
362 AssertStmt(cchFormat == (ssize_t)cbActual - 1, rc = VERR_INTERNAL_ERROR_3);
363 }
364 else
365 rc = VERR_BUFFER_OVERFLOW;
366 }
367 else
368 {
369 cbActual = pThis->Asn1Core.cb * 3 - 1 /* save one separator */ + 1 /* terminator */;
370 if (cbActual <= cbBuf)
371 {
372 rc = RTStrPrintHexBytes(pszBuf, cbBuf, pThis->Asn1Core.uData.pv, pThis->Asn1Core.cb, RTSTRPRINTHEXBYTES_F_SEP_SPACE);
373 Assert(rc == VINF_SUCCESS);
374 }
375 else
376 rc = VERR_BUFFER_OVERFLOW;
377 }
378 if (pcbActual)
379 *pcbActual = cbActual;
380 return rc;
381}
382
383
384/*
385 * ASN.1 INTEGER - Standard Methods.
386 */
387
388RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1Integer_Vtable =
389{
390 "RTAsn1Integer",
391 sizeof(RTASN1INTEGER),
392 ASN1_TAG_INTEGER,
393 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
394 0,
395 (PFNRTASN1COREVTDTOR)RTAsn1Integer_Delete,
396 NULL,
397 (PFNRTASN1COREVTCLONE)RTAsn1Integer_Clone,
398 (PFNRTASN1COREVTCOMPARE)RTAsn1Integer_Compare,
399 (PFNRTASN1COREVTCHECKSANITY)RTAsn1Integer_CheckSanity,
400 NULL,
401 NULL
402};
403
404
405RTDECL(int) RTAsn1Integer_Init(PRTASN1INTEGER pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
406{
407 RT_NOREF_PV(pAllocator);
408 RTAsn1Core_InitEx(&pThis->Asn1Core,
409 ASN1_TAG_INTEGER,
410 ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
411 &g_RTAsn1Integer_Vtable,
412 RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
413 pThis->uValue.u = 1;
414 pThis->Asn1Core.cb = 1;
415 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[0];
416 return VINF_SUCCESS;
417}
418
419
420RTDECL(int) RTAsn1Integer_Clone(PRTASN1INTEGER pThis, PCRTASN1INTEGER pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
421{
422 AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
423 RT_ZERO(*pThis);
424 if (RTAsn1Integer_IsPresent(pSrc))
425 {
426 AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1Integer_Vtable, VERR_INTERNAL_ERROR_3);
427
428 int rc;
429 if ( pSrc->Asn1Core.cb != 1
430 || pSrc->uValue.u >= RT_ELEMENTS(g_abSmall))
431 {
432 /* Value is too large, copy it. */
433 rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
434 if (RT_FAILURE(rc))
435 return rc;
436 }
437 else
438 {
439 /* Use one of the const values. */
440 rc = RTAsn1Core_CloneNoContent(&pThis->Asn1Core, &pSrc->Asn1Core);
441 if (RT_FAILURE(rc))
442 return rc;
443 Assert(g_abSmall[pSrc->uValue.u] == pSrc->uValue.u);
444 pThis->Asn1Core.uData.pv = (void *)&g_abSmall[pSrc->uValue.u];
445 }
446 pThis->uValue.u = pSrc->uValue.u;
447 }
448 return VINF_SUCCESS;
449}
450
451
452RTDECL(void) RTAsn1Integer_Delete(PRTASN1INTEGER pThis)
453{
454 if ( pThis
455 && RTAsn1Integer_IsPresent(pThis))
456 {
457 Assert(pThis->Asn1Core.pOps == &g_RTAsn1Integer_Vtable);
458
459 RTAsn1ContentFree(&pThis->Asn1Core);
460 RT_ZERO(*pThis);
461 }
462}
463
464
465RTDECL(int) RTAsn1Integer_Enum(PRTASN1INTEGER pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
466{
467 RT_NOREF_PV(pThis); RT_NOREF_PV(pfnCallback); RT_NOREF_PV(uDepth); RT_NOREF_PV(pvUser);
468 Assert(pThis && (!RTAsn1Integer_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1Integer_Vtable));
469
470 /* No children to enumerate. */
471 return VINF_SUCCESS;
472}
473
474
475RTDECL(int) RTAsn1Integer_Compare(PCRTASN1INTEGER pLeft, PCRTASN1INTEGER pRight)
476{
477 return RTAsn1Integer_UnsignedCompare(pLeft, pRight);
478}
479
480
481RTDECL(int) RTAsn1Integer_CheckSanity(PCRTASN1INTEGER pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
482{
483 RT_NOREF_PV(fFlags);
484 if (RT_UNLIKELY(!RTAsn1Integer_IsPresent(pThis)))
485 return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (INTEGER).", pszErrorTag);
486 return VINF_SUCCESS;
487}
488
489
490/*
491 * Generate code for the associated collection types.
492 */
493#define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-integer-template.h"
494#include <iprt/asn1-generator-internal-header.h>
495#include <iprt/asn1-generator-core.h>
496#include <iprt/asn1-generator-init.h>
497#include <iprt/asn1-generator-sanity.h>
498
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