00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CODEX_ASN1_ARRAY_H__
00019 #define __CODEX_ASN1_ARRAY_H__
00020
00021 #include <vector>
00022 #include "Base.h"
00023 #include "Integer.h"
00024
00025 namespace CODEX_ASN1
00026 {
00030 template< class T >
00031 class Array : public Base
00032 {
00033 public :
00035 typedef T ValueType;
00037 typedef vector< T* > ArrayType;
00039 typedef typename ArrayType::const_iterator ArrayItr;
00040
00045 Array() : Base( false ) {}
00047 Array( const Array& aArr ) :
00048 Base( aArr.m_initialized )
00049 {
00050 if ( &aArr == this )
00051 {
00052 return;
00053 }
00054 while( m_array.size() )
00055 {
00056 delete m_array.back();
00057 m_array.pop_back();
00058 }
00059 ArrayItr itr = aArr.m_array.begin();
00060 ArrayItr end = aArr.m_array.end();
00061 for ( ; itr != end ; ++itr )
00062 {
00063 m_array.push_back( new T( **itr ) );
00064 }
00065 }
00066
00068 virtual ~Array()
00069 {
00070 while( m_array.size() )
00071 {
00072 delete m_array.back();
00073 m_array.pop_back();
00074 }
00075 }
00076
00078 void operator=( const Array& aArr )
00079 {
00080 if ( &aArr == this )
00081 {
00082 return;
00083 }
00084 while( m_array.size() )
00085 {
00086 delete m_array.back();
00087 m_array.pop_back();
00088 }
00089 m_initialized = aArr.m_initialized;
00090 ArrayItr itr = aArr.m_array.begin();
00091 ArrayItr end = aArr.m_array.end();
00092 for ( ; itr != end ; ++itr )
00093 {
00094 m_array.push_back( new T( **itr ) );
00095 }
00096 }
00097
00099 unsigned int size() const { return m_array.size(); }
00101 const T* element( unsigned int i ) const { return m_array[i]; }
00103 ArrayItr begin() const { return m_array.begin(); }
00105 ArrayItr end() const { return m_array.end(); }
00107 void append( T* item )
00108 {
00109 m_initialized = true;
00110 m_array.push_back( item );
00111 }
00112
00113 int marshal( unsigned char ** pp ) const
00114 {
00115 int r=0;
00116 int ret=0;
00117 unsigned char * p;
00118
00119 Integer length(m_array.size());
00120 ret += length.marshal(0);
00121
00122 ArrayItr itr = m_array.begin();
00123 ArrayItr end = m_array.end();
00124 for ( ; itr != end ; ++itr )
00125 {
00126 ret += (*itr)->marshal(0);
00127 }
00128 M_ASN1_I2D_seq_total();
00129
00130 length.marshal(&p);
00131 itr = m_array.begin();
00132 for ( ; itr != end ; ++itr )
00133 {
00134 (*itr)->marshal(&p);
00135 }
00136 M_ASN1_I2D_finish();
00137 }
00138
00139 void* unmarshal( void* bogus, unsigned char** pp, long length )
00140 {
00141 if ( m_initialized )
00142 {
00143 return 0;
00144 }
00145 if ( (0 == pp) || (0 == *pp) )
00146 {
00147 return 0;
00148 }
00149 ASN1_CTX c;
00150 c.pp = pp;
00151 c.q = *pp;
00152 c.error = ERR_R_NESTED_ASN1_ERROR;
00153 int i;
00154 Integer alength;
00155
00156 M_ASN1_D2I_Init();
00157 M_ASN1_D2I_start_sequence();
00158 M_ASN1_D2I_get(i, alength.unmarshal);
00159 for ( int j = 0 ; j < alength.value() ; ++j )
00160 {
00161 T* item = new T;
00162 M_ASN1_D2I_get(i, item->unmarshal);
00163 m_array.push_back(item);
00164 }
00165 if ( ! asn1_Finish(&c) )
00166 {
00167 return 0;
00168 }
00169 *pp = c.p;
00170 m_initialized = true;
00171 return this;
00172 err:
00173 return 0;
00174 }
00175
00176 protected :
00177
00178 private :
00179 ArrayType m_array;
00180 };
00181
00182 }
00183
00184 #endif