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