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