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