-
Notifications
You must be signed in to change notification settings - Fork 1
/
unescape.c
156 lines (147 loc) · 3.8 KB
/
unescape.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2019 YASUOKA Masahiko <yasuoka@yasuoka.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stdlib.h>
#define IS_XDIGIT(_c) ( \
(('0' <= (_c) && (_c) <= '9')) || \
(('a' <= (_c) && (_c) <= 'f')) || \
(('A' <= (_c) && (_c) <= 'F')))
#define XDIGIT(_c) ( \
(('0' <= (_c) && (_c) <= '9'))? (_c) - '0' : \
(('a' <= (_c) && (_c) <= 'f'))? (_c) - 'a' + 10 : \
(('A' <= (_c) && (_c) <= 'F'))? (_c) - 'A' + 10 : (-1))
const char *unescape(const char *, char *, size_t);
/*
* Unescape the given JSON string reprentation to UTF-8 string.
* Reference: RFC 7158
*/
const char *
unescape(const char *in, char *out, size_t outsiz)
{
int i, j, esc = 0;
uint16_t W1, W2;
uint32_t U;
for (i = 0, j = 0; in[i] != '\0'; i++) {
if (j >= outsiz)
goto fail;
if (!esc) {
switch (in[i]) {
case '\\':
esc = 1;
break;
default:
out[j++] = in[i];
break;
}
} else {
esc = 0;
switch (in[i]) {
case '"':
case '\\':
case '/':
out[j++] = in[i];
break;
default:
goto fail;
case 'b':
out[j++] = 0x08;
break;
case 'f':
out[j++] = 0x0c;
break;
case 'n':
out[j++] = 0x0a;
break;
case 'r':
out[j++] = 0x0d;
break;
case 't':
out[j++] = 0x09;
break;
case 'u':
/*
* RFC 2781, 2.2 Decoding UTF-16
*/
if (!IS_XDIGIT(in[i + 1]) ||
!IS_XDIGIT(in[i + 2]) ||
!IS_XDIGIT(in[i + 3]) ||
!IS_XDIGIT(in[i + 4]))
goto fail;
W1 =
(XDIGIT(in[i + 1]) << 12) |
(XDIGIT(in[i + 2]) << 8) |
(XDIGIT(in[i + 3]) << 4) |
XDIGIT(in[i + 4]);
i += 5;
if (W1 < 0xD800 || 0xDFFF < W1)
U = W1;
else if (!(0xD800 <= W1 && W1 <= 0xDBFF))
goto fail;
else {
if (in[i] != '\\' ||
in[i + 1] != 'u' ||
!IS_XDIGIT(in[i + 2]) ||
!IS_XDIGIT(in[i + 3]) ||
!IS_XDIGIT(in[i + 4]) ||
!IS_XDIGIT(in[i + 5]))
goto fail;
W2 =
(XDIGIT(in[i + 2]) << 12) |
(XDIGIT(in[i + 3]) << 8) |
(XDIGIT(in[i + 4]) << 4) |
XDIGIT(in[i + 5]);
i += 6;
if (!(0xDC00 <= W2 && W2 <= 0xDFFF))
goto fail;
U = ((W1 & 0x3FF) << 10) | (W2 & 0x3FF);
U += 0x10000;
}
/*
* RFC 3629, 3. UTF-8 definition
*/
if (U <= 0x0000007F)
out[j++] = U & 0x7F;
else if (U <= 0x000007FF) {
if (j + 1 >= outsiz)
goto fail;
out[j++] = 0xC0 | ((U >> 6) & 0x1F);
out[j++] = 0x80 | (U & 0x3F);
} else if (U <= 0x0000FFFF) {
if (j + 2 >= outsiz)
goto fail;
out[j++] = 0xE0 | ((U >> 12) & 0x0F);
out[j++] = 0x80 | ((U >> 6) & 0x3F);
out[j++] = 0x80 | (U & 0x3F);
} else if (0x00010000 <= U && U <= 0x0010FFFF) {
if (j + 3 >= outsiz)
goto fail;
out[j++] = 0xF0 | ((U >> 18) & 0x07);
out[j++] = 0x80 | ((U >> 12) & 0x3F);
out[j++] = 0x80 | ((U >> 6) & 0x3F);
out[j++] = 0x80 | (U & 0x3F);
} else
goto fail;
--i; /* prepare for ++ at the for loop */
}
}
}
if (j >= outsiz)
goto fail;
out[j++] = '\0';
return (out);
fail:
return (NULL);
}