| 1 |
|
| /* Must be defined before including Perl header files or we slow down by 2x! */ |
| 2 |
|
| #define PERL_NO_GET_CONTEXT |
| 3 |
|
| |
| 4 |
|
| #include "EXTERN.h" |
| 5 |
|
| #include "perl.h" |
| 6 |
|
| #include "XSUB.h" |
| 7 |
|
| |
| 8 |
|
| #define NEED_newSV_type_GLOBAL |
| 9 |
|
| #include "ppport.h" |
| 10 |
|
| |
| 11 |
|
| #include "srl_encoder.h" |
| 12 |
|
| #include "srl_buffer.h" |
| 13 |
|
| #include "srl_common.h" |
| 14 |
|
| #include "srl_protocol.h" |
| 15 |
|
| #include "srl_siphash.h" |
| 16 |
|
| |
| 17 |
|
| /* new() marks its handle; a blessed integer from elsewhere is not one. */ |
| 18 |
|
| static MGVTBL srl_handle_vtbl; |
| 19 |
|
| static int |
| 20 |
14 |
| srl_is_handle(SV *sv) |
| 21 |
|
| { |
| 22 |
|
| MAGIC *mg; |
| 23 |
14 |
▶ | if (SvTYPE(sv) < SVt_PVMG) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(sv) < SVt_PVMG) |
0 |
14 |
14 |
|
| 24 |
0 |
| return 0; |
| 25 |
14 |
▶ | for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) |
Branch: 1 branch
| Branch | True | False | Total |
| for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) |
14 |
0 |
14 |
|
| 26 |
14 |
▶ | if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) |
Branch: 2 branches
| Branch | True | False | Total |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) |
14 |
0 |
14 |
| if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &srl_handle_vtbl) |
14 |
0 |
14 |
|
| 27 |
14 |
| return 1; |
| 28 |
0 |
| return 0; |
| 29 |
|
| } |
| 30 |
|
| #define SRL_IS_HANDLE(sv) srl_is_handle(sv) |
| 31 |
|
| |
| 32 |
|
| /* Generated code for exposing C constants to Perl */ |
| 33 |
|
| #include "ptable.h" |
| 34 |
|
| |
| 35 |
|
| #ifndef GvCV_set |
| 36 |
|
| # define GvCV_set(gv, cv) (GvCV(gv) = (cv)) |
| 37 |
|
| #endif |
| 38 |
|
| |
| 39 |
|
| #if defined(cv_set_call_checker) && defined(XopENTRY_set) |
| 40 |
|
| # define USE_CUSTOM_OPS 1 |
| 41 |
|
| #else |
| 42 |
|
| # define USE_CUSTOM_OPS 0 |
| 43 |
|
| #endif |
| 44 |
|
| |
| 45 |
|
| #define pp1_sereal_encode_with_object(has_hdr) THX_pp1_sereal_encode_with_object(aTHX_ has_hdr) |
| 46 |
|
| static void |
| 47 |
7 |
| THX_pp1_sereal_encode_with_object(pTHX_ U8 has_hdr) |
| 48 |
|
| { |
| 49 |
|
| SV *encoder_ref_sv, *encoder_sv, *body_sv, *header_sv; |
| 50 |
|
| srl_encoder_t *enc; |
| 51 |
|
| char *stash_name; |
| 52 |
|
| SV *ret_sv; |
| 53 |
7 |
| dSP; |
| 54 |
|
| |
| 55 |
7 |
▶ | header_sv = has_hdr ? POPs : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| header_sv = has_hdr ? POPs : NULL; |
0 |
7 |
7 |
|
| 56 |
7 |
| body_sv = POPs; |
| 57 |
7 |
| PUTBACK; |
| 58 |
|
| |
| 59 |
7 |
| encoder_ref_sv = TOPs; |
| 60 |
|
| |
| 61 |
7 |
▶ | if (!expect_true( |
Branch: 14 branches
| Branch | True | False | Total |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
0 |
7 |
7 |
| if (!expect_true( |
0 |
0 |
0 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
0 |
7 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
7 |
0 |
7 |
| if (!expect_true( |
0 |
7 |
7 |
|
| 62 |
|
| encoder_ref_sv && |
| 63 |
|
| SvROK(encoder_ref_sv) && |
| 64 |
|
| (encoder_sv = SvRV(encoder_ref_sv)) && |
| 65 |
|
| SvOBJECT(encoder_sv) && |
| 66 |
|
| (stash_name= HvNAME(SvSTASH(encoder_sv))) && |
| 67 |
|
| !strcmp(stash_name, "Sereal::Encoder") && |
| 68 |
|
| SRL_IS_HANDLE(encoder_sv) |
| 69 |
|
| )) |
| 70 |
|
| { |
| 71 |
0 |
| croak("handle is not a Sereal::Encoder handle"); |
| 72 |
|
| } |
| 73 |
|
| /* we should never have an IV smaller than a PTR */ |
| 74 |
7 |
| enc= INT2PTR(srl_encoder_t *,SvIV(encoder_sv)); |
| 75 |
|
| /* User code may clear the variable containing the encoder while it runs. |
| 76 |
|
| * Keep the owner alive until the encode cleanup hook has finished. */ |
| 77 |
7 |
| SAVEFREESV(SvREFCNT_inc(encoder_sv)); |
| 78 |
|
| |
| 79 |
7 |
▶ | if (header_sv && !SvOK(header_sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (header_sv && !SvOK(header_sv)) |
0 |
7 |
7 |
| if (header_sv && !SvOK(header_sv)) |
0 |
0 |
0 |
|
| 80 |
0 |
| header_sv = NULL; |
| 81 |
|
| |
| 82 |
|
| /* We always copy the string since we might reuse the string buffer. That |
| 83 |
|
| * means we already have to do a malloc and we might as well use the |
| 84 |
|
| * opportunity to allocate only as much memory as we really need to hold |
| 85 |
|
| * the output. */ |
| 86 |
7 |
| ret_sv= srl_dump_data_structure_mortal_sv(aTHX_ enc, body_sv, header_sv, SRL_ENC_SV_COPY_ALWAYS); |
| 87 |
7 |
| SPAGAIN; |
| 88 |
7 |
| TOPs = ret_sv; |
| 89 |
7 |
| } |
| 90 |
|
| |
| 91 |
|
| #if USE_CUSTOM_OPS |
| 92 |
|
| |
| 93 |
|
| #ifdef PERL_RC_STACK |
| 94 |
|
| /* Custom ops bypass the XSUB wrapper, so balance their stack references. */ |
| 95 |
|
| XSPP_wrapped(THX_pp_sereal_encode_with_object, 2 + !!PL_op->op_private, 0) |
| 96 |
|
| #else |
| 97 |
|
| static OP * |
| 98 |
0 |
| THX_pp_sereal_encode_with_object(pTHX) |
| 99 |
|
| #endif |
| 100 |
|
| { |
| 101 |
0 |
| pp1_sereal_encode_with_object(PL_op->op_private); |
| 102 |
0 |
| return NORMAL; |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
|
| static OP * |
| 106 |
0 |
| THX_ck_entersub_args_sereal_encode_with_object(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) |
| 107 |
|
| { |
| 108 |
|
| OP *pushop, *firstargop, *cvop, *lastargop, *argop, *newop; |
| 109 |
|
| int arity; |
| 110 |
|
| |
| 111 |
|
| /* Walk the OP structure under the "entersub" to validate that we |
| 112 |
|
| * can use the custom OP implementation. */ |
| 113 |
|
| |
| 114 |
0 |
| entersubop = ck_entersub_args_proto(entersubop, namegv, ckobj); |
| 115 |
0 |
| pushop = cUNOPx(entersubop)->op_first; |
| 116 |
0 |
▶ | if (!OpHAS_SIBLING(pushop)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!OpHAS_SIBLING(pushop)) |
0 |
0 |
0 |
|
| 117 |
0 |
| pushop = cUNOPx(pushop)->op_first; |
| 118 |
0 |
▶ | firstargop = OpSIBLING(pushop); |
Branch: 1 branch
| Branch | True | False | Total |
| firstargop = OpSIBLING(pushop); |
0 |
0 |
0 |
|
| 119 |
|
| |
| 120 |
0 |
▶ | for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; |
Branch: 2 branches
| Branch | True | False | Total |
| for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; |
0 |
0 |
0 |
| for (cvop = firstargop; OpHAS_SIBLING(cvop); cvop = OpSIBLING(cvop)) ; |
0 |
0 |
0 |
|
| 121 |
|
| |
| 122 |
0 |
▶ | for (arity = 0, lastargop = pushop, argop = firstargop; argop != cvop; |
Branch: 1 branch
| Branch | True | False | Total |
| for (arity = 0, lastargop = pushop, argop = firstargop; argop != cvop; |
0 |
0 |
0 |
|
| 123 |
0 |
| lastargop = argop, argop = OpSIBLING(argop)) |
| 124 |
|
| { |
| 125 |
0 |
▶ | arity++; |
Branch: 1 branch
| Branch | True | False | Total |
| arity++; |
0 |
0 |
0 |
|
| 126 |
|
| } |
| 127 |
|
| |
| 128 |
0 |
▶ | if (expect_false(arity < 2 || arity > 3)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (expect_false(arity < 2 || arity > 3)) |
0 |
0 |
0 |
| if (expect_false(arity < 2 || arity > 3)) |
0 |
0 |
0 |
| if (expect_false(arity < 2 || arity > 3)) |
0 |
0 |
0 |
|
| 129 |
0 |
| return entersubop; |
| 130 |
|
| |
| 131 |
|
| /* If we get here, we can replace the entersub with a suitable |
| 132 |
|
| * sereal_encode_with_object custom OP. */ |
| 133 |
|
| |
| 134 |
|
| #ifdef op_sibling_splice |
| 135 |
|
| /* op_sibling_splice is new in 5.31 and we have to do things differenly */ |
| 136 |
|
| |
| 137 |
|
| /* cut out all ops between the pushmark and the RV2CV */ |
| 138 |
0 |
| op_sibling_splice(NULL, pushop, arity, NULL); |
| 139 |
|
| /* then throw everything else out */ |
| 140 |
0 |
| op_free(entersubop); |
| 141 |
0 |
| newop = newUNOP(OP_NULL, 0, NULL); |
| 142 |
|
| |
| 143 |
|
| #else |
| 144 |
|
| |
| 145 |
|
| OpMORESIB_set(pushop, cvop); |
| 146 |
|
| OpLASTSIB_set(lastargop, op_parent(lastargop)); |
| 147 |
|
| op_free(entersubop); |
| 148 |
|
| newop = newUNOP(OP_NULL, 0, firstargop); |
| 149 |
|
| |
| 150 |
|
| #endif |
| 151 |
|
| |
| 152 |
0 |
| newop->op_type = OP_CUSTOM; |
| 153 |
0 |
| newop->op_private = arity == 3; |
| 154 |
0 |
| newop->op_ppaddr = THX_pp_sereal_encode_with_object; |
| 155 |
|
| |
| 156 |
|
| #ifdef op_sibling_splice |
| 157 |
|
| |
| 158 |
|
| /* attach the spliced-out args as children of the custom op, while |
| 159 |
|
| * deleting the stub op created by newUNOP() */ |
| 160 |
0 |
| op_free(op_sibling_splice(newop, NULL, 1, firstargop)); |
| 161 |
|
| |
| 162 |
|
| #endif |
| 163 |
|
| |
| 164 |
0 |
| return newop; |
| 165 |
|
| } |
| 166 |
|
| |
| 167 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 168 |
|
| |
| 169 |
|
| static void |
| 170 |
7 |
| THX_xsfunc_sereal_encode_with_object(pTHX_ CV *cv) |
| 171 |
|
| { |
| 172 |
7 |
| dMARK; |
| 173 |
7 |
| dSP; |
| 174 |
7 |
| SSize_t arity = SP - MARK; |
| 175 |
|
| PERL_UNUSED_ARG(cv); |
| 176 |
7 |
▶ | if (arity < 2 || arity > 3) |
Branch: 2 branches
| Branch | True | False | Total |
| if (arity < 2 || arity > 3) |
7 |
0 |
7 |
| if (arity < 2 || arity > 3) |
0 |
7 |
7 |
|
| 177 |
0 |
| croak("bad Sereal encoder usage"); |
| 178 |
7 |
| pp1_sereal_encode_with_object(arity == 3); |
| 179 |
7 |
| } |
| 180 |
|
| |
| 181 |
|
| #define MY_CXT_KEY "Sereal::Encoder::_stash" XS_VERSION |
| 182 |
|
| |
| 183 |
|
| typedef struct { |
| 184 |
|
| sv_with_hash options[SRL_ENC_OPT_COUNT]; |
| 185 |
|
| U8 known_option_heads[SRL_OPTION_INDEX_BUCKETS]; |
| 186 |
|
| U8 known_option_next[SRL_ENC_OPT_COUNT]; |
| 187 |
|
| } my_cxt_t; |
| 188 |
|
| |
| 189 |
|
| START_MY_CXT |
| 190 |
|
| |
| 191 |
|
| MODULE = Sereal::Encoder PACKAGE = Sereal::Encoder |
| 192 |
|
| PROTOTYPES: DISABLE |
| 193 |
|
| |
| 194 |
|
| SV * |
| 195 |
|
| prepare_checksum_key(key = NULL, opt_key = NULL) |
| 196 |
|
| SV *key; |
| 197 |
|
| SV *opt_key; |
| 198 |
|
| PREINIT: |
| 199 |
6 |
| STRLEN key_len = 0; |
| 200 |
6 |
| STRLEN opt_key_len = 0; |
| 201 |
6 |
| const U8 *key_bytes = (const U8 *)""; |
| 202 |
6 |
▶ | const U8 *opt_key_bytes = (const U8 *)""; |
Branch: 1 branch
| Branch | True | False | Total |
| const U8 *opt_key_bytes = (const U8 *)""; |
0 |
6 |
6 |
|
| 203 |
|
| U8 digest[16]; |
| 204 |
|
| CODE: |
| 205 |
6 |
▶ | if (key && SvOK(key)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (key && SvOK(key)) |
6 |
0 |
6 |
| if (key && SvOK(key)) |
6 |
0 |
6 |
|
| 206 |
6 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 207 |
6 |
▶ | if (opt_key && SvOK(opt_key)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (opt_key && SvOK(opt_key)) { |
0 |
6 |
6 |
| if (opt_key && SvOK(opt_key)) { |
0 |
0 |
0 |
|
| 208 |
0 |
| opt_key_bytes = (const U8 *)SvPVbyte(opt_key, opt_key_len); |
| 209 |
|
| } |
| 210 |
6 |
| srl_siphash_prepare_key(key_bytes, (size_t)key_len, |
| 211 |
|
| opt_key_bytes, (size_t)opt_key_len, digest); |
| 212 |
6 |
| RETVAL = newSVpvn((const char *)digest, sizeof(digest)); |
| 213 |
|
| OUTPUT: RETVAL |
| 214 |
|
| |
| 215 |
|
| SV * |
| 216 |
|
| siphash24(data, key = NULL) |
| 217 |
|
| SV *data; |
| 218 |
|
| SV *key; |
| 219 |
|
| PREINIT: |
| 220 |
|
| STRLEN data_len; |
| 221 |
|
| STRLEN key_len; |
| 222 |
|
| const U8 *data_bytes; |
| 223 |
|
| const U8 *key_bytes; |
| 224 |
|
| srl_siphash_ctx sip; |
| 225 |
|
| srl_u64 digest; |
| 226 |
|
| U8 digest_bytes[8]; |
| 227 |
|
| unsigned int i; |
| 228 |
|
| static const U8 zero_key[16] = { 0 }; |
| 229 |
|
| CODE: |
| 230 |
0 |
| data_bytes = (const U8 *)SvPVbyte(data, data_len); |
| 231 |
0 |
▶ | if (key == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key == NULL) { |
0 |
0 |
0 |
|
| 232 |
0 |
| key_bytes = zero_key; |
| 233 |
|
| } |
| 234 |
|
| else { |
| 235 |
0 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 236 |
0 |
▶ | if (key_len != 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != 16) |
0 |
0 |
0 |
|
| 237 |
0 |
| croak("siphash24 key must be exactly 16 bytes"); |
| 238 |
|
| } |
| 239 |
0 |
| srl_siphash_init_rounds(&sip, key_bytes, 2, 4, 0); |
| 240 |
0 |
| srl_siphash_update(&sip, data_bytes, (size_t)data_len); |
| 241 |
0 |
| digest = srl_siphash_final64(&sip); |
| 242 |
0 |
▶ | for (i = 0; i < 8; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) |
0 |
0 |
0 |
|
| 243 |
0 |
| digest_bytes[i] = (U8)(digest >> (8 * i)); |
| 244 |
0 |
| RETVAL = newSVpvn((const char *)digest_bytes, sizeof(digest_bytes)); |
| 245 |
|
| OUTPUT: RETVAL |
| 246 |
|
| |
| 247 |
|
| SV * |
| 248 |
|
| siphash24_128(data, key = NULL) |
| 249 |
|
| SV *data; |
| 250 |
|
| SV *key; |
| 251 |
|
| PREINIT: |
| 252 |
|
| STRLEN data_len; |
| 253 |
|
| STRLEN key_len; |
| 254 |
|
| const U8 *data_bytes; |
| 255 |
|
| const U8 *key_bytes; |
| 256 |
|
| srl_siphash_ctx sip; |
| 257 |
|
| U8 digest[16]; |
| 258 |
|
| static const U8 zero_key[16] = { 0 }; |
| 259 |
|
| CODE: |
| 260 |
0 |
| data_bytes = (const U8 *)SvPVbyte(data, data_len); |
| 261 |
0 |
▶ | if (key == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key == NULL) { |
0 |
0 |
0 |
|
| 262 |
0 |
| key_bytes = zero_key; |
| 263 |
|
| } |
| 264 |
|
| else { |
| 265 |
0 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 266 |
0 |
▶ | if (key_len != 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != 16) |
0 |
0 |
0 |
|
| 267 |
0 |
| croak("siphash24_128 key must be exactly 16 bytes"); |
| 268 |
|
| } |
| 269 |
0 |
| srl_siphash_init_rounds(&sip, key_bytes, 2, 4, 1); |
| 270 |
0 |
| srl_siphash_update(&sip, data_bytes, (size_t)data_len); |
| 271 |
0 |
| srl_siphash_final128(&sip, digest); |
| 272 |
0 |
| RETVAL = newSVpvn((const char *)digest, sizeof(digest)); |
| 273 |
|
| OUTPUT: RETVAL |
| 274 |
|
| |
| 275 |
|
| SV * |
| 276 |
|
| siphash36_128(data, key = NULL) |
| 277 |
|
| SV *data; |
| 278 |
|
| SV *key; |
| 279 |
|
| PREINIT: |
| 280 |
|
| STRLEN data_len; |
| 281 |
|
| STRLEN key_len; |
| 282 |
|
| const U8 *data_bytes; |
| 283 |
|
| const U8 *key_bytes; |
| 284 |
|
| srl_siphash_ctx sip; |
| 285 |
|
| U8 digest[16]; |
| 286 |
|
| static const U8 zero_key[16] = { 0 }; |
| 287 |
|
| CODE: |
| 288 |
0 |
| data_bytes = (const U8 *)SvPVbyte(data, data_len); |
| 289 |
0 |
▶ | if (key == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key == NULL) { |
0 |
0 |
0 |
|
| 290 |
0 |
| key_bytes = zero_key; |
| 291 |
|
| } |
| 292 |
|
| else { |
| 293 |
0 |
| key_bytes = (const U8 *)SvPVbyte(key, key_len); |
| 294 |
0 |
▶ | if (key_len != 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_len != 16) |
0 |
0 |
0 |
|
| 295 |
0 |
| croak("siphash36_128 key must be exactly 16 bytes"); |
| 296 |
|
| } |
| 297 |
0 |
| srl_siphash_init_rounds(&sip, key_bytes, 3, 6, 1); |
| 298 |
0 |
| srl_siphash_update(&sip, data_bytes, (size_t)data_len); |
| 299 |
0 |
| srl_siphash_final128(&sip, digest); |
| 300 |
0 |
| RETVAL = newSVpvn((const char *)digest, sizeof(digest)); |
| 301 |
|
| OUTPUT: RETVAL |
| 302 |
|
| |
| 303 |
|
| BOOT: |
| 304 |
|
| { |
| 305 |
|
| { |
| 306 |
|
| MY_CXT_INIT; |
| 307 |
30 |
| Zero(MY_CXT.known_option_heads, SRL_OPTION_INDEX_BUCKETS, U8); |
| 308 |
30 |
| Zero(MY_CXT.known_option_next, SRL_ENC_OPT_COUNT, U8); |
| 309 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_ALIASED_DEDUPE_STRINGS, SRL_ENC_OPT_STR_ALIASED_DEDUPE_STRINGS ); |
| 310 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CANONICAL, SRL_ENC_OPT_STR_CANONICAL ); |
| 311 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CANONICAL_REFS, SRL_ENC_OPT_STR_CANONICAL_REFS ); |
| 312 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_COMPRESS, SRL_ENC_OPT_STR_COMPRESS ); |
| 313 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_COMPRESS_LEVEL, SRL_ENC_OPT_STR_COMPRESS_LEVEL ); |
| 314 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_COMPRESS_THRESHOLD, SRL_ENC_OPT_STR_COMPRESS_THRESHOLD ); |
| 315 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CROAK_ON_BLESS, SRL_ENC_OPT_STR_CROAK_ON_BLESS ); |
| 316 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_DEDUPE_STRINGS, SRL_ENC_OPT_STR_DEDUPE_STRINGS ); |
| 317 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FREEZE_CALLBACKS, SRL_ENC_OPT_STR_FREEZE_CALLBACKS ); |
| 318 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_MAX_RECURSION_DEPTH, SRL_ENC_OPT_STR_MAX_RECURSION_DEPTH ); |
| 319 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_NO_BLESS_OBJECTS, SRL_ENC_OPT_STR_NO_BLESS_OBJECTS ); |
| 320 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_NO_SHARED_HASHKEYS, SRL_ENC_OPT_STR_NO_SHARED_HASHKEYS ); |
| 321 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_PROTOCOL_VERSION, SRL_ENC_OPT_STR_PROTOCOL_VERSION ); |
| 322 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SNAPPY, SRL_ENC_OPT_STR_SNAPPY ); |
| 323 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SNAPPY_INCR, SRL_ENC_OPT_STR_SNAPPY_INCR ); |
| 324 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SNAPPY_THRESHOLD, SRL_ENC_OPT_STR_SNAPPY_THRESHOLD ); |
| 325 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_SORT_KEYS, SRL_ENC_OPT_STR_SORT_KEYS ); |
| 326 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_STRINGIFY_UNKNOWN, SRL_ENC_OPT_STR_STRINGIFY_UNKNOWN ); |
| 327 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_UNDEF_UNKNOWN, SRL_ENC_OPT_STR_UNDEF_UNKNOWN ); |
| 328 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_USE_PROTOCOL_V1, SRL_ENC_OPT_STR_USE_PROTOCOL_V1 ); |
| 329 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_WARN_UNKNOWN, SRL_ENC_OPT_STR_WARN_UNKNOWN ); |
| 330 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_USE_STANDARD_DOUBLE, SRL_ENC_OPT_STR_USE_STANDARD_DOUBLE ); |
| 331 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FREEZE_ALLOW_CLASSES, SRL_ENC_OPT_STR_FREEZE_ALLOW_CLASSES ); |
| 332 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FREEZE_DENY_ACTION, SRL_ENC_OPT_STR_FREEZE_DENY_ACTION ); |
| 333 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY_V1, SRL_ENC_OPT_STR_CHECKSUM_KEY_V1 ); |
| 334 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY_V2, SRL_ENC_OPT_STR_CHECKSUM_KEY_V2 ); |
| 335 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY_V3, SRL_ENC_OPT_STR_CHECKSUM_KEY_V3 ); |
| 336 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_POLICY, SRL_ENC_OPT_STR_CHECKSUM_POLICY ); |
| 337 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_UNKNOWN_OPTION_POLICY, SRL_ENC_OPT_STR_UNKNOWN_OPTION_POLICY ); |
| 338 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_KEY, SRL_ENC_OPT_STR_CHECKSUM_KEY ); |
| 339 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_CHECKSUM_MODE, SRL_ENC_OPT_STR_CHECKSUM_MODE ); |
| 340 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_FLOAT_TYPE, SRL_ENC_OPT_STR_FLOAT_TYPE ); |
| 341 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_ALLOW_CLASSES, SRL_ENC_OPT_STR_ALLOW_CLASSES ); |
| 342 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_TRACK_CLASSES, SRL_ENC_OPT_STR_TRACK_CLASSES ); |
| 343 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_REFUSE_OBJECTS, SRL_ENC_OPT_STR_REFUSE_OBJECTS ); |
| 344 |
30 |
| SRL_INIT_OPTION( SRL_ENC_OPT_IDX_PARANOID, SRL_ENC_OPT_STR_PARANOID ); |
| 345 |
|
| } |
| 346 |
|
| #if USE_CUSTOM_OPS |
| 347 |
|
| { |
| 348 |
|
| /* Perl borrows this descriptor; it must outlive every interpreter. */ |
| 349 |
|
| static const XOP xop = { |
| 350 |
|
| XOPf_xop_name | XOPf_xop_desc | XOPf_xop_class, |
| 351 |
|
| "sereal_encode_with_object", "sereal_encode_with_object", OA_UNOP |
| 352 |
|
| }; |
| 353 |
30 |
| Perl_custom_op_register(aTHX_ THX_pp_sereal_encode_with_object, &xop); |
| 354 |
|
| } |
| 355 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 356 |
|
| { |
| 357 |
|
| GV *gv; |
| 358 |
30 |
| CV *cv = newXSproto_portable("Sereal::Encoder::sereal_encode_with_object", |
| 359 |
|
| THX_xsfunc_sereal_encode_with_object, __FILE__, "$$;$"); |
| 360 |
|
| #if USE_CUSTOM_OPS |
| 361 |
30 |
| cv_set_call_checker(cv, THX_ck_entersub_args_sereal_encode_with_object, (SV*)cv); |
| 362 |
|
| #endif /* USE_CUSTOM_OPS */ |
| 363 |
30 |
| gv = gv_fetchpv("Sereal::Encoder::encode", GV_ADDMULTI, SVt_PVCV); |
| 364 |
30 |
| GvCV_set(gv, cv); |
| 365 |
|
| } |
| 366 |
|
| } |
| 367 |
|
| |
| 368 |
|
| srl_encoder_t * |
| 369 |
|
| new(CLASS, opt = NULL) |
| 370 |
|
| char *CLASS; |
| 371 |
|
| HV *opt; |
| 372 |
|
| PREINIT: |
| 373 |
|
| dMY_CXT; |
| 374 |
|
| CODE: |
| 375 |
7 |
| RETVAL = srl_build_encoder_struct(aTHX_ opt, MY_CXT.options, |
| 376 |
|
| MY_CXT.known_option_heads, MY_CXT.known_option_next); |
| 377 |
7 |
| RETVAL->flags |= SRL_F_REUSE_ENCODER; |
| 378 |
|
| OUTPUT: RETVAL |
| 379 |
|
| |
| 380 |
|
| void |
| 381 |
|
| DESTROY(enc) |
| 382 |
|
| srl_encoder_t *enc; |
| 383 |
|
| CODE: |
| 384 |
7 |
| srl_destroy_encoder(aTHX_ enc); |
| 385 |
|
| |
| 386 |
|
| U32 |
| 387 |
|
| flags(enc) |
| 388 |
|
| srl_encoder_t *enc; |
| 389 |
|
| CODE: |
| 390 |
0 |
▶ | RETVAL = enc->flags; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = enc->flags; |
0 |
0 |
0 |
|
| 391 |
|
| OUTPUT: RETVAL |
| 392 |
|
| |
| 393 |
|
| void |
| 394 |
|
| encode_sereal(src, opt = NULL) |
| 395 |
|
| SV *src; |
| 396 |
|
| HV *opt; |
| 397 |
|
| PREINIT: |
| 398 |
|
| srl_encoder_t *enc; |
| 399 |
|
| dMY_CXT; |
| 400 |
|
| PPCODE: |
| 401 |
0 |
| enc = srl_build_encoder_struct(aTHX_ opt, MY_CXT.options, |
| 402 |
|
| MY_CXT.known_option_heads, MY_CXT.known_option_next); |
| 403 |
|
| assert(enc != NULL); |
| 404 |
|
| /* Avoid copy by stealing string buffer if it is not too large. |
| 405 |
|
| * This makes sense in the functional interface since the string |
| 406 |
|
| * buffer isn't ever going to be reused. */ |
| 407 |
0 |
| ST(0) = srl_dump_data_structure_mortal_sv(aTHX_ enc, src, NULL, SRL_ENC_SV_REUSE_MAYBE); |
| 408 |
0 |
| XSRETURN(1); |
| 409 |
|
| |
| 410 |
|
| void |
| 411 |
|
| encode_sereal_with_header_data(src, hdr_user_data_src, opt = NULL) |
| 412 |
|
| SV *src; |
| 413 |
|
| SV *hdr_user_data_src; |
| 414 |
|
| HV *opt; |
| 415 |
|
| PREINIT: |
| 416 |
|
| srl_encoder_t *enc; |
| 417 |
|
| dMY_CXT; |
| 418 |
|
| PPCODE: |
| 419 |
0 |
▶ | if (!SvOK(hdr_user_data_src)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(hdr_user_data_src)) |
0 |
0 |
0 |
|
| 420 |
0 |
| hdr_user_data_src = NULL; |
| 421 |
0 |
| enc = srl_build_encoder_struct(aTHX_ opt, MY_CXT.options, |
| 422 |
|
| MY_CXT.known_option_heads, MY_CXT.known_option_next); |
| 423 |
|
| assert(enc != NULL); |
| 424 |
|
| /* Avoid copy by stealing string buffer if it is not too large. |
| 425 |
|
| * This makes sense in the functional interface since the string |
| 426 |
|
| * buffer isn't ever going to be reused. */ |
| 427 |
0 |
| ST(0) = srl_dump_data_structure_mortal_sv(aTHX_ enc, src, hdr_user_data_src, SRL_ENC_SV_REUSE_MAYBE); |
| 428 |
0 |
| XSRETURN(1); |
| 429 |
|
| |
| 430 |
|
| MODULE = Sereal::Encoder PACKAGE = Sereal::Encoder::_ptabletest |
| 431 |
|
| |
| 432 |
|
| void |
| 433 |
|
| test() |
| 434 |
|
| PREINIT: |
| 435 |
|
| PTABLE_t *tbl; |
| 436 |
|
| PTABLE_ITER_t *iter; |
| 437 |
|
| PTABLE_ENTRY_t *ent; |
| 438 |
0 |
| UV i, n = 20; |
| 439 |
|
| char *check[20]; |
| 440 |
0 |
| char fail[5] = "not "; |
| 441 |
0 |
| char noop[1] = ""; |
| 442 |
|
| CODE: |
| 443 |
0 |
| tbl = PTABLE_new_size(10); |
| 444 |
0 |
▶ | for (i = 0; i < (UV)n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < (UV)n; ++i) { |
0 |
0 |
0 |
|
| 445 |
0 |
| PTABLE_store(tbl, INT2PTR(void *,(1000+i)), INT2PTR(void *, (1000+i))); |
| 446 |
0 |
| check[i] = fail; |
| 447 |
|
| } |
| 448 |
0 |
▶ | for (i = 0; i < (UV)n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < (UV)n; ++i) { |
0 |
0 |
0 |
|
| 449 |
0 |
| const UV res = PTR2UV(PTABLE_fetch(tbl, INT2PTR(void *, (1000+i)))); |
| 450 |
0 |
▶ | printf("%sok %u - fetch %u\n", (res == (UV)(1000+i)) ? noop : fail, (unsigned int)(1+i), (unsigned int)(i+1)); |
Branch: 1 branch
| Branch | True | False | Total |
| printf("%sok %u - fetch %u\n", (res == (UV)(1000+i)) ? noop : fail, (unsigned int)(1+i), (unsigned int)(i+1)); |
0 |
0 |
0 |
|
| 451 |
|
| } |
| 452 |
0 |
| iter = PTABLE_iter_new(tbl); |
| 453 |
0 |
▶ | while ( NULL != (ent = PTABLE_iter_next(iter)) ) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ( NULL != (ent = PTABLE_iter_next(iter)) ) { |
0 |
0 |
0 |
|
| 454 |
0 |
| const UV res = (PTR2UV(ent->value)) - 1000; |
| 455 |
0 |
▶ | if (res < 20) |
Branch: 1 branch
| Branch | True | False | Total |
| if (res < 20) |
0 |
0 |
0 |
|
| 456 |
0 |
| check[res] = noop; |
| 457 |
|
| else |
| 458 |
0 |
| abort(); |
| 459 |
|
| } |
| 460 |
0 |
▶ | for (i = 0; i < (UV)n; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < (UV)n; ++i) { |
0 |
0 |
0 |
|
| 461 |
0 |
| printf("%sok %u - iter %u\n", check[i], (unsigned int)(21+i), (unsigned int)(i+1)); |
| 462 |
|
| } |
| 463 |
0 |
| PTABLE_iter_free(iter); |
| 464 |
0 |
| PTABLE_free(tbl); |