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 79563 2017-09-20 18:52:46Z gouriano $
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(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);
293  void ExpectSysTagByte(TByte byte);
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 
312  TByte FlushTag(void);
313  void ExpectIndefiniteLength(void);
314  bool PeekIndefiniteLength(void);
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);
327  Uint1 ReadByte(void);
328  Int1 ReadSByte(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  void SkipBytes(size_t count);
334 
335  void ReadStringValue(size_t length, string& s, EFixNonPrint fix_type);
336  void SkipTagData(void);
337  bool HaveMoreElements(void);
338  void UnexpectedMember(TLongTag tag, const CItemsInfo& items);
339  void UnexpectedByte(TByte byte);
340  void GetTagPattern(vector<int>& pattern, size_t max_length);
341 
343 };
344 
345 
346 /* @} */
347 
348 
350 
352 
353 #endif /* OBJISTRASNB__HPP */
bitvector Bit-vector container with runtime compression of bits
Definition: bm.h:108
virtual void EndChoiceVariant(void)
Definition: objistr.cpp:1456
virtual void SkipString(EStringType type=eStringTypeVisible)=0
CObjectIStream –.
Definition: objistr.hpp:92
virtual void SkipSNumber(void)=0
virtual void EndClassMember(void)
Definition: objistr.cpp:1350
unsigned NCBI_INT8_TYPE Uint8
Unsigned 8 byte sized integer.
Definition: ncbitype.h:146
signed int Int4
Alias for signed int.
Definition: ncbitype.h:120
virtual bool BeginContainerElement(TTypeInfo elementType)=0
unsigned int Uint4
Alias for unsigned int.
Definition: ncbitype.h:121
virtual Int4 ReadInt4(void)
Definition: objistr.cpp:1693
virtual void BeginClass(const CClassTypeInfo *classInfo)=0
EStringType
String type.
Definition: serialdef.hpp:184
virtual void SkipNull(void)=0
virtual void ReadNull(void)=0
MLIOVIR void SkipClassRandom(const CClassTypeInfo *classType)
Definition: objistr.cpp:1408
virtual void SkipByteBlock(void)=0
virtual void SkipFNumber(void)=0
virtual string ReadOtherPointer(void)=0
MLIOVIR void SkipContainer(const CContainerTypeInfo *containerType)
Definition: objistr.cpp:1327
virtual void BeginChars(CharBlock &block)=0
MLIOVIR void SkipChoiceSimple(const CChoiceTypeInfo *choiceType)
Definition: objistr.cpp:1481
virtual void SkipBitString(void)=0
signed NCBI_INT8_TYPE Int8
Signed 8 byte sized integer.
Definition: ncbitype.h:143
Serializable object that stores any combination of parsable data.
Definition: serialbase.hpp:263
virtual void EndClass(void)
Definition: objistr.cpp:1346
virtual void SkipPointer(TTypeInfo declaredType)
Definition: objistr.cpp:1212
virtual void ReadPackedString(string &s, CPackString &pack_string, EStringType type=eStringTypeVisible)
Definition: objistr.cpp:1740
virtual pair< TObjectPtr, TTypeInfo > ReadPointer(TTypeInfo declaredType)
Definition: objistr.cpp:1111
virtual TObjectIndex ReadObjectPointer(void)=0
virtual TMemberIndex BeginClassMember(const CClassTypeInfo *classType)=0
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:101
unsigned char Uint1
Alias for unsigned char.
Definition: ncbitype.h:117
vector< Int8 > m_DataLimits
virtual set< TTypeInfo > GuessDataType(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:889
const char * tag
int TEnumValueType
Definition: serialdef.hpp:231
size_t TMemberIndex
Type used for indexing class members and choice variants.
Definition: serialdef.hpp:229
size_t TObjectIndex
Definition: objistr.hpp:1011
#define NCBI_DEPRECATED_CTOR(decl)
Macro used to mark a constructor as deprecated.
Definition: ncbimisc.hpp:1118
MLIOVIR void ReadChoiceSimple(const CChoiceTypeInfo *choiceType, TObjectPtr choicePtr)
Definition: objistr.cpp:1460
pair< size_t, bool > ReadBytes(CStreamBuffer &b, char *dst, size_t length)
Definition: test.cpp:264
MLIOVIR void ReadClassRandom(const CClassTypeInfo *classType, TObjectPtr classPtr)
Definition: objistr.cpp:1354
virtual size_t ReadChars(CharBlock &block, char *buffer, size_t count)=0
Definition: type.c:5
virtual void SkipStringStore(void)
Definition: objistr.cpp:1810
virtual void EndBytes(const ByteBlock &block)
Definition: objistr.cpp:1649
CObjectIStreamAsnBinary –.
Definition: objistrasnb.hpp:57
virtual char * ReadCString(void)
Definition: objistr.cpp:1728
virtual double ReadDouble(void)=0
virtual void SkipAnyContentVariant(void)
Definition: objistr.cpp:1815
int size
VisibleString (in ASN.1 sense)
Definition: serialdef.hpp:185
virtual void SkipBool(void)=0
#define NCBI_XSERIAL_EXPORT
Definition: ncbi_export.h:1410
virtual void EndContainer(void)=0
virtual void ResetState(void) override
Definition: objistr.cpp:474
MLIOVIR void ReadClassSequential(const CClassTypeInfo *classType, TObjectPtr classPtr)
Definition: objistr.cpp:1376
virtual void BeginNamedType(TTypeInfo namedTypeInfo)
Definition: objistr.cpp:1254
MLIOVIR void SkipNamedType(TTypeInfo namedTypeInfo, TTypeInfo typeInfo)
Definition: objistr.cpp:1280
virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo *choiceType)=0
virtual EPointerType ReadPointerType(void)=0
static void byte(MDB_val *v)
Definition: mdb_dump.c:81
virtual Int8 ReadInt8(void)=0
void ReadString(CStreamBuffer &b)
Definition: test.cpp:196
virtual void BeginBytes(ByteBlock &block)=0
NLM_EXTERN Nlm_CharPtr LIBCALL SkipChar(char FAR *theString, char Char)
virtual Uint8 ReadUint8(void)=0
signed char Int1
Alias for signed char.
Definition: ncbitype.h:116
virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues &values)=0
virtual void EndChoice(void)
Definition: objistr.cpp:1453
#define ReadLength
virtual void BeginContainer(const CContainerTypeInfo *containerType)=0
virtual Uint4 ReadUint4(void)
Definition: objistr.cpp:1702
MLIOVIR void SkipClassSequential(const CClassTypeInfo *classType)
Definition: objistr.cpp:1429
CTypeInfo class contains all information about C++ types (both basic and classes): members and layout...
Definition: typeinfo.hpp:74
virtual void SkipAnyContentObject(void)=0
enum ENcbiOwnership EOwnership
Ownership relations between objects.
virtual void BeginChoice(const CChoiceTypeInfo *choiceType)
Definition: objistr.cpp:1450
MLIOVIR void ReadNamedType(TTypeInfo namedTypeInfo, TTypeInfo typeInfo, TObjectPtr object)
Definition: objistr.cpp:1262
CObjectOStreamAsnBinary –.
Definition: objostrasnb.hpp:56
std::istream & in(std::istream &in_, double &x_)
CNcbiIstream & ReadContainer(CNcbiIstream &is, TCont &cont)
Read a container from a stream.
Definition: cgi_serial.hpp:234
static const char * str(char *buf, int n)
Definition: stats.c:84
Definition: set.hpp:44
virtual void ReadStringStore(string &s)
Definition: objistr.cpp:1735
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:141
void * TObjectPtr
Definition: serialdef.hpp:55
virtual void ReadAnyContentObject(CAnyContentObject &obj)=0
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:98
virtual void SkipUNumber(void)=0
virtual char ReadChar(void)=0
static uschar * buffer
Definition: pcretest.c:187
virtual void EndChars(const CharBlock &block)
Definition: objistr.cpp:1653
EFixNonPrint
How to process non-printing character in the ASN VisibleString.
Definition: serialdef.hpp:173
virtual void ReadOtherPointerEnd(void)
Definition: objistr.cpp:1201
virtual void ReadBitString(CBitString &obj)=0
virtual bool ReadBool(void)=0
virtual void EndNamedType(void)
Definition: objistr.cpp:1258
Modified on Sun May 20 14:21:35 2018 by modify_doxy.py rev. 546573