| 1 |
#include "xh_config.h" |
| 2 |
#include "xh_core.h" |
| 3 |
|
| 4 |
static const char DEF_CONTENT_KEY[] = "content"; |
| 5 |
|
| 6 |
static void xh_x2h_parse_chunk(xh_x2h_ctx_t *ctx, xh_char_t **buf, size_t *bytesleft, xh_bool_t terminate); |
| 7 |
|
| 8 |
void |
| 9 |
xh_x2h_destroy_ctx(xh_x2h_ctx_t *ctx) |
| 10 |
{ |
| 11 |
if (ctx->hash != NULL) SvREFCNT_dec(ctx->hash); |
| 12 |
if (ctx->nodes != NULL) free(ctx->nodes); |
| 13 |
if (ctx->tmp != NULL) free(ctx->tmp); |
| 14 |
|
| 15 |
xh_destroy_opts(&ctx->opts); |
| 16 |
} |
| 17 |
|
| 18 |
void |
| 19 |
xh_x2h_init_ctx(xh_x2h_ctx_t *ctx, I32 ax, I32 items) |
| 20 |
{ |
| 21 |
xh_opts_t *opts = NULL; |
| 22 |
xh_int_t nparam = 0; |
| 23 |
|
| 24 |
memset(ctx, 0, sizeof(xh_x2h_ctx_t)); |
| 25 |
|
| 26 |
opts = (xh_opts_t *) xh_get_obj_param(&nparam, ax, items, "XML::Hash::XS"); |
| 27 |
ctx->input = xh_get_str_param(&nparam, ax, items); |
| 28 |
xh_merge_opts(&ctx->opts, opts, nparam, ax, items); |
| 29 |
|
| 30 |
if ((ctx->nodes = malloc(sizeof(xh_x2h_node_t) * ctx->opts.max_depth)) == NULL) { |
| 31 |
croak("Memory allocation error"); |
| 32 |
} |
| 33 |
memset(ctx->nodes, 0, sizeof(xh_x2h_node_t) * ctx->opts.max_depth); |
| 34 |
} |
| 35 |
|
| 36 |
static void |
| 37 |
xh_x2h_init_result(xh_x2h_ctx_t *ctx) |
| 38 |
{ |
| 39 |
if (ctx->opts.filter.enable) { |
| 40 |
ctx->flags |= XH_X2H_FILTER_ENABLED; |
| 41 |
if (ctx->opts.cb == NULL) |
| 42 |
ctx->result = newRV_noinc((SV *) newAV()); |
| 43 |
} |
| 44 |
else { |
| 45 |
ctx->result = newRV_noinc((SV *) newHV()); |
| 46 |
ctx->nodes[0].lval = ctx->lval = &ctx->result; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
static void |
| 51 |
xh_x2h_adjust_pointers(xh_x2h_ctx_t *ctx, xh_char_t *from, xh_char_t *to) |
| 52 |
{ |
| 53 |
intptr_t delta = (intptr_t) to - (intptr_t) from; |
| 54 |
|
| 55 |
if (ctx->node != NULL) ctx->node += delta; |
| 56 |
if (ctx->content != NULL) ctx->content += delta; |
| 57 |
if (ctx->end != NULL) ctx->end += delta; |
| 58 |
if (ctx->end_of_attr_value != NULL) ctx->end_of_attr_value += delta; |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
xh_x2h_stream_init(xh_x2h_stream_t *stream, xh_opts_t *opts, I32 ax, I32 items) |
| 63 |
{ |
| 64 |
xh_x2h_ctx_t *ctx = &stream->ctx; |
| 65 |
|
| 66 |
memset(stream, 0, sizeof(*stream)); |
| 67 |
xh_merge_opts(&ctx->opts, opts, 1, ax, items); |
| 68 |
if (ctx->opts.encoding[0] != '\0' && |
| 69 |
xh_strcasecmp(ctx->opts.encoding, XH_INTERNAL_ENCODING) != 0) |
| 70 |
croak("Incremental parser supports UTF-8 input only"); |
| 71 |
if ((ctx->nodes = malloc(sizeof(xh_x2h_node_t) * ctx->opts.max_depth)) == NULL) |
| 72 |
croak("Memory allocation error"); |
| 73 |
memset(ctx->nodes, 0, sizeof(xh_x2h_node_t) * ctx->opts.max_depth); |
| 74 |
xh_x2h_init_result(ctx); |
| 75 |
} |
| 76 |
|
| 77 |
void |
| 78 |
xh_x2h_stream_destroy(xh_x2h_stream_t *stream) |
| 79 |
{ |
| 80 |
if (stream->ctx.result != NULL) |
| 81 |
SvREFCNT_dec(stream->ctx.result); |
| 82 |
xh_buffer_destroy(&stream->tail); |
| 83 |
xh_x2h_destroy_ctx(&stream->ctx); |
| 84 |
} |
| 85 |
|
| 86 |
void |
| 87 |
xh_x2h_stream_feed(xh_x2h_stream_t *stream, xh_char_t *data, size_t len, xh_bool_t finish) |
| 88 |
{ |
| 89 |
xh_x2h_ctx_t *ctx = &stream->ctx; |
| 90 |
xh_char_t *buf, *preserve, *old_start; |
| 91 |
size_t input_len = len, saved_len; |
| 92 |
|
| 93 |
if (stream->finished) croak("Parser is already finished"); |
| 94 |
if (stream->stopped) croak("Parser is stopped"); |
| 95 |
if (stream->failed) croak("Parser is in failed state"); |
| 96 |
if (stream->busy) croak("Recursive feed on the same parser"); |
| 97 |
stream->busy = TRUE; |
| 98 |
|
| 99 |
preserve = ctx->node != NULL ? ctx->node : ctx->content; |
| 100 |
if (stream->tail.start != NULL) { |
| 101 |
if (preserve == NULL) { |
| 102 |
stream->tail.cur = stream->tail.start; |
| 103 |
} |
| 104 |
else { |
| 105 |
saved_len = stream->tail.cur - preserve; |
| 106 |
if (preserve != stream->tail.start) |
| 107 |
xh_memmove(stream->tail.start, preserve, saved_len); |
| 108 |
xh_x2h_adjust_pointers(ctx, preserve, stream->tail.start); |
| 109 |
stream->tail.cur = stream->tail.start + saved_len; |
| 110 |
} |
| 111 |
old_start = stream->tail.start; |
| 112 |
xh_buffer_grow(&stream->tail, len); |
| 113 |
if (old_start != stream->tail.start) |
| 114 |
xh_x2h_adjust_pointers(ctx, old_start, stream->tail.start); |
| 115 |
buf = stream->tail.cur; |
| 116 |
if (len) memcpy(buf, data, len); |
| 117 |
stream->tail.cur += len; |
| 118 |
} |
| 119 |
else { |
| 120 |
buf = data; |
| 121 |
} |
| 122 |
|
| 123 |
do { |
| 124 |
xh_x2h_parse_chunk(ctx, &buf, &len, finish); |
| 125 |
|
| 126 |
if (ctx->flags & XH_X2H_STOPPED) { |
| 127 |
stream->stopped = TRUE; |
| 128 |
len = 0; |
| 129 |
break; |
| 130 |
} |
| 131 |
|
| 132 |
if (ctx->state == XML_DECL_FOUND && ctx->encoding[0] != '\0' && |
| 133 |
xh_strcasecmp(ctx->encoding, XH_INTERNAL_ENCODING) != 0) |
| 134 |
croak("Incremental parser supports UTF-8 input only"); |
| 135 |
} while (len > 0); |
| 136 |
|
| 137 |
if (stream->stopped) { |
| 138 |
stream->busy = FALSE; |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
if (stream->tail.start == NULL && !finish) { |
| 143 |
preserve = ctx->node != NULL ? ctx->node : ctx->content; |
| 144 |
if (preserve != NULL) { |
| 145 |
saved_len = (data + input_len) - preserve; |
| 146 |
xh_buffer_init(&stream->tail, saved_len); |
| 147 |
memcpy(stream->tail.start, preserve, saved_len); |
| 148 |
stream->tail.cur = stream->tail.start + saved_len; |
| 149 |
xh_x2h_adjust_pointers(ctx, preserve, stream->tail.start); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
stream->busy = FALSE; |
| 154 |
} |
| 155 |
|
| 156 |
SV * |
| 157 |
xh_x2h_stream_finish(xh_x2h_stream_t *stream) |
| 158 |
{ |
| 159 |
xh_x2h_ctx_t *ctx = &stream->ctx; |
| 160 |
HV *hv; |
| 161 |
HE *he; |
| 162 |
SV *result, *root; |
| 163 |
xh_char_t empty = '\0'; |
| 164 |
|
| 165 |
if (stream->finished) croak("Parser is already finished"); |
| 166 |
if (stream->stopped) { |
| 167 |
stream->finished = TRUE; |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
xh_x2h_stream_feed(stream, &empty, 0, TRUE); |
| 172 |
if (ctx->state != PARSER_ST_DONE) |
| 173 |
croak("Invalid XML"); |
| 174 |
stream->finished = TRUE; |
| 175 |
|
| 176 |
result = ctx->result; |
| 177 |
ctx->result = NULL; |
| 178 |
if (ctx->opts.filter.enable) { |
| 179 |
if (ctx->opts.cb != NULL) { |
| 180 |
SvREFCNT_dec(result); |
| 181 |
result = NULL; |
| 182 |
} |
| 183 |
} |
| 184 |
else if (!ctx->opts.keep_root) { |
| 185 |
root = result; |
| 186 |
hv = (HV *) SvRV(root); |
| 187 |
hv_iterinit(hv); |
| 188 |
if ((he = hv_iternext(hv))) { |
| 189 |
result = hv_iterval(hv, he); |
| 190 |
SvREFCNT_inc(result); |
| 191 |
} |
| 192 |
else { |
| 193 |
result = NULL; |
| 194 |
} |
| 195 |
SvREFCNT_dec(root); |
| 196 |
} |
| 197 |
return result; |
| 198 |
} |
| 199 |
|
| 200 |
XH_INLINE void |
| 201 |
xh_x2h_xpath_update(xh_char_t *xpath, xh_char_t *name, size_t name_len) |
| 202 |
{ |
| 203 |
size_t len; |
| 204 |
|
| 205 |
len = xh_strlen(xpath); |
| 206 |
if (name != NULL) { |
| 207 |
if ((len + name_len + 1) > XH_X2H_XPATH_MAX_LEN) |
| 208 |
croak("XPath too long"); |
| 209 |
|
| 210 |
xpath[len++] = '/'; |
| 211 |
for (;name_len--;) xpath[len++] = *name++; |
| 212 |
} |
| 213 |
else if (len == 0) { |
| 214 |
croak("Can't update xpath, something wrong!"); |
| 215 |
} |
| 216 |
else { |
| 217 |
for (;--len && xpath[len] != '/';) {/* void */} |
| 218 |
} |
| 219 |
xpath[len] = '\0'; |
| 220 |
|
| 221 |
xh_log_trace1("xpath: [%s]", xpath); |
| 222 |
} |
| 223 |
|
| 224 |
XH_INLINE xh_bool_t |
| 225 |
xh_x2h_match_node(xh_char_t *name, size_t name_len, SV *expr) |
| 226 |
{ |
| 227 |
SSize_t i, l; |
| 228 |
AV *av; |
| 229 |
SV *fake_str; |
| 230 |
xh_char_t *expr_str; |
| 231 |
STRLEN expr_len; |
| 232 |
REGEXP *re; |
| 233 |
xh_bool_t matched; |
| 234 |
|
| 235 |
xh_log_trace2("match node: [%.*s]", name_len, name); |
| 236 |
|
| 237 |
fake_str = newSV(0); |
| 238 |
matched = TRUE; |
| 239 |
|
| 240 |
if ( SvRXOK(expr) ) { |
| 241 |
re = (REGEXP *) SvRX(expr); |
| 242 |
if (re != NULL && pregexec(re, (char *) name, (char *) (name + name_len), |
| 243 |
(char *) name, name_len, fake_str, 0) |
| 244 |
) { |
| 245 |
goto MATCHED; |
| 246 |
} |
| 247 |
} |
| 248 |
else if ( SvROK(expr) && SvTYPE(SvRV(expr)) == SVt_PVAV ) { |
| 249 |
av = (AV *) SvRV(expr); |
| 250 |
l = av_len(av); |
| 251 |
for(i = 0; i <= l; i++) { |
| 252 |
expr = *av_fetch(av, i, 0); |
| 253 |
if ( SvRXOK(expr) ) { |
| 254 |
re = (REGEXP *) SvRX(expr); |
| 255 |
if (re != NULL && pregexec(re, (char *) name, (char *) (name + name_len), |
| 256 |
(char *) name, name_len, fake_str, 0) |
| 257 |
) { |
| 258 |
goto MATCHED; |
| 259 |
} |
| 260 |
} |
| 261 |
else { |
| 262 |
expr_str = (xh_char_t *) SvPVutf8(expr, expr_len); |
| 263 |
if (name_len == expr_len && !xh_strncmp(name, expr_str, name_len)) { |
| 264 |
goto MATCHED; |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
} else { |
| 269 |
expr_str = (xh_char_t *) SvPVutf8(expr, expr_len); |
| 270 |
if (name_len == expr_len && !xh_strncmp(name, expr_str, name_len)) { |
| 271 |
goto MATCHED; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
matched = FALSE; |
| 276 |
|
| 277 |
MATCHED: |
| 278 |
SvREFCNT_dec(fake_str); |
| 279 |
|
| 280 |
return matched; |
| 281 |
} |
| 282 |
|
| 283 |
XH_INLINE xh_int_t |
| 284 |
xh_x2h_callback_action(SV *result) |
| 285 |
{ |
| 286 |
xh_char_t *value; |
| 287 |
STRLEN len; |
| 288 |
|
| 289 |
if (!SvOK(result)) return XH_CB_CONTINUE; |
| 290 |
value = XH_CHAR_CAST SvPV(result, len); |
| 291 |
if (len == 16 && xh_strncmp(value, XH_CHAR_CAST "XML_HASH_XS_STOP", 16) == 0) |
| 292 |
return XH_CB_STOP; |
| 293 |
if (len == 16 && xh_strncmp(value, XH_CHAR_CAST "XML_HASH_XS_SKIP", 16) == 0) |
| 294 |
return XH_CB_SKIP; |
| 295 |
return XH_CB_CONTINUE; |
| 296 |
} |
| 297 |
|
| 298 |
XH_INLINE xh_int_t |
| 299 |
xh_x2h_pass_matched_node(SV *cb, SV *val) |
| 300 |
{ |
| 301 |
xh_int_t action = XH_CB_CONTINUE; |
| 302 |
int count; |
| 303 |
dSP; |
| 304 |
|
| 305 |
ENTER; SAVETMPS; |
| 306 |
PUSHMARK(SP); |
| 307 |
XPUSHs(val); |
| 308 |
PUTBACK; |
| 309 |
|
| 310 |
count = call_sv(cb, G_SCALAR); |
| 311 |
|
| 312 |
SPAGAIN; |
| 313 |
if (count > 0) { |
| 314 |
SV *result = POPs; |
| 315 |
action = xh_x2h_callback_action(result); |
| 316 |
} |
| 317 |
PUTBACK; |
| 318 |
|
| 319 |
FREETMPS; |
| 320 |
LEAVE; |
| 321 |
|
| 322 |
return action; |
| 323 |
} |
| 324 |
|
| 325 |
static xh_int_t |
| 326 |
xh_x2h_pass_event(xh_x2h_ctx_t *ctx, const char *event, SV *val, unsigned int event_depth) |
| 327 |
{ |
| 328 |
xh_int_t action = XH_CB_CONTINUE; |
| 329 |
const char *name; |
| 330 |
HV *meta, *attrs; |
| 331 |
HE *he; |
| 332 |
SV *meta_ref; |
| 333 |
int count; |
| 334 |
dSP; |
| 335 |
|
| 336 |
meta = newHV(); |
| 337 |
name = strrchr((const char *) ctx->xpath, '/'); |
| 338 |
name = name == NULL ? (const char *) ctx->xpath : name + 1; |
| 339 |
(void) hv_store(meta, "name", 4, newSVpv(name, 0), 0); |
| 340 |
(void) hv_store(meta, "path", 4, newSVpv((const char *) ctx->xpath, 0), 0); |
| 341 |
(void) hv_store(meta, "depth", 5, newSVuv(event_depth), 0); |
| 342 |
|
| 343 |
if (event[0] == 's') { |
| 344 |
attrs = newHV(); |
| 345 |
if (val != NULL && SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) { |
| 346 |
hv_iterinit((HV *) SvRV(val)); |
| 347 |
while ((he = hv_iternext((HV *) SvRV(val))) != NULL) { |
| 348 |
(void) hv_store_ent(attrs, hv_iterkeysv(he), |
| 349 |
newSVsv(hv_iterval((HV *) SvRV(val), he)), 0); |
| 350 |
} |
| 351 |
} |
| 352 |
(void) hv_store(meta, "attributes", 10, newRV_noinc((SV *) attrs), 0); |
| 353 |
} |
| 354 |
|
| 355 |
ENTER; SAVETMPS; |
| 356 |
meta_ref = sv_2mortal(newRV_noinc((SV *) meta)); |
| 357 |
PUSHMARK(SP); |
| 358 |
XPUSHs(sv_2mortal(newSVpv(event, 0))); |
| 359 |
XPUSHs(val == NULL ? &PL_sv_undef : val); |
| 360 |
XPUSHs(meta_ref); |
| 361 |
PUTBACK; |
| 362 |
|
| 363 |
count = call_sv(ctx->opts.cb, G_SCALAR); |
| 364 |
|
| 365 |
SPAGAIN; |
| 366 |
if (count > 0) { |
| 367 |
SV *result = POPs; |
| 368 |
action = xh_x2h_callback_action(result); |
| 369 |
} |
| 370 |
PUTBACK; |
| 371 |
|
| 372 |
FREETMPS; |
| 373 |
LEAVE; |
| 374 |
|
| 375 |
return action; |
| 376 |
} |
| 377 |
|
| 378 |
#define NEW_STRING(s, l, f) \ |
| 379 |
( \ |
| 380 |
!((f) & XH_X2H_IS_NOT_BLANK) && ctx->opts.suppress_empty == XH_SUPPRESS_EMPTY_TO_UNDEF\ |
| 381 |
? newSV(0) \ |
| 382 |
: !((f) & XH_X2H_IS_NOT_BLANK) && ctx->opts.suppress_empty == XH_SUPPRESS_EMPTY_TO_STRING\ |
| 383 |
? newSVpvn_utf8("", 0, ctx->opts.utf8) \ |
| 384 |
: newSVpvn_utf8((const char *) (s), (l), ctx->opts.utf8)\ |
| 385 |
) |
| 386 |
|
| 387 |
#define SET_STRING(v, s, l, f) \ |
| 388 |
if (!((f) & XH_X2H_IS_NOT_BLANK) && ctx->opts.suppress_empty == XH_SUPPRESS_EMPTY_TO_UNDEF) {\ |
| 389 |
sv_setsv((v), &PL_sv_undef); \ |
| 390 |
} \ |
| 391 |
else if (!((f) & XH_X2H_IS_NOT_BLANK) && ctx->opts.suppress_empty == XH_SUPPRESS_EMPTY_TO_STRING) {\ |
| 392 |
sv_setpvn((v), "", 0); \ |
| 393 |
if (ctx->opts.utf8) SvUTF8_on(v); \ |
| 394 |
} \ |
| 395 |
else { \ |
| 396 |
sv_setpvn((v), (const char *) (s), (l)); \ |
| 397 |
if (ctx->opts.utf8) SvUTF8_on(v); \ |
| 398 |
} |
| 399 |
|
| 400 |
#define CAT_STRING(v, s, l, f) \ |
| 401 |
if (((f) & XH_X2H_IS_NOT_BLANK) || ctx->opts.suppress_empty == XH_SUPPRESS_EMPTY_NONE) {\ |
| 402 |
if ( SvOK(v) ) { \ |
| 403 |
sv_catpvn((v), (const char *) (s), (l)); \ |
| 404 |
} \ |
| 405 |
else { \ |
| 406 |
sv_setpvn((v), (const char *) (s), (l)); \ |
| 407 |
} \ |
| 408 |
if (ctx->opts.utf8) SvUTF8_on(v); \ |
| 409 |
} |
| 410 |
|
| 411 |
#define SAVE_VALUE(lv, v , s, l, f) \ |
| 412 |
xh_log_trace2("save value: [%.*s]", l, s); \ |
| 413 |
if ( ((f) & XH_X2H_TAG_EXISTS) || SvOK(v) ) { \ |
| 414 |
xh_log_trace0("add to array"); \ |
| 415 |
/* get array if value is reference to array */ \ |
| 416 |
if ( SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) { \ |
| 417 |
av = (AV *) SvRV(v); \ |
| 418 |
} \ |
| 419 |
/* create a new array and move value to array */ \ |
| 420 |
else { \ |
| 421 |
av = newAV(); \ |
| 422 |
*(lv) = newRV_noinc((SV *) av); \ |
| 423 |
av_store(av, 0, v); \ |
| 424 |
(v) = *(lv); \ |
| 425 |
} \ |
| 426 |
/* add value to array */ \ |
| 427 |
(lv) = av_store(av, av_len(av) + 1, NEW_STRING((s), (l), f)); \ |
| 428 |
} \ |
| 429 |
else { \ |
| 430 |
xh_log_trace0("set string"); \ |
| 431 |
SET_STRING((v), (s), (l), f); \ |
| 432 |
} \ |
| 433 |
|
| 434 |
#define _OPEN_TAG(s, l) \ |
| 435 |
val = *lval; \ |
| 436 |
/* if content exists that move to hash with 'content' key */ \ |
| 437 |
if ( !SvROK(val) || SvTYPE(SvRV(val)) == SVt_PVAV ) { \ |
| 438 |
*lval = newRV_noinc((SV *) newHV()); \ |
| 439 |
if (SvROK(val) || (SvOK(val) && SvCUR(val))) { \ |
| 440 |
(void) hv_store((HV *) SvRV(*lval), (const char *) content_key, (I32) content_key_len, val, 0);\ |
| 441 |
} \ |
| 442 |
else { \ |
| 443 |
SvREFCNT_dec(val); \ |
| 444 |
} \ |
| 445 |
val = *lval; \ |
| 446 |
} \ |
| 447 |
extra_flags = 0; \ |
| 448 |
if (ctx->opts.suppress_empty == XH_SUPPRESS_EMPTY_TO_UNDEF && \ |
| 449 |
hv_exists((HV *) SvRV(val), (const char *) (s), ctx->opts.utf8 ? -(l) : (l))) {\ |
| 450 |
extra_flags = XH_X2H_TAG_EXISTS; \ |
| 451 |
} \ |
| 452 |
/* fetch existen or create empty hash entry */ \ |
| 453 |
lval = hv_fetch((HV *) SvRV(val), (const char *) (s), ctx->opts.utf8 ? -(l) : (l), 1);\ |
| 454 |
/* save as empty string */ \ |
| 455 |
val = *lval; \ |
| 456 |
SAVE_VALUE(lval, val, "", 0, extra_flags) \ |
| 457 |
if (++depth >= ctx->opts.max_depth) goto MAX_DEPTH_EXCEEDED; \ |
| 458 |
nodes[depth].lval = lval; \ |
| 459 |
nodes[depth].flags = XH_X2H_NODE_FLAG_NONE; \ |
| 460 |
if (depth > 1 && ctx->opts.force_array.enable && (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) \ |
| 461 |
&& (ctx->opts.force_array.always || xh_x2h_match_node(s, l, ctx->opts.force_array.expr))\ |
| 462 |
) { \ |
| 463 |
nodes[depth].flags |= XH_X2H_NODE_FLAG_FORCE_ARRAY; \ |
| 464 |
} \ |
| 465 |
(s) = NULL; |
| 466 |
|
| 467 |
#define OPEN_TAG(s, l) \ |
| 468 |
xh_log_trace2("new tag: [%.*s]", l, s); \ |
| 469 |
flags |= XH_X2H_TEXT_NODE; \ |
| 470 |
if (real_depth == 0) { \ |
| 471 |
if (flags & XH_X2H_ROOT_FOUND) goto INVALID_XML; \ |
| 472 |
flags |= XH_X2H_ROOT_FOUND; \ |
| 473 |
} \ |
| 474 |
if (flags & XH_X2H_SKIP_SUBTREE) { \ |
| 475 |
(s) = NULL; \ |
| 476 |
} \ |
| 477 |
else if (XH_X2H_FILTER_SEARCH(flags)) { \ |
| 478 |
xh_x2h_xpath_update(ctx->xpath, s, l); \ |
| 479 |
if (xh_x2h_match_node(ctx->xpath, xh_strlen(ctx->xpath), ctx->opts.filter.expr)) {\ |
| 480 |
xh_log_trace2("match node: [%.*s]", l, s); \ |
| 481 |
ctx->hash = newRV_noinc((SV *) newHV()); \ |
| 482 |
nodes[0].lval = lval = &ctx->hash; \ |
| 483 |
depth = 0; \ |
| 484 |
flags |= XH_X2H_FILTER_MATCHED; \ |
| 485 |
} \ |
| 486 |
} \ |
| 487 |
if (!XH_X2H_FILTER_SEARCH(flags)) { \ |
| 488 |
_OPEN_TAG(s, l) \ |
| 489 |
} \ |
| 490 |
real_depth++; |
| 491 |
|
| 492 |
#define EVENT_START \ |
| 493 |
if (ctx->opts.cb_mode == XH_CB_MODE_EVENTS && \ |
| 494 |
ctx->opts.cb != NULL && \ |
| 495 |
(flags & XH_X2H_FILTER_MATCHED) && depth == 1 \ |
| 496 |
) { \ |
| 497 |
val = *nodes[depth].lval; \ |
| 498 |
code = xh_x2h_pass_event(ctx, "start", val, real_depth); \ |
| 499 |
if (code == XH_CB_STOP || code == XH_CB_SKIP) { \ |
| 500 |
SvREFCNT_dec(ctx->hash); \ |
| 501 |
ctx->hash = NULL; \ |
| 502 |
nodes[0].lval = lval = &ctx->hash; \ |
| 503 |
depth = 0; \ |
| 504 |
flags ^= XH_X2H_FILTER_MATCHED; \ |
| 505 |
if (code == XH_CB_STOP) goto PARSER_STOP; \ |
| 506 |
flags |= XH_X2H_SKIP_SUBTREE; \ |
| 507 |
skip_depth = real_depth; \ |
| 508 |
} \ |
| 509 |
} |
| 510 |
|
| 511 |
#define _CLOSE_TAG \ |
| 512 |
val = *nodes[depth].lval; \ |
| 513 |
if (ctx->opts.force_content && !SvROK(val)) { \ |
| 514 |
lval = nodes[depth].lval; \ |
| 515 |
*lval = newRV_noinc((SV *) newHV()); \ |
| 516 |
(void) hv_store((HV *) SvRV(*lval), (const char *) content_key, (I32) content_key_len, val, 0);\ |
| 517 |
val = *lval; \ |
| 518 |
} \ |
| 519 |
if ((nodes[depth].flags & XH_X2H_NODE_FLAG_FORCE_ARRAY) \ |
| 520 |
&& (!SvROK(val) || SvTYPE(SvRV(val)) != SVt_PVAV) \ |
| 521 |
) { \ |
| 522 |
lval = nodes[depth].lval; \ |
| 523 |
av = newAV(); \ |
| 524 |
*lval = newRV_noinc((SV *) av); \ |
| 525 |
av_store(av, 0, val); \ |
| 526 |
} \ |
| 527 |
lval = nodes[--depth].lval; |
| 528 |
|
| 529 |
#define CLOSE_TAG \ |
| 530 |
xh_log_trace0("close tag"); \ |
| 531 |
flags &= ~XH_X2H_TEXT_NODE; \ |
| 532 |
if (real_depth == 0) goto INVALID_XML; \ |
| 533 |
if (flags & XH_X2H_SKIP_SUBTREE) { \ |
| 534 |
if (real_depth == skip_depth) { \ |
| 535 |
flags ^= XH_X2H_SKIP_SUBTREE; \ |
| 536 |
} \ |
| 537 |
} \ |
| 538 |
else if (!XH_X2H_FILTER_SEARCH(flags)) { \ |
| 539 |
_CLOSE_TAG \ |
| 540 |
} \ |
| 541 |
if (!(flags & XH_X2H_SKIP_SUBTREE) && \ |
| 542 |
(flags & XH_X2H_FILTER_MATCHED) && depth == 0 \ |
| 543 |
) { \ |
| 544 |
xh_log_trace0("match node finished"); \ |
| 545 |
val = *nodes[0].lval; \ |
| 546 |
if (!ctx->opts.keep_root) { \ |
| 547 |
val = SvRV(val); \ |
| 548 |
hv_iterinit((HV *) val); \ |
| 549 |
val = hv_iterval((HV *) val, hv_iternext((HV *) val)); \ |
| 550 |
SvREFCNT_inc(val); \ |
| 551 |
SvREFCNT_dec(*nodes[0].lval); \ |
| 552 |
ctx->hash = NULL; \ |
| 553 |
} \ |
| 554 |
if (ctx->opts.cb == NULL) { \ |
| 555 |
av_push((AV *) SvRV(ctx->result), val); \ |
| 556 |
ctx->hash = NULL; \ |
| 557 |
} \ |
| 558 |
else { \ |
| 559 |
code = ctx->opts.cb_mode == XH_CB_MODE_EVENTS \ |
| 560 |
? xh_x2h_pass_event(ctx, "end", val, real_depth) \ |
| 561 |
: xh_x2h_pass_matched_node(ctx->opts.cb, val); \ |
| 562 |
SvREFCNT_dec(val); \ |
| 563 |
ctx->hash = NULL; \ |
| 564 |
if (code == XH_CB_SKIP) croak("SKIP is valid only for start events");\ |
| 565 |
if (code == XH_CB_STOP) goto PARSER_STOP; \ |
| 566 |
} \ |
| 567 |
flags ^= XH_X2H_FILTER_MATCHED; \ |
| 568 |
} \ |
| 569 |
if (!(flags & XH_X2H_SKIP_SUBTREE) && \ |
| 570 |
(flags & (XH_X2H_FILTER_ENABLED | XH_X2H_FILTER_MATCHED)) == XH_X2H_FILTER_ENABLED\ |
| 571 |
) { \ |
| 572 |
xh_x2h_xpath_update(ctx->xpath, NULL, 0); \ |
| 573 |
} \ |
| 574 |
real_depth--; |
| 575 |
|
| 576 |
#define NEW_NODE_ATTRIBUTE(k, kl, v, vl) \ |
| 577 |
if (!XH_X2H_FILTER_SEARCH(flags)) { \ |
| 578 |
_OPEN_TAG(k, kl) \ |
| 579 |
_NEW_TEXT(v, vl) \ |
| 580 |
_CLOSE_TAG \ |
| 581 |
} |
| 582 |
|
| 583 |
#define _NEW_NODE_ATTRIBUTE(k, kl, v, vl) \ |
| 584 |
xh_log_trace4("new attr name: [%.*s] value: [%.*s]", kl, k, vl, v); \ |
| 585 |
/* create hash if not created already */ \ |
| 586 |
if ( !SvROK(*lval) ) { \ |
| 587 |
/* destroy empty old scalar (empty string) */ \ |
| 588 |
SvREFCNT_dec(*lval); \ |
| 589 |
*lval = newRV_noinc((SV *) newHV()); \ |
| 590 |
} \ |
| 591 |
/* save key/value */ \ |
| 592 |
(void) hv_store((HV *) SvRV(*lval), (const char *) (k), ctx->opts.utf8 ? -(kl) : (kl),\ |
| 593 |
NEW_STRING(v, vl, flags), 0); \ |
| 594 |
(k) = (v) = NULL; |
| 595 |
|
| 596 |
#define NEW_XML_DECL_ATTRIBUTE(k, kl, v, vl) \ |
| 597 |
xh_log_trace4("new xml decl attr name: [%.*s] value: [%.*s]", kl, k, vl, v);\ |
| 598 |
/* save encoding parameter to converter context if param found */ \ |
| 599 |
if ((kl) == (sizeof("encoding") - 1) && \ |
| 600 |
xh_strncmp((k), XH_CHAR_CAST "encoding", sizeof("encoding") - 1) == 0) {\ |
| 601 |
xh_str_range_copy(ctx->encoding, XH_CHAR_CAST (v), vl, XH_PARAM_LEN);\ |
| 602 |
} \ |
| 603 |
(k) = (v) = NULL; |
| 604 |
|
| 605 |
#define NEW_PI_ATTRIBUTE(k, kl, v, vl) \ |
| 606 |
xh_log_trace4("new PI attr name: [%.*s] value: [%.*s]", kl, k, vl, v); |
| 607 |
|
| 608 |
#define NEW_ATTRIBUTE(k, kl, v, vl) NEW_NODE_ATTRIBUTE(k, kl, v, vl) |
| 609 |
|
| 610 |
#define _NEW_TEXT(s, l) \ |
| 611 |
val = *lval; \ |
| 612 |
if ( SvROK(val) ) { \ |
| 613 |
xh_log_trace0("add to array"); \ |
| 614 |
/* add content to array*/ \ |
| 615 |
if (SvTYPE(SvRV(val)) == SVt_PVAV) { \ |
| 616 |
av = (AV *) SvRV(val); \ |
| 617 |
av_store(av, av_len(av) + 1, NEW_STRING(s, l, flags)); \ |
| 618 |
} \ |
| 619 |
/* save content to hash with "content" key */ \ |
| 620 |
else { \ |
| 621 |
extra_flags = 0; \ |
| 622 |
if (ctx->opts.suppress_empty == XH_SUPPRESS_EMPTY_TO_UNDEF &&\ |
| 623 |
hv_exists((HV *) SvRV(val), (const char *) content_key, (I32) content_key_len)) {\ |
| 624 |
extra_flags = XH_X2H_TAG_EXISTS; \ |
| 625 |
} \ |
| 626 |
xh_log_trace0("save to hash"); \ |
| 627 |
lval = hv_fetch((HV *) SvRV(val), (const char *) content_key, (I32) content_key_len, 1);\ |
| 628 |
val = *lval; \ |
| 629 |
SAVE_VALUE(lval, val, s, l, flags | extra_flags) \ |
| 630 |
lval = nodes[depth].lval; \ |
| 631 |
} \ |
| 632 |
} \ |
| 633 |
else if (SvOK(val) && SvCUR(val) && !ctx->opts.merge_text) { \ |
| 634 |
xh_log_trace0("create a new array"); \ |
| 635 |
xh_log_trace1("create a new array val: %s", SvPV_nolen(val)); \ |
| 636 |
xh_log_trace3("create a new array svrok: %d type: %d rtype: %d", SvROK(val), SvTYPE(val), SvTYPE(SvRV(val)));\ |
| 637 |
/* content already exists, create a new array and move*/ \ |
| 638 |
/* old and new content to array */ \ |
| 639 |
av = newAV(); \ |
| 640 |
*lval = newRV_noinc((SV *) av); \ |
| 641 |
av_store(av, 0, val); \ |
| 642 |
av_store(av, av_len(av) + 1, NEW_STRING(s, l, flags)); \ |
| 643 |
} \ |
| 644 |
else { \ |
| 645 |
xh_log_trace0("concat"); \ |
| 646 |
/* concatenate with previous string */ \ |
| 647 |
CAT_STRING(val, s, l, flags) \ |
| 648 |
} \ |
| 649 |
|
| 650 |
#define NEW_TEXT(s, l) \ |
| 651 |
xh_log_trace2("new text: [%.*s]", l, s); \ |
| 652 |
if (real_depth == 0) goto INVALID_XML; \ |
| 653 |
if (!XH_X2H_FILTER_SEARCH(flags)) { \ |
| 654 |
_NEW_TEXT(s, l) \ |
| 655 |
} |
| 656 |
|
| 657 |
#define NEW_COMMENT(s, l) (s) = NULL; |
| 658 |
|
| 659 |
#define NEW_CDATA(s, l) NEW_TEXT(s, l) |
| 660 |
|
| 661 |
#define CHECK_EOF_WITH_CHUNK(loop) \ |
| 662 |
if (cur >= eof || *cur == '\0') { \ |
| 663 |
eof = cur; \ |
| 664 |
if (terminate) goto PPCAT(loop, _FINISH); \ |
| 665 |
ctx->state = PPCAT(loop, _START); \ |
| 666 |
goto CHUNK_FINISH; \ |
| 667 |
} \ |
| 668 |
|
| 669 |
#define CHECK_EOF_WITHOUT_CHUNK(loop) \ |
| 670 |
if (cur >= eof || *cur == '\0') goto PPCAT(loop, _FINISH); \ |
| 671 |
|
| 672 |
#define CHECK_EOF(loop) CHECK_EOF_WITH_CHUNK(loop) |
| 673 |
|
| 674 |
#define DO(loop) \ |
| 675 |
PPCAT(loop, _START): \ |
| 676 |
CHECK_EOF(loop) \ |
| 677 |
c = *cur++; \ |
| 678 |
xh_log_trace3("'%c'=[0x%X] %s start", c, c, STRINGIZE(loop)); \ |
| 679 |
switch (c) { |
| 680 |
|
| 681 |
#define _DO(loop) \ |
| 682 |
PPCAT(loop, _START): \ |
| 683 |
CHECK_EOF_WITHOUT_CHUNK(loop) \ |
| 684 |
c = *cur++; \ |
| 685 |
xh_log_trace3("'%c'=[0x%X] %s start", c, c, STRINGIZE(loop)); \ |
| 686 |
switch (c) { |
| 687 |
|
| 688 |
#define END(loop) \ |
| 689 |
} \ |
| 690 |
xh_log_trace1(" %s end", STRINGIZE(loop)); \ |
| 691 |
goto PPCAT(loop, _START); \ |
| 692 |
PPCAT(loop, _FINISH): |
| 693 |
|
| 694 |
#define EXPECT_ANY(desc) \ |
| 695 |
default: xh_log_trace3("'%c'=[0x%X] - %s expected", c, c, desc); |
| 696 |
|
| 697 |
#define EXPECT_CHAR(desc, c1) \ |
| 698 |
case c1: xh_log_trace3("'%c'=[0x%X] - %s expected", c, c, desc); |
| 699 |
|
| 700 |
#define EXPECT_BLANK_WO_CR(desc) \ |
| 701 |
case ' ': case '\t': case '\n': \ |
| 702 |
xh_log_trace3("'%c'=[0x%X] - %s expected", c, c, desc); |
| 703 |
|
| 704 |
#define EXPECT_BLANK(desc) \ |
| 705 |
case ' ': case '\t': case '\n': case '\r': \ |
| 706 |
xh_log_trace3("'%c'=[0x%X] - %s expected", c, c, desc); |
| 707 |
|
| 708 |
#define EXPECT_DIGIT(desc) \ |
| 709 |
case '0': case '1': case '2': case '3': case '4': \ |
| 710 |
case '5': case '6': case '7': case '8': case '9': \ |
| 711 |
xh_log_trace3("'%c'=[0x%X] - %s expected", c, c, desc); |
| 712 |
|
| 713 |
#define EXPECT_HEX_CHAR_LC(desc) \ |
| 714 |
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': \ |
| 715 |
xh_log_trace3("'%c'=[0x%X] - %s expected", c, c, desc); |
| 716 |
|
| 717 |
#define EXPECT_HEX_CHAR_UC(desc) \ |
| 718 |
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': \ |
| 719 |
xh_log_trace3("'%c'=[0x%X] - %s expected", c, c, desc); |
| 720 |
|
| 721 |
#define SKIP_BLANK \ |
| 722 |
EXPECT_BLANK("skip blank") break; |
| 723 |
|
| 724 |
#define SCAN2(loop, c1, c2) \ |
| 725 |
DO(PPCAT(loop, _1)) EXPECT_CHAR(STRINGIZE(c1), c1) \ |
| 726 |
DO(PPCAT(loop, _2)) EXPECT_CHAR(STRINGIZE(c2), c2) |
| 727 |
|
| 728 |
#define END2(loop, stop) \ |
| 729 |
EXPECT_ANY("wrong character") goto stop; \ |
| 730 |
END(PPCAT(loop, _2)) goto stop; \ |
| 731 |
EXPECT_ANY("wrong character") goto stop; \ |
| 732 |
END(PPCAT(loop, _1)) |
| 733 |
|
| 734 |
#define SCAN3(loop, c1, c2, c3) \ |
| 735 |
DO(PPCAT(loop, _1)) EXPECT_CHAR(STRINGIZE(c1), c1) \ |
| 736 |
DO(PPCAT(loop, _2)) EXPECT_CHAR(STRINGIZE(c2), c2) \ |
| 737 |
DO(PPCAT(loop, _3)) EXPECT_CHAR(STRINGIZE(c3), c3) |
| 738 |
|
| 739 |
#define END3(loop, stop) \ |
| 740 |
EXPECT_ANY("wrong character") goto stop; \ |
| 741 |
END(PPCAT(loop, _3)) goto stop; \ |
| 742 |
EXPECT_ANY("wrong character") goto stop; \ |
| 743 |
END(PPCAT(loop, _2)) goto stop; \ |
| 744 |
EXPECT_ANY("wrong character") goto stop; \ |
| 745 |
END(PPCAT(loop, _1)) |
| 746 |
|
| 747 |
#define SCAN5(loop, c1, c2, c3, c4, c5) \ |
| 748 |
SCAN3(PPCAT(loop, _1), c1, c2, c3) \ |
| 749 |
SCAN2(PPCAT(loop, _2), c4, c5) |
| 750 |
|
| 751 |
#define END5(loop, stop) \ |
| 752 |
END2(PPCAT(loop, _2), stop) \ |
| 753 |
END3(PPCAT(loop, _1), stop) |
| 754 |
|
| 755 |
#define SCAN6(loop, c1, c2, c3, c4, c5, c6) \ |
| 756 |
SCAN3(PPCAT(loop, _1), c1, c2, c3) \ |
| 757 |
SCAN3(PPCAT(loop, _2), c4, c5, c6) |
| 758 |
|
| 759 |
#define END6(loop, stop) \ |
| 760 |
END3(PPCAT(loop, _2), stop) \ |
| 761 |
END3(PPCAT(loop, _1), stop) |
| 762 |
|
| 763 |
#define SCAN10(loop, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10) \ |
| 764 |
SCAN5(PPCAT(loop, _1), c1, c2, c3, c4, c5) \ |
| 765 |
SCAN5(PPCAT(loop, _2), c6, c7, c8, c9, c10) |
| 766 |
|
| 767 |
#define END10(loop, stop) \ |
| 768 |
END5(PPCAT(loop, _2), stop) \ |
| 769 |
END5(PPCAT(loop, _1), stop) |
| 770 |
|
| 771 |
#define SEARCH_END_TAG \ |
| 772 |
EXPECT_CHAR("end tag", '>') \ |
| 773 |
EVENT_START \ |
| 774 |
goto PARSE_CONTENT; \ |
| 775 |
EXPECT_CHAR("self closing tag", '/') \ |
| 776 |
EVENT_START \ |
| 777 |
CLOSE_TAG \ |
| 778 |
DO(SEARCH_END_TAG) \ |
| 779 |
EXPECT_CHAR("end tag", '>') \ |
| 780 |
goto PARSE_CONTENT; \ |
| 781 |
EXPECT_ANY("wrong character") \ |
| 782 |
goto INVALID_XML; \ |
| 783 |
END(SEARCH_END_TAG) \ |
| 784 |
goto INVALID_XML; |
| 785 |
|
| 786 |
#define SEARCH_NODE_ATTRIBUTE_VALUE(loop, top_loop, quot) \ |
| 787 |
EXPECT_CHAR("start attr value", quot) \ |
| 788 |
content = NULL; \ |
| 789 |
end_of_attr_value = NULL; \ |
| 790 |
flags &= ~(XH_X2H_NEED_NORMALIZE | XH_X2H_IS_NOT_BLANK); \ |
| 791 |
DO(PPCAT(loop, _END_ATTR_VALUE)) \ |
| 792 |
EXPECT_CHAR("attr value end", quot) \ |
| 793 |
if (flags & XH_X2H_NEED_NORMALIZE) { \ |
| 794 |
NORMALIZE_TEXT(loop, content, end_of_attr_value - content)\ |
| 795 |
NEW_ATTRIBUTE(node, end - node, enc, enc_len) \ |
| 796 |
} \ |
| 797 |
else if (content != NULL) { \ |
| 798 |
NEW_ATTRIBUTE(node, end - node, content, end_of_attr_value - content)\ |
| 799 |
} \ |
| 800 |
else { \ |
| 801 |
NEW_ATTRIBUTE(node, end - node, "", 0) \ |
| 802 |
} \ |
| 803 |
goto top_loop; \ |
| 804 |
EXPECT_BLANK_WO_CR("blank") \ |
| 805 |
if (!ctx->opts.trim) \ |
| 806 |
goto PPCAT(loop, _START_ATTR_VALUE); \ |
| 807 |
break; \ |
| 808 |
EXPECT_CHAR("CR", '\r') \ |
| 809 |
if (content != NULL) { \ |
| 810 |
flags |= XH_X2H_NORMALIZE_LINE_FEED; \ |
| 811 |
} \ |
| 812 |
if (!ctx->opts.trim) \ |
| 813 |
goto PPCAT(loop, _START_ATTR_VALUE); \ |
| 814 |
break; \ |
| 815 |
EXPECT_CHAR("reference", '&') \ |
| 816 |
flags |= (XH_X2H_NORMALIZE_REF | XH_X2H_IS_NOT_BLANK); \ |
| 817 |
goto PPCAT(loop, _START_ATTR_VALUE); \ |
| 818 |
EXPECT_ANY("any char") \ |
| 819 |
flags |= XH_X2H_IS_NOT_BLANK; \ |
| 820 |
PPCAT(loop, _START_ATTR_VALUE): \ |
| 821 |
if (content == NULL) content = cur - 1; \ |
| 822 |
end_of_attr_value = cur; \ |
| 823 |
END(PPCAT(loop, _END_ATTR_VALUE)) \ |
| 824 |
goto INVALID_XML; |
| 825 |
|
| 826 |
#define SEARCH_XML_DECL_ATTRIBUTE_VALUE(loop, top_loop, quot) \ |
| 827 |
EXPECT_CHAR("start attr value", quot) \ |
| 828 |
content = cur; \ |
| 829 |
DO(PPCAT(loop, _END_ATTR_VALUE)) \ |
| 830 |
EXPECT_CHAR("attr value end", quot) \ |
| 831 |
NEW_ATTRIBUTE(node, end - node, content, cur - content - 1)\ |
| 832 |
goto top_loop; \ |
| 833 |
END(PPCAT(loop, _END_ATTR_VALUE)) \ |
| 834 |
goto INVALID_XML; |
| 835 |
|
| 836 |
#define SEARCH_ATTRIBUTE_VALUE(loop, top_loop, quot) SEARCH_NODE_ATTRIBUTE_VALUE(loop, top_loop, quot) |
| 837 |
|
| 838 |
#define SEARCH_ATTRIBUTES(loop, search_end_tag) \ |
| 839 |
PPCAT(loop, _SEARCH_ATTRIBUTES_LOOP): \ |
| 840 |
DO(PPCAT(loop, _SEARCH_ATTR)) \ |
| 841 |
search_end_tag \ |
| 842 |
\ |
| 843 |
SKIP_BLANK \ |
| 844 |
\ |
| 845 |
EXPECT_ANY("start attr name") \ |
| 846 |
node = cur - 1; \ |
| 847 |
\ |
| 848 |
DO(PPCAT(loop, _PARSE_ATTR_NAME)) \ |
| 849 |
EXPECT_BLANK("end attr name") \ |
| 850 |
end = cur - 1; \ |
| 851 |
xh_log_trace2("attr name: [%.*s]", end - node, node);\ |
| 852 |
\ |
| 853 |
DO(PPCAT(loop, _ATTR_SKIP_BLANK)) \ |
| 854 |
EXPECT_CHAR("search attr value", '=') \ |
| 855 |
goto PPCAT(loop, _SEARCH_ATTRIBUTE_VALUE); \ |
| 856 |
SKIP_BLANK \ |
| 857 |
EXPECT_ANY("wrong character") \ |
| 858 |
goto INVALID_XML; \ |
| 859 |
END(PPCAT(loop, _ATTR_SKIP_BLANK)) \ |
| 860 |
goto INVALID_XML; \ |
| 861 |
EXPECT_CHAR("end attr name", '=') \ |
| 862 |
end = cur - 1; \ |
| 863 |
xh_log_trace2("attr name: [%.*s]", end - node, node);\ |
| 864 |
\ |
| 865 |
PPCAT(loop, _SEARCH_ATTRIBUTE_VALUE): \ |
| 866 |
DO(PPCAT(loop, _PARSE_ATTR_VALUE)) \ |
| 867 |
SEARCH_ATTRIBUTE_VALUE(PPCAT(loop, _1), PPCAT(loop, _SEARCH_ATTRIBUTES_LOOP), '"')\ |
| 868 |
SEARCH_ATTRIBUTE_VALUE(PPCAT(loop, _2), PPCAT(loop, _SEARCH_ATTRIBUTES_LOOP), '\'')\ |
| 869 |
SKIP_BLANK \ |
| 870 |
EXPECT_ANY("wrong character") \ |
| 871 |
goto INVALID_XML; \ |
| 872 |
END(PPCAT(loop, _PARSE_ATTR_VALUE)) \ |
| 873 |
goto INVALID_XML; \ |
| 874 |
END(PPCAT(loop, _PARSE_ATTR_NAME)) \ |
| 875 |
goto INVALID_XML; \ |
| 876 |
END(PPCAT(loop, _SEARCH_ATTR)) \ |
| 877 |
goto INVALID_XML; |
| 878 |
|
| 879 |
#define SEARCH_END_XML_DECLARATION \ |
| 880 |
EXPECT_CHAR("end tag", '?') \ |
| 881 |
DO(XML_DECL_SEARCH_END_TAG2) \ |
| 882 |
EXPECT_CHAR("end tag", '>') \ |
| 883 |
goto XML_DECL_FOUND; \ |
| 884 |
EXPECT_ANY("wrong character") \ |
| 885 |
goto INVALID_XML; \ |
| 886 |
END(XML_DECL_SEARCH_END_TAG2) \ |
| 887 |
goto INVALID_XML; |
| 888 |
|
| 889 |
#define SEARCH_END_PI \ |
| 890 |
EXPECT_CHAR("end tag", '?') \ |
| 891 |
DO(PI_SEARCH_END_TAG2) \ |
| 892 |
EXPECT_CHAR("end tag", '>') \ |
| 893 |
goto PARSE_CONTENT; \ |
| 894 |
EXPECT_ANY("wrong character") \ |
| 895 |
goto INVALID_XML; \ |
| 896 |
END(PI_SEARCH_END_TAG2) \ |
| 897 |
goto INVALID_XML; |
| 898 |
|
| 899 |
#define PARSE_DOCTYPE_END \ |
| 900 |
goto PARSE_DOCTYPE_INTSUBSET_START; |
| 901 |
|
| 902 |
#define PARSE_DOCTYPE_LITERAL(loop, next, quot) \ |
| 903 |
EXPECT_CHAR("start of literal", quot) \ |
| 904 |
DO(PPCAT(loop, _END_OF_LITERAL)) \ |
| 905 |
EXPECT_CHAR("end of literal", quot) \ |
| 906 |
next \ |
| 907 |
END(PPCAT(loop, _END_OF_LITERAL)) \ |
| 908 |
goto INVALID_XML; |
| 909 |
|
| 910 |
#define PARSE_DOCTYPE_LITERALS(prefix, next) \ |
| 911 |
PARSE_DOCTYPE_LITERAL(PPCAT(prefix, _1), next, '"')\ |
| 912 |
PARSE_DOCTYPE_LITERAL(PPCAT(prefix, _2), next, '\'') |
| 913 |
|
| 914 |
#define PARSE_DOCTYPE_DELIM(prefix, next) \ |
| 915 |
DO(PPCAT(prefix, _DOCTYPE_DELIM)) \ |
| 916 |
EXPECT_BLANK("delimiter") \ |
| 917 |
DO(PPCAT(prefix, _DOCTYPE_DELIM_SKIP_BLANK)) \ |
| 918 |
SKIP_BLANK \ |
| 919 |
next \ |
| 920 |
EXPECT_ANY("wrong character") \ |
| 921 |
goto INVALID_XML; \ |
| 922 |
END(PPCAT(prefix, _DOCTYPE_DELIM_SKIP_BLANK)) \ |
| 923 |
goto INVALID_XML; \ |
| 924 |
EXPECT_ANY("wrong character") \ |
| 925 |
goto INVALID_XML; \ |
| 926 |
END(PPCAT(prefix, _DOCTYPE_DELIM)) \ |
| 927 |
goto INVALID_XML; |
| 928 |
|
| 929 |
#define PARSE_DOCTYPE_SYSTEM \ |
| 930 |
SCAN5(DOCTYPE_SYSTEM, 'Y', 'S', 'T', 'E', 'M') \ |
| 931 |
PARSE_DOCTYPE_DELIM(DOCTYPE_SYSTEM_LOCATION, PARSE_DOCTYPE_LITERALS(DOCTYPE_SYSTEM, PARSE_DOCTYPE_END))\ |
| 932 |
END5(DOCTYPE_SYSTEM, INVALID_XML) \ |
| 933 |
goto INVALID_XML; |
| 934 |
|
| 935 |
#define PARSE_DOCTYPE_PUBLIC_ID(prefix) \ |
| 936 |
PARSE_DOCTYPE_LITERAL( \ |
| 937 |
PPCAT(prefix, _1), \ |
| 938 |
PARSE_DOCTYPE_DELIM(DOCTYPE_PUBLIC_LOCATION_1, PARSE_DOCTYPE_LITERALS(DOCTYPE_PUBLIC_LOCATION_1, PARSE_DOCTYPE_END)),\ |
| 939 |
'"' \ |
| 940 |
) \ |
| 941 |
PARSE_DOCTYPE_LITERAL( \ |
| 942 |
PPCAT(prefix, _2), \ |
| 943 |
PARSE_DOCTYPE_DELIM(DOCTYPE_PUBLIC_LOCATION_2, PARSE_DOCTYPE_LITERALS(DOCTYPE_PUBLIC_LOCATION_2, PARSE_DOCTYPE_END)),\ |
| 944 |
'\'' \ |
| 945 |
) |
| 946 |
|
| 947 |
#define PARSE_DOCTYPE_PUBLIC \ |
| 948 |
SCAN5(DOCTYPE_PUBLIC, 'U', 'B', 'L', 'I', 'C') \ |
| 949 |
PARSE_DOCTYPE_DELIM(DOCTYPE_PUBLIC_ID, PARSE_DOCTYPE_PUBLIC_ID(DOCTYPE_PUBLIC))\ |
| 950 |
END5(DOCTYPE_PUBLIC, INVALID_XML) \ |
| 951 |
goto INVALID_XML; |
| 952 |
|
| 953 |
#define PARSE_DOCTYPE \ |
| 954 |
SCAN6(DOCTYPE, 'O', 'C', 'T', 'Y', 'P', 'E') \ |
| 955 |
if (flags & (XH_X2H_ROOT_FOUND | XH_X2H_DOCTYPE_FOUND)) goto INVALID_XML;\ |
| 956 |
flags |= XH_X2H_DOCTYPE_FOUND; \ |
| 957 |
DO(DOCTYPE_NAME) \ |
| 958 |
EXPECT_BLANK("delimiter") \ |
| 959 |
DO(DOCTYPE_NAME_START) \ |
| 960 |
SKIP_BLANK \ |
| 961 |
EXPECT_ANY("start name") \ |
| 962 |
DO(DOCTYPE_NAME_END) \ |
| 963 |
EXPECT_BLANK("end name") \ |
| 964 |
DO(DOCTYPE_NAME_BLANK) \ |
| 965 |
SKIP_BLANK \ |
| 966 |
EXPECT_CHAR("end doctype", '>') \ |
| 967 |
goto PARSE_CONTENT; \ |
| 968 |
EXPECT_CHAR("SYSTEM", 'S') \ |
| 969 |
PARSE_DOCTYPE_SYSTEM \ |
| 970 |
EXPECT_CHAR("PUBLIC", 'P') \ |
| 971 |
PARSE_DOCTYPE_PUBLIC \ |
| 972 |
EXPECT_CHAR("internal subset", '[') \ |
| 973 |
goto PARSE_DOCTYPE_INTSUBSET; \ |
| 974 |
EXPECT_ANY("wrong character") \ |
| 975 |
goto INVALID_XML; \ |
| 976 |
END(DOCTYPE_NAME_BLANK) \ |
| 977 |
goto INVALID_XML; \ |
| 978 |
EXPECT_CHAR("end doctype", '>') \ |
| 979 |
goto PARSE_CONTENT; \ |
| 980 |
END(DOCTYPE_NAME_END) \ |
| 981 |
goto INVALID_XML; \ |
| 982 |
END(DOCTYPE_NAME_START) \ |
| 983 |
goto INVALID_XML; \ |
| 984 |
EXPECT_ANY("wrong character") \ |
| 985 |
goto INVALID_XML; \ |
| 986 |
END(DOCTYPE_NAME) \ |
| 987 |
goto INVALID_XML; \ |
| 988 |
END6(DOCTYPE, INVALID_XML) \ |
| 989 |
goto INVALID_XML; |
| 990 |
|
| 991 |
#define PARSE_COMMENT \ |
| 992 |
DO(COMMENT1) \ |
| 993 |
EXPECT_CHAR("-", '-') \ |
| 994 |
content = NULL; \ |
| 995 |
DO(END_COMMENT1) \ |
| 996 |
SKIP_BLANK \ |
| 997 |
EXPECT_CHAR("1st -", '-') \ |
| 998 |
if (content == NULL) content = end = cur - 1; \ |
| 999 |
DO(END_COMMENT2) \ |
| 1000 |
EXPECT_CHAR("2nd -", '-') \ |
| 1001 |
DO(END_COMMENT3) \ |
| 1002 |
EXPECT_CHAR(">", '>') \ |
| 1003 |
NEW_COMMENT(content, end - content) \ |
| 1004 |
goto PARSE_CONTENT; \ |
| 1005 |
EXPECT_CHAR("2nd -", '-') \ |
| 1006 |
end = cur - 2; \ |
| 1007 |
goto END_COMMENT3_START; \ |
| 1008 |
EXPECT_ANY("any character") \ |
| 1009 |
end = cur - 1; \ |
| 1010 |
goto END_COMMENT1_START; \ |
| 1011 |
END(END_COMMENT3) \ |
| 1012 |
EXPECT_BLANK("skip blank") \ |
| 1013 |
end = cur - 1; \ |
| 1014 |
goto END_COMMENT1_START; \ |
| 1015 |
EXPECT_ANY("any character") \ |
| 1016 |
end = cur; \ |
| 1017 |
goto END_COMMENT1_START; \ |
| 1018 |
END(END_COMMENT2) \ |
| 1019 |
EXPECT_ANY("any char") \ |
| 1020 |
if (content == NULL) content = cur - 1; \ |
| 1021 |
end = cur; \ |
| 1022 |
END(END_COMMENT1) \ |
| 1023 |
goto INVALID_XML; \ |
| 1024 |
\ |
| 1025 |
EXPECT_ANY("wrong character") \ |
| 1026 |
goto INVALID_XML; \ |
| 1027 |
\ |
| 1028 |
END(COMMENT1) \ |
| 1029 |
goto INVALID_XML; |
| 1030 |
|
| 1031 |
#define PARSE_CDATA \ |
| 1032 |
SCAN6(CDATA, 'C', 'D', 'A', 'T', 'A', '[') \ |
| 1033 |
content = end = cur; \ |
| 1034 |
DO(END_CDATA1) \ |
| 1035 |
EXPECT_CHAR("1st ]", ']') \ |
| 1036 |
DO(END_CDATA2) \ |
| 1037 |
EXPECT_CHAR("2nd ]", ']') \ |
| 1038 |
DO(END_CDATA3) \ |
| 1039 |
EXPECT_CHAR(">", '>') \ |
| 1040 |
end = cur - 3; \ |
| 1041 |
NEW_CDATA(content, end - content) \ |
| 1042 |
goto PARSE_CONTENT; \ |
| 1043 |
EXPECT_CHAR("2nd ]", ']') \ |
| 1044 |
goto END_CDATA3_START; \ |
| 1045 |
EXPECT_ANY("any character") \ |
| 1046 |
goto END_CDATA1_START; \ |
| 1047 |
END(END_CDATA3) \ |
| 1048 |
EXPECT_ANY("any character") \ |
| 1049 |
goto END_CDATA1_START; \ |
| 1050 |
END(END_CDATA2) \ |
| 1051 |
; \ |
| 1052 |
END(END_CDATA1) \ |
| 1053 |
goto INVALID_XML; \ |
| 1054 |
END6(CDATA, INVALID_XML) |
| 1055 |
|
| 1056 |
#define PARSE_CDATA_WITH_TRIM \ |
| 1057 |
SCAN6(CDATA_WITH_TRIM, 'C', 'D', 'A', 'T', 'A', '[') \ |
| 1058 |
content = NULL; \ |
| 1059 |
DO(END_CDATA_WITH_TRIM1) \ |
| 1060 |
SKIP_BLANK \ |
| 1061 |
EXPECT_CHAR("1st ]", ']') \ |
| 1062 |
if (content == NULL) content = end = cur - 1; \ |
| 1063 |
DO(END_CDATA_WITH_TRIM2) \ |
| 1064 |
EXPECT_CHAR("2nd ]", ']') \ |
| 1065 |
DO(END_CDATA_WITH_TRIM3) \ |
| 1066 |
EXPECT_CHAR(">", '>') \ |
| 1067 |
NEW_CDATA(content, end - content) \ |
| 1068 |
goto PARSE_CONTENT; \ |
| 1069 |
EXPECT_CHAR("2nd ]", ']') \ |
| 1070 |
end = cur - 2; \ |
| 1071 |
goto END_CDATA_WITH_TRIM3_START; \ |
| 1072 |
EXPECT_ANY("any character") \ |
| 1073 |
end = cur - 1; \ |
| 1074 |
goto END_CDATA_WITH_TRIM1_START; \ |
| 1075 |
END(END_CDATA_WITH_TRIM3) \ |
| 1076 |
EXPECT_BLANK("skip blank") \ |
| 1077 |
end = cur - 1; \ |
| 1078 |
goto END_CDATA_WITH_TRIM1_START; \ |
| 1079 |
EXPECT_ANY("any character") \ |
| 1080 |
end = cur; \ |
| 1081 |
goto END_CDATA_WITH_TRIM1_START; \ |
| 1082 |
END(END_CDATA_WITH_TRIM2) \ |
| 1083 |
EXPECT_ANY("any char") \ |
| 1084 |
if (content == NULL) content = cur - 1; \ |
| 1085 |
end = cur; \ |
| 1086 |
END(END_CDATA_WITH_TRIM1) \ |
| 1087 |
goto INVALID_XML; \ |
| 1088 |
END6(CDATA_WITH_TRIM, INVALID_XML) |
| 1089 |
|
| 1090 |
#define NORMALIZE_REFERENCE(loop) \ |
| 1091 |
_DO(PPCAT(loop, _REFERENCE)) \ |
| 1092 |
EXPECT_CHAR("char reference", '#') \ |
| 1093 |
_DO(PPCAT(loop, _CHAR_REFERENCE)) \ |
| 1094 |
EXPECT_CHAR("hex", 'x') \ |
| 1095 |
code = 0; \ |
| 1096 |
_DO(PPCAT(loop, _HEX_CHAR_REFERENCE_LOOP)) \ |
| 1097 |
EXPECT_DIGIT("hex digit") \ |
| 1098 |
code = code * 16 + (c - '0'); \ |
| 1099 |
break; \ |
| 1100 |
EXPECT_HEX_CHAR_LC("hex a-f") \ |
| 1101 |
code = code * 16 + (c - 'a') + 10; \ |
| 1102 |
break; \ |
| 1103 |
EXPECT_HEX_CHAR_UC("hex A-F") \ |
| 1104 |
code = code * 16 + (c - 'A') + 10; \ |
| 1105 |
break; \ |
| 1106 |
EXPECT_CHAR("reference end", ';') \ |
| 1107 |
goto PPCAT(loop, _REFEFENCE_VALUE); \ |
| 1108 |
END(PPCAT(loop, _HEX_CHAR_REFERENCE_LOOP)) \ |
| 1109 |
goto INVALID_REF; \ |
| 1110 |
EXPECT_DIGIT("digit") \ |
| 1111 |
code = (c - '0'); \ |
| 1112 |
_DO(PPCAT(loop, _CHAR_REFERENCE_LOOP)) \ |
| 1113 |
EXPECT_DIGIT("digit") \ |
| 1114 |
code = code * 10 + (c - '0'); \ |
| 1115 |
break; \ |
| 1116 |
EXPECT_CHAR("reference end", ';') \ |
| 1117 |
goto PPCAT(loop, _REFEFENCE_VALUE); \ |
| 1118 |
END(PPCAT(loop, _CHAR_REFERENCE_LOOP)) \ |
| 1119 |
goto INVALID_REF; \ |
| 1120 |
EXPECT_ANY("any char") \ |
| 1121 |
goto INVALID_REF; \ |
| 1122 |
END(PPCAT(loop, _CHAR_REFERENCE)) \ |
| 1123 |
goto INVALID_REF; \ |
| 1124 |
EXPECT_CHAR("amp or apos", 'a') \ |
| 1125 |
if (xh_str_auto_equal3(cur, 'm', 'p', ';', "mp;")) { \ |
| 1126 |
code = '&'; \ |
| 1127 |
cur += 3; \ |
| 1128 |
goto PPCAT(loop, _REFEFENCE_VALUE); \ |
| 1129 |
} \ |
| 1130 |
if (xh_str_auto_equal4(cur, 'p', 'o', 's', ';', "pos;")) { \ |
| 1131 |
code = '\''; \ |
| 1132 |
cur += 4; \ |
| 1133 |
goto PPCAT(loop, _REFEFENCE_VALUE); \ |
| 1134 |
} \ |
| 1135 |
goto INVALID_REF; \ |
| 1136 |
EXPECT_CHAR("lt", 'l') \ |
| 1137 |
if (xh_str_auto_equal2(cur, 't', ';', "t;")) { \ |
| 1138 |
code = '<'; \ |
| 1139 |
cur += 2; \ |
| 1140 |
goto PPCAT(loop, _REFEFENCE_VALUE); \ |
| 1141 |
} \ |
| 1142 |
goto INVALID_REF; \ |
| 1143 |
EXPECT_CHAR("gt", 'g') \ |
| 1144 |
if (xh_str_auto_equal2(cur, 't', ';', "t;")) { \ |
| 1145 |
code = '>'; \ |
| 1146 |
cur += 2; \ |
| 1147 |
goto PPCAT(loop, _REFEFENCE_VALUE); \ |
| 1148 |
} \ |
| 1149 |
goto INVALID_REF; \ |
| 1150 |
EXPECT_CHAR("quot", 'q') \ |
| 1151 |
if (xh_str_auto_equal4(cur, 'u', 'o', 't', ';', "uot;")) { \ |
| 1152 |
code = '"'; \ |
| 1153 |
cur += 4; \ |
| 1154 |
goto PPCAT(loop, _REFEFENCE_VALUE); \ |
| 1155 |
} \ |
| 1156 |
goto INVALID_REF; \ |
| 1157 |
EXPECT_ANY("any char") \ |
| 1158 |
goto INVALID_REF; \ |
| 1159 |
END(PPCAT(loop, _REFERENCE)) \ |
| 1160 |
goto INVALID_REF; \ |
| 1161 |
PPCAT(loop, _REFEFENCE_VALUE): \ |
| 1162 |
xh_log_trace1("parse reference value: %lu", code); \ |
| 1163 |
if (code == 0 || code > 0x10FFFF) goto INVALID_REF; \ |
| 1164 |
if (code >= 0x80) { \ |
| 1165 |
if (code < 0x800) { \ |
| 1166 |
*enc_cur++ = (code >> 6) | 0xC0; bits = 0; \ |
| 1167 |
} \ |
| 1168 |
else if (code < 0x10000) { \ |
| 1169 |
*enc_cur++ = (code >> 12) | 0xE0; bits = 6; \ |
| 1170 |
} \ |
| 1171 |
else if (code < 0x110000) { \ |
| 1172 |
*enc_cur++ = (code >> 18) | 0xF0; bits = 12; \ |
| 1173 |
} \ |
| 1174 |
else { \ |
| 1175 |
goto INVALID_REF; \ |
| 1176 |
} \ |
| 1177 |
for (; bits >= 0; bits-= 6) { \ |
| 1178 |
*enc_cur++ = ((code >> bits) & 0x3F) | 0x80; \ |
| 1179 |
} \ |
| 1180 |
} \ |
| 1181 |
else { \ |
| 1182 |
*enc_cur++ = (xh_char_t) code; \ |
| 1183 |
} |
| 1184 |
|
| 1185 |
#define NORMALIZE_LINE_FEED(loop) \ |
| 1186 |
_DO(PPCAT(loop, _NORMALIZE_LINE_FEED)) \ |
| 1187 |
EXPECT_CHAR("LF", '\n') \ |
| 1188 |
goto PPCAT(loop, _NORMALIZE_LINE_FEED_END); \ |
| 1189 |
EXPECT_ANY("any char") \ |
| 1190 |
cur--; \ |
| 1191 |
goto PPCAT(loop, _NORMALIZE_LINE_FEED_END); \ |
| 1192 |
END(PPCAT(loop, _NORMALIZE_LINE_FEED)) \ |
| 1193 |
PPCAT(loop, _NORMALIZE_LINE_FEED_END): \ |
| 1194 |
*enc_cur++ = '\n'; |
| 1195 |
|
| 1196 |
#define NORMALIZE_TEXT(loop, s, l) \ |
| 1197 |
enc_len = l; \ |
| 1198 |
if (enc_len) { \ |
| 1199 |
old_cur = cur; \ |
| 1200 |
old_eof = eof; \ |
| 1201 |
cur = s; \ |
| 1202 |
eof = cur + enc_len; \ |
| 1203 |
if (ctx->tmp == NULL) { \ |
| 1204 |
xh_log_trace1("malloc() %lu", enc_len); \ |
| 1205 |
if ((ctx->tmp = malloc(enc_len)) == NULL) goto MALLOC; \ |
| 1206 |
ctx->tmp_size = enc_len; \ |
| 1207 |
} \ |
| 1208 |
else if (enc_len > ctx->tmp_size) { \ |
| 1209 |
xh_log_trace1("realloc() %lu", enc_len); \ |
| 1210 |
if ((enc = realloc(ctx->tmp, enc_len)) == NULL) goto MALLOC;\ |
| 1211 |
ctx->tmp = enc; \ |
| 1212 |
ctx->tmp_size = enc_len; \ |
| 1213 |
} \ |
| 1214 |
enc = enc_cur = ctx->tmp; \ |
| 1215 |
memcpy(enc, cur, enc_len); \ |
| 1216 |
_DO(PPCAT(loop, _NORMALIZE_TEXT)) \ |
| 1217 |
EXPECT_CHAR("reference", '&') \ |
| 1218 |
NORMALIZE_REFERENCE(loop) \ |
| 1219 |
break; \ |
| 1220 |
EXPECT_CHAR("CR", '\r') \ |
| 1221 |
NORMALIZE_LINE_FEED(loop) \ |
| 1222 |
break; \ |
| 1223 |
EXPECT_ANY("any char") \ |
| 1224 |
*enc_cur++ = c; \ |
| 1225 |
END(PPCAT(loop, _NORMALIZE_TEXT)) \ |
| 1226 |
enc_len = enc_cur - enc; \ |
| 1227 |
cur = old_cur; \ |
| 1228 |
eof = old_eof; \ |
| 1229 |
} \ |
| 1230 |
else { \ |
| 1231 |
enc = s; \ |
| 1232 |
} |
| 1233 |
|
| 1234 |
#define END_OF_TEXT(loop, s, l) \ |
| 1235 |
if (s != NULL) { \ |
| 1236 |
if (flags & (XH_X2H_IS_NOT_BLANK | XH_X2H_TEXT_NODE)) { \ |
| 1237 |
if (flags & XH_X2H_NEED_NORMALIZE) { \ |
| 1238 |
NORMALIZE_TEXT(loop, s, (l)) \ |
| 1239 |
NEW_TEXT(enc, enc_len) \ |
| 1240 |
} \ |
| 1241 |
else { \ |
| 1242 |
NEW_TEXT(s, (l)) \ |
| 1243 |
} \ |
| 1244 |
} \ |
| 1245 |
s = NULL; \ |
| 1246 |
} |
| 1247 |
|
| 1248 |
static void |
| 1249 |
xh_x2h_parse_chunk(xh_x2h_ctx_t *ctx, xh_char_t **buf, size_t *bytesleft, xh_bool_t terminate) |
| 1250 |
{ |
| 1251 |
xh_char_t c, *cur, *node, *end, *content, *eof, *enc, |
| 1252 |
*enc_cur, *old_cur, *old_eof, *content_key, |
| 1253 |
*end_of_attr_value; |
| 1254 |
unsigned int depth, real_depth, code, flags, extra_flags, skip_depth; |
| 1255 |
int bits; |
| 1256 |
SV **lval, *val; |
| 1257 |
xh_x2h_node_t *nodes; |
| 1258 |
AV *av; |
| 1259 |
size_t enc_len, content_key_len; |
| 1260 |
|
| 1261 |
cur = *buf; |
| 1262 |
eof = cur + *bytesleft; |
| 1263 |
nodes = ctx->nodes; |
| 1264 |
depth = ctx->depth; |
| 1265 |
real_depth = ctx->real_depth; |
| 1266 |
flags = ctx->flags; |
| 1267 |
node = ctx->node; |
| 1268 |
end = ctx->end; |
| 1269 |
end_of_attr_value = ctx->end_of_attr_value; |
| 1270 |
content = ctx->content; |
| 1271 |
code = ctx->code; |
| 1272 |
skip_depth = ctx->skip_depth; |
| 1273 |
lval = ctx->lval; |
| 1274 |
enc = enc_cur = old_eof = old_cur = NULL; |
| 1275 |
c = '\0'; |
| 1276 |
|
| 1277 |
if (ctx->opts.content[0] == '\0') { |
| 1278 |
content_key = (xh_char_t *) DEF_CONTENT_KEY; |
| 1279 |
content_key_len = sizeof(DEF_CONTENT_KEY) - 1; |
| 1280 |
} |
| 1281 |
else { |
| 1282 |
content_key = ctx->opts.content; |
| 1283 |
content_key_len = xh_strlen(ctx->opts.content); |
| 1284 |
} |
| 1285 |
|
| 1286 |
#define XH_X2H_PROCESS_STATE(st) case st: goto st; |
| 1287 |
switch (ctx->state) { |
| 1288 |
case PARSER_ST_NONE: break; |
| 1289 |
XH_X2H_PARSER_STATE_LIST |
| 1290 |
case XML_DECL_FOUND: break; |
| 1291 |
case PARSER_ST_DONE: goto DONE; |
| 1292 |
} |
| 1293 |
#undef XH_X2H_PROCESS_STATE |
| 1294 |
|
| 1295 |
PARSE_CONTENT: |
| 1296 |
content = NULL; |
| 1297 |
flags &= ~(XH_X2H_NEED_NORMALIZE | XH_X2H_IS_NOT_BLANK); |
| 1298 |
DO(CONTENT) |
| 1299 |
EXPECT_CHAR("new element", '<') |
| 1300 |
DO(PARSE_ELEMENT) |
| 1301 |
EXPECT_CHAR("xml declaration", '?') |
| 1302 |
if (real_depth != 0) goto INVALID_XML; |
| 1303 |
END_OF_TEXT(TEXT_BEFORE_XML_DECL, content, end - content) |
| 1304 |
SCAN3(XML_DECL, 'x', 'm', 'l') |
| 1305 |
DO(XML_DECL_ATTR) |
| 1306 |
EXPECT_BLANK("blank") |
| 1307 |
#undef NEW_ATTRIBUTE |
| 1308 |
#define NEW_ATTRIBUTE(k, kl, v, vl) NEW_XML_DECL_ATTRIBUTE(k, kl, v, vl) |
| 1309 |
#undef SEARCH_ATTRIBUTE_VALUE |
| 1310 |
#define SEARCH_ATTRIBUTE_VALUE(loop, top_loop, quot) SEARCH_XML_DECL_ATTRIBUTE_VALUE(loop, top_loop, quot) |
| 1311 |
SEARCH_ATTRIBUTES(XML_DECL_ATTR, SEARCH_END_XML_DECLARATION) |
| 1312 |
#undef NEW_ATTRIBUTE |
| 1313 |
#define NEW_ATTRIBUTE(k, kl, v, vl) NEW_NODE_ATTRIBUTE(k, kl, v, vl) |
| 1314 |
#undef SEARCH_ATTRIBUTE_VALUE |
| 1315 |
#define SEARCH_ATTRIBUTE_VALUE(loop, top_loop, quot) SEARCH_NODE_ATTRIBUTE_VALUE(loop, top_loop, quot) |
| 1316 |
goto INVALID_XML; |
| 1317 |
EXPECT_CHAR("PI", '-') |
| 1318 |
SCAN10(STYLESHEET_PI, 's', 't', 'y', 'l', 'e', 's', 'h', 'e', 'e', 't') |
| 1319 |
DO(STYLESHEET_PI_ATTR) |
| 1320 |
EXPECT_BLANK("blank") |
| 1321 |
#undef NEW_ATTRIBUTE |
| 1322 |
#define NEW_ATTRIBUTE(k, kl, v, vl) NEW_PI_ATTRIBUTE(k, kl, v, vl) |
| 1323 |
#undef SEARCH_ATTRIBUTE_VALUE |
| 1324 |
#define SEARCH_ATTRIBUTE_VALUE(loop, top_loop, quot) SEARCH_XML_DECL_ATTRIBUTE_VALUE(loop, top_loop, quot) |
| 1325 |
SEARCH_ATTRIBUTES(STYLESHEET_PI_ATTR, SEARCH_END_PI) |
| 1326 |
#undef NEW_ATTRIBUTE |
| 1327 |
#define NEW_ATTRIBUTE(k, kl, v, vl) NEW_NODE_ATTRIBUTE(k, kl, v, vl) |
| 1328 |
#undef SEARCH_ATTRIBUTE_VALUE |
| 1329 |
#define SEARCH_ATTRIBUTE_VALUE(loop, top_loop, quot) SEARCH_NODE_ATTRIBUTE_VALUE(loop, top_loop, quot) |
| 1330 |
goto INVALID_XML; |
| 1331 |
EXPECT_ANY("wrong character") |
| 1332 |
goto INVALID_XML; |
| 1333 |
END(STYLESHEET_PI_ATTR) |
| 1334 |
goto INVALID_XML; |
| 1335 |
END10(STYLESHEET_PI, INVALID_XML) |
| 1336 |
goto INVALID_XML; |
| 1337 |
EXPECT_ANY("wrong character") |
| 1338 |
goto INVALID_XML; |
| 1339 |
END(XML_DECL_ATTR) |
| 1340 |
goto INVALID_XML; |
| 1341 |
END3(XML_DECL, INVALID_XML) |
| 1342 |
goto INVALID_XML; |
| 1343 |
EXPECT_CHAR("comment or cdata or doctype", '!') |
| 1344 |
flags &= ~XH_X2H_TEXT_NODE; |
| 1345 |
END_OF_TEXT(TEXT_BEFORE_COMMENT, content, end - content) |
| 1346 |
DO(XML_COMMENT_NODE_OR_CDATA) |
| 1347 |
EXPECT_CHAR("comment", '-') |
| 1348 |
PARSE_COMMENT |
| 1349 |
EXPECT_CHAR("cdata", '[') |
| 1350 |
if (ctx->opts.trim) { |
| 1351 |
PARSE_CDATA_WITH_TRIM |
| 1352 |
; |
| 1353 |
} |
| 1354 |
else { |
| 1355 |
PARSE_CDATA |
| 1356 |
; |
| 1357 |
} |
| 1358 |
EXPECT_CHAR("doctype", 'D') |
| 1359 |
PARSE_DOCTYPE |
| 1360 |
EXPECT_ANY("wrong character") |
| 1361 |
goto INVALID_XML; |
| 1362 |
END(XML_COMMENT_NODE_OR_CDATA) |
| 1363 |
goto INVALID_XML; |
| 1364 |
EXPECT_CHAR("closing tag", '/') |
| 1365 |
END_OF_TEXT(TEXT_BEFORE_CLOSING_TAG, content, end - content) |
| 1366 |
//node = cur; |
| 1367 |
DO(PARSE_CLOSING_TAG) |
| 1368 |
EXPECT_CHAR("end tag name", '>') |
| 1369 |
CLOSE_TAG |
| 1370 |
goto PARSE_CONTENT; |
| 1371 |
EXPECT_BLANK("end tag name") |
| 1372 |
DO(SEARCH_CLOSING_END_TAG) |
| 1373 |
EXPECT_CHAR("end tag", '>') |
| 1374 |
CLOSE_TAG |
| 1375 |
goto PARSE_CONTENT; |
| 1376 |
SKIP_BLANK |
| 1377 |
EXPECT_ANY("wrong character") |
| 1378 |
goto INVALID_XML; |
| 1379 |
END(SEARCH_CLOSING_END_TAG) |
| 1380 |
goto INVALID_XML; |
| 1381 |
END(PARSE_CLOSING_TAG) |
| 1382 |
goto INVALID_XML; |
| 1383 |
EXPECT_ANY("opening tag") |
| 1384 |
flags &= ~XH_X2H_TEXT_NODE; |
| 1385 |
END_OF_TEXT(TEXT_BEFORE_OPENING_TAG, content, end - content) |
| 1386 |
node = cur - 1; |
| 1387 |
DO(PARSE_OPENING_TAG) |
| 1388 |
EXPECT_CHAR("end tag", '>') |
| 1389 |
OPEN_TAG(node, cur - node - 1) |
| 1390 |
EVENT_START |
| 1391 |
goto PARSE_CONTENT; |
| 1392 |
EXPECT_CHAR("self closing tag", '/') |
| 1393 |
OPEN_TAG(node, cur - node - 1) |
| 1394 |
EVENT_START |
| 1395 |
CLOSE_TAG |
| 1396 |
|
| 1397 |
DO(SEARCH_OPENING_END_TAG) |
| 1398 |
EXPECT_CHAR("end tag", '>') |
| 1399 |
goto PARSE_CONTENT; |
| 1400 |
EXPECT_ANY("wrong character") |
| 1401 |
goto INVALID_XML; |
| 1402 |
END(SEARCH_OPENING_END_TAG) |
| 1403 |
goto INVALID_XML; |
| 1404 |
EXPECT_BLANK("end tag name") |
| 1405 |
OPEN_TAG(node, cur - node - 1) |
| 1406 |
|
| 1407 |
SEARCH_ATTRIBUTES(NODE, SEARCH_END_TAG) |
| 1408 |
|
| 1409 |
goto PARSE_CONTENT; |
| 1410 |
END(PARSE_OPENING_TAG); |
| 1411 |
goto INVALID_XML; |
| 1412 |
END(PARSE_ELEMENT) |
| 1413 |
|
| 1414 |
EXPECT_CHAR("wrong symbol", '>') |
| 1415 |
goto INVALID_XML; |
| 1416 |
EXPECT_BLANK_WO_CR("blank") |
| 1417 |
if (!ctx->opts.trim) |
| 1418 |
goto START_CONTENT; |
| 1419 |
break; |
| 1420 |
EXPECT_CHAR("CR", '\r') |
| 1421 |
if (content != NULL) { |
| 1422 |
flags |= XH_X2H_NORMALIZE_LINE_FEED; |
| 1423 |
} |
| 1424 |
if (!ctx->opts.trim) |
| 1425 |
goto START_CONTENT; |
| 1426 |
break; |
| 1427 |
EXPECT_CHAR("reference", '&') |
| 1428 |
flags |= (XH_X2H_NORMALIZE_REF | XH_X2H_IS_NOT_BLANK); |
| 1429 |
goto START_CONTENT; |
| 1430 |
EXPECT_ANY("any char") |
| 1431 |
flags |= XH_X2H_IS_NOT_BLANK; |
| 1432 |
START_CONTENT: |
| 1433 |
if (content == NULL) content = cur - 1; |
| 1434 |
end = cur; |
| 1435 |
END(CONTENT) |
| 1436 |
|
| 1437 |
if ( |
| 1438 |
((content != NULL) && (flags & XH_X2H_IS_NOT_BLANK)) || |
| 1439 |
(real_depth != 0) || |
| 1440 |
!(flags & XH_X2H_ROOT_FOUND) |
| 1441 |
) goto INVALID_XML; |
| 1442 |
|
| 1443 |
ctx->state = PARSER_ST_DONE; |
| 1444 |
*bytesleft = eof - cur; |
| 1445 |
*buf = cur; |
| 1446 |
return; |
| 1447 |
|
| 1448 |
PARSE_DOCTYPE_INTSUBSET: |
| 1449 |
DO(DOCTYPE_INTSUBSET) |
| 1450 |
EXPECT_CHAR("end of internal subset", ']') |
| 1451 |
DO(DOCTYPE_END) |
| 1452 |
SKIP_BLANK |
| 1453 |
EXPECT_CHAR("end doctype", '>') |
| 1454 |
goto PARSE_CONTENT; |
| 1455 |
EXPECT_ANY("wrong character") |
| 1456 |
goto INVALID_XML; |
| 1457 |
END(DOCTYPE_END) |
| 1458 |
goto INVALID_XML; |
| 1459 |
END(DOCTYPE_INTSUBSET) |
| 1460 |
goto INVALID_XML; |
| 1461 |
|
| 1462 |
PARSE_DOCTYPE_INTSUBSET_START: |
| 1463 |
DO(DOCTYPE_INTSUBSET_START) |
| 1464 |
SKIP_BLANK |
| 1465 |
EXPECT_CHAR("end doctype", '>') |
| 1466 |
goto PARSE_CONTENT; |
| 1467 |
EXPECT_CHAR("start of internal subset", '[') |
| 1468 |
goto PARSE_DOCTYPE_INTSUBSET; |
| 1469 |
EXPECT_ANY("wrong character") |
| 1470 |
goto INVALID_XML; |
| 1471 |
END(DOCTYPE_INTSUBSET_START) |
| 1472 |
goto INVALID_XML; |
| 1473 |
|
| 1474 |
XML_DECL_FOUND: |
| 1475 |
ctx->state = XML_DECL_FOUND; |
| 1476 |
goto CHUNK_FINISH; |
| 1477 |
PARSER_STOP: |
| 1478 |
flags |= XH_X2H_STOPPED; |
| 1479 |
CHUNK_FINISH: |
| 1480 |
ctx->content = content; |
| 1481 |
ctx->node = node; |
| 1482 |
ctx->end = end; |
| 1483 |
ctx->end_of_attr_value = end_of_attr_value; |
| 1484 |
ctx->depth = depth; |
| 1485 |
ctx->real_depth = real_depth; |
| 1486 |
ctx->flags = flags; |
| 1487 |
ctx->code = code; |
| 1488 |
ctx->skip_depth = skip_depth; |
| 1489 |
ctx->lval = lval; |
| 1490 |
*bytesleft = eof - cur; |
| 1491 |
*buf = cur; |
| 1492 |
return; |
| 1493 |
|
| 1494 |
MAX_DEPTH_EXCEEDED: |
| 1495 |
croak("Maximum depth exceeded"); |
| 1496 |
INVALID_XML: |
| 1497 |
croak("Invalid XML"); |
| 1498 |
INVALID_REF: |
| 1499 |
croak("Invalid reference"); |
| 1500 |
MALLOC: |
| 1501 |
croak("Memory allocation error"); |
| 1502 |
DONE: |
| 1503 |
croak("Parsing is done"); |
| 1504 |
} |
| 1505 |
|
| 1506 |
static void |
| 1507 |
xh_x2h_parse(xh_x2h_ctx_t *ctx, xh_reader_t *reader) |
| 1508 |
{ |
| 1509 |
xh_char_t *buf, *preserve; |
| 1510 |
size_t len, off; |
| 1511 |
xh_bool_t eof; |
| 1512 |
|
| 1513 |
do { |
| 1514 |
preserve = ctx->node != NULL ? ctx->node : ctx->content; |
| 1515 |
|
| 1516 |
len = reader->read(reader, &buf, preserve, &off); |
| 1517 |
eof = (len == 0); |
| 1518 |
if (off) { |
| 1519 |
if (ctx->node != NULL) ctx->node -= off; |
| 1520 |
if (ctx->content != NULL) ctx->content -= off; |
| 1521 |
if (ctx->end != NULL) ctx->end -= off; |
| 1522 |
if (ctx->end_of_attr_value != NULL) ctx->end_of_attr_value -= off; |
| 1523 |
} |
| 1524 |
|
| 1525 |
xh_log_trace2("read buf: %.*s", len, buf); |
| 1526 |
|
| 1527 |
do { |
| 1528 |
xh_log_trace2("parse buf: %.*s", len, buf); |
| 1529 |
|
| 1530 |
xh_x2h_parse_chunk(ctx, &buf, &len, eof); |
| 1531 |
|
| 1532 |
if (ctx->flags & XH_X2H_STOPPED) |
| 1533 |
return; |
| 1534 |
|
| 1535 |
if (ctx->state == XML_DECL_FOUND && ctx->opts.encoding[0] == '\0' && ctx->encoding[0] != '\0') { |
| 1536 |
reader->switch_encoding(reader, ctx->encoding, &buf, &len); |
| 1537 |
} |
| 1538 |
} while (len > 0); |
| 1539 |
} while (!eof); |
| 1540 |
|
| 1541 |
if (ctx->state != PARSER_ST_DONE) |
| 1542 |
croak("Invalid XML"); |
| 1543 |
} |
| 1544 |
|
| 1545 |
SV * |
| 1546 |
xh_x2h(xh_x2h_ctx_t *ctx) |
| 1547 |
{ |
| 1548 |
HV *hv; |
| 1549 |
HE *he; |
| 1550 |
SV *result; |
| 1551 |
|
| 1552 |
dXCPT; |
| 1553 |
XCPT_TRY_START |
| 1554 |
{ |
| 1555 |
xh_x2h_init_result(ctx); |
| 1556 |
|
| 1557 |
xh_reader_init(&ctx->reader, ctx->input, ctx->opts.encoding, ctx->opts.buf_size); |
| 1558 |
|
| 1559 |
xh_x2h_parse(ctx, &ctx->reader); |
| 1560 |
} XCPT_TRY_END |
| 1561 |
|
| 1562 |
XCPT_CATCH |
| 1563 |
{ |
| 1564 |
if (ctx->result != NULL) SvREFCNT_dec(ctx->result); |
| 1565 |
xh_reader_destroy(&ctx->reader); |
| 1566 |
XCPT_RETHROW; |
| 1567 |
} |
| 1568 |
|
| 1569 |
xh_reader_destroy(&ctx->reader); |
| 1570 |
|
| 1571 |
result = ctx->result; |
| 1572 |
if (ctx->opts.filter.enable) { |
| 1573 |
if (ctx->opts.cb != NULL) result = NULL; |
| 1574 |
} |
| 1575 |
else if (!ctx->opts.keep_root) { |
| 1576 |
hv = (HV *) SvRV(result); |
| 1577 |
hv_iterinit(hv); |
| 1578 |
if ((he = hv_iternext(hv))) { |
| 1579 |
result = hv_iterval(hv, he); |
| 1580 |
SvREFCNT_inc(result); |
| 1581 |
} |
| 1582 |
else { |
| 1583 |
result = NULL; |
| 1584 |
} |
| 1585 |
SvREFCNT_dec(ctx->result); |
| 1586 |
} |
| 1587 |
|
| 1588 |
return result; |
| 1589 |
} |