📄 asn1.h
字号:
/* * asn1.h * * Abstract Syntax Notation 1 Encoding Rules * * Copyright (c) 1993-1998 Equivalence Pty. Ltd. * * Copyright (c) 2001 Institute for Information Industry, Taiwan, Republic of China * (http://www.iii.org.tw/iiia/ewelcome.htm) * * The contents of this file are subject to the Mozilla Public License * Version 1.0 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See * the License for the specific language governing rights and limitations * under the License. * * The Original Code is Portable Windows Library. * * The Initial Developer of the Original Code is Equivalence Pty. Ltd. * * Portions are Copyright (C) 1993 Free Software Foundation, Inc. * All Rights Reserved. * * Contributor(s): Huang-Ming Huang * * The code is adapted from asner.h of PWLib, but the dependancy on PWLib has * been removed. * * $Log: asn1.h,v $ * Revision 1.15 2002/08/20 22:35:54 mangelo * Add MSVC DLL support * * Revision 1.14 2002/07/18 06:53:33 btrummer * Added range checks for PEREncoder's encodeLength(), encodeUnsigned() * and encodeConstrainedLength(). Encoding an object which violates its * length constraint will fail now. * * Revision 1.13 2002/07/17 11:17:51 btrummer * Check for the macro __IBMCPP__ rather than _AIX for the last AiX-fix. * * Revision 1.12 2002/07/11 08:34:08 btrummer * Added AiX-fix for the initialization of Constrained_INTEGER::theInfo. * * Revision 1.11 2002/07/11 07:51:07 btrummer * Added some InfoType typedefs needed for AiX. * * Revision 1.10 2002/07/11 06:22:19 btrummer * Fixed the constructor * OBJECT_IDENTIFIER(InputIterator, InputIterator, const void*), * which raised an internal compiler error on Windows. * * Revision 1.9 2002/07/04 03:47:22 mangelo * added explicit wstring typedef for GCC 2.95.x * * Revision 1.8 2002/07/03 06:15:26 btrummer * Added a range-check for PERDecoder::rollback(), so beginPosition will * never be set outside of endPosition. * * Revision 1.7 2002/07/02 04:45:13 mangelo * Modify for VC.Net and GCC 3.1 * * Revision 1.6 2001/11/13 16:40:12 mangelo * Fixed the missing retrun in OCTET_STRING::operator=(const char*) * * Revision 1.5 2001/10/25 02:47:56 mangelo * fixed the wrong declaration of SEQUNCE_OF::InfoType.type * * Revision 1.4 2001/10/05 18:51:18 mangelo * Fixed wrong declarations of AbstractString.TpyeInfo.type and BMPString.TypeInfo.type. Added tagMode() to SEQUENCE * * Revision 1.3 2001/08/29 15:47:34 mangelo * Fixed the declaration of SizeConstraint class template * * Revision 1.2 2001/08/03 07:43:10 mangelo * Fixed isValid() bugs * * * 2001/07/26 Huang-Ming Huang * Fixed INTEGER::isStrictValid() when the lowerLimit() >=0 and value > INT_MAX. * * 2001/07/26 Huang-Ming Huang * Added custom memory manager using boost/pool. * * 2001/07/16 Huang-Ming Huang * Optional components of SEQUENCE is now created on demand. * * 2001/06/26 Huang-Ming Huang * Version 2.1 Reimplemented to minimize the code size. * */#ifndef _ASN1_H#define _ASN1_H#if defined(_MSC_VER) && (_MSC_VER <= 1200)#pragma warning(disable: 4786)#endif#if defined(_MSC_VER) && !defined(NDEBUG) && !defined(_CPPRTTI)#error Please Use /GR compiler option for debug version. In Visual Stdio, check the Enable RTTI box under C++ Language of C/C++ Tab in Project/Settings#endif#ifdef ASN1_ALLOCATOR#include <boost/pool/singleton_pool.hpp>#endif#ifdef min#undef min#endif#ifdef max#undef max#endif#include <assert.h>#include <string>#include <memory>#include <time.h>#include <boost/iterator.hpp>#include "AssocVector.h"#if !defined(ASN1_STATIC) && defined(_MSC_VER)#define ASN1_EXPORT __declspec(dllimport)#else#define ASN1_EXPORT #endif #ifdef ASN1_HAS_IOSTREAM#include <sstream>#define AVN_ONLY(x) x#else#define AVN_ONLY(x) #endif#ifdef NEED_WSTRING_TYPEDEFnamespace std { typedef basic_string<wchar_t> wstring;}#endif/////////////////////////////////////////////////////////////////////////////namespace ASN1 {class Visitor;class ConstVistor;enum ConstraintType { Unconstrained, PartiallyConstrained, FixedConstraint, ExtendableConstraint};class Visitor;class ConstVisitor;class AbstractData;namespace detail {template <unsigned v>struct int_to_type{};template <class T>struct Allocator{#ifdef ASN1_ALLOCATOR static void* operator new (std::size_t sz) { assert(sizeof(T) == sz); return boost::singleton_pool<int_to_type<sizeof(T)>, sizeof(T)>::malloc(); } static void operator delete(void* p) { boost::singleton_pool<int_to_type<sizeof(T)> , sizeof(T)>::free(p); }#endif};}/** * Base class for ASN.1 types. * * The ASN1::AbstractData class provides an abstract base class from which all ASN.1 * builtin and user-defined type classes are derived. This class offers polymorphic * functionality including copying/cloning, comparison, validity check, and output. * The class is abstract and can only be instantiated in derived forms. * * The ASN1::AbstractData class also plays the role \a Element in \a Visitor Design Pattern, described * by Gamma, et al. It has the accept() operations which dispatch the request according to their exact * subtype (ConcreteElement). In this implementation, there are two kinds of \a Visitors, \c Visitor * and \c ConstVistor. See those two classes for more detail explanation. * * @warning For all the AbstractData subclass, the assignment operator do not involve the exchange of * constraints and tags. That is to say, the type been assigned will * retain the same constraints and tags as it hasn't been assigned. * However all the copy constructors do copy the constraints and tags from the object been copied. * It is the programmer's responsibility to prevent using copy constructor on different types. * */class AbstractData { public: virtual ~AbstractData() {}; /** * Allocate a copy of this object on the freestore. * * The object allocated is of * the same actual subclass as the object on which this method is invoked, and * has a copy of all of its values. The allocated object is owned by the caller, * who is responsible for deleting it. */ AbstractData* clone() const { return do_clone(); } /** * Verify that the object conforms to any type constraints on it. * * These constraints * consist of those inherent to the ASN.1 type (such as NumericString), and any subtype * constraints. The isValid() function accepts a value as an acceptable extended value. * That is, if the ASN.1 type is defined as being extensible (using the ... notation), * then a value that could fit within such an extension is considered valid. */ bool isValid() const ; /** * Verify that the object conforms to any type constraints on it, * without allowing for any extended values. * * For types that do not have * extensibility defined, isStrictlyValid() is equivalent to isValid(). * For constructed types, isStrictlyValid() returns true when isStrictlyValid() * is true for all the components. */ bool isStrictlyValid() const ; /** * Compare two AbstractData Objects. * * @return The return value is 0 if this object equals to \c other; * the return value > 0 if this object is greater than \c other. * * This implementation allows only the comparison between compatible types, e.g. * IA5String and VisibleString. The behavior of comparing between incompatible * types is undefined. */ int compare(const AbstractData& other) const { return do_compare(other); } /* * Most operators are defined as member functions rather than friend functions because * GCC 2.95.x have problem to resolve template operator functions when any non-template * operator function exists. */ bool operator == (const AbstractData& rhs) const { return compare(rhs) == 0; } bool operator != (const AbstractData& rhs) const { return compare(rhs) != 0; } bool operator < (const AbstractData& rhs) const { return compare(rhs) < 0; } bool operator > (const AbstractData& rhs) const { return compare(rhs) > 0; } bool operator <= (const AbstractData& rhs) const { return compare(rhs) <= 0; } bool operator >= (const AbstractData& rhs) const { return compare(rhs) >= 0; } /** * Returns the ASN.1 Tag. * * An ASN.1 tag has two parts, tag class and tag number. * The return value of getTag() is (tagClass << 16 | tagNumber). */ unsigned getTag() const { return info()->tag; } /** * Accept a \c Visitor which implement a fragment of algorithm or request for * each corresponding class of ASN.1 object structure. * * @param v The \c Visitor. * @return true if the operation has been successfully executed. */ bool accept(Visitor& v) { return do_accept(v) ;} /** * Accept a \c Visitor which implement a fragment of algorithm or request for * each corresponding class of ASN.1 object structure. * * @param v The \c ConstVisitor. * @return true if the operation has been successfully executed. */ bool accept(ConstVisitor& v) const { return do_accept(v); } /** * Create a AbstractData object based on the \c info structure. * * This is a factory method for creating an instance of ASN1::AbstractData * object. Each concrete ASN1::AbstractData subclass contains a structure which describe * the information of the type, constraints, and how it can be instantiate. Using this information, * an object can be created by the static member function. * * @return NULL if the object creation is fail; otherwise a new object is successfully * created and should be deleted by its caller. */ inline static AbstractData* create(const void* info) { return info == NULL ? NULL : static_cast<const InfoType*>(info)->create(info); } private: virtual int do_compare(const AbstractData& other) const =0; virtual AbstractData* do_clone() const = 0; virtual bool do_accept(Visitor&) = 0; virtual bool do_accept(ConstVisitor&) const =0; protected: AbstractData(const void* info); typedef AbstractData* (*CreateFun)(const void*); struct InfoType { CreateFun create; unsigned tag; /* the tag is represented using the formula (tagClass << 16 | tagNumber) */ }; const void* info_; public: const InfoType* info() const { return static_cast<const InfoType*>(info_);} #ifdef ASN1_HAS_IOSTREAM public: friend std::ostream & operator<<( std::ostream &strm, // Stream to print the object into. const AbstractData & obj // Object to print to the stream. ); friend std::istream & operator >>( std::istream &strm, // Stream which stores the object into. AbstractData & obj // Object to retrive from the stream. ); std::ios_base::iostate get_from(std::istream &); std::ios_base::iostate print_on(std::ostream &) const ; /** * Set the AbstractData object from a string containing ASN.1 value notation. * * This supports full ASN.1 except for the following limitations: * \li Value references are not allowed. That is, a value can not contain the names of other values. * Note that values are commonly used to provide the high order part of an Object Identifier. * \li Open type is not currently supported. */ bool setFromValueNotation(const std::string& valueString);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -