VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use