-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathtlv.c
314 lines (281 loc) · 6.19 KB
/
tlv.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include "include/libemv.h"
#include "internal.h"
#include <string.h>
// Application buffer, from ICC and terminal
// ([unsigned short tag][int length][data]..)
static unsigned char* tlv_buffer;
static int tlv_allocated;
static int tlv_length;
void libemv_init_tlv_buffer(void)
{
tlv_buffer = 0;
tlv_allocated = 0;
tlv_length = 0;
}
void libemv_destroy_tlv_buffer(void)
{
if (tlv_buffer)
libemv_free(tlv_buffer);
}
static void check_and_reserve_buffer(int incrSize)
{
if (tlv_length + incrSize <= tlv_allocated)
return;
if (tlv_allocated == 0)
{
// Init size
tlv_allocated = 2 * 1024;
tlv_buffer = libemv_malloc(tlv_allocated);
} else
{
// incrSize musn't very big, but just in case
while (tlv_length + incrSize > tlv_allocated)
tlv_allocated *= 2;
// Realloc must copy old data
tlv_buffer = libemv_realloc(tlv_buffer, tlv_allocated);
}
// Unable allocate
if (tlv_buffer == 0)
{
if (libemv_debug_enabled)
libemv_printf("Unable allocate memory\n");
}
}
LIBEMV_API unsigned char* libemv_get_tag(unsigned short tag, int* outSize)
{
unsigned char* currBuf;
int currPos;
currBuf = tlv_buffer;
currPos = 0;
while (currPos < tlv_length)
{
unsigned short currTag;
int currLength;
// Corrupted buffer
if (currPos + (int) sizeof(unsigned short) + (int) sizeof(int) > tlv_length)
{
if (libemv_debug_enabled)
libemv_printf("tlv buffer malfunc\n");
break;
}
memcpy(&currTag, currBuf, sizeof(unsigned short));
currBuf += sizeof(unsigned short);
currPos += sizeof(unsigned short);
memcpy(&currLength, currBuf, sizeof(int));
currBuf += sizeof(int);
currPos += sizeof(int);
if (currTag == tag)
{
*outSize = currLength;
return currBuf;
}
currBuf += currLength;
currPos += currLength;
}
// Not found
return 0;
}
int libemv_get_next_tag(int shift, unsigned short* outTag, unsigned char** outBuffer, int* outSize)
{
int sh;
sh = shift;
// Tag
if (sh + (int) sizeof(unsigned short) > tlv_length)
return 0;
memcpy(outTag, tlv_buffer + sh, sizeof(unsigned short));
sh += sizeof(unsigned short);
// Length
if (sh + (int) sizeof(int) > tlv_length)
return 0;
memcpy(outSize, tlv_buffer + sh, sizeof(int));
sh += sizeof(int);
// Value
if (sh + *outSize > tlv_length)
return 0;
*outBuffer = tlv_buffer + sh;
sh += *outSize;
return sh;
}
void libemv_set_tag(unsigned short tag, unsigned char* data, int size)
{
unsigned char* findData;
int findDataSize;
findData = libemv_get_tag(tag, &findDataSize);
if (findData)
{
// Replace data
if (findDataSize == size)
{
// Size didn't change, just copy buffer
memcpy(findData, data, size);
} else
{
// Reserve memory and move data
check_and_reserve_buffer(size - findDataSize);
memmove(findData + size, findData + findDataSize, tlv_length - (findData - tlv_buffer) - findDataSize);
memcpy(findData - sizeof(int), &size, sizeof(int));
memcpy(findData, data, size);
tlv_length += size - findDataSize;
}
} else
{
unsigned char* endBuffer;
// Add data to the end of buffer
check_and_reserve_buffer(sizeof(unsigned short) + sizeof(int) + size);
endBuffer = tlv_buffer + tlv_length;
memcpy(endBuffer, &tag, sizeof(unsigned short));
endBuffer += sizeof(unsigned short);
memcpy(endBuffer, &size, sizeof(int));
endBuffer += sizeof(int);
memcpy(endBuffer, data, size);
tlv_length += sizeof(unsigned short) + sizeof(int) + size;
}
}
void libemv_clear_tlv_buffer(void)
{
tlv_length = 0;
}
int libemv_parse_tlv(unsigned char* inBuffer, int inBufferSize, unsigned short* outTag, unsigned char** outBuffer, int* outSize)
{
unsigned char* buf;
int bufSize;
buf = inBuffer;
bufSize = inBufferSize;
// 1 byte tag
if (bufSize <= 0)
return 0;
*outTag = *buf;
if ((*buf & 0x1F) == 0x1F)
{
// 2 byte tag
buf++;
bufSize--;
if (bufSize <= 0)
return 0;
// Check "Another byte follows"
if (*buf & 0x80)
return 0;
*outTag <<= 8;
*outTag |= *buf;
}
buf++;
bufSize--;
// 1 byte length
if (bufSize <= 0)
return 0;
if (*buf & 0x80)
{
int nBytes = *buf & 0x7F;
// Next bytes length
*outSize = 0;
while (nBytes--)
{
buf++;
bufSize--;
if (bufSize <= 0)
return 0;
*outSize <<= 8;
*outSize |= *buf;
}
} else
{
*outSize = *buf;
}
buf++;
bufSize--;
// Check size more than input size or max size
if (inBufferSize < *outSize + (buf - inBuffer))
return 0;
// Point data and return
*outBuffer = buf;
return *outSize + (buf - inBuffer);
}
int libemv_make_tlv(unsigned char* inBuffer, int inBufferSize, unsigned short tag, unsigned char* tlvBuffer)
{
int tlvSize;
tlvSize = 0;
if ((tag & 0x1F00) == 0x1F00)
{
// 2 byte tag
tlvBuffer[tlvSize++] = (tag >> 8) & 0xFF;
tlvBuffer[tlvSize++] = tag & 0xFF;
} else
{
// 1 byte tag
tlvBuffer[tlvSize++] = tag & 0xFF;
}
if (inBufferSize > 0x7F)
{
// n byte length
int n;
if (inBufferSize < 0x100)
n = 1;
else if (inBufferSize < 0x10000)
n = 2;
else if (inBufferSize < 0x1000000)
n = 3;
else
n = 4;
n |= 0x80;
tlvBuffer[tlvSize++] = n & 0xFF;
while (n--)
{
tlvBuffer[tlvSize++] = (inBufferSize >> (n * 8)) & 0xFF;
}
} else
{
// 1 byte length
tlvBuffer[tlvSize++] = inBufferSize & 0x7F;
}
// Copy data
memcpy(tlvBuffer + tlvSize, inBuffer, inBufferSize);
tlvSize += inBufferSize;
return tlvSize;
}
int libemv_dol(unsigned char* dol, int dolSize, unsigned char* outBuffer)
{
int outSize;
int dolShift;
outSize = 0;
dolShift = 0;
while (dolShift < dolSize)
{
unsigned short tag;
int size;
unsigned char* findData;
int findSize;
int sizeToCopy;
// Tag could be 1 or 2 byte
tag = dol[dolShift];
if ((dol[dolShift] & 0x1F) == 0x1F)
{
dolShift++;
if (dolShift >= dolSize)
break;
tag <<= 8;
tag |= dol[dolShift];
}
dolShift++;
if (dolShift >= dolSize)
break;
// Length could be only 1 byte
size = dol[dolShift];
dolShift++;
// Copy data
findData = libemv_get_tag(tag, &findSize);
if (!findData)
findSize = 0;
sizeToCopy = findSize < size ? findSize : size;
if (findData)
{
memcpy(outBuffer + outSize, findData, sizeToCopy);
outSize += sizeToCopy;
}
if (size > findSize)
{
memset(outBuffer + outSize, 0, size - findSize);
outSize += size - findSize;
}
}
return outSize;
}