File Coverage

houdini/houdini_uri_e.c
Criterion Covered Total %
statement 21 23 91.3
branch 18 18 100.0
condition n/a
subroutine n/a
pod n/a
total 39 41 95.1


line stmt bran cond sub pod time code
1             #include
2             #include
3             #include
4              
5             #include "houdini.h"
6              
7             static const char URL_SAFE[] = {
8             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
11             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
12             0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
13             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
14             0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
15             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
16             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
17             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24             };
25              
26             static const char URI_SAFE[] = {
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, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
30             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
31             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
32             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
33             0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
34             1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
35             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
39             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
40             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
41             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43             };
44              
45             static int
46 8           escape(gh_buf *ob, const uint8_t *src, size_t size,
47             const char *safe_table, bool escape_plus)
48             {
49             static const uint8_t hex_chars[] = "0123456789ABCDEF";
50              
51             size_t i = 0, org;
52             uint8_t hex_str[3];
53              
54 8           hex_str[0] = '%';
55              
56 27 100         while (i < size) {
57             org = i;
58 102 100         while (i < size && safe_table[src[i]] != 0)
    100          
59 77           i++;
60              
61 25 100         if (likely(i > org)) {
62 14 100         if (unlikely(org == 0)) {
63 6 100         if (i >= size)
64             return 0;
65              
66 5           gh_buf_grow(ob, HOUDINI_ESCAPED_SIZE(size));
67             }
68              
69 13           gh_buf_put(ob, src + org, i - org);
70             }
71              
72             /* escaping */
73 24 100         if (i >= size)
74             break;
75              
76 19 100         if (src[i] == ' ' && escape_plus) {
    100          
77 1           gh_buf_putc(ob, '+');
78             } else {
79 18           hex_str[1] = hex_chars[(src[i] >> 4) & 0xF];
80 18           hex_str[2] = hex_chars[src[i] & 0xF];
81 18           gh_buf_put(ob, hex_str, 3);
82             }
83              
84 19           i++;
85             }
86              
87             return 1;
88             }
89              
90             int
91 4           houdini_escape_uri(gh_buf *ob, const uint8_t *src, size_t size)
92             {
93 4           return escape(ob, src, size, URI_SAFE, false);
94             }
95              
96             int
97 0           houdini_escape_uri_component(gh_buf *ob, const uint8_t *src, size_t size)
98             {
99 0           return escape(ob, src, size, URL_SAFE, false);
100             }
101              
102             int
103 4           houdini_escape_url(gh_buf *ob, const uint8_t *src, size_t size)
104             {
105 4           return escape(ob, src, size, URL_SAFE, true);
106             }
107