void srl_iterator_unite(pTHX_ srl_iterator_t *iter) { UV offset; srl_stack_t *stack = iter->pstack; SRL_ITER_TRACE("--------------------------"); if (expect_false(SRL_STACK_DEPTH(stack) <= 0)) SRL_ITER_ERROR("There is nothing to unite. Please call disjoin first."); while (!SRL_ITER_STACK_ON_ROOT(stack)) { srl_stack_pop(stack); } offset = stack->ptr->offset; srl_stack_pop(stack); // remove SRL_ITER_STACK_ROOT_TAG SRL_ITER_ASSERT_STACK(iter); iter->buf.pos = iter->buf.body_pos + offset; SRL_ITER_REPORT_STACK_STATE(iter); SRL_ITER_TRACE("ofs %"UVuf" body_ofs %"UVuf, (UV) SRL_RDR_POS_OFS((iter)->pbuf), (UV) SRL_RDR_BODY_POS_OFS((iter)->pbuf)); DEBUG_ASSERT_RDR_SANE(iter->pbuf); }
void srl_iterator_disjoin(pTHX_ srl_iterator_t *iter) { srl_iterator_stack_ptr stack_ptr; SRL_ITER_REPORT_STACK_STATE(iter); SRL_ITER_TRACE("ofs %"UVuf" body_ofs %"UVuf, (UV) SRL_RDR_POS_OFS((iter)->pbuf), (UV) SRL_RDR_BODY_POS_OFS((iter)->pbuf)); // This record apart of being a boundary stores offset to idx's tag (i.e // current tag). By default stack keeps offset to tag's starting point // (i.e. continer located in the buf). srl_stack_push_ptr(iter->pstack, stack_ptr); stack_ptr->offset = SRL_RDR_BODY_POS_OFS(iter->pbuf); // disjoint point stack_ptr->tag = SRL_ITER_STACK_ROOT_TAG; stack_ptr->count = 1; stack_ptr->idx = 1; }
IV srl_iterator_array_goto(pTHX_ srl_iterator_t *iter, I32 idx) { I32 s_idx; srl_iterator_stack_ptr stack_ptr = iter->stack.ptr; DEBUG_ASSERT_RDR_SANE(iter->pbuf); SRL_ITER_ASSERT_EOF(iter, "array element"); SRL_ITER_ASSERT_STACK(iter); SRL_ITER_ASSERT_ARRAY_ON_STACK(iter); SRL_ITER_TRACE("idx=%d", idx); SRL_ITER_REPORT_STACK_STATE(iter); if (idx >= 0) { s_idx = stack_ptr->count - idx; if (idx >= (I32) stack_ptr->count) { SRL_ITER_TRACE("Index is out of range, idx=%d count=%u", idx, stack_ptr->count); return SRL_ITER_NOT_FOUND; } } else { s_idx = -idx; if (s_idx > (I32) stack_ptr->count) { SRL_ITER_TRACE("Index is out of range, idx=%d count=%u", idx, stack_ptr->count); return SRL_ITER_NOT_FOUND; } } if (s_idx == stack_ptr->idx) { return SRL_RDR_BODY_POS_OFS(iter->pbuf); // already at expected position } else if (s_idx > stack_ptr->idx) { SRL_ITER_ERRORf2("Can't go backwards, idx=%d, count=%u", idx, stack_ptr->count); } // srl_iterator_next garantee that we remans on current stack srl_iterator_next(aTHX_ iter, stack_ptr->idx - s_idx); assert(stack_ptr->idx == s_idx); return SRL_RDR_BODY_POS_OFS(iter->pbuf); }
SRL_STATIC_INLINE void srl_iterator_restore_stack_position(pTHX_ srl_iterator_t *iter) { SRL_ITER_ASSERT_STACK(iter); iter->stack.ptr->idx = iter->stack.ptr->count; iter->buf.pos = iter->buf.body_pos + iter->stack.ptr->offset; SRL_ITER_REPORT_STACK_STATE(iter); SRL_ITER_TRACE("ofs %"UVuf" body_ofs %"UVuf, (UV) SRL_RDR_POS_OFS((iter)->pbuf), (UV) SRL_RDR_BODY_POS_OFS((iter)->pbuf)); DEBUG_ASSERT_RDR_SANE(iter->pbuf); }
IV srl_iterator_hash_exists(pTHX_ srl_iterator_t *iter, const char *name, STRLEN name_len) { U8 tag; UV length, offset; const char *key_ptr; IV stack_depth = iter->stack.depth; srl_iterator_stack_ptr stack_ptr = iter->stack.ptr; DEBUG_ASSERT_RDR_SANE(iter->pbuf); SRL_ITER_ASSERT_EOF(iter, "stringish"); SRL_ITER_ASSERT_STACK(iter); SRL_ITER_ASSERT_HASH_ON_STACK(iter); SRL_ITER_TRACE("name=%.*s", (int) name_len, name); SRL_ITER_REPORT_STACK_STATE(iter); while (stack_ptr->idx) { stack_ptr->idx--; // do not make it be part of while clause SRL_ITER_ASSERT_STACK(iter); assert(stack_ptr->idx % 2 == 1); assert(iter->stack.depth == stack_depth); DEBUG_ASSERT_RDR_SANE(iter->pbuf); tag = *iter->buf.pos & ~SRL_HDR_TRACK_FLAG; SRL_ITER_REPORT_TAG(iter, tag); iter->buf.pos++; switch (tag) { CASE_SRL_HDR_SHORT_BINARY: length = SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag); key_ptr = (const char *) iter->buf.pos; iter->buf.pos += length; break; case SRL_HDR_BINARY: length = srl_read_varint_uv_length(aTHX_ iter->pbuf, " while reading BINARY"); key_ptr = (const char *) iter->buf.pos; iter->buf.pos += length; break; case SRL_HDR_STR_UTF8: // TODO deal with UTF8 length = srl_read_varint_uv_length(aTHX_ iter->pbuf, " while reading STR_UTF8"); key_ptr = (const char *) iter->buf.pos; iter->buf.pos += length; break; case SRL_HDR_COPY: offset = srl_read_varint_uv_offset(aTHX_ iter->pbuf, " while reading COPY tag"); key_ptr = (const char *) iter->buf.body_pos + offset; tag = *key_ptr & ~SRL_HDR_TRACK_FLAG; key_ptr++; /* Note we do NOT validate these items, as we have already read them * and if they were a problem we would not be here to process them! */ switch (tag) { CASE_SRL_HDR_SHORT_BINARY: length = SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag); break; case SRL_HDR_BINARY: SET_UV_FROM_VARINT(iter->pbuf, length, key_ptr); break; case SRL_HDR_STR_UTF8: // TODO deal with UTF8 SET_UV_FROM_VARINT(iter->pbuf, length, key_ptr); break; default: SRL_RDR_ERROR_BAD_COPY(iter->pbuf, SRL_HDR_HASH); } break; default: SRL_RDR_ERROR_UNEXPECTED(iter->pbuf, tag, "stringish"); } if (expect_false((srl_reader_char_ptr) key_ptr >= iter->buf.end)) { SRL_RDR_ERROR_EOF(iter->pbuf, "string content"); } if ( length == name_len && memcmp(name, key_ptr, name_len) == 0) { SRL_ITER_TRACE("found key '%.*s' at offset %"UVuf, (int) name_len, name, SRL_RDR_BODY_POS_OFS(iter->pbuf)); return SRL_RDR_BODY_POS_OFS(iter->pbuf); } // srl_iterator_next garantee that we remans on current stack srl_iterator_next(aTHX_ iter, 1); stack_ptr = iter->stack.ptr; } SRL_ITER_TRACE("didn't found key '%.*s'", (int) name_len, name); return SRL_ITER_NOT_FOUND; }
UV srl_iterator_offset(pTHX_ srl_iterator_t *iter) { return SRL_RDR_BODY_POS_OFS(iter->pbuf); }
void srl_iterator_set_document(pTHX_ srl_iterator_t *iter, SV *src) { STRLEN len; UV header_len; U8 encoding_flags; U8 protocol_version; srl_reader_char_ptr tmp; IV proto_version_and_encoding_flags_int; /* clear previous buffer */ if (iter->tmp_buf_owner) { SvREFCNT_dec(iter->tmp_buf_owner); iter->tmp_buf_owner = NULL; } if (iter->document) { SvREFCNT_dec(iter->document); iter->document = NULL; } iter->document = src; SvREFCNT_inc(iter->document); tmp = (srl_reader_char_ptr) SvPV(src, len); iter->buf.start = iter->buf.pos = tmp; iter->buf.end = iter->buf.start + len; proto_version_and_encoding_flags_int = srl_validate_header_version(aTHX_ iter->buf.start, len); if (proto_version_and_encoding_flags_int < 1) { if (proto_version_and_encoding_flags_int == 0) SRL_RDR_ERROR(iter->pbuf, "Bad Sereal header: It seems your document was accidentally UTF-8 encoded"); else SRL_RDR_ERROR(iter->pbuf, "Bad Sereal header: Not a valid Sereal document."); } iter->buf.pos += 5; encoding_flags = (U8) (proto_version_and_encoding_flags_int & SRL_PROTOCOL_ENCODING_MASK); protocol_version = (U8) (proto_version_and_encoding_flags_int & SRL_PROTOCOL_VERSION_MASK); if (expect_false(protocol_version > 3 || protocol_version < 1)) { SRL_RDR_ERRORf1(iter->pbuf, "Unsupported Sereal protocol version %u", (unsigned int) protocol_version); } // skip header in any case header_len = srl_read_varint_uv_length(aTHX_ iter->pbuf, " while reading header"); iter->buf.pos += header_len; if (encoding_flags == SRL_PROTOCOL_ENCODING_RAW) { /* no op */ } else if ( encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY || encoding_flags == SRL_PROTOCOL_ENCODING_SNAPPY_INCREMENTAL) { srl_decompress_body_snappy(aTHX_ iter->pbuf, encoding_flags, &iter->tmp_buf_owner); SvREFCNT_inc(iter->tmp_buf_owner); } else if (encoding_flags == SRL_PROTOCOL_ENCODING_ZLIB) { srl_decompress_body_zlib(aTHX_ iter->pbuf, &iter->tmp_buf_owner); SvREFCNT_inc(iter->tmp_buf_owner); } else { SRL_RDR_ERROR(iter->pbuf, "Sereal document encoded in an unknown format"); } /* this function *MUST* be called after calling srl_decompress_body* */ SRL_RDR_UPDATE_BODY_POS(iter->pbuf, protocol_version); DEBUG_ASSERT_RDR_SANE(iter->pbuf); iter->first_tag_offset = SRL_RDR_BODY_POS_OFS(iter->pbuf); srl_iterator_reset(aTHX_ iter); }
SRL_STATIC_INLINE void srl_merge_single_value(pTHX_ srl_merger_t *mrg) { U8 tag; UV length, offset; ptable_entry_ptr ptable_entry; read_again: assert(mrg->recursion_depth >= 0); DEBUG_ASSERT_RDR_SANE(mrg->pibuf); DEBUG_ASSERT_BUF_SANE(&mrg->obuf); if (expect_false(++mrg->recursion_depth > mrg->max_recursion_depth)) SRL_RDR_ERRORf1(mrg->pibuf, "Reached recursion limit (%lu) during merging", mrg->max_recursion_depth); ptable_entry = NULL; if (expect_false(SRL_RDR_DONE(mrg->pibuf))) SRL_RDR_ERROR(mrg->pibuf, "Unexpected termination of input buffer"); tag = *mrg->ibuf.pos & ~SRL_HDR_TRACK_FLAG; SRL_REPORT_CURRENT_TAG(mrg, tag); if (mrg->tracked_offsets && !srl_stack_empty(mrg->tracked_offsets)) { UV itag_offset = SRL_RDR_BODY_POS_OFS(mrg->pibuf); if (expect_false(itag_offset == srl_stack_peek_nocheck(aTHX_ mrg->tracked_offsets))) { // trackme case srl_stack_pop_nocheck(mrg->tracked_offsets); ptable_entry = srl_store_tracked_offset(aTHX_ mrg, itag_offset, BODY_POS_OFS(&mrg->obuf)); } } if (tag <= SRL_HDR_NEG_HIGH) { srl_buf_cat_tag_nocheck(mrg, tag); } else if (tag >= SRL_HDR_ARRAYREF_LOW && tag <= SRL_HDR_ARRAYREF_HIGH) { srl_merge_array(aTHX_ mrg, tag, SRL_HDR_ARRAYREF_LEN_FROM_TAG(tag)); } else if (tag >= SRL_HDR_HASHREF_LOW && tag <= SRL_HDR_HASHREF_HIGH) { srl_merge_hash(aTHX_ mrg, tag, SRL_HDR_HASHREF_LEN_FROM_TAG(tag)); } else if (tag >= SRL_HDR_SHORT_BINARY_LOW) { srl_merge_short_binary(aTHX_ mrg, tag, ptable_entry); } else { switch (tag) { case SRL_HDR_VARINT: case SRL_HDR_ZIGZAG: srl_buf_cat_tag_nocheck(mrg, tag); srl_copy_varint(aTHX_ mrg); break; case SRL_HDR_FLOAT: srl_buf_copy_content_nocheck(aTHX_ mrg, 5); break; case SRL_HDR_DOUBLE: srl_buf_copy_content_nocheck(aTHX_ mrg, 9); break; case SRL_HDR_LONG_DOUBLE: srl_buf_copy_content_nocheck(aTHX_ mrg, 17); break; case SRL_HDR_TRUE: case SRL_HDR_FALSE: case SRL_HDR_UNDEF: case SRL_HDR_CANONICAL_UNDEF: srl_buf_cat_tag_nocheck(mrg, tag); break; case SRL_HDR_BINARY: case SRL_HDR_STR_UTF8: srl_merge_binary_utf8(aTHX_ mrg, ptable_entry); break; case SRL_HDR_HASH: mrg->ibuf.pos++; // skip tag in input buffer length = srl_read_varint_uv_count(aTHX_ mrg->pibuf, " while reading ARRAY or HASH"); srl_merge_hash(aTHX_ mrg, tag, length); break; case SRL_HDR_ARRAY: mrg->ibuf.pos++; // skip tag in input buffer length = srl_read_varint_uv_count(aTHX_ mrg->pibuf, " while reading ARRAY or HASH"); srl_merge_array(aTHX_ mrg, tag, length); break; default: switch (tag) { case SRL_HDR_COPY: case SRL_HDR_REFP: case SRL_HDR_ALIAS: mrg->ibuf.pos++; // skip tag in input buffer offset = srl_read_varint_uv_offset(aTHX_ mrg->pibuf, " while reading COPY/ALIAS/REFP"); offset = srl_lookup_tracked_offset(aTHX_ mrg, offset); // convert ibuf offset to obuf offset srl_buf_cat_varint(aTHX_ &mrg->obuf, tag, offset); if (tag == SRL_HDR_REFP || tag == SRL_HDR_ALIAS) { SRL_SET_TRACK_FLAG(*(mrg->obuf.body_pos + offset)); } break; case SRL_HDR_REFN: case SRL_HDR_WEAKEN: case SRL_HDR_EXTEND: srl_buf_cat_tag_nocheck(mrg, tag); goto read_again; case SRL_HDR_OBJECT: case SRL_HDR_OBJECT_FREEZE: srl_merge_object(aTHX_ mrg, tag); break; case SRL_HDR_REGEXP: srl_buf_cat_tag_nocheck(mrg, tag); srl_merge_stringish(aTHX_ mrg); tag = *mrg->ibuf.pos; if (expect_false(tag < SRL_HDR_SHORT_BINARY_LOW)) SRL_RDR_ERROR_UNEXPECTED(mrg->pibuf, tag, "SRL_HDR_SHORT_BINARY"); srl_buf_copy_content_nocheck(aTHX_ mrg, SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag) + 1); break; case SRL_HDR_OBJECTV: case SRL_HDR_OBJECTV_FREEZE: mrg->ibuf.pos++; // skip tag in input buffer offset = srl_read_varint_uv_offset(aTHX_ mrg->pibuf, " while reading OBJECTV/OBJECTV_FREEZE"); offset = srl_lookup_tracked_offset(aTHX_ mrg, offset); // convert ibuf offset to obuf offset srl_buf_cat_varint(aTHX_ &mrg->obuf, tag, offset); goto read_again; case SRL_HDR_PAD: while (SRL_RDR_NOT_DONE(mrg->pibuf) && *mrg->ibuf.pos == SRL_HDR_PAD) { srl_buf_cat_tag_nocheck(mrg, SRL_HDR_PAD); } goto read_again; default: SRL_RDR_ERROR_UNIMPLEMENTED(mrg->pibuf, tag, ""); break; } } } --mrg->recursion_depth; DEBUG_ASSERT_RDR_SANE(mrg->pibuf); DEBUG_ASSERT_BUF_SANE(&mrg->obuf); }