1 /* 2 __ __ _ 3 ___\ \/ /_ __ __ _| |_ 4 / _ \\ /| '_ \ / _` | __| 5 | __// \| |_) | (_| | |_ 6 \___/_/\_\ .__/ \__,_|\__| 7 |_| XML parser 8 9 Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk> 10 Copyright (c) 2017-2026 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 #include "../lib/xcs.h" 51 52 #define STRUCT_EXTENSION_COUNT 8 53 54 #ifdef XML_UNICODE_WCHAR_T 55 # include <wchar.h> 56 # define XML_FMT_STR "ls" 57 #else 58 # define XML_FMT_STR "s" 59 #endif 60 61 static XML_Char * 62 xmlstrdup(const XML_Char *s) { 63 size_t byte_count = (xcslen(s) + 1) * sizeof(XML_Char); 64 XML_Char *const dup = malloc(byte_count); 65 66 assert(dup != NULL); 67 memcpy(dup, s, byte_count); 68 return dup; 69 } 70 71 void 72 StructData_Init(StructData *storage) { 73 assert(storage != NULL); 74 storage->count = 0; 75 storage->max_count = 0; 76 storage->entries = NULL; 77 } 78 79 void 80 StructData_AddItem(StructData *storage, const XML_Char *s, int data0, int data1, 81 int data2) { 82 StructDataEntry *entry; 83 84 assert(storage != NULL); 85 assert(s != NULL); 86 if (storage->count == storage->max_count) { 87 StructDataEntry *new_entries; 88 89 storage->max_count += STRUCT_EXTENSION_COUNT; 90 new_entries = realloc(storage->entries, 91 storage->max_count * sizeof(StructDataEntry)); 92 assert(new_entries != NULL); 93 storage->entries = new_entries; 94 } 95 96 entry = &storage->entries[storage->count]; 97 entry->str = xmlstrdup(s); 98 entry->data0 = data0; 99 entry->data1 = data1; 100 entry->data2 = data2; 101 storage->count++; 102 } 103 104 /* 'fail()' aborts the function via a longjmp, so there is no point 105 * in returning a value from this function. 106 */ 107 void 108 StructData_CheckItems(StructData *storage, const StructDataEntry *expected, 109 int count) { 110 char buffer[1024]; 111 112 assert(storage != NULL); 113 assert(expected != NULL); 114 if (count != storage->count) { 115 snprintf(buffer, sizeof(buffer), 116 "wrong number of entries: got %d, expected %d", storage->count, 117 count); 118 StructData_Dispose(storage); 119 fail(buffer); 120 } else { 121 for (int i = 0; i < count; i++) { 122 const StructDataEntry *got = &storage->entries[i]; 123 const StructDataEntry *want = &expected[i]; 124 125 assert(got != NULL); 126 assert(want != NULL); 127 128 if (xcscmp(got->str, want->str) != 0) { 129 StructData_Dispose(storage); 130 fail("structure got bad string"); 131 } else { 132 if (got->data0 != want->data0 || got->data1 != want->data1 133 || got->data2 != want->data2) { 134 snprintf(buffer, sizeof(buffer), 135 "struct '%" XML_FMT_STR 136 "' expected (%d,%d,%d), got (%d,%d,%d)", 137 got->str, want->data0, want->data1, want->data2, got->data0, 138 got->data1, got->data2); 139 StructData_Dispose(storage); 140 fail(buffer); 141 } 142 } 143 } 144 } 145 } 146 147 void 148 StructData_Dispose(StructData *storage) { 149 int i; 150 151 assert(storage != NULL); 152 for (i = 0; i < storage->count; i++) 153 free((void *)storage->entries[i].str); 154 free(storage->entries); 155 156 storage->count = 0; 157 storage->entries = NULL; 158 } 159