chardata.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2002-2004 Fred L. Drake, Jr. <[email protected]>
  9. Copyright (c) 2003 Greg Stein <[email protected]>
  10. Copyright (c) 2016 Gilles Espinasse <[email protected]>
  11. Copyright (c) 2016-2023 Sebastian Pipping <[email protected]>
  12. Copyright (c) 2017 Joe Orton <[email protected]>
  13. Copyright (c) 2017 Rhodri James <[email protected]>
  14. Copyright (c) 2022 Sean McBride <[email protected]>
  15. Licensed under the MIT license:
  16. Permission is hereby granted, free of charge, to any person obtaining
  17. a copy of this software and associated documentation files (the
  18. "Software"), to deal in the Software without restriction, including
  19. without limitation the rights to use, copy, modify, merge, publish,
  20. distribute, sublicense, and/or sell copies of the Software, and to permit
  21. persons to whom the Software is furnished to do so, subject to the
  22. following conditions:
  23. The above copyright notice and this permission notice shall be included
  24. in all copies or substantial portions of the Software.
  25. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  28. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  29. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  30. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  31. USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. #if defined(NDEBUG)
  34. # undef NDEBUG /* because test suite relies on assert(...) at the moment */
  35. #endif
  36. #include "expat_config.h"
  37. #include "minicheck.h"
  38. #include <assert.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include "chardata.h"
  42. static int
  43. xmlstrlen(const XML_Char *s) {
  44. int len = 0;
  45. assert(s != NULL);
  46. while (s[len] != 0)
  47. ++len;
  48. return len;
  49. }
  50. void
  51. CharData_Init(CharData *storage) {
  52. assert(storage != NULL);
  53. storage->count = -1;
  54. }
  55. void
  56. CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) {
  57. int maxchars;
  58. assert(storage != NULL);
  59. assert(s != NULL);
  60. maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
  61. if (storage->count < 0)
  62. storage->count = 0;
  63. if (len < 0)
  64. len = xmlstrlen(s);
  65. if ((len + storage->count) > maxchars) {
  66. len = (maxchars - storage->count);
  67. }
  68. if (len + storage->count < (int)sizeof(storage->data)) {
  69. memcpy(storage->data + storage->count, s, len * sizeof(storage->data[0]));
  70. storage->count += len;
  71. }
  72. }
  73. int
  74. CharData_CheckXMLChars(CharData *storage, const XML_Char *expected) {
  75. int len = xmlstrlen(expected);
  76. int count;
  77. assert(storage != NULL);
  78. count = (storage->count < 0) ? 0 : storage->count;
  79. if (len != count) {
  80. char buffer[1024];
  81. snprintf(buffer, sizeof(buffer),
  82. "wrong number of data characters: got %d, expected %d", count,
  83. len);
  84. fail(buffer);
  85. return 0;
  86. }
  87. if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
  88. fail("got bad data bytes");
  89. return 0;
  90. }
  91. return 1;
  92. }