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
37
38 #include <assert.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #include "structdata.h"
44 #include "minicheck.h"
45
46 #define STRUCT_EXTENSION_COUNT 8
47
48 #ifdef XML_UNICODE_WCHAR_T
49 # include <wchar.h>
50 # define XML_FMT_STR "ls"
51 # define xcstrlen(s) wcslen(s)
52 # define xcstrcmp(s, t) wcscmp((s), (t))
53 #else
54 # define XML_FMT_STR "s"
55 # define xcstrlen(s) strlen(s)
56 # define xcstrcmp(s, t) strcmp((s), (t))
57 #endif
58
59
60 static XML_Char *
xmlstrdup(const XML_Char * s)61 xmlstrdup(const XML_Char *s)
62 {
63 size_t byte_count = (xcstrlen(s) + 1) * sizeof(XML_Char);
64 XML_Char *dup = malloc(byte_count);
65
66 assert(dup != NULL);
67 memcpy(dup, s, byte_count);
68 return dup;
69 }
70
71
72 void
StructData_Init(StructData * storage)73 StructData_Init(StructData *storage)
74 {
75 assert(storage != NULL);
76 storage->count = 0;
77 storage->max_count = 0;
78 storage->entries = NULL;
79 }
80
81 void
StructData_AddItem(StructData * storage,const XML_Char * s,int data0,int data1,int data2)82 StructData_AddItem(StructData *storage,
83 const XML_Char *s,
84 int data0,
85 int data1,
86 int data2)
87 {
88 StructDataEntry *entry;
89
90 assert(storage != NULL);
91 assert(s != NULL);
92 if (storage->count == storage->max_count) {
93 StructDataEntry *new;
94
95 storage->max_count += STRUCT_EXTENSION_COUNT;
96 new = realloc(storage->entries,
97 storage->max_count * sizeof(StructDataEntry));
98 assert(new != NULL);
99 storage->entries = new;
100 }
101
102 entry = &storage->entries[storage->count];
103 entry->str = xmlstrdup(s);
104 entry->data0 = data0;
105 entry->data1 = data1;
106 entry->data2 = data2;
107 storage->count++;
108 }
109
110 /* 'fail()' aborts the function via a longjmp, so there is no point
111 * in returning a value from this function.
112 */
113 void
StructData_CheckItems(StructData * storage,const StructDataEntry * expected,int count)114 StructData_CheckItems(StructData *storage,
115 const StructDataEntry *expected,
116 int count)
117 {
118 char buffer[1024];
119 int i;
120
121 assert(storage != NULL);
122 assert(expected != NULL);
123 if (count != storage->count) {
124 sprintf(buffer, "wrong number of entries: got %d, expected %d",
125 storage->count, count);
126 StructData_Dispose(storage);
127 fail(buffer);
128 }
129 for (i = 0; i < count; i++)
130 {
131 const StructDataEntry *got = &storage->entries[i];
132 const StructDataEntry *want = &expected[i];
133
134 if (xcstrcmp(got->str, want->str) != 0) {
135 StructData_Dispose(storage);
136 fail("structure got bad string");
137 }
138 if (got->data0 != want->data0 ||
139 got->data1 != want->data1 ||
140 got->data2 != want->data2) {
141 sprintf(buffer,
142 "struct '%" XML_FMT_STR
143 "' expected (%d,%d,%d), got (%d,%d,%d)",
144 got->str,
145 want->data0, want->data1, want->data2,
146 got->data0, got->data1, got->data2);
147 StructData_Dispose(storage);
148 fail(buffer);
149 }
150 }
151 }
152
153 void
StructData_Dispose(StructData * storage)154 StructData_Dispose(StructData *storage)
155 {
156 int i;
157
158 assert(storage != NULL);
159 for (i = 0; i < storage->count; i++)
160 free((void *)storage->entries[i].str);
161 free(storage->entries);
162 }
163