File Coverage

houdini/houdini_html_e.c
Criterion Covered Total %
statement 14 16 87.5
branch 15 18 83.3
condition n/a
subroutine n/a
pod n/a
total 29 34 85.2


line stmt bran cond sub pod time code
1             #include
2             #include
3             #include
4              
5             #include "houdini.h"
6              
7             /**
8             * According to the OWASP rules:
9             *
10             * & --> &
11             * < --> <
12             * > --> >
13             * " --> "
14             * ' --> ' ' is not recommended
15             * / --> / forward slash is included as it helps end an HTML entity
16             *
17             */
18             static const char HTML_ESCAPE_TABLE[] = {
19             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21             0, 0, 1, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0, 0, 4,
22             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 0, 6, 0,
23             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35             };
36              
37             static const char *HTML_ESCAPES[] = {
38             "",
39             """,
40             "&",
41             "'",
42             "/",
43             "<",
44             ">"
45             };
46              
47             int
48 7           houdini_escape_html0(gh_buf *ob, const uint8_t *src, size_t size, int secure)
49             {
50             size_t i = 0, org, esc = 0;
51              
52 25 100         while (i < size) {
53             org = i;
54 82 100         while (i < size && (esc = HTML_ESCAPE_TABLE[src[i]]) == 0)
    100          
55 61           i++;
56              
57 21 100         if (i > org) {
58 13 100         if (unlikely(org == 0)) {
59 3 50         if (i >= size)
60             return 0;
61              
62 0           gh_buf_grow(ob, HOUDINI_ESCAPED_SIZE(size));
63             }
64              
65 10           gh_buf_put(ob, src + org, i - org);
66             }
67              
68             /* escaping */
69 18 50         if (unlikely(i >= size))
70             break;
71              
72             /* The forward slash is only escaped in secure mode */
73 18 100         if (src[i] == '/' && !secure) {
    50          
74 0           gh_buf_putc(ob, '/');
75             } else {
76 18           gh_buf_puts(ob, HTML_ESCAPES[esc]);
77             }
78              
79 18           i++;
80             }
81              
82             return 1;
83             }
84              
85             int
86 7           houdini_escape_html(gh_buf *ob, const uint8_t *src, size_t size)
87             {
88 7           return houdini_escape_html0(ob, src, size, 1);
89             }
90