|
| 1 | +/* |
| 2 | + * Redistribution and use in source and binary forms, with or |
| 3 | + * without modification, are permitted provided that the following |
| 4 | + * conditions are met: |
| 5 | + * |
| 6 | + * 1. Redistributions of source code must retain the above |
| 7 | + * copyright notice, this list of conditions and the |
| 8 | + * following disclaimer. |
| 9 | + * |
| 10 | + * 2. Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following |
| 12 | + * disclaimer in the documentation and/or other materials |
| 13 | + * provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY AUTHORS ``AS IS'' AND |
| 16 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 17 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 19 | + * AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 20 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 26 | + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | + * SUCH DAMAGE. |
| 28 | + * |
| 29 | + * Copyright (C) 2016 Tarantool AUTHORS: |
| 30 | + * please see AUTHORS file. |
| 31 | + */ |
| 32 | + |
| 33 | +#include "json_encoders.h" |
| 34 | + |
| 35 | + |
| 36 | +static inline void |
| 37 | +hex_buf_init(char *hex_buf) |
| 38 | +{ |
| 39 | + hex_buf[0] = '\\'; |
| 40 | + hex_buf[1] = 'u'; |
| 41 | + hex_buf[2] = '0'; |
| 42 | + hex_buf[3] = '0'; |
| 43 | + hex_buf[6] = 0; |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +static inline void |
| 48 | +char_to_hex(char *hex_buf, unsigned char c) |
| 49 | +{ |
| 50 | + static const char * hexchar = "0123456789ABCDEF"; |
| 51 | + hex_buf[0] = hexchar[c >> 4]; |
| 52 | + hex_buf[1] = hexchar[c & 0x0F]; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +const char * /*error message*/ |
| 57 | +json_encode_string( |
| 58 | + char **buf, size_t buf_len, |
| 59 | + const char *str, size_t str_len, |
| 60 | + bool escape_solidus) |
| 61 | +{ |
| 62 | + if (!buf || !*buf || !buf_len || !str || !str_len) |
| 63 | + return "json_encode_string: invalid arguments"; |
| 64 | + |
| 65 | + char hex_buf[7]; |
| 66 | + |
| 67 | + hex_buf_init(hex_buf); |
| 68 | + |
| 69 | + if (!append_ch(buf, &buf_len, '\"')) |
| 70 | + goto oom; |
| 71 | + |
| 72 | + for (;str_len != 0; --str_len, ++str) { |
| 73 | + |
| 74 | + const char ch = *str; |
| 75 | + const char *escaped = NULL; |
| 76 | + |
| 77 | + switch (ch) { |
| 78 | + case '\r': escaped = "\\r"; break; |
| 79 | + case '\n': escaped = "\\n"; break; |
| 80 | + case '\\': escaped = "\\\\"; break; |
| 81 | + /* it is not required to escape a solidus in JSON: |
| 82 | + * read sec. 2.5: http://www.ietf.org/rfc/rfc4627.txt |
| 83 | + * specifically, this production from the grammar: |
| 84 | + * unescaped = %x20-21 / %x23-5B / %x5D-10FFFF |
| 85 | + */ |
| 86 | + case '/': if (escape_solidus) escaped = "\\/"; break; |
| 87 | + case '"': escaped = "\\\""; break; |
| 88 | + case '\f': escaped = "\\f"; break; |
| 89 | + case '\b': escaped = "\\b"; break; |
| 90 | + case '\t': escaped = "\\t"; break; |
| 91 | + default: |
| 92 | + if ((unsigned char) ch < 32) { |
| 93 | + char_to_hex(hex_buf + 4, ch); |
| 94 | + escaped = hex_buf; |
| 95 | + } |
| 96 | + break; |
| 97 | + } /* switch */ |
| 98 | + |
| 99 | + if (escaped) { |
| 100 | + if (!append_str(buf, &buf_len, escaped, strlen(escaped))) |
| 101 | + goto oom; |
| 102 | + } else { |
| 103 | + if (!append_ch(buf, &buf_len, ch)) |
| 104 | + goto oom; |
| 105 | + } |
| 106 | + } /* for */ |
| 107 | + |
| 108 | + if (!append_ch(buf, &buf_len, '\"')) |
| 109 | + goto oom; |
| 110 | + |
| 111 | + return NULL; |
| 112 | +oom: |
| 113 | + return "json_encode_string: out of memory"; |
| 114 | +} |
0 commit comments