-
-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathescapejsstringliteral.c
More file actions
146 lines (144 loc) · 5.35 KB
/
escapejsstringliteral.c
File metadata and controls
146 lines (144 loc) · 5.35 KB
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
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2021 Justine Alexandra Roberts Tunney │
│ │
│ Permission to use, copy, modify, and/or 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 "libc/bits/likely.h"
#include "libc/str/thompike.h"
#include "libc/str/utf16.h"
#include "libc/x/x.h"
#include "net/http/escape.h"
static const char kEscapeLiteral[128] = {
9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 2, 9, 4, 3, 9, 9, // 0x00
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 0x10
0, 0, 7, 0, 0, 0, 9, 8, 0, 0, 0, 0, 0, 0, 0, 6, // 0x20
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 0, // 0x30
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x40
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, // 0x50
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x60
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, // 0x70
};
/**
* Escapes UTF-8 data for JavaScript or JSON string literal.
*
* HTML entities and forward slash are escaped too for added safety.
*
* We assume the UTF-8 is well-formed and can be represented as UTF-16.
* Things that can't be decoded will fall back to binary. Things that
* can't be encoded will use invalid codepoint markers. This function is
* agnostic to numbers that have been used with malicious intent in the
* past under buggy software. Noncanonical encodings such as overlong
* NUL are canonicalized as NUL. Therefore it isn't necessary to say
* EscapeJsStringLiteral(Underlong(𝑥)) since EscapeJsStringLiteral(𝑥)
* will do the same thing.
*
* @param p is input value
* @param n if -1 implies strlen
* @param out_size if non-NULL receives output length
* @return allocated NUL-terminated buffer, or NULL w/ errno
*/
char *EscapeJsStringLiteral(const char *p, size_t n, size_t *z) {
uint64_t w;
char *q, *r;
size_t i, j, m;
wint_t x, a, b;
if (z) *z = 0;
if (n == -1) n = p ? strlen(p) : 0;
if ((q = r = malloc(n * 6 + 6 + 1))) {
for (i = 0; i < n;) {
x = p[i++] & 0xff;
if (x >= 0300) {
a = ThomPikeByte(x);
m = ThomPikeLen(x) - 1;
if (i + m <= n) {
for (j = 0;;) {
b = p[i + j] & 0xff;
if (!ThomPikeCont(b)) break;
a = ThomPikeMerge(a, b);
if (++j == m) {
x = a;
i += j;
break;
}
}
}
}
switch (0 <= x && x <= 127 ? kEscapeLiteral[x] : 9) {
case 0:
*q++ = x;
break;
case 1:
q[0] = '\\';
q[1] = 't';
q += 2;
break;
case 2:
q[0] = '\\';
q[1] = 'n';
q += 2;
break;
case 3:
q[0] = '\\';
q[1] = 'r';
q += 2;
break;
case 4:
q[0] = '\\';
q[1] = 'f';
q += 2;
break;
case 5:
q[0] = '\\';
q[1] = '\\';
q += 2;
break;
case 6:
q[0] = '\\';
q[1] = '/';
q += 2;
break;
case 7:
q[0] = '\\';
q[1] = '"';
q += 2;
break;
case 8:
q[0] = '\\';
q[1] = '\'';
q += 2;
break;
case 9:
w = EncodeUtf16(x);
do {
q[0] = '\\';
q[1] = 'u';
q[2] = "0123456789abcdef"[(w & 0xF000) >> 014];
q[3] = "0123456789abcdef"[(w & 0x0F00) >> 010];
q[4] = "0123456789abcdef"[(w & 0x00F0) >> 004];
q[5] = "0123456789abcdef"[(w & 0x000F) >> 000];
q += 6;
} while ((w >>= 16));
break;
default:
unreachable;
}
}
if (z) *z = q - r;
*q++ = '\0';
if ((q = realloc(r, q - r))) r = q;
}
return r;
}