… | |
… | |
187 | return SvUTF8 (sv) |
187 | return SvUTF8 (sv) |
188 | ? utf8_distance (offset, SvPVX (sv)) |
188 | ? utf8_distance (offset, SvPVX (sv)) |
189 | : offset - SvPVX (sv); |
189 | : offset - SvPVX (sv); |
190 | } |
190 | } |
191 | |
191 | |
|
|
192 | ///////////////////////////////////////////////////////////////////////////// |
|
|
193 | // fp hell |
|
|
194 | |
|
|
195 | // scan a group of digits, and a trailing exponent |
|
|
196 | static void |
|
|
197 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
|
|
198 | { |
|
|
199 | UV uaccum = 0; |
|
|
200 | int eaccum = 0; |
|
|
201 | |
|
|
202 | // if we recurse too deep, skip all remaining digits |
|
|
203 | // to avoid a stack overflow attack |
|
|
204 | if (expect_false (--maxdepth <= 0)) |
|
|
205 | while (((U8)*s - '0') < 10) |
|
|
206 | ++s; |
|
|
207 | |
|
|
208 | for (;;) |
|
|
209 | { |
|
|
210 | U8 dig = (U8)*s - '0'; |
|
|
211 | |
|
|
212 | if (expect_false (dig >= 10)) |
|
|
213 | { |
|
|
214 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
215 | { |
|
|
216 | ++s; |
|
|
217 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
|
|
218 | } |
|
|
219 | else if ((dig | ' ') == 'e' - '0') |
|
|
220 | { |
|
|
221 | int exp2 = 0; |
|
|
222 | int neg = 0; |
|
|
223 | |
|
|
224 | ++s; |
|
|
225 | |
|
|
226 | if (*s == '-') |
|
|
227 | { |
|
|
228 | ++s; |
|
|
229 | neg = 1; |
|
|
230 | } |
|
|
231 | else if (*s == '+') |
|
|
232 | ++s; |
|
|
233 | |
|
|
234 | while ((dig = (U8)*s - '0') < 10) |
|
|
235 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
236 | |
|
|
237 | *expo += neg ? -exp2 : exp2; |
|
|
238 | } |
|
|
239 | |
|
|
240 | break; |
|
|
241 | } |
|
|
242 | |
|
|
243 | ++s; |
|
|
244 | |
|
|
245 | uaccum = uaccum * 10 + dig; |
|
|
246 | ++eaccum; |
|
|
247 | |
|
|
248 | // if we have too many digits, then recurse for more |
|
|
249 | // we actually do this for rather few digits |
|
|
250 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
251 | { |
|
|
252 | if (postdp) *expo -= eaccum; |
|
|
253 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
|
|
254 | if (postdp) *expo += eaccum; |
|
|
255 | |
|
|
256 | break; |
|
|
257 | } |
|
|
258 | } |
|
|
259 | |
|
|
260 | // this relies greatly on the quality of the pow () |
|
|
261 | // implementation of the platform, but a good |
|
|
262 | // implementation is hard to beat. |
|
|
263 | if (postdp) *expo -= eaccum; |
|
|
264 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
265 | *expo += eaccum; |
|
|
266 | } |
|
|
267 | |
|
|
268 | static NV |
|
|
269 | json_atof (const char *s) |
|
|
270 | { |
|
|
271 | NV accum = 0.; |
|
|
272 | int expo = 0; |
|
|
273 | int neg = 0; |
|
|
274 | |
|
|
275 | if (*s == '-') |
|
|
276 | { |
|
|
277 | ++s; |
|
|
278 | neg = 1; |
|
|
279 | } |
|
|
280 | |
|
|
281 | // a recursion depth of ten gives us >>500 bits |
|
|
282 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
283 | |
|
|
284 | return neg ? -accum : accum; |
|
|
285 | } |
192 | ///////////////////////////////////////////////////////////////////////////// |
286 | ///////////////////////////////////////////////////////////////////////////// |
193 | // encoder |
287 | // encoder |
194 | |
288 | |
195 | // structure used for encoding JSON |
289 | // structure used for encoding JSON |
196 | typedef struct |
290 | typedef struct |
… | |
… | |
1119 | } |
1213 | } |
1120 | |
1214 | |
1121 | len -= *start == '-' ? 1 : 0; |
1215 | len -= *start == '-' ? 1 : 0; |
1122 | |
1216 | |
1123 | // does not fit into IV or UV, try NV |
1217 | // does not fit into IV or UV, try NV |
1124 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1218 | if (len <= NV_DIG) |
1125 | #if defined (LDBL_DIG) |
|
|
1126 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1127 | #endif |
|
|
1128 | ) |
|
|
1129 | // fits into NV without loss of precision |
1219 | // fits into NV without loss of precision |
1130 | return newSVnv (Atof (start)); |
1220 | return newSVnv (json_atof (start)); |
1131 | |
1221 | |
1132 | // everything else fails, convert it to a string |
1222 | // everything else fails, convert it to a string |
1133 | return newSVpvn (start, dec->cur - start); |
1223 | return newSVpvn (start, dec->cur - start); |
1134 | } |
1224 | } |
1135 | |
1225 | |
1136 | // loss of precision here |
1226 | // loss of precision here |
1137 | return newSVnv (Atof (start)); |
1227 | return newSVnv (json_atof (start)); |
1138 | |
1228 | |
1139 | fail: |
1229 | fail: |
1140 | return 0; |
1230 | return 0; |
1141 | } |
1231 | } |
1142 | |
1232 | |
… | |
… | |
1311 | dSP; |
1401 | dSP; |
1312 | int count; |
1402 | int count; |
1313 | |
1403 | |
1314 | ENTER; SAVETMPS; PUSHMARK (SP); |
1404 | ENTER; SAVETMPS; PUSHMARK (SP); |
1315 | XPUSHs (HeVAL (he)); |
1405 | XPUSHs (HeVAL (he)); |
|
|
1406 | sv_2mortal (sv); |
1316 | |
1407 | |
1317 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1408 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1318 | |
1409 | |
1319 | if (count == 1) |
1410 | if (count == 1) |
1320 | { |
1411 | { |
1321 | sv = newSVsv (POPs); |
1412 | sv = newSVsv (POPs); |
1322 | FREETMPS; LEAVE; |
1413 | FREETMPS; LEAVE; |
1323 | return sv; |
1414 | return sv; |
1324 | } |
1415 | } |
1325 | |
1416 | |
|
|
1417 | SvREFCNT_inc (sv); |
1326 | FREETMPS; LEAVE; |
1418 | FREETMPS; LEAVE; |
1327 | } |
1419 | } |
1328 | } |
1420 | } |
1329 | |
1421 | |
1330 | if (dec->json.cb_object) |
1422 | if (dec->json.cb_object) |
… | |
… | |
1963 | json_init (&json); |
2055 | json_init (&json); |
1964 | json.flags |= ix; |
2056 | json.flags |= ix; |
1965 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2057 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1966 | } |
2058 | } |
1967 | |
2059 | |
1968 | |
|
|