-
Notifications
You must be signed in to change notification settings - Fork 1
/
Array.hpp
291 lines (174 loc) · 7.49 KB
/
Array.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* PKCS#11 library for .Net smart cards
* Copyright (C) 2007-2009 Gemalto <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef __GEMALTO_ARRAY__
#define __GEMALTO_ARRAY__
#include <string>
#include <string.h>
#include <vector>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/version.hpp>
#include "MarshallerCfg.h"
#include "Except.h"
MARSHALLER_NS_BEGIN
/*
*/
class SMARTCARDMARSHALLER_DLLAPI StringArray
{
private:
std::string** buffer;
unsigned int _length;
public:
inline StringArray( ) { _length = 0; buffer = NULL; }
inline StringArray( const unsigned int& n ) { _length = n; buffer = new std::string*[ _length ]; for( unsigned int i = 0 ; i < _length ; ++i ) { buffer[ i ] = NULL; } }
inline StringArray( const StringArray& a ) { _length = a._length; buffer = new std::string*[ _length ]; for( unsigned int i = 0 ; i < _length ; ++i ) { buffer[ i ] = a.buffer[ i ]; } }
inline virtual ~StringArray( ) { for( unsigned int i = 0 ; i < _length; ++i ) { delete buffer[ i ]; } delete[ ] buffer; }
inline bool IsNull( void ) const { return !_length; }
inline unsigned int GetLength( void ) const { return _length; }
inline std::string* GetStringAt( const unsigned int& i ) { if( i >= _length ) { throw ArgumentOutOfRangeException((lpCharPtr)""); } return buffer[ i ]; }
inline void SetStringAt( const unsigned int& i, std::string* s ) { if( i >= _length ) { throw ArgumentOutOfRangeException((lpCharPtr)""); } buffer[ i ] = s; }
};
#define s8Array u8Array
class SMARTCARDMARSHALLER_DLLAPI u8Array
{
private:
u8* buffer;
s4 _length;
public:
u8Array(s4 nelement);
u8Array(const u8Array &rhs);
~u8Array(void);
u1 IsNull(void);
u4 GetLength(void);
void SetBuffer(u8* buffer);
u8* GetBuffer(void);
u8 ReadU8At(u4 pos);
void SetU8At(u4 pos, u8 val);
u8Array& operator +(u8 val);
u8Array& operator +=(u8 val);
u8Array& operator +(u8Array &cArray);
u8Array& operator +=(u8Array &cArray);
};
#define s4Array u4Array
class SMARTCARDMARSHALLER_DLLAPI u4Array
{
private:
u4* buffer;
s4 _length;
public:
u4Array(s4 nelement);
u4Array(const u4Array &rhs);
~u4Array(void);
u1 IsNull(void);
u4 GetLength(void);
void SetBuffer(u4* buffer);
u4* GetBuffer(void);
u4 ReadU4At(u4 pos);
void SetU4At(u4 pos, u4 val);
u4Array& operator +(u4 val);
u4Array& operator +=(u4 val);
u4Array& operator +(u4Array &cArray);
u4Array& operator +=(u4Array &cArray);
};
#define s2Array u2Array
#define charArray u2Array
class SMARTCARDMARSHALLER_DLLAPI u2Array
{
private:
u2* buffer;
s4 _length;
public:
u2Array(s4 nelement);
u2Array(const u2Array &rhs);
~u2Array(void);
u1 IsNull(void);
u4 GetLength(void);
void SetBuffer(u2* buffer);
u2* GetBuffer(void);
u2 ReadU2At(u4 pos);
void SetU2At(u4 pos, u2 val);
u2Array& operator +(u2 val);
u2Array& operator +=(u2 val);
u2Array& operator +(u2Array &cArray);
u2Array& operator +=(u2Array &cArray);
};
#define s1Array u1Array
#define MemoryStream u1Array
class SMARTCARDMARSHALLER_DLLAPI u1Array
{
private:
unsigned char* buffer;
unsigned int _length;
public:
inline u1Array( ) { _length = 0; buffer = 0; }
inline u1Array( const unsigned int& n ) { _length = n; buffer = new unsigned char[ _length ]; memset( buffer, 0, _length ); }
inline u1Array( const u1Array& a ) { _length = a._length; buffer = new unsigned char[ _length ]; memcpy( buffer, a.buffer, _length ); }
inline u1Array( const u1Array &a, const unsigned int& o, const unsigned int& l ) { if( (o + l) > a.GetLength( ) ) { throw ArgumentOutOfRangeException((lpCharPtr)" u1Array constructor (1)"); } _length = l; buffer = new unsigned char[ _length ]; memcpy( buffer, a.buffer + o, _length ); }
inline u1Array( const std::vector< unsigned char >& a ) { _length = a.size( ); buffer = new unsigned char[ _length ]; for( unsigned int i = 0 ; i < _length ; ++i ) { buffer[ i ] = a.at( i ); } }
inline virtual ~u1Array( ) { delete[ ] buffer; }
inline bool IsNull( void ) const { return !_length; }
inline unsigned int GetLength( void ) const { return _length; }
inline void SetBuffer( const unsigned char* b ) { if( b ) { memcpy( buffer, b, _length ); } }
inline unsigned char ReadU1At( const unsigned int& p ) const { if( p >= _length ) { throw ArgumentOutOfRangeException((lpCharPtr)"u1Array::ReadU1At"); } return buffer[ p ]; }
inline void SetU1At( const unsigned int& p, const unsigned char& v ) { if( p >= _length ) { throw ArgumentOutOfRangeException((lpCharPtr)"u1Array::SetU1At"); } buffer[ p ] = v; }
u1Array& Append( std::string* );
u1Array& Append( const char* );
inline unsigned char* GetBuffer( void ) { return buffer; }
inline const unsigned char* GetBuffer( void ) const { return buffer; }
u1Array& operator +( const unsigned char& );
u1Array& operator +=( const unsigned char& );
u1Array& operator +( const u2& );
u1Array& operator +=( const u2& );
u1Array& operator +( const u4& );
u1Array& operator +=( const u4& );
u1Array& operator +( const u8& );
u1Array& operator +=( const u8& );
u1Array& operator =( const u1Array& );
u1Array& operator +( const u1Array& );
u1Array& operator +=( const u1Array& );
inline void reset( void ) { delete[ ] buffer; buffer = NULL; _length = 0; }
inline void reset( unsigned int a_uiLength ) { delete[ ] buffer; _length = a_uiLength; buffer = new unsigned char[ a_uiLength ]; memset( buffer, 0, _length ); }
inline void reset( Marshaller::u1Array* a_pArray ) { delete[ ] buffer; _length = 0; if( a_pArray ) { _length = a_pArray->_length; buffer = new unsigned char[ _length ]; memcpy( buffer, a_pArray->buffer, _length ); } }
// Boost serialization of the array
friend class boost::serialization::access;
template< class Archive > void save( Archive& ar, const unsigned int /*version*/ ) const {
ar << _length;
//Log::log( "Array::save - length <%ld>", _length );
ar.save_binary( buffer, _length );
//Log::logCK_UTF8CHAR_PTR( "Array::save - buffer", buffer, _length );
}
template< class Archive > void load( Archive& ar, const unsigned int /*version*/ ) {
ar >> _length;
//Log::log( "Array::load - length <%ld>", _length );
buffer = 0;
if( _length > 0 ) {
buffer = new unsigned char[ _length ];
ar.load_binary( buffer, _length );
}
//Log::logCK_UTF8CHAR_PTR( "Array::load - buffer", buffer, _length );
}
BOOST_SERIALIZATION_SPLIT_MEMBER( )
};
extern u2 ComputeUTF8Length(M_SAL_IN lpCharPtr str);
extern void UTF8Encode(M_SAL_IN lpCharPtr str, u1Array &utf8Data);
extern u2 ComputeLPSTRLength(u1Array &array, u4 offset, u4 len);
extern void UTF8Decode(u1Array &array, u4 offset, u4 len, M_SAL_INOUT lpCharPtr &charData);
MARSHALLER_NS_END
BOOST_CLASS_VERSION( Marshaller::u1Array, 1 )
#endif // __GEMALTO_ARRAY__