1 /*
2 __ __ _
3 ___\ \/ /_ __ __ _| |_
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
7 |_| XML parser
8
9 Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
10 Copyright (c) 2017-2023 Sebastian Pipping <sebastian@pipping.org>
11 Copyright (c) 2022 Sean McBride <sean@rogue-research.com>
12 Copyright (c) 2026 Matthew Fernandez <matthew.fernandez@gmail.com>
13 Licensed under the MIT license:
14
15 Permission is hereby granted, free of charge, to any person obtaining
16 a copy of this software and associated documentation files (the
17 "Software"), to deal in the Software without restriction, including
18 without limitation the rights to use, copy, modify, merge, publish,
19 distribute, sublicense, and/or sell copies of the Software, and to permit
20 persons to whom the Software is furnished to do so, subject to the
21 following conditions:
22
23 The above copyright notice and this permission notice shall be included
24 in all copies or substantial portions of the Software.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
29 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
30 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
31 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
32 USE OR OTHER DEALINGS IN THE SOFTWARE.
33 */
34
35 #if defined(NDEBUG)
36 # undef NDEBUG /* because test suite relies on assert(...) at the moment */
37 #endif
38
39 #include "expat_config.h"
40
41 #include <assert.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45
46 #include "structdata.h"
47 #include "minicheck.h"
48
49 #define STRUCT_EXTENSION_COUNT 8
50
51 #ifdef XML_UNICODE_WCHAR_T
52 # include <wchar.h>
53 # define XML_FMT_STR "ls"
54 # define xcstrlen(s) wcslen(s)
55 # define xcstrcmp(s, t) wcscmp((s), (t))
56 #else
57 # define XML_FMT_STR "s"
58 # define xcstrlen(s) strlen(s)
59 # define xcstrcmp(s, t) strcmp((s), (t))
60 #endif
61
62 static XML_Char *
xmlstrdup(const XML_Char * s)63 xmlstrdup(const XML_Char *s) {
64 size_t byte_count = (xcstrlen(s) + 1) * sizeof(XML_Char);
65 XML_Char *const dup = malloc(byte_count);
66
67 assert(dup != NULL);
68 memcpy(dup, s, byte_count);
69 return dup;
70 }
71
72 void
StructData_Init(StructData * storage)73 StructData_Init(StructData *storage) {
74 assert(storage != NULL);
75 storage->count = 0;
76 storage->max_count = 0;
77 storage->entries = NULL;
78 }
79
80 void
StructData_AddItem(StructData * storage,const XML_Char * s,int data0,int data1,int data2)81 StructData_AddItem(StructData *storage, const XML_Char *s, int data0, int data1,
82 int data2) {
83 StructDataEntry *entry;
84
85 assert(storage != NULL);
86 assert(s != NULL);
87 if (storage->count == storage->max_count) {
88 StructDataEntry *new_entries;
89
90 storage->max_count += STRUCT_EXTENSION_COUNT;
91 new_entries = realloc(storage->entries,
92 storage->max_count * sizeof(StructDataEntry));
93 assert(new_entries != NULL);
94 storage->entries = new_entries;
95 }
96
97 entry = &storage->entries[storage->count];
98 entry->str = xmlstrdup(s);
99 entry->data0 = data0;
100 entry->data1 = data1;
101 entry->data2 = data2;
102 storage->count++;
103 }
104
105 /* 'fail()' aborts the function via a longjmp, so there is no point
106 * in returning a value from this function.
107 */
108 void
StructData_CheckItems(StructData * storage,const StructDataEntry * expected,int count)109 StructData_CheckItems(StructData *storage, const StructDataEntry *expected,
110 int count) {
111 char buffer[1024];
112
113 assert(storage != NULL);
114 assert(expected != NULL);
115 if (count != storage->count) {
116 snprintf(buffer, sizeof(buffer),
117 "wrong number of entries: got %d, expected %d", storage->count,
118 count);
119 StructData_Dispose(storage);
120 fail(buffer);
121 } else {
122 for (int i = 0; i < count; i++) {
123 const StructDataEntry *got = &storage->entries[i];
124 const StructDataEntry *want = &expected[i];
125
126 assert(got != NULL);
127 assert(want != NULL);
128
129 if (xcstrcmp(got->str, want->str) != 0) {
130 StructData_Dispose(storage);
131 fail("structure got bad string");
132 } else {
133 if (got->data0 != want->data0 || got->data1 != want->data1
134 || got->data2 != want->data2) {
135 snprintf(buffer, sizeof(buffer),
136 "struct '%" XML_FMT_STR
137 "' expected (%d,%d,%d), got (%d,%d,%d)",
138 got->str, want->data0, want->data1, want->data2, got->data0,
139 got->data1, got->data2);
140 StructData_Dispose(storage);
141 fail(buffer);
142 }
143 }
144 }
145 }
146 }
147
148 void
StructData_Dispose(StructData * storage)149 StructData_Dispose(StructData *storage) {
150 int i;
151
152 assert(storage != NULL);
153 for (i = 0; i < storage->count; i++)
154 free((void *)storage->entries[i].str);
155 free(storage->entries);
156
157 storage->count = 0;
158 storage->entries = NULL;
159 }
160