-
Notifications
You must be signed in to change notification settings - Fork 13
/
xml.c
811 lines (679 loc) · 24.1 KB
/
xml.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
/* #############################################################################
* code for all xml stuff
* #############################################################################
* Copyright (C) 2005-2009 Harry Brueckner
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contact: Harry Brueckner <harry_b@mm.st>
* Muenchener Strasse 12a
* 85253 Kleinberghofen
* Germany
* #############################################################################
*/
/* #############################################################################
* includes
*/
#include "cpm.h"
#include <stdarg.h>
#ifdef HAVE_LIBZ
#include <zlib.h>
#endif
#include "configuration.h"
#include "general.h"
#include "gpg.h"
#include "interface_keys.h"
#include "interface_xml.h"
#include "listhandler.h"
#include "memory.h"
#include "string.h"
#include "xml.h"
#include "zlib.h"
/* #############################################################################
* internal functions
*/
int checkDtd(SHOWERROR_FN showerror_cb);
void xmlRemoveDtd(void);
void xmlVersionNodeUpdate(long oldversion, xmlNode* rootnode);
void xmlVersionUpdate(int silent);
void xmlValidateError(void* context, const char* msg, ...);
void xmlValidateWarning(void* context, const char* msg, ...);
/* #############################################################################
* global variables
*/
xmlDocPtr xmldoc;
SHOWERROR_FN validateShowError = NULL;
const static char* dtd_1 =
"<!ENTITY % creation \"\n"
" created-by CDATA #REQUIRED\n"
" created-on CDATA #REQUIRED\n"
" \">\n"
"<!ENTITY % creationtime \"\n"
" created-on CDATA #REQUIRED\n"
" \">\n"
"<!ENTITY % modification \"\n"
" modified-by CDATA #IMPLIED\n"
" modified-on CDATA #IMPLIED\n"
" \">\n"
"<!ENTITY % modificationtime \"\n"
" modified-on CDATA #IMPLIED\n"
" \">\n"
"<!ELEMENT root ((node*|template?|editor?)*)>\n"
"<!ATTLIST root\n"
" version CDATA #REQUIRED\n"
" %creation;\n"
" %modification;\n"
">\n";
const static char* dtd_2 =
"<!ELEMENT comment (#PCDATA)>\n"
"<!ATTLIST comment\n"
" %creation;\n"
" %modification;\n"
">\n"
"<!ELEMENT editor (user*)>\n"
"<!ATTLIST editor\n"
" %creationtime;\n"
">\n"
"<!ELEMENT node ((comment?|node*)*)>\n"
"<!ATTLIST node\n"
" %creation;\n"
" %modification;\n"
" label CDATA #REQUIRED\n"
">\n"
"<!ELEMENT template (title*)>\n"
"<!ATTLIST template\n"
" %creationtime;\n"
" %modification;\n"
">\n";
const static char* dtd_3 =
"<!ELEMENT title (#PCDATA)>\n"
"<!ATTLIST title\n"
" %creationtime;\n"
" %modificationtime;\n"
" level CDATA #REQUIRED\n"
">\n"
"<!ELEMENT user (#PCDATA)>\n"
"<!ATTLIST user\n"
" uid CDATA #REQUIRED\n"
" %creationtime;\n"
">\n";
/* #############################################################################
*
* Description add a DTD to the xml document
* Author Harry Brueckner
* Date 2005-05-04
* Arguments SHOWERROR_FN - callback function for error messages
* Return -1 if the DTD could not be updated, 0 if the document did not
* validate and 1 if everything is ok
*/
int checkDtd(SHOWERROR_FN showerror_cb)
{
xmlDtd* dtd;
xmlParserInputBuffer* inputbuffer;
xmlValidCtxt* context = xmlNewValidCtxt();
char* dtdbuffer;
TRACE(99, "checkDtd()", NULL);
if (!xmldoc)
{ return -1; }
xmlRemoveDtd();
/* first we create the DTD as a whole */
dtdbuffer = memAlloc(__FILE__, __LINE__,
strlen(dtd_1) + strlen(dtd_2) + strlen(dtd_3) + 1);
if (!dtdbuffer)
{ return -1; }
strStrncpy(dtdbuffer, dtd_1, strlen(dtd_1) + 1);
strStrncat(dtdbuffer, dtd_2, strlen(dtd_2) + 1);
strStrncat(dtdbuffer, dtd_3, strlen(dtd_3) + 1);
/* create the xml buffer */
inputbuffer = xmlParserInputBufferCreateMem(dtdbuffer, strlen(dtdbuffer),
XML_CHAR_ENCODING_8859_1);
memFreeString(__FILE__, __LINE__, dtdbuffer);
if (!dtdbuffer)
{ return -1; }
/* and finally create the DTD */
dtd = xmlIOParseDTD(NULL, inputbuffer, XML_CHAR_ENCODING_8859_1);
if (!dtd)
{ return -1; }
/* and attach the DTD to the document */
if (!xmldoc -> children)
{ xmlAddChild((xmlNode*)xmldoc, (xmlNode*)dtd); }
else
{ xmlAddPrevSibling(xmldoc -> children, (xmlNode*)dtd); }
/* we set our own error handler */
validateShowError = showerror_cb;
context -> error = xmlValidateError;
context -> warning = xmlValidateWarning;
return xmlValidateDtd(context, xmldoc, dtd);
}
/* #############################################################################
*
* Description free the XML parser stuff
* Author Harry Brueckner
* Date 2005-03-18
* Arguments void
* Return void
*/
void freeXML(void)
{
TRACE(99, "freeXML()", NULL);
if (xmldoc)
{ xmlFreeDoc(xmldoc); }
/* cleanup function for the XML library. */
xmlCleanupParser();
/* this is to debug memory for regression tests */
xmlMemoryDump();
}
/* #############################################################################
*
* Description initialize the XML parser
* Author Harry Brueckner
* Date 2005-03-18
* Arguments void
* Return void
*/
void initXML(void)
{
TRACE(99, "initXML()", NULL);
xmldoc = NULL;
/* this initialize the library and check potential ABI mismatches
* between the version it was compiled for and the actual shared
* library used.
*/
LIBXML_TEST_VERSION
}
/* #############################################################################
*
* Description read, decrypt and parse the given filename
* Author Harry Brueckner
* Date 2005-03-18
* Arguments char* filename - filename to read
* char** errormsg - pointer to the GpgMe error message, if any
* PASSPHRASE_FN - passphrase callback function
* SHOWERROR_FN - callback function for error messages
* Return 1 on error, otherwise 0
*/
int xmlDataFileRead(char* filename, char** errormsg,
PASSPHRASE_FN passphrase_cb, SHOWERROR_FN showerror_cb)
{
xmlNode* node;
struct stat filestat;
off_t size;
int error = 0,
fd,
gpgsize = 0,
validate;
char* buffer = NULL;
char* gpgbuffer = NULL;
char* tmpbuffer = NULL;
TRACE(99, "xmlDataFileRead()", NULL);
/* we initialize the error message */
*errormsg = NULL;
fd = fileLockOpen(filename, O_RDONLY, -1, &tmpbuffer);
if (fd == -1)
{ /* unable to read the file, we start a new one */
showerror_cb(_("file error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
if (fileExists(filename))
{ /* if the file exists and we can't read it it's a hard error and
* we must not continue; we don't need another error message since
* we already had one displayed.
*/
return 1;
}
/* we create a new, empty document */
xmldoc = xmlNewDoc(BAD_CAST "1.0");
node = xmlNewDocNode(xmldoc, NULL, BAD_CAST "root", NULL);
xmlDocSetRootElement(xmldoc, node);
createEditorsNode();
createTemplateNode();
/* we insert the current version and also create the created and
* modified entries.
*/
xmlVersionUpdate(1);
checkDtd(showerror_cb);
keyDefaults();
/* we created a new document, so it's changed */
runtime -> datachanged = 1;
}
else if (!fstat(fd, &filestat) &&
filestat.st_size)
{ /* we found a file and have to read it */
size = filestat.st_size;
buffer = memAlloc(__FILE__, __LINE__, size);
/* Flawfinder: ignore */
if (read(fd, buffer, size) != size)
{
memFree(__FILE__, __LINE__, buffer, size);
return 1;
}
close(fd);
if (config -> encryptdata)
{
error = gpgDecrypt(buffer, size, &gpgbuffer, &gpgsize,
passphrase_cb, showerror_cb);
if (error)
{ *errormsg = _("could not decrypt database file."); }
/* since we decrypted the file, we swap buffers */
memFree(__FILE__, __LINE__, buffer, size);
buffer = gpgbuffer;
size = gpgsize;
#ifdef TEST_OPTION
if (config -> testrun &&
!strcmp("decrypt", config -> testrun) &&
buffer)
{ printf("%s", buffer); }
#endif
/* TODO: add the same keys as used in the encrypted file;
* For now we just always add the default keys
*/
if (runtime -> realmhint)
{ /* if we have a realm, we add it to the default keys since we
* probably want to encrypt data for ourselves as well
*/
config -> defaultkeys = listAdd(config -> defaultkeys,
runtime -> realmhint);
}
keyDefaults();
}
else
{
/* if we run in unencrypted mode, we must add all default keys */
keyDefaults();
showerror_cb(_("warning"), _("the database file is read in unecrypted mode."));
}
if (!error &&
buffer[0] == '\x1f' && buffer[1] == '\x8b')
{ /* we have a gzipped buffer and must compress it */
error = zlibDecompress(buffer, size, &gpgbuffer, &gpgsize,
errormsg);
if (error)
{
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
snprintf(tmpbuffer, STDBUFFERLENGTH,
_("error (%s) compressing file '%s'."),
*errormsg,
filename);
showerror_cb(_("compression error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
memFree(__FILE__, __LINE__, buffer, size);
return 1;
}
memFree(__FILE__, __LINE__, buffer, size);
buffer = gpgbuffer;
size = gpgsize;
}
else
{
if (!error && config -> compression > 0)
{
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
snprintf(tmpbuffer, STDBUFFERLENGTH,
_("database '%s' was not compressed."),
filename);
showerror_cb(_("warning"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
}
}
if (!error)
{
xmldoc = xmlReadMemory(buffer, size, filename, config -> encoding,
XML_PARSE_PEDANTIC | XML_PARSE_NONET | XML_PARSE_NOCDATA);
if (!xmldoc)
{
memFree(__FILE__, __LINE__, buffer, size);
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
snprintf(tmpbuffer, STDBUFFERLENGTH,
_("failed to parse xml document '%s'."),
filename);
showerror_cb(_("file error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
return 1;
}
}
if (buffer)
{ memFree(__FILE__, __LINE__, buffer, size); }
/* we update our document version */
if (!error)
{ xmlVersionUpdate(0); }
if (!error)
{
validate = checkDtd(showerror_cb);
if (validate != 1)
{
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
snprintf(tmpbuffer, STDBUFFERLENGTH,
_("failed to validate xml document '%s'."),
filename);
showerror_cb(_("file error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
return 1;
}
}
}
else
{ /* the given file has size 0 */
error = 1;
}
return error;
}
/* #############################################################################
*
* Description encrypt and write our data to the given filename
* Author Harry Brueckner
* Date 2005-03-18
* Arguments char* filename - filename to write to
* char** errormsg - pointer to the GpgMe error message, if any
* PASSPHRASE_FN - passphrase callback function
* SHOWERROR_FN - callback function for error messages
* Return 1 on error, otherwise 0
*/
int xmlDataFileWrite(char* filename, char** errormsg,
PASSPHRASE_FN passphrase_cb, SHOWERROR_FN showerror_cb)
{
xmlNode* rootnode;
xmlChar* xmlbuffer = NULL;
int error = 0,
fd,
gpgsize = 0,
size;
char* buffer = NULL;
char* gpgbuffer = NULL;
char* tmpbuffer = NULL;
/* we initialize the error message */
*errormsg = NULL;
/* update the modification date of the root node */
rootnode = xmlDocGetRootElement(xmldoc);
if (rootnode)
{ xmlSetModification(rootnode); }
/* we create the memory buffer */
xmlDocDumpMemoryEnc(xmldoc, &xmlbuffer, &size, config -> encoding);
buffer = (char*)xmlbuffer;
if (buffer && !error && config -> encryptdata)
{ /* we have a buffer and must compress it */
error = zlibCompress(buffer, size, &gpgbuffer, &gpgsize, errormsg);
if (error)
{
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
snprintf(tmpbuffer, STDBUFFERLENGTH,
_("error (%s) compressing file '%s'."),
*errormsg,
filename);
showerror_cb(_("compression error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
memFree(__FILE__, __LINE__, buffer, size);
return 1;
}
xmlFree(buffer);
buffer = gpgbuffer;
size = gpgsize;
}
else if (buffer && !error && !config -> encryptdata)
{ /* we don't have to encrypt data, so nothing is compressed */
gpgbuffer = memAlloc(__FILE__, __LINE__, size);
/* Flawfinder: ignore */
memcpy(gpgbuffer, buffer, size);
xmlFree(buffer);
buffer = gpgbuffer;
}
if (buffer && !error &&
config -> encryptdata)
{
error = gpgEncrypt(buffer, size, &gpgbuffer, &gpgsize, passphrase_cb,
showerror_cb);
if (error)
{ *errormsg = _("could not encrypt database file."); }
memFree(__FILE__, __LINE__, buffer, size);
buffer = gpgbuffer;
size = gpgsize;
}
else
{
showerror_cb(_("warning"),
_("the database file is written in unecrypted mode."));
}
if (buffer && !error)
{ /* if we have a buffer we write the file */
createBackupfile(filename, showerror_cb);
fd = fileLockOpen(filename, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, &tmpbuffer);
if (fd == -1)
{ /* error opening the file */
showerror_cb(_("file error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
*errormsg = strerror(errno);
memFree(__FILE__, __LINE__, buffer, size);
return 1;
}
if (write(fd, buffer, size) != size)
{ /* error writing the file */
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
snprintf(tmpbuffer, STDBUFFERLENGTH,
_("error %d (%s) writing file '%s'."),
errno,
strerror(errno),
filename);
showerror_cb(_("file error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
*errormsg = strerror(errno);
memFree(__FILE__, __LINE__, buffer, size);
return 1;
}
lockf(fd, F_UNLCK, 0L);
close(fd);
}
if (buffer && size)
{ memFree(__FILE__, __LINE__, buffer, size); }
return error;
}
/* #############################################################################
*
* Description encode the XML entities in the given string
* Author Harry Brueckner
* Date 2005-07-08
* Arguments xmlChar* string - string to encode the entities
* Return xmlChar* encoded string
*/
xmlChar* xmlEncodeCommentEntities(xmlChar* string)
{
TRACE(99, "xmlEncodeCommentEntities()", NULL);
return xmlEncodeEntitiesReentrant(xmldoc, string);
}
/* #############################################################################
*
* Description get the documents root node
* Author Harry Brueckner
* Date 2005-04-18
* Arguments void
* Return xmlNode* node pointer to the root element
*/
xmlNode* xmlGetDocumentRoot(void)
{
TRACE(99, "xmlGetDocumentRoot()", NULL);
if (xmldoc)
{ return xmlDocGetRootElement(xmldoc); }
else
{ return NULL; }
}
/* #############################################################################
*
* Description remove all DTDs from the XML document
* Author Harry Brueckner
* Date 2005-05-06
* Arguments void
* Return void
*/
void xmlRemoveDtd(void)
{
xmlNode* curnode;
xmlNode* delnode = NULL;
TRACE(99, "xmlRemoveDtd()", NULL);
if (!xmldoc)
{ return; }
/* we delete all DTDs in the document */
curnode = xmldoc -> children;
while (curnode)
{
if (curnode -> type == XML_DTD_NODE)
{ delnode = curnode; }
curnode = curnode -> next;
if (delnode)
{
xmlUnlinkNode(delnode);
delnode = NULL;
}
}
}
/* #############################################################################
*
* Description error/warning callback for DTD validation
* Author Harry Brueckner
* Date 2005-05-06
* Arguments void* context - the validation context
* const char* msg - format message
* ... - arguments for the message
* Return void
*/
void xmlValidateError(void* context, const char* msg, ...)
{
va_list list;
int size;
char* tmpbuffer;
TRACE(99, "xmlValidateError()", NULL);
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
va_start(list, msg);
/* Flawfinder: ignore */
vsnprintf(tmpbuffer, STDBUFFERLENGTH, msg, list);
va_end(list);
/* the message has a \n at the end so we remove it */
size = strlen(tmpbuffer);
if (size > 0 && size < STDBUFFERLENGTH)
{ tmpbuffer[size - 1] = 0; }
validateShowError(_("validation error"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
}
/* #############################################################################
*
* Description error/warning callback for DTD validation
* Author Harry Brueckner
* Date 2005-05-06
* Arguments void* context - the validation context
* const char* msg - format message
* ... - arguments for the message
* Return void
*/
void xmlValidateWarning(void* context, const char* msg, ...)
{
va_list list;
int size;
char* tmpbuffer;
TRACE(99, "xmlValidateWarning()", NULL);
tmpbuffer = memAlloc(__FILE__, __LINE__, STDBUFFERLENGTH);
va_start(list, msg);
/* Flawfinder: ignore */
vsnprintf(tmpbuffer, STDBUFFERLENGTH, msg, list);
va_end(list);
/* the message has a \n at the end so we remove it */
size = strlen(tmpbuffer);
if (size > 0 && size < STDBUFFERLENGTH)
{ tmpbuffer[size - 1] = 0; }
validateShowError(_("validation warning"), tmpbuffer);
memFree(__FILE__, __LINE__, tmpbuffer, STDBUFFERLENGTH);
}
/* #############################################################################
*
* Description walk the tree and update all nodes
* Author Harry Brueckner
* Date 2005-04-04
* Arguments long oldversion - the version to convert from
* xmlNode* rootnode - the node to walk
* Return void
*/
void xmlVersionNodeUpdate(long oldversion, xmlNode* rootnode)
{
xmlNode* curnode;
TRACE(99, "xmlVersionNodeUpdate()", NULL);
if (!rootnode)
{ return; }
curnode = rootnode -> children;
while (curnode)
{
if (curnode -> type == XML_ELEMENT_NODE)
{
if (oldversion < 0x000000002)
{ /* we upgrade from 'unknown' version; before 0.2alpha,
* the version attribute did not exist
*/
xmlSetCreation(curnode);
}
}
xmlVersionNodeUpdate(oldversion, curnode);
curnode = curnode -> next;
}
}
/* #############################################################################
*
* Description check if our current document has the correct version, if not
* update it
* Author Harry Brueckner
* Date 2005-04-04
* Arguments xmlNode* node - the node to walk
* Return void
*/
void xmlVersionUpdate(int silent)
{
static char* curversion = PACKAGE_VERSION;
xmlNode* rootnode;
xmlChar* prop;
unsigned long version;
int size;
char* old;
char* ptr;
TRACE(99, "xmlVersionUpdate()", NULL);
rootnode = xmlDocGetRootElement(xmldoc);
if (!rootnode)
{ return; }
/* get the current version of the document */
prop = xmlGetProp(rootnode, BAD_CAST "version");
if (prop && !strcmp(curversion, (char*)prop))
{ return; }
if (!prop)
{ /* we do not even have a version info, so we add a new one */
xmlVersionNodeUpdate(0, rootnode);
xmlNewProp(rootnode, BAD_CAST "version", BAD_CAST curversion);
xmlSetCreation(rootnode);
}
else
{ /* we have version information but its not up to date */
size = strlen((char*)prop) + 1;
old = memAlloc(__FILE__, __LINE__, size);
strStrncpy(old, (char*)prop, size);
ptr = old;
while (*ptr && *ptr != '.')
{ ptr++; }
if (*ptr == '.')
{
*ptr = '\x0';
}
/* Flawfinder: ignore */
version = (atoi(old) << 16) + atoi(ptr + 1);
xmlSetProp(rootnode, BAD_CAST "version", BAD_CAST curversion);
xmlVersionNodeUpdate(version, rootnode);
memFree(__FILE__, __LINE__, old, size);
}
/* we update the modified timestamp */
xmlSetModification(rootnode);
runtime -> datachanged = 1;
if (!silent)
{ runtime -> updatestatus = 1; }
}
/* #############################################################################
*/