forked from jessek/hashdeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxml.cpp
334 lines (286 loc) · 7.08 KB
/
xml.cpp
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
/**
* implementation for C++ XML generation class
*
* The software provided here is released by the Naval Postgraduate
* School, an agency of the U.S. Department of Navy. The software
* bears no warranty, either expressed or implied. NPS does not assume
* legal liability nor responsibility for a User's use of the software
* or the results of such use.
*
* Please note that within the United States, copyright protection,
* under Section 105 of the United States Code, Title 17, is not
* available for any work of the United States Government and/or for
* any works created by United States Government employees. User
* acknowledges that this software contains work which was created by
* NPS government employees and is therefore in the public domain and
* not subject to copyright.
*/
// $Id$
#include "common.h" // normally you remove this
#include "xml.h"
using namespace std;
#include <iostream>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/param.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/stat.h>
// Implementation of mkstemp for windows found on pan-devel mailing
// list archive
// @http://www.mail-archive.com/[email protected]/msg00294.html
#ifndef _S_IREAD
#define _S_IREAD 256
#endif
#ifndef _S_IWRITE
#define _S_IWRITE 128
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef _O_SHORT_LIVED
#define _O_SHORT_LIVED 0
#endif
#ifndef HAVE_MKSTEMP
extern "C"
int mkstemp(char *tmpl)
{
int ret=-1;
mktemp(tmpl); ret=open(tmpl,O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE);
return ret;
}
#endif
static const char *cstr(const string &str){
return str.c_str();
}
static string xml_lt("<");
static string xml_gt(">");
static string xml_am("&");
static string xml_ap("'");
static string xml_qu(""");
string XML::xmlescape(const string &xml)
{
string ret;
for(string::const_iterator i = xml.begin(); i!=xml.end(); i++){
switch(*i){
case '>': ret += xml_gt; break;
case '<': ret += xml_lt; break;
case '&': ret += xml_am; break;
case '\'': ret += xml_ap; break;
case '"': ret += xml_qu; break;
case '\000': break; // remove nulls
default:
ret += *i;
}
}
return ret;
}
/**
* Strip an XML string as necessary for a tag name.
*/
string XML::xmlstrip(const string &xml)
{
string ret;
for(string::const_iterator i = xml.begin(); i!=xml.end(); i++){
if(isprint(*i) && !strchr("<>\r\n&'\"",*i)){
ret += isspace(*i) ? '_' : tolower(*i);
}
}
return ret;
}
/****************************************************************/
XML::XML()
{
out = 0;
make_dtd = false;
tempfile_template = "/tmp/xml_XXXXXXXX"; // a reasonable default
gettimeofday(&t0,0);
}
XML::XML(FILE *out_)
{
out = out_;
make_dtd = false;
tempfile_template = "/tmp/xml_XXXXXXXX"; // a reasonable default
gettimeofday(&t0,0);
open();
}
void XML::set_tempfile_template(std::string temp)
{
tempfile_template = temp;
}
void XML::set_outFILE(FILE *out_)
{
out = out_;
}
void XML::set_outfilename(string outfilename_)
{
outfilename = outfilename_;
tempfile_template = outfilename_ + "_tmp_XXXXXXXX"; // a better default
}
void XML::set_makeDTD(bool flag)
{
make_dtd = flag;
if(make_dtd){ // need to write to a temp file
char buf[1024];
memset(buf,0,sizeof(buf));
strcpy(buf,tempfile_template.c_str());
int fd = mkstemp(buf);
if(fd<0){
perror(buf);
exit(1);
}
tempfilename = buf;
out = fdopen(fd,"r+");
if(!out){
perror("fdopen");
exit(1);
}
}
}
static const char *xml_header = "<?xml version='1.0' encoding='UTF-8'?>\n";
void XML::open()
{
if(out==0) out = fopen(cstr(outfilename),"w");
fputs(xml_header,out); // write out the XML header
}
void XML::close()
{
if(make_dtd){
/* If we are making the DTD, then we have been writing to a temp file.
* Open the real file, write the DTD, and copy over the tempfile.
*/
FILE *rf = fopen(cstr(outfilename),"w");
if(!rf){
perror(cstr(outfilename));
fprintf(stderr,"%s will not be deleted\n",tempfilename.c_str());
exit(1);
}
fseek(out,0L,SEEK_SET);
char buf[65536];
if(fgets(buf,sizeof(buf),out)){
fputs(buf,rf); // copy over first line --- the XML header
}
write_dtd(rf); // write the DTD
while(!feof(out)){ // copy the rest
int r = fread(buf,1,sizeof(buf),out);
if(r<=0) break;
int r2 = fwrite(buf,1,r,rf);
if(r2<0){
fprintf(stderr,"Cannot write to %s\n",cstr(outfilename));
fprintf(stderr,"%s will not be deleted\n",tempfilename.c_str());
exit(1);
}
}
fclose(rf); rf = 0;
unlink(tempfilename.c_str());
}
fclose(out); out = 0;
}
void XML::write_dtd(FILE *f)
{
fprintf(f,"<!DOCTYPE fiwalk\n");
fprintf(f,"[\n");
for(set<string>::const_iterator it = tags.begin(); it != tags.end(); it++){
const char *s = (*it).c_str();
fprintf(f,"<!ELEMENT %s ANY >\n",s);
}
fprintf(f,"<!ATTLIST volume startsector CDATA #IMPLIED>\n");
fprintf(f,"<!ATTLIST run start CDATA #IMPLIED>\n");
fprintf(f,"<!ATTLIST run len CDATA #IMPLIED>\n");
fprintf(f,"]>\n");
}
/**
* make sure that a tag is valid and, if so, add it to the list of tags we use
*/
void XML::verify_tag(string tag)
{
if(tag[0]=='/') tag = tag.substr(1);
if(tag.find(" ") != string::npos){
cerr << "tag '" << tag << "' contains space. Cannot continue.\n";
exit(1);
}
tags.insert(tag);
}
void XML::puts(const string &v)
{
fputs(v.c_str(),out);
}
void XML::spaces()
{
string sp;
for(int i=tag_stack.size();i>0;i--){
sp.push_back(' ');
sp.push_back(' ');
}
puts(sp);
}
void XML::writexml(const string &xml)
{
bool first = true;
for(string::const_iterator it = xml.begin(); it!=xml.end(); it++){
if(first){
spaces();
first = false;
}
fputc(*it,out);
if((*it)=='\n'){
first = true;
}
}
}
void XML::tagout(const string &tag,const string &attribute)
{
verify_tag(tag);
fprintf(out,"<%s%s%s>",cstr(tag),attribute.size()>0 ? " " : "",cstr(attribute));
}
void XML::xmlout(const string &tag,const string &value,const string &attribute,bool escape_value)
{
spaces();
tagout(tag,attribute);
if(escape_value) fputs(cstr(xmlescape(value)),out);
else fputs(cstr(value),out);
tagout("/"+tag,"");
fputc('\n',out);
}
void XML::xmlprintf(const std::string &tag,const std::string &attribute, const char *fmt,...)
{
spaces();
tagout(tag,attribute);
va_list ap;
va_start(ap, fmt);
vfprintf(out,fmt,ap);
va_end(ap);
tagout("/"+tag,"");
fputc('\n',out);
}
void XML::push(const string &tag,const string &attribute)
{
spaces();
tag_stack.push(tag);
tagout(tag,attribute);
fputc('\n',out);
}
void XML::pop()
{
assert(tag_stack.size()>0);
string tag = tag_stack.top();
tag_stack.pop();
spaces();
tagout("/"+tag,"");
fputc('\n',out);
fflush(out);
}
void XML::printf(const char *format,...)
{
va_list ap;
va_start(ap, format);
vfprintf(out,format,ap);
va_end(ap);
}
void XML::comment(const string &comment_)
{
fputs("<!-- ",out);
fputs(comment_.c_str(),out);
fputs(" -->\n",out);
}