| 1 |
|
| #include "EXTERN.h" |
| 2 |
|
| #include "perl.h" |
| 3 |
|
| #include "XSUB.h" |
| 4 |
|
| |
| 5 |
|
| /* this is a trimmed down version of Perl_sv_cmp which doesn't consider |
| 6 |
|
| * locale and UTF8: i hope this to be more compliant with Bencode specs */ |
| 7 |
5 |
| static int _raw_cmp(const void *v1, const void *v2) { |
| 8 |
|
| STRLEN cur1, cur2; |
| 9 |
|
| char *pv1, *pv2; |
| 10 |
|
| int cmp, retval; |
| 11 |
|
| |
| 12 |
5 |
| pv1 = SvPV(*(SV **)v1, cur1); |
| 13 |
5 |
▶ | pv2 = SvPV(*(SV **)v2, cur2); |
Branch: 1 branch
| Branch | True | False | Total |
| pv2 = SvPV(*(SV **)v2, cur2); |
5 |
0 |
5 |
|
| 14 |
|
| |
| 15 |
5 |
| retval = memcmp((void*)pv1, (void*)pv2, cur1 < cur2 ? cur1 : cur2); |
| 16 |
5 |
▶ | if (retval) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (retval) { |
5 |
0 |
5 |
|
| 17 |
5 |
▶ | cmp = retval < 0 ? -1 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| cmp = retval < 0 ? -1 : 1; |
1 |
4 |
5 |
|
| 18 |
0 |
▶ | } else if (cur1 == cur2) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cur1 == cur2) { |
0 |
0 |
0 |
|
| 19 |
0 |
| cmp = 0; |
| 20 |
|
| } else { |
| 21 |
0 |
▶ | cmp = cur1 < cur2 ? -1 : 1; |
Branch: 1 branch
| Branch | True | False | Total |
| cmp = cur1 < cur2 ? -1 : 1; |
0 |
0 |
0 |
|
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
5 |
| return cmp; |
| 25 |
|
| } |
| 26 |
|
| |
| 27 |
|
| |
| 28 |
15 |
| static bool _is_int(char *pv, STRLEN len, STRLEN *offset) { |
| 29 |
15 |
| STRLEN i = 0; |
| 30 |
15 |
| bool is_int = 0; |
| 31 |
15 |
| bool first_zero = 0; |
| 32 |
15 |
| bool plus = 0; |
| 33 |
15 |
| bool minus = 0; |
| 34 |
15 |
▶ | if (pv[0] == '+') i = plus = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pv[0] == '+') i = plus = 1; |
0 |
15 |
15 |
|
| 35 |
15 |
▶ | if (pv[0] == '-') i = minus = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pv[0] == '-') i = minus = 1; |
0 |
15 |
15 |
|
| 36 |
|
| |
| 37 |
40 |
▶ | for (;i < len;i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (;i < len;i++) { |
35 |
5 |
40 |
|
| 38 |
35 |
▶ | if (isDIGIT(pv[i])) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (isDIGIT(pv[i])) { |
25 |
10 |
35 |
|
| 39 |
25 |
▶ | if (!is_int && pv[i] == '0') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!is_int && pv[i] == '0') { |
5 |
20 |
25 |
| if (!is_int && pv[i] == '0') { |
1 |
4 |
5 |
|
| 40 |
1 |
▶ | if (first_zero) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (first_zero) { |
0 |
1 |
1 |
|
| 41 |
0 |
| first_zero = 0; |
| 42 |
0 |
| break; |
| 43 |
|
| } else { |
| 44 |
1 |
| first_zero = 1; |
| 45 |
1 |
| continue; |
| 46 |
|
| } |
| 47 |
|
| } |
| 48 |
24 |
| is_int = 1; |
| 49 |
|
| } else { |
| 50 |
10 |
| return 0; |
| 51 |
|
| } |
| 52 |
|
| } |
| 53 |
5 |
▶ | if (is_int ^ first_zero) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_int ^ first_zero) { |
4 |
1 |
5 |
|
| 54 |
4 |
▶ | *offset = (plus || (minus && first_zero)) ? 1 : 0; |
Branch: 3 branches
| Branch | True | False | Total |
| *offset = (plus || (minus && first_zero)) ? 1 : 0; |
4 |
0 |
4 |
| *offset = (plus || (minus && first_zero)) ? 1 : 0; |
0 |
4 |
4 |
| *offset = (plus || (minus && first_zero)) ? 1 : 0; |
0 |
0 |
0 |
|
| 55 |
4 |
| return 1; |
| 56 |
|
| } else { |
| 57 |
1 |
| return 0; |
| 58 |
|
| } |
| 59 |
|
| } |
| 60 |
|
| |
| 61 |
52 |
| static void _bencode(SV *line, SV *stuff, bool coerce, bool hkey) { |
| 62 |
|
| char *pv; |
| 63 |
|
| STRLEN len, offset; |
| 64 |
|
| |
| 65 |
52 |
▶ | if (hkey) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hkey) { |
11 |
41 |
52 |
|
| 66 |
11 |
| pv = SvPV(stuff, len); |
| 67 |
11 |
| sv_catpvf(line, "%d:", len); |
| 68 |
11 |
| sv_catpvn(line, pv, len); |
| 69 |
34 |
| return; |
| 70 |
|
| } |
| 71 |
41 |
▶ | if (SvIOK(stuff) && !SvNOK(stuff) && !SvPOK(stuff)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvIOK(stuff) && !SvNOK(stuff) && !SvPOK(stuff)) { |
10 |
31 |
41 |
| if (SvIOK(stuff) && !SvNOK(stuff) && !SvPOK(stuff)) { |
10 |
0 |
10 |
| if (SvIOK(stuff) && !SvNOK(stuff) && !SvPOK(stuff)) { |
10 |
0 |
10 |
|
| 72 |
10 |
| sv_catpvf(line, "i%de", SvIVX(stuff)); |
| 73 |
10 |
| return; |
| 74 |
|
| } |
| 75 |
31 |
▶ | if (SvROK(stuff)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(stuff)) { |
15 |
16 |
31 |
|
| 76 |
15 |
| switch (SvTYPE(SvRV(stuff))) { |
| 77 |
|
| AV *av, *keys; |
| 78 |
|
| HV *hv; |
| 79 |
|
| SV *sv; |
| 80 |
|
| HE *entry; |
| 81 |
|
| I32 len, i; |
| 82 |
6 |
| case SVt_PVAV: |
| 83 |
6 |
| sv_catpv(line, "l"); |
| 84 |
6 |
| av = (AV*)SvRV(stuff); |
| 85 |
6 |
| len = av_len(av) + 1; |
| 86 |
18 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
12 |
6 |
18 |
|
| 87 |
12 |
| _bencode(line, *av_fetch(av, i, 0), coerce, 0); |
| 88 |
|
| } |
| 89 |
6 |
| sv_catpv(line, "e"); |
| 90 |
6 |
| break; |
| 91 |
8 |
| case SVt_PVHV: |
| 92 |
8 |
| sv_catpv(line, "d"); |
| 93 |
8 |
| hv = (HV*)SvRV(stuff); |
| 94 |
8 |
| keys = (AV*)sv_2mortal((SV*)newAV()); |
| 95 |
8 |
| (void)hv_iterinit(hv); |
| 96 |
19 |
▶ | while ((entry = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((entry = hv_iternext(hv))) { |
11 |
8 |
19 |
|
| 97 |
11 |
| sv = hv_iterkeysv(entry); |
| 98 |
11 |
| (void)SvREFCNT_inc(sv); |
| 99 |
11 |
| av_push(keys, sv); |
| 100 |
|
| } |
| 101 |
8 |
| qsort(AvARRAY(keys), av_len(keys) + 1, sizeof(SV*), _raw_cmp); |
| 102 |
8 |
| len = av_len(keys) + 1; |
| 103 |
18 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
11 |
7 |
18 |
|
| 104 |
11 |
| sv = *av_fetch(keys, i, 0); |
| 105 |
11 |
| _bencode(line, sv, coerce, 1); |
| 106 |
11 |
| _bencode(line, HeVAL( |
| 107 |
|
| hv_fetch_ent(hv, sv, FALSE, 0) |
| 108 |
|
| ), coerce, 0); |
| 109 |
|
| } |
| 110 |
7 |
| sv_catpv(line, "e"); |
| 111 |
7 |
| break; |
| 112 |
1 |
| default: |
| 113 |
1 |
| croak("Cannot serialize this kind of reference: %_", stuff); |
| 114 |
|
| } |
| 115 |
13 |
| return; |
| 116 |
|
| } |
| 117 |
16 |
| pv = SvPV(stuff, len); |
| 118 |
16 |
▶ | if (coerce && _is_int(pv, len, &offset)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (coerce && _is_int(pv, len, &offset)) { |
15 |
1 |
16 |
| if (coerce && _is_int(pv, len, &offset)) { |
4 |
11 |
15 |
|
| 119 |
4 |
| sv_catpvf(line, "i%se", pv + offset); |
| 120 |
|
| } else { |
| 121 |
12 |
| sv_catpvf(line, "%d:", len); |
| 122 |
12 |
| sv_catpvn(line, pv, len); |
| 123 |
|
| } |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| /* Decode XS implementation by Andrew Danforth <adanforth@gmail.com>, |
| 127 |
|
| * based entirely upon the Perl implementation by Giulio Motta */ |
| 128 |
|
| |
| 129 |
|
| struct decode_stack_entry { |
| 130 |
|
| SV *sv; |
| 131 |
|
| SV *key; |
| 132 |
|
| }; |
| 133 |
|
| |
| 134 |
|
| struct decode { |
| 135 |
|
| struct decode_stack_entry *stack_entries; |
| 136 |
|
| int stack_size; |
| 137 |
|
| int stack_next; |
| 138 |
|
| |
| 139 |
|
| char *start; |
| 140 |
|
| char *end; |
| 141 |
|
| STRLEN len; |
| 142 |
|
| |
| 143 |
|
| char *ptr; |
| 144 |
|
| }; |
| 145 |
|
| |
| 146 |
46 |
| static void decode_free(struct decode *decode) { |
| 147 |
57 |
▶ | for(; decode->stack_next; decode->stack_next--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(; decode->stack_next; decode->stack_next--) { |
11 |
46 |
57 |
|
| 148 |
11 |
| struct decode_stack_entry *e = &decode->stack_entries[decode->stack_next - 1]; |
| 149 |
11 |
▶ | if (e->sv) SvREFCNT_dec(e->sv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->sv) SvREFCNT_dec(e->sv); |
11 |
0 |
11 |
|
| 150 |
11 |
▶ | if (e->key) SvREFCNT_dec(e->key); |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->key) SvREFCNT_dec(e->key); |
1 |
10 |
11 |
|
| 151 |
|
| } |
| 152 |
46 |
| Safefree(decode->stack_entries); |
| 153 |
46 |
| } |
| 154 |
|
| |
| 155 |
|
| #define decode_height(s) (s->stack_next) |
| 156 |
|
| #define decode_top(s) (&s->stack_entries[s->stack_next - 1]) |
| 157 |
|
| #define decode_pop(s) (&s->stack_entries[--s->stack_next]) |
| 158 |
|
| |
| 159 |
58 |
| static void decode_push(struct decode *decode, SV *sv) { |
| 160 |
58 |
▶ | if (decode->stack_next == decode->stack_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (decode->stack_next == decode->stack_size) { |
0 |
58 |
58 |
|
| 161 |
0 |
| decode->stack_size <<= 1; |
| 162 |
0 |
| Renew(decode->stack_entries, decode->stack_size, struct decode_stack_entry); |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
58 |
| decode->stack_entries[decode->stack_next].sv = sv; |
| 166 |
58 |
| decode->stack_entries[decode->stack_next].key = NULL; |
| 167 |
58 |
| decode->stack_next++; |
| 168 |
58 |
| } |
| 169 |
|
| |
| 170 |
|
| #define DECODE_CROAK(msg) { \ |
| 171 |
|
| decode_free(decode); \ |
| 172 |
|
| croak("bdecode error: %s: pos %d, %s", msg, decode->ptr - decode->start, decode->start); \ |
| 173 |
|
| } |
| 174 |
|
| #define OVERFLOW_IF(exp) if (exp) DECODE_CROAK("overflow") |
| 175 |
|
| |
| 176 |
69 |
| static STRLEN find_num(struct decode *decode, char endchr, int allow_sign) { |
| 177 |
69 |
| char *s = decode->ptr; |
| 178 |
69 |
| char sign = 0; |
| 179 |
|
| |
| 180 |
69 |
▶ | if (s != decode->end && allow_sign && (*s == '+' || *s == '-')) |
Branch: 4 branches
| Branch | True | False | Total |
| if (s != decode->end && allow_sign && (*s == '+' || *s == '-')) |
69 |
0 |
69 |
| if (s != decode->end && allow_sign && (*s == '+' || *s == '-')) |
25 |
44 |
69 |
| if (s != decode->end && allow_sign && (*s == '+' || *s == '-')) |
25 |
0 |
25 |
| if (s != decode->end && allow_sign && (*s == '+' || *s == '-')) |
4 |
21 |
25 |
|
| 181 |
4 |
| sign = *s++; |
| 182 |
|
| |
| 183 |
175 |
▶ | for(; s < decode->end; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(; s < decode->end; s++) { |
174 |
1 |
175 |
|
| 184 |
174 |
▶ | if (*s == endchr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == endchr) { |
66 |
108 |
174 |
|
| 185 |
66 |
| STRLEN len = s - decode->ptr; |
| 186 |
66 |
▶ | if (sign && len == 1) |
Branch: 2 branches
| Branch | True | False | Total |
| if (sign && len == 1) |
4 |
62 |
66 |
| if (sign && len == 1) |
0 |
4 |
4 |
|
| 187 |
0 |
| DECODE_CROAK("invalid number"); |
| 188 |
66 |
| return(len); |
| 189 |
108 |
▶ | } else if (!isDIGIT(*s)) |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!isDIGIT(*s)) |
2 |
106 |
108 |
|
| 190 |
2 |
| DECODE_CROAK("invalid number"); |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
1 |
| DECODE_CROAK("overflow"); |
| 194 |
|
| } |
| 195 |
|
| |
| 196 |
83 |
| static void push_data(struct decode *decode, SV *data) { |
| 197 |
83 |
▶ | if (decode_height(decode)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (decode_height(decode)) { |
52 |
31 |
83 |
|
| 198 |
52 |
| struct decode_stack_entry *e = decode_top(decode); |
| 199 |
|
| |
| 200 |
52 |
▶ | if (SvTYPE(SvRV(e->sv)) == SVt_PVAV) { /* array */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(SvRV(e->sv)) == SVt_PVAV) { /* array */ |
20 |
32 |
52 |
|
| 201 |
20 |
| av_push((AV*)SvRV(e->sv), data); |
| 202 |
32 |
▶ | } else if (SvTYPE(SvRV(e->sv)) == SVt_PVHV) { /* hash */ |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvTYPE(SvRV(e->sv)) == SVt_PVHV) { /* hash */ |
31 |
1 |
32 |
|
| 203 |
31 |
▶ | if (!e->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->key) { |
17 |
14 |
31 |
|
| 204 |
17 |
▶ | if (SvROK(data)) DECODE_CROAK("dictionary keys must be strings"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(data)) DECODE_CROAK("dictionary keys must be strings"); |
1 |
16 |
17 |
|
| 205 |
16 |
| e->key = data; |
| 206 |
|
| } else { |
| 207 |
14 |
▶ | if (!hv_store_ent((HV*)SvRV(e->sv), e->key, data, 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_store_ent((HV*)SvRV(e->sv), e->key, data, 0)) |
0 |
14 |
14 |
|
| 208 |
0 |
| SvREFCNT_dec(data); |
| 209 |
14 |
| SvREFCNT_dec(e->key); |
| 210 |
14 |
| e->key = NULL; |
| 211 |
|
| } |
| 212 |
|
| } else { |
| 213 |
1 |
| SvREFCNT_dec(data); |
| 214 |
1 |
| DECODE_CROAK("this should never happen"); |
| 215 |
|
| } |
| 216 |
|
| } else { |
| 217 |
31 |
| decode_push(decode, data); |
| 218 |
|
| } |
| 219 |
81 |
| } |
| 220 |
|
| |
| 221 |
21 |
| static void pop_data(struct decode *decode) { |
| 222 |
|
| struct decode_stack_entry *e; |
| 223 |
|
| |
| 224 |
21 |
▶ | if (!decode_height(decode)) DECODE_CROAK("format error"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!decode_height(decode)) DECODE_CROAK("format error"); |
0 |
21 |
21 |
|
| 225 |
|
| |
| 226 |
21 |
| e = decode_pop(decode); |
| 227 |
21 |
▶ | if (e->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->key) { |
1 |
20 |
21 |
|
| 228 |
1 |
| SvREFCNT_dec(e->sv); |
| 229 |
1 |
| SvREFCNT_dec(e->key); |
| 230 |
1 |
| DECODE_CROAK("dictionary key with no value"); |
| 231 |
|
| } |
| 232 |
20 |
| push_data(decode, e->sv); |
| 233 |
19 |
| } |
| 234 |
|
| |
| 235 |
10 |
| static void _cleanse(SV *sv) { |
| 236 |
10 |
▶ | if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) return; |
0 |
10 |
10 |
| if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) return; |
0 |
0 |
0 |
| if (SvIOK(sv) && !SvNOK(sv) && !SvPOK(sv)) return; |
0 |
0 |
0 |
|
| 237 |
10 |
| (void)SvIV(sv); |
| 238 |
10 |
▶ | SvIOK_only(sv); |
Branch: 4 branches
| Branch | True | False | Total |
| SvIOK_only(sv); |
0 |
10 |
10 |
| SvIOK_only(sv); |
0 |
0 |
0 |
| SvIOK_only(sv); |
0 |
0 |
0 |
| SvIOK_only(sv); |
0 |
10 |
10 |
|
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
46 |
| static SV* _bdecode(struct decode *decode) { |
| 242 |
46 |
| I32 depth = 0; |
| 243 |
46 |
| I32 coerce = SvTRUE(get_sv("Convert::Bencode_XS::COERCE", TRUE)); |
| 244 |
|
| |
| 245 |
154 |
▶ | while(decode->ptr < decode->end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(decode->ptr < decode->end) { |
122 |
32 |
154 |
|
| 246 |
122 |
▶ | if (*decode->ptr == 'l') { /* array */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*decode->ptr == 'l') { /* array */ |
13 |
109 |
122 |
|
| 247 |
13 |
| decode_push(decode, newRV_noinc((SV*)newAV())); |
| 248 |
13 |
| depth++; |
| 249 |
13 |
| decode->ptr++; |
| 250 |
109 |
▶ | } else if (*decode->ptr == 'd') { /* hash */ |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*decode->ptr == 'd') { /* hash */ |
14 |
95 |
109 |
|
| 251 |
14 |
| decode_push(decode, newRV_noinc((SV*)newHV())); |
| 252 |
14 |
| depth++; |
| 253 |
14 |
| decode->ptr++; |
| 254 |
95 |
▶ | } else if (*decode->ptr == 'e') { /* end of hash/array */ |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*decode->ptr == 'e') { /* end of hash/array */ |
21 |
74 |
95 |
|
| 255 |
21 |
| pop_data(decode); |
| 256 |
19 |
| depth--; |
| 257 |
19 |
| decode->ptr++; |
| 258 |
74 |
▶ | } else if (*decode->ptr == 'i') { /* integer */ |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (*decode->ptr == 'i') { /* integer */ |
25 |
49 |
74 |
|
| 259 |
|
| STRLEN len; |
| 260 |
|
| SV *n; |
| 261 |
|
| |
| 262 |
25 |
| decode->ptr++; |
| 263 |
25 |
| len = find_num(decode, 'e', 1); |
| 264 |
23 |
▶ | if (!len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!len) |
1 |
22 |
23 |
|
| 265 |
1 |
| DECODE_CROAK("number must have nonzero length"); |
| 266 |
|
| |
| 267 |
22 |
| n = newSVpvn(decode->ptr, len); |
| 268 |
22 |
▶ | if (!coerce) _cleanse(n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!coerce) _cleanse(n); |
9 |
13 |
22 |
|
| 269 |
22 |
| push_data(decode, n); |
| 270 |
|
| |
| 271 |
22 |
| decode->ptr += len + 1; |
| 272 |
49 |
▶ | } else if (isDIGIT(*decode->ptr)) { /* string */ |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (isDIGIT(*decode->ptr)) { /* string */ |
44 |
5 |
49 |
|
| 273 |
44 |
| STRLEN strlen, numlen = find_num(decode, ':', 0); |
| 274 |
|
| |
| 275 |
43 |
▶ | OVERFLOW_IF(decode->ptr + 1 + numlen > decode->end); |
Branch: 1 branch
| Branch | True | False | Total |
| OVERFLOW_IF(decode->ptr + 1 + numlen > decode->end); |
0 |
43 |
43 |
|
| 276 |
|
| |
| 277 |
43 |
| errno = 0; |
| 278 |
43 |
| strlen = strtol(decode->ptr, NULL, 10); |
| 279 |
43 |
▶ | if (errno) |
Branch: 1 branch
| Branch | True | False | Total |
| if (errno) |
0 |
43 |
43 |
|
| 280 |
0 |
| DECODE_CROAK("invalid number"); |
| 281 |
|
| |
| 282 |
43 |
| decode->ptr += 1 + numlen; |
| 283 |
43 |
▶ | OVERFLOW_IF(decode->ptr + strlen > decode->end); |
Branch: 1 branch
| Branch | True | False | Total |
| OVERFLOW_IF(decode->ptr + strlen > decode->end); |
2 |
41 |
43 |
|
| 284 |
41 |
| push_data(decode, newSVpvn(decode->ptr, strlen)); |
| 285 |
|
| |
| 286 |
40 |
| decode->ptr += strlen; |
| 287 |
|
| } else { |
| 288 |
5 |
| DECODE_CROAK("bad format"); |
| 289 |
|
| } |
| 290 |
|
| } |
| 291 |
|
| |
| 292 |
32 |
▶ | OVERFLOW_IF(decode->ptr > decode->end); |
Branch: 1 branch
| Branch | True | False | Total |
| OVERFLOW_IF(decode->ptr > decode->end); |
0 |
32 |
32 |
|
| 293 |
|
| |
| 294 |
32 |
▶ | if (decode_height(decode) != 1 || depth) |
Branch: 2 branches
| Branch | True | False | Total |
| if (decode_height(decode) != 1 || depth) |
31 |
1 |
32 |
| if (decode_height(decode) != 1 || depth) |
5 |
26 |
31 |
|
| 295 |
6 |
| DECODE_CROAK("bad format"); |
| 296 |
|
| |
| 297 |
26 |
| return(decode_pop(decode)->sv); |
| 298 |
|
| } |
| 299 |
|
| |
| 300 |
|
| MODULE = Convert::Bencode_XS PACKAGE = Convert::Bencode_XS |
| 301 |
|
| |
| 302 |
|
| SV* |
| 303 |
|
| bencode(stuff) |
| 304 |
|
| SV * stuff |
| 305 |
|
| PROTOTYPE: $ |
| 306 |
|
| PREINIT: |
| 307 |
18 |
| SV *line = newSV(8100); |
| 308 |
|
| CODE: |
| 309 |
18 |
| sv_setpv(line, ""); |
| 310 |
18 |
| _bencode( |
| 311 |
|
| line, |
| 312 |
|
| stuff, |
| 313 |
18 |
| SvTRUE(get_sv("Convert::Bencode_XS::COERCE", TRUE)), |
| 314 |
|
| 0 |
| 315 |
|
| ); |
| 316 |
17 |
| RETVAL = line; |
| 317 |
|
| OUTPUT: |
| 318 |
|
| RETVAL |
| 319 |
|
| |
| 320 |
|
| SV* |
| 321 |
|
| bdecode(string) |
| 322 |
|
| SV *string |
| 323 |
|
| PROTOTYPE: $ |
| 324 |
|
| CODE: |
| 325 |
|
| struct decode decode; |
| 326 |
|
| |
| 327 |
46 |
▶ | if (!SvPOK(string)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvPOK(string)) |
0 |
46 |
46 |
|
| 328 |
0 |
| croak("bdecode only accepts scalar strings"); |
| 329 |
|
| |
| 330 |
46 |
| decode.start = SvPV(string, decode.len); |
| 331 |
46 |
| decode.end = decode.start + decode.len; |
| 332 |
46 |
| decode.ptr = decode.start; |
| 333 |
|
| |
| 334 |
46 |
| decode.stack_next = 0; |
| 335 |
46 |
| decode.stack_size = 128; |
| 336 |
46 |
| New(0, decode.stack_entries, decode.stack_size, struct decode_stack_entry); |
| 337 |
|
| |
| 338 |
46 |
| RETVAL = _bdecode(&decode); |
| 339 |
|
| |
| 340 |
26 |
| decode_free(&decode); |
| 341 |
|
| OUTPUT: |
| 342 |
|
| RETVAL |
| 343 |
|
| |
| 344 |
|
| void |
| 345 |
|
| cleanse(sv) |
| 346 |
|
| SV * sv |
| 347 |
|
| PROTOTYPE: $ |
| 348 |
|
| CODE: |
| 349 |
1 |
| _cleanse(sv); |