| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include <math.h> |
| 6 |
|
| |
| 7 |
|
| /* Back-compatibility */ |
| 8 |
|
| #ifndef G_LIST |
| 9 |
|
| #define G_LIST G_ARRAY |
| 10 |
|
| #endif |
| 11 |
|
| |
| 12 |
|
| /* Types of data structures that can nest */ |
| 13 |
|
| enum PendingStackType { |
| 14 |
|
| array, map, set, attribute, push |
| 15 |
|
| }; |
| 16 |
|
| /* Container representation - basic stack of nested lists */ |
| 17 |
|
| struct pending_stack { |
| 18 |
|
| AV *data; |
| 19 |
|
| struct pending_stack *prev; |
| 20 |
|
| long expected; |
| 21 |
|
| enum PendingStackType type; |
| 22 |
|
| }; |
| 23 |
|
| |
| 24 |
|
| void |
| 25 |
150 |
| add_value(struct pending_stack *target, SV *v) |
| 26 |
|
| { |
| 27 |
150 |
▶ | if(!target) |
Branch: 1 branch
| Branch | True | False | Total |
| if(!target) |
0 |
150 |
150 |
|
| 28 |
0 |
| return; |
| 29 |
|
| |
| 30 |
150 |
| av_push( |
| 31 |
|
| target->data, |
| 32 |
|
| v |
| 33 |
|
| ); |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
|
| MODULE = Net::Async::Redis::XS PACKAGE = Net::Async::Redis::XS |
| 37 |
|
| |
| 38 |
|
| PROTOTYPES: DISABLE |
| 39 |
|
| |
| 40 |
|
| AV * |
| 41 |
|
| decode_buffer(SV *this, SV *p) |
| 42 |
|
| PPCODE: |
| 43 |
|
| /* Plain bytestring required: no magic, no UTF-8, no nonsense */ |
| 44 |
105 |
▶ | if(SvTYPE(p) != SVt_PV) |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvTYPE(p) != SVt_PV) |
2 |
103 |
105 |
|
| 45 |
2 |
| croak("expected a string"); |
| 46 |
103 |
▶ | if(SvUTF8(p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if(SvUTF8(p)) |
0 |
103 |
103 |
|
| 47 |
0 |
| sv_utf8_downgrade(p, true); |
| 48 |
|
| |
| 49 |
|
| STRLEN len; |
| 50 |
103 |
| const char *in = SvPVbyte(p, len); |
| 51 |
103 |
| const char *ptr = in; |
| 52 |
103 |
| const char *end = in + len; |
| 53 |
103 |
| struct pending_stack *ps = NULL; |
| 54 |
103 |
| AV *results = (AV *) sv_2mortal((SV *) newAV()); |
| 55 |
103 |
| int extracted_item = 0; |
| 56 |
103 |
| SV *extracted = &PL_sv_undef; |
| 57 |
|
| /* Perl strings _should_ guarantee this, so perhaps better as an assert? */ |
| 58 |
103 |
▶ | if(*end != '\0') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(*end != '\0') { |
0 |
103 |
103 |
|
| 59 |
0 |
| croak("no trailing null?"); |
| 60 |
|
| } |
| 61 |
|
| |
| 62 |
|
| /* The shortest command is a single-character null, which has the |
| 63 |
|
| * type `_` followed by CRLF terminator, so that's at least 3 |
| 64 |
|
| * characters for a valid command. |
| 65 |
|
| */ |
| 66 |
103 |
▶ | if(len >= 3) { // && *(end - 1) == '\x0A' && *(end - 2) == '\x0D') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(len >= 3) { // && *(end - 1) == '\x0A' && *(end - 2) == '\x0D') { |
6 |
97 |
103 |
|
| 67 |
508 |
▶ | while(*ptr && ptr < end) { |
Branch: 2 branches
| Branch | True | False | Total |
| while(*ptr && ptr < end) { |
31 |
477 |
508 |
| while(*ptr && ptr < end) { |
477 |
0 |
477 |
|
| 68 |
|
| /* First step is to check the data type and extract it if we can */ |
| 69 |
477 |
| switch(*ptr++) { |
| 70 |
276 |
| case '~': /* set */ |
| 71 |
|
| case '*': { /* array */ |
| 72 |
276 |
| int n = 0; |
| 73 |
|
| /* We effectively want grok_atoUV behaviour, but without having a full UV */ |
| 74 |
546 |
▶ | while(*ptr >= '0' && *ptr <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| while(*ptr >= '0' && *ptr <= '9') { |
270 |
276 |
546 |
| while(*ptr >= '0' && *ptr <= '9') { |
270 |
0 |
270 |
|
| 75 |
270 |
| n = (n * 10) + (*ptr - '0'); |
| 76 |
270 |
| ++ptr; |
| 77 |
|
| } |
| 78 |
276 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
20 |
256 |
276 |
|
| 79 |
20 |
| goto end_parsing; |
| 80 |
|
| } |
| 81 |
256 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
256 |
0 |
256 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
256 |
256 |
|
| 82 |
0 |
| croak("protocol violation - array length not followed by CRLF"); |
| 83 |
|
| } |
| 84 |
256 |
| ptr += 2; |
| 85 |
|
| |
| 86 |
256 |
| AV *x = (AV *) sv_2mortal((SV *)newAV()); |
| 87 |
256 |
▶ | if(n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(n > 0) { |
253 |
3 |
256 |
|
| 88 |
253 |
| av_extend(x, n); |
| 89 |
|
| } |
| 90 |
256 |
| struct pending_stack *pn = Newx(pn, 1, struct pending_stack); |
| 91 |
256 |
| pn->data = x; |
| 92 |
256 |
| pn->prev = ps; |
| 93 |
256 |
| pn->expected = n; |
| 94 |
256 |
| pn->type = array; |
| 95 |
256 |
| ps = pn; |
| 96 |
256 |
| break; |
| 97 |
|
| } |
| 98 |
1 |
| case '>': { /* push (pubsub) */ |
| 99 |
1 |
| int n = 0; |
| 100 |
2 |
▶ | while(*ptr >= '0' && *ptr <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| while(*ptr >= '0' && *ptr <= '9') { |
1 |
1 |
2 |
| while(*ptr >= '0' && *ptr <= '9') { |
1 |
0 |
1 |
|
| 101 |
1 |
| n = (n * 10) + (*ptr - '0'); |
| 102 |
1 |
| ++ptr; |
| 103 |
|
| } |
| 104 |
1 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
0 |
1 |
1 |
|
| 105 |
0 |
| goto end_parsing; |
| 106 |
|
| } |
| 107 |
1 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
1 |
0 |
1 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
1 |
1 |
|
| 108 |
0 |
| croak("protocol violation - push length not followed by CRLF"); |
| 109 |
|
| } |
| 110 |
1 |
| ptr += 2; |
| 111 |
|
| |
| 112 |
1 |
| AV *x = (AV *) sv_2mortal((SV *)newAV()); |
| 113 |
1 |
▶ | if(n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(n > 0) { |
1 |
0 |
1 |
|
| 114 |
1 |
| av_extend(x, n); |
| 115 |
|
| } |
| 116 |
1 |
| struct pending_stack *pn = Newx(pn, 1, struct pending_stack); |
| 117 |
1 |
| pn->data = x; |
| 118 |
1 |
| pn->prev = ps; |
| 119 |
1 |
| pn->expected = n; |
| 120 |
1 |
| pn->type = push; |
| 121 |
1 |
| ps = pn; |
| 122 |
1 |
| break; |
| 123 |
|
| } |
| 124 |
1 |
| case '%': { /* hash */ |
| 125 |
1 |
| int n = 0; |
| 126 |
2 |
▶ | while(*ptr >= '0' && *ptr <= '9') { |
Branch: 2 branches
| Branch | True | False | Total |
| while(*ptr >= '0' && *ptr <= '9') { |
1 |
1 |
2 |
| while(*ptr >= '0' && *ptr <= '9') { |
1 |
0 |
1 |
|
| 127 |
1 |
| n = (n * 10) + (*ptr - '0'); |
| 128 |
1 |
| ++ptr; |
| 129 |
|
| } |
| 130 |
1 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
0 |
1 |
1 |
|
| 131 |
0 |
| goto end_parsing; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| /* Hash of key/value pairs */ |
| 135 |
1 |
| n = n * 2; |
| 136 |
1 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
1 |
0 |
1 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
1 |
1 |
|
| 137 |
0 |
| croak("protocol violation - number of hash entries not followed by CRLF"); |
| 138 |
|
| } |
| 139 |
1 |
| ptr += 2; |
| 140 |
1 |
| AV *x = (AV *) sv_2mortal((SV *)newAV()); |
| 141 |
1 |
▶ | if(n > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(n > 0) { |
0 |
1 |
1 |
|
| 142 |
0 |
| av_extend(x, n); |
| 143 |
|
| } |
| 144 |
1 |
| struct pending_stack *pn = Newx(pn, 1, struct pending_stack); |
| 145 |
1 |
| pn->data = x; |
| 146 |
1 |
| pn->prev = ps; |
| 147 |
1 |
| pn->expected = n; |
| 148 |
1 |
| pn->type = map; |
| 149 |
1 |
| ps = pn; |
| 150 |
1 |
| break; |
| 151 |
|
| } |
| 152 |
63 |
| case ':': { /* integer */ |
| 153 |
63 |
| int n = 0; |
| 154 |
63 |
| int negative = 0; |
| 155 |
63 |
▶ | if(*ptr == '-') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(*ptr == '-') { |
1 |
62 |
63 |
|
| 156 |
1 |
| negative = 1; |
| 157 |
1 |
| ++ptr; |
| 158 |
|
| } |
| 159 |
126 |
▶ | while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
Branch: 3 branches
| Branch | True | False | Total |
| while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
63 |
63 |
126 |
| while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
63 |
0 |
63 |
| while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
63 |
0 |
63 |
|
| 160 |
63 |
| n = (n * 10) + (*ptr - '0'); |
| 161 |
63 |
| ++ptr; |
| 162 |
|
| } |
| 163 |
63 |
▶ | if(negative) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(negative) { |
1 |
62 |
63 |
|
| 164 |
1 |
| n = -n; |
| 165 |
|
| } |
| 166 |
63 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
6 |
57 |
63 |
|
| 167 |
6 |
| goto end_parsing; |
| 168 |
|
| } |
| 169 |
57 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
57 |
0 |
57 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
57 |
57 |
|
| 170 |
0 |
| croak("protocol violation - integer not followed by CRLF\n"); |
| 171 |
|
| } |
| 172 |
57 |
| ptr += 2; |
| 173 |
57 |
| SV *v = newSViv(n); |
| 174 |
57 |
▶ | if(ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ps) { |
52 |
5 |
57 |
|
| 175 |
52 |
| add_value(ps, v); |
| 176 |
|
| } else { |
| 177 |
5 |
| av_push(results, v); |
| 178 |
5 |
| extracted_item = 1; |
| 179 |
|
| } |
| 180 |
57 |
| break; |
| 181 |
|
| } |
| 182 |
5 |
| case ',': { /* decimal floating-point */ |
| 183 |
5 |
| float n = 0; |
| 184 |
5 |
| int negative = 0; |
| 185 |
5 |
▶ | if(*ptr == '-') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(*ptr == '-') { |
1 |
4 |
5 |
|
| 186 |
1 |
| negative = 1; |
| 187 |
1 |
| ++ptr; |
| 188 |
|
| } |
| 189 |
5 |
▶ | if(*ptr == 'i' || *ptr == 'n') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(*ptr == 'i' || *ptr == 'n') { |
5 |
0 |
5 |
| if(*ptr == 'i' || *ptr == 'n') { |
0 |
5 |
5 |
|
| 190 |
0 |
▶ | if(!strncmp(ptr, "inf", 3)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(!strncmp(ptr, "inf", 3)) { |
0 |
0 |
0 |
|
| 191 |
0 |
| n = NV_INF; |
| 192 |
0 |
▶ | } else if(!strncmp(ptr, "nan", 3)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if(!strncmp(ptr, "nan", 3)) { |
0 |
0 |
0 |
|
| 193 |
0 |
| n = NV_NAN; |
| 194 |
|
| } |
| 195 |
|
| } else { |
| 196 |
5 |
| int fraction = 0; |
| 197 |
5 |
| int digits = 0; |
| 198 |
36 |
▶ | while((*ptr == '.' || (*ptr >= '0' && *ptr <= '9')) && ptr < end) { |
Branch: 4 branches
| Branch | True | False | Total |
| while((*ptr == '.' || (*ptr >= '0' && *ptr <= '9')) && ptr < end) { |
32 |
4 |
36 |
| while((*ptr == '.' || (*ptr >= '0' && *ptr <= '9')) && ptr < end) { |
27 |
5 |
32 |
| while((*ptr == '.' || (*ptr >= '0' && *ptr <= '9')) && ptr < end) { |
27 |
0 |
27 |
| while((*ptr == '.' || (*ptr >= '0' && *ptr <= '9')) && ptr < end) { |
31 |
0 |
31 |
|
| 199 |
31 |
▶ | if(*ptr == '.') { |
Branch: 1 branch
| Branch | True | False | Total |
| if(*ptr == '.') { |
4 |
27 |
31 |
|
| 200 |
4 |
| fraction = 1; |
| 201 |
|
| } else { |
| 202 |
27 |
| n = (n * 10) + (*ptr - '0'); |
| 203 |
27 |
▶ | if(fraction) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(fraction) { |
22 |
5 |
27 |
|
| 204 |
22 |
| ++digits; |
| 205 |
|
| } |
| 206 |
|
| } |
| 207 |
31 |
| ++ptr; |
| 208 |
|
| } |
| 209 |
5 |
▶ | if(digits > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(digits > 0) { |
4 |
1 |
5 |
|
| 210 |
4 |
| n = n / pow(10, digits); |
| 211 |
|
| } |
| 212 |
|
| } |
| 213 |
5 |
▶ | if(negative) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(negative) { |
1 |
4 |
5 |
|
| 214 |
1 |
| n = -n; |
| 215 |
|
| } |
| 216 |
5 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
0 |
5 |
5 |
|
| 217 |
0 |
| goto end_parsing; |
| 218 |
|
| } |
| 219 |
5 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
5 |
0 |
5 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
5 |
5 |
|
| 220 |
0 |
| croak("protocol violation - decimal numebr not followed by CRLF\n"); |
| 221 |
|
| } |
| 222 |
5 |
| ptr += 2; |
| 223 |
5 |
| SV *v = newSVnv(n); |
| 224 |
5 |
▶ | if(ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ps) { |
0 |
5 |
5 |
|
| 225 |
0 |
| add_value(ps, v); |
| 226 |
|
| } else { |
| 227 |
5 |
| av_push(results, v); |
| 228 |
5 |
| extracted_item = 1; |
| 229 |
|
| } |
| 230 |
5 |
| break; |
| 231 |
|
| } |
| 232 |
0 |
| case '=': /* verbatim string - fall through */ |
| 233 |
|
| case '$': { /* bulk string */ |
| 234 |
0 |
| int n = 0; |
| 235 |
|
| SV *v; |
| 236 |
0 |
▶ | if(ptr[0] == '-' && ptr[1] == '1') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] == '-' && ptr[1] == '1') { |
0 |
0 |
0 |
| if(ptr[0] == '-' && ptr[1] == '1') { |
0 |
0 |
0 |
|
| 237 |
0 |
▶ | if(ptr + 4 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 4 > end) { |
0 |
0 |
0 |
|
| 238 |
0 |
| goto end_parsing; |
| 239 |
|
| } |
| 240 |
|
| // null |
| 241 |
0 |
| ptr += 2; |
| 242 |
0 |
| v = &PL_sv_undef; |
| 243 |
|
| } else { |
| 244 |
0 |
▶ | while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
Branch: 3 branches
| Branch | True | False | Total |
| while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
0 |
0 |
0 |
| while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
0 |
0 |
0 |
| while(*ptr >= '0' && *ptr <= '9' && ptr < end) { |
0 |
0 |
0 |
|
| 245 |
0 |
| n = (n * 10) + (*ptr - '0'); |
| 246 |
0 |
| ++ptr; |
| 247 |
|
| } |
| 248 |
0 |
▶ | if(ptr + n + 4 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + n + 4 > end) { |
0 |
0 |
0 |
|
| 249 |
0 |
| goto end_parsing; |
| 250 |
|
| } |
| 251 |
0 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
0 |
0 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
0 |
0 |
|
| 252 |
0 |
| croak("protocol violation - bulk string length not followed by CRLF"); |
| 253 |
|
| } |
| 254 |
0 |
| ptr += 2; |
| 255 |
0 |
| v = newSVpvn(ptr, n); |
| 256 |
0 |
| ptr += n; |
| 257 |
|
| } |
| 258 |
0 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
0 |
0 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
0 |
0 |
|
| 259 |
0 |
| croak("protocol violation - bulk string not terminated by CRLF"); |
| 260 |
|
| } |
| 261 |
0 |
| ptr += 2; |
| 262 |
0 |
▶ | if(ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ps) { |
0 |
0 |
0 |
|
| 263 |
0 |
| add_value(ps, v); |
| 264 |
|
| } else { |
| 265 |
0 |
| av_push(results, v); |
| 266 |
0 |
| extracted_item = 1; |
| 267 |
|
| } |
| 268 |
0 |
| break; |
| 269 |
|
| } |
| 270 |
129 |
| case '(': /* big number - treat as a string for now */ |
| 271 |
|
| case '+': { /* string */ |
| 272 |
129 |
| const char *start = ptr; |
| 273 |
260 |
▶ | while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
Branch: 4 branches
| Branch | True | False | Total |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
240 |
20 |
260 |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
131 |
109 |
240 |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
131 |
0 |
131 |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
131 |
0 |
131 |
|
| 274 |
131 |
| ++ptr; |
| 275 |
|
| } |
| 276 |
129 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
30 |
99 |
129 |
|
| 277 |
30 |
| goto end_parsing; |
| 278 |
|
| } |
| 279 |
99 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
99 |
0 |
99 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
99 |
99 |
|
| 280 |
0 |
| croak("protocol violation - string not terminated by CRLF"); |
| 281 |
|
| } |
| 282 |
99 |
| int n = ptr - start; |
| 283 |
99 |
| SV *v = newSVpvn(start, n); |
| 284 |
99 |
| ptr += 2; |
| 285 |
99 |
▶ | if(ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ps) { |
98 |
1 |
99 |
|
| 286 |
98 |
| add_value(ps, v); |
| 287 |
|
| } else { |
| 288 |
1 |
| av_push(results, v); |
| 289 |
1 |
| extracted_item = 1; |
| 290 |
|
| } |
| 291 |
99 |
| break; |
| 292 |
|
| } |
| 293 |
2 |
| case '-': { /* error */ |
| 294 |
2 |
| const char *start = ptr; |
| 295 |
12 |
▶ | while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
Branch: 4 branches
| Branch | True | False | Total |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
12 |
0 |
12 |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
10 |
2 |
12 |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
10 |
0 |
10 |
| while(*ptr && (ptr[0] != '\x0D' && ptr[1] != '\x0A' && ptr < end)) { |
10 |
0 |
10 |
|
| 296 |
10 |
| ++ptr; |
| 297 |
|
| } |
| 298 |
2 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
0 |
2 |
2 |
|
| 299 |
0 |
| goto end_parsing; |
| 300 |
|
| } |
| 301 |
2 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
2 |
0 |
2 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
2 |
2 |
|
| 302 |
0 |
| croak("protocol violation - error not terminated by CRLF"); |
| 303 |
|
| } |
| 304 |
2 |
| int n = ptr - start; |
| 305 |
2 |
| SV *v = newSVpvn(start, n); |
| 306 |
2 |
| ptr += 2; |
| 307 |
2 |
| SV *rv = SvRV(this); |
| 308 |
|
| |
| 309 |
|
| /* Remove anything we processed - we're doing this _before_ the call, |
| 310 |
|
| * since it may throw an exception */ |
| 311 |
2 |
| sv_chop(p, ptr); |
| 312 |
2 |
| ptr = SvPVbyte(p, len); |
| 313 |
2 |
| end = ptr + len - 1; |
| 314 |
2 |
▶ | if(hv_exists((HV *) rv, "error", 5)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(hv_exists((HV *) rv, "error", 5)) { |
2 |
0 |
2 |
|
| 315 |
2 |
| SV **cv_ptr = hv_fetchs((HV *) rv, "error", 0); |
| 316 |
2 |
▶ | if(cv_ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(cv_ptr) { |
2 |
0 |
2 |
|
| 317 |
2 |
| CV *cv = (CV *) *cv_ptr; |
| 318 |
2 |
| dSP; |
| 319 |
2 |
| ENTER; |
| 320 |
2 |
| SAVETMPS; |
| 321 |
2 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
2 |
2 |
|
| 322 |
2 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
2 |
2 |
|
| 323 |
2 |
| PUSHs(sv_2mortal(v)); |
| 324 |
2 |
| PUTBACK; |
| 325 |
2 |
| call_sv((SV *) cv, G_VOID | G_DISCARD); |
| 326 |
2 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
2 |
0 |
2 |
|
| 327 |
2 |
| LEAVE; |
| 328 |
|
| } else { |
| 329 |
0 |
| warn("no CV for ->{error}"); |
| 330 |
|
| } |
| 331 |
|
| } else { |
| 332 |
0 |
| warn("no ->{error} handler"); |
| 333 |
|
| } |
| 334 |
|
| /* Note that we are _not_ setting extracted_item here, because there |
| 335 |
|
| * were no items to put in results, and we've already updated the buffer |
| 336 |
|
| * to move past the error item. */ |
| 337 |
2 |
| break; |
| 338 |
|
| } |
| 339 |
0 |
| case '_': { /* single-character null */ |
| 340 |
0 |
| int n = 0; |
| 341 |
0 |
| SV *v = &PL_sv_undef; |
| 342 |
0 |
▶ | if(ptr + 2 > end) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ptr + 2 > end) { |
0 |
0 |
0 |
|
| 343 |
0 |
| goto end_parsing; |
| 344 |
|
| } |
| 345 |
0 |
▶ | if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
Branch: 2 branches
| Branch | True | False | Total |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
0 |
0 |
| if(ptr[0] != '\x0D' || ptr[1] != '\x0A') { |
0 |
0 |
0 |
|
| 346 |
0 |
| croak("protocol violation - single-character null not followed by CRLF"); |
| 347 |
|
| } |
| 348 |
0 |
| ptr += 2; |
| 349 |
0 |
▶ | if(ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ps) { |
0 |
0 |
0 |
|
| 350 |
0 |
| add_value(ps, v); |
| 351 |
|
| } else { |
| 352 |
0 |
| av_push(results, v); |
| 353 |
0 |
| extracted_item = 1; |
| 354 |
|
| } |
| 355 |
0 |
| break; |
| 356 |
|
| } |
| 357 |
0 |
| default: |
| 358 |
0 |
| croak("Unknown type %d, bail out", ptr[-1]); |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
|
| /* Do some housekeeping after parsing: see if we've accumulated enough |
| 362 |
|
| * data to fill the requirements for one or more levels of our nested container |
| 363 |
|
| * handling. |
| 364 |
|
| */ |
| 365 |
439 |
▶ | while(ps && av_count(ps->data) >= ps->expected) { |
Branch: 2 branches
| Branch | True | False | Total |
| while(ps && av_count(ps->data) >= ps->expected) { |
418 |
21 |
439 |
| while(ps && av_count(ps->data) >= ps->expected) { |
18 |
400 |
418 |
|
| 366 |
18 |
| AV *data = ps->data; |
| 367 |
18 |
| struct pending_stack *orig = ps; |
| 368 |
18 |
| ps = orig->prev; |
| 369 |
18 |
| SV *value_ref = newRV((SV *) data); |
| 370 |
18 |
▶ | if(ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(ps) { |
10 |
8 |
18 |
|
| 371 |
10 |
| av_push( |
| 372 |
|
| ps->data, |
| 373 |
|
| value_ref |
| 374 |
|
| ); |
| 375 |
|
| } else { |
| 376 |
8 |
| switch(orig->type) { |
| 377 |
1 |
| case push: { /* pub/sub is treated as an out-of-bound message */ |
| 378 |
1 |
| SV *rv = SvRV(this); |
| 379 |
1 |
▶ | if(hv_exists((HV *) rv, "pubsub", 6)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(hv_exists((HV *) rv, "pubsub", 6)) { |
1 |
0 |
1 |
|
| 380 |
1 |
| SV **cv_ptr = hv_fetchs((HV *) rv, "pubsub", 0); |
| 381 |
1 |
▶ | if(cv_ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(cv_ptr) { |
1 |
0 |
1 |
|
| 382 |
1 |
| CV *cv = (CV *) *cv_ptr; |
| 383 |
1 |
| dSP; |
| 384 |
1 |
| ENTER; |
| 385 |
1 |
| SAVETMPS; |
| 386 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 387 |
1 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
1 |
1 |
|
| 388 |
1 |
| PUSHs(sv_2mortal(value_ref)); |
| 389 |
1 |
| PUTBACK; |
| 390 |
1 |
| call_sv((SV *) cv, G_VOID | G_DISCARD); |
| 391 |
1 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
1 |
0 |
1 |
|
| 392 |
1 |
| LEAVE; |
| 393 |
|
| } else { |
| 394 |
0 |
| warn("no CV for ->{pubsub}"); |
| 395 |
|
| } |
| 396 |
|
| } else { |
| 397 |
0 |
| warn("no ->{pubsub} handler"); |
| 398 |
|
| } |
| 399 |
1 |
| break; |
| 400 |
|
| } |
| 401 |
0 |
| case attribute: { |
| 402 |
|
| /* attributes aren't used much: for now, we emit as an event, |
| 403 |
|
| * but eventually would aim to apply this to the response, |
| 404 |
|
| * by returning a blessed object for example |
| 405 |
|
| */ |
| 406 |
0 |
| SV *rv = SvRV(this); |
| 407 |
0 |
▶ | if(hv_exists((HV *) rv, "attribute", 9)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(hv_exists((HV *) rv, "attribute", 9)) { |
0 |
0 |
0 |
|
| 408 |
0 |
| SV **cv_ptr = hv_fetchs((HV *) rv, "attribute", 0); |
| 409 |
0 |
▶ | if(cv_ptr) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(cv_ptr) { |
0 |
0 |
0 |
|
| 410 |
0 |
| CV *cv = (CV *) *cv_ptr; |
| 411 |
0 |
| dSP; |
| 412 |
0 |
| ENTER; |
| 413 |
0 |
| SAVETMPS; |
| 414 |
0 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
0 |
0 |
|
| 415 |
0 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
0 |
0 |
|
| 416 |
0 |
| PUSHs(sv_2mortal(value_ref)); |
| 417 |
0 |
| PUTBACK; |
| 418 |
0 |
| call_sv((SV *) cv, G_VOID | G_DISCARD); |
| 419 |
0 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
0 |
0 |
0 |
|
| 420 |
0 |
| LEAVE; |
| 421 |
|
| } else { |
| 422 |
0 |
| warn("no CV for ->{attribute}"); |
| 423 |
|
| } |
| 424 |
|
| } else { |
| 425 |
0 |
| warn("no ->{attribute} handler"); |
| 426 |
|
| } |
| 427 |
0 |
| break; |
| 428 |
|
| } |
| 429 |
7 |
| default: |
| 430 |
|
| /* Yes, we fall through as a default for map and array: unless the |
| 431 |
|
| * hashrefs option is set, we want to map all key/value pairs to plain |
| 432 |
|
| * arrays anyway. |
| 433 |
|
| */ |
| 434 |
7 |
| av_push( |
| 435 |
|
| results, |
| 436 |
|
| value_ref |
| 437 |
|
| ); |
| 438 |
7 |
| extracted_item = 1; |
| 439 |
7 |
| break; |
| 440 |
|
| } |
| 441 |
|
| } |
| 442 |
18 |
| Safefree(orig); |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
|
| /* Every time we reach the end of a complete item, we update |
| 446 |
|
| * our parsed string progress and add to our list of things to |
| 447 |
|
| * return. |
| 448 |
|
| */ |
| 449 |
421 |
▶ | if(extracted_item) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(extracted_item) { |
18 |
403 |
421 |
|
| 450 |
|
| /* Remove anything we processed */ |
| 451 |
18 |
| sv_chop(p, ptr); |
| 452 |
18 |
| ptr = SvPVbyte(p, len); |
| 453 |
18 |
| end = ptr + len; |
| 454 |
18 |
| extracted_item = 0; |
| 455 |
|
| /* ... and our "list" is only ever going to be a single item if we're in scalar context */ |
| 456 |
18 |
▶ | if (GIMME_V == G_SCALAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_SCALAR) { |
10 |
8 |
18 |
|
| 457 |
10 |
| extracted = av_shift(results); |
| 458 |
10 |
| break; |
| 459 |
|
| } |
| 460 |
|
| } |
| 461 |
|
| } |
| 462 |
|
| } |
| 463 |
37 |
| end_parsing: |
| 464 |
|
| /* Clean up our temporary parse stack */ |
| 465 |
343 |
▶ | while(ps) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(ps) { |
240 |
103 |
343 |
|
| 466 |
240 |
| struct pending_stack *orig = ps; |
| 467 |
240 |
| ps = ps->prev; |
| 468 |
240 |
| Safefree(orig); |
| 469 |
|
| } |
| 470 |
|
| |
| 471 |
|
| /* Flatten our results back into scalars for return */ |
| 472 |
103 |
▶ | if (GIMME_V == G_LIST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (GIMME_V == G_LIST) { |
51 |
52 |
103 |
|
| 473 |
51 |
| long count = av_count(results); |
| 474 |
51 |
▶ | if(count) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(count) { |
8 |
43 |
51 |
|
| 475 |
8 |
▶ | EXTEND(SP, count); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, count); |
8 |
0 |
8 |
| EXTEND(SP, count); |
0 |
8 |
8 |
|
| 476 |
16 |
▶ | for(int i = 0; i < count; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(int i = 0; i < count; ++i) { |
8 |
8 |
16 |
|
| 477 |
8 |
| mPUSHs(av_shift(results)); |
| 478 |
|
| } |
| 479 |
|
| } |
| 480 |
52 |
▶ | } else if (GIMME_V == G_SCALAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (GIMME_V == G_SCALAR) { |
52 |
0 |
52 |
|
| 481 |
52 |
▶ | mXPUSHs(extracted); |
Branch: 1 branch
| Branch | True | False | Total |
| mXPUSHs(extracted); |
0 |
52 |
52 |
|
| 482 |
|
| } |