1 #ifndef OBJISTRASN__HPP
2 #define OBJISTRASN__HPP
3 
4 /*  $Id: objistrasn.hpp 546693 2017-09-20 17:04:38Z 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 text format
33 */
34 
35 #include <corelib/ncbistd.hpp>
36 #include <serial/objistr.hpp>
37 #include <corelib/tempstr.hpp>
38 
39 
40 /** @addtogroup ObjStreamSupport
41  *
42  * @{
43  */
44 
45 
46 BEGIN_NCBI_SCOPE
47 
48 /////////////////////////////////////////////////////////////////////////////
49 ///
50 /// CObjectIStreamAsn --
51 ///
52 /// Decode input data in ASN.1 text format
53 class NCBI_XSERIAL_EXPORT CObjectIStreamAsn : public CObjectIStream
54 {
55 public:
56     /// Constructor.
57     ///
58     /// @param how
59     ///   Defines how to fix unprintable characters in ASN VisiableString
60     CObjectIStreamAsn(EFixNonPrint how = eFNP_Default);
61 
62     /// Constructor.
63     ///
64     /// @param in
65     ///   input stream
66     /// @param how
67     ///   Defines how to fix unprintable characters in ASN VisiableString
68     CObjectIStreamAsn(CNcbiIstream& in,
69                       EFixNonPrint how = eFNP_Default);
70 
71     /// Constructor.
72     ///
73     /// @param in
74     ///   input stream
75     /// @param deleteIn
76     ///   when TRUE, the input stream will be deleted automatically
77     ///   when the reader is deleted
78     /// @param how
79     ///   Defines how to fix unprintable characters in ASN VisiableString
80     /// @deprecated
81     ///   Use one with EOwnership enum instead
82     NCBI_DEPRECATED_CTOR(CObjectIStreamAsn(CNcbiIstream& in,
83                       bool deleteIn,
84                       EFixNonPrint how = eFNP_Default));
85 
86     /// Constructor.
87     ///
88     /// @param in
89     ///   input stream
90     /// @param deleteIn
91     ///   When eTakeOwnership, the input stream will be deleted automatically
92     ///   when the reader is deleted
93     /// @param how
94     ///   Defines how to fix unprintable characters in ASN VisiableString
95     CObjectIStreamAsn(CNcbiIstream& in,
96                       EOwnership deleteIn,
97                       EFixNonPrint how = eFNP_Default);
98 
99     /// Constructor.
100     ///
101     /// @param buffer
102     ///   Data source memory buffer
103     /// @param size
104     ///   Memory buffer size
105     /// @param how
106     ///   Defines how to fix unprintable characters in ASN VisiableString
107     CObjectIStreamAsn(const char* buffer, size_t size,
108                       EFixNonPrint how = eFNP_Default);
109 
110     /// Check if there is still some meaningful data that can be read;
111     /// this function will skip white spaces and comments
112     ///
113     /// @return
114     ///   TRUE if there is no more data
115     virtual bool EndOfData(void) override;
116 
117     /// Get current stream position as string.
118     /// Useful for diagnostic and information messages.
119     ///
120     /// @return
121     ///   string
122     virtual string GetPosition(void) const override;
123 
124 
125     virtual string ReadFileHeader(void) override;
126     virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues& values) override;
127     virtual void ReadNull(void) override;
128 
129     void ReadAnyContent(string& value);
130     virtual void ReadAnyContentObject(CAnyContentObject& obj) override;
131     void SkipAnyContent(void);
132     virtual void SkipAnyContentObject(void) override;
133 
134     virtual void ReadBitString(CBitString& obj) override;
135     virtual void SkipBitString(void) override;
136 
137 protected:
138     TObjectIndex ReadIndex(void);
139 
140     // action: read ID into local buffer
141     // return: ID pointer and length
142     // note: it is not zero ended
143     CTempString ScanEndOfId(bool isId);
144     CTempString ReadTypeId(char firstChar);
145     CTempString ReadMemberId(char firstChar);
146     CTempString ReadUCaseId(char firstChar);
147     CTempString ReadLCaseId(char firstChar);
148     CTempString ReadNumber(void);
149 
150     virtual bool ReadBool(void) override;
151     virtual char ReadChar(void) override;
152     virtual Int4 ReadInt4(void) override;
153     virtual Uint4 ReadUint4(void) override;
154     virtual Int8 ReadInt8(void) override;
155     virtual Uint8 ReadUint8(void) override;
156     virtual double ReadDouble(void) override;
157     virtual void ReadString(string& s,EStringType type = eStringTypeVisible) override;
158     void ReadStringValue(string& s, EFixNonPrint fix_method);
159     void FixInput(size_t count, EFixNonPrint fix_method, size_t line);
160 
161     virtual void SkipBool(void) override;
162     virtual void SkipChar(void) override;
163     virtual void SkipSNumber(void) override;
164     virtual void SkipUNumber(void) override;
165     virtual void SkipFNumber(void) override;
166     virtual void SkipString(EStringType type = eStringTypeVisible) override;
167     virtual void SkipNull(void) override;
168     virtual void SkipByteBlock(void) override;
169 
170 #ifdef VIRTUAL_MID_LEVEL_IO
171     virtual void ReadContainer(const CContainerTypeInfo* containerType,
172                                TObjectPtr containerPtr) override;
173     virtual void SkipContainer(const CContainerTypeInfo* containerType) override;
174 
175     virtual void ReadClassSequential(const CClassTypeInfo* classType,
176                                      TObjectPtr classPtr) override;
177     virtual void ReadClassRandom(const CClassTypeInfo* classType,
178                                  TObjectPtr classPtr) override;
179     virtual void SkipClassSequential(const CClassTypeInfo* classType) override;
180     virtual void SkipClassRandom(const CClassTypeInfo* classType) override;
181 #endif
182     // low level I/O
183     virtual void BeginContainer(const CContainerTypeInfo* containerType) override;
184     virtual void EndContainer(void) override;
185     virtual bool BeginContainerElement(TTypeInfo elementType) override;
186 
187     virtual void BeginClass(const CClassTypeInfo* classInfo) override;
188     virtual void EndClass(void) override;
189 
190     virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType) override;
191     virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType,
192                                           TMemberIndex pos) override;
193 
194     virtual void BeginChoice(const CChoiceTypeInfo* choiceType) override;
195     virtual void EndChoice(void) override;
196     virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo* choiceType) override;
197 
198     virtual void BeginBytes(ByteBlock& block) override;
199     int GetHexChar(void);
200     virtual size_t ReadBytes(ByteBlock& block, char* dst, size_t length) override;
201     virtual void EndBytes(const ByteBlock& block) override;
202 
203     virtual void BeginChars(CharBlock& block) override;
204     virtual size_t ReadChars(CharBlock& block, char* dst, size_t length) override;
205 
206 private:
207     virtual EPointerType ReadPointerType(void) override;
208     virtual TObjectIndex ReadObjectPointer(void) override;
209     virtual string ReadOtherPointer(void) override;
210     virtual void StartDelayBuffer(void) override;
211 
212     // low level methods
213     char GetChar(void);
214     char PeekChar(void);
215 
216     // parse methods
217     char GetChar(bool skipWhiteSpace);
218     char PeekChar(bool skipWhiteSpace);
219 
220     bool GetChar(char c, bool skipWhiteSpace = false);
221     void Expect(char c, bool skipWhiteSpace = false);
222     bool Expect(char charTrue, char charFalse, bool skipWhiteSpace = false);
223     void ExpectString(const char* s, bool skipWhiteSpace = false);
224 
225     static bool FirstIdChar(char c);
226     static bool IdChar(char c);
227 
228     void SkipEndOfLine(char c);
229     char SkipWhiteSpace(void);
230     char SkipWhiteSpaceAndGetChar(void);
231     void SkipComments(void);
232     void UnexpectedMember(const CTempString& id, const CItemsInfo& items);
233     void BadStringChar(size_t startLine, char c);
234     void UnendedString(size_t startLine);
235 
236     void AppendStringData(string& s,
237                           size_t count,
238                           EFixNonPrint fix_method,
239                           size_t line);
240     void AppendLongStringData(string& s,
241                               size_t count,
242                               EFixNonPrint fix_method,
243                               size_t line);
244 
245     void StartBlock(void);
246     bool NextElement(void);
247     void EndBlock(void);
248     TMemberIndex GetAltItemIndex(const CClassTypeInfoBase* classType,
249                                  const CTempString& id,
250                                  const TMemberIndex pos = kInvalidMember);
251     TMemberIndex GetMemberIndex(const CClassTypeInfo* classType,
252                                 const CTempString& id);
253     TMemberIndex GetMemberIndex(const CClassTypeInfo* classType,
254                                 const CTempString& id,
255                                 const TMemberIndex pos);
256     TMemberIndex GetChoiceIndex(const CChoiceTypeInfo* choiceType,
257                                 const CTempString& id);
258 
259     bool m_BlockStart;
260 };
261 
262 
263 /* @} */
264 
265 
266 END_NCBI_SCOPE
267 
268 #endif  /* OBJISTRB__HPP */
269