Home | History | Annotate | Line # | Download | only in tests
      1 /*
      2                             __  __            _
      3                          ___\ \/ /_ __   __ _| |_
      4                         / _ \\  /| '_ \ / _` | __|
      5                        |  __//  \| |_) | (_| | |_
      6                         \___/_/\_\ .__/ \__,_|\__|
      7                                  |_| XML parser
      8 
      9    Copyright (c) 2002-2004 Fred L. Drake, Jr. <fdrake (at) users.sourceforge.net>
     10    Copyright (c) 2003      Greg Stein <gstein (at) users.sourceforge.net>
     11    Copyright (c) 2016      Gilles Espinasse <g.esp (at) free.fr>
     12    Copyright (c) 2016-2023 Sebastian Pipping <sebastian (at) pipping.org>
     13    Copyright (c) 2017      Joe Orton <jorton (at) redhat.com>
     14    Copyright (c) 2017      Rhodri James <rhodri (at) wildebeest.org.uk>
     15    Copyright (c) 2022      Sean McBride <sean (at) 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 #include "../lib/xcsinc.c"
     51 
     52 void
     53 CharData_Init(CharData *storage) {
     54   assert(storage != NULL);
     55   storage->count = -1;
     56 }
     57 
     58 void
     59 CharData_AppendXMLChars(CharData *storage, const XML_Char *s, int len) {
     60   int maxchars;
     61 
     62   assert(storage != NULL);
     63   assert(s != NULL);
     64   maxchars = sizeof(storage->data) / sizeof(storage->data[0]);
     65   if (storage->count < 0)
     66     storage->count = 0;
     67   if (len < 0)
     68     len = (int)xcslen(s);
     69   if ((len + storage->count) > maxchars) {
     70     len = (maxchars - storage->count);
     71   }
     72   if (len + storage->count < (int)sizeof(storage->data)) {
     73     memcpy(storage->data + storage->count, s, len * sizeof(storage->data[0]));
     74     storage->count += len;
     75   }
     76 }
     77 
     78 int
     79 CharData_CheckXMLChars(CharData *storage, const XML_Char *expected) {
     80   int len = (int)xcslen(expected);
     81   int count;
     82 
     83   assert(storage != NULL);
     84   count = (storage->count < 0) ? 0 : storage->count;
     85   if (len != count) {
     86     char buffer[1024];
     87     snprintf(buffer, sizeof(buffer),
     88              "wrong number of data characters: got %d, expected %d", count,
     89              len);
     90     fail(buffer);
     91     return 0;
     92   }
     93   if (memcmp(expected, storage->data, len * sizeof(storage->data[0])) != 0) {
     94     fail("got bad data bytes");
     95     return 0;
     96   }
     97   return 1;
     98 }
     99