xref: /openbsd-src/lib/libexpat/tests/chardata.c (revision bd8f1dc3b0e01803a74947836eef57849c13acb0)
12e724bc9Sbluhm /*
22e724bc9Sbluhm                             __  __            _
32e724bc9Sbluhm                          ___\ \/ /_ __   __ _| |_
42e724bc9Sbluhm                         / _ \\  /| '_ \ / _` | __|
52e724bc9Sbluhm                        |  __//  \| |_) | (_| | |_
62e724bc9Sbluhm                         \___/_/\_\ .__/ \__,_|\__|
72e724bc9Sbluhm                                  |_| XML parser
833ab7b2bSbluhm 
908819b41Sbluhm    Copyright (c) 2002-2004 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
1008819b41Sbluhm    Copyright (c) 2003      Greg Stein <gstein@users.sourceforge.net>
1108819b41Sbluhm    Copyright (c) 2016      Gilles Espinasse <g.esp@free.fr>
12*bd8f1dc3Sbluhm    Copyright (c) 2016-2023 Sebastian Pipping <sebastian@pipping.org>
1308819b41Sbluhm    Copyright (c) 2017      Joe Orton <jorton@redhat.com>
1408819b41Sbluhm    Copyright (c) 2017      Rhodri James <rhodri@wildebeest.org.uk>
15*bd8f1dc3Sbluhm    Copyright (c) 2022      Sean McBride <sean@rogue-research.com>
162e724bc9Sbluhm    Licensed under the MIT license:
172e724bc9Sbluhm 
182e724bc9Sbluhm    Permission is  hereby granted,  free of charge,  to any  person obtaining
192e724bc9Sbluhm    a  copy  of  this  software   and  associated  documentation  files  (the
202e724bc9Sbluhm    "Software"),  to  deal in  the  Software  without restriction,  including
212e724bc9Sbluhm    without  limitation the  rights  to use,  copy,  modify, merge,  publish,
222e724bc9Sbluhm    distribute, sublicense, and/or sell copies of the Software, and to permit
232e724bc9Sbluhm    persons  to whom  the Software  is  furnished to  do so,  subject to  the
242e724bc9Sbluhm    following conditions:
252e724bc9Sbluhm 
262e724bc9Sbluhm    The above copyright  notice and this permission notice  shall be included
272e724bc9Sbluhm    in all copies or substantial portions of the Software.
282e724bc9Sbluhm 
292e724bc9Sbluhm    THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
302e724bc9Sbluhm    EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
312e724bc9Sbluhm    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
322e724bc9Sbluhm    NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
332e724bc9Sbluhm    DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
342e724bc9Sbluhm    OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
352e724bc9Sbluhm    USE OR OTHER DEALINGS IN THE SOFTWARE.
3633ab7b2bSbluhm */
3733ab7b2bSbluhm 
38*bd8f1dc3Sbluhm #if defined(NDEBUG)
39*bd8f1dc3Sbluhm #  undef NDEBUG /* because test suite relies on assert(...) at the moment */
40*bd8f1dc3Sbluhm #endif
41*bd8f1dc3Sbluhm 
42*bd8f1dc3Sbluhm #include "expat_config.h"
4333ab7b2bSbluhm #include "minicheck.h"
4433ab7b2bSbluhm 
4533ab7b2bSbluhm #include <assert.h>
4633ab7b2bSbluhm #include <stdio.h>
4733ab7b2bSbluhm #include <string.h>
4833ab7b2bSbluhm 
4933ab7b2bSbluhm #include "chardata.h"
5033ab7b2bSbluhm 
5133ab7b2bSbluhm static int
xmlstrlen(const XML_Char * s)5228ce3119Sbluhm xmlstrlen(const XML_Char *s) {
5333ab7b2bSbluhm   int len = 0;
5433ab7b2bSbluhm   assert(s != NULL);
5533ab7b2bSbluhm   while (s[len] != 0)
5633ab7b2bSbluhm     ++len;
5733ab7b2bSbluhm   return len;
5833ab7b2bSbluhm }
5933ab7b2bSbluhm 
6033ab7b2bSbluhm void
CharData_Init(CharData * storage)6128ce3119Sbluhm CharData_Init(CharData *storage) {
6233ab7b2bSbluhm   assert(storage != NULL);
6333ab7b2bSbluhm   storage->count = -1;
6433ab7b2bSbluhm }
6533ab7b2bSbluhm 
6633ab7b2bSbluhm void
CharData_AppendXMLChars(CharData * storage,const XML_Char * s,int len)6728ce3119Sbluhm CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) {
6833ab7b2bSbluhm   int maxchars;
6933ab7b2bSbluhm 
7033ab7b2bSbluhm   assert(storage != NULL);
7133ab7b2bSbluhm   assert(s != NULL);
7233ab7b2bSbluhm   maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
7333ab7b2bSbluhm   if (storage->count < 0)
7433ab7b2bSbluhm     storage->count = 0;
7533ab7b2bSbluhm   if (len < 0)
7633ab7b2bSbluhm     len = xmlstrlen(s);
7733ab7b2bSbluhm   if ((len + storage->count) > maxchars) {
7833ab7b2bSbluhm     len = (maxchars - storage->count);
7933ab7b2bSbluhm   }
8033ab7b2bSbluhm   if (len + storage->count < (int)sizeof(storage->data)) {
8128ce3119Sbluhm     memcpy(storage->data + storage->count, s, len * sizeof(storage->data[0]));
8233ab7b2bSbluhm     storage->count += len;
8333ab7b2bSbluhm   }
8433ab7b2bSbluhm }
8533ab7b2bSbluhm 
8633ab7b2bSbluhm int
CharData_CheckXMLChars(CharData * storage,const XML_Char * expected)8728ce3119Sbluhm CharData_CheckXMLChars(CharData *storage, const XML_Char *expected) {
8833ab7b2bSbluhm   int len = xmlstrlen(expected);
8933ab7b2bSbluhm   int count;
9033ab7b2bSbluhm 
9133ab7b2bSbluhm   assert(storage != NULL);
9233ab7b2bSbluhm   count = (storage->count < 0) ? 0 : storage->count;
9333ab7b2bSbluhm   if (len != count) {
94*bd8f1dc3Sbluhm     char buffer[1024];
95*bd8f1dc3Sbluhm     snprintf(buffer, sizeof(buffer),
96*bd8f1dc3Sbluhm              "wrong number of data characters: got %d, expected %d", count,
97*bd8f1dc3Sbluhm              len);
9833ab7b2bSbluhm     fail(buffer);
9933ab7b2bSbluhm     return 0;
10033ab7b2bSbluhm   }
10133ab7b2bSbluhm   if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
10233ab7b2bSbluhm     fail("got bad data bytes");
10333ab7b2bSbluhm     return 0;
10433ab7b2bSbluhm   }
10533ab7b2bSbluhm   return 1;
10633ab7b2bSbluhm }
107