forked from GROUP-Bank/houdini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoudini_js_e.c
90 lines (73 loc) · 1.84 KB
/
houdini_js_e.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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "houdini.h"
static const char JS_ESCAPE[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
int
houdini_escape_js(gh_buf *ob, const uint8_t *src, size_t size)
{
size_t i = 0, org, ch;
while (i < size) {
org = i;
while (i < size && JS_ESCAPE[src[i]] == 0)
i++;
if (likely(i > org)) {
if (unlikely(org == 0)) {
if (i >= size)
return 0;
gh_buf_grow(ob, HOUDINI_ESCAPED_SIZE(size));
}
gh_buf_put(ob, src + org, i - org);
}
/* escaping */
if (i >= size)
break;
ch = src[i];
switch (ch) {
case '/':
/*
* Escape only if preceded by a lt
*/
if (i && src[i - 1] == '<')
gh_buf_putc(ob, '\\');
gh_buf_putc(ob, ch);
break;
case '\r':
/*
* Escape as \n, and skip the next \n if it's there
*/
if (i + 1 < size && src[i + 1] == '\n') i++;
case '\n':
/*
* Escape actually as '\','n', not as '\', '\n'
*/
ch = 'n';
default:
/*
* Normal escaping
*/
gh_buf_putc(ob, '\\');
gh_buf_putc(ob, ch);
break;
}
i++;
}
return 1;
}