1 /*
2 __ __ _
3 ___\ \/ /_ __ __ _| |_
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
7 |_| XML parser
8
9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10 Copyright (c) 2000-2017 Expat development team
11 Licensed under the MIT license:
12
13 Permission is hereby granted, free of charge, to any person obtaining
14 a copy of this software and associated documentation files (the
15 "Software"), to deal in the Software without restriction, including
16 without limitation the rights to use, copy, modify, merge, publish,
17 distribute, sublicense, and/or sell copies of the Software, and to permit
18 persons to whom the Software is furnished to do so, subject to the
19 following conditions:
20
21 The above copyright notice and this permission notice shall be included
22 in all copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
29 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30 USE OR OTHER DEALINGS IN THE SOFTWARE.
31 */
32
33 #ifdef HAVE_EXPAT_CONFIG_H
34 # include <expat_config.h>
35 #endif
36 #include "minicheck.h"
37
38 #include <assert.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 #include "chardata.h"
43
44 static int
xmlstrlen(const XML_Char * s)45 xmlstrlen(const XML_Char *s) {
46 int len = 0;
47 assert(s != NULL);
48 while (s[len] != 0)
49 ++len;
50 return len;
51 }
52
53 void
CharData_Init(CharData * storage)54 CharData_Init(CharData *storage) {
55 assert(storage != NULL);
56 storage->count = -1;
57 }
58
59 void
CharData_AppendXMLChars(CharData * storage,const XML_Char * s,int len)60 CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) {
61 int maxchars;
62
63 assert(storage != NULL);
64 assert(s != NULL);
65 maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
66 if (storage->count < 0)
67 storage->count = 0;
68 if (len < 0)
69 len = xmlstrlen(s);
70 if ((len + storage->count) > maxchars) {
71 len = (maxchars - storage->count);
72 }
73 if (len + storage->count < (int)sizeof(storage->data)) {
74 memcpy(storage->data + storage->count, s, len * sizeof(storage->data[0]));
75 storage->count += len;
76 }
77 }
78
79 int
CharData_CheckXMLChars(CharData * storage,const XML_Char * expected)80 CharData_CheckXMLChars(CharData *storage, const XML_Char *expected) {
81 char buffer[1024];
82 int len = xmlstrlen(expected);
83 int count;
84
85 assert(storage != NULL);
86 count = (storage->count < 0) ? 0 : storage->count;
87 if (len != count) {
88 sprintf(buffer, "wrong number of data characters: got %d, expected %d",
89 count, len);
90 fail(buffer);
91 return 0;
92 }
93 if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
94 fail("got bad data bytes");
95 return 0;
96 }
97 return 1;
98 }
99