1355604Sdelphij/*
2355604Sdelphij                            __  __            _
3355604Sdelphij                         ___\ \/ /_ __   __ _| |_
4355604Sdelphij                        / _ \\  /| '_ \ / _` | __|
5355604Sdelphij                       |  __//  \| |_) | (_| | |_
6355604Sdelphij                        \___/_/\_\ .__/ \__,_|\__|
7355604Sdelphij                                 |_| XML parser
8104349Sphk
9355604Sdelphij   Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10355604Sdelphij   Copyright (c) 2000-2017 Expat development team
11355604Sdelphij   Licensed under the MIT license:
12355604Sdelphij
13355604Sdelphij   Permission is  hereby granted,  free of charge,  to any  person obtaining
14355604Sdelphij   a  copy  of  this  software   and  associated  documentation  files  (the
15355604Sdelphij   "Software"),  to  deal in  the  Software  without restriction,  including
16355604Sdelphij   without  limitation the  rights  to use,  copy,  modify, merge,  publish,
17355604Sdelphij   distribute, sublicense, and/or sell copies of the Software, and to permit
18355604Sdelphij   persons  to whom  the Software  is  furnished to  do so,  subject to  the
19355604Sdelphij   following conditions:
20355604Sdelphij
21355604Sdelphij   The above copyright  notice and this permission notice  shall be included
22355604Sdelphij   in all copies or substantial portions of the Software.
23355604Sdelphij
24355604Sdelphij   THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
25355604Sdelphij   EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
26355604Sdelphij   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27355604Sdelphij   NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28355604Sdelphij   DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
29355604Sdelphij   OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30355604Sdelphij   USE OR OTHER DEALINGS IN THE SOFTWARE.
31178848Scokane*/
32178848Scokane
33178848Scokane#ifdef HAVE_EXPAT_CONFIG_H
34355604Sdelphij#  include <expat_config.h>
35178848Scokane#endif
36178848Scokane#include "minicheck.h"
37178848Scokane
38104349Sphk#include <assert.h>
39104349Sphk#include <stdio.h>
40104349Sphk#include <string.h>
41104349Sphk
42104349Sphk#include "chardata.h"
43104349Sphk
44104349Sphkstatic int
45355604Sdelphijxmlstrlen(const XML_Char *s) {
46355604Sdelphij  int len = 0;
47355604Sdelphij  assert(s != NULL);
48355604Sdelphij  while (s[len] != 0)
49355604Sdelphij    ++len;
50355604Sdelphij  return len;
51104349Sphk}
52104349Sphk
53104349Sphkvoid
54355604SdelphijCharData_Init(CharData *storage) {
55355604Sdelphij  assert(storage != NULL);
56355604Sdelphij  storage->count = -1;
57104349Sphk}
58104349Sphk
59104349Sphkvoid
60355604SdelphijCharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) {
61355604Sdelphij  int maxchars;
62104349Sphk
63355604Sdelphij  assert(storage != NULL);
64355604Sdelphij  assert(s != NULL);
65355604Sdelphij  maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
66355604Sdelphij  if (storage->count < 0)
67355604Sdelphij    storage->count = 0;
68355604Sdelphij  if (len < 0)
69355604Sdelphij    len = xmlstrlen(s);
70355604Sdelphij  if ((len + storage->count) > maxchars) {
71355604Sdelphij    len = (maxchars - storage->count);
72355604Sdelphij  }
73355604Sdelphij  if (len + storage->count < (int)sizeof(storage->data)) {
74355604Sdelphij    memcpy(storage->data + storage->count, s, len * sizeof(storage->data[0]));
75355604Sdelphij    storage->count += len;
76355604Sdelphij  }
77104349Sphk}
78104349Sphk
79104349Sphkint
80355604SdelphijCharData_CheckXMLChars(CharData *storage, const XML_Char *expected) {
81355604Sdelphij  char buffer[1024];
82355604Sdelphij  int len = xmlstrlen(expected);
83355604Sdelphij  int count;
84104349Sphk
85355604Sdelphij  assert(storage != NULL);
86355604Sdelphij  count = (storage->count < 0) ? 0 : storage->count;
87355604Sdelphij  if (len != count) {
88355604Sdelphij    sprintf(buffer, "wrong number of data characters: got %d, expected %d",
89355604Sdelphij            count, len);
90355604Sdelphij    fail(buffer);
91355604Sdelphij    return 0;
92355604Sdelphij  }
93355604Sdelphij  if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
94355604Sdelphij    fail("got bad data bytes");
95355604Sdelphij    return 0;
96355604Sdelphij  }
97355604Sdelphij  return 1;
98104349Sphk}
99