-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtlvparse.c
More file actions
382 lines (341 loc) · 7.18 KB
/
tlvparse.c
File metadata and controls
382 lines (341 loc) · 7.18 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*****************************************************************************
* 文件描述:PBOC3.0-TLV Parse
* 应用平台:linux
* 创建时间:20150918
* 作者:RedXu
*****************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "tlvparse.h"
/**
* 检查bit位是否为真
* @param value [待检测值]
* @param bit [1~8位 低~高]
* @return [1 真 0 假]
*/
static inline int CheckBit(unsigned char value, int bit)
{
unsigned char bitvalue[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
if((bit >= 1)&&(bit <= 8))
{
if(value & bitvalue[bit-1])
return(1);
else
return(0);
}
else
{
printf("FILE: %s LINE: %d -- 传入的函数参数错误! bit=[%d]\n",
__FILE__, __LINE__, bit);
return(-1);
}
}
/**
* Create TLVNode
* @return [description]
*/
static struct TLVNode* TLV_CreateNode(void)
{
struct TLVNode* node = (struct TLVNode *)malloc(sizeof(*node));
memset(node,0,sizeof(*node));
return node;
}
/**
* Free TLVNode Memory
* @param node [node]
*/
void TLV_Free(struct TLVNode* node)
{
int i;
if(node == NULL)
{
return;
}
if(node->Value)
free(node->Value);
for(i=0;i<node->SubCount;i++)
{
TLV_Free(node->Sub[i]);
node->Sub[i] = NULL;
}
if(node->Next)
{
TLV_Free(node->Next);
node->Next = NULL;
}
free(node);
}
/**
* 打印TLV子结点信息(深度优先)
* @param parent [description]
* @param step [description]
*/
static void TLV_DebugSubNode(struct TLVNode* parent,int step)
{
int i,j;
for(i=0;i<parent->SubCount;i++)
{
struct TLVNode* sub = parent->Sub[i];
printf("[%d]SUBTAG=%04X LEN=[%d] ADDR=[%p]\n",step,sub->Tag,sub->Length,sub);
for(j=0;j<sub->Length;j++)
{
printf("%02X ",sub->Value[j]);
}
printf("\n");
printf("[%d]MoreFlag=%d SubFlag=%d\n",step,sub->MoreFlag,sub->SubFlag);
printf("---------------------------\n");
if(sub->SubFlag == 1)
{
TLV_DebugSubNode(sub,step+1);
}
}
}
/**
* DEBUG TLVNode
* @param node [description]
*/
void TLV_Debug(struct TLVNode* node)
{
int i;
printf("\n************************BEGIN TLV DEBUG************************\n");
printf("TAG=%04X LEN=[%d] ADDR=[%p]\n",node->Tag,node->Length,node);
for(i=0;i<node->Length;i++)
{
printf("%02X ",node->Value[i]);
}
printf("\n");
printf("MoreFlag=%d SubFlag=%d\n",node->MoreFlag,node->SubFlag);
if(node->SubFlag == 1)
{
TLV_DebugSubNode(node,1);
}
if(node->Next)
{
printf("Next=[%p]\n",node->Next);
}
printf("************************END TLV DEBUG************************\n");
}
/**
* 解析一条数据到TLVNode结构
* @param buf [description]
* @param size [description]
* @param sflag [description]
* @return [description]
*/
static struct TLVNode* TLV_Parse_One(unsigned char* buf,int size)
{
int index = 0;
int i;
uint16_t tag1,tag2,tagsize;
uint16_t len,lensize;
unsigned char* value;
struct TLVNode* node = TLV_CreateNode();
tag1 = tag2 = 0;
tagsize = 1;
tag1 = buf[index++];
if((tag1 & 0x1f) == 0x1f)
{
tagsize++;
tag2 = buf[index++];
//tag2 b8 must be 0!
}
if(tagsize == 1)
node->Tag = tag1;
else
node->Tag = (tag1<<8) + tag2;
node->TagSize = tagsize;
//SubFlag
node->SubFlag = CheckBit(tag1,6);
//L zone
len = 0;
lensize = 1;
len = buf[index++];
if(CheckBit(len,8) == 0)
{
node->Length = len;
}
else
{
lensize = len & 0x7f;
len = 0;
for(i=0;i<lensize;i++)
{
//不确定长度是否是小端编码?
len += (uint16_t)buf[index++] << (i*8);
}
//fix lensize
lensize++;
}
node->Length = len;
node->LengthSize = lensize;
//V zone
value = (unsigned char *)malloc(len);
memcpy(value,buf+index,len);
node->Value = value;
index += len;
if(index < size)
{
node->MoreFlag = 1;
}
else if(index == size)
{
node->MoreFlag = 0;
}
else
{
printf("Parse Error! index=%d size=%d\n",index,size);
}
return node;
}
/**
* [TLV_Parse_More description]
* @param parent [description]
* @return [1 more 0 over]
*/
static int TLV_Parse_SubNodes(struct TLVNode* parent)
{
//check have more
int sublen = 0;
int i;
//have no subtag!
if(parent->SubFlag == 0)
return 0;
for(i=0;i<parent->SubCount;i++)
{
sublen += (parent->Sub[i]->TagSize + parent->Sub[i]->Length + parent->Sub[i]->LengthSize);
}
if(sublen < parent->Length)
{
struct TLVNode* subnode = TLV_Parse_One(parent->Value+sublen,parent->Length-sublen);
parent->Sub[parent->SubCount++] = subnode;
return subnode->MoreFlag;
}
else
{
return 0;
}
}
/**
* 解析子段内嵌数据(广度优先)
* @param parent [description]
*/
static void TLV_Parse_Sub(struct TLVNode* parent)
{
int i;
if(parent->SubFlag != 0)
{
//parse sub nodes.
while(TLV_Parse_SubNodes(parent) != 0)
{
}
for(i=0;i<parent->SubCount;i++)
{
if(parent->Sub[i]->SubFlag != 0)
{
TLV_Parse_Sub(parent->Sub[i]);
}
}
}
}
/**
* 解析数据,生成TLV结构
* @param buf [数据]
* @param size [数据长度]
* @return [TLVNode]
*/
struct TLVNode* TLV_Parse(unsigned char* buf,int size)
{
struct TLVNode* node = TLV_Parse_One(buf,size);
TLV_Parse_Sub(node);
return node;
}
/**
* 拷贝一个节点到另一个结点(不包含Next)
* @param dst [description]
* @param src [description]
*/
static void TLV_Copy(struct TLVNode* dst,struct TLVNode* src)
{
int i;
memcpy(dst,src,sizeof(*src));
dst->Value = (unsigned char*)malloc(src->Length);
memcpy(dst->Value,src->Value,src->Length);
for(i=0;i<src->SubCount;i++)
{
dst->Sub[i] = TLV_CreateNode();
TLV_Copy(dst->Sub[i],src->Sub[i]);
}
}
/**
* 合并src结构到target
* @param target [目标TLVNode]
* @param src [源TLVNode]
*/
void TLV_Merge(struct TLVNode* target,struct TLVNode* src)
{
int i;
int found = 0;
struct TLVNode* tmpnode = target;
assert(target != NULL);
while(tmpnode)
{
//只比较一级tag是否相同,子tag必须是不同的
if(tmpnode->Tag == src->Tag)
{
found = 1;
break;
}
if(tmpnode->Next)
tmpnode = tmpnode->Next;
else
break;
}
if(found == 0)
{
tmpnode->Next = TLV_CreateNode();
TLV_Copy(tmpnode->Next,src);
}
else
{
for(i=0;i<src->SubCount;i++)
{
tmpnode->Sub[tmpnode->SubCount] = TLV_CreateNode();
TLV_Copy(tmpnode->Sub[tmpnode->SubCount],src->Sub[i]);
tmpnode->SubCount++;
tmpnode->SubFlag = 1;
}
}
}
/**
* 在node中查找tag.
* @param node [TLVNode]
* @param tag [tag标签]
* @return [NULL - 未找到]
*/
struct TLVNode* TLV_Find(struct TLVNode* node,uint16_t tag)
{
int i;
struct TLVNode* tmpnode;
if(node->Tag == tag)
{
return node;
}
for(i=0;i<node->SubCount;i++)
{
tmpnode = NULL;
tmpnode = TLV_Find(node->Sub[i],tag);
if(tmpnode != NULL)
return tmpnode;
}
if(node->Next)
{
tmpnode = NULL;
tmpnode = TLV_Find(node->Next,tag);
if(tmpnode != NULL)
return tmpnode;
}
return NULL;
}