structdata.c 4.8 KB

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