third_party/modest/source/myhtml/serialization.c

filter: Statementstmt 0.0% 0 / 279 Branchbran 0.0% 0 / 234 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 513 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« third_party/modest/source/myhtml/rules.c ↑ Summary third_party/modest/source/myhtml/stream.c »
1 /*
2 Copyright (C) 2016-2017 Alexander Borisov
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
18 Author: lex.borisov@gmail.com (Alexander Borisov)
19 Author: https://github.com/EmielBruijntjes (Emiel Bruijntjes)
20 */
21  
22 #include "myhtml/serialization.h"
23  
24 /**
25 * Forward declaration of all the functions that are used inside this module
26 */
27 static mystatus_t myhtml_serialization_append(const char* str, size_t size, mycore_callback_serialize_f callback, void *ptr);
28 static mystatus_t myhtml_serialization_append_attr(const char* str, size_t length, mycore_callback_serialize_f callback, void *ptr);
29 static mystatus_t myhtml_serialization_attributes(myhtml_tree_t* tree, myhtml_tree_attr_t* attr, mycore_callback_serialize_f callback, void *ptr);
30 static mystatus_t myhtml_serialization_node_append_text_node(myhtml_tree_node_t* node, mycore_callback_serialize_f callback, void *ptr);
31 static mystatus_t myhtml_serialization_node_append_close(myhtml_tree_node_t* node, mycore_callback_serialize_f callback, void *ptr);
32  
33 /**
34 * See the function myhtml_serialization_tree_buffer
35 */
36 0 mystatus_t myhtml_serialization(myhtml_tree_node_t* scope_node, mycore_string_raw_t* str)
37 {
38 0 return myhtml_serialization_tree_buffer(scope_node, str);
39 }
40  
41 /**
42 * See the function myhtml_serialization_node_buffer
43 */
44 0 mystatus_t myhtml_serialization_node(myhtml_tree_node_t* node, mycore_string_raw_t* str)
45 {
46 0 return myhtml_serialization_node_buffer(node, str);
47 }
48  
49 /**
50 * The serialize function for an entire tree
51 * @param tree the tree to be serialized
52 * @param scope_node the scope_node
53 * @param callback function that will be called for all strings that have to be printed
54 * @param ptr user-supplied pointer
55 * @return bool
56 */
57 0 mystatus_t myhtml_serialization_tree_callback(myhtml_tree_node_t* scope_node, mycore_callback_serialize_f callback, void *ptr)
58 {
59 0 myhtml_tree_node_t* node = scope_node;
60
61 /*
62 Document node it is a root node of tree, it is not a tag element.
63
64 For HTML, the root element will always be <HTML> tag.
65 It is always present in the HTML document and it always a root of HTML document.
66 */
67
68 0 ▶ if(scope_node && scope_node->tree && scope_node->tree->document == scope_node) {
Branch: 3 branches
BranchTrueFalseTotal
if(scope_node && scope_node->tree && scope_node->tree->document == scope_node) { 0 0 0
if(scope_node && scope_node->tree && scope_node->tree->document == scope_node) { 0 0 0
if(scope_node && scope_node->tree && scope_node->tree->document == scope_node) { 0 0 0
69 0 node = scope_node->child;
70 }
71
72 0 ▶ while(node) {
Branch: 1 branch
BranchTrueFalseTotal
while(node) { 0 0 0
73 0 ▶ if(myhtml_serialization_node_callback(node, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_node_callback(node, callback, ptr)) 0 0 0
74 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
75
76 0 ▶ if(node->child)
Branch: 1 branch
BranchTrueFalseTotal
if(node->child) 0 0 0
77 0 node = node->child;
78 else {
79 0 ▶ while(node != scope_node && node->next == NULL) {
Branch: 2 branches
BranchTrueFalseTotal
while(node != scope_node && node->next == NULL) { 0 0 0
while(node != scope_node && node->next == NULL) { 0 0 0
80 0 ▶ if(myhtml_serialization_node_append_close(node, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_node_append_close(node, callback, ptr)) 0 0 0
81 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
82
83 0 node = node->parent;
84 }
85
86 0 ▶ if(node == scope_node) {
Branch: 1 branch
BranchTrueFalseTotal
if(node == scope_node) { 0 0 0
87 0 ▶ if(node != node->tree->document) {
Branch: 1 branch
BranchTrueFalseTotal
if(node != node->tree->document) { 0 0 0
88 0 ▶ if(myhtml_serialization_node_append_close(node, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_node_append_close(node, callback, ptr)) 0 0 0
89 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
90 }
91
92 0 break;
93 }
94
95 0 ▶ if(myhtml_serialization_node_append_close(node, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_node_append_close(node, callback, ptr)) 0 0 0
96 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
97
98 0 node = node->next;
99 }
100 }
101
102 0 return MyCORE_STATUS_OK;
103 }
104  
105 /**
106 * The serialize function for a single node
107 * @param tree the tree to be serialized
108 * @param node the node that is going to be serialized
109 * @param callback function that will be called for all strings that have to be printed
110 * @param ptr user-supplied pointer
111 * @return bool
112 */
113 0 mystatus_t myhtml_serialization_node_callback(myhtml_tree_node_t* node, mycore_callback_serialize_f callback, void *ptr)
114 {
115 0 switch (node->tag_id) {
116 0 case MyHTML_TAG__TEXT: {
117 0 ▶ if(myhtml_serialization_node_append_text_node(node, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_node_append_text_node(node, callback, ptr)) 0 0 0
118 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
119
120 0 break;
121 }
122 0 case MyHTML_TAG__COMMENT: {
123 0 ▶ if(callback("<!--", 4, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("<!--", 4, ptr)) 0 0 0
124 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
125
126 0 ▶ if(node->token && node->token->str.data) {
Branch: 2 branches
BranchTrueFalseTotal
if(node->token && node->token->str.data) { 0 0 0
if(node->token && node->token->str.data) { 0 0 0
127 0 ▶ if(callback(node->token->str.data, node->token->str.length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(node->token->str.data, node->token->str.length, ptr)) 0 0 0
128 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
129 }
130
131 0 ▶ if(callback("-->", 3, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("-->", 3, ptr)) 0 0 0
132 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
133
134 0 break;
135 }
136 0 case MyHTML_TAG__DOCTYPE: {
137 0 ▶ if(callback("<!DOCTYPE", 9, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("<!DOCTYPE", 9, ptr)) 0 0 0
138 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
139
140 0 ▶ if(callback(" ", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(" ", 1, ptr)) 0 0 0
141 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
142
143 0 ▶ if(node->token) {
Branch: 1 branch
BranchTrueFalseTotal
if(node->token) { 0 0 0
144 0 myhtml_tree_attr_t* attr = node->token->attr_first;
145
146 0 ▶ if(attr && attr->key.data && attr->key.length) {
Branch: 3 branches
BranchTrueFalseTotal
if(attr && attr->key.data && attr->key.length) { 0 0 0
if(attr && attr->key.data && attr->key.length) { 0 0 0
if(attr && attr->key.data && attr->key.length) { 0 0 0
147 0 ▶ if(callback(attr->key.data, attr->key.length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(attr->key.data, attr->key.length, ptr)) 0 0 0
148 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
149
150 0 attr = attr->next;
151
152 0 ▶ if(attr) {
Branch: 1 branch
BranchTrueFalseTotal
if(attr) { 0 0 0
153 0 myhtml_tree_attr_t *system_id = NULL, *public_id = NULL;
154
155 0 ▶ if(attr->value.length == 6) {
Branch: 1 branch
BranchTrueFalseTotal
if(attr->value.length == 6) { 0 0 0
156 0 ▶ if(mycore_strcasecmp(attr->value.data, "SYSTEM") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if(mycore_strcasecmp(attr->value.data, "SYSTEM") == 0) { 0 0 0
157 0 system_id = attr->next;
158 0 ▶ } else if(mycore_strcasecmp(attr->value.data, "PUBLIC") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if(mycore_strcasecmp(attr->value.data, "PUBLIC") == 0) { 0 0 0
159 0 public_id = attr->next;
160 0 ▶ system_id = public_id ? public_id->next : NULL;
Branch: 1 branch
BranchTrueFalseTotal
system_id = public_id ? public_id->next : NULL; 0 0 0
161 }
162 }
163
164 0 ▶ if(public_id && public_id->value.length > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(public_id && public_id->value.length > 0) { 0 0 0
if(public_id && public_id->value.length > 0) { 0 0 0
165 0 ▶ if(callback(" PUBLIC \"", 9, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(" PUBLIC \"", 9, ptr)) 0 0 0
166 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
167
168 0 ▶ if(callback(public_id->value.data, public_id->value.length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(public_id->value.data, public_id->value.length, ptr)) 0 0 0
169 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
170
171 0 ▶ if(callback("\"", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("\"", 1, ptr)) 0 0 0
172 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
173 }
174
175 0 ▶ if(system_id && system_id->value.length > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(system_id && system_id->value.length > 0) { 0 0 0
if(system_id && system_id->value.length > 0) { 0 0 0
176 0 ▶ if(!public_id || public_id->value.length == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if(!public_id || public_id->value.length == 0) { 0 0 0
if(!public_id || public_id->value.length == 0) { 0 0 0
177 0 ▶ if(callback(" SYSTEM", 7, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(" SYSTEM", 7, ptr)) 0 0 0
178 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
179 }
180
181 0 ▶ if(callback(" \"", 2, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(" \"", 2, ptr)) 0 0 0
182 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
183
184 0 ▶ if(system_id->value.data && system_id->value.length) {
Branch: 2 branches
BranchTrueFalseTotal
if(system_id->value.data && system_id->value.length) { 0 0 0
if(system_id->value.data && system_id->value.length) { 0 0 0
185 0 ▶ if(callback(system_id->value.data, system_id->value.length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(system_id->value.data, system_id->value.length, ptr)) 0 0 0
186 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
187 }
188
189 0 ▶ if(callback("\"", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("\"", 1, ptr)) 0 0 0
190 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
191 }
192 }
193 }
194 }
195
196 0 ▶ if(callback(">", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(">", 1, ptr)) 0 0 0
197 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
198
199 0 break;
200 }
201 0 default: {
202 size_t length;
203 0 const char *tag = myhtml_tag_name_by_id(node->tree, node->tag_id, &length);
204  
205 0 ▶ if(callback("<", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("<", 1, ptr)) 0 0 0
206 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
207
208 0 ▶ if(callback(tag, length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(tag, length, ptr)) 0 0 0
209 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
210
211 0 ▶ if(node->token) {
Branch: 1 branch
BranchTrueFalseTotal
if(node->token) { 0 0 0
212 0 ▶ if(myhtml_serialization_attributes(node->tree, node->token->attr_first, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_attributes(node->tree, node->token->attr_first, callback, ptr)) 0 0 0
213 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
214 }
215
216 0 ▶ if(callback(">", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(">", 1, ptr)) 0 0 0
217 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
218
219 0 break;
220 }
221 }
222
223 0 return MyCORE_STATUS_OK;
224 }
225  
226 /**
227 * Internal function to process attributes
228 * @param tree
229 * @param attr
230 * @param callback
231 * @param ptr
232 */
233 0 mystatus_t myhtml_serialization_attributes(myhtml_tree_t* tree, myhtml_tree_attr_t* attr, mycore_callback_serialize_f callback, void* ptr)
234 {
235 0 ▶ while(attr) {
Branch: 1 branch
BranchTrueFalseTotal
while(attr) { 0 0 0
236 0 ▶ if(callback(" ", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(" ", 1, ptr)) 0 0 0
237 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
238
239 0 switch (attr->ns) {
240 0 case MyHTML_NAMESPACE_XML:
241 0 ▶ if(callback("xml:", 4, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("xml:", 4, ptr)) 0 0 0
242 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
243
244 0 break;
245 0 case MyHTML_NAMESPACE_XMLNS: {
246 /*
247 If the attribute is in the XMLNS namespace and the attribute's local name is not xmlns
248 The attribute's serialized name is the string "xmlns:" followed by the attribute's local name.
249 */
250 0 ▶ if(attr->key.data && attr->key.length == 5 && mycore_strcmp(attr->key.data, "xmlns")) {
Branch: 3 branches
BranchTrueFalseTotal
if(attr->key.data && attr->key.length == 5 && mycore_strcmp(attr->key.data, "xmlns")) { 0 0 0
if(attr->key.data && attr->key.length == 5 && mycore_strcmp(attr->key.data, "xmlns")) { 0 0 0
if(attr->key.data && attr->key.length == 5 && mycore_strcmp(attr->key.data, "xmlns")) { 0 0 0
251 0 ▶ if(callback("xmlns:", 6, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("xmlns:", 6, ptr)) 0 0 0
252 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
253 }
254
255 0 break;
256 }
257 0 case MyHTML_NAMESPACE_XLINK: {
258 0 ▶ if(callback("xlink:", 6, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("xlink:", 6, ptr)) 0 0 0
259 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
260
261 0 break;
262 }
263 0 default:
264 0 break;
265 }
266
267
268 size_t length;
269 0 const char *data = myhtml_attribute_key(attr, &length);
270 0 ▶ if(data) {
Branch: 1 branch
BranchTrueFalseTotal
if(data) { 0 0 0
271 0 ▶ if(callback(data, length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data, length, ptr)) 0 0 0
272 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
273 }
274 0 ▶ if(callback("=\"", 2, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("=\"", 2, ptr)) 0 0 0
275 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
276
277 0 data = myhtml_attribute_value(attr, &length);
278 0 ▶ if(data) {
Branch: 1 branch
BranchTrueFalseTotal
if(data) { 0 0 0
279 0 ▶ if(myhtml_serialization_append_attr(data, length, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_append_attr(data, length, callback, ptr)) 0 0 0
280 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
281 }
282 0 ▶ if(callback("\"", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("\"", 1, ptr)) 0 0 0
283 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
284
285 0 attr = attr->next;
286 }
287
288 0 return MyCORE_STATUS_OK;
289 }
290  
291 /**
292 * Internal function to process a close tag
293 * @param tree
294 * @param node
295 * @param callback
296 * @param ptr
297 */
298 0 mystatus_t myhtml_serialization_node_append_close(myhtml_tree_node_t* node, mycore_callback_serialize_f callback, void* ptr)
299 {
300 0 switch (node->tag_id) {
301 0 case MyHTML_TAG__TEXT:
302 case MyHTML_TAG__COMMENT:
303 case MyHTML_TAG__DOCTYPE:
304 0 return MyCORE_STATUS_OK;
305
306 // https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments
307 0 case MyHTML_TAG_AREA:
308 case MyHTML_TAG_BASE:
309 case MyHTML_TAG_BGSOUND:
310 case MyHTML_TAG_BR:
311 case MyHTML_TAG_COL:
312 case MyHTML_TAG_EMBED:
313 case MyHTML_TAG_FRAME:
314 case MyHTML_TAG_HR:
315 case MyHTML_TAG_IMG:
316 case MyHTML_TAG_INPUT:
317 case MyHTML_TAG_KEYGEN:
318 case MyHTML_TAG_LINK:
319 case MyHTML_TAG_META:
320 case MyHTML_TAG_PARAM:
321 case MyHTML_TAG_SOURCE:
322 case MyHTML_TAG_TRACK:
323 case MyHTML_TAG_WBR:
324 0 ▶ if(node->ns == MyHTML_NAMESPACE_HTML)
Branch: 1 branch
BranchTrueFalseTotal
if(node->ns == MyHTML_NAMESPACE_HTML) 0 0 0
325 0 return MyCORE_STATUS_OK;
326 0 break;
327 }
328
329 size_t length;
330 0 const char *tag = myhtml_tag_name_by_id(node->tree, node->tag_id, &length);
331
332 0 ▶ if(callback("</", 2, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("</", 2, ptr)) 0 0 0
333 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
334
335 0 ▶ if(callback(tag, length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(tag, length, ptr)) 0 0 0
336 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
337
338 0 ▶ if(callback(">", 1, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(">", 1, ptr)) 0 0 0
339 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
340
341 0 return MyCORE_STATUS_OK;
342 }
343  
344 /**
345 * Internal function to process a text node
346 * @param tree
347 * @param node
348 * @param callback
349 * @param ptr
350 */
351 0 mystatus_t myhtml_serialization_node_append_text_node(myhtml_tree_node_t* node, mycore_callback_serialize_f callback, void* ptr)
352 {
353 0 ▶ if(node->token == NULL || node->token->str.data == NULL) return MyCORE_STATUS_OK;
Branch: 2 branches
BranchTrueFalseTotal
if(node->token == NULL || node->token->str.data == NULL) return MyCORE_STATUS_OK; 0 0 0
if(node->token == NULL || node->token->str.data == NULL) return MyCORE_STATUS_OK; 0 0 0
354
355 0 ▶ if(node->parent == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(node->parent == NULL) { 0 0 0
356 0 ▶ if(myhtml_serialization_append(node->token->str.data, node->token->str.length, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_append(node->token->str.data, node->token->str.length, callback, ptr)) 0 0 0
357 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
358
359 0 return MyCORE_STATUS_OK;
360 }
361
362 0 ▶ switch (node->parent->tag_id) {
Branch: 1 branch
BranchTrueFalseTotal
switch (node->parent->tag_id) { 0 0 0
363 0 case MyHTML_TAG_STYLE:
364 case MyHTML_TAG_SCRIPT:
365 case MyHTML_TAG_XMP:
366 case MyHTML_TAG_IFRAME:
367 case MyHTML_TAG_NOEMBED:
368 case MyHTML_TAG_NOFRAMES:
369 case MyHTML_TAG_PLAINTEXT:
370 0 ▶ if(callback(node->token->str.data, node->token->str.length, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(node->token->str.data, node->token->str.length, ptr)) 0 0 0
371 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
372 0 break;
373 0 default:
374 0 ▶ if(myhtml_serialization_append(node->token->str.data, node->token->str.length, callback, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_append(node->token->str.data, node->token->str.length, callback, ptr)) 0 0 0
375 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
376
377 0 break;
378 }
379
380 0 return MyCORE_STATUS_OK;
381 }
382  
383 /**
384 * Internal method to process a buffer that could contain to-be-quoted output
385 * @param data
386 * @param size
387 * @param callback
388 * @param ptr
389 */
390 0 mystatus_t myhtml_serialization_append(const char *data, size_t size, mycore_callback_serialize_f callback, void* ptr)
391 {
392 // number of chars not yet displayed
393 0 size_t notwritten = 0;
394
395 // iterate over the buffer
396 0 ▶ for (size_t i = 0; i < size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < size; ++i) { 0 0 0
397 0 switch ((unsigned char)data[i]) {
398 0 case '&':
399 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
400 0 ▶ if(callback(data + i - notwritten, notwritten, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + i - notwritten, notwritten, ptr)) 0 0 0
401 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
402 }
403
404 0 ▶ if(callback("&amp;", 5, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("&amp;", 5, ptr)) 0 0 0
405 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
406
407 0 notwritten = 0;
408 0 break;
409 0 case '<':
410 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
411 0 ▶ if(callback(data + i - notwritten, notwritten, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + i - notwritten, notwritten, ptr)) 0 0 0
412 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
413 }
414
415 0 ▶ if(callback("&lt;", 4, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("&lt;", 4, ptr)) 0 0 0
416 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
417
418 0 notwritten = 0;
419 0 break;
420 0 case '>':
421 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
422 0 ▶ if(callback(data + i - notwritten, notwritten, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + i - notwritten, notwritten, ptr)) 0 0 0
423 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
424 }
425
426 0 ▶ if(callback("&gt;", 4, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("&gt;", 4, ptr)) 0 0 0
427 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
428
429 0 notwritten = 0;
430 0 break;
431 0 case 0xA0:
432 0 ▶ if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) {
Branch: 2 branches
BranchTrueFalseTotal
if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) { 0 0 0
if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) { 0 0 0
433 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
434 0 ▶ if(callback(data + i - notwritten, (notwritten - 1), ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + i - notwritten, (notwritten - 1), ptr)) 0 0 0
435 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
436 }
437
438 0 ▶ if(callback("&nbsp;", 6, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("&nbsp;", 6, ptr)) 0 0 0
439 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
440
441 0 notwritten = 0;
442 }
443 else {
444 0 ++notwritten;
445 }
446
447 0 break;
448 0 default:
449 0 ++notwritten;
450 0 break;
451 }
452 }
453
454 0 ▶ if(notwritten)
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) 0 0 0
455 0 ▶ if(callback(data + size - notwritten, notwritten, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + size - notwritten, notwritten, ptr)) 0 0 0
456 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
457
458 0 return MyCORE_STATUS_OK;
459 }
460  
461 /**
462 * Internal method to process an attribute
463 * @param data
464 * @param size
465 * @param callback
466 * @param ptr
467 */
468 0 mystatus_t myhtml_serialization_append_attr(const char* data, size_t size, mycore_callback_serialize_f callback, void* ptr)
469 {
470 // number of chars not yet displayed
471 0 size_t notwritten = 0;
472
473 // iterate over the buffer
474 0 ▶ for (size_t i = 0; i < size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (size_t i = 0; i < size; ++i) { 0 0 0
475 0 switch ((unsigned char)data[i]) {
476 0 case '&':
477 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
478 0 ▶ if(callback(data + i - notwritten, notwritten, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + i - notwritten, notwritten, ptr)) 0 0 0
479 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
480 }
481
482 0 ▶ if(callback("&amp;", 5, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("&amp;", 5, ptr)) 0 0 0
483 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
484
485 0 notwritten = 0;
486 0 break;
487 0 case '"':
488 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
489 0 ▶ if(callback(data + i - notwritten, notwritten, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + i - notwritten, notwritten, ptr)) 0 0 0
490 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
491 }
492
493 0 ▶ if(callback("&quot;", 6, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("&quot;", 6, ptr)) 0 0 0
494 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
495
496 0 notwritten = 0;
497 0 break;
498 0 case 0xA0:
499 0 ▶ if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) {
Branch: 2 branches
BranchTrueFalseTotal
if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) { 0 0 0
if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) { 0 0 0
500 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
501 0 ▶ if(callback(data + i - notwritten, (notwritten - 1), ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + i - notwritten, (notwritten - 1), ptr)) 0 0 0
502 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
503 }
504
505 0 ▶ if(callback("&nbsp;", 6, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback("&nbsp;", 6, ptr)) 0 0 0
506 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
507
508 0 notwritten = 0;
509 }
510 else {
511 0 ++notwritten;
512 }
513
514 0 break;
515 0 default:
516 0 ++notwritten;
517 0 break;
518 }
519 }
520
521 0 ▶ if(notwritten) {
Branch: 1 branch
BranchTrueFalseTotal
if(notwritten) { 0 0 0
522 0 ▶ if(callback(data + size - notwritten, notwritten, ptr))
Branch: 1 branch
BranchTrueFalseTotal
if(callback(data + size - notwritten, notwritten, ptr)) 0 0 0
523 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
524 }
525
526 0 return MyCORE_STATUS_OK;
527 }
528  
529 /**
530 * Reallocate the buffer
531 * @param str the buffer to reallocate
532 * @param size new size
533 */
534 0 mystatus_t myhtml_serialization_reallocate(mycore_string_raw_t *str, size_t size)
535 {
536 // construct a buffer
537 0 char *data = (char*)mycore_realloc(str->data, size * sizeof(char));
538  
539 // was it ok?
540 0 ▶ if(data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(data == NULL) { 0 0 0
541 0 memset(str, 0, sizeof(mycore_string_raw_t));
542 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
543 }
544 else {
545 // reallocation succeeded
546 0 str->data = data;
547 0 str->size = size;
548 }
549
550 0 return MyCORE_STATUS_OK;
551 }
552  
553 /**
554 * Implementation of the mycore_callback_serialize_f function for internal
555 * use that concatenats everything to a string
556 * @param data
557 * @param size
558 */
559 0 mystatus_t myhtml_serialization_concatenate(const char* data, size_t length, void *ptr)
560 {
561 0 mycore_string_raw_t *str = (mycore_string_raw_t*)ptr;
562
563 // do we still have enough size in the output buffer?
564 0 ▶ if ((length + str->length) >= str->size) {
Branch: 1 branch
BranchTrueFalseTotal
if ((length + str->length) >= str->size) { 0 0 0
565 0 ▶ if(myhtml_serialization_reallocate(str, length + str->length + 4096))
Branch: 1 branch
BranchTrueFalseTotal
if(myhtml_serialization_reallocate(str, length + str->length + 4096)) 0 0 0
566 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
567 }
568
569 // copy data
570 0 strncpy(&str->data[ str->length ], data, length);
571
572 // update counters
573 0 str->length += length;
574 0 str->data[ str->length ] = '\0';
575
576 0 return MyCORE_STATUS_OK;
577 }
578  
579 /**
580 * Serialize tree to an output string
581 * @param tree
582 * @param scope_node
583 * @param str
584 * @return bool
585 */
586 0 mystatus_t myhtml_serialization_tree_buffer(myhtml_tree_node_t* scope_node, mycore_string_raw_t* str) {
587  
588 // we need an output variable
589 0 ▶ if(str == NULL) return false;
Branch: 1 branch
BranchTrueFalseTotal
if(str == NULL) return false; 0 0 0
590
591 // allocate space that is most likely enough for the output
592 0 ▶ if(str->data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(str->data == NULL) { 0 0 0
593 0 str->size = 4098 * 5;
594 0 str->length = 0;
595 0 str->data = (char*)mycore_malloc(str->size * sizeof(char));
596
597 0 ▶ if(str->data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(str->data == NULL) { 0 0 0
598 0 str->size = 0;
599 0 return MyCORE_STATUS_ERROR_MEMORY_ALLOCATION;
600 }
601 }
602
603 0 return myhtml_serialization_tree_callback(scope_node, myhtml_serialization_concatenate, str);
604 }
605  
606 /**
607 * Serialize node to an output string
608 * @param tree
609 * @param node
610 * @param str
611 * @return bool
612 */
613 0 mystatus_t myhtml_serialization_node_buffer(myhtml_tree_node_t* node, mycore_string_raw_t* str) {
614  
615 // we need an output variable
616 0 ▶ if(str == NULL) return false;
Branch: 1 branch
BranchTrueFalseTotal
if(str == NULL) return false; 0 0 0
617
618 // allocate space that is most likely enough for the output
619 0 ▶ if(str->data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(str->data == NULL) { 0 0 0
620 0 str->size = 2048;
621 0 str->length = 0;
622 0 str->data = (char*)mycore_malloc(str->size * sizeof(char));
623
624 0 ▶ if(str->data == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if(str->data == NULL) { 0 0 0
625 0 str->size = 0;
626 0 return false;
627 }
628 }
629
630 0 return myhtml_serialization_node_callback(node, myhtml_serialization_concatenate, str);
631 }
632  
« third_party/modest/source/myhtml/rules.c ↑ Summary third_party/modest/source/myhtml/stream.c »