VirtualBox

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

Last change on this file since 56290 was 56290, checked in by vboxsync, 10 years ago

IPRT: Updated (C) year.

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