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