Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | |
| 3 | #define DEFAULT_ENCODING "utf-8" |
| 4 | #define S_CHAR(c) (c >= ' ' && c <= '~' && c != '\\' && c != '"') |
| 5 | #define MIN_EXPANSION 6 |
| 6 | |
| 7 | #ifdef Py_UNICODE_WIDE |
| 8 | #define MAX_EXPANSION (2 * MIN_EXPANSION) |
| 9 | #else |
| 10 | #define MAX_EXPANSION MIN_EXPANSION |
| 11 | #endif |
| 12 | |
| 13 | static Py_ssize_t |
| 14 | ascii_escape_char(Py_UNICODE c, char *output, Py_ssize_t chars) |
| 15 | { |
| 16 | Py_UNICODE x; |
| 17 | output[chars++] = '\\'; |
| 18 | switch (c) { |
| 19 | case '\\': output[chars++] = (char)c; break; |
| 20 | case '"': output[chars++] = (char)c; break; |
| 21 | case '\b': output[chars++] = 'b'; break; |
| 22 | case '\f': output[chars++] = 'f'; break; |
| 23 | case '\n': output[chars++] = 'n'; break; |
| 24 | case '\r': output[chars++] = 'r'; break; |
| 25 | case '\t': output[chars++] = 't'; break; |
| 26 | default: |
| 27 | #ifdef Py_UNICODE_WIDE |
| 28 | if (c >= 0x10000) { |
| 29 | /* UTF-16 surrogate pair */ |
| 30 | Py_UNICODE v = c - 0x10000; |
| 31 | c = 0xd800 | ((v >> 10) & 0x3ff); |
| 32 | output[chars++] = 'u'; |
| 33 | x = (c & 0xf000) >> 12; |
| 34 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 35 | x = (c & 0x0f00) >> 8; |
| 36 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 37 | x = (c & 0x00f0) >> 4; |
| 38 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 39 | x = (c & 0x000f); |
| 40 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 41 | c = 0xdc00 | (v & 0x3ff); |
| 42 | output[chars++] = '\\'; |
| 43 | } |
| 44 | #endif |
| 45 | output[chars++] = 'u'; |
| 46 | x = (c & 0xf000) >> 12; |
| 47 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 48 | x = (c & 0x0f00) >> 8; |
| 49 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 50 | x = (c & 0x00f0) >> 4; |
| 51 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 52 | x = (c & 0x000f); |
| 53 | output[chars++] = (x < 10) ? '0' + x : 'a' + (x - 10); |
| 54 | } |
| 55 | return chars; |
| 56 | } |
| 57 | |
| 58 | static PyObject * |
| 59 | ascii_escape_unicode(PyObject *pystr) |
| 60 | { |
| 61 | Py_ssize_t i; |
| 62 | Py_ssize_t input_chars; |
| 63 | Py_ssize_t output_size; |
| 64 | Py_ssize_t chars; |
| 65 | PyObject *rval; |
| 66 | char *output; |
| 67 | Py_UNICODE *input_unicode; |
| 68 | |
| 69 | input_chars = PyUnicode_GET_SIZE(pystr); |
| 70 | input_unicode = PyUnicode_AS_UNICODE(pystr); |
| 71 | /* One char input can be up to 6 chars output, estimate 4 of these */ |
| 72 | output_size = 2 + (MIN_EXPANSION * 4) + input_chars; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 73 | rval = PyBytes_FromStringAndSize(NULL, output_size); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 74 | if (rval == NULL) { |
| 75 | return NULL; |
| 76 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 77 | output = PyBytes_AS_STRING(rval); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 78 | chars = 0; |
| 79 | output[chars++] = '"'; |
| 80 | for (i = 0; i < input_chars; i++) { |
| 81 | Py_UNICODE c = input_unicode[i]; |
| 82 | if (S_CHAR(c)) { |
| 83 | output[chars++] = (char)c; |
| 84 | } |
| 85 | else { |
| 86 | chars = ascii_escape_char(c, output, chars); |
| 87 | } |
| 88 | if (output_size - chars < (1 + MAX_EXPANSION)) { |
| 89 | /* There's more than four, so let's resize by a lot */ |
| 90 | output_size *= 2; |
| 91 | /* This is an upper bound */ |
| 92 | if (output_size > 2 + (input_chars * MAX_EXPANSION)) { |
| 93 | output_size = 2 + (input_chars * MAX_EXPANSION); |
| 94 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 95 | if (_PyBytes_Resize(&rval, output_size) == -1) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 96 | return NULL; |
| 97 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 98 | output = PyBytes_AS_STRING(rval); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | output[chars++] = '"'; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 102 | if (_PyBytes_Resize(&rval, chars) == -1) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 103 | return NULL; |
| 104 | } |
| 105 | return rval; |
| 106 | } |
| 107 | |
| 108 | static PyObject * |
| 109 | ascii_escape_str(PyObject *pystr) |
| 110 | { |
| 111 | Py_ssize_t i; |
| 112 | Py_ssize_t input_chars; |
| 113 | Py_ssize_t output_size; |
| 114 | Py_ssize_t chars; |
| 115 | PyObject *rval; |
| 116 | char *output; |
| 117 | char *input_str; |
| 118 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 119 | input_chars = PyBytes_GET_SIZE(pystr); |
| 120 | input_str = PyBytes_AS_STRING(pystr); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 121 | /* One char input can be up to 6 chars output, estimate 4 of these */ |
| 122 | output_size = 2 + (MIN_EXPANSION * 4) + input_chars; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 123 | rval = PyBytes_FromStringAndSize(NULL, output_size); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 124 | if (rval == NULL) { |
| 125 | return NULL; |
| 126 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 127 | output = PyBytes_AS_STRING(rval); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 128 | chars = 0; |
| 129 | output[chars++] = '"'; |
| 130 | for (i = 0; i < input_chars; i++) { |
| 131 | Py_UNICODE c = (Py_UNICODE)input_str[i]; |
| 132 | if (S_CHAR(c)) { |
| 133 | output[chars++] = (char)c; |
| 134 | } |
| 135 | else if (c > 0x7F) { |
| 136 | /* We hit a non-ASCII character, bail to unicode mode */ |
| 137 | PyObject *uni; |
| 138 | Py_DECREF(rval); |
| 139 | uni = PyUnicode_DecodeUTF8(input_str, input_chars, "strict"); |
| 140 | if (uni == NULL) { |
| 141 | return NULL; |
| 142 | } |
| 143 | rval = ascii_escape_unicode(uni); |
| 144 | Py_DECREF(uni); |
| 145 | return rval; |
| 146 | } |
| 147 | else { |
| 148 | chars = ascii_escape_char(c, output, chars); |
| 149 | } |
| 150 | /* An ASCII char can't possibly expand to a surrogate! */ |
| 151 | if (output_size - chars < (1 + MIN_EXPANSION)) { |
| 152 | /* There's more than four, so let's resize by a lot */ |
| 153 | output_size *= 2; |
| 154 | if (output_size > 2 + (input_chars * MIN_EXPANSION)) { |
| 155 | output_size = 2 + (input_chars * MIN_EXPANSION); |
| 156 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 157 | if (_PyBytes_Resize(&rval, output_size) == -1) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 158 | return NULL; |
| 159 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 160 | output = PyBytes_AS_STRING(rval); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | output[chars++] = '"'; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 164 | if (_PyBytes_Resize(&rval, chars) == -1) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
| 167 | return rval; |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | raise_errmsg(char *msg, PyObject *s, Py_ssize_t end) |
| 172 | { |
| 173 | static PyObject *errmsg_fn = NULL; |
| 174 | PyObject *pymsg; |
| 175 | if (errmsg_fn == NULL) { |
| 176 | PyObject *decoder = PyImport_ImportModule("json.decoder"); |
| 177 | if (decoder == NULL) |
| 178 | return; |
| 179 | errmsg_fn = PyObject_GetAttrString(decoder, "errmsg"); |
| 180 | if (errmsg_fn == NULL) |
| 181 | return; |
| 182 | Py_XDECREF(decoder); |
| 183 | } |
| 184 | pymsg = PyObject_CallFunction(errmsg_fn, "(zOn)", msg, s, end); |
| 185 | PyErr_SetObject(PyExc_ValueError, pymsg); |
| 186 | Py_DECREF(pymsg); |
| 187 | /* |
| 188 | |
| 189 | def linecol(doc, pos): |
| 190 | lineno = doc.count('\n', 0, pos) + 1 |
| 191 | if lineno == 1: |
| 192 | colno = pos |
| 193 | else: |
| 194 | colno = pos - doc.rindex('\n', 0, pos) |
| 195 | return lineno, colno |
| 196 | |
| 197 | def errmsg(msg, doc, pos, end=None): |
| 198 | lineno, colno = linecol(doc, pos) |
| 199 | if end is None: |
| 200 | return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos) |
| 201 | endlineno, endcolno = linecol(doc, end) |
| 202 | return '%s: line %d column %d - line %d column %d (char %d - %d)' % ( |
| 203 | msg, lineno, colno, endlineno, endcolno, pos, end) |
| 204 | |
| 205 | */ |
| 206 | } |
| 207 | |
| 208 | static PyObject * |
| 209 | join_list_unicode(PyObject *lst) |
| 210 | { |
| 211 | static PyObject *ustr = NULL; |
| 212 | static PyObject *joinstr = NULL; |
| 213 | if (ustr == NULL) { |
| 214 | Py_UNICODE c = 0; |
| 215 | ustr = PyUnicode_FromUnicode(&c, 0); |
| 216 | } |
| 217 | if (joinstr == NULL) { |
| 218 | joinstr = PyUnicode_InternFromString("join"); |
| 219 | } |
| 220 | if (joinstr == NULL || ustr == NULL) { |
| 221 | return NULL; |
| 222 | } |
| 223 | return PyObject_CallMethodObjArgs(ustr, joinstr, lst, NULL); |
| 224 | } |
| 225 | |
| 226 | static PyObject * |
| 227 | scanstring_str(PyObject *pystr, Py_ssize_t end, char *encoding, int strict) |
| 228 | { |
| 229 | PyObject *rval; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 230 | Py_ssize_t len = PyBytes_GET_SIZE(pystr); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 231 | Py_ssize_t begin = end - 1; |
| 232 | Py_ssize_t next = begin; |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 233 | char *buf = PyBytes_AS_STRING(pystr); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 234 | Py_buffer info; |
| 235 | PyObject *chunks = PyList_New(0); |
| 236 | if (chunks == NULL) { |
| 237 | goto bail; |
| 238 | } |
Benjamin Peterson | 7af6eec | 2008-07-19 22:26:35 +0000 | [diff] [blame] | 239 | if (end < 0 || len <= end) { |
| 240 | PyErr_SetString(PyExc_ValueError, "end is out of bounds"); |
| 241 | goto bail; |
| 242 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 243 | while (1) { |
| 244 | /* Find the end of the string or the next escape */ |
| 245 | Py_UNICODE c = 0; |
| 246 | PyObject *chunk = NULL; |
| 247 | for (next = end; next < len; next++) { |
| 248 | c = buf[next]; |
| 249 | if (c == '"' || c == '\\') { |
| 250 | break; |
| 251 | } |
| 252 | else if (strict && c <= 0x1f) { |
Benjamin Peterson | 7af6eec | 2008-07-19 22:26:35 +0000 | [diff] [blame] | 253 | raise_errmsg("Invalid control character at", pystr, next); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 254 | goto bail; |
| 255 | } |
| 256 | } |
| 257 | if (!(c == '"' || c == '\\')) { |
| 258 | raise_errmsg("Unterminated string starting at", pystr, begin); |
| 259 | goto bail; |
| 260 | } |
| 261 | /* Pick up this chunk if it's not zero length */ |
| 262 | if (next != end) { |
Amaury Forgeot d'Arc | cb0cdce | 2008-05-08 20:56:43 +0000 | [diff] [blame] | 263 | PyObject *strchunk; |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 264 | if (PyBuffer_FillInfo(&info, NULL, &buf[end], next - end, 1, 0) < 0) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 265 | goto bail; |
| 266 | } |
Antoine Pitrou | ee58fa4 | 2008-08-19 18:22:14 +0000 | [diff] [blame] | 267 | strchunk = PyMemoryView_FromBuffer(&info); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 268 | if (strchunk == NULL) { |
| 269 | goto bail; |
| 270 | } |
| 271 | chunk = PyUnicode_FromEncodedObject(strchunk, encoding, NULL); |
| 272 | Py_DECREF(strchunk); |
| 273 | if (chunk == NULL) { |
| 274 | goto bail; |
| 275 | } |
| 276 | if (PyList_Append(chunks, chunk)) { |
| 277 | goto bail; |
| 278 | } |
| 279 | Py_DECREF(chunk); |
| 280 | } |
| 281 | next++; |
| 282 | if (c == '"') { |
| 283 | end = next; |
| 284 | break; |
| 285 | } |
| 286 | if (next == len) { |
| 287 | raise_errmsg("Unterminated string starting at", pystr, begin); |
| 288 | goto bail; |
| 289 | } |
| 290 | c = buf[next]; |
| 291 | if (c != 'u') { |
| 292 | /* Non-unicode backslash escapes */ |
| 293 | end = next + 1; |
| 294 | switch (c) { |
| 295 | case '"': break; |
| 296 | case '\\': break; |
| 297 | case '/': break; |
| 298 | case 'b': c = '\b'; break; |
| 299 | case 'f': c = '\f'; break; |
| 300 | case 'n': c = '\n'; break; |
| 301 | case 'r': c = '\r'; break; |
| 302 | case 't': c = '\t'; break; |
| 303 | default: c = 0; |
| 304 | } |
| 305 | if (c == 0) { |
| 306 | raise_errmsg("Invalid \\escape", pystr, end - 2); |
| 307 | goto bail; |
| 308 | } |
| 309 | } |
| 310 | else { |
| 311 | c = 0; |
| 312 | next++; |
| 313 | end = next + 4; |
| 314 | if (end >= len) { |
| 315 | raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); |
| 316 | goto bail; |
| 317 | } |
| 318 | /* Decode 4 hex digits */ |
| 319 | for (; next < end; next++) { |
| 320 | Py_ssize_t shl = (end - next - 1) << 2; |
| 321 | Py_UNICODE digit = buf[next]; |
| 322 | switch (digit) { |
| 323 | case '0': case '1': case '2': case '3': case '4': |
| 324 | case '5': case '6': case '7': case '8': case '9': |
| 325 | c |= (digit - '0') << shl; break; |
| 326 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 327 | case 'f': |
| 328 | c |= (digit - 'a' + 10) << shl; break; |
| 329 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 330 | case 'F': |
| 331 | c |= (digit - 'A' + 10) << shl; break; |
| 332 | default: |
| 333 | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 334 | goto bail; |
| 335 | } |
| 336 | } |
| 337 | #ifdef Py_UNICODE_WIDE |
| 338 | /* Surrogate pair */ |
| 339 | if (c >= 0xd800 && c <= 0xdbff) { |
| 340 | Py_UNICODE c2 = 0; |
| 341 | if (end + 6 >= len) { |
| 342 | raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, |
| 343 | end - 5); |
| 344 | } |
| 345 | if (buf[next++] != '\\' || buf[next++] != 'u') { |
| 346 | raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, |
| 347 | end - 5); |
| 348 | } |
| 349 | end += 6; |
| 350 | /* Decode 4 hex digits */ |
| 351 | for (; next < end; next++) { |
| 352 | Py_ssize_t shl = (end - next - 1) << 2; |
| 353 | Py_UNICODE digit = buf[next]; |
| 354 | switch (digit) { |
| 355 | case '0': case '1': case '2': case '3': case '4': |
| 356 | case '5': case '6': case '7': case '8': case '9': |
| 357 | c2 |= (digit - '0') << shl; break; |
| 358 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 359 | case 'f': |
| 360 | c2 |= (digit - 'a' + 10) << shl; break; |
| 361 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 362 | case 'F': |
| 363 | c2 |= (digit - 'A' + 10) << shl; break; |
| 364 | default: |
| 365 | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 366 | goto bail; |
| 367 | } |
| 368 | } |
| 369 | c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00)); |
| 370 | } |
| 371 | #endif |
| 372 | } |
| 373 | chunk = PyUnicode_FromUnicode(&c, 1); |
| 374 | if (chunk == NULL) { |
| 375 | goto bail; |
| 376 | } |
| 377 | if (PyList_Append(chunks, chunk)) { |
| 378 | goto bail; |
| 379 | } |
| 380 | Py_DECREF(chunk); |
| 381 | } |
| 382 | |
| 383 | rval = join_list_unicode(chunks); |
| 384 | if (rval == NULL) { |
| 385 | goto bail; |
| 386 | } |
| 387 | Py_DECREF(chunks); |
| 388 | chunks = NULL; |
| 389 | return Py_BuildValue("(Nn)", rval, end); |
| 390 | bail: |
| 391 | Py_XDECREF(chunks); |
| 392 | return NULL; |
| 393 | } |
| 394 | |
| 395 | |
| 396 | static PyObject * |
| 397 | scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict) |
| 398 | { |
| 399 | PyObject *rval; |
| 400 | Py_ssize_t len = PyUnicode_GET_SIZE(pystr); |
| 401 | Py_ssize_t begin = end - 1; |
| 402 | Py_ssize_t next = begin; |
| 403 | const Py_UNICODE *buf = PyUnicode_AS_UNICODE(pystr); |
| 404 | PyObject *chunks = PyList_New(0); |
| 405 | if (chunks == NULL) { |
| 406 | goto bail; |
| 407 | } |
Benjamin Peterson | 7af6eec | 2008-07-19 22:26:35 +0000 | [diff] [blame] | 408 | if (end < 0 || len <= end) { |
| 409 | PyErr_SetString(PyExc_ValueError, "end is out of bounds"); |
| 410 | goto bail; |
| 411 | } |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 412 | while (1) { |
| 413 | /* Find the end of the string or the next escape */ |
| 414 | Py_UNICODE c = 0; |
| 415 | PyObject *chunk = NULL; |
| 416 | for (next = end; next < len; next++) { |
| 417 | c = buf[next]; |
| 418 | if (c == '"' || c == '\\') { |
| 419 | break; |
| 420 | } |
| 421 | else if (strict && c <= 0x1f) { |
Benjamin Peterson | 7af6eec | 2008-07-19 22:26:35 +0000 | [diff] [blame] | 422 | raise_errmsg("Invalid control character at", pystr, next); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 423 | goto bail; |
| 424 | } |
| 425 | } |
| 426 | if (!(c == '"' || c == '\\')) { |
| 427 | raise_errmsg("Unterminated string starting at", pystr, begin); |
| 428 | goto bail; |
| 429 | } |
| 430 | /* Pick up this chunk if it's not zero length */ |
| 431 | if (next != end) { |
| 432 | chunk = PyUnicode_FromUnicode(&buf[end], next - end); |
| 433 | if (chunk == NULL) { |
| 434 | goto bail; |
| 435 | } |
| 436 | if (PyList_Append(chunks, chunk)) { |
| 437 | goto bail; |
| 438 | } |
| 439 | Py_DECREF(chunk); |
| 440 | } |
| 441 | next++; |
| 442 | if (c == '"') { |
| 443 | end = next; |
| 444 | break; |
| 445 | } |
| 446 | if (next == len) { |
| 447 | raise_errmsg("Unterminated string starting at", pystr, begin); |
| 448 | goto bail; |
| 449 | } |
| 450 | c = buf[next]; |
| 451 | if (c != 'u') { |
| 452 | /* Non-unicode backslash escapes */ |
| 453 | end = next + 1; |
| 454 | switch (c) { |
| 455 | case '"': break; |
| 456 | case '\\': break; |
| 457 | case '/': break; |
| 458 | case 'b': c = '\b'; break; |
| 459 | case 'f': c = '\f'; break; |
| 460 | case 'n': c = '\n'; break; |
| 461 | case 'r': c = '\r'; break; |
| 462 | case 't': c = '\t'; break; |
| 463 | default: c = 0; |
| 464 | } |
| 465 | if (c == 0) { |
| 466 | raise_errmsg("Invalid \\escape", pystr, end - 2); |
| 467 | goto bail; |
| 468 | } |
| 469 | } |
| 470 | else { |
| 471 | c = 0; |
| 472 | next++; |
| 473 | end = next + 4; |
| 474 | if (end >= len) { |
| 475 | raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1); |
| 476 | goto bail; |
| 477 | } |
| 478 | /* Decode 4 hex digits */ |
| 479 | for (; next < end; next++) { |
| 480 | Py_ssize_t shl = (end - next - 1) << 2; |
| 481 | Py_UNICODE digit = buf[next]; |
| 482 | switch (digit) { |
| 483 | case '0': case '1': case '2': case '3': case '4': |
| 484 | case '5': case '6': case '7': case '8': case '9': |
| 485 | c |= (digit - '0') << shl; break; |
| 486 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 487 | case 'f': |
| 488 | c |= (digit - 'a' + 10) << shl; break; |
| 489 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 490 | case 'F': |
| 491 | c |= (digit - 'A' + 10) << shl; break; |
| 492 | default: |
| 493 | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 494 | goto bail; |
| 495 | } |
| 496 | } |
| 497 | #ifdef Py_UNICODE_WIDE |
| 498 | /* Surrogate pair */ |
| 499 | if (c >= 0xd800 && c <= 0xdbff) { |
| 500 | Py_UNICODE c2 = 0; |
| 501 | if (end + 6 >= len) { |
| 502 | raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, |
| 503 | end - 5); |
| 504 | } |
| 505 | if (buf[next++] != '\\' || buf[next++] != 'u') { |
| 506 | raise_errmsg("Invalid \\uXXXX\\uXXXX surrogate pair", pystr, |
| 507 | end - 5); |
| 508 | } |
| 509 | end += 6; |
| 510 | /* Decode 4 hex digits */ |
| 511 | for (; next < end; next++) { |
| 512 | Py_ssize_t shl = (end - next - 1) << 2; |
| 513 | Py_UNICODE digit = buf[next]; |
| 514 | switch (digit) { |
| 515 | case '0': case '1': case '2': case '3': case '4': |
| 516 | case '5': case '6': case '7': case '8': case '9': |
| 517 | c2 |= (digit - '0') << shl; break; |
| 518 | case 'a': case 'b': case 'c': case 'd': case 'e': |
| 519 | case 'f': |
| 520 | c2 |= (digit - 'a' + 10) << shl; break; |
| 521 | case 'A': case 'B': case 'C': case 'D': case 'E': |
| 522 | case 'F': |
| 523 | c2 |= (digit - 'A' + 10) << shl; break; |
| 524 | default: |
| 525 | raise_errmsg("Invalid \\uXXXX escape", pystr, end - 5); |
| 526 | goto bail; |
| 527 | } |
| 528 | } |
| 529 | c = 0x10000 + (((c - 0xd800) << 10) | (c2 - 0xdc00)); |
| 530 | } |
| 531 | #endif |
| 532 | } |
| 533 | chunk = PyUnicode_FromUnicode(&c, 1); |
| 534 | if (chunk == NULL) { |
| 535 | goto bail; |
| 536 | } |
| 537 | if (PyList_Append(chunks, chunk)) { |
| 538 | goto bail; |
| 539 | } |
| 540 | Py_DECREF(chunk); |
| 541 | } |
| 542 | |
| 543 | rval = join_list_unicode(chunks); |
| 544 | if (rval == NULL) { |
| 545 | goto bail; |
| 546 | } |
| 547 | Py_DECREF(chunks); |
| 548 | chunks = NULL; |
| 549 | return Py_BuildValue("(Nn)", rval, end); |
| 550 | bail: |
| 551 | Py_XDECREF(chunks); |
| 552 | return NULL; |
| 553 | } |
| 554 | |
| 555 | PyDoc_STRVAR(pydoc_scanstring, |
| 556 | "scanstring(str_or_bytes, end, encoding) -> (bytes, end)\n"); |
| 557 | |
| 558 | static PyObject * |
| 559 | py_scanstring(PyObject* self, PyObject *args) |
| 560 | { |
| 561 | PyObject *pystr; |
| 562 | Py_ssize_t end; |
| 563 | char *encoding = NULL; |
| 564 | int strict = 0; |
| 565 | if (!PyArg_ParseTuple(args, "On|zi:scanstring", &pystr, &end, &encoding, &strict)) { |
| 566 | return NULL; |
| 567 | } |
| 568 | if (encoding == NULL) { |
| 569 | encoding = DEFAULT_ENCODING; |
| 570 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 571 | if (PyBytes_Check(pystr)) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 572 | return scanstring_str(pystr, end, encoding, strict); |
| 573 | } |
| 574 | else if (PyUnicode_Check(pystr)) { |
| 575 | return scanstring_unicode(pystr, end, strict); |
| 576 | } |
| 577 | else { |
| 578 | PyErr_Format(PyExc_TypeError, |
| 579 | "first argument must be a string or bytes, not %.80s", |
| 580 | Py_TYPE(pystr)->tp_name); |
| 581 | return NULL; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | PyDoc_STRVAR(pydoc_encode_basestring_ascii, |
| 586 | "encode_basestring_ascii(str_or_bytes) -> bytes\n"); |
| 587 | |
| 588 | static PyObject * |
| 589 | py_encode_basestring_ascii(PyObject* self, PyObject *pystr) |
| 590 | { |
| 591 | PyObject *rval; |
| 592 | /* METH_O */ |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 593 | if (PyBytes_Check(pystr)) { |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 594 | rval = ascii_escape_str(pystr); |
| 595 | } |
| 596 | else if (PyUnicode_Check(pystr)) { |
| 597 | rval = ascii_escape_unicode(pystr); |
| 598 | } |
| 599 | else { |
| 600 | PyErr_Format(PyExc_TypeError, |
| 601 | "first argument must be a string or unicode, not %.80s", |
| 602 | Py_TYPE(pystr)->tp_name); |
| 603 | return NULL; |
| 604 | } |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 605 | if (PyBytes_Check(rval)) { |
| 606 | PyObject *urval = PyUnicode_DecodeASCII(PyBytes_AS_STRING(rval), PyBytes_GET_SIZE(rval), NULL); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 607 | Py_DECREF(rval); |
| 608 | return urval; |
| 609 | } |
| 610 | return rval; |
| 611 | } |
| 612 | |
| 613 | static PyMethodDef json_methods[] = { |
| 614 | {"encode_basestring_ascii", (PyCFunction)py_encode_basestring_ascii, |
| 615 | METH_O, pydoc_encode_basestring_ascii}, |
| 616 | {"scanstring", (PyCFunction)py_scanstring, METH_VARARGS, |
| 617 | pydoc_scanstring}, |
| 618 | {NULL, NULL, 0, NULL} |
| 619 | }; |
| 620 | |
| 621 | PyDoc_STRVAR(module_doc, |
| 622 | "json speedups\n"); |
| 623 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 624 | static struct PyModuleDef jsonmodule = { |
| 625 | PyModuleDef_HEAD_INIT, |
| 626 | "_json", |
| 627 | module_doc, |
| 628 | -1, |
| 629 | json_methods, |
| 630 | NULL, |
| 631 | NULL, |
| 632 | NULL, |
| 633 | NULL |
| 634 | }; |
| 635 | |
| 636 | PyObject* |
| 637 | PyInit__json(void) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 638 | { |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 639 | return PyModule_Create(&jsonmodule); |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 640 | } |