-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathAxmlParser.c
995 lines (831 loc) · 19.8 KB
/
AxmlParser.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/* AXML Parser
* https://github.com/claudxiao/AndTools
* Claud Xiao <[email protected]>
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#ifdef _WIN32 /* Windows */
#pragma warning(disable:4996)
#ifndef snprintf
#define snprintf _snprintf
#endif
#endif /* _WIN32 */
#include "AxmlParser.h"
/* chunks' magic numbers */
enum{
CHUNK_HEAD = 0x00080003,
CHUNK_STRING = 0x001c0001,
CHUNK_RESOURCE = 0x00080180,
CHUNK_STARTNS = 0x00100100,
CHUNK_ENDNS = 0x00100101,
CHUNK_STARTTAG = 0x00100102,
CHUNK_ENDTAG = 0x00100103,
CHUNK_TEXT = 0x00100104,
};
/* attributes' types */
enum{
ATTR_NULL = 0,
ATTR_REFERENCE = 1,
ATTR_ATTRIBUTE = 2,
ATTR_STRING = 3,
ATTR_FLOAT = 4,
ATTR_DIMENSION = 5,
ATTR_FRACTION = 6,
ATTR_FIRSTINT = 16,
ATTR_DEC = 16,
ATTR_HEX = 17,
ATTR_BOOLEAN = 18,
ATTR_FIRSTCOLOR = 28,
ATTR_ARGB8 = 28,
ATTR_RGB8 = 29,
ATTR_ARGB4 = 30,
ATTR_RGB4 = 31,
ATTR_LASTCOLOR = 31,
ATTR_LASTINT = 31,
};
/* string table */
typedef struct{
uint32_t count; /* count of all strings */
uint32_t *offsets; /* each string's offset in raw data block */
unsigned char *data; /* raw data block, contains all strings encoded by UTF-16LE */
size_t len; /* length of raw data block */
unsigned char **strings;/* string table, point to strings encoded by UTF-8 */
} StringTable_t;
/* attribute structure within tag */
typedef struct{
uint32_t uri; /* uri of its namespace */
uint32_t name;
uint32_t string; /* attribute value if type == ATTR_STRING */
uint32_t type; /* attribute type, == ATTR_* 指定属性值得类型,如string, int, float等*/
uint32_t data; /* attribute value, encoded on type 属性的值,该值根据属性的 type进行编码*/
} Attribute_t;
typedef struct AttrStack_t{
Attribute_t *list; /* attributes of current tag */
uint32_t count; /* count of these attributes */
struct AttrStack_t *next;
} AttrStack_t;
/* namespace record */
typedef struct NsRecord{
uint32_t prefix;
uint32_t uri;
struct NsRecord *next; /* yes, it's a single linked list */
} NsRecord_t;
/* a parser, also a axml parser handle for user */
typedef struct {
unsigned char *buf; /* origin raw data, to be parsed */
size_t size; /* size of raw data */
size_t cur; /* current parsing position in raw data */
StringTable_t *st;
NsRecord_t *nsList;
int nsNew; /* if a new namespace coming */
uint32_t tagName; /* current tag's name */
uint32_t tagUri; /* current tag's namespace's uri */
uint32_t text; /* when tag is text, its content */
AttrStack_t *attr; /* attributes */
} Parser_t;
/* get a 4-byte integer, and mark as parsed */
/* uses byte oprations to avoid little or big-endian conflict */
uint32_t
GetInt32(Parser_t *ap)
{
uint32_t value = 0;
unsigned char *p = ap->buf + ap->cur;
value = p[0] | p[1]<<8 | p[2]<<16 | p[3]<<24;
ap->cur += 4;
return value;
}
static void
CopyData(Parser_t *ap, unsigned char * to, size_t size)
{
memcpy(to, ap->buf + ap->cur, size);
ap->cur += size;
return;
}
/* skip some uknown of useless fields, don't parse them */
static void
SkipInt32(Parser_t *ap, size_t num)
{
ap->cur += 4 * num;
return;
}
/* if no more byte need to be parsed */
static int
NoMoreData(Parser_t *ap)
{
return ap->cur >= ap->size;
}
static int
ParseHeadChunk(Parser_t *ap)
{
/* file magic */
if(GetInt32(ap) != CHUNK_HEAD)
{
fprintf(stderr, "Error: not valid AXML file.\n");
return -1;
}
/* file size */
if(GetInt32(ap) != ap->size)
{
fprintf(stderr, "Error: not complete file.\n");
return -1;
}
return 0;
}
static int
ParseStringChunk(Parser_t *ap)
{
uint32_t chunkSize;
uint32_t styleCount;
uint32_t stringOffset;
uint32_t styleOffset;
size_t i;
/* chunk type */
if(GetInt32(ap) != CHUNK_STRING)
{
fprintf(stderr, "Error: not valid string chunk.\n");
return -1;
}
/* chunk size */
chunkSize = GetInt32(ap);
/* count of strings */
ap->st->count = GetInt32(ap);
/* count of styles */
styleCount = GetInt32(ap);
/* unknown field */
SkipInt32(ap, 1);
/* offset of string raw data in chunk */
stringOffset = GetInt32(ap);
/* offset of style w=raw data in chunk */
styleOffset = GetInt32(ap);
/* strings' offsets table */
ap->st->offsets = (uint32_t *)malloc(ap->st->count * sizeof(uint32_t));
if(ap->st->offsets == NULL)
{
fprintf(stderr, "Error: init strings' offsets table.\n");
return -1;
}
for(i = 0; i < ap->st->count; i++)
ap->st->offsets[i] = GetInt32(ap);
/* init string table */
ap->st->strings = (unsigned char **)malloc(ap->st->count * sizeof(unsigned char *));
if(ap->st->strings == NULL)
{
fprintf(stderr, "Error: init string table.\n");
free(ap->st->offsets);
ap->st->offsets = NULL;
return -1;
}
for(i = 0; i < ap->st->count; i++)
ap->st->strings[i] = NULL;
/* skip style offset table */
if(styleCount != 0)
SkipInt32(ap, styleCount);
/* save string raw data */
ap->st->len = (styleOffset ? styleOffset : chunkSize) - stringOffset;
ap->st->data = (unsigned char *)malloc(ap->st->len);
if(ap->st->data == NULL)
{
fprintf(stderr, "Error: init string raw data.\n");
free(ap->st->strings);
ap->st->strings = NULL;
free(ap->st->offsets);
ap->st->offsets = NULL;
return -1;
}
CopyData(ap, ap->st->data, ap->st->len);
/*restore styleData startAddr*/
g_styleDataOff = ap->cur;
//printf("styleDataOff is: %08x\n", g_styleDataOff);
/* skip style raw data */
if(styleOffset != 0)
SkipInt32(ap, (chunkSize-styleOffset)/4);
return 0;
}
static int
ParseResourceChunk(Parser_t *ap)
{
uint32_t chunkSize;
/* chunk type */
if(GetInt32(ap) != CHUNK_RESOURCE)
{
fprintf(stderr, "Error: not valid resource chunk.\n");
return -1;
}
/*modify by wan*/
/*get ResourcesChunkSizeOffset*/
g_res_ChunkSizeOffset = ap->cur;
/* chunk size */
chunkSize = GetInt32(ap);
if(chunkSize % 4 != 0)
{
fprintf(stderr, "Error: not valid resource chunk.\n");
return -1;
}
/* skip res id table */
SkipInt32(ap, chunkSize/4-2);
return 0;
}
void *
AxmlOpen(char *buffer, size_t size)
{
Parser_t *ap;
if(buffer == NULL)
{
fprintf(stderr, "Error: AxmlOpen get an invalid parameter.\n");
return NULL;
}
ap = (Parser_t *)malloc(sizeof(Parser_t));
if(ap == NULL)
{
fprintf(stderr, "Error: init parser.\n");
return NULL;
}
/* init parser */
ap->buf = (unsigned char *)buffer;
ap->size = size;
ap->cur = 0;
ap->nsList = NULL;
ap->nsNew = 0;
ap->attr = NULL;
ap->tagName = (uint32_t)(-1);
ap->tagUri = (uint32_t)(-1);
ap->text = (uint32_t)(-1);
ap->st = (StringTable_t *)malloc(sizeof(StringTable_t));
if(ap->st == NULL)
{
fprintf(stderr, "Error: init string table struct.\n");
free(ap);
return NULL;
}
/* parse first three chunks */
if( ParseHeadChunk(ap) != 0 ||
ParseStringChunk(ap) != 0 ||
ParseResourceChunk(ap) != 0 )
{
free(ap->st);
free(ap);
return NULL;
}
return (void *)ap;
}
int
AxmlClose(void *axml)
{
Parser_t *ap;
uint32_t i;
if(axml == NULL)
{
fprintf(stderr, "Error: AxmlClose get an invalid parameter.\n");
return -1;
}
ap = (Parser_t *)axml;
if(ap->st->data)
free(ap->st->data);
if(ap->st->strings)
{
for(i = 0; i < ap->st->count; i++)
if(ap->st->strings[i])
free(ap->st->strings[i]);
free(ap->st->strings);
}
if(ap->st->offsets)
free(ap->st->offsets);
if(ap->st)
free(ap->st);
if(ap)
free(ap);
return 0;
}
AxmlEvent_t
AxmlNext(void *axml)
{
static AxmlEvent_t event = -1;
char *tagname;
int flag = 0;
Parser_t *ap;
uint32_t chunkType;
/* when init */
if(event == -1)
{
event = AE_STARTDOC;
return event;
}
ap = (Parser_t *)axml;
/* when buffer ends */
if(NoMoreData(ap))
event = AE_ENDDOC;
if(event == AE_ENDDOC)
return event;
/* common chunk head */
chunkType = GetInt32(ap); //再次取得chunk头部类型标志
SkipInt32(ap, 1); /* chunk size, unused */
SkipInt32(ap, 1); /* line number, unused */
SkipInt32(ap, 1); /* unknown field */
if(chunkType == CHUNK_STARTTAG)
{
uint32_t i;
AttrStack_t *attr;
attr = (AttrStack_t *)malloc(sizeof(AttrStack_t));
if(attr == NULL)
{
fprintf(stderr, "Error: init attribute.\n");
return AE_ERROR;
}
ap->tagUri = GetInt32(ap);
ap->tagName = GetInt32(ap);
/*modify by wan*/
tagname = AxmlGetTagName(ap);
if(!strcmp(tagname, "application")){
g_appTag_nameOff = ap->cur - 4;
flag = 1;
//printf("[Test]g_appTag_nameOff is %08x\nname:%08x\n", g_appTag_nameOff, ap->tagName);
}
SkipInt32(ap, 1); /* flags, unknown usage */
attr->count = GetInt32(ap) & 0x0000ffff;
SkipInt32(ap, 1); /* classAttribute, unknown usage */
attr->list = (Attribute_t *)malloc(
attr->count * sizeof(Attribute_t));
if(attr->list == NULL)
{
fprintf(stderr, "Error: init attribute list.\n");
free(attr);
return AE_ERROR;
}
/* attribute list */
for(i = 0; i < attr->count; i++)
{
attr->list[i].uri = GetInt32(ap);
attr->list[i].name = GetInt32(ap);
attr->list[i].string = GetInt32(ap);
/* note: type must >> 24 */
attr->list[i].type = GetInt32(ap) >> 24;
attr->list[i].data = GetInt32(ap);
}
/*modify by wan*/
/*获取uri方便后面构造attr的时候设置attr的uri值*/
if(flag){ //说明处于application tag,那么就获取该tag的第一个attr的uri值
g_appURIindex = attr->list[0].uri;
}
attr->next = ap->attr;
ap->attr = attr;
event = AE_STARTTAG;
}
else if(chunkType == CHUNK_ENDTAG)
{
AttrStack_t *attr;
ap->tagUri = GetInt32(ap);
ap->tagName = GetInt32(ap);
if(ap->attr != NULL)
{
attr = ap->attr;
ap->attr = ap->attr->next;
free(attr->list);
free(attr);
}
event = AE_ENDTAG;
}
else if(chunkType == CHUNK_STARTNS)
{
NsRecord_t *ns = (NsRecord_t *)malloc(sizeof(NsRecord_t));
if(ns == NULL)
{
fprintf(stderr, "Error: init namespace.\n");
return AE_ERROR;
}
ns->prefix = GetInt32(ap);
ns->uri = GetInt32(ap);
ns->next = ap->nsList;
ap->nsList = ns;
/* get a new namespace */
ap->nsNew = 1;
/* note this recursion rather than return a event*/
return AxmlNext(ap); //注意这里是递归地进行遍历 !!
}
else if(chunkType == CHUNK_ENDNS)
{
NsRecord_t *ns = ap->nsList;
if(ns == NULL)
{
fprintf(stderr, "Error: end a namespace.\n");
return AE_ERROR;
}
SkipInt32(ap, 1); /* ended prefix */
SkipInt32(ap, 1); /* ended uri */
ap->nsList = ns->next;
free(ns);
/* note this recursion rather than return a event*/
return AxmlNext(ap);
}
else if(chunkType == CHUNK_TEXT)
{
ap->text = GetInt32(ap);
SkipInt32(ap, 2); /* unknown fields */
event = AE_TEXT;
}
else
{
event = AE_ERROR;
}
return event;
}
/** \brief Convert UTF-16LE string into UTF-8 string
*
* You must call this function with to=NULL firstly to get UTF-8 size;
* then you should alloc enough memory to the string;
* at last call this function again to convert actually.
* \param to Pointer to target UTF-8 string
* \param from Pointer to source UTF-16LE string
* \param nch Count of UTF-16LE characters, including terminal zero
* \retval -1 Converting error.
* \retval positive Bytes of UTF-8 string, including terminal zero.
*/
static size_t
UTF16LEtoUTF8(unsigned char *to, unsigned char *from, size_t nch)
{
size_t total = 0;
while(nch > 0)
{
uint32_t ucs4;
size_t count;
/* utf-16le -> ucs-4, defined in RFC 2781 */
ucs4 = from[0] + (from[1]<<8);
from += 2;
nch--;
if(ucs4 < 0xd800 || ucs4 > 0xdfff)
{
;
}
else if(ucs4 >= 0xd800 && ucs4 <= 0xdbff)
{
unsigned int ext;
if(nch <= 0)
return -1;
ext = from[0] + (from[1]<<8);
from += 2;
nch--;
if(ext < 0xdc00 || ext >0xdfff)
return -1;
ucs4 = ((ucs4 & 0x3ff)<<10) + (ext & 0x3ff) + 0x10000;
}
else
{
return -1;
}
/* ucs-4 -> utf-8, defined in RFC 2279 */
if(ucs4 < 0x80) count = 1;
else if(ucs4 < 0x800) count = 2;
else if(ucs4 < 0x10000) count = 3;
else if(ucs4 < 0x200000) count = 4;
else if(ucs4 < 0x4000000) count = 5;
else if(ucs4 < 0x80000000) count = 6;
else return 0;
total += count;
if(to == NULL)
continue;
switch(count)
{
case 6: to[5] = 0x80 | (ucs4 & 0x3f); ucs4 >>= 6; ucs4 |= 0x4000000;
case 5: to[4] = 0x80 | (ucs4 & 0x3f); ucs4 >>= 6; ucs4 |= 0x200000;
case 4: to[3] = 0x80 | (ucs4 & 0x3f); ucs4 >>= 6; ucs4 |= 0x10000;
case 3: to[2] = 0x80 | (ucs4 & 0x3f); ucs4 >>= 6; ucs4 |= 0x800;
case 2: to[1] = 0x80 | (ucs4 & 0x3f); ucs4 >>= 6; ucs4 |= 0xc0;
case 1: to[0] = ucs4; break;
}
to += count;
}
if(to != NULL)
to[0] = '\0';
return total+1;
}
static char *
GetString(Parser_t *ap, uint32_t id)
{
static char *emptyString = "";
unsigned char *offset;
uint16_t chNum;
size_t size;
/* out of index range */
if(id >= ap->st->count)
return emptyString;
/* already parsed, directly use previous result */
if(ap->st->strings[id] != NULL)
return (char *)(ap->st->strings[id]);
/* point to string's raw data */
offset = ap->st->data + ap->st->offsets[id];
/* its first 2 bytes is string's characters count */
chNum = *(uint16_t *)offset;
size = UTF16LEtoUTF8(NULL, offset+2, (size_t)chNum);
if(size == (size_t)-1)
return emptyString;
ap->st->strings[id] = (unsigned char *)malloc(size);
if(ap->st->strings[id] == NULL)
return emptyString;
UTF16LEtoUTF8(ap->st->strings[id], offset+2, (size_t)chNum);
return (char *)(ap->st->strings[id]);
}
char *
AxmlGetTagName(void *axml)
{
Parser_t *ap;
ap = (Parser_t *)axml;
return GetString(ap, ap->tagName);
}
char *
AxmlGetTagPrefix(void *axml)
{
Parser_t *ap;
NsRecord_t *ns;
uint32_t nodePrefix = 0xffffffff;
ap = (Parser_t *)axml;
for(ns = ap->nsList; ns != NULL; ns = ns->next)
{
if(ns->uri == ap->tagUri)
nodePrefix = ns->prefix;
}
/* mention that when index out of range, GetString() returns empty string */
return GetString(ap, nodePrefix);
}
char *
AxmlGetText(void *axml)
{
Parser_t *ap;
ap = (Parser_t *)axml;
return GetString(ap, ap->text);
}
uint32_t
AxmlGetAttrCount(void *axml)
{
Parser_t *ap;
ap = (Parser_t *)axml;
return ap->attr->count;
}
char *
AxmlGetAttrPrefix(void *axml, uint32_t i)
{
Parser_t *ap;
NsRecord_t *ns;
uint32_t prefix = 0xffffffff;
uint32_t uri;
ap = (Parser_t *)axml;
uri = ap->attr->list[i].uri;
for(ns = ap->nsList; ns != NULL; ns = ns->next)
{
if(ns->uri == uri)
prefix = ns->prefix;
}
return GetString(ap, prefix);
}
char *
AxmlGetAttrName(void *axml, uint32_t i)
{
Parser_t *ap;
ap = (Parser_t *)axml;
return GetString(ap, ap->attr->list[i].name);
}
char *
AxmlGetAttrValue(void *axml, uint32_t i)
{
static float RadixTable[] = {0.00390625f, 3.051758E-005f, 1.192093E-007f, 4.656613E-010f};
static char *DimemsionTable[] = {"px", "dip", "sp", "pt", "in", "mm", "", ""};
static char *FractionTable[] = {"%", "%p", "", "", "", "", "", ""};
Parser_t *ap;
uint32_t type;
uint32_t data;
char *buf;
ap = (Parser_t *)axml;
type = ap->attr->list[i].type;
if(type == ATTR_STRING)
{
char *str;
str = GetString(ap, ap->attr->list[i].string);
/* free by user */
buf = (char *)malloc(strlen(str)+1);
memset(buf, 0, strlen(str)+1);
strncpy(buf, str, strlen(str));
return buf;
}
data = ap->attr->list[i].data;
/* free by user */
buf = (char *)malloc(32);
memset(buf, 0 ,32);
if(type == ATTR_NULL)
{
;
}
else if(type == ATTR_REFERENCE)
{
if(data>>24 == 1)
snprintf(buf, 18, "@android:%08X", data);
else
snprintf(buf, 10, "@%08X", data);
}
else if(type == ATTR_ATTRIBUTE)
{
if(data>>24 == 1)
snprintf(buf, 18, "?android:%08x", data);
else
snprintf(buf, 10, "?%08X", data);
}
else if(type == ATTR_FLOAT)
{
snprintf(buf, 20, "%g", *(float *)&data);
}
else if(type == ATTR_DIMENSION)
{
snprintf(buf, 20, "%f%s",
(float)(data & 0xffffff00) * RadixTable[(data >> 4) & 0x03],
DimemsionTable[data & 0x0f] );
}
else if(type == ATTR_FRACTION)
{
snprintf(buf, 20, "%f%s",
(float)(data & 0xffffff00) * RadixTable[(data >> 4) & 0x03],
FractionTable[data & 0x0f] );
}
else if(type == ATTR_HEX)
{
snprintf(buf, 11, "0x%08x", data);
}
else if(type == ATTR_BOOLEAN)
{
if(data == 0)
strncpy(buf, "false", 32);
else
strncpy(buf, "true", 32);
}
else if(type >= ATTR_FIRSTCOLOR && type <= ATTR_LASTCOLOR)
{
snprintf(buf, 10, "#%08x", data);
}
else if(type >= ATTR_FIRSTINT && type <= ATTR_LASTINT)
{
snprintf(buf, 32, "%d", data);
}
else
{
snprintf(buf, 32, "<0x%x, type 0x%02x>", data, type);
}
return buf;
}
int
AxmlNewNamespace(void *axml)
{
Parser_t *ap;
ap = (Parser_t *)axml;
if(ap->nsNew == 0)
return 0;
else
{
ap->nsNew = 0;
return 1;
}
}
char *
AxmlGetNsPrefix(void *axml)
{
Parser_t *ap;
ap = (Parser_t *)axml;
return GetString(ap, ap->nsList->prefix);
}
char *
AxmlGetNsUri(void *axml)
{
Parser_t *ap;
ap = (Parser_t *)axml;
return GetString(ap, ap->nsList->uri);
}
typedef struct{
char *data;
size_t size;
size_t cur;
} Buff_t;
static int
InitBuff(Buff_t *buf)
{
if(buf == NULL)
return -1;
buf->size = 32*1024;
buf->data = (char *)malloc(buf->size);
if(buf->data == NULL)
{
fprintf(stderr, "Error: init buffer.\n");
return -1;
}
buf->cur = 0;
return 0;
}
/*
格式化打印字符串到buff数组中。这里的buff数组就是我们看到的文本格式的xml
*/
static int
PrintToBuff(Buff_t *buf, size_t maxlen, const char *format, ...)
{
va_list ap;
size_t len;
if(maxlen >= buf->size - buf->cur) //如果buf的剩余空间小于等于max的话,就需要追加分配空间
{
buf->size += 32*1024; //一次性追加分配2^15字节
buf->data = (char *)realloc(buf->data, buf->size);
if(buf->data == NULL)
{
fprintf(stderr, "Error: realloc buffer.\n");
return -1;
}
}
va_start(ap, format); //初始化,让ap指向可变参数列表的第一个参数
vsnprintf(buf->data + buf->cur, buf->size - buf->cur, format, ap); //将可变参数格式化输出到一个字符数组
va_end(ap); //关闭ap指针。确保程序的健壮性
len = strlen(buf->data + buf->cur); //获取输入的实际字符长度
if(len > maxlen)
{
fprintf(stderr, "Error: length more than expected.\n");
return -1;
}
buf->cur += len;
return 0;
}
int
AxmlToXml(char **outbuf, size_t *outsize, char *inbuf, size_t insize)
{
void *axml;
AxmlEvent_t event;
Buff_t buf;
int tabCnt = 0;
if(InitBuff(&buf) != 0)
return -1;
axml = AxmlOpen(inbuf, insize);
if(axml == NULL)
return -1;
while((event = AxmlNext(axml)) != AE_ENDDOC)
{
char *prefix;
char *name;
char *value;
uint32_t i, n;
switch(event){
case AE_STARTDOC: //如果是文档开始标志,就打印下面字符串到buff
PrintToBuff(&buf, 50, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
break;
case AE_STARTTAG:
PrintToBuff(&buf, tabCnt*4+1, "%*s", tabCnt*4, "");
tabCnt++;
prefix = AxmlGetTagPrefix(axml);
name = AxmlGetTagName(axml);
if(strlen(prefix) != 0)
PrintToBuff(&buf, strlen(prefix)+strlen(name)+5, "<%s:%s ", prefix, name);
else
PrintToBuff(&buf, strlen(name)+3, "<%s ", name);
if(AxmlNewNamespace(axml))
{
prefix = AxmlGetNsPrefix(axml);
name = AxmlGetNsUri(axml);
PrintToBuff(&buf, strlen(prefix)+strlen(name)+12, "xmlns:%s=\"%s\" ", prefix, name);
}
n = AxmlGetAttrCount(axml);
for(i = 0; i < n; i++)
{
prefix = AxmlGetAttrPrefix(axml, i);
name = AxmlGetAttrName(axml, i);
value = AxmlGetAttrValue(axml, i);
if(strlen(prefix) != 0)
PrintToBuff(&buf, strlen(prefix)+strlen(name)+strlen(value)+8,
"%s:%s=\"%s\" ", prefix, name, value);
else
PrintToBuff(&buf, strlen(name)+strlen(value)+6, "%s=\"%s\" ", name, value);
/* must manually free attribute value here */
free(value);
}
PrintToBuff(&buf, 3, ">\n");
break;
case AE_ENDTAG:
--tabCnt;
PrintToBuff(&buf, tabCnt*4+1, "%*s", tabCnt*4, "");
prefix = AxmlGetTagPrefix(axml);
name = AxmlGetTagName(axml);
if(strlen(prefix) != 0)
PrintToBuff(&buf, strlen(prefix)+strlen(name)+7, "</%s:%s>\n", prefix, name);
else
PrintToBuff(&buf, strlen(name)+5, "</%s>\n", name);
break;
case AE_TEXT:
name = AxmlGetText(axml);
PrintToBuff(&buf, strlen(name)+2, "%s\n", name);
break;
case AE_ERROR:
fprintf(stderr, "Error: AxmlNext() returns a AE_ERROR event.\n");
AxmlClose(axml);
return -1;
break;
default:
break;
}
}
AxmlClose(axml);
(*outbuf) = buf.data;
(*outsize) = buf.cur;
return 0;
}