VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-ut-boolean.cpp@ 78098

Last change on this file since 78098 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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