xref: /freebsd/contrib/expat/tests/chardata.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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-2021 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    Licensed under the MIT license:
16 
17    Permission is  hereby granted,  free of charge,  to any  person obtaining
18    a  copy  of  this  software   and  associated  documentation  files  (the
19    "Software"),  to  deal in  the  Software  without restriction,  including
20    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
21    distribute, sublicense, and/or sell copies of the Software, and to permit
22    persons  to whom  the Software  is  furnished to  do so,  subject to  the
23    following conditions:
24 
25    The above copyright  notice and this permission notice  shall be included
26    in all copies or substantial portions of the Software.
27 
28    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
29    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
30    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
31    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
32    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
33    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
34    USE OR OTHER DEALINGS IN THE SOFTWARE.
35 */
36 
37 #include <expat_config.h>
38 #include "minicheck.h"
39 
40 #include <assert.h>
41 #include <stdio.h>
42 #include <string.h>
43 
44 #include "chardata.h"
45 
46 static int
47 xmlstrlen(const XML_Char *s) {
48   int len = 0;
49   assert(s != NULL);
50   while (s[len] != 0)
51     ++len;
52   return len;
53 }
54 
55 void
56 CharData_Init(CharData *storage) {
57   assert(storage != NULL);
58   storage->count = -1;
59 }
60 
61 void
62 CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) {
63   int maxchars;
64 
65   assert(storage != NULL);
66   assert(s != NULL);
67   maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
68   if (storage->count < 0)
69     storage->count = 0;
70   if (len < 0)
71     len = xmlstrlen(s);
72   if ((len + storage->count) > maxchars) {
73     len = (maxchars - storage->count);
74   }
75   if (len + storage->count < (int)sizeof(storage->data)) {
76     memcpy(storage->data + storage->count, s, len * sizeof(storage->data[0]));
77     storage->count += len;
78   }
79 }
80 
81 int
82 CharData_CheckXMLChars(CharData *storage, const XML_Char *expected) {
83   char buffer[1024];
84   int len = xmlstrlen(expected);
85   int count;
86 
87   assert(storage != NULL);
88   count = (storage->count < 0) ? 0 : storage->count;
89   if (len != count) {
90     sprintf(buffer, "wrong number of data characters: got %d, expected %d",
91             count, len);
92     fail(buffer);
93     return 0;
94   }
95   if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
96     fail("got bad data bytes");
97     return 0;
98   }
99   return 1;
100 }
101