| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| #define MP_SOURCE 1 |
| 6 |
|
| #include "msgpuck.h" |
| 7 |
|
| |
| 8 |
|
| |
| 9 |
|
| |
| 10 |
0 |
| void _mpack_item(SV *res, SV *o) |
| 11 |
|
| { |
| 12 |
|
| size_t len, res_len, new_len; |
| 13 |
|
| char *s, *res_s; |
| 14 |
0 |
| res_s = SvPVbyte(res, res_len); |
| 15 |
|
| unsigned i; |
| 16 |
|
| |
| 17 |
0 |
▶ | if (!SvOK(o)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(o)) { |
0 |
0 |
0 |
|
| 18 |
0 |
| new_len = res_len + mp_sizeof_nil(); |
| 19 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 20 |
0 |
| SvCUR_set(res, new_len); |
| 21 |
0 |
| mp_encode_nil(res_s + res_len); |
| 22 |
0 |
| return; |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
0 |
▶ | if (SvROK(o)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(o)) { |
0 |
0 |
0 |
|
| 26 |
0 |
| o = SvRV(o); |
| 27 |
0 |
▶ | if (SvOBJECT(o)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOBJECT(o)) { |
0 |
0 |
0 |
|
| 28 |
0 |
| SvGETMAGIC(o); |
| 29 |
0 |
| HV *stash = SvSTASH(o); |
| 30 |
0 |
| GV *mtd = gv_fetchmethod_autoload(stash, "msgpack", 0); |
| 31 |
0 |
▶ | if (!mtd) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mtd) |
0 |
0 |
0 |
|
| 32 |
0 |
| croak("Object has no method 'msgpack'"); |
| 33 |
0 |
| dSP; |
| 34 |
0 |
| ENTER; |
| 35 |
0 |
| SAVETMPS; |
| 36 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 37 |
0 |
▶ | XPUSHs (sv_bless (sv_2mortal (newRV_inc(o)), stash)); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs (sv_bless (sv_2mortal (newRV_inc(o)), stash)); |
0 |
0 |
0 |
|
| 38 |
0 |
| PUTBACK; |
| 39 |
0 |
| call_sv((SV *)GvCV(mtd), G_SCALAR); |
| 40 |
0 |
| SPAGAIN; |
| 41 |
|
| |
| 42 |
0 |
| SV *pkt = POPs; |
| 43 |
|
| |
| 44 |
0 |
▶ | if (!SvOK(pkt)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(pkt)) |
0 |
0 |
0 |
|
| 45 |
0 |
| croak("O->msgpack returned undef"); |
| 46 |
|
| |
| 47 |
0 |
| s = SvPV(pkt, len); |
| 48 |
|
| |
| 49 |
0 |
| new_len = res_len + len; |
| 50 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 51 |
0 |
| SvCUR_set(res, new_len); |
| 52 |
0 |
| memcpy(res_s + res_len, s, len); |
| 53 |
|
| |
| 54 |
0 |
| PUTBACK; |
| 55 |
0 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
0 |
0 |
|
| 56 |
0 |
| LEAVE; |
| 57 |
|
| |
| 58 |
0 |
| return; |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
0 |
| switch(SvTYPE(o)) { |
| 62 |
0 |
| case SVt_PVAV: { |
| 63 |
0 |
| AV *a = (AV *)o; |
| 64 |
0 |
| len = av_len(a) + 1; |
| 65 |
0 |
| new_len = res_len + mp_sizeof_array(len); |
| 66 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 67 |
0 |
| SvCUR_set(res, new_len); |
| 68 |
0 |
| mp_encode_array(res_s + res_len, len); |
| 69 |
|
| |
| 70 |
0 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
0 |
0 |
0 |
|
| 71 |
0 |
| SV **item = av_fetch(a, i, 0); |
| 72 |
0 |
▶ | if (!item) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!item) |
0 |
0 |
0 |
|
| 73 |
0 |
| _mpack_item(res, 0); |
| 74 |
|
| else |
| 75 |
0 |
| _mpack_item(res, *item); |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
0 |
| break; |
| 79 |
|
| } |
| 80 |
0 |
| case SVt_PVHV: { |
| 81 |
0 |
| HV *h = (HV *)o; |
| 82 |
0 |
| len = hv_iterinit(h); |
| 83 |
0 |
| new_len = res_len + mp_sizeof_map(len); |
| 84 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 85 |
0 |
| SvCUR_set(res, new_len); |
| 86 |
0 |
| mp_encode_map(res_s + res_len, len); |
| 87 |
|
| |
| 88 |
0 |
| for (;;) { |
| 89 |
0 |
| HE * iter = hv_iternext(h); |
| 90 |
0 |
▶ | if (!iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!iter) |
0 |
0 |
0 |
|
| 91 |
0 |
| break; |
| 92 |
|
| |
| 93 |
0 |
| SV *k = hv_iterkeysv(iter); |
| 94 |
0 |
| SV *v = HeVAL(iter); |
| 95 |
0 |
| _mpack_item(res, k); |
| 96 |
0 |
| _mpack_item(res, v); |
| 97 |
|
| |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
0 |
| break; |
| 101 |
|
| } |
| 102 |
|
| |
| 103 |
0 |
| default: |
| 104 |
0 |
| croak("Can't serialize reference"); |
| 105 |
|
| } |
| 106 |
0 |
| return; |
| 107 |
|
| } |
| 108 |
|
| |
| 109 |
0 |
| switch(SvTYPE(o) & SVt_MASK) { |
| 110 |
0 |
| case SVt_PV: |
| 111 |
|
| case SVt_PVIV: |
| 112 |
|
| case SVt_PVNV: |
| 113 |
|
| case SVt_PVMG: |
| 114 |
|
| case SVt_REGEXP: |
| 115 |
0 |
▶ | if (!looks_like_number(o)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!looks_like_number(o)) { |
0 |
0 |
0 |
|
| 116 |
0 |
| s = SvPV(o, len); |
| 117 |
0 |
| new_len = res_len + mp_sizeof_str(len); |
| 118 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 119 |
0 |
| SvCUR_set(res, new_len); |
| 120 |
0 |
| mp_encode_str(res_s + res_len, s, len); |
| 121 |
0 |
| break; |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| case SVt_NV: { |
| 125 |
0 |
| NV v = SvNV(o); |
| 126 |
0 |
| IV iv = (IV)v; |
| 127 |
|
| |
| 128 |
0 |
▶ | if (v != iv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v != iv) { |
0 |
0 |
0 |
|
| 129 |
0 |
| new_len = res_len + mp_sizeof_double(v); |
| 130 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 131 |
0 |
| SvCUR_set(res, new_len); |
| 132 |
0 |
| mp_encode_double(res_s + res_len, v); |
| 133 |
0 |
| break; |
| 134 |
|
| } |
| 135 |
|
| } |
| 136 |
|
| case SVt_IV: { |
| 137 |
0 |
| IV v = SvIV(o); |
| 138 |
0 |
▶ | if (v >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v >= 0) { |
0 |
0 |
0 |
|
| 139 |
0 |
| new_len = res_len + mp_sizeof_uint(v); |
| 140 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 141 |
0 |
| SvCUR_set(res, new_len); |
| 142 |
0 |
| mp_encode_uint(res_s + res_len, v); |
| 143 |
|
| } else { |
| 144 |
0 |
| new_len = res_len + mp_sizeof_int(v); |
| 145 |
0 |
▶ | res_s = SvGROW(res, new_len); |
Branch: 2 branches
| Branch | True | False | Total |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
| res_s = SvGROW(res, new_len); |
0 |
0 |
0 |
|
| 146 |
0 |
| SvCUR_set(res, new_len); |
| 147 |
0 |
| mp_encode_int(res_s + res_len, v); |
| 148 |
|
| } |
| 149 |
0 |
| break; |
| 150 |
|
| } |
| 151 |
0 |
| default: |
| 152 |
0 |
| croak("Internal msgpack error %d", SvTYPE(o)); |
| 153 |
|
| } |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
|
| |
| 157 |
|
| const char * |
| 158 |
0 |
| _munpack_item(const char *p, size_t len, SV **res, HV *ext, int utf) |
| 159 |
|
| { |
| 160 |
0 |
▶ | if (!len || !p) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!len || !p) |
0 |
0 |
0 |
| if (!len || !p) |
0 |
0 |
0 |
|
| 161 |
0 |
| croak("Internal error: out of pointer"); |
| 162 |
|
| |
| 163 |
0 |
| const char *pe = p + len; |
| 164 |
|
| |
| 165 |
0 |
| switch(mp_typeof(*p)) { |
| 166 |
0 |
| case MP_UINT: |
| 167 |
0 |
| *res = newSViv( mp_decode_uint(&p) ); |
| 168 |
0 |
| break; |
| 169 |
0 |
| case MP_INT: |
| 170 |
0 |
| *res = newSViv( mp_decode_int(&p) ); |
| 171 |
0 |
| break; |
| 172 |
0 |
| case MP_FLOAT: |
| 173 |
0 |
| *res = newSVnv( mp_decode_float(&p) ); |
| 174 |
0 |
| break; |
| 175 |
0 |
| case MP_DOUBLE: |
| 176 |
0 |
| *res = newSVnv( mp_decode_double(&p) ); |
| 177 |
0 |
| break; |
| 178 |
0 |
| case MP_STR: { |
| 179 |
|
| const char *s; |
| 180 |
|
| uint32_t len; |
| 181 |
0 |
| s = mp_decode_str(&p, &len); |
| 182 |
0 |
▶ | *res = newSVpvn_flags(s, len, utf ? SVf_UTF8 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| *res = newSVpvn_flags(s, len, utf ? SVf_UTF8 : 0); |
0 |
0 |
0 |
|
| 183 |
0 |
| break; |
| 184 |
|
| } |
| 185 |
0 |
| case MP_NIL: { |
| 186 |
0 |
| mp_decode_nil(&p); |
| 187 |
0 |
| *res = newSV(0); |
| 188 |
0 |
| break; |
| 189 |
|
| } |
| 190 |
0 |
| case MP_BOOL: |
| 191 |
0 |
▶ | if (mp_decode_bool(&p)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mp_decode_bool(&p)) { |
0 |
0 |
0 |
|
| 192 |
0 |
| *res = newSViv(1); |
| 193 |
|
| } else { |
| 194 |
0 |
| *res = newSViv(0); |
| 195 |
|
| } |
| 196 |
0 |
| break; |
| 197 |
0 |
| case MP_MAP: { |
| 198 |
|
| uint32_t l, i; |
| 199 |
0 |
| l = mp_decode_map(&p); |
| 200 |
0 |
| HV * h = newHV(); |
| 201 |
0 |
| sv_2mortal((SV *)h); |
| 202 |
0 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
0 |
0 |
0 |
|
| 203 |
0 |
| SV *k = 0; |
| 204 |
0 |
| SV *v = 0; |
| 205 |
0 |
▶ | if (p >= pe) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= pe) |
0 |
0 |
0 |
|
| 206 |
0 |
| croak("Unexpected EOF msgunpack str"); |
| 207 |
0 |
| p = _munpack_item(p, pe - p, &k, ext, utf); |
| 208 |
0 |
| sv_2mortal(k); |
| 209 |
0 |
▶ | if (p >= pe) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= pe) |
0 |
0 |
0 |
|
| 210 |
0 |
| croak("Unexpected EOF msgunpack str"); |
| 211 |
0 |
| p = _munpack_item(p, pe - p, &v, ext, utf); |
| 212 |
0 |
| hv_store_ent(h, k, v, 0); |
| 213 |
|
| } |
| 214 |
0 |
| *res = newRV((SV *)h); |
| 215 |
0 |
| break; |
| 216 |
|
| } |
| 217 |
0 |
▶ | case MP_ARRAY: { |
Branch: 1 branch
| Branch | True | False | Total |
| case MP_ARRAY: { |
0 |
0 |
0 |
|
| 218 |
|
| uint32_t l, i; |
| 219 |
0 |
| l = mp_decode_array(&p); |
| 220 |
0 |
| AV *a = newAV(); |
| 221 |
0 |
| sv_2mortal((SV *)a); |
| 222 |
0 |
▶ | for (i = 0; i < l; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < l; i++) { |
0 |
0 |
0 |
|
| 223 |
0 |
| SV *item = 0; |
| 224 |
0 |
▶ | if (p >= pe) |
Branch: 1 branch
| Branch | True | False | Total |
| if (p >= pe) |
0 |
0 |
0 |
|
| 225 |
0 |
| croak("Unexpected EOF msgunpack str"); |
| 226 |
0 |
| p = _munpack_item(p, pe - p, &item, ext, utf); |
| 227 |
0 |
| av_push(a, item); |
| 228 |
|
| |
| 229 |
|
| } |
| 230 |
0 |
| *res = newRV((SV *)a); |
| 231 |
0 |
| break; |
| 232 |
|
| } |
| 233 |
0 |
| case MP_EXT: { |
| 234 |
0 |
| croak("Isn't defined yet"); |
| 235 |
|
| } |
| 236 |
0 |
| default: |
| 237 |
0 |
| croak("Unexpected symbol 0x%02x", 0xFF & (int)(*p)); |
| 238 |
|
| |
| 239 |
|
| } |
| 240 |
0 |
| return p; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
|
| |