| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/array_list.h> |
| 7 |
#include <aws/common/logging.h> |
| 8 |
#include <aws/common/private/xml_parser_impl.h> |
| 9 |
|
| 10 |
#ifdef _MSC_VER |
| 11 |
|
| 12 |
# pragma warning(disable : 4204) |
| 13 |
#endif |
| 14 |
|
| 15 |
static const size_t s_max_document_depth = 20; |
| 16 |
#define MAX_NAME_LEN ((size_t)256) |
| 17 |
#define NODE_CLOSE_OVERHEAD ((size_t)3) |
| 18 |
|
| 19 |
struct cb_stack_data { |
| 20 |
aws_xml_parser_on_node_encountered_fn *cb; |
| 21 |
void *user_data; |
| 22 |
}; |
| 23 |
|
| 24 |
int s_node_next_sibling(struct aws_xml_parser *parser); |
| 25 |
|
| 26 |
static bool s_double_quote_fn(uint8_t value) { |
| 27 |
return value == '"'; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
static int s_load_node_decl( |
| 36 |
struct aws_xml_parser *parser, |
| 37 |
struct aws_byte_cursor *decl_body, |
| 38 |
struct aws_xml_node *node) { |
| 39 |
AWS_PRECONDITION(parser); |
| 40 |
AWS_PRECONDITION(decl_body); |
| 41 |
AWS_PRECONDITION(node); |
| 42 |
|
| 43 |
if (decl_body->len == 0) { |
| 44 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 45 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 46 |
} |
| 47 |
|
| 48 |
node->is_empty = decl_body->ptr[decl_body->len - 1] == '/'; |
| 49 |
|
| 50 |
struct aws_array_list splits; |
| 51 |
AWS_ZERO_STRUCT(splits); |
| 52 |
|
| 53 |
AWS_ZERO_ARRAY(parser->split_scratch); |
| 54 |
aws_array_list_init_static( |
| 55 |
&splits, parser->split_scratch, AWS_ARRAY_SIZE(parser->split_scratch), sizeof(struct aws_byte_cursor)); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
if (aws_byte_cursor_split_on_char(decl_body, ' ', &splits)) { |
| 60 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 61 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 62 |
} |
| 63 |
|
| 64 |
size_t splits_count = aws_array_list_length(&splits); |
| 65 |
|
| 66 |
if (splits_count < 1) { |
| 67 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 68 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 69 |
} |
| 70 |
|
| 71 |
aws_array_list_get_at(&splits, &node->name, 0); |
| 72 |
|
| 73 |
AWS_ZERO_ARRAY(parser->attributes); |
| 74 |
if (splits.length > 1) { |
| 75 |
aws_array_list_init_static( |
| 76 |
&node->attributes, |
| 77 |
parser->attributes, |
| 78 |
AWS_ARRAY_SIZE(parser->attributes), |
| 79 |
sizeof(struct aws_xml_attribute)); |
| 80 |
|
| 81 |
for (size_t i = 1; i < splits.length; ++i) { |
| 82 |
struct aws_byte_cursor attribute_pair; |
| 83 |
AWS_ZERO_STRUCT(attribute_pair); |
| 84 |
aws_array_list_get_at(&splits, &attribute_pair, i); |
| 85 |
|
| 86 |
struct aws_byte_cursor att_val_pair[2]; |
| 87 |
AWS_ZERO_ARRAY(att_val_pair); |
| 88 |
struct aws_array_list att_val_pair_lst; |
| 89 |
AWS_ZERO_STRUCT(att_val_pair_lst); |
| 90 |
aws_array_list_init_static(&att_val_pair_lst, att_val_pair, 2, sizeof(struct aws_byte_cursor)); |
| 91 |
|
| 92 |
if (!aws_byte_cursor_split_on_char(&attribute_pair, '=', &att_val_pair_lst)) { |
| 93 |
struct aws_xml_attribute attribute = { |
| 94 |
.name = att_val_pair[0], |
| 95 |
.value = aws_byte_cursor_trim_pred(&att_val_pair[1], s_double_quote_fn), |
| 96 |
}; |
| 97 |
aws_array_list_push_back(&node->attributes, &attribute); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return AWS_OP_SUCCESS; |
| 103 |
} |
| 104 |
|
| 105 |
int aws_xml_parse(struct aws_allocator *allocator, const struct aws_xml_parser_options *options) { |
| 106 |
|
| 107 |
AWS_PRECONDITION(allocator); |
| 108 |
AWS_PRECONDITION(options); |
| 109 |
AWS_PRECONDITION(options->on_root_encountered); |
| 110 |
|
| 111 |
struct aws_xml_parser parser = { |
| 112 |
.allocator = allocator, |
| 113 |
.doc = options->doc, |
| 114 |
.max_depth = options->max_depth ? options->max_depth : s_max_document_depth, |
| 115 |
.error = AWS_OP_SUCCESS, |
| 116 |
}; |
| 117 |
aws_array_list_init_dynamic(&parser.callback_stack, allocator, 4, sizeof(struct cb_stack_data)); |
| 118 |
|
| 119 |
|
| 120 |
while (parser.doc.len) { |
| 121 |
const uint8_t *start = memchr(parser.doc.ptr, '<', parser.doc.len); |
| 122 |
if (!start) { |
| 123 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 124 |
parser.error = aws_raise_error(AWS_ERROR_INVALID_XML); |
| 125 |
goto clean_up; |
| 126 |
} |
| 127 |
|
| 128 |
const uint8_t *location = memchr(parser.doc.ptr, '>', parser.doc.len); |
| 129 |
if (!location) { |
| 130 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 131 |
parser.error = aws_raise_error(AWS_ERROR_INVALID_XML); |
| 132 |
goto clean_up; |
| 133 |
} |
| 134 |
|
| 135 |
if (start > location) { |
| 136 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 137 |
parser.error = aws_raise_error(AWS_ERROR_INVALID_XML); |
| 138 |
goto clean_up; |
| 139 |
} |
| 140 |
|
| 141 |
aws_byte_cursor_advance(&parser.doc, start - parser.doc.ptr); |
| 142 |
|
| 143 |
|
| 144 |
if (*(parser.doc.ptr + 1) == '?' || *(parser.doc.ptr + 1) == '!') { |
| 145 |
|
| 146 |
size_t advance = location - parser.doc.ptr + 1; |
| 147 |
aws_byte_cursor_advance(&parser.doc, advance); |
| 148 |
} else { |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
struct cb_stack_data stack_data = { |
| 155 |
.cb = options->on_root_encountered, |
| 156 |
.user_data = options->user_data, |
| 157 |
}; |
| 158 |
|
| 159 |
aws_array_list_push_back(&parser.callback_stack, &stack_data); |
| 160 |
parser.error = s_node_next_sibling(&parser); |
| 161 |
|
| 162 |
clean_up: |
| 163 |
aws_array_list_clean_up(&parser.callback_stack); |
| 164 |
return parser.error; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
static bool s_is_tag_name_boundary(uint8_t c) { |
| 169 |
return c == ' ' || c == '>' || c == '/' || c == '\t' || c == '\r' || c == '\n'; |
| 170 |
} |
| 171 |
|
| 172 |
static struct aws_byte_cursor s_comment_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("<!--"); |
| 173 |
static struct aws_byte_cursor s_comment_end = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("-->"); |
| 174 |
static struct aws_byte_cursor s_cdata_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("<![CDATA["); |
| 175 |
static struct aws_byte_cursor s_cdata_end = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("]]>"); |
| 176 |
static struct aws_byte_cursor s_pi_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("<?"); |
| 177 |
static struct aws_byte_cursor s_pi_end = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("?>"); |
| 178 |
static struct aws_byte_cursor s_self_close_suffix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("/>"); |
| 179 |
static struct aws_byte_cursor s_decl_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("<!"); |
| 180 |
static struct aws_byte_cursor s_close_bracket = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(">"); |
| 181 |
static struct aws_byte_cursor s_closing_prefix = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("</"); |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static int s_try_skip_non_element(struct aws_byte_cursor *input, bool *out_skipped) { |
| 198 |
*out_skipped = false; |
| 199 |
|
| 200 |
if (input->len == 0 || input->ptr[0] != '<') { |
| 201 |
return AWS_OP_SUCCESS; |
| 202 |
} |
| 203 |
|
| 204 |
if (aws_byte_cursor_starts_with(input, &s_comment_prefix)) { |
| 205 |
aws_byte_cursor_advance(input, s_comment_prefix.len); |
| 206 |
struct aws_byte_cursor found; |
| 207 |
if (aws_byte_cursor_find_exact(input, &s_comment_end, &found)) { |
| 208 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 209 |
} |
| 210 |
aws_byte_cursor_advance(input, (found.ptr + s_comment_end.len) - input->ptr); |
| 211 |
*out_skipped = true; |
| 212 |
return AWS_OP_SUCCESS; |
| 213 |
} |
| 214 |
if (aws_byte_cursor_starts_with(input, &s_cdata_prefix)) { |
| 215 |
aws_byte_cursor_advance(input, s_cdata_prefix.len); |
| 216 |
struct aws_byte_cursor found; |
| 217 |
if (aws_byte_cursor_find_exact(input, &s_cdata_end, &found)) { |
| 218 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 219 |
} |
| 220 |
aws_byte_cursor_advance(input, (found.ptr + s_cdata_end.len) - input->ptr); |
| 221 |
*out_skipped = true; |
| 222 |
return AWS_OP_SUCCESS; |
| 223 |
} |
| 224 |
if (aws_byte_cursor_starts_with(input, &s_pi_prefix)) { |
| 225 |
aws_byte_cursor_advance(input, s_pi_prefix.len); |
| 226 |
struct aws_byte_cursor found; |
| 227 |
if (aws_byte_cursor_find_exact(input, &s_pi_end, &found)) { |
| 228 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 229 |
} |
| 230 |
aws_byte_cursor_advance(input, (found.ptr + s_pi_end.len) - input->ptr); |
| 231 |
*out_skipped = true; |
| 232 |
return AWS_OP_SUCCESS; |
| 233 |
} |
| 234 |
|
| 235 |
return AWS_OP_SUCCESS; |
| 236 |
} |
| 237 |
|
| 238 |
int s_advance_to_closing_tag( |
| 239 |
struct aws_xml_parser *parser, |
| 240 |
struct aws_xml_node *node, |
| 241 |
struct aws_byte_cursor *out_body) { |
| 242 |
AWS_PRECONDITION(parser); |
| 243 |
AWS_PRECONDITION(node); |
| 244 |
|
| 245 |
if (node->is_empty) { |
| 246 |
if (out_body) { |
| 247 |
out_body->ptr = NULL; |
| 248 |
out_body->len = 0; |
| 249 |
} |
| 250 |
return AWS_OP_SUCCESS; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
uint8_t name_close[MAX_NAME_LEN + NODE_CLOSE_OVERHEAD] = {0}; |
| 256 |
uint8_t name_open[MAX_NAME_LEN + NODE_CLOSE_OVERHEAD] = {0}; |
| 257 |
|
| 258 |
struct aws_byte_buf closing_cmp_buf = aws_byte_buf_from_empty_array(name_close, sizeof(name_close)); |
| 259 |
struct aws_byte_buf open_cmp_buf = aws_byte_buf_from_empty_array(name_open, sizeof(name_open)); |
| 260 |
|
| 261 |
size_t closing_name_len = node->name.len + NODE_CLOSE_OVERHEAD; |
| 262 |
|
| 263 |
if (closing_name_len > node->doc_at_body.len) { |
| 264 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 265 |
parser->error = aws_raise_error(AWS_ERROR_INVALID_XML); |
| 266 |
return AWS_OP_ERR; |
| 267 |
} |
| 268 |
|
| 269 |
if (sizeof(name_close) < closing_name_len) { |
| 270 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 271 |
parser->error = aws_raise_error(AWS_ERROR_INVALID_XML); |
| 272 |
return AWS_OP_ERR; |
| 273 |
} |
| 274 |
|
| 275 |
struct aws_byte_cursor open_bracket = aws_byte_cursor_from_c_str("<"); |
| 276 |
struct aws_byte_cursor close_token = aws_byte_cursor_from_c_str("/"); |
| 277 |
struct aws_byte_cursor close_bracket = aws_byte_cursor_from_c_str(">"); |
| 278 |
|
| 279 |
aws_byte_buf_append(&open_cmp_buf, &open_bracket); |
| 280 |
aws_byte_buf_append(&open_cmp_buf, &node->name); |
| 281 |
|
| 282 |
aws_byte_buf_append(&closing_cmp_buf, &open_bracket); |
| 283 |
aws_byte_buf_append(&closing_cmp_buf, &close_token); |
| 284 |
aws_byte_buf_append(&closing_cmp_buf, &node->name); |
| 285 |
aws_byte_buf_append(&closing_cmp_buf, &close_bracket); |
| 286 |
|
| 287 |
struct aws_byte_cursor to_find_open = aws_byte_cursor_from_buf(&open_cmp_buf); |
| 288 |
struct aws_byte_cursor to_find_close = aws_byte_cursor_from_buf(&closing_cmp_buf); |
| 289 |
|
| 290 |
|
| 291 |
size_t depth = 1; |
| 292 |
while (parser->doc.len > 0) { |
| 293 |
const uint8_t *open = memchr(parser->doc.ptr, '<', parser->doc.len); |
| 294 |
if (!open) { |
| 295 |
break; |
| 296 |
} |
| 297 |
aws_byte_cursor_advance(&parser->doc, open - parser->doc.ptr); |
| 298 |
|
| 299 |
|
| 300 |
bool skipped = false; |
| 301 |
if (s_try_skip_non_element(&parser->doc, &skipped)) { |
| 302 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 303 |
return AWS_OP_ERR; |
| 304 |
} |
| 305 |
if (skipped) { |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
if (aws_byte_cursor_starts_with(&parser->doc, &to_find_close)) { |
| 311 |
depth--; |
| 312 |
if (depth == 0) { |
| 313 |
size_t len = parser->doc.ptr - node->doc_at_body.ptr; |
| 314 |
if (out_body) { |
| 315 |
*out_body = aws_byte_cursor_from_array(node->doc_at_body.ptr, len); |
| 316 |
} |
| 317 |
aws_byte_cursor_advance(&parser->doc, to_find_close.len); |
| 318 |
return parser->error; |
| 319 |
} |
| 320 |
aws_byte_cursor_advance(&parser->doc, to_find_close.len); |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
if (aws_byte_cursor_starts_with(&parser->doc, &to_find_open)) { |
| 326 |
|
| 327 |
struct aws_byte_cursor after_open = parser->doc; |
| 328 |
aws_byte_cursor_advance(&after_open, to_find_open.len); |
| 329 |
|
| 330 |
if (after_open.len > 0 && s_is_tag_name_boundary(*after_open.ptr)) { |
| 331 |
*/ |
| 332 |
if (!aws_byte_cursor_starts_with(&after_open, &s_self_close_suffix)) { |
| 333 |
depth++; |
| 334 |
} |
| 335 |
} |
| 336 |
ks |
| 337 |
|
| 338 |
aws_byte_cursor_advance(&parser->doc, 1); |
| 339 |
continue; |
| 340 |
} |
| 341 |
|
| 342 |
*/ |
| 343 |
aws_byte_cursor_advance(&parser->doc, 1); |
| 344 |
} |
| 345 |
|
| 346 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 347 |
parser->error = aws_raise_error(AWS_ERROR_INVALID_XML); |
| 348 |
return AWS_OP_ERR; |
| 349 |
} |
| 350 |
|
| 351 |
int aws_xml_node_as_body(struct aws_xml_node *node, struct aws_byte_cursor *out_body) { |
| 352 |
AWS_PRECONDITION(node); |
| 353 |
|
| 354 |
AWS_FATAL_ASSERT(!node->processed && "XML node can be traversed, or read as body, but not both."); |
| 355 |
node->processed = true; |
| 356 |
return s_advance_to_closing_tag(node->parser, node, out_body); |
| 357 |
} |
| 358 |
|
| 359 |
int aws_xml_node_traverse( |
| 360 |
struct aws_xml_node *node, |
| 361 |
aws_xml_parser_on_node_encountered_fn *on_node_encountered, |
| 362 |
void *user_data) { |
| 363 |
AWS_PRECONDITION(node); |
| 364 |
AWS_PRECONDITION(on_node_encountered); |
| 365 |
|
| 366 |
struct aws_xml_parser *parser = node->parser; |
| 367 |
|
| 368 |
AWS_FATAL_ASSERT(!node->processed && "XML node can be traversed, or read as body, but not both."); |
| 369 |
node->processed = true; |
| 370 |
struct cb_stack_data stack_data = { |
| 371 |
.cb = on_node_encountered, |
| 372 |
.user_data = user_data, |
| 373 |
}; |
| 374 |
|
| 375 |
size_t doc_depth = aws_array_list_length(&parser->callback_stack); |
| 376 |
if (doc_depth >= parser->max_depth) { |
| 377 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document exceeds max depth of %zu.", parser->max_depth); |
| 378 |
aws_raise_error(AWS_ERROR_INVALID_XML); |
| 379 |
goto error; |
| 380 |
} |
| 381 |
|
| 382 |
aws_array_list_push_back(&parser->callback_stack, &stack_data); |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
while (!parser->error) { |
| 387 |
const uint8_t *open = memchr(parser->doc.ptr, '<', parser->doc.len); |
| 388 |
|
| 389 |
if (!open) { |
| 390 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 391 |
aws_raise_error(AWS_ERROR_INVALID_XML); |
| 392 |
goto error; |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
aws_byte_cursor_advance(&parser->doc, open - parser->doc.ptr); |
| 397 |
|
| 398 |
*/ |
| 399 |
bool skipped = false; |
| 400 |
if (s_try_skip_non_element(&parser->doc, &skipped)) { |
| 401 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 402 |
goto error; |
| 403 |
} |
| 404 |
if (skipped) { |
| 405 |
continue; |
| 406 |
} |
| 407 |
|
| 408 |
*/ |
| 409 |
if (aws_byte_cursor_starts_with(&parser->doc, &s_decl_prefix)) { |
| 410 |
struct aws_byte_cursor found; |
| 411 |
if (aws_byte_cursor_find_exact(&parser->doc, &s_close_bracket, &found)) { |
| 412 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 413 |
aws_raise_error(AWS_ERROR_INVALID_XML); |
| 414 |
goto error; |
| 415 |
} |
| 416 |
aws_byte_cursor_advance(&parser->doc, (found.ptr + 1) - parser->doc.ptr); |
| 417 |
continue; |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
if (parser->doc.len < 2) { |
| 422 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 423 |
aws_raise_error(AWS_ERROR_INVALID_XML); |
| 424 |
goto error; |
| 425 |
} |
| 426 |
const uint8_t *end_location = memchr(parser->doc.ptr + 1, '>', parser->doc.len - 1); |
| 427 |
|
| 428 |
if (!end_location) { |
| 429 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 430 |
aws_raise_error(AWS_ERROR_INVALID_XML); |
| 431 |
goto error; |
| 432 |
} |
| 433 |
|
| 434 |
bool parent_closed = aws_byte_cursor_starts_with(&parser->doc, &s_closing_prefix); |
| 435 |
|
| 436 |
size_t node_name_len = end_location - parser->doc.ptr; |
| 437 |
struct aws_byte_cursor decl_body = aws_byte_cursor_from_array(parser->doc.ptr + 1, node_name_len - 1); |
| 438 |
|
| 439 |
aws_byte_cursor_advance(&parser->doc, end_location - parser->doc.ptr + 1); |
| 440 |
|
| 441 |
if (parent_closed) { |
| 442 |
break; |
| 443 |
} |
| 444 |
|
| 445 |
struct aws_xml_node next_node = { |
| 446 |
.parser = parser, |
| 447 |
.doc_at_body = parser->doc, |
| 448 |
.processed = false, |
| 449 |
}; |
| 450 |
|
| 451 |
if (s_load_node_decl(parser, &decl_body, &next_node)) { |
| 452 |
return AWS_OP_ERR; |
| 453 |
} |
| 454 |
|
| 455 |
if (on_node_encountered(&next_node, user_data)) { |
| 456 |
goto error; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
if (!next_node.processed) { |
| 461 |
if (s_advance_to_closing_tag(parser, &next_node, NULL)) { |
| 462 |
goto error; |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
aws_array_list_pop_back(&parser->callback_stack); |
| 468 |
return parser->error; |
| 469 |
|
| 470 |
error: |
| 471 |
parser->error = AWS_OP_ERR; |
| 472 |
return parser->error; |
| 473 |
} |
| 474 |
|
| 475 |
struct aws_byte_cursor aws_xml_node_get_name(const struct aws_xml_node *node) { |
| 476 |
AWS_PRECONDITION(node); |
| 477 |
return node->name; |
| 478 |
} |
| 479 |
|
| 480 |
size_t aws_xml_node_get_num_attributes(const struct aws_xml_node *node) { |
| 481 |
AWS_PRECONDITION(node); |
| 482 |
return aws_array_list_length(&node->attributes); |
| 483 |
} |
| 484 |
|
| 485 |
struct aws_xml_attribute aws_xml_node_get_attribute(const struct aws_xml_node *node, size_t attribute_index) { |
| 486 |
AWS_PRECONDITION(node); |
| 487 |
|
| 488 |
struct aws_xml_attribute attribute; |
| 489 |
if (aws_array_list_get_at(&node->attributes, &attribute, attribute_index)) { |
| 490 |
AWS_FATAL_ASSERT(0 && "Invalid XML attribute index"); |
| 491 |
} |
| 492 |
|
| 493 |
return attribute; |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
int s_node_next_sibling(struct aws_xml_parser *parser) { |
| 498 |
AWS_PRECONDITION(parser); |
| 499 |
|
| 500 |
const uint8_t *next_location = memchr(parser->doc.ptr, '<', parser->doc.len); |
| 501 |
|
| 502 |
if (!next_location) { |
| 503 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 504 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 505 |
} |
| 506 |
|
| 507 |
aws_byte_cursor_advance(&parser->doc, next_location - parser->doc.ptr); |
| 508 |
const uint8_t *end_location = memchr(parser->doc.ptr, '>', parser->doc.len); |
| 509 |
|
| 510 |
if (!end_location || next_location >= end_location) { |
| 511 |
AWS_LOGF_ERROR(AWS_LS_COMMON_XML_PARSER, "XML document is invalid."); |
| 512 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 513 |
} |
| 514 |
|
| 515 |
size_t node_name_len = end_location - next_location; |
| 516 |
aws_byte_cursor_advance(&parser->doc, end_location - parser->doc.ptr + 1); |
| 517 |
|
| 518 |
struct aws_byte_cursor node_decl_body = aws_byte_cursor_from_array(next_location + 1, node_name_len - 1); |
| 519 |
|
| 520 |
struct aws_xml_node sibling_node = { |
| 521 |
.parser = parser, |
| 522 |
.doc_at_body = parser->doc, |
| 523 |
.processed = false, |
| 524 |
}; |
| 525 |
|
| 526 |
if (s_load_node_decl(parser, &node_decl_body, &sibling_node)) { |
| 527 |
return AWS_OP_ERR; |
| 528 |
} |
| 529 |
|
| 530 |
struct cb_stack_data stack_data; |
| 531 |
AWS_ZERO_STRUCT(stack_data); |
| 532 |
aws_array_list_back(&parser->callback_stack, &stack_data); |
| 533 |
AWS_FATAL_ASSERT(stack_data.cb); |
| 534 |
|
| 535 |
if (stack_data.cb(&sibling_node, stack_data.user_data)) { |
| 536 |
return AWS_OP_ERR; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
if (!sibling_node.processed) { |
| 541 |
if (s_advance_to_closing_tag(parser, &sibling_node, NULL)) { |
| 542 |
return AWS_OP_ERR; |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
return parser->error; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
static int s_build_unescaped_buffer(struct aws_byte_cursor data, struct aws_byte_buf *out) { |
| 557 |
bool first_split = true; |
| 558 |
|
| 559 |
struct aws_byte_cursor substr = {0}; |
| 560 |
while (aws_byte_cursor_next_split(&data, '&', &substr)) { |
| 561 |
if (first_split) { |
| 562 |
AWS_RETURN_ERROR_IF(aws_byte_buf_append(out, &substr) == AWS_OP_SUCCESS, AWS_ERROR_INVALID_STATE); |
| 563 |
first_split = false; |
| 564 |
continue; |
| 565 |
} |
| 566 |
|
| 567 |
struct aws_byte_cursor escaped = {0}; |
| 568 |
AWS_RETURN_ERROR_IF(aws_byte_cursor_next_split(&substr, ';', &escaped), AWS_ERROR_INVALID_STATE); |
| 569 |
aws_byte_cursor_advance(&substr, escaped.len + 1); |
| 570 |
|
| 571 |
if (escaped.len == 2 && aws_byte_cursor_eq_c_str(&escaped, "lt")) { |
| 572 |
AWS_RETURN_ERROR_IF(aws_byte_buf_write_u8(out, '<'), AWS_ERROR_INVALID_STATE); |
| 573 |
} else if (escaped.len == 2 && aws_byte_cursor_eq_c_str(&escaped, "gt")) { |
| 574 |
AWS_RETURN_ERROR_IF(aws_byte_buf_write_u8(out, '>'), AWS_ERROR_INVALID_STATE); |
| 575 |
} else if (escaped.len == 3 && aws_byte_cursor_eq_c_str(&escaped, "amp")) { |
| 576 |
AWS_RETURN_ERROR_IF(aws_byte_buf_write_u8(out, '&'), AWS_ERROR_INVALID_STATE); |
| 577 |
} else if (escaped.len == 4 && aws_byte_cursor_eq_c_str(&escaped, "quot")) { |
| 578 |
AWS_RETURN_ERROR_IF(aws_byte_buf_write_u8(out, '"'), AWS_ERROR_INVALID_STATE); |
| 579 |
} else if (escaped.len == 4 && aws_byte_cursor_eq_c_str(&escaped, "apos")) { |
| 580 |
AWS_RETURN_ERROR_IF(aws_byte_buf_write_u8(out, '\''), AWS_ERROR_INVALID_STATE); |
| 581 |
} else if (escaped.len >= 2 && escaped.ptr[0] == '#') { |
| 582 |
uint64_t codepoint = 0; |
| 583 |
struct aws_byte_cursor codepoint_cur = escaped; |
| 584 |
if (escaped.ptr[1] == 'x' || escaped.ptr[1] == 'X') { |
| 585 |
aws_byte_cursor_advance(&codepoint_cur, 2); |
| 586 |
AWS_RETURN_ERROR_IF( |
| 587 |
aws_byte_cursor_utf8_parse_u64_hex(codepoint_cur, &codepoint) == AWS_OP_SUCCESS, |
| 588 |
AWS_ERROR_INVALID_XML); |
| 589 |
} else { |
| 590 |
aws_byte_cursor_advance(&codepoint_cur, 1); |
| 591 |
AWS_RETURN_ERROR_IF( |
| 592 |
aws_byte_cursor_utf8_parse_u64(codepoint_cur, &codepoint) == AWS_OP_SUCCESS, AWS_ERROR_INVALID_XML); |
| 593 |
} |
| 594 |
|
| 595 |
if (codepoint <= 0x7F) { |
| 596 |
AWS_RETURN_ERROR_IF(aws_byte_buf_write_u8(out, (uint8_t)codepoint), AWS_ERROR_INVALID_STATE); |
| 597 |
} else if (codepoint <= 0x7FF) { |
| 598 |
AWS_RETURN_ERROR_IF( |
| 599 |
aws_byte_buf_write_u8(out, (uint8_t)(0xC0 | (codepoint >> 6))), AWS_ERROR_INVALID_STATE); |
| 600 |
AWS_RETURN_ERROR_IF( |
| 601 |
aws_byte_buf_write_u8(out, (uint8_t)(0x80 | (codepoint & 0x3F))), AWS_ERROR_INVALID_STATE); |
| 602 |
} else if (codepoint <= 0xFFFF) { |
| 603 |
AWS_RETURN_ERROR_IF( |
| 604 |
aws_byte_buf_write_u8(out, (uint8_t)(0xE0 | (codepoint >> 12))), AWS_ERROR_INVALID_STATE); |
| 605 |
AWS_RETURN_ERROR_IF( |
| 606 |
aws_byte_buf_write_u8(out, (uint8_t)(0x80 | ((codepoint >> 6) & 0x3F))), AWS_ERROR_INVALID_STATE); |
| 607 |
AWS_RETURN_ERROR_IF( |
| 608 |
aws_byte_buf_write_u8(out, (uint8_t)(0x80 | (codepoint & 0x3F))), AWS_ERROR_INVALID_STATE); |
| 609 |
} else if (codepoint <= 0x10FFFF) { |
| 610 |
AWS_RETURN_ERROR_IF( |
| 611 |
aws_byte_buf_write_u8(out, (uint8_t)(0xF0 | (codepoint >> 18))), AWS_ERROR_INVALID_STATE); |
| 612 |
AWS_RETURN_ERROR_IF( |
| 613 |
aws_byte_buf_write_u8(out, (uint8_t)(0x80 | ((codepoint >> 12) & 0x3F))), AWS_ERROR_INVALID_STATE); |
| 614 |
AWS_RETURN_ERROR_IF( |
| 615 |
aws_byte_buf_write_u8(out, (uint8_t)(0x80 | ((codepoint >> 6) & 0x3F))), AWS_ERROR_INVALID_STATE); |
| 616 |
AWS_RETURN_ERROR_IF( |
| 617 |
aws_byte_buf_write_u8(out, (uint8_t)(0x80 | (codepoint & 0x3F))), AWS_ERROR_INVALID_STATE); |
| 618 |
} else { |
| 619 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 620 |
} |
| 621 |
} else { |
| 622 |
return aws_raise_error(AWS_ERROR_INVALID_XML); |
| 623 |
} |
| 624 |
|
| 625 |
AWS_RETURN_ERROR_IF(aws_byte_buf_write_from_whole_cursor(out, substr), AWS_ERROR_INVALID_STATE); |
| 626 |
} |
| 627 |
|
| 628 |
return AWS_OP_SUCCESS; |
| 629 |
} |
| 630 |
|
| 631 |
int aws_byte_buf_append_unescaped_xml( |
| 632 |
struct aws_allocator *allocator, |
| 633 |
struct aws_byte_cursor data, |
| 634 |
struct aws_byte_buf *out) { |
| 635 |
|
| 636 |
struct aws_byte_buf temp; |
| 637 |
aws_byte_buf_init(&temp, allocator, data.len); |
| 638 |
|
| 639 |
if (s_build_unescaped_buffer(data, &temp)) { |
| 640 |
aws_byte_buf_clean_up(&temp); |
| 641 |
return AWS_OP_ERR; |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
if (aws_byte_buf_reserve_relative(out, temp.len)) { |
| 648 |
aws_byte_buf_clean_up(&temp); |
| 649 |
return AWS_OP_ERR; |
| 650 |
} |
| 651 |
|
| 652 |
struct aws_byte_cursor temp_cur = aws_byte_cursor_from_buf(&temp); |
| 653 |
if (aws_byte_buf_append(out, &temp_cur)) { |
| 654 |
aws_byte_buf_clean_up(&temp); |
| 655 |
return AWS_OP_ERR; |
| 656 |
} |
| 657 |
|
| 658 |
aws_byte_buf_clean_up(&temp); |
| 659 |
return AWS_OP_SUCCESS; |
| 660 |
} |
| 661 |
|
| 662 |
int aws_xml_node_as_body_unescaped( |
| 663 |
struct aws_allocator *allocator, |
| 664 |
struct aws_xml_node *node, |
| 665 |
struct aws_byte_buf *out_body) { |
| 666 |
struct aws_byte_cursor body = {0}; |
| 667 |
if (aws_xml_node_as_body(node, &body)) { |
| 668 |
return AWS_OP_ERR; |
| 669 |
} |
| 670 |
|
| 671 |
return aws_byte_buf_append_unescaped_xml(allocator, body, out_body); |
| 672 |
} |