chardata.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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-2021 Sebastian Pipping <[email protected]>
  12. Copyright (c) 2017 Joe Orton <[email protected]>
  13. Copyright (c) 2017 Rhodri James <[email protected]>
  14. Licensed under the MIT license:
  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. The above copyright notice and this permission notice shall be included
  23. in all copies or substantial portions of the Software.
  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. #include <expat_config.h>
  33. #include "minicheck.h"
  34. #include <assert.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include "chardata.h"
  38. static int
  39. xmlstrlen(const XML_Char *s) {
  40. int len = 0;
  41. assert(s != NULL);
  42. while (s[len] != 0)
  43. ++len;
  44. return len;
  45. }
  46. void
  47. CharData_Init(CharData *storage) {
  48. assert(storage != NULL);
  49. storage->count = -1;
  50. }
  51. void
  52. CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) {
  53. int maxchars;
  54. assert(storage != NULL);
  55. assert(s != NULL);
  56. maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
  57. if (storage->count < 0)
  58. storage->count = 0;
  59. if (len < 0)
  60. len = xmlstrlen(s);
  61. if ((len + storage->count) > maxchars) {
  62. len = (maxchars - storage->count);
  63. }
  64. if (len + storage->count < (int)sizeof(storage->data)) {
  65. memcpy(storage->data + storage->count, s, len * sizeof(storage->data[0]));
  66. storage->count += len;
  67. }
  68. }
  69. int
  70. CharData_CheckXMLChars(CharData *storage, const XML_Char *expected) {
  71. char buffer[1024];
  72. int len = xmlstrlen(expected);
  73. int count;
  74. assert(storage != NULL);
  75. count = (storage->count < 0) ? 0 : storage->count;
  76. if (len != count) {
  77. sprintf(buffer, "wrong number of data characters: got %d, expected %d",
  78. count, len);
  79. fail(buffer);
  80. return 0;
  81. }
  82. if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
  83. fail("got bad data bytes");
  84. return 0;
  85. }
  86. return 1;
  87. }