NCBI C++ ToolKit
objistrasnb.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef OBJISTRASNB__HPP
2 #define OBJISTRASNB__HPP
3 
4 /* $Id: objistrasnb.hpp 95875 2022-01-10 13:54:54Z grichenk $
5 * ===========================================================================
6 *
7 * PUBLIC DOMAIN NOTICE
8 * National Center for Biotechnology Information
9 *
10 * This software/database is a "United States Government Work" under the
11 * terms of the United States Copyright Act. It was written as part of
12 * the author's official duties as a United States Government employee and
13 * thus cannot be copyrighted. This software/database is freely available
14 * to the public for use. The National Library of Medicine and the U.S.
15 * Government have not placed any restriction on its use or reproduction.
16 *
17 * Although all reasonable efforts have been taken to ensure the accuracy
18 * and reliability of the software and data, the NLM and the U.S.
19 * Government do not and cannot warrant the performance or results that
20 * may be obtained by using this software or data. The NLM and the U.S.
21 * Government disclaim all warranties, express or implied, including
22 * warranties of performance, merchantability or fitness for any particular
23 * purpose.
24 *
25 * Please cite the author in any work or product based on this material.
26 *
27 * ===========================================================================
28 *
29 * Author: Eugene Vasilchenko
30 *
31 * File Description:
32 * Decode input data in ASN binary format (BER)
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/objistr.hpp>
38 #include <stack>
39 
40 
41 /** @addtogroup ObjStreamSupport
42  *
43  * @{
44  */
45 
46 #define USE_DEF_LEN 1
47 
49 
51 
52 /////////////////////////////////////////////////////////////////////////////
53 ///
54 /// CObjectIStreamAsnBinary --
55 ///
56 /// Decode input data in ASN.1 binary format (BER)
58  public CAsnBinaryDefs
59 {
60 public:
61 
62  /// Constructor.
63  ///
64  /// @param how
65  /// Defines how to fix unprintable characters in ASN VisiableString
67 
68  /// Constructor.
69  ///
70  /// @param in
71  /// input stream
72  /// @param how
73  /// Defines how to fix unprintable characters in ASN VisiableString
76 
77  /// Constructor.
78  ///
79  /// @param in
80  /// input stream
81  /// @param deleteIn
82  /// when TRUE, the input stream will be deleted automatically
83  /// when the reader is deleted
84  /// @param how
85  /// Defines how to fix unprintable characters in ASN VisiableString
86  /// @deprecated
87  /// Use one with EOwnership enum instead
89  bool deleteIn,
90  EFixNonPrint how = eFNP_Default));
91 
92  /// Constructor.
93  ///
94  /// @param in
95  /// input stream
96  /// @param deleteIn
97  /// When eTakeOwnership, the input stream will be deleted automatically
98  /// when the reader is deleted
99  /// @param how
100  /// Defines how to fix unprintable characters in ASN VisiableString
102  EOwnership deleteIn,
103  EFixNonPrint how = eFNP_Default);
104 
105  /// Constructor.
106  ///
107  /// @param reader
108  /// Data source
109  /// @param how
110  /// Defines how to fix unprintable characters in ASN VisiableString
112  EFixNonPrint how = eFNP_Default);
113 
114  /// Constructor.
115  ///
116  /// @param buffer
117  /// Data source memory buffer
118  /// @param size
119  /// Memory buffer size
120  /// @param how
121  /// Defines how to fix unprintable characters in ASN VisiableString
122  CObjectIStreamAsnBinary(const char* buffer,
123  size_t size,
124  EFixNonPrint how = eFNP_Default);
125 
126 
127  virtual set<TTypeInfo> GuessDataType(const set<TTypeInfo>& known_types,
128  size_t max_length = 16,
129  size_t max_bytes = 1024*1024) override;
130 
131  virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues& values) override;
132  virtual void ReadNull(void) override;
133 
134  virtual void ReadAnyContentObject(CAnyContentObject& obj) override;
135  void SkipAnyContent(void);
136  virtual void SkipAnyContentObject(void) override;
137  virtual void SkipAnyContentVariant(void) override;
138 
139  virtual void ReadBitString(CBitString& obj) override;
140  virtual void SkipBitString(void) override;
141 
142 protected:
143  virtual bool ReadBool(void) override;
144  virtual char ReadChar(void) override;
145  virtual Int4 ReadInt4(void) override;
146  virtual Uint4 ReadUint4(void) override;
147  virtual Int8 ReadInt8(void) override;
148  virtual Uint8 ReadUint8(void) override;
149  virtual double ReadDouble(void) override;
150  virtual void ReadString(string& s,EStringType type = eStringTypeVisible) override;
151  virtual void ReadPackedString(string& s,
152  CPackString& pack_string,
153  EStringType type) override;
154  virtual char* ReadCString(void) override;
155  virtual void ReadStringStore(string& s) override;
156 
157  virtual void SkipBool(void) override;
158  virtual void SkipChar(void) override;
159  virtual void SkipSNumber(void) override;
160  virtual void SkipUNumber(void) override;
161  virtual void SkipFNumber(void) override;
162  virtual void SkipString(EStringType type = eStringTypeVisible) override;
163  virtual void SkipStringStore(void) override;
164  virtual void SkipNull(void) override;
165  virtual void SkipByteBlock(void) override;
166 
167 #ifdef VIRTUAL_MID_LEVEL_IO
168  virtual void ReadNamedType(TTypeInfo namedTypeInfo,
169  TTypeInfo typeInfo, TObjectPtr object) override;
170  virtual void SkipNamedType(TTypeInfo namedTypeInfo,
171  TTypeInfo typeInfo) override;
172  virtual void ReadContainer(const CContainerTypeInfo* containerType,
173  TObjectPtr containerPtr) override;
174  virtual void SkipContainer(const CContainerTypeInfo* containerType) override;
175 
176  virtual void ReadClassSequential(const CClassTypeInfo* classType,
177  TObjectPtr classPtr) override;
178  virtual void ReadClassRandom(const CClassTypeInfo* classType,
179  TObjectPtr classPtr) override;
180  virtual void SkipClassSequential(const CClassTypeInfo* classType) override;
181  virtual void SkipClassRandom(const CClassTypeInfo* classType) override;
182 
183  virtual void ReadChoiceSimple(const CChoiceTypeInfo* choiceType,
184  TObjectPtr choicePtr) override;
185  virtual void SkipChoiceSimple(const CChoiceTypeInfo* choiceType) override;
186 
187 #endif
188 
189  // low level I/O
190  virtual void BeginNamedType(TTypeInfo namedTypeInfo) override;
191  virtual void EndNamedType(void) override;
192 
193  virtual void BeginContainer(const CContainerTypeInfo* containerType) override;
194  virtual void EndContainer(void) override;
195  virtual bool BeginContainerElement(TTypeInfo elementType) override;
196 
197  virtual void BeginClass(const CClassTypeInfo* classInfo) override;
198  virtual void EndClass(void) override;
199  virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType) override;
200  virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType,
201  TMemberIndex pos) override;
202  virtual void EndClassMember(void) override;
203 
204  virtual void BeginChoice(const CChoiceTypeInfo* choiceType) override;
205  virtual void EndChoice(void) override;
206  virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo* choiceType) override;
207  virtual void EndChoiceVariant(void) override;
208 
209  virtual void BeginBytes(ByteBlock& block) override;
210  virtual size_t ReadBytes(ByteBlock& block, char* dst, size_t length) override;
211  virtual void EndBytes(const ByteBlock& block) override;
212 
213  virtual void BeginChars(CharBlock& block) override;
214  virtual size_t ReadChars(CharBlock& block, char* dst, size_t length) override;
215  virtual void EndChars(const CharBlock& block) override;
216 
217 #if HAVE_NCBI_C
218  friend class CObjectIStream::AsnIo;
219 #endif
220  void ResetThisState(void);
221  virtual void ResetState(void) override;
222 
223 private:
224  virtual EPointerType ReadPointerType(void) override;
225  virtual TObjectIndex ReadObjectPointer(void) override;
226  virtual string ReadOtherPointer(void) override;
227  virtual void ReadOtherPointerEnd(void) override;
228  virtual pair<TObjectPtr, TTypeInfo> ReadPointer(TTypeInfo declaredType) override;
229  virtual void SkipPointer(TTypeInfo declaredType) override;
230 
231  bool SkipRealValue(void);
232 
233 #if CHECK_INSTREAM_STATE
234  enum ETagState {
235  eTagStart,
236  eTagParsed,
237  eLengthValue,
238  eData
239  };
240  // states:
241  // before StartTag (Peek*Tag/ExpectSysTag) tag:
242  // m_CurrentTagLength == 0
243  // stream position on tag start
244  // after Peek*Tag/ExpectSysTag tag:
245  // m_CurrentTagLength == beginning of LENGTH field
246  // stream position on tag start
247  // after FlushTag (Read*Length/ExpectIndefiniteLength):
248  // m_CurrentTagLength == 0
249  // stream position on tad DATA start
250  // tag limit is pushed on stack and new tag limit is updated
251  // after EndOfTag
252  // m_CurrentTagLength == 0
253  // stream position on tag DATA end
254  // tag limit is popped from stack
255  // m_CurrentTagLength == beginning of LENGTH field
256  // -- after any of Peek?Tag or ExpectSysTag
257  //
258  ETagState m_CurrentTagState;
259 #endif
260 #if CHECK_INSTREAM_LIMITS
261  Int8 m_CurrentTagLimit; // end of current tag data
262  stack<Int8> m_Limits;
263 #endif
264  size_t m_CurrentTagLength; // length of tag header (without length field)
266 #if USE_DEF_LEN
268  vector<Int8> m_DataLimits;
269 #endif
270 
271  // low level interface
272 private:
273  TByte PeekByte(size_t index = 0);
274  TByte PeekTagByte(size_t index = 0);
275  TByte StartTag(TByte first_tag_byte);
276  TLongTag PeekTag(TByte first_tag_byte);
277  TLongTag PeekLongTag(void);
278  void ExpectTagClassByte(TByte first_tag_byte,
279  TByte expected_class_byte);
280  void UnexpectedTagClassByte(TByte first_tag_byte,
281  TByte expected_class_byte);
282  void UnexpectedShortLength(size_t got_length,
283  size_t expected_length);
284  void UnexpectedFixedLength(void);
285  void UnexpectedContinuation(void);
286  void UnexpectedLongLength(void);
287  void UnexpectedTagValue(ETagClass tag_class, TLongTag tag_got, TLongTag tag_expected);
288  TLongTag PeekTag(TByte first_tag_byte,
289  ETagClass tag_class,
290  ETagConstructed tag_constructed);
291  string PeekClassTag(void);
292  TByte PeekAnyTagFirstByte(void);
294  void ExpectStringTag(EStringType type);
295  string TagToString(TByte byte);
296  void UnexpectedSysTagByte(TByte byte);
297  void ExpectSysTag(ETagClass tag_class,
298  ETagConstructed tag_constructed,
299  ETagValue tag_value);
300  void ExpectSysTag(ETagValue tag_value);
301  void ExpectIntegerTag(void);
302 
303  void ExpectTag(ETagClass tag_class,
304  ETagConstructed tag_constructed,
305  TLongTag tag_value);
306  void ExpectTag(TByte first_tag_byte,
307  ETagClass tag_class,
308  ETagConstructed tag_constructed,
309  TLongTag tag_value);
310  void UndoPeekTag(void);
311 
315  void ExpectContainer(bool random);
316 public:
317  size_t ReadShortLength(void);
318 private:
319  size_t ReadLength(void);
320  size_t ReadLengthInlined(void);
321  size_t ReadLengthLong(TByte byte);
322  size_t StartTagData(size_t length);
323  void ExpectShortLength(size_t length);
324  void ExpectEndOfContent(void);
325 public:
326  void EndOfTag(void);
329  void ExpectByte(Uint1 byte);
330 private:
331  void ReadBytes(char* buffer, size_t count);
332  void ReadBytes(string& str, size_t count);
333  bool FixVisibleChars(char* buffer, size_t& count, EFixNonPrint fix_method);
334  bool FixVisibleChars(string& str, EFixNonPrint fix_method);
335  void SkipBytes(size_t count);
336 
337  void ReadStringValue(size_t length, string& s, EFixNonPrint fix_type);
338  void SkipTagData(void);
339  bool HaveMoreElements(void);
340  void UnexpectedMember(TLongTag tag, const CItemsInfo& items);
341  void UnexpectedByte(TByte byte);
342  void GetTagPattern(vector<int>& pattern, size_t max_length);
343 
345 };
346 
347 
348 /* @} */
349 
350 
352 
354 
355 #endif /* OBJISTRASNB__HPP */
pair< size_t, bool > ReadBytes(CStreamBuffer &b, char *dst, size_t length)
Definition: test.cpp:264
void ReadString(CStreamBuffer &b)
Definition: test.cpp:196
CNcbiIstream & ReadContainer(CNcbiIstream &is, TCont &cont)
Read a container from a stream.
Definition: cgi_serial.hpp:234
Serializable object that stores any combination of parsable data.
Definition: serialbase.hpp:264
CObjectIStreamAsnBinary –.
Definition: objistrasnb.hpp:59
CObjectIStream –.
Definition: objistr.hpp:93
CObjectOStreamAsnBinary –.
Definition: objostrasnb.hpp:58
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:76
Definition: set.hpp:45
Include a standard set of the NCBI C++ Toolkit most basic headers.
NLM_EXTERN Nlm_CharPtr LIBCALL SkipChar(char FAR *theString, char Char)
#define NCBI_DEPRECATED_CTOR(decl)
Macro used to mark a constructor as deprecated.
Definition: ncbimisc.hpp:1209
EFixNonPrint
How to process non-printing character in the ASN VisibleString.
Definition: serialdef.hpp:173
void * TObjectPtr
Definition: serialdef.hpp:55
int TEnumValueType
Definition: serialdef.hpp:232
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:230
EStringType
String type.
Definition: serialdef.hpp:185
@ eFNP_Default
Definition: serialdef.hpp:181
@ eStringTypeVisible
VisibleString (in ASN.1 sense)
Definition: serialdef.hpp:186
vector< Int8 > m_DataLimits
void ExpectContainer(bool random)
virtual void ReadStringStore(string &s)
Definition: objistr.cpp:1757
virtual EPointerType ReadPointerType(void)=0
virtual bool ReadBool(void)=0
void ExpectByte(Uint1 byte)
virtual void EndClassMember(void)
Definition: objistr.cpp:1372
void ExpectSysTagByte(TByte byte)
virtual set< TTypeInfo > GuessDataType(const set< TTypeInfo > &known_types, size_t max_length=16, size_t max_bytes=1024 *1024)
Identify the type of data in the stream.
Definition: objistr.cpp:911
TByte StartTag(TByte first_tag_byte)
MLIOVIR void ReadClassSequential(const CClassTypeInfo *classType, TObjectPtr classPtr)
Definition: objistr.cpp:1398
size_t ReadShortLength(void)
MLIOVIR void SkipChoiceSimple(const CChoiceTypeInfo *choiceType)
Definition: objistr.cpp:1503
virtual void BeginContainer(const CContainerTypeInfo *containerType) override
virtual void ReadNull(void)=0
bool PeekIndefiniteLength(void)
virtual void EndBytes(const ByteBlock &block)
Definition: objistr.cpp:1671
TLongTag PeekTag(TByte first_tag_byte, ETagClass tag_class, ETagConstructed tag_constructed)
virtual void SkipByteBlock(void)=0
virtual TMemberIndex BeginClassMember(const CClassTypeInfo *classType)=0
virtual size_t ReadChars(CharBlock &block, char *buffer, size_t count)=0
virtual string ReadOtherPointer(void)=0
virtual void SkipString(EStringType type=eStringTypeVisible)=0
virtual void SkipFNumber(void)=0
virtual void ReadOtherPointerEnd(void)
Definition: objistr.cpp:1223
virtual void BeginBytes(ByteBlock &block)=0
void ExpectSysTag(ETagClass tag_class, ETagConstructed tag_constructed, ETagValue tag_value)
virtual void BeginNamedType(TTypeInfo namedTypeInfo)
Definition: objistr.cpp:1276
virtual double ReadDouble(void)=0
virtual void ResetState(void) override
Definition: objistr.cpp:486
virtual void SkipBool(void)=0
void ExpectSysTag(ETagValue tag_value)
void ExpectTagClassByte(TByte first_tag_byte, TByte expected_class_byte)
size_t ReadLengthInlined(void)
virtual void EndNamedType(void)
Definition: objistr.cpp:1280
virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo *choiceType)=0
void ExpectShortLength(size_t length)
virtual void SkipSNumber(void)=0
virtual void SkipUNumber(void)=0
virtual Uint4 ReadUint4(void)
Definition: objistr.cpp:1724
virtual char * ReadCString(void)
Definition: objistr.cpp:1750
virtual void SkipAnyContentVariant(void)
Definition: objistr.cpp:1837
virtual void SkipNull(void)=0
virtual void SkipStringStore(void)
Definition: objistr.cpp:1832
virtual void BeginChars(CharBlock &block)=0
virtual void ReadPackedString(string &s, CPackString &pack_string, EStringType type=eStringTypeVisible)
Definition: objistr.cpp:1762
TByte PeekByte(size_t index=0)
void ExpectTag(TByte first_tag_byte, ETagClass tag_class, ETagConstructed tag_constructed, TLongTag tag_value)
virtual char ReadChar(void)=0
virtual void SkipPointer(TTypeInfo declaredType)
Definition: objistr.cpp:1234
virtual void ReadAnyContentObject(CAnyContentObject &obj)=0
virtual bool BeginContainerElement(TTypeInfo elementType) override
MLIOVIR void ReadChoiceSimple(const CChoiceTypeInfo *choiceType, TObjectPtr choicePtr)
Definition: objistr.cpp:1482
virtual void ReadBitString(CBitString &obj)=0
virtual pair< TObjectPtr, TTypeInfo > ReadPointer(TTypeInfo declaredType)
Definition: objistr.cpp:1133
virtual void SkipBitString(void)=0
TByte PeekTagByte(size_t index=0)
void ExpectEndOfContent(void)
virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues &values)=0
size_t StartTagData(size_t length)
MLIOVIR void ReadNamedType(TTypeInfo namedTypeInfo, TTypeInfo typeInfo, TObjectPtr object)
Definition: objistr.cpp:1284
size_t TObjectIndex
Definition: objistr.hpp:1028
virtual Int8 ReadInt8(void)=0
virtual TObjectIndex ReadObjectPointer(void)=0
MLIOVIR void SkipContainer(const CContainerTypeInfo *containerType)
Definition: objistr.cpp:1349
MLIOVIR void SkipClassSequential(const CClassTypeInfo *classType)
Definition: objistr.cpp:1451
virtual Int4 ReadInt4(void)
Definition: objistr.cpp:1715
virtual void SkipAnyContentObject(void)=0
TLongTag PeekTag(TByte first_tag_byte)
void ExpectIndefiniteLength(void)
virtual Uint8 ReadUint8(void)=0
virtual void EndClass(void)
Definition: objistr.cpp:1368
virtual void EndChoiceVariant(void)
Definition: objistr.cpp:1478
virtual void BeginClass(const CClassTypeInfo *classInfo)=0
virtual void EndChars(const CharBlock &block)
Definition: objistr.cpp:1675
virtual void EndContainer(void) override
void ExpectTag(ETagClass tag_class, ETagConstructed tag_constructed, TLongTag tag_value)
virtual void EndChoice(void)
Definition: objistr.cpp:1475
MLIOVIR void SkipClassRandom(const CClassTypeInfo *classType)
Definition: objistr.cpp:1430
MLIOVIR void SkipNamedType(TTypeInfo namedTypeInfo, TTypeInfo typeInfo)
Definition: objistr.cpp:1302
virtual void BeginChoice(const CChoiceTypeInfo *choiceType)
Definition: objistr.cpp:1472
MLIOVIR void ReadClassRandom(const CClassTypeInfo *classType, TObjectPtr classPtr)
Definition: objistr.cpp:1376
uint8_t Uint1
1-byte (8-bit) unsigned integer
Definition: ncbitype.h:99
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
int8_t Int1
1-byte (8-bit) signed integer
Definition: ncbitype.h:98
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
enum ENcbiOwnership EOwnership
Ownership relations between objects.
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1435
const struct ncbi::grid::netcache::search::fields::SIZE size
const char * tag
std::istream & in(std::istream &in_, double &x_)
#define ReadLength
static pcre_uint8 * buffer
Definition: pcretest.c:1051
static const char * str(char *buf, int n)
Definition: stats.c:84
Definition: type.c:6
Modified on Mon Feb 26 04:03:52 2024 by modify_doxy.py rev. 669887