summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2022-04-06 01:06:30 +0200
committerGravatar Nikias Bassen2022-04-06 01:10:08 +0200
commitdb93bae96d64140230ad050061632531644c46ad (patch)
tree165f690d141b87a4b6871859f140136a4bdebf0a
parentd25e919f29078127b0305f88cdcbc33a798c5d90 (diff)
downloadlibplist-db93bae96d64140230ad050061632531644c46ad.tar.gz
libplist-db93bae96d64140230ad050061632531644c46ad.tar.bz2
jplist: Escape characters [0x00..0x1F] when converting to JSON
-rw-r--r--src/jplist.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/jplist.c b/src/jplist.c
index 45f0544..6a44f8c 100644
--- a/src/jplist.c
+++ b/src/jplist.c
@@ -151,6 +151,12 @@ static int node_to_json(node_t* node, bytearray_t **outbuf, uint32_t depth, int
151 151
152 case PLIST_STRING: 152 case PLIST_STRING:
153 case PLIST_KEY: { 153 case PLIST_KEY: {
154 const char *charmap[32] = {
155 "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004", "\\u0005", "\\u0006", "\\u0007",
156 "\\b", "\\t", "\\n", "\\u000b", "\\f", "\\r", "\\u000e", "\\u000f",
157 "\\u0010", "\\u0011", "\\u0012", "\\u0013", "\\u0014", "\\u0015", "\\u0016", "\\u0017",
158 "\\u0018", "\\u0019", "\\u001a", "\\u001b", "\\u001c", "\\u001d", "\\u001e", "\\u001f",
159 };
154 size_t j = 0; 160 size_t j = 0;
155 size_t len = 0; 161 size_t len = 0;
156 off_t start = 0; 162 off_t start = 0;
@@ -160,14 +166,15 @@ static int node_to_json(node_t* node, bytearray_t **outbuf, uint32_t depth, int
160 166
161 len = node_data->length; 167 len = node_data->length;
162 for (j = 0; j < len; j++) { 168 for (j = 0; j < len; j++) {
163 switch (node_data->strval[j]) { 169 unsigned char ch = (unsigned char)node_data->strval[j];
164 case '"': 170 if (ch < 0x20) {
171 str_buf_append(*outbuf, node_data->strval + start, cur - start);
172 str_buf_append(*outbuf, charmap[ch], (charmap[ch][1] == 'u') ? 6 : 2);
173 start = cur+1;
174 } else if (ch == '"') {
165 str_buf_append(*outbuf, node_data->strval + start, cur - start); 175 str_buf_append(*outbuf, node_data->strval + start, cur - start);
166 str_buf_append(*outbuf, "\\\"", 2); 176 str_buf_append(*outbuf, "\\\"", 2);
167 start = cur+1; 177 start = cur+1;
168 break;
169 default:
170 break;
171 } 178 }
172 cur++; 179 cur++;
173 } 180 }