-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlv.c
72 lines (62 loc) · 2.03 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
#include <libxml/xmlreader.h>
#include <string.h>
#include <stdlib.h>
#include "tlv.h"
static char * outputBuffer;
static char * currentPosition;
inline static void fillInLengthValue(int * isNodeOpened, const char * xmlText) {
uint32_t typeLength = (uint32_t)strlen(xmlText);
memcpy(currentPosition, &typeLength, sizeof(typeLength));
currentPosition += sizeof(typeLength);
memcpy(currentPosition, xmlText, strlen(xmlText));
currentPosition += strlen(xmlText);
*isNodeOpened = 0;
}
static void processNode(xmlTextReaderPtr reader) {
static int isNodeOpened;
uint32_t type;
const char * xmlType;
const char * xmlText;
if (xmlTextReaderDepth(reader)) {
if (isNodeOpened) {
xmlText = (const char *)xmlTextReaderConstValue(reader);
fillInLengthValue(&isNodeOpened, xmlText);
}
if (!isNodeOpened && xmlTextReaderNodeType(reader) == 1) {
xmlType = (const char *)xmlTextReaderConstName(reader);
if (!strcmp(xmlType, "text")) {
type = Text;
} else {
type = Numeric;
}
memcpy(currentPosition, &type, sizeof(type));
currentPosition += sizeof(type);
isNodeOpened = 1;
}
}
}
void streamFile(const char *filename, size_t bufferSize) {
xmlTextReaderPtr reader;
int ret;
FILE * filePtr;
outputBuffer = (char *)malloc(bufferSize);
currentPosition = outputBuffer;
reader = xmlReaderForFile(filename, NULL, 0);
if (reader != NULL) {
ret = xmlTextReaderRead(reader);
while (ret == 1) {
processNode(reader);
ret = xmlTextReaderRead(reader);
}
if (ret) {
fprintf(stderr, "%s : failed to parse\n", filename);
}
} else {
fprintf(stderr, "Unable to open %s\n", filename);
}
filePtr = fopen(outputFilename, "wb");
fwrite(outputBuffer, currentPosition - outputBuffer, 1, filePtr);
fclose(filePtr);
xmlCleanupParser();
free(outputBuffer);
}