forked from purdeaandrei/image_meta_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlmem2xmlfile.c
172 lines (156 loc) · 6.04 KB
/
xmlmem2xmlfile.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
/* Copyright (c) 2012, Movidius Ltd.
Released under the BSD 3-clause license.
Author: Purdea Andrei <andrei -at- purdea.ro>
<andrei.purdea -at- movidius.com> */
/* this file contains stuff used to write an mxml memory structure into an xml file */
#include <mxml.h>
#include "xmlmem2xmlfile.h"
#include "common.h"
static char *custom_save_hexdump(mxml_node_t *node) {
if (strcmp(node->parent->value.element.name, "hexdump")==0) {
binary_t *t = (binary_t *)(node->value.custom.data);
size_t size = t->size;
size_t offset = t->offset;
unsigned char *data = t->data;
char *encoded = (char *) malloc(((size + 15) >> 4) * (8 + 1 + 3 * 16 + 1 + 16 + 1) + 1);
char *pos = encoded;
pos[0] = 0; // if it's empty
size_t i;
for (i=0;i<size;i++) {
if ((i & 0x0F) == 0) {
pos += snprintf(pos, 10, "%08lx:", offset + i);
}
pos += snprintf(pos, 4, " %02x", data[i]);
if (i == size - 1) {
size_t j = (i & 0x0F);
for(;j<15;j++) pos += snprintf(pos, 4, " "); // pad the last line with spaces
}
if (((i & 0x0F) == 0x0F) || (i == size - 1)) {
char strpr[17];
size_t j;
for (j=(i & 0x0F);j<16;j--) {
strpr[j] = data[i - ((i & 0x0F) - j)];
if (!isprint(strpr[j])) strpr[j]='.';
switch (strpr[j]) {
case '>':
case '<':
case '"':
case '&':
strpr[j] = '.';
}
}
strpr[(i & 0x0f) + 1] = 0;
pos+=snprintf(pos, 16 + 3, " %s\n", strpr);
}
}
return encoded;
}
return strdup("Error: unknown parent for custom node (looking for: hexdump)\n");
}
static char *custom_save_include(mxml_node_t *node) {
if (strcmp(node->parent->value.element.name, "include")==0) {
const char *bin_filename = mxmlElementGetAttr(node->parent, "path");
FILE *f = fopen(bin_filename, "wb");
if (f==NULL) printf("Error opening for writing: %s\n", bin_filename);
binary_t *cdata = (binary_t *) node->value.custom.data;
if (cdata->size > 0) {
if (1 != fwrite(cdata->data, cdata->size, 1, f)) {
printf("Error writing %ld bytes to file '%s'\n", cdata->size, bin_filename);
}
} else printf("Warning: weird binary data size: %ld\n", cdata->size);
fclose(f);
return strdup("");
}
return strdup("Error: unknown parent for custom node (looking for: include)\n");
}
// this is the callback function used by mxml to add white space for readability:
static const char * whitespace_cb(mxml_node_t *node, int where) {
if (node->type == MXML_ELEMENT && 0==strcmp(node->value.element.name, "include")) {
// make sure includes only occuppy one line
if (where == MXML_WS_AFTER_OPEN || where == MXML_WS_BEFORE_CLOSE) return "";
}
if (where == MXML_WS_AFTER_CLOSE || where == MXML_WS_AFTER_OPEN) return "\n";
static char spaces[100];
int depth = 0;
while (node->parent!=NULL) {
if (depth < 99) {
spaces[depth] = '\t';
spaces[depth + 1] = 0;
}
node = node->parent;
depth++;
}
return spaces + 1;
}
void convert_hexdump(mxml_node_t *xml) {
mxml_node_t *node = xml;
do {
if (node->type==MXML_ELEMENT && 0==strcmp(node->value.element.name, "binary"))
mxmlSetElement(node, "hexdump");
} while (NULL!=(node = mxmlWalkNext(node, xml, MXML_DESCEND)));
}
void revert_hexdump(mxml_node_t *xml) {
mxml_node_t *node = xml;
do {
if (node->type==MXML_ELEMENT && 0==strcmp(node->value.element.name, "hexdump"))
mxmlSetElement(node, "binary");
} while (NULL!=(node = mxmlWalkNext(node, xml, MXML_DESCEND)));
}
void convert_include(mxml_node_t *xml, char *output_filename) {
mxml_node_t *node = xml;
int last_file_index = 0;
do {
if (node->type==MXML_ELEMENT && 0==strcmp(node->value.element.name, "binary")) {
mxmlSetElement(node, "include");
char *bin_filename = (char *) malloc(strlen(output_filename) + strlen(".0000.bin") + 1);
snprintf(bin_filename, strlen(output_filename) + strlen(".0000.bin") + 1, "%s.%04u.bin", output_filename, last_file_index++);
mxmlElementSetAttrf(node, "path", "%s", bin_filename);
free(bin_filename);
}
} while (NULL!=(node = mxmlWalkNext(node, xml, MXML_DESCEND)));
}
void revert_include(mxml_node_t *xml) {
mxml_node_t *node = xml;
do {
if (node->type==MXML_ELEMENT && 0==strcmp(node->value.element.name, "include")) {
mxmlSetElement(node, "binary");
mxmlElementDeleteAttr(node, "path");
}
} while (NULL!=(node = mxmlWalkNext(node, xml, MXML_DESCEND)));
}
void xm2xf_save_xml_filename(mxml_node_t *xml, char * fn, int inline_binary) {
FILE *f = fopen(fn, "w");
if (f==NULL) {
ERROR("Can't open file %s for writing!\n", fn);
}
mxmlSetWrapMargin(0);
if (inline_binary) {
mxmlSetCustomHandlers(NULL, custom_save_hexdump);
convert_hexdump(xml);
} else {
mxmlSetCustomHandlers(NULL, custom_save_include);
convert_include(xml, fn);
}
mxmlSaveFile(xml, f, whitespace_cb);
if (inline_binary) {
revert_hexdump(xml);
} else {
revert_include(xml);
}
fclose(f);
}
void xm2xf_save_xml_file(mxml_node_t *xml, FILE *f) {
mxmlSetWrapMargin(0);
mxmlSetCustomHandlers(NULL, custom_save_hexdump);
convert_hexdump(xml);
mxmlSaveFile(xml, f, whitespace_cb);
revert_hexdump(xml);
}
char *xm2xf_save_xml_mem(mxml_node_t *xml) {
mxmlSetWrapMargin(0);
mxmlSetCustomHandlers(NULL, custom_save_hexdump);
convert_hexdump(xml);
char *data = mxmlSaveAllocString(xml, whitespace_cb);
revert_hexdump(xml);
return data;
}