include/serial/objistrasnb.hpp

Go to the documentation of this file.
00001 #ifndef OBJISTRASNB__HPP
00002 #define OBJISTRASNB__HPP
00003 
00004 /*  $Id: objistrasnb.hpp 103491 2007-05-04 17:18:18Z kazimird $
00005 * ===========================================================================
00006 *
00007 *                            PUBLIC DOMAIN NOTICE
00008 *               National Center for Biotechnology Information
00009 *
00010 *  This software/database is a "United States Government Work" under the
00011 *  terms of the United States Copyright Act.  It was written as part of
00012 *  the author's official duties as a United States Government employee and
00013 *  thus cannot be copyrighted.  This software/database is freely available
00014 *  to the public for use. The National Library of Medicine and the U.S.
00015 *  Government have not placed any restriction on its use or reproduction.
00016 *
00017 *  Although all reasonable efforts have been taken to ensure the accuracy
00018 *  and reliability of the software and data, the NLM and the U.S.
00019 *  Government do not and cannot warrant the performance or results that
00020 *  may be obtained by using this software or data. The NLM and the U.S.
00021 *  Government disclaim all warranties, express or implied, including
00022 *  warranties of performance, merchantability or fitness for any particular
00023 *  purpose.
00024 *
00025 *  Please cite the author in any work or product based on this material.
00026 *
00027 * ===========================================================================
00028 *
00029 * Author: Eugene Vasilchenko
00030 *
00031 * File Description:
00032 *   Decode input data in ASN binary format (BER)
00033 */
00034 
00035 #include <corelib/ncbistd.hpp>
00036 #include <serial/objistr.hpp>
00037 #include <serial/impl/objstrasnb.hpp>
00038 #include <stack>
00039 
00040 
00041 /** @addtogroup ObjStreamSupport
00042  *
00043  * @{
00044  */
00045 
00046 
00047 BEGIN_NCBI_SCOPE
00048 
00049 class CObjectOStreamAsnBinary;
00050 
00051 /////////////////////////////////////////////////////////////////////////////
00052 ///
00053 /// CObjectIStreamAsnBinary --
00054 ///
00055 /// Decode input data in ASN.1 binary format (BER)
00056 class  CObjectIStreamAsnBinary : public CObjectIStream,
00057                                                     public CAsnBinaryDefs
00058 {
00059 public:
00060 
00061     /// Constructor.
00062     ///
00063     /// @param how
00064     ///   Defines how to fix unprintable characters in ASN VisiableString
00065     CObjectIStreamAsnBinary(EFixNonPrint how = eFNP_Default);
00066 
00067     /// Constructor.
00068     ///
00069     /// @param in
00070     ///   input stream    
00071     /// @param how
00072     ///   Defines how to fix unprintable characters in ASN VisiableString
00073     CObjectIStreamAsnBinary(CNcbiIstream& in,
00074                             EFixNonPrint how = eFNP_Default);
00075 
00076     /// Constructor.
00077     ///
00078     /// @param in
00079     ///   input stream    
00080     /// @param deleteIn
00081     ///   when TRUE, the input stream will be deleted automatically
00082     ///   when the reader is deleted
00083     /// @param how
00084     ///   Defines how to fix unprintable characters in ASN VisiableString
00085     CObjectIStreamAsnBinary(CNcbiIstream& in,
00086                             bool deleteIn,
00087                             EFixNonPrint how = eFNP_Default);
00088 
00089     /// Constructor.
00090     ///
00091     /// @param reader
00092     ///   Data source
00093     /// @param how
00094     ///   Defines how to fix unprintable characters in ASN VisiableString
00095     CObjectIStreamAsnBinary(CByteSourceReader& reader,
00096                             EFixNonPrint how = eFNP_Default);
00097 
00098     /// Constructor.
00099     ///
00100     /// @param buffer
00101     ///   Data source memory buffer
00102     /// @param size
00103     ///   Memory buffer size
00104     /// @param how
00105     ///   Defines how to fix unprintable characters in ASN VisiableString
00106     CObjectIStreamAsnBinary(const char* buffer,
00107                             size_t size,
00108                             EFixNonPrint how = eFNP_Default);
00109 
00110 
00111     virtual TEnumValueType ReadEnum(const CEnumeratedTypeValues& values);
00112     virtual void ReadNull(void);
00113 
00114     virtual void ReadAnyContentObject(CAnyContentObject& obj);
00115     bool SkipAnyContent(void);
00116     virtual void SkipAnyContentObject(void);
00117     virtual void SkipAnyContentVariant(void);
00118 
00119     virtual void ReadBitString(CBitString& obj);
00120     virtual void SkipBitString(void);
00121 
00122     EFixNonPrint FixNonPrint(EFixNonPrint how)
00123         {
00124             EFixNonPrint tmp = m_FixMethod;
00125             m_FixMethod = how;
00126             return tmp;
00127         }
00128 
00129 protected:
00130     virtual bool ReadBool(void);
00131     virtual char ReadChar(void);
00132     virtual Int4 ReadInt4(void);
00133     virtual Uint4 ReadUint4(void);
00134     virtual Int8 ReadInt8(void);
00135     virtual Uint8 ReadUint8(void);
00136     virtual double ReadDouble(void);
00137     virtual void ReadString(string& s,EStringType type = eStringTypeVisible);
00138     virtual void ReadPackedString(string& s,
00139                                   CPackString& pack_string,
00140                                   EStringType type);
00141     virtual char* ReadCString(void);
00142     virtual void ReadStringStore(string& s);
00143 
00144     virtual void SkipBool(void);
00145     virtual void SkipChar(void);
00146     virtual void SkipSNumber(void);
00147     virtual void SkipUNumber(void);
00148     virtual void SkipFNumber(void);
00149     virtual void SkipString(EStringType type = eStringTypeVisible);
00150     virtual void SkipStringStore(void);
00151     virtual void SkipNull(void);
00152     virtual void SkipByteBlock(void);
00153 
00154 #ifdef VIRTUAL_MID_LEVEL_IO
00155     virtual void ReadContainer(const CContainerTypeInfo* containerType,
00156                                TObjectPtr containerPtr);
00157     virtual void SkipContainer(const CContainerTypeInfo* containerType);
00158 
00159     virtual void ReadClassSequential(const CClassTypeInfo* classType,
00160                                      TObjectPtr classPtr);
00161     virtual void ReadClassRandom(const CClassTypeInfo* classType,
00162                                  TObjectPtr classPtr);
00163     virtual void SkipClassSequential(const CClassTypeInfo* classType);
00164     virtual void SkipClassRandom(const CClassTypeInfo* classType);
00165 
00166     virtual void ReadChoice(const CChoiceTypeInfo* choiceType,
00167                             TObjectPtr choicePtr);
00168     virtual void SkipChoice(const CChoiceTypeInfo* choiceType);
00169 
00170 #endif
00171 
00172     // low level I/O
00173     virtual void BeginContainer(const CContainerTypeInfo* containerType);
00174     virtual void EndContainer(void);
00175     virtual bool BeginContainerElement(TTypeInfo elementType);
00176 
00177     virtual void BeginClass(const CClassTypeInfo* classInfo);
00178     virtual void EndClass(void);
00179     virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType);
00180     virtual TMemberIndex BeginClassMember(const CClassTypeInfo* classType,
00181                                           TMemberIndex pos);
00182     virtual void EndClassMember(void);
00183 
00184     virtual void BeginChoice(const CChoiceTypeInfo* choiceType);
00185     virtual void EndChoice(void);
00186     virtual TMemberIndex BeginChoiceVariant(const CChoiceTypeInfo* choiceType);
00187     virtual void EndChoiceVariant(void);
00188 
00189     virtual void BeginBytes(ByteBlock& block);
00190     virtual size_t ReadBytes(ByteBlock& block, char* dst, size_t length);
00191     virtual void EndBytes(const ByteBlock& block);
00192 
00193     virtual void BeginChars(CharBlock& block);
00194     virtual size_t ReadChars(CharBlock& block, char* dst, size_t length);
00195     virtual void EndChars(const CharBlock& block);
00196 
00197 #if HAVE_NCBI_C
00198     friend class CObjectIStream::AsnIo;
00199 #endif
00200 
00201 private:
00202     virtual EPointerType ReadPointerType(void);
00203     virtual TObjectIndex ReadObjectPointer(void);
00204     virtual string ReadOtherPointer(void);
00205     virtual void ReadOtherPointerEnd(void);
00206 
00207     bool SkipRealValue(void);
00208 
00209     EFixNonPrint m_FixMethod; // method of fixing non-printable chars
00210 
00211 #if CHECK_INSTREAM_STATE
00212     enum ETagState {
00213         eTagStart,
00214         eTagParsed,
00215         eLengthValue,
00216         eData
00217     };
00218     // states:
00219     // before StartTag (Peek*Tag/ExpectSysTag) tag:
00220     //     m_CurrentTagLength == 0
00221     //     stream position on tag start
00222     // after Peek*Tag/ExpectSysTag tag:
00223     //     m_CurrentTagLength == beginning of LENGTH field
00224     //     stream position on tag start
00225     // after FlushTag (Read*Length/ExpectIndefiniteLength):
00226     //     m_CurrentTagLength == 0
00227     //     stream position on tad DATA start
00228     //     tag limit is pushed on stack and new tag limit is updated
00229     // after EndOfTag
00230     //     m_CurrentTagLength == 0
00231     //     stream position on tag DATA end
00232     //     tag limit is popped from stack
00233     // m_CurrentTagLength == beginning of LENGTH field
00234     //                         -- after any of Peek?Tag or ExpectSysTag
00235     // 
00236     ETagState m_CurrentTagState;
00237 #endif
00238 #if CHECK_INSTREAM_LIMITS
00239     Int8 m_CurrentTagLimit;   // end of current tag data
00240     stack<Int8> m_Limits;
00241 #endif
00242     size_t m_CurrentTagLength;  // length of tag header (without length field)
00243 
00244     // low level interface
00245 private:
00246     TByte PeekTagByte(size_t index = 0);
00247     TByte StartTag(TByte first_tag_byte);
00248     TLongTag PeekTag(TByte first_tag_byte);
00249     void ExpectTagClassByte(TByte first_tag_byte,
00250                             TByte expected_class_byte);
00251     void UnexpectedTagClassByte(TByte first_tag_byte,
00252                                 TByte expected_class_byte);
00253     TLongTag PeekTag(TByte first_tag_byte,
00254                      ETagClass tag_class,
00255                      ETagConstructed tag_constructed);
00256     string PeekClassTag(void);
00257     TByte PeekAnyTagFirstByte(void);
00258     void ExpectSysTagByte(TByte byte);
00259     void UnexpectedSysTagByte(TByte byte);
00260     void ExpectSysTag(ETagClass tag_class,
00261                       ETagConstructed tag_constructed,
00262                       ETagValue tag_value);
00263     void ExpectSysTag(ETagValue tag_value);
00264     TByte FlushTag(void);
00265     void ExpectIndefiniteLength(void);
00266     bool PeekIndefiniteLength(void);
00267     void ExpectContainer(bool random);
00268 public:
00269     size_t ReadShortLength(void);
00270 private:
00271     size_t ReadLength(void);
00272     size_t StartTagData(size_t length);
00273     void ExpectShortLength(size_t length);
00274     void ExpectEndOfContent(void);
00275 public:
00276     void EndOfTag(void);
00277     Uint1 ReadByte(void);
00278     Int1 ReadSByte(void);
00279     void ExpectByte(Uint1 byte);
00280 private:
00281     void ReadBytes(char* buffer, size_t count);
00282     void ReadBytes(string& str, size_t count);
00283     void SkipBytes(size_t count);
00284 
00285     void ReadStringValue(size_t length, string& s, EFixNonPrint fix_type);
00286     void SkipTagData(void);
00287     bool HaveMoreElements(void);
00288     void UnexpectedMember(TLongTag tag);
00289     void UnexpectedByte(TByte byte);
00290 
00291     friend class CObjectOStreamAsnBinary;
00292 };
00293 
00294 
00295 /* @} */
00296 
00297 
00298 #include <serial/impl/objistrasnb.inl>
00299 
00300 END_NCBI_SCOPE
00301 
00302 #endif  /* OBJISTRASNB__HPP */
00303 
00304 

Generated on Wed Feb 4 23:33:07 2009 for NCBI C++ ToolKit by  doxygen 1.4.6
Modified on Thu Feb 05 09:30:37 2009 by modify_doxy.py rev. 117643