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