1 | /* $Id: asn1-ut-boolean.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, BOOLEAN Type.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 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/bignum.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 | #include <iprt/formats/asn1.h>
|
---|
49 |
|
---|
50 |
|
---|
51 | /*********************************************************************************************************************************
|
---|
52 | * Global Variables *
|
---|
53 | *********************************************************************************************************************************/
|
---|
54 | /** The false value (DER & CER). */
|
---|
55 | static const uint8_t g_bFalse = 0;
|
---|
56 | /** The true value (DER & CER). */
|
---|
57 | static const uint8_t g_bTrue = 0xff;
|
---|
58 |
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * ASN.1 BOOLEAN - Special Methods.
|
---|
62 | */
|
---|
63 |
|
---|
64 | RTDECL(int) RTAsn1Boolean_InitDefault(PRTASN1BOOLEAN pThis, bool fValue, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
65 | {
|
---|
66 | RT_NOREF_PV(pAllocator);
|
---|
67 | RTAsn1Core_InitEx(&pThis->Asn1Core, ASN1_TAG_BOOLEAN, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
|
---|
68 | &g_RTAsn1Boolean_Vtable, RTASN1CORE_F_DEFAULT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
|
---|
69 | pThis->fValue = fValue;
|
---|
70 | pThis->Asn1Core.uData.pv = (void *)(fValue ? &g_bTrue : &g_bFalse);
|
---|
71 | return VINF_SUCCESS;
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | RTDECL(int) RTAsn1Boolean_Set(PRTASN1BOOLEAN pThis, bool fValue)
|
---|
76 | {
|
---|
77 | /* Since we don't need an allocator, let's automatically initialize the struct. */
|
---|
78 | if (!RTAsn1Boolean_IsPresent(pThis))
|
---|
79 | RTAsn1Boolean_Init(pThis, NULL);
|
---|
80 | else
|
---|
81 | RTAsn1ContentFree(&pThis->Asn1Core);
|
---|
82 | pThis->fValue = fValue;
|
---|
83 | pThis->Asn1Core.uData.pv = (void *)(fValue ? &g_bTrue : &g_bFalse);
|
---|
84 | pThis->Asn1Core.cb = 1;
|
---|
85 | pThis->Asn1Core.fFlags &= ~RTASN1CORE_F_DEFAULT;
|
---|
86 | pThis->Asn1Core.fFlags |= RTASN1CORE_F_PRESENT;
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | /*
|
---|
93 | * ASN.1 BOOLEAN - Standard Methods.
|
---|
94 | */
|
---|
95 |
|
---|
96 | RT_DECL_DATA_CONST(RTASN1COREVTABLE const) g_RTAsn1Boolean_Vtable =
|
---|
97 | {
|
---|
98 | "RTAsn1Boolean",
|
---|
99 | sizeof(RTASN1BOOLEAN),
|
---|
100 | ASN1_TAG_BOOLEAN,
|
---|
101 | ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
|
---|
102 | 0,
|
---|
103 | (PFNRTASN1COREVTDTOR)RTAsn1Boolean_Delete,
|
---|
104 | NULL,
|
---|
105 | (PFNRTASN1COREVTCLONE)RTAsn1Boolean_Clone,
|
---|
106 | (PFNRTASN1COREVTCOMPARE)RTAsn1Boolean_Compare,
|
---|
107 | (PFNRTASN1COREVTCHECKSANITY)RTAsn1Boolean_CheckSanity,
|
---|
108 | NULL,
|
---|
109 | NULL
|
---|
110 | };
|
---|
111 |
|
---|
112 |
|
---|
113 | RTDECL(int) RTAsn1Boolean_Init(PRTASN1BOOLEAN pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
114 | {
|
---|
115 | RT_NOREF_PV(pAllocator);
|
---|
116 | RTAsn1Core_InitEx(&pThis->Asn1Core, ASN1_TAG_BOOLEAN, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE,
|
---|
117 | &g_RTAsn1Boolean_Vtable, RTASN1CORE_F_PRESENT | RTASN1CORE_F_PRIMITE_TAG_STRUCT);
|
---|
118 | pThis->fValue = true;
|
---|
119 | pThis->Asn1Core.cb = 1;
|
---|
120 | pThis->Asn1Core.uData.pv = (void *)&g_bTrue;
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(int) RTAsn1Boolean_Clone(PRTASN1BOOLEAN pThis, PCRTASN1BOOLEAN pSrc, PCRTASN1ALLOCATORVTABLE pAllocator)
|
---|
126 | {
|
---|
127 | AssertPtr(pSrc); AssertPtr(pThis); AssertPtr(pAllocator);
|
---|
128 | RT_ZERO(*pThis);
|
---|
129 | if (RTAsn1Boolean_IsPresent(pSrc))
|
---|
130 | {
|
---|
131 | AssertReturn(pSrc->Asn1Core.pOps == &g_RTAsn1Boolean_Vtable, VERR_INTERNAL_ERROR_3);
|
---|
132 | AssertReturn(pSrc->Asn1Core.cb <= 1, VERR_INTERNAL_ERROR_4);
|
---|
133 |
|
---|
134 | int rc;
|
---|
135 | if ( pSrc->Asn1Core.cb == 1
|
---|
136 | && pSrc->Asn1Core.uData.pu8[0] != 0x00
|
---|
137 | && pSrc->Asn1Core.uData.pu8[0] != 0xff)
|
---|
138 | {
|
---|
139 | /* DER/CER incompatible value must be copied as-is. */
|
---|
140 | rc = RTAsn1Core_CloneContent(&pThis->Asn1Core, &pSrc->Asn1Core, pAllocator);
|
---|
141 | if (RT_FAILURE(rc))
|
---|
142 | return rc;
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | /* No value or one of the standard values. */
|
---|
147 | rc = RTAsn1Core_CloneNoContent(&pThis->Asn1Core, &pSrc->Asn1Core);
|
---|
148 | if (RT_FAILURE(rc))
|
---|
149 | return rc;
|
---|
150 | pThis->Asn1Core.uData.pv = (void *)(pSrc->fValue ? &g_bTrue : &g_bFalse);
|
---|
151 | }
|
---|
152 | pThis->fValue = pSrc->fValue;
|
---|
153 | }
|
---|
154 | return VINF_SUCCESS;
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | RTDECL(void) RTAsn1Boolean_Delete(PRTASN1BOOLEAN pThis)
|
---|
159 | {
|
---|
160 | if ( pThis
|
---|
161 | && RTAsn1Boolean_IsPresent(pThis))
|
---|
162 | {
|
---|
163 | Assert(pThis->Asn1Core.pOps == &g_RTAsn1Boolean_Vtable);
|
---|
164 | Assert(pThis->Asn1Core.cb <= 1);
|
---|
165 |
|
---|
166 | RTAsn1ContentFree(&pThis->Asn1Core);
|
---|
167 | RT_ZERO(*pThis);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | RTDECL(int) RTAsn1Boolean_Enum(PRTASN1BOOLEAN pThis, PFNRTASN1ENUMCALLBACK pfnCallback, uint32_t uDepth, void *pvUser)
|
---|
173 | {
|
---|
174 | Assert(pThis && (!RTAsn1Boolean_IsPresent(pThis) || pThis->Asn1Core.pOps == &g_RTAsn1Boolean_Vtable));
|
---|
175 | RT_NOREF_PV(pThis); RT_NOREF_PV(pfnCallback); RT_NOREF_PV(uDepth); RT_NOREF_PV(pvUser);
|
---|
176 |
|
---|
177 | /* No children to enumerate. */
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | RTDECL(int) RTAsn1Boolean_Compare(PCRTASN1BOOLEAN pLeft, PCRTASN1BOOLEAN pRight)
|
---|
183 | {
|
---|
184 | Assert(pLeft && (!RTAsn1Boolean_IsPresent(pLeft) || pLeft->Asn1Core.pOps == &g_RTAsn1Boolean_Vtable));
|
---|
185 | Assert(pRight && (!RTAsn1Boolean_IsPresent(pRight) || pRight->Asn1Core.pOps == &g_RTAsn1Boolean_Vtable));
|
---|
186 |
|
---|
187 | int iDiff;
|
---|
188 | if (RTAsn1Boolean_IsPresent(pLeft))
|
---|
189 | {
|
---|
190 | if (RTAsn1Boolean_IsPresent(pRight))
|
---|
191 | iDiff = (int)pLeft->fValue - (int)pRight->fValue;
|
---|
192 | else
|
---|
193 | iDiff = -1;
|
---|
194 | }
|
---|
195 | else
|
---|
196 | iDiff = 0 - (int)RTAsn1Boolean_IsPresent(pRight);
|
---|
197 | return iDiff;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | RTDECL(int) RTAsn1Boolean_CheckSanity(PCRTASN1BOOLEAN pThis, uint32_t fFlags, PRTERRINFO pErrInfo, const char *pszErrorTag)
|
---|
202 | {
|
---|
203 | if (RT_UNLIKELY(!RTAsn1Boolean_IsPresent(pThis)))
|
---|
204 | return RTErrInfoSetF(pErrInfo, VERR_ASN1_NOT_PRESENT, "%s: Missing (BOOLEAN).", pszErrorTag);
|
---|
205 | RT_NOREF_PV(fFlags);
|
---|
206 | return VINF_SUCCESS;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /*
|
---|
211 | * Generate code for the associated collection types.
|
---|
212 | */
|
---|
213 | #define RTASN1TMPL_TEMPLATE_FILE "../common/asn1/asn1-ut-boolean-template.h"
|
---|
214 | #include <iprt/asn1-generator-internal-header.h>
|
---|
215 | #include <iprt/asn1-generator-core.h>
|
---|
216 | #include <iprt/asn1-generator-init.h>
|
---|
217 | #include <iprt/asn1-generator-sanity.h>
|
---|
218 |
|
---|