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