| 1 |
|
| /* Must be defined before including Perl header files or we slow down by 2x! */ |
| 2 |
|
| #define PERL_NO_GET_CONTEXT |
| 3 |
|
| |
| 4 |
|
| #ifdef __cplusplus |
| 5 |
|
| extern "C" { |
| 6 |
|
| #endif |
| 7 |
|
| #include "EXTERN.h" |
| 8 |
|
| #include "perl.h" |
| 9 |
|
| #include "XSUB.h" |
| 10 |
|
| #include "ppport.h" |
| 11 |
|
| #ifdef __cplusplus |
| 12 |
|
| } |
| 13 |
|
| #endif |
| 14 |
|
| |
| 15 |
|
| #include <stdlib.h> |
| 16 |
|
| |
| 17 |
|
| #ifndef PERL_VERSION |
| 18 |
|
| # include <patchlevel.h> |
| 19 |
|
| # if !(defined(PERL_VERSION) || (PERL_SUBVERSION > 0 && defined(PATCHLEVEL))) |
| 20 |
|
| # include <could_not_find_Perl_patchlevel.h> |
| 21 |
|
| # endif |
| 22 |
|
| # define PERL_REVISION 5 |
| 23 |
|
| # define PERL_VERSION PATCHLEVEL |
| 24 |
|
| # define PERL_SUBVERSION PERL_SUBVERSION |
| 25 |
|
| #endif |
| 26 |
|
| #if PERL_VERSION < 8 |
| 27 |
|
| # define PERL_MAGIC_qr 'r' /* precompiled qr// regex */ |
| 28 |
|
| # define BFD_Svs_SMG_OR_RMG SVs_RMG |
| 29 |
|
| #elif ((PERL_VERSION==8) && (PERL_SUBVERSION >= 1) || (PERL_VERSION>8)) |
| 30 |
|
| # define BFD_Svs_SMG_OR_RMG SVs_SMG |
| 31 |
|
| # define MY_PLACEHOLDER PL_sv_placeholder |
| 32 |
|
| #else |
| 33 |
|
| # define BFD_Svs_SMG_OR_RMG SVs_RMG |
| 34 |
|
| # define MY_PLACEHOLDER PL_sv_undef |
| 35 |
|
| #endif |
| 36 |
|
| #if (((PERL_VERSION == 9) && (PERL_SUBVERSION >= 4)) || (PERL_VERSION > 9)) |
| 37 |
|
| # define NEW_REGEX_ENGINE 1 |
| 38 |
|
| #endif |
| 39 |
|
| #if (((PERL_VERSION == 8) && (PERL_SUBVERSION >= 1)) || (PERL_VERSION > 8)) |
| 40 |
|
| #define MY_CAN_FIND_PLACEHOLDERS |
| 41 |
|
| #define HAS_SV2OBJ |
| 42 |
|
| #endif |
| 43 |
|
| |
| 44 |
|
| #include "srl_protocol.h" |
| 45 |
|
| |
| 46 |
|
| //#define SRL_SPLITTER_TRACE(msg, args...) warn((msg), args) |
| 47 |
|
| #define SRL_SPLITTER_TRACE(msg, args...) |
| 48 |
|
| |
| 49 |
|
| #include "srl_splitter.h" |
| 50 |
|
| #include "srl_common.h" |
| 51 |
|
| #include "srl_protocol.h" |
| 52 |
|
| #include "srl_inline.h" |
| 53 |
|
| |
| 54 |
|
| #include "snappy/csnappy_decompress.c" |
| 55 |
|
| #include "miniz.h" |
| 56 |
|
| |
| 57 |
|
| #include "uthash.h" |
| 58 |
|
| |
| 59 |
|
| #define STACK_SIZE_INCR 1024 |
| 60 |
|
| |
| 61 |
|
| #define IS_SRL_HDR_ARRAYREF(tag) (((tag) & SRL_HDR_ARRAYREF) == SRL_HDR_ARRAYREF) |
| 62 |
|
| #define IS_SRL_HDR_HASHREF(tag) (((tag) & SRL_HDR_HASHREF) == SRL_HDR_HASHREF) |
| 63 |
|
| #define IS_SRL_HDR_SHORT_BINARY(tag) (((tag) & SRL_HDR_SHORT_BINARY_LOW) == SRL_HDR_SHORT_BINARY_LOW) |
| 64 |
|
| #define SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag) ((tag) & SRL_MASK_SHORT_BINARY_LEN) |
| 65 |
|
| |
| 66 |
|
| |
| 67 |
|
| #define SRL_SPLITTER_SET_OPTION(splitter, flag_num) ((splitter)->flags |= (flag_num)) |
| 68 |
|
| #define SRL_SPLITTER_HAVE_OPTION(splitter, flag_num) ((splitter)->flags & (flag_num)) |
| 69 |
|
| |
| 70 |
|
| |
| 71 |
|
| #define SRL_MAX_VARINT_LENGTH 11 |
| 72 |
|
| |
| 73 |
|
| |
| 74 |
|
| /* predeclare all our subs so we have one definitive authority for their signatures */ |
| 75 |
|
| SRL_STATIC_INLINE srl_splitter_t * srl_empty_splitter_struct(pTHX); |
| 76 |
|
| SRL_STATIC_INLINE void _parse_header(pTHX_ srl_splitter_t *splitter); |
| 77 |
|
| SRL_STATIC_INLINE UV _read_varint_uv_nocheck(srl_splitter_t *splitter); |
| 78 |
|
| SRL_STATIC_INLINE int _parse(pTHX_ srl_splitter_t * splitter); |
| 79 |
|
| SRL_STATIC_INLINE void _read_tag(pTHX_ srl_splitter_t * splitter, char tag); |
| 80 |
|
| SRL_STATIC_INLINE void _read_varint(srl_splitter_t * splitter); |
| 81 |
|
| SRL_STATIC_INLINE void _read_zigzag(srl_splitter_t * splitter); |
| 82 |
|
| SRL_STATIC_INLINE void _read_float(srl_splitter_t * splitter); |
| 83 |
|
| SRL_STATIC_INLINE void _read_double(srl_splitter_t * splitter); |
| 84 |
|
| SRL_STATIC_INLINE void _read_long_double(srl_splitter_t * splitter); |
| 85 |
|
| SRL_STATIC_INLINE void _read_string(pTHX_ srl_splitter_t * splitter, bool is_utf8); |
| 86 |
|
| SRL_STATIC_INLINE void _read_weaken(srl_splitter_t * splitter); |
| 87 |
|
| SRL_STATIC_INLINE void _read_refn(srl_splitter_t * splitter); |
| 88 |
|
| SRL_STATIC_INLINE void _read_refp(pTHX_ srl_splitter_t * splitter); |
| 89 |
|
| SRL_STATIC_INLINE void _read_object(srl_splitter_t * splitter, bool is_freeze); |
| 90 |
|
| SRL_STATIC_INLINE void _read_objectv(pTHX_ srl_splitter_t * splitter, bool is_freeze); |
| 91 |
|
| SRL_STATIC_INLINE void _read_copy(pTHX_ srl_splitter_t * splitter); |
| 92 |
|
| SRL_STATIC_INLINE void _read_alias(pTHX_ srl_splitter_t * splitter); |
| 93 |
|
| SRL_STATIC_INLINE void _read_hash(srl_splitter_t * splitter); |
| 94 |
|
| SRL_STATIC_INLINE void _read_array(srl_splitter_t * splitter); |
| 95 |
|
| SRL_STATIC_INLINE void _read_regexp(srl_splitter_t * splitter); |
| 96 |
|
| SRL_STATIC_INLINE void _update_varint_from_to(char *varint_start, char *varint_end, UV number); |
| 97 |
|
| SRL_STATIC_INLINE char* _set_varint_nocheck(char* buf, UV n); |
| 98 |
|
| SRL_STATIC_INLINE bool _maybe_flush_chunk (pTHX_ srl_splitter_t *splitter, char* end_pos, char* next_start_pos); |
| 99 |
|
| SRL_STATIC_INLINE void _empty_hashes(pTHX); |
| 100 |
|
| SRL_STATIC_INLINE void _check_for_duplicates(pTHX_ srl_splitter_t * splitter, char* binary_start_pos, UV len, bool is_utf8); |
| 101 |
|
| SRL_STATIC_INLINE void _cat_to_chunk(pTHX_ srl_splitter_t *splitter, char* str, UV str_len); |
| 102 |
|
| SRL_STATIC_INLINE UV stack_pop(srl_splitter_stack_t * stack); |
| 103 |
|
| SRL_STATIC_INLINE bool stack_is_empty(srl_splitter_stack_t * stack); |
| 104 |
|
| SRL_STATIC_INLINE void stack_push(srl_splitter_stack_t * stack, UV val); |
| 105 |
|
| |
| 106 |
|
| |
| 107 |
183722 |
| SRL_STATIC_INLINE void _cat_to_chunk(pTHX_ srl_splitter_t *splitter, char* str, UV str_len) { |
| 108 |
183722 |
| sv_catpvn(splitter->chunk, str, str_len); |
| 109 |
183722 |
| splitter->chunk_current_offset += str_len; |
| 110 |
183722 |
| splitter->chunk_size += str_len; |
| 111 |
183722 |
| } |
| 112 |
|
| |
| 113 |
|
| typedef struct { |
| 114 |
|
| char* key; /* the string to dedup */ |
| 115 |
|
| UV value; /* the position in the chunk */ |
| 116 |
|
| UT_hash_handle hh; /* makes this structure hashable */ |
| 117 |
|
| } dedupe_el_t; |
| 118 |
|
| |
| 119 |
|
| dedupe_el_t *dedupe_hashtable = NULL; /* important! initialize to NULL */ |
| 120 |
|
| dedupe_el_t *dedupe_hashtable_utf8 = NULL; /* important! initialize to NULL */ |
| 121 |
|
| |
| 122 |
|
| typedef struct { |
| 123 |
|
| UV key; /* key, the original offset in input */ |
| 124 |
|
| UV value; /* value, the new offset in the chunk */ |
| 125 |
|
| UT_hash_handle hh; /* makes this structure hashable */ |
| 126 |
|
| } offset_el_t; |
| 127 |
|
| |
| 128 |
|
| offset_el_t *offset_hashtable = NULL; /* important! initialize to NULL */ |
| 129 |
|
| |
| 130 |
251011 |
| SRL_STATIC_INLINE UV stack_pop(srl_splitter_stack_t * stack) { |
| 131 |
251011 |
| UV val = 0; |
| 132 |
251011 |
▶ | if ( stack->top <= 0 ) |
Branch: 1 branch
| Branch | True | False | Total |
| if ( stack->top <= 0 ) |
0 |
251011 |
251011 |
|
| 133 |
0 |
| croak("Stack is empty"); |
| 134 |
251011 |
| val = stack->data[stack->top-1]; |
| 135 |
251011 |
| stack->top--; |
| 136 |
251011 |
| return val; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
221729 |
| SRL_STATIC_INLINE bool stack_is_empty(srl_splitter_stack_t * stack) { |
| 140 |
221729 |
| return (stack->top == 0); |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
251011 |
| SRL_STATIC_INLINE void stack_push(srl_splitter_stack_t * stack, UV val) { |
| 144 |
251011 |
▶ | if (stack->top >= stack->size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (stack->top >= stack->size) { |
0 |
251011 |
251011 |
|
| 145 |
0 |
| UV new_size = stack->size + STACK_SIZE_INCR; |
| 146 |
|
| UV* tmp; |
| 147 |
|
| dTHX; |
| 148 |
0 |
▶ | Newxz(tmp, new_size, UV ); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(tmp, new_size, UV ); |
0 |
0 |
0 |
|
| 149 |
0 |
| memcpy(tmp, stack->data, stack->size * sizeof(UV)); |
| 150 |
0 |
| Safefree(stack->data); |
| 151 |
0 |
| stack->data = tmp; |
| 152 |
0 |
| stack->size = new_size; |
| 153 |
|
| } |
| 154 |
251011 |
| stack->data[stack->top++] = val; |
| 155 |
251011 |
| } |
| 156 |
|
| |
| 157 |
7 |
| srl_splitter_t * srl_build_splitter_struct(pTHX_ HV *opt) { |
| 158 |
|
| srl_splitter_t *splitter; |
| 159 |
|
| SV **svp; |
| 160 |
|
| STRLEN input_len; |
| 161 |
|
| |
| 162 |
7 |
| splitter = srl_empty_splitter_struct(aTHX); |
| 163 |
|
| |
| 164 |
|
| /* load options */ |
| 165 |
7 |
| svp = hv_fetchs(opt, "input", 0); |
| 166 |
7 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
7 |
0 |
7 |
| if (svp && SvOK(*svp)) { |
7 |
0 |
7 |
|
| 167 |
7 |
| splitter->input_str = SvPV(*svp, input_len); |
| 168 |
7 |
| splitter->pos = splitter->input_str; |
| 169 |
7 |
| splitter->input_len = input_len; |
| 170 |
7 |
| splitter->input_str_end = splitter->input_str + input_len; |
| 171 |
7 |
| splitter->input_sv = SvREFCNT_inc(*svp); |
| 172 |
|
| SRL_SPLITTER_TRACE("input_size %" UVuf, input_len); |
| 173 |
|
| } else { |
| 174 |
0 |
| croak ("no input given"); |
| 175 |
|
| } |
| 176 |
7 |
| svp = hv_fetchs(opt, "chunk_size", 0); |
| 177 |
7 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
7 |
0 |
7 |
| if (svp && SvOK(*svp)) { |
7 |
0 |
7 |
|
| 178 |
7 |
| splitter->size_limit = SvUV(*svp); |
| 179 |
|
| SRL_SPLITTER_TRACE("size_limit %" UVuf, splitter->size_limit); |
| 180 |
|
| } |
| 181 |
|
| |
| 182 |
7 |
| splitter->compression_format = 0; |
| 183 |
7 |
| svp = hv_fetchs(opt, "compress", 0); |
| 184 |
7 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
2 |
5 |
7 |
| if (svp && SvOK(*svp)) { |
2 |
0 |
2 |
|
| 185 |
2 |
| IV compression_format = SvIV(*svp); |
| 186 |
|
| |
| 187 |
|
| /* See also Splitter.pm's constants */ |
| 188 |
2 |
| switch (compression_format) { |
| 189 |
0 |
| case 0: |
| 190 |
|
| SRL_SPLITTER_TRACE("no compression %s", ""); |
| 191 |
0 |
| break; |
| 192 |
0 |
| case 1: |
| 193 |
0 |
| croak("incremental snappy compression not yet supported. Try gzip"); |
| 194 |
|
| break; |
| 195 |
2 |
| case 2: |
| 196 |
2 |
| splitter->compression_format = 2; |
| 197 |
|
| SRL_SPLITTER_TRACE("gzip compression %s", ""); |
| 198 |
2 |
| break; |
| 199 |
0 |
| default: |
| 200 |
0 |
| croak("invalid valie for 'compress' parameter"); |
| 201 |
|
| } |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
7 |
| splitter->header_str = NULL; |
| 205 |
7 |
| splitter->header_sv = NULL; |
| 206 |
7 |
| splitter->header_len = 0; |
| 207 |
7 |
| svp = hv_fetchs(opt, "header_data_template", 0); |
| 208 |
7 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
1 |
6 |
7 |
| if (svp && SvOK(*svp)) { |
1 |
0 |
1 |
|
| 209 |
|
| STRLEN header_len; |
| 210 |
1 |
| splitter->header_str = SvPV(*svp, header_len); |
| 211 |
1 |
| splitter->header_sv = SvREFCNT_inc(*svp); |
| 212 |
1 |
| splitter->header_len = header_len; |
| 213 |
|
| SRL_SPLITTER_TRACE("header_data_template found, of length %lu", header_len); |
| 214 |
|
| } |
| 215 |
|
| |
| 216 |
7 |
| splitter->header_count_idx = -1; |
| 217 |
7 |
| svp = hv_fetchs(opt, "header_count_idx", 0); |
| 218 |
7 |
▶ | if (svp && SvOK(*svp)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (svp && SvOK(*svp)) { |
1 |
6 |
7 |
| if (svp && SvOK(*svp)) { |
1 |
0 |
1 |
|
| 219 |
1 |
| splitter->header_count_idx = SvIV(*svp); |
| 220 |
|
| SRL_SPLITTER_TRACE("header_count_idx found, %ld", splitter->header_count_idx); |
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
7 |
| _parse_header(aTHX_ splitter); |
| 224 |
|
| |
| 225 |
|
| /* initialize stacks */ |
| 226 |
|
| srl_splitter_stack_t * status_stack; |
| 227 |
7 |
| Newxz(status_stack, 1, srl_splitter_stack_t ); |
| 228 |
7 |
| Newxz(status_stack->data, STACK_SIZE_INCR, UV ); |
| 229 |
7 |
| status_stack->size = STACK_SIZE_INCR; |
| 230 |
7 |
| status_stack->top = 0; |
| 231 |
7 |
| splitter->status_stack = status_stack; |
| 232 |
|
| |
| 233 |
|
| /* initialize */ |
| 234 |
7 |
| splitter->deepness = 0; |
| 235 |
|
| |
| 236 |
7 |
| char tag = *(splitter->pos); |
| 237 |
7 |
| splitter->pos++; |
| 238 |
7 |
▶ | if (IS_SRL_HDR_ARRAYREF(tag)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (IS_SRL_HDR_ARRAYREF(tag)) { |
6 |
1 |
7 |
|
| 239 |
6 |
| int len = tag & 0xF; |
| 240 |
6 |
| splitter->input_nb_elts = len; |
| 241 |
|
| SRL_SPLITTER_TRACE(" * ARRAYREF of len, %d", len); |
| 242 |
18 |
▶ | while (len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len-- > 0) { |
12 |
6 |
18 |
|
| 243 |
12 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 244 |
|
| } |
| 245 |
1 |
▶ | } else if (tag == SRL_HDR_REFN) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (tag == SRL_HDR_REFN) { |
1 |
0 |
1 |
|
| 246 |
1 |
| tag = *(splitter->pos); |
| 247 |
1 |
| splitter->pos++; |
| 248 |
1 |
▶ | if (tag == SRL_HDR_ARRAY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tag == SRL_HDR_ARRAY) { |
1 |
0 |
1 |
|
| 249 |
1 |
| UV len = _read_varint_uv_nocheck(splitter); |
| 250 |
1 |
| splitter->input_nb_elts = len; |
| 251 |
|
| SRL_SPLITTER_TRACE(" * ARRAY of len, %lu", len); |
| 252 |
132 |
▶ | while (len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len-- > 0) { |
131 |
1 |
132 |
|
| 253 |
131 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 254 |
|
| } |
| 255 |
|
| } else { |
| 256 |
0 |
| croak("first tag is REFN but next tag is not ARRAY"); |
| 257 |
|
| } |
| 258 |
|
| } else { |
| 259 |
0 |
| croak("first tag is not an ArrayRef"); |
| 260 |
|
| } |
| 261 |
|
| |
| 262 |
|
| /* now splitter->pos is on the first array element */ |
| 263 |
7 |
| return splitter; |
| 264 |
|
| } |
| 265 |
|
| |
| 266 |
|
| |
| 267 |
7 |
| void srl_destroy_splitter(pTHX_ srl_splitter_t *splitter) { |
| 268 |
7 |
| _empty_hashes(aTHX); |
| 269 |
7 |
| SvREFCNT_dec(splitter->input_sv); |
| 270 |
7 |
▶ | if (splitter->header_sv != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter->header_sv != NULL) |
1 |
6 |
7 |
|
| 271 |
1 |
| SvREFCNT_dec(splitter->header_sv); |
| 272 |
7 |
▶ | if (splitter->status_stack->data != NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter->status_stack->data != NULL) |
7 |
0 |
7 |
|
| 273 |
7 |
| Safefree(splitter->status_stack->data); |
| 274 |
7 |
| Safefree(splitter); |
| 275 |
7 |
| } |
| 276 |
|
| |
| 277 |
7 |
| SRL_STATIC_INLINE srl_splitter_t * srl_empty_splitter_struct(pTHX) { |
| 278 |
7 |
| srl_splitter_t *splitter = NULL; |
| 279 |
7 |
| Newx(splitter, 1, srl_splitter_t); |
| 280 |
7 |
▶ | if (splitter == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter == NULL) { |
0 |
7 |
7 |
|
| 281 |
0 |
| croak("Out of memory"); |
| 282 |
|
| } |
| 283 |
7 |
| return splitter; |
| 284 |
|
| } |
| 285 |
|
| |
| 286 |
7 |
| void _parse_header(pTHX_ srl_splitter_t *splitter) { |
| 287 |
7 |
| int magic_string = 1; |
| 288 |
7 |
| int high_magic_string = 1; |
| 289 |
|
| |
| 290 |
|
| U8 version_encoding; |
| 291 |
|
| U8 version; |
| 292 |
|
| U8 encoding_flags; |
| 293 |
|
| UV header_len; |
| 294 |
|
| |
| 295 |
7 |
| int is_zlib_encoded = 0; |
| 296 |
7 |
| int is_snappy_encoded = 0; |
| 297 |
7 |
| int is_snappyincr_encoded = 0; |
| 298 |
|
| |
| 299 |
|
| // SRL_MAGIC_STRLEN + PROTOCOL_LENGTH + OPTIONAL-HEADER-SIZE(at least 1 byte) + DATA(at least 1 byte) |
| 300 |
7 |
▶ | if (splitter->input_len < SRL_MAGIC_STRLEN + 1 + 1 + 1){ |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter->input_len < SRL_MAGIC_STRLEN + 1 + 1 + 1){ |
0 |
7 |
7 |
|
| 301 |
0 |
| croak("input Sereal string lacks data"); |
| 302 |
7 |
▶ | } else if ( (high_magic_string = strncmp(splitter->input_str, SRL_MAGIC_STRING, SRL_MAGIC_STRLEN)) |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( (high_magic_string = strncmp(splitter->input_str, SRL_MAGIC_STRING, SRL_MAGIC_STRLEN)) |
7 |
0 |
7 |
|
| 303 |
7 |
▶ | && (magic_string = strncmp(splitter->input_str, SRL_MAGIC_STRING_HIGHBIT, SRL_MAGIC_STRLEN)) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| && (magic_string = strncmp(splitter->input_str, SRL_MAGIC_STRING_HIGHBIT, SRL_MAGIC_STRLEN)) ) { |
0 |
7 |
7 |
|
| 304 |
0 |
| croak("input Sereal string has wrong Sereal magic"); |
| 305 |
|
| } |
| 306 |
|
| |
| 307 |
7 |
| splitter->pos += SRL_MAGIC_STRLEN; |
| 308 |
|
| |
| 309 |
7 |
| version_encoding = (U8)*(splitter->pos); |
| 310 |
7 |
| version = (U8)(version_encoding & SRL_PROTOCOL_VERSION_MASK); |
| 311 |
7 |
| encoding_flags = (U8)(version_encoding & SRL_PROTOCOL_ENCODING_MASK); |
| 312 |
|
| |
| 313 |
7 |
▶ | if ( version <= 0 |
Branch: 1 branch
| Branch | True | False | Total |
| if ( version <= 0 |
7 |
0 |
7 |
|
| 314 |
7 |
▶ | || ( version < 3 && high_magic_string ) |
Branch: 2 branches
| Branch | True | False | Total |
| || ( version < 3 && high_magic_string ) |
0 |
7 |
7 |
| || ( version < 3 && high_magic_string ) |
0 |
0 |
0 |
|
| 315 |
7 |
▶ | || ( version > 2 && magic_string ) ) { |
Branch: 2 branches
| Branch | True | False | Total |
| || ( version > 2 && magic_string ) ) { |
7 |
0 |
7 |
| || ( version > 2 && magic_string ) ) { |
0 |
7 |
7 |
|
| 316 |
0 |
| croak("unsupported Sereal versions/protocol"); |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
7 |
| switch(encoding_flags) { |
| 320 |
|
| |
| 321 |
7 |
| case SRL_PROTOCOL_ENCODING_RAW: |
| 322 |
|
| /* no op */ |
| 323 |
|
| SRL_SPLITTER_TRACE("encoding is raw %s", ""); |
| 324 |
7 |
| break; |
| 325 |
|
| |
| 326 |
0 |
| case SRL_PROTOCOL_ENCODING_SNAPPY: |
| 327 |
|
| SRL_SPLITTER_TRACE("encoding is snappy %s", ""); |
| 328 |
0 |
| is_snappy_encoded = 1; |
| 329 |
0 |
| break; |
| 330 |
|
| |
| 331 |
0 |
| case SRL_PROTOCOL_ENCODING_SNAPPY_INCREMENTAL: |
| 332 |
|
| SRL_SPLITTER_TRACE("encoding is snappy incr %s", ""); |
| 333 |
0 |
| is_snappy_encoded = is_snappyincr_encoded = 1; |
| 334 |
0 |
| break; |
| 335 |
|
| |
| 336 |
0 |
| case SRL_PROTOCOL_ENCODING_ZLIB: |
| 337 |
|
| SRL_SPLITTER_TRACE("encoding is zlib %s", ""); |
| 338 |
0 |
| is_zlib_encoded = 1; |
| 339 |
0 |
| break; |
| 340 |
|
| |
| 341 |
0 |
| default: |
| 342 |
0 |
| croak("Sereal document encoded in an unknown format"); |
| 343 |
|
| } |
| 344 |
|
| |
| 345 |
|
| SRL_SPLITTER_TRACE("header version is %hhu", version); |
| 346 |
|
| |
| 347 |
|
| // move after protocol version |
| 348 |
7 |
| splitter->pos += 1; |
| 349 |
|
| |
| 350 |
7 |
| header_len= _read_varint_uv_nocheck(splitter); |
| 351 |
|
| |
| 352 |
|
| SRL_SPLITTER_TRACE("header len is %lu", header_len); |
| 353 |
|
| |
| 354 |
|
| //TODO: add code for processing the header |
| 355 |
7 |
| splitter->pos += header_len; |
| 356 |
|
| |
| 357 |
7 |
▶ | if (version < 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (version < 2) { |
0 |
7 |
7 |
|
| 358 |
0 |
| splitter->input_body_pos = splitter->input_str; |
| 359 |
|
| } else { |
| 360 |
7 |
| splitter->input_body_pos = splitter->pos; |
| 361 |
|
| } |
| 362 |
|
| |
| 363 |
7 |
▶ | if (is_snappy_encoded) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_snappy_encoded) { |
0 |
7 |
7 |
|
| 364 |
|
| UV compressed_len; |
| 365 |
|
| uint32_t uncompressed_len; |
| 366 |
|
| int decompress_ok; |
| 367 |
|
| char * new_input_str; |
| 368 |
|
| |
| 369 |
0 |
▶ | if (is_snappyincr_encoded) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_snappyincr_encoded) { |
0 |
0 |
0 |
|
| 370 |
0 |
| compressed_len = _read_varint_uv_nocheck(splitter); |
| 371 |
|
| } else { |
| 372 |
0 |
| compressed_len = splitter->input_len - (splitter->pos - splitter->input_str); |
| 373 |
|
| } |
| 374 |
|
| SRL_SPLITTER_TRACE("snappy compressed len %"UVuf, compressed_len); |
| 375 |
|
| // splitter->pos is now at start of compressed payload |
| 376 |
|
| |
| 377 |
|
| int snappy_header_len; |
| 378 |
|
| char *old_pos; |
| 379 |
0 |
| old_pos = splitter->pos; |
| 380 |
0 |
| snappy_header_len = csnappy_get_uncompressed_length( |
| 381 |
|
| (char *)old_pos, |
| 382 |
|
| compressed_len, |
| 383 |
|
| &uncompressed_len |
| 384 |
|
| ); |
| 385 |
0 |
▶ | if (snappy_header_len == CSNAPPY_E_HEADER_BAD) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (snappy_header_len == CSNAPPY_E_HEADER_BAD) { |
0 |
0 |
0 |
|
| 386 |
0 |
| croak("invalid Snappy header in Snappy-compressed Sereal packet"); |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
|
| // allocate a new SV for uncompressed data |
| 390 |
0 |
| sv_2mortal(splitter->input_sv); |
| 391 |
0 |
| splitter->input_sv = newSVpvs(""); |
| 392 |
0 |
▶ | new_input_str = SvGROW(splitter->input_sv, uncompressed_len); |
Branch: 2 branches
| Branch | True | False | Total |
| new_input_str = SvGROW(splitter->input_sv, uncompressed_len); |
0 |
0 |
0 |
| new_input_str = SvGROW(splitter->input_sv, uncompressed_len); |
0 |
0 |
0 |
|
| 393 |
|
| |
| 394 |
0 |
| decompress_ok = csnappy_decompress_noheader((char *) (old_pos + snappy_header_len), |
| 395 |
|
| compressed_len - snappy_header_len, |
| 396 |
|
| (char *) new_input_str, |
| 397 |
|
| &uncompressed_len); |
| 398 |
0 |
▶ | if ( decompress_ok != 0 ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( decompress_ok != 0 ) { |
0 |
0 |
0 |
|
| 399 |
0 |
| croak("Snappy decompression of Sereal packet payload failed"); |
| 400 |
|
| } |
| 401 |
|
| |
| 402 |
0 |
| splitter->input_str = new_input_str; |
| 403 |
|
| SRL_SPLITTER_TRACE(" decompress OK: uncompressed length: %d\n", uncompressed_len); |
| 404 |
|
| |
| 405 |
0 |
| splitter->pos = splitter->input_str;; |
| 406 |
0 |
| splitter->input_len = uncompressed_len; |
| 407 |
0 |
| splitter->input_body_pos = splitter->pos; |
| 408 |
|
| |
| 409 |
7 |
▶ | } else if (is_zlib_encoded) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (is_zlib_encoded) { |
0 |
7 |
7 |
|
| 410 |
|
| |
| 411 |
0 |
| UV uncompressed_len = _read_varint_uv_nocheck(splitter); |
| 412 |
0 |
| UV compressed_len = _read_varint_uv_nocheck(splitter); |
| 413 |
|
| char * new_input_str; |
| 414 |
|
| |
| 415 |
|
| // splitter->pos is now at start of compressed payload |
| 416 |
|
| SRL_SPLITTER_TRACE("unzipping %s", ""); |
| 417 |
|
| SRL_SPLITTER_TRACE("compressed_len : %" UVuf, compressed_len); |
| 418 |
|
| SRL_SPLITTER_TRACE("uncompressed_len : %" UVuf, uncompressed_len); |
| 419 |
|
| |
| 420 |
|
| |
| 421 |
0 |
| mz_ulong tmp = uncompressed_len; |
| 422 |
|
| |
| 423 |
|
| // allocate a new SV for uncompressed data |
| 424 |
0 |
| sv_2mortal(splitter->input_sv); |
| 425 |
0 |
| splitter->input_sv = newSVpvs(""); |
| 426 |
0 |
▶ | new_input_str = SvGROW(splitter->input_sv, uncompressed_len); |
Branch: 2 branches
| Branch | True | False | Total |
| new_input_str = SvGROW(splitter->input_sv, uncompressed_len); |
0 |
0 |
0 |
| new_input_str = SvGROW(splitter->input_sv, uncompressed_len); |
0 |
0 |
0 |
|
| 427 |
|
| |
| 428 |
0 |
| char *compressed = splitter->pos; |
| 429 |
|
| |
| 430 |
0 |
| int decompress_ok = mz_uncompress( (unsigned char *) new_input_str, |
| 431 |
|
| &tmp, |
| 432 |
|
| (const unsigned char *) compressed, |
| 433 |
|
| compressed_len ); |
| 434 |
|
| |
| 435 |
0 |
▶ | if (decompress_ok != Z_OK) |
Branch: 1 branch
| Branch | True | False | Total |
| if (decompress_ok != Z_OK) |
0 |
0 |
0 |
|
| 436 |
0 |
| croak("ZLIB decompression of Sereal packet payload failed"); |
| 437 |
|
| |
| 438 |
0 |
| splitter->input_str = new_input_str; |
| 439 |
|
| SRL_SPLITTER_TRACE(" decompress OK: length %lu\n", uncompressed_len); |
| 440 |
|
| |
| 441 |
0 |
| splitter->pos = splitter->input_str; |
| 442 |
0 |
| splitter->input_len = (STRLEN)tmp; |
| 443 |
0 |
| splitter->input_body_pos = splitter->pos; |
| 444 |
|
| |
| 445 |
|
| } |
| 446 |
7 |
| } |
| 447 |
|
| |
| 448 |
33 |
| SRL_STATIC_INLINE int _parse(pTHX_ srl_splitter_t * splitter) { |
| 449 |
|
| |
| 450 |
|
| char tag; |
| 451 |
|
| |
| 452 |
221762 |
▶ | while( ! stack_is_empty(splitter->status_stack) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while( ! stack_is_empty(splitter->status_stack) ) { |
221718 |
11 |
221729 |
|
| 453 |
221718 |
| UV status = stack_pop(splitter->status_stack); |
| 454 |
|
| UV absolute_offset; |
| 455 |
|
| |
| 456 |
|
| SRL_SPLITTER_TRACE("* ITERATING -- deepness value: %d", splitter->deepness); |
| 457 |
221718 |
| switch(status) { |
| 458 |
44967 |
| case ST_DEEPNESS_UP: |
| 459 |
44967 |
| splitter->deepness--; |
| 460 |
|
| SRL_SPLITTER_TRACE(" * DEEPNESS UP -- deepness value: %d", splitter->deepness); |
| 461 |
44967 |
| break; |
| 462 |
147458 |
| case ST_VALUE: |
| 463 |
147458 |
| tag = *(splitter->pos); |
| 464 |
|
| SRL_SPLITTER_TRACE(" * VALUE tag %d -- deepness value: %d", tag, splitter->deepness); |
| 465 |
147458 |
▶ | if (tag & SRL_HDR_TRACK_FLAG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tag & SRL_HDR_TRACK_FLAG) { |
2 |
147456 |
147458 |
|
| 466 |
2 |
| tag = tag & ~SRL_HDR_TRACK_FLAG; |
| 467 |
|
| SRL_SPLITTER_TRACE(" * tag must be tracked, %ld\n", splitter->pos - splitter->input_body_pos); |
| 468 |
|
| |
| 469 |
2 |
| offset_el_t *element = NULL; |
| 470 |
|
| |
| 471 |
2 |
| UV origin_offset = splitter->pos - splitter->input_body_pos + 1; |
| 472 |
2 |
| UV new_offset = splitter->chunk_current_offset + (splitter->pos - splitter->chunk_iter_start); |
| 473 |
|
| |
| 474 |
2 |
▶ | HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
Branch: 7 branches
| Branch | True | False | Total |
| HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
0 |
2 |
2 |
| HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &origin_offset, sizeof(UV), element); |
0 |
0 |
0 |
|
| 475 |
|
| |
| 476 |
2 |
▶ | if(element == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(element == NULL) { |
2 |
0 |
2 |
|
| 477 |
2 |
| Newx(element, 1, offset_el_t); |
| 478 |
2 |
| element->key = origin_offset; |
| 479 |
2 |
| element->value = new_offset; |
| 480 |
|
| SRL_SPLITTER_TRACE(" * adding %lu -> %lu\n", element->key, element->value); |
| 481 |
2 |
▶ | HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
Branch: 15 branches
| Branch | True | False | Total |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
2 |
0 |
2 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
2 |
2 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
2 |
2 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
2 |
2 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
2 |
2 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
2 |
2 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, offset_hashtable, &(element->key), sizeof(UV), element); |
0 |
0 |
0 |
|
| 482 |
|
| } |
| 483 |
|
| } |
| 484 |
147458 |
| splitter->pos++; |
| 485 |
147458 |
| _read_tag(aTHX_ splitter, tag); |
| 486 |
147458 |
| break; |
| 487 |
29293 |
| case ST_ABSOLUTE_JUMP: |
| 488 |
|
| /* before jumping, flush the chunk */ |
| 489 |
29293 |
| _maybe_flush_chunk(aTHX_ splitter, NULL, NULL); |
| 490 |
29293 |
| absolute_offset = stack_pop(splitter->status_stack); |
| 491 |
|
| SRL_SPLITTER_TRACE(" * ABSOLUTE_JUMP to %lu", (UV) ( (char*)absolute_offset - splitter->input_str ) ); |
| 492 |
29293 |
| splitter->pos = (char*) absolute_offset; |
| 493 |
29293 |
| splitter->chunk_iter_start = splitter->pos; |
| 494 |
29293 |
| break; |
| 495 |
0 |
| default: |
| 496 |
0 |
| croak("unknown stack value %lu", status); |
| 497 |
|
| } |
| 498 |
221718 |
▶ | if ( splitter->deepness == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( splitter->deepness == 0) { |
143 |
221575 |
221718 |
|
| 499 |
|
| /* Here it means we have properly parsed a full VALUE, so we have |
| 500 |
|
| an additional array element in our chunk */ |
| 501 |
143 |
| splitter->chunk_nb_elts++; |
| 502 |
143 |
| if ( (UV)(splitter->chunk_size + |
| 503 |
143 |
▶ | splitter->pos - splitter->chunk_iter_start) >= splitter->size_limit) { |
Branch: 1 branch
| Branch | True | False | Total |
| splitter->pos - splitter->chunk_iter_start) >= splitter->size_limit) { |
22 |
121 |
143 |
|
| 504 |
22 |
| _maybe_flush_chunk(aTHX_ splitter, NULL, NULL); |
| 505 |
22 |
| return 1; |
| 506 |
|
| } |
| 507 |
|
| } |
| 508 |
|
| } |
| 509 |
|
| SRL_SPLITTER_TRACE("* END ITERATING (deepness value: %d)", splitter->deepness); |
| 510 |
11 |
▶ | if (splitter->deepness != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter->deepness != 0) |
0 |
11 |
11 |
|
| 511 |
0 |
| croak("Something wrong happens: parsing finished but deepness is not zero"); |
| 512 |
|
| |
| 513 |
|
| /* iteration is finished, if we had to flush something return success */ |
| 514 |
11 |
▶ | if (_maybe_flush_chunk(aTHX_ splitter, NULL, NULL)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (_maybe_flush_chunk(aTHX_ splitter, NULL, NULL)) |
3 |
8 |
11 |
|
| 515 |
3 |
| return 1; |
| 516 |
|
| |
| 517 |
|
| /* maybe we didn't have to flush but the chunk is not empty: return success */ |
| 518 |
8 |
▶ | if (splitter->chunk_size > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter->chunk_size > 0) |
1 |
7 |
8 |
|
| 519 |
1 |
| return 1; |
| 520 |
|
| |
| 521 |
|
| /* otherwise, no data anymore, return failure */ |
| 522 |
7 |
| return 0; |
| 523 |
|
| } |
| 524 |
|
| |
| 525 |
95590 |
| void _check_for_duplicates(pTHX_ srl_splitter_t * splitter, char* binary_start_pos, UV len, bool is_utf8) { |
| 526 |
95590 |
| dedupe_el_t *element = NULL; |
| 527 |
95590 |
▶ | if (is_utf8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_utf8) { |
0 |
95590 |
95590 |
|
| 528 |
0 |
▶ | HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
Branch: 7 branches
| Branch | True | False | Total |
| HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_FIND(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
|
| 529 |
|
| } else { |
| 530 |
514125 |
▶ | HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
Branch: 7 branches
| Branch | True | False | Total |
| HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
95564 |
26 |
95590 |
| HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
197769 |
95564 |
293333 |
| HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
94424 |
1140 |
95564 |
| HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
291141 |
3827 |
294968 |
| HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
74202 |
216939 |
291141 |
| HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
200544 |
20222 |
220766 |
| HASH_FIND(hh, dedupe_hashtable, splitter->pos, len, element); |
294968 |
21362 |
316330 |
|
| 531 |
|
| } |
| 532 |
95590 |
▶ | if ( element != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( element != NULL) { |
74202 |
21388 |
95590 |
|
| 533 |
|
| SRL_SPLITTER_TRACE(" * FOUND DEDUP key %s value %lu", element->key, element->value); |
| 534 |
74202 |
| _maybe_flush_chunk(aTHX_ splitter, binary_start_pos, splitter->pos + len); |
| 535 |
|
| |
| 536 |
|
| /* the copy tag */ |
| 537 |
|
| char tmp[SRL_MAX_VARINT_LENGTH]; |
| 538 |
74202 |
| tmp[0] = 0x2f; |
| 539 |
74202 |
| _cat_to_chunk(aTHX_ splitter, tmp, 1 ); |
| 540 |
|
| |
| 541 |
74202 |
| UV len = (UV) (_set_varint_nocheck(tmp, element->value) - tmp); |
| 542 |
74202 |
| _cat_to_chunk(aTHX_ splitter, tmp, len); |
| 543 |
|
| |
| 544 |
|
| } else { |
| 545 |
21388 |
| UV offset = splitter->chunk_current_offset + ( binary_start_pos - splitter->chunk_iter_start); |
| 546 |
21388 |
| Newx(element, 1, dedupe_el_t); |
| 547 |
21388 |
| element->key = ""; |
| 548 |
21388 |
| element->value = offset; |
| 549 |
|
| SRL_SPLITTER_TRACE(" * ADDED DEDUP offset %lu", offset); |
| 550 |
21388 |
▶ | if (is_utf8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_utf8) { |
0 |
21388 |
21388 |
|
| 551 |
0 |
▶ | HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
Branch: 15 branches
| Branch | True | False | Total |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable_utf8, splitter->pos, len, element); |
0 |
0 |
0 |
|
| 552 |
|
| } else { |
| 553 |
101242 |
▶ | HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
Branch: 15 branches
| Branch | True | False | Total |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
26 |
21362 |
21388 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
0 |
26 |
26 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
0 |
26 |
26 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
48915 |
21388 |
70303 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
20222 |
1166 |
21388 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
58 |
21330 |
21388 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
58 |
0 |
58 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
0 |
58 |
58 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
58 |
0 |
58 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
4267 |
21616 |
25883 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
16583 |
9300 |
25883 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
25883 |
5056 |
30939 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
5056 |
58 |
5114 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
0 |
58 |
58 |
| HASH_ADD_KEYPTR(hh, dedupe_hashtable, splitter->pos, len, element); |
0 |
58 |
58 |
|
| 554 |
|
| } |
| 555 |
|
| } |
| 556 |
|
| |
| 557 |
95590 |
| splitter->pos += len; |
| 558 |
95590 |
| return; |
| 559 |
|
| } |
| 560 |
|
| |
| 561 |
|
| void |
| 562 |
147458 |
| _read_tag(pTHX_ srl_splitter_t * splitter, char tag) |
| 563 |
|
| { |
| 564 |
|
| /* first, self-contained tags*/ |
| 565 |
147458 |
▶ | if ( tag <= SRL_HDR_POS_HIGH ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( tag <= SRL_HDR_POS_HIGH ) { |
147447 |
11 |
147458 |
|
| 566 |
|
| SRL_SPLITTER_TRACE(" * POS INTEGER %d", (int)tag); |
| 567 |
147447 |
▶ | } else if ( tag <= SRL_HDR_NEG_HIGH) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( tag <= SRL_HDR_NEG_HIGH) { |
147447 |
0 |
147447 |
|
| 568 |
|
| SRL_SPLITTER_TRACE(" * NEG INTEGER %d", (int)tag - 32); |
| 569 |
147447 |
▶ | } else if ( IS_SRL_HDR_SHORT_BINARY(tag) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( IS_SRL_HDR_SHORT_BINARY(tag) ) { |
13 |
147434 |
147447 |
|
| 570 |
13 |
| UV len = SRL_HDR_SHORT_BINARY_LEN_FROM_TAG(tag); |
| 571 |
|
| SRL_SPLITTER_TRACE(" * SHORT BINARY of length %lu", len); |
| 572 |
13 |
| char *binary_start_pos = splitter->pos - 1; |
| 573 |
13 |
| _check_for_duplicates(aTHX_ splitter, binary_start_pos, len, 0); |
| 574 |
147434 |
▶ | } else if ( IS_SRL_HDR_HASHREF(tag) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( IS_SRL_HDR_HASHREF(tag) ) { |
4555 |
142879 |
147434 |
|
| 575 |
4555 |
| int len = tag & 0xF; |
| 576 |
|
| SRL_SPLITTER_TRACE(" * SHORT HASHREF of length %d", len); |
| 577 |
4555 |
| splitter->deepness++; |
| 578 |
4555 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 579 |
17848 |
▶ | while (len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len-- > 0) { |
13293 |
4555 |
17848 |
|
| 580 |
13293 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 581 |
13293 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 582 |
|
| } |
| 583 |
142879 |
▶ | } else if ( IS_SRL_HDR_ARRAYREF(tag) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if ( IS_SRL_HDR_ARRAYREF(tag) ) { |
10180 |
132699 |
142879 |
|
| 584 |
10180 |
| int len = tag & 0xF; |
| 585 |
|
| SRL_SPLITTER_TRACE(" * SHORT ARRAY of length %d", len); |
| 586 |
10180 |
| splitter->deepness++; |
| 587 |
10180 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 588 |
64586 |
▶ | while (len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len-- > 0) { |
54406 |
10180 |
64586 |
|
| 589 |
54406 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 590 |
|
| } |
| 591 |
|
| } else { |
| 592 |
132699 |
| switch (tag) { |
| 593 |
0 |
| case SRL_HDR_VARINT: _read_varint(splitter); break; |
| 594 |
0 |
| case SRL_HDR_ZIGZAG: _read_zigzag(splitter); break; |
| 595 |
0 |
| case SRL_HDR_FLOAT: _read_float(splitter); break; |
| 596 |
0 |
| case SRL_HDR_DOUBLE: _read_double(splitter); break; |
| 597 |
0 |
| case SRL_HDR_LONG_DOUBLE: _read_long_double(splitter); break; |
| 598 |
0 |
| case SRL_HDR_TRUE: /* no op */ break; |
| 599 |
0 |
| case SRL_HDR_FALSE: /* no op */ break; |
| 600 |
6888 |
| case SRL_HDR_CANONICAL_UNDEF: |
| 601 |
6888 |
| case SRL_HDR_UNDEF: /* no op */ break; |
| 602 |
95577 |
| case SRL_HDR_BINARY: _read_string(aTHX_ splitter, 0); break; |
| 603 |
0 |
| case SRL_HDR_STR_UTF8: _read_string(aTHX_ splitter, 1); break; |
| 604 |
0 |
| case SRL_HDR_WEAKEN: _read_weaken(splitter); break; |
| 605 |
469 |
| case SRL_HDR_REFN: _read_refn(splitter); break; |
| 606 |
2 |
| case SRL_HDR_REFP: _read_refp(aTHX_ splitter); break; |
| 607 |
444 |
| case SRL_HDR_HASH: _read_hash(splitter); break; |
| 608 |
25 |
| case SRL_HDR_ARRAY: _read_array(splitter); break; |
| 609 |
1 |
| case SRL_HDR_OBJECT: _read_object(splitter, 0); break; |
| 610 |
0 |
| case SRL_HDR_OBJECT_FREEZE: _read_object(splitter, 1); break; |
| 611 |
1 |
| case SRL_HDR_OBJECTV: _read_objectv(aTHX_ splitter, 0); break; |
| 612 |
0 |
| case SRL_HDR_OBJECTV_FREEZE: _read_objectv(aTHX_ splitter, 1); break; |
| 613 |
0 |
| case SRL_HDR_ALIAS: _read_alias(aTHX_ splitter); break; |
| 614 |
29292 |
| case SRL_HDR_COPY: _read_copy(aTHX_ splitter); break; |
| 615 |
0 |
| case SRL_HDR_EXTEND: /* no op */ break; |
| 616 |
0 |
| case SRL_HDR_REGEXP: _read_regexp(splitter); break; |
| 617 |
0 |
| case SRL_HDR_PAD: /* no op */ break; |
| 618 |
0 |
| default: croak("Unexpected tag value"); break; |
| 619 |
|
| } |
| 620 |
|
| } |
| 621 |
147458 |
| } |
| 622 |
|
| |
| 623 |
|
| |
| 624 |
0 |
| SRL_STATIC_INLINE void _read_varint(srl_splitter_t * splitter) { |
| 625 |
0 |
| _read_varint_uv_nocheck(splitter); |
| 626 |
|
| SRL_SPLITTER_TRACE(" * VARINT %s", ""); |
| 627 |
0 |
| } |
| 628 |
|
| |
| 629 |
0 |
| SRL_STATIC_INLINE void _read_zigzag(srl_splitter_t * splitter) { |
| 630 |
0 |
| _read_varint_uv_nocheck(splitter); |
| 631 |
|
| SRL_SPLITTER_TRACE(" * ZIGZAG %s", ""); |
| 632 |
0 |
| } |
| 633 |
|
| |
| 634 |
0 |
| SRL_STATIC_INLINE void _read_float(srl_splitter_t * splitter) { |
| 635 |
|
| SRL_SPLITTER_TRACE(" * FLOAT %s", ""); |
| 636 |
0 |
| splitter->pos += sizeof(float); |
| 637 |
0 |
| } |
| 638 |
|
| |
| 639 |
0 |
| SRL_STATIC_INLINE void _read_double(srl_splitter_t * splitter) { |
| 640 |
|
| SRL_SPLITTER_TRACE(" * DOUBLE %s", ""); |
| 641 |
0 |
| splitter->pos += sizeof(double); |
| 642 |
0 |
| } |
| 643 |
|
| |
| 644 |
0 |
| SRL_STATIC_INLINE void _read_long_double(srl_splitter_t * splitter) { |
| 645 |
|
| SRL_SPLITTER_TRACE(" * LONG DOUBLE %s", ""); |
| 646 |
0 |
| splitter->pos += sizeof(long double); |
| 647 |
0 |
| } |
| 648 |
|
| |
| 649 |
95577 |
| SRL_STATIC_INLINE void _read_string(pTHX_ srl_splitter_t * splitter, bool is_utf8) { |
| 650 |
95577 |
| char *binary_start_pos = splitter->pos - 1; |
| 651 |
95577 |
| UV len = _read_varint_uv_nocheck(splitter); |
| 652 |
|
| SRL_SPLITTER_TRACE(" * STRING of length %lu", len); |
| 653 |
95577 |
| _check_for_duplicates(aTHX_ splitter, binary_start_pos, len, is_utf8); |
| 654 |
95577 |
| } |
| 655 |
|
| |
| 656 |
0 |
| SRL_STATIC_INLINE void _read_weaken(srl_splitter_t * splitter) { |
| 657 |
|
| SRL_SPLITTER_TRACE(" * WEAKEN, %s", ""); |
| 658 |
0 |
| splitter->deepness++; |
| 659 |
0 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 660 |
0 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 661 |
0 |
| } |
| 662 |
|
| |
| 663 |
469 |
| SRL_STATIC_INLINE void _read_refn(srl_splitter_t * splitter) { |
| 664 |
|
| SRL_SPLITTER_TRACE(" * REFN, %s", ""); |
| 665 |
469 |
| splitter->deepness++; |
| 666 |
469 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 667 |
469 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 668 |
469 |
| } |
| 669 |
|
| |
| 670 |
2 |
| SRL_STATIC_INLINE void _read_refp(pTHX_ srl_splitter_t * splitter) { |
| 671 |
|
| /* we save the position at the refp tag */ |
| 672 |
2 |
| char* saved_pos = splitter->pos - 1; |
| 673 |
2 |
| UV offset = _read_varint_uv_nocheck(splitter); |
| 674 |
|
| |
| 675 |
2 |
▶ | if (offset == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset == 0) |
0 |
2 |
2 |
|
| 676 |
0 |
| croak("REFP offset is zero !"); |
| 677 |
|
| |
| 678 |
|
| SRL_SPLITTER_TRACE(" * REFP, must jump to offset %lu, from input_body_pos %lu, then back here %lu.", |
| 679 |
|
| offset, |
| 680 |
|
| splitter->input_body_pos - splitter->input_str, |
| 681 |
|
| splitter->pos - splitter->input_str); |
| 682 |
|
| |
| 683 |
|
| /* if we have to flush the chunk first, let's do it, until before the REFP tag */ |
| 684 |
2 |
| _maybe_flush_chunk(aTHX_ splitter, saved_pos, NULL); |
| 685 |
|
| |
| 686 |
|
| /* search in the mapping hash */ |
| 687 |
2 |
| offset_el_t *element = NULL; |
| 688 |
2 |
▶ | HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
Branch: 7 branches
| Branch | True | False | Total |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
2 |
0 |
2 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
2 |
2 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
2 |
0 |
2 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
2 |
0 |
2 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
2 |
0 |
2 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
2 |
0 |
2 |
|
| 689 |
2 |
▶ | if (element != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (element != NULL) { |
2 |
0 |
2 |
|
| 690 |
2 |
| UV new_offset = element->value; |
| 691 |
|
| /* insert a refp */ |
| 692 |
|
| char tmp_str[SRL_MAX_VARINT_LENGTH]; |
| 693 |
2 |
| tmp_str[0] = 0x29; |
| 694 |
2 |
| _cat_to_chunk(aTHX_ splitter, tmp_str, 1 ); |
| 695 |
|
| |
| 696 |
|
| /* append the offset as a varint */ |
| 697 |
2 |
| UV varint_len = (UV) (_set_varint_nocheck(tmp_str, new_offset) - tmp_str); |
| 698 |
2 |
| _cat_to_chunk(aTHX_ splitter, tmp_str, varint_len); |
| 699 |
|
| } else { |
| 700 |
|
| |
| 701 |
|
| /* otherwise insert an refn tag instead of the refp */ |
| 702 |
|
| char tmp_str[1]; |
| 703 |
0 |
| tmp_str[0] = 0x28; |
| 704 |
0 |
| _cat_to_chunk(aTHX_ splitter, tmp_str, 1 ); |
| 705 |
|
| |
| 706 |
0 |
| splitter->deepness++; |
| 707 |
0 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 708 |
|
| |
| 709 |
0 |
| stack_push(splitter->status_stack, (UV)splitter->pos); |
| 710 |
0 |
| stack_push(splitter->status_stack, ST_ABSOLUTE_JUMP); |
| 711 |
|
| |
| 712 |
0 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 713 |
|
| |
| 714 |
|
| /* then do the jump */ |
| 715 |
0 |
| char * landing_pos = splitter->input_body_pos + offset - 1; |
| 716 |
0 |
| splitter->pos = landing_pos; |
| 717 |
0 |
| splitter->chunk_iter_start = landing_pos; |
| 718 |
|
| } |
| 719 |
2 |
| } |
| 720 |
|
| |
| 721 |
1 |
| SRL_STATIC_INLINE void _read_object(srl_splitter_t * splitter, bool is_freeze) { |
| 722 |
|
| SRL_SPLITTER_TRACE(" * OBJECT%s, %s", (is_freeze ? "FREEZE" : ""), ""); |
| 723 |
1 |
| splitter->deepness++; |
| 724 |
1 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 725 |
1 |
| stack_push(splitter->status_stack, ST_VALUE); /* for the class name */ |
| 726 |
1 |
| stack_push(splitter->status_stack, ST_VALUE); /* for the object struct */ |
| 727 |
1 |
| } |
| 728 |
|
| |
| 729 |
1 |
| SRL_STATIC_INLINE void _read_objectv(pTHX_ srl_splitter_t * splitter, bool is_freeze) { |
| 730 |
|
| /* we save the position at the objectv tag */ |
| 731 |
1 |
| char* saved_pos = splitter->pos - 1; |
| 732 |
1 |
| UV offset = _read_varint_uv_nocheck(splitter); |
| 733 |
1 |
▶ | if (offset == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset == 0) |
0 |
1 |
1 |
|
| 734 |
0 |
| croak("OBJECTV offset is zero !"); |
| 735 |
|
| |
| 736 |
|
| SRL_SPLITTER_TRACE(" * OBJECTV%s, jump to offset %lu, from input_body_pos %lu, then back here %lu.", |
| 737 |
|
| (is_freeze ? "FREEZE" : ""), offset, |
| 738 |
|
| splitter->input_body_pos - splitter->input_str, |
| 739 |
|
| splitter->pos - splitter->input_str); |
| 740 |
|
| |
| 741 |
|
| /* if we have to flush the chunk first, let's do it, until before the OBJECTV tag */ |
| 742 |
1 |
| _maybe_flush_chunk(aTHX_ splitter, saved_pos, NULL); |
| 743 |
|
| |
| 744 |
|
| /* insert an object tag instead of the objectv tag */ |
| 745 |
|
| char tmp_str[1]; |
| 746 |
1 |
▶ | tmp_str[0] = (is_freeze ? 0x32 : 0x2c); |
Branch: 1 branch
| Branch | True | False | Total |
| tmp_str[0] = (is_freeze ? 0x32 : 0x2c); |
0 |
1 |
1 |
|
| 747 |
1 |
| _cat_to_chunk(aTHX_ splitter, tmp_str, 1 ); |
| 748 |
|
| |
| 749 |
|
| /* set the instructions in the stack. Warning, we are pushing, so the order |
| 750 |
|
| will be reversed when we pop */ |
| 751 |
1 |
| splitter->deepness++; |
| 752 |
1 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 753 |
|
| |
| 754 |
1 |
| stack_push(splitter->status_stack, ST_VALUE); /* for the object item-tag */ |
| 755 |
|
| |
| 756 |
1 |
| stack_push(splitter->status_stack, (UV)splitter->pos); |
| 757 |
1 |
| stack_push(splitter->status_stack, ST_ABSOLUTE_JUMP); |
| 758 |
|
| |
| 759 |
1 |
| stack_push(splitter->status_stack, ST_VALUE); /* parse the pointed value */ |
| 760 |
|
| |
| 761 |
|
| /* then do the jump */ |
| 762 |
1 |
| char * landing_pos = splitter->input_body_pos + offset - 1; |
| 763 |
1 |
| splitter->pos = landing_pos; |
| 764 |
1 |
| splitter->chunk_iter_start = landing_pos; |
| 765 |
|
| |
| 766 |
1 |
| } |
| 767 |
|
| |
| 768 |
29292 |
| SRL_STATIC_INLINE void _read_copy(pTHX_ srl_splitter_t * splitter) { |
| 769 |
|
| /* we save the position at the copy tag */ |
| 770 |
29292 |
| char* saved_pos = splitter->pos - 1; |
| 771 |
29292 |
| UV offset = _read_varint_uv_nocheck(splitter); |
| 772 |
29292 |
▶ | if (offset == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset == 0) |
0 |
29292 |
29292 |
|
| 773 |
0 |
| croak("COPY offset is zero !"); |
| 774 |
|
| |
| 775 |
|
| SRL_SPLITTER_TRACE(" * COPY, must jump to offset %lu, from input_body_pos %lu, then back here %lu.", |
| 776 |
|
| offset, |
| 777 |
|
| splitter->input_body_pos - splitter->input_str, |
| 778 |
|
| splitter->pos - splitter->input_str); |
| 779 |
|
| |
| 780 |
|
| /* if we have to flush the chunk first, let's do it, until before the COPY tag */ |
| 781 |
29292 |
| _maybe_flush_chunk(aTHX_ splitter, saved_pos, NULL); |
| 782 |
|
| |
| 783 |
|
| /* set the instructions in the stack. Warning, we are pushing, so the order |
| 784 |
|
| will be reversed when we pop */ |
| 785 |
29292 |
| splitter->deepness++; |
| 786 |
29292 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 787 |
|
| |
| 788 |
29292 |
| stack_push(splitter->status_stack, (UV)splitter->pos); |
| 789 |
29292 |
| stack_push(splitter->status_stack, ST_ABSOLUTE_JUMP); |
| 790 |
|
| |
| 791 |
|
| /* parse the pointed value */ |
| 792 |
29292 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 793 |
|
| |
| 794 |
|
| /* then do the jump */ |
| 795 |
29292 |
| char * landing_pos = splitter->input_body_pos + offset - 1; |
| 796 |
29292 |
| splitter->pos = landing_pos; |
| 797 |
29292 |
| splitter->chunk_iter_start = landing_pos; |
| 798 |
29292 |
| } |
| 799 |
|
| |
| 800 |
0 |
| SRL_STATIC_INLINE void _read_alias(pTHX_ srl_splitter_t * splitter) { |
| 801 |
|
| /* we save the position at the alias tag */ |
| 802 |
0 |
| char* saved_pos = splitter->pos - 1; |
| 803 |
0 |
| UV offset = _read_varint_uv_nocheck(splitter); |
| 804 |
|
| |
| 805 |
0 |
▶ | if (offset == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (offset == 0) |
0 |
0 |
0 |
|
| 806 |
0 |
| croak("ALIAS offset is zero !"); |
| 807 |
|
| |
| 808 |
|
| SRL_SPLITTER_TRACE(" * ALIAS, must jump to offset %lu, from input_body_pos %lu, then back here %lu.", |
| 809 |
|
| offset, |
| 810 |
|
| splitter->input_body_pos - splitter->input_str, |
| 811 |
|
| splitter->pos - splitter->input_str); |
| 812 |
|
| |
| 813 |
|
| /* if we have to flush the chunk first, let's do it, until before the ALIAS tag */ |
| 814 |
0 |
| _maybe_flush_chunk(aTHX_ splitter, saved_pos, NULL); |
| 815 |
|
| |
| 816 |
|
| /* search in the mapping hash */ |
| 817 |
0 |
| offset_el_t *element = NULL; |
| 818 |
0 |
▶ | HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
Branch: 7 branches
| Branch | True | False | Total |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
| HASH_FIND(hh, offset_hashtable, &offset, sizeof(UV), element); |
0 |
0 |
0 |
|
| 819 |
0 |
▶ | if (element != NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (element != NULL) { |
0 |
0 |
0 |
|
| 820 |
0 |
| UV new_offset = element->value; |
| 821 |
|
| /* insert an ALIAS tag */ |
| 822 |
|
| char tmp_str[SRL_MAX_VARINT_LENGTH]; |
| 823 |
0 |
| tmp_str[0] = 0x2e; |
| 824 |
0 |
| _cat_to_chunk(aTHX_ splitter, tmp_str, 1 ); |
| 825 |
|
| |
| 826 |
|
| /* append the offset as a varint */ |
| 827 |
0 |
| UV varint_len = (UV) (_set_varint_nocheck(tmp_str, new_offset) - tmp_str); |
| 828 |
0 |
| _cat_to_chunk(aTHX_ splitter, tmp_str, varint_len); |
| 829 |
|
| } else { |
| 830 |
|
| |
| 831 |
|
| /* otherwise duplicate the data here */ |
| 832 |
|
| |
| 833 |
0 |
| splitter->deepness++; |
| 834 |
0 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 835 |
|
| |
| 836 |
0 |
| stack_push(splitter->status_stack, (UV)splitter->pos); |
| 837 |
0 |
| stack_push(splitter->status_stack, ST_ABSOLUTE_JUMP); |
| 838 |
|
| |
| 839 |
0 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 840 |
|
| |
| 841 |
|
| /* then do the jump */ |
| 842 |
0 |
| char * landing_pos = splitter->input_body_pos + offset - 1; |
| 843 |
0 |
| splitter->pos = landing_pos; |
| 844 |
0 |
| splitter->chunk_iter_start = landing_pos; |
| 845 |
|
| } |
| 846 |
|
| |
| 847 |
0 |
| } |
| 848 |
|
| |
| 849 |
444 |
| SRL_STATIC_INLINE void _read_hash(srl_splitter_t * splitter) { |
| 850 |
444 |
| UV len = _read_varint_uv_nocheck(splitter); |
| 851 |
|
| SRL_SPLITTER_TRACE(" * HASH of len, %lu", len); |
| 852 |
444 |
| splitter->deepness++; |
| 853 |
444 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 854 |
17985 |
▶ | while (len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len-- > 0) { |
17541 |
444 |
17985 |
|
| 855 |
17541 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 856 |
17541 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 857 |
|
| } |
| 858 |
444 |
| return; |
| 859 |
|
| } |
| 860 |
|
| |
| 861 |
25 |
| SRL_STATIC_INLINE void _read_array(srl_splitter_t * splitter) { |
| 862 |
25 |
| UV len = _read_varint_uv_nocheck(splitter); |
| 863 |
|
| SRL_SPLITTER_TRACE(" * ARRAY of len, %lu", len); |
| 864 |
25 |
| splitter->deepness++; |
| 865 |
25 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 866 |
1501 |
▶ | while (len-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len-- > 0) { |
1476 |
25 |
1501 |
|
| 867 |
1476 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 868 |
|
| } |
| 869 |
25 |
| return; |
| 870 |
|
| } |
| 871 |
|
| |
| 872 |
0 |
| SRL_STATIC_INLINE void _read_regexp(srl_splitter_t * splitter) { |
| 873 |
0 |
| splitter->deepness++; |
| 874 |
0 |
| stack_push(splitter->status_stack, ST_DEEPNESS_UP); |
| 875 |
0 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 876 |
0 |
| stack_push(splitter->status_stack, ST_VALUE); |
| 877 |
0 |
| } |
| 878 |
|
| |
| 879 |
40 |
| void _empty_hashes(pTHX) { |
| 880 |
|
| dedupe_el_t *elt_d, *tmp_d; |
| 881 |
21428 |
▶ | HASH_ITER(hh, dedupe_hashtable, elt_d, tmp_d) { |
Branch: 3 branches
| Branch | True | False | Total |
| HASH_ITER(hh, dedupe_hashtable, elt_d, tmp_d) { |
26 |
14 |
40 |
| HASH_ITER(hh, dedupe_hashtable, elt_d, tmp_d) { |
21362 |
26 |
21388 |
| HASH_ITER(hh, dedupe_hashtable, elt_d, tmp_d) { |
21388 |
40 |
21428 |
|
| 882 |
21388 |
▶ | HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
Branch: 8 branches
| Branch | True | False | Total |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
21388 |
0 |
21388 |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
26 |
21362 |
21388 |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
0 |
21362 |
21362 |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
0 |
21362 |
21362 |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
21362 |
0 |
21362 |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
7818 |
13544 |
21362 |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
13544 |
7818 |
21362 |
| HASH_DEL(dedupe_hashtable,elt_d); /* delete; dedupe_hashtable advances to next */ |
8337 |
13025 |
21362 |
|
| 883 |
21388 |
| Safefree(elt_d); |
| 884 |
|
| } |
| 885 |
|
| |
| 886 |
40 |
▶ | HASH_ITER(hh, dedupe_hashtable_utf8, elt_d, tmp_d) { |
Branch: 3 branches
| Branch | True | False | Total |
| HASH_ITER(hh, dedupe_hashtable_utf8, elt_d, tmp_d) { |
0 |
40 |
40 |
| HASH_ITER(hh, dedupe_hashtable_utf8, elt_d, tmp_d) { |
0 |
0 |
0 |
| HASH_ITER(hh, dedupe_hashtable_utf8, elt_d, tmp_d) { |
0 |
40 |
40 |
|
| 887 |
0 |
▶ | HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
Branch: 8 branches
| Branch | True | False | Total |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
| HASH_DEL(dedupe_hashtable_utf8,elt_d); /* delete; dedupe_hashtable_utf8 advances to next */ |
0 |
0 |
0 |
|
| 888 |
0 |
| Safefree(elt_d); |
| 889 |
|
| } |
| 890 |
|
| |
| 891 |
|
| offset_el_t *elt_o, *tmp_o; |
| 892 |
42 |
▶ | HASH_ITER(hh, offset_hashtable, elt_o, tmp_o) { |
Branch: 3 branches
| Branch | True | False | Total |
| HASH_ITER(hh, offset_hashtable, elt_o, tmp_o) { |
2 |
38 |
40 |
| HASH_ITER(hh, offset_hashtable, elt_o, tmp_o) { |
0 |
2 |
2 |
| HASH_ITER(hh, offset_hashtable, elt_o, tmp_o) { |
2 |
40 |
42 |
|
| 893 |
2 |
▶ | HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
Branch: 8 branches
| Branch | True | False | Total |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
2 |
0 |
2 |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
2 |
0 |
2 |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
0 |
0 |
0 |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
0 |
0 |
0 |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
0 |
0 |
0 |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
0 |
0 |
0 |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
0 |
0 |
0 |
| HASH_DEL(offset_hashtable, elt_o); /* delete; offset_hashtable advances to next */ |
0 |
0 |
0 |
|
| 894 |
2 |
| Safefree(elt_o); |
| 895 |
|
| } |
| 896 |
40 |
| } |
| 897 |
|
| |
| 898 |
33 |
| SV* srl_splitter_next_chunk(pTHX_ srl_splitter_t * splitter) { |
| 899 |
|
| |
| 900 |
|
| /* create a new chunk */ |
| 901 |
|
| |
| 902 |
|
| /* empty the dedupe_hashtable and offset_hashtable */ |
| 903 |
33 |
| _empty_hashes(aTHX); |
| 904 |
|
| |
| 905 |
|
| /* zero length Perl string */ |
| 906 |
33 |
| splitter->chunk = newSVpvn("", 0); |
| 907 |
33 |
| splitter->chunk_size = 0; |
| 908 |
33 |
| splitter->chunk_start = splitter->pos; |
| 909 |
33 |
| splitter->chunk_iter_start = splitter->pos; |
| 910 |
33 |
| splitter->chunk_nb_elts = 0; |
| 911 |
|
| |
| 912 |
33 |
| splitter->chunk_body_pos = splitter->chunk_start; |
| 913 |
|
| |
| 914 |
|
| /* allocate the chunk to avoid multiple memory allocation */ |
| 915 |
33 |
▶ | SvGROW(splitter->chunk, splitter->size_limit + 50); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(splitter->chunk, splitter->size_limit + 50); |
33 |
0 |
33 |
| SvGROW(splitter->chunk, splitter->size_limit + 50); |
33 |
0 |
33 |
|
| 916 |
|
| |
| 917 |
|
| /* for some reason, jump offset start at 1 in sereal spec, go figure why */ |
| 918 |
33 |
| splitter->chunk_current_offset = 1; |
| 919 |
|
| |
| 920 |
33 |
| UV chunk_header_len = 0; |
| 921 |
|
| /* srl magic */ |
| 922 |
33 |
| sv_catpvn(splitter->chunk, SRL_MAGIC_STRING_HIGHBIT, SRL_MAGIC_STRLEN); |
| 923 |
33 |
| splitter->chunk_body_pos += SRL_MAGIC_STRLEN; |
| 924 |
33 |
| chunk_header_len += SRL_MAGIC_STRLEN; |
| 925 |
|
| |
| 926 |
|
| /* srl version-type type=raw, version=3 */ |
| 927 |
33 |
| sv_catpvn(splitter->chunk, "\3", 1); |
| 928 |
33 |
| splitter->chunk_body_pos += 1; |
| 929 |
33 |
| chunk_header_len += 1; |
| 930 |
|
| |
| 931 |
33 |
▶ | if ( ! splitter->header_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( ! splitter->header_len) { |
31 |
2 |
33 |
|
| 932 |
|
| /* no srl header */ |
| 933 |
31 |
| sv_catpvn(splitter->chunk, "\0", 1); |
| 934 |
31 |
| splitter->chunk_body_pos += 1; |
| 935 |
31 |
| chunk_header_len += 1; |
| 936 |
|
| } else { |
| 937 |
2 |
| sv_catpvn(splitter->chunk, splitter->header_str, splitter->header_len); |
| 938 |
2 |
| splitter->chunk_body_pos += splitter->header_len; |
| 939 |
2 |
| chunk_header_len += splitter->header_len; |
| 940 |
|
| } |
| 941 |
|
| |
| 942 |
|
| char tmp_str[SRL_MAX_VARINT_LENGTH]; |
| 943 |
33 |
| tmp_str[0] = 0x28; /* REFN */ |
| 944 |
33 |
| sv_catpvn(splitter->chunk, tmp_str, 1); |
| 945 |
33 |
| splitter->chunk_current_offset += 1; |
| 946 |
|
| |
| 947 |
33 |
| tmp_str[0] = 0x2b; /* ARRAY */ |
| 948 |
33 |
| sv_catpvn(splitter->chunk, tmp_str, 1); |
| 949 |
33 |
| splitter->chunk_current_offset += 1; |
| 950 |
|
| |
| 951 |
|
| /* append the varint of the maximum array's number of elements */ |
| 952 |
33 |
| UV varint_len = (UV) (_set_varint_nocheck(tmp_str, splitter->input_nb_elts) - tmp_str); |
| 953 |
|
| /* This is the char number where we're going to write the varint */ |
| 954 |
33 |
| UV varint_pos = SvCUR(splitter->chunk); |
| 955 |
33 |
| sv_catpvn(splitter->chunk, tmp_str, varint_len); |
| 956 |
33 |
| splitter->chunk_current_offset += varint_len; |
| 957 |
|
| |
| 958 |
33 |
| int found = _parse(aTHX_ splitter); |
| 959 |
33 |
▶ | if (found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) { |
26 |
7 |
33 |
|
| 960 |
26 |
| char * varint_start = SvPVX(splitter->chunk) + varint_pos; |
| 961 |
26 |
| char * varint_end = varint_start + varint_len - 1; |
| 962 |
26 |
| _update_varint_from_to(varint_start, varint_end, splitter->chunk_nb_elts); |
| 963 |
|
| |
| 964 |
26 |
▶ | if (splitter->header_count_idx != -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter->header_count_idx != -1) { |
1 |
25 |
26 |
|
| 965 |
|
| /* chunk + magic size + version size + header varint size(8) + index where the count is */ |
| 966 |
1 |
| char * header_count_varint_start = SvPVX(splitter->chunk) + SRL_MAGIC_STRLEN + 1 + splitter->header_count_idx; |
| 967 |
|
| /* note: instead of 8, it should be SRL_MAX_VARINT_LENGTH, |
| 968 |
|
| srl_decoder.c:831 is buggy: decoding of varint only support |
| 969 |
|
| varint of size 8 bytes, instead of 11 */ |
| 970 |
1 |
| char * header_count_varint_end = header_count_varint_start + 8 - 1; |
| 971 |
1 |
| _update_varint_from_to(header_count_varint_start, header_count_varint_end, splitter->chunk_nb_elts); |
| 972 |
|
| } |
| 973 |
|
| |
| 974 |
26 |
▶ | if (splitter->compression_format == 0) /* no compression */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (splitter->compression_format == 0) /* no compression */ |
7 |
19 |
26 |
|
| 975 |
7 |
| return splitter->chunk; |
| 976 |
|
| |
| 977 |
|
| /* gzip compression: reforge the SV completely */ |
| 978 |
|
| |
| 979 |
19 |
| UV uncompressed_body_length = SvCUR(splitter->chunk) - chunk_header_len; |
| 980 |
|
| SRL_SPLITTER_TRACE(" * UNCOMPRESS BODY_LENGTH %lu", uncompressed_body_length); |
| 981 |
|
| |
| 982 |
|
| size_t dest_len; |
| 983 |
|
| /* Get uncompressed payload and total packet output (after compression) lengths */ |
| 984 |
19 |
| dest_len = chunk_header_len + 1 + (size_t)mz_compressBound(uncompressed_body_length) + (2 * SRL_MAX_VARINT_LENGTH); |
| 985 |
|
| |
| 986 |
19 |
| SV* compressed_chunk = newSVpvn("", 0); |
| 987 |
19 |
| sv_catpvn(compressed_chunk, SvPVX(splitter->chunk), chunk_header_len); |
| 988 |
|
| |
| 989 |
|
| |
| 990 |
|
| SRL_SPLITTER_TRACE(" * ALLOCATED DEST LEN %lu", dest_len); |
| 991 |
19 |
▶ | char * compressed_pos = SvGROW(compressed_chunk, dest_len); |
Branch: 2 branches
| Branch | True | False | Total |
| char * compressed_pos = SvGROW(compressed_chunk, dest_len); |
19 |
0 |
19 |
| char * compressed_pos = SvGROW(compressed_chunk, dest_len); |
19 |
0 |
19 |
|
| 992 |
|
| |
| 993 |
19 |
| compressed_pos += chunk_header_len; |
| 994 |
|
| |
| 995 |
|
| /* append the varint of the uncompressed length */ |
| 996 |
19 |
| compressed_pos = _set_varint_nocheck(compressed_pos, uncompressed_body_length); |
| 997 |
|
| /* append the varint of the compressed length */ |
| 998 |
19 |
| char* varint_start2 = compressed_pos; |
| 999 |
19 |
| compressed_pos = _set_varint_nocheck(compressed_pos, dest_len); |
| 1000 |
19 |
| char* varint_end2 = compressed_pos -1; |
| 1001 |
|
| |
| 1002 |
|
| |
| 1003 |
19 |
| splitter->compression_level = MZ_DEFAULT_COMPRESSION; |
| 1004 |
|
| |
| 1005 |
19 |
| mz_ulong dl = (mz_ulong)dest_len; |
| 1006 |
19 |
| int status = mz_compress2( |
| 1007 |
|
| (unsigned char *) compressed_pos, |
| 1008 |
|
| &dl, |
| 1009 |
19 |
| (const unsigned char *)(SvPVX(splitter->chunk) + chunk_header_len), |
| 1010 |
|
| (mz_ulong)uncompressed_body_length, |
| 1011 |
19 |
| splitter->compression_level |
| 1012 |
|
| ); |
| 1013 |
|
| (void)status; |
| 1014 |
|
| assert(status == Z_OK); |
| 1015 |
19 |
| dest_len = (size_t)dl; |
| 1016 |
|
| |
| 1017 |
|
| SRL_SPLITTER_TRACE(" * COMPRESSED LEN %lu", dest_len); |
| 1018 |
|
| |
| 1019 |
19 |
| _update_varint_from_to(varint_start2, varint_end2, dest_len); |
| 1020 |
|
| |
| 1021 |
19 |
| SvCUR_set(compressed_chunk, compressed_pos - SvPVX(compressed_chunk) + dest_len); |
| 1022 |
|
| |
| 1023 |
19 |
| (SvPVX(compressed_chunk))[4] = 0x33; |
| 1024 |
|
| |
| 1025 |
19 |
| sv_2mortal(splitter->chunk); |
| 1026 |
19 |
| return compressed_chunk; |
| 1027 |
|
| } |
| 1028 |
7 |
| return &PL_sv_undef; |
| 1029 |
|
| } |
| 1030 |
|
| |
| 1031 |
|
| |
| 1032 |
74275 |
| SRL_STATIC_INLINE char* _set_varint_nocheck(char* buf, UV n) { |
| 1033 |
161980 |
▶ | while (n >= 0x80) { /* while we are larger than 7 bits long */ |
Branch: 1 branch
| Branch | True | False | Total |
| while (n >= 0x80) { /* while we are larger than 7 bits long */ |
87705 |
74275 |
161980 |
|
| 1034 |
87705 |
| *(buf++) = (n & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */ |
| 1035 |
87705 |
| n = n >> 7; /* shift off the 7 least significant bits */ |
| 1036 |
|
| } |
| 1037 |
74275 |
| *(buf++) = n; /* encode the last 7 bits without the high bit being set */ |
| 1038 |
74275 |
| return buf; |
| 1039 |
|
| } |
| 1040 |
|
| |
| 1041 |
|
| |
| 1042 |
|
| |
| 1043 |
125349 |
| SRL_STATIC_INLINE UV _read_varint_uv_nocheck(srl_splitter_t *splitter) { |
| 1044 |
|
| |
| 1045 |
125349 |
| UV result = 0; |
| 1046 |
125349 |
| unsigned lshift = 0; |
| 1047 |
|
| |
| 1048 |
167967 |
▶ | while (*(splitter->pos) & 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*(splitter->pos) & 0x80) { |
42618 |
125349 |
167967 |
|
| 1049 |
42618 |
| result |= ((UV)( *(splitter->pos) & 0x7F) << lshift); |
| 1050 |
42618 |
| lshift += 7; |
| 1051 |
42618 |
| splitter->pos++; |
| 1052 |
|
| } |
| 1053 |
|
| |
| 1054 |
125349 |
| result |= ((UV)*(splitter->pos) << lshift); |
| 1055 |
125349 |
| splitter->pos++; |
| 1056 |
|
| |
| 1057 |
125349 |
| return result; |
| 1058 |
|
| } |
| 1059 |
|
| |
| 1060 |
|
| /* Update a varint anywhere in the output stream with defined start and end |
| 1061 |
|
| * positions. This can produce non-canonical varints and is useful for filling |
| 1062 |
|
| * pre-allocated varints. */ |
| 1063 |
46 |
| SRL_STATIC_INLINE void _update_varint_from_to(char *varint_start, char *varint_end, UV number) { |
| 1064 |
82 |
▶ | while (number >= 0x80) { /* while we are larger than 7 bits long */ |
Branch: 1 branch
| Branch | True | False | Total |
| while (number >= 0x80) { /* while we are larger than 7 bits long */ |
36 |
46 |
82 |
|
| 1065 |
36 |
| *varint_start++ = (number & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */ |
| 1066 |
36 |
| number = number >> 7; /* shift off the 7 least significant bits */ |
| 1067 |
|
| } |
| 1068 |
|
| /* if it is the same size we can use a canonical varint */ |
| 1069 |
46 |
▶ | if ( varint_start == varint_end ) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ( varint_start == varint_end ) { |
26 |
20 |
46 |
|
| 1070 |
26 |
| *varint_start = number; /* encode the last 7 bits without the high bit being set */ |
| 1071 |
|
| } else { |
| 1072 |
|
| /* if not we produce a non-canonical varint, basically we stuff |
| 1073 |
|
| * 0 bits (via 0x80) into the "tail" of the varint, until we can |
| 1074 |
|
| * stick in a null to terminate the sequence. This means that the |
| 1075 |
|
| * varint is effectively "self-padding", and we only need special |
| 1076 |
|
| * logic in the encoder - a decoder will happily process a non-canonical |
| 1077 |
|
| * varint with no problem */ |
| 1078 |
20 |
| *varint_start++ = (number & 0x7f) | 0x80; |
| 1079 |
26 |
▶ | while ( varint_start < varint_end ) |
Branch: 1 branch
| Branch | True | False | Total |
| while ( varint_start < varint_end ) |
6 |
20 |
26 |
|
| 1080 |
6 |
| *varint_start++ = 0x80; |
| 1081 |
20 |
| *varint_start= 0; |
| 1082 |
|
| } |
| 1083 |
46 |
| } |
| 1084 |
|
| |
| 1085 |
132823 |
| SRL_STATIC_INLINE bool _maybe_flush_chunk (pTHX_ srl_splitter_t *splitter, char* end_pos, char* next_start_pos) { |
| 1086 |
|
| UV len; |
| 1087 |
132823 |
| bool did_we_flush = 0; |
| 1088 |
132823 |
▶ | if (end_pos == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (end_pos == NULL) |
29326 |
103497 |
132823 |
|
| 1089 |
29326 |
| end_pos = splitter->pos; |
| 1090 |
132823 |
▶ | if (next_start_pos == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (next_start_pos == NULL) |
58621 |
74202 |
132823 |
|
| 1091 |
58621 |
| next_start_pos = splitter->pos; |
| 1092 |
132823 |
▶ | if (end_pos > splitter->chunk_iter_start) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (end_pos > splitter->chunk_iter_start) { |
35313 |
97510 |
132823 |
|
| 1093 |
35313 |
| len = (UV) (end_pos - splitter->chunk_iter_start); |
| 1094 |
35313 |
| _cat_to_chunk(aTHX_ splitter, splitter->chunk_iter_start, len ); |
| 1095 |
35313 |
| did_we_flush = 1; |
| 1096 |
|
| } |
| 1097 |
|
| /* still do that, if the caller wanted to set a special next_start_pos */ |
| 1098 |
132823 |
| splitter->chunk_iter_start = next_start_pos; |
| 1099 |
132823 |
| return did_we_flush; |
| 1100 |
|
| } |