Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cASN.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cASN_H
7 #define _INC_cASN_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "../GrayLibBase.h"
14 #include "GrayCore/include/cMem.h"
15 #include "GrayCore/include/cHeap.h"
16 #include "GrayCore/include/StrT.h"
18 
19 namespace GrayLib
20 {
21  enum ASN_TYPE
22  {
34 
35  ASN_Unknown = 0x00,
36  ASN_BOOLEAN = 0x01,
37  ASN_INTEGER = 0x02,
38  ASN_ArrayBits = 0x03,
39  ASN_ArrayBytes = 0x04,
40  ASN_NULL = 0x05,
41  ASN_OID = 0x06,
42  // 7 = Object Descriptor.
43  // 8 = InstanceOf, External.
44  ASN_REAL = 0x09,
45  ASN_ENUMERATED = 0x0a,
46  // 11 = EMBEDDED PDV.
47  ASN_StringUTF8 = 0x0C,
48  // 13 = Relative-OID
49  // 14 = Reserved?
50  // 15 = Reserved?
51  ASN_SEQUENCE = 0x10, // 16 = We have child blocks.
52  ASN_SET = 0x11, // 17 = An ASN_OID, value pair.
53  // NumericString = 0x12, // 18
54  ASN_StringPrintable = 0x13, // 19 PRINTABLE_STRING
55  ASN_StringT61 = 0x14, // 20 = TeletexString, T61String
56  // 0x15 = 21 = VideotexString
57  ASN_StringIA5 = 0x16, // 22 = IA5String
58  ASN_TimeUTC = 0x17, // 23 = UTCTime
59  ASN_TimeGeneral = 0x18, // 24 = GeneralizedTime, 32 bit year.
60  // ASN_GraphicString // 25 = GraphicString
61  // 26 = VisibleString, ISO646String
62  // 27 = GeneralString
63  ASN_StringUniversal = 0x1C, // 28 = UniversalString
64  // 29 = CharacterString
65  ASN_Bitmap = 0x1E, // 30 = BMPString
66 
67  // Mask Bits.
68  ASNF_MASK = 0x1F,
69  ASNF_CONSTRUCTED = 0x20, // bitmask. indicates known format of child blocks.
72  // ASNF_PRIVATE = ASNF_CONSTRUCTED | ASNF_CONTEXT_SPECIFIC,
73  };
74 
75  typedef BYTE ASN_t; // ASN_TYPE fits in here.
76 
78  {
85 
86  typedef cMemBlock SUPER_t;
87 
88  public:
90 
91  static const char* const k_szTypeName[ASNF_MASK];
92 
93  public:
94  cASNBuf() noexcept
95  : m_eASNTag(ASN_Unknown) // 0
96  {
97  }
98 
99  ASN_TYPE get_ASNType() const noexcept
100  {
103  return (ASN_TYPE)(m_eASNTag&ASNF_MASK);
104  }
105 
106  bool isEmptyBuf() const
107  {
108  if (m_eASNTag != ASN_NULL && m_eASNTag != ASN_Unknown)
109  return false;
110  if (get_DataSize() != 0)
111  return false;
112  return true;
113  }
114 
115  bool isSequence() const noexcept
116  {
118  ASN_TYPE e = get_ASNType();
119  if (e == ASN_SEQUENCE || e == ASN_SET)
120  return true;
121  return false;
122  }
123 
124  bool isPrintable() const noexcept
125  {
127  return(m_eASNTag == ASN_StringUTF8 || m_eASNTag == ASN_StringPrintable);
128  }
129 
130  bool IsEqualBuf(const cASNBuf& b) const
131  {
133  return (m_eASNTag == b.m_eASNTag && IsEqualData(&b));
134  }
135 
136  bool IsEqualStr(const void* pStr, size_t nSize) const
137  {
139  if (this->get_DataSize() != nSize)
140  {
141  return false;
142  }
143  if (StrT::CmpIN<char>(this->get_DataA(), (const char*)pStr, (StrLen_t)nSize) == COMPARE_Equal)
144  {
145  return true;
146  }
147  return false;
148  }
149 
150  bool IsEqualStr(const cASNBuf& b) const
151  {
154 
155  if (IsEqualBuf(b)) // exact same.
156  return true;
157  if (!this->isPrintable() || !b.isPrintable())
158  return false;
159  return IsEqualStr(b.get_DataBytes(), b.get_DataSize());
160  }
161 
162  void SetEmptyBuf()
163  {
164  m_eASNTag = ASN_Unknown;
165  SUPER_t::SetEmptyBlock();
166  }
167 
168  bool get_Bool() const
169  {
170  ASSERT(m_eASNTag == ASN_BOOLEAN);
171  ASSERT(get_DataSize() > 0);
172  const BYTE bVal = *get_DataBytes();
173  return (bVal == 0) ? false : true;
174  }
175 
176  int get_Int() const;
177 
178  HRESULT GetDescriptionStr(StrBuilder& s) const;
179 
181  };
182 
184  {
188 
189  typedef cASNBuf SUPER_t;
190 
191  private:
192  bool m_bAllocated;
193 
194  private:
195  void SetEmptyBlock() IS_DELETE; // prevent use of this.
196  void SetBlock(void* pData, size_t nSize) IS_DELETE;
197 
198  public:
199  cASNBufAlloc() noexcept
200  : m_bAllocated(false)
201  {
202  }
204  {
205  SetEmptyBuf();
206  }
207 
208  void SetEmptyBuf()
209  {
211  if (m_bAllocated)
212  {
213  cHeap::FreePtr(get_DataBytes());
214  m_bAllocated = false;
215  }
216  SUPER_t::SetEmptyBuf(); // __super
217  }
218 
219  bool SetAlloc(ASN_TYPE eASNTag, size_t nSize, const void* pData = nullptr)
220  {
221  // I allocate space for the value. I must also free it.
222  if (!m_bAllocated)
223  {
224  SUPER_t::SetEmptyBuf(); // __super
225  m_bAllocated = true;
226  }
227  void* pDataNew = cHeap::ReAllocPtr(m_pData, nSize);
228  if (pDataNew == nullptr)
229  return false;
230 
231  m_eASNTag = eASNTag;
232 
233  if (pData != nullptr)
234  {
235  ::memcpy(pDataNew, pData, nSize);
236  }
237  SUPER_t::SetBlock(pDataNew, nSize);
238  return true;
239  }
240  };
241 }
242 #endif
#define IS_DELETE
Definition: GrayCore.h:67
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
Definition: cASN.h:184
void SetEmptyBuf()
Definition: cASN.h:208
~cASNBufAlloc()
Definition: cASN.h:203
cASNBufAlloc() noexcept
Definition: cASN.h:199
bool SetAlloc(ASN_TYPE eASNTag, size_t nSize, const void *pData=nullptr)
Definition: cASN.h:219
Definition: cASN.h:78
ASN_TYPE get_ASNType() const noexcept
Definition: cASN.h:99
cASNBuf() noexcept
Definition: cASN.h:94
bool isPrintable() const noexcept
Definition: cASN.h:124
bool isEmptyBuf() const
Definition: cASN.h:106
bool get_Bool() const
Definition: cASN.h:168
bool IsEqualStr(const void *pStr, size_t nSize) const
Definition: cASN.h:136
void SetEmptyBuf()
Definition: cASN.h:162
ASN_TYPE m_eASNTag
ASN1 tag defines the data type, e.g. ASN_StringUTF8. ASN_TYPE byte mask.
Definition: cASN.h:89
bool IsEqualStr(const cASNBuf &b) const
Definition: cASN.h:150
bool isSequence() const noexcept
Definition: cASN.h:115
bool IsEqualBuf(const cASNBuf &b) const
Definition: cASN.h:130
Definition: StrBuilder.h:18
Definition: cMem.h:311
BYTE * get_DataBytes() const noexcept
Definition: cMem.h:354
size_t get_DataSize() const noexcept
Definition: cMem.h:344
Definition: cMesh.h:22
ASN_TYPE
Definition: cASN.h:22
@ ASN_StringIA5
Definition: cASN.h:57
@ ASN_Bitmap
Definition: cASN.h:65
@ ASN_Unknown
AKA PRIMITIVE, UNIVERSAL (unknown type?) reserved for BER, End Of Contents or N/A.
Definition: cASN.h:35
@ ASN_ArrayBytes
OCTET STRING = Model binary data whose length is a multiple of eight.
Definition: cASN.h:39
@ ASN_TimeUTC
Definition: cASN.h:58
@ ASN_TimeGeneral
Definition: cASN.h:59
@ ASNF_MASK
Definition: cASN.h:68
@ ASNF_CONTEXT_SPECIFIC
Definition: cASN.h:71
@ ASN_StringT61
Definition: cASN.h:55
@ ASNF_CONSTRUCTED
Definition: cASN.h:69
@ ASN_INTEGER
Definition: cASN.h:37
@ ASN_NULL
Definition: cASN.h:40
@ ASN_ENUMERATED
10 = Model values of variables with at least three states
Definition: cASN.h:45
@ ASN_StringUTF8
12 = UTF8String
Definition: cASN.h:47
@ ASN_OID
Name information objects.
Definition: cASN.h:41
@ ASNF_APPLICATION
Definition: cASN.h:70
@ ASN_SET
Definition: cASN.h:52
@ ASN_SEQUENCE
Definition: cASN.h:51
@ ASN_BOOLEAN
Definition: cASN.h:36
@ ASN_REAL
Model real float variable values.
Definition: cASN.h:44
@ ASN_StringPrintable
Definition: cASN.h:54
@ ASN_StringUniversal
Definition: cASN.h:63
@ ASN_ArrayBits
BIT STRING = Model binary data of arbitrary length.
Definition: cASN.h:38
BYTE ASN_t
Definition: cASN.h:75
int StrLen_t
the length of a string in chars (bytes for UTF8, wchar_t for UNICODE). or offset in characters....
Definition: StrConst.h:32
@ COMPARE_Equal
VARCMP_EQ.
Definition: cValT.h:23
static void *__stdcall ReAllocPtr(void *pData, size_t nSize)
Definition: cHeap.cpp:158
static void __stdcall FreePtr(void *pData)
Definition: cHeap.cpp:103