hamlit 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,249 +0,0 @@
1
- /*
2
- * Copyright (C) the libgit2 contributors. All rights reserved.
3
- *
4
- * This file is part of libgit2, distributed under the GNU GPL v2 with
5
- * a Linking Exception. For full terms see the included COPYING file.
6
- */
7
- #include <stdarg.h>
8
- #include <ctype.h>
9
- #include <string.h>
10
- #include <assert.h>
11
- #include <string.h>
12
- #include <stdio.h>
13
- #include <stdlib.h>
14
- #include <sys/param.h>
15
-
16
- #include "buffer.h"
17
-
18
- /* Used as default value for gh_buf->ptr so that people can always
19
- * assume ptr is non-NULL and zero terminated even for new gh_bufs.
20
- */
21
- char gh_buf__initbuf[1];
22
- char gh_buf__oom[1];
23
-
24
- #define ENSURE_SIZE(b, d) \
25
- if ((d) > buf->asize && gh_buf_grow(b, (d)) < 0)\
26
- return -1;
27
-
28
- void gh_buf_init(gh_buf *buf, size_t initial_size)
29
- {
30
- buf->asize = 0;
31
- buf->size = 0;
32
- buf->ptr = gh_buf__initbuf;
33
-
34
- if (initial_size)
35
- gh_buf_grow(buf, initial_size);
36
- }
37
-
38
- int gh_buf_try_grow(gh_buf *buf, size_t target_size, bool mark_oom)
39
- {
40
- char *new_ptr;
41
- size_t new_size;
42
-
43
- if (buf->ptr == gh_buf__oom)
44
- return -1;
45
-
46
- if (target_size <= buf->asize)
47
- return 0;
48
-
49
- if (buf->asize == 0) {
50
- new_size = target_size;
51
- new_ptr = NULL;
52
- } else {
53
- new_size = buf->asize;
54
- new_ptr = buf->ptr;
55
- }
56
-
57
- /* grow the buffer size by 1.5, until it's big enough
58
- * to fit our target size */
59
- while (new_size < target_size)
60
- new_size = (new_size << 1) - (new_size >> 1);
61
-
62
- /* round allocation up to multiple of 8 */
63
- new_size = (new_size + 7) & ~7;
64
-
65
- new_ptr = realloc(new_ptr, new_size);
66
-
67
- if (!new_ptr) {
68
- if (mark_oom)
69
- buf->ptr = gh_buf__oom;
70
- return -1;
71
- }
72
-
73
- buf->asize = new_size;
74
- buf->ptr = new_ptr;
75
-
76
- /* truncate the existing buffer size if necessary */
77
- if (buf->size >= buf->asize)
78
- buf->size = buf->asize - 1;
79
- buf->ptr[buf->size] = '\0';
80
-
81
- return 0;
82
- }
83
-
84
- void gh_buf_free(gh_buf *buf)
85
- {
86
- if (!buf) return;
87
-
88
- if (buf->ptr != gh_buf__initbuf && buf->ptr != gh_buf__oom)
89
- free(buf->ptr);
90
-
91
- gh_buf_init(buf, 0);
92
- }
93
-
94
- void gh_buf_clear(gh_buf *buf)
95
- {
96
- buf->size = 0;
97
- if (buf->asize > 0)
98
- buf->ptr[0] = '\0';
99
- }
100
-
101
- int gh_buf_set(gh_buf *buf, const char *data, size_t len)
102
- {
103
- if (len == 0 || data == NULL) {
104
- gh_buf_clear(buf);
105
- } else {
106
- if (data != buf->ptr) {
107
- ENSURE_SIZE(buf, len + 1);
108
- memmove(buf->ptr, data, len);
109
- }
110
- buf->size = len;
111
- buf->ptr[buf->size] = '\0';
112
- }
113
- return 0;
114
- }
115
-
116
- int gh_buf_sets(gh_buf *buf, const char *string)
117
- {
118
- return gh_buf_set(buf, string, string ? strlen(string) : 0);
119
- }
120
-
121
- int gh_buf_putc(gh_buf *buf, char c)
122
- {
123
- ENSURE_SIZE(buf, buf->size + 2);
124
- buf->ptr[buf->size++] = c;
125
- buf->ptr[buf->size] = '\0';
126
- return 0;
127
- }
128
-
129
- int gh_buf_put(gh_buf *buf, const void *data, size_t len)
130
- {
131
- ENSURE_SIZE(buf, buf->size + len + 1);
132
- memmove(buf->ptr + buf->size, data, len);
133
- buf->size += len;
134
- buf->ptr[buf->size] = '\0';
135
- return 0;
136
- }
137
-
138
- int gh_buf_puts(gh_buf *buf, const char *string)
139
- {
140
- assert(string);
141
- return gh_buf_put(buf, string, strlen(string));
142
- }
143
-
144
- int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap)
145
- {
146
- int len;
147
- const size_t expected_size = buf->size + (strlen(format) * 2);
148
-
149
- ENSURE_SIZE(buf, expected_size);
150
-
151
- while (1) {
152
- va_list args;
153
- va_copy(args, ap);
154
-
155
- len = vsnprintf(
156
- buf->ptr + buf->size,
157
- buf->asize - buf->size,
158
- format, args
159
- );
160
-
161
- if (len < 0) {
162
- free(buf->ptr);
163
- buf->ptr = gh_buf__oom;
164
- return -1;
165
- }
166
-
167
- if ((size_t)len + 1 <= buf->asize - buf->size) {
168
- buf->size += len;
169
- break;
170
- }
171
-
172
- ENSURE_SIZE(buf, buf->size + len + 1);
173
- }
174
-
175
- return 0;
176
- }
177
-
178
- int gh_buf_printf(gh_buf *buf, const char *format, ...)
179
- {
180
- int r;
181
- va_list ap;
182
-
183
- va_start(ap, format);
184
- r = gh_buf_vprintf(buf, format, ap);
185
- va_end(ap);
186
-
187
- return r;
188
- }
189
-
190
- void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf)
191
- {
192
- size_t copylen;
193
-
194
- assert(data && datasize && buf);
195
-
196
- data[0] = '\0';
197
-
198
- if (buf->size == 0 || buf->asize <= 0)
199
- return;
200
-
201
- copylen = buf->size;
202
- if (copylen > datasize - 1)
203
- copylen = datasize - 1;
204
- memmove(data, buf->ptr, copylen);
205
- data[copylen] = '\0';
206
- }
207
-
208
- void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b)
209
- {
210
- gh_buf t = *buf_a;
211
- *buf_a = *buf_b;
212
- *buf_b = t;
213
- }
214
-
215
- char *gh_buf_detach(gh_buf *buf)
216
- {
217
- char *data = buf->ptr;
218
-
219
- if (buf->asize == 0 || buf->ptr == gh_buf__oom)
220
- return NULL;
221
-
222
- gh_buf_init(buf, 0);
223
-
224
- return data;
225
- }
226
-
227
- void gh_buf_attach(gh_buf *buf, char *ptr, size_t asize)
228
- {
229
- gh_buf_free(buf);
230
-
231
- if (ptr) {
232
- buf->ptr = ptr;
233
- buf->size = strlen(ptr);
234
- if (asize)
235
- buf->asize = (asize < buf->size) ? buf->size + 1 : asize;
236
- else /* pass 0 to fall back on strlen + 1 */
237
- buf->asize = buf->size + 1;
238
- } else {
239
- gh_buf_grow(buf, asize);
240
- }
241
- }
242
-
243
- int gh_buf_cmp(const gh_buf *a, const gh_buf *b)
244
- {
245
- int result = memcmp(a->ptr, b->ptr, a->size < b->size ? a->size : b->size);
246
- return (result != 0) ? result :
247
- (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0;
248
- }
249
-
@@ -1,113 +0,0 @@
1
- /*
2
- * Copyright (C) the libgit2 contributors. All rights reserved.
3
- *
4
- * This file is part of libgit2, distributed under the GNU GPL v2 with
5
- * a Linking Exception. For full terms see the included COPYING file.
6
- */
7
- #ifndef INCLUDE_buffer_h__
8
- #define INCLUDE_buffer_h__
9
-
10
- #include <stdbool.h>
11
- #include <stddef.h>
12
- #include <stdarg.h>
13
- #include <sys/types.h>
14
- #include <stdint.h>
15
-
16
- typedef struct {
17
- char *ptr;
18
- size_t asize, size;
19
- } gh_buf;
20
-
21
- extern char gh_buf__initbuf[];
22
- extern char gh_buf__oom[];
23
-
24
- #define GH_BUF_INIT { gh_buf__initbuf, 0, 0 }
25
-
26
- /**
27
- * Initialize a gh_buf structure.
28
- *
29
- * For the cases where GH_BUF_INIT cannot be used to do static
30
- * initialization.
31
- */
32
- extern void gh_buf_init(gh_buf *buf, size_t initial_size);
33
-
34
- /**
35
- * Attempt to grow the buffer to hold at least `target_size` bytes.
36
- *
37
- * If the allocation fails, this will return an error. If mark_oom is true,
38
- * this will mark the buffer as invalid for future operations; if false,
39
- * existing buffer content will be preserved, but calling code must handle
40
- * that buffer was not expanded.
41
- */
42
- extern int gh_buf_try_grow(gh_buf *buf, size_t target_size, bool mark_oom);
43
-
44
- /**
45
- * Grow the buffer to hold at least `target_size` bytes.
46
- *
47
- * If the allocation fails, this will return an error and the buffer will be
48
- * marked as invalid for future operations, invaliding contents.
49
- *
50
- * @return 0 on success or -1 on failure
51
- */
52
- static inline int gh_buf_grow(gh_buf *buf, size_t target_size)
53
- {
54
- return gh_buf_try_grow(buf, target_size, true);
55
- }
56
-
57
- extern void gh_buf_free(gh_buf *buf);
58
- extern void gh_buf_swap(gh_buf *buf_a, gh_buf *buf_b);
59
-
60
- /**
61
- * Test if there have been any reallocation failures with this gh_buf.
62
- *
63
- * Any function that writes to a gh_buf can fail due to memory allocation
64
- * issues. If one fails, the gh_buf will be marked with an OOM error and
65
- * further calls to modify the buffer will fail. Check gh_buf_oom() at the
66
- * end of your sequence and it will be true if you ran out of memory at any
67
- * point with that buffer.
68
- *
69
- * @return false if no error, true if allocation error
70
- */
71
- static inline bool gh_buf_oom(const gh_buf *buf)
72
- {
73
- return (buf->ptr == gh_buf__oom);
74
- }
75
-
76
-
77
- static inline size_t gh_buf_len(const gh_buf *buf)
78
- {
79
- return buf->size;
80
- }
81
-
82
- extern int gh_buf_cmp(const gh_buf *a, const gh_buf *b);
83
-
84
- extern void gh_buf_attach(gh_buf *buf, char *ptr, size_t asize);
85
- extern char *gh_buf_detach(gh_buf *buf);
86
- extern void gh_buf_copy_cstr(char *data, size_t datasize, const gh_buf *buf);
87
-
88
- static inline const char *gh_buf_cstr(const gh_buf *buf)
89
- {
90
- return buf->ptr;
91
- }
92
-
93
- /*
94
- * Functions below that return int value error codes will return 0 on
95
- * success or -1 on failure (which generally means an allocation failed).
96
- * Using a gh_buf where the allocation has failed with result in -1 from
97
- * all further calls using that buffer. As a result, you can ignore the
98
- * return code of these functions and call them in a series then just call
99
- * gh_buf_oom at the end.
100
- */
101
- extern int gh_buf_set(gh_buf *buf, const char *data, size_t len);
102
- extern int gh_buf_sets(gh_buf *buf, const char *string);
103
- extern int gh_buf_putc(gh_buf *buf, char c);
104
- extern int gh_buf_put(gh_buf *buf, const void *data, size_t len);
105
- extern int gh_buf_puts(gh_buf *buf, const char *string);
106
- extern int gh_buf_printf(gh_buf *buf, const char *format, ...)
107
- __attribute__((format (printf, 2, 3)));
108
- extern int gh_buf_vprintf(gh_buf *buf, const char *format, va_list ap);
109
- extern void gh_buf_clear(gh_buf *buf);
110
-
111
- #define gh_buf_PUTS(buf, str) gh_buf_put(buf, str, sizeof(str) - 1)
112
-
113
- #endif
@@ -1,46 +0,0 @@
1
- #ifndef __HOUDINI_H__
2
- #define __HOUDINI_H__
3
-
4
- #ifdef __cplusplus
5
- extern "C" {
6
- #endif
7
-
8
- #include <stdint.h>
9
- #include "buffer.h"
10
-
11
- #define likely(x) __builtin_expect((x),1)
12
- #define unlikely(x) __builtin_expect((x),0)
13
-
14
- #ifdef HOUDINI_USE_LOCALE
15
- # define _isxdigit(c) isxdigit(c)
16
- # define _isdigit(c) isdigit(c)
17
- #else
18
- /*
19
- * Helper _isdigit methods -- do not trust the current locale
20
- * */
21
- # define _isxdigit(c) (strchr("0123456789ABCDEFabcdef", (c)) != NULL)
22
- # define _isdigit(c) ((c) >= '0' && (c) <= '9')
23
- #endif
24
-
25
- #define HOUDINI_ESCAPED_SIZE(x) (((x) * 12) / 10)
26
- #define HOUDINI_UNESCAPED_SIZE(x) (x)
27
-
28
- extern int houdini_escape_html(gh_buf *ob, const uint8_t *src, size_t size);
29
- extern int houdini_escape_html0(gh_buf *ob, const uint8_t *src, size_t size, int secure);
30
- extern int houdini_unescape_html(gh_buf *ob, const uint8_t *src, size_t size);
31
- extern int houdini_escape_xml(gh_buf *ob, const uint8_t *src, size_t size);
32
- extern int houdini_escape_uri(gh_buf *ob, const uint8_t *src, size_t size);
33
- extern int houdini_escape_uri_component(gh_buf *ob, const uint8_t *src, size_t size);
34
- extern int houdini_escape_url(gh_buf *ob, const uint8_t *src, size_t size);
35
- extern int houdini_escape_href(gh_buf *ob, const uint8_t *src, size_t size);
36
- extern int houdini_unescape_uri(gh_buf *ob, const uint8_t *src, size_t size);
37
- extern int houdini_unescape_uri_component(gh_buf *ob, const uint8_t *src, size_t size);
38
- extern int houdini_unescape_url(gh_buf *ob, const uint8_t *src, size_t size);
39
- extern int houdini_escape_js(gh_buf *ob, const uint8_t *src, size_t size);
40
- extern int houdini_unescape_js(gh_buf *ob, const uint8_t *src, size_t size);
41
-
42
- #ifdef __cplusplus
43
- }
44
- #endif
45
-
46
- #endif
@@ -1,115 +0,0 @@
1
- #include <assert.h>
2
- #include <stdio.h>
3
- #include <string.h>
4
-
5
- #include "houdini.h"
6
-
7
- /*
8
- * The following characters will not be escaped:
9
- *
10
- * -_.+!*'(),%#@?=;:/,+&$ alphanum
11
- *
12
- * Note that this character set is the addition of:
13
- *
14
- * - The characters which are safe to be in an URL
15
- * - The characters which are *not* safe to be in
16
- * an URL because they are RESERVED characters.
17
- *
18
- * We asume (lazily) that any RESERVED char that
19
- * appears inside an URL is actually meant to
20
- * have its native function (i.e. as an URL
21
- * component/separator) and hence needs no escaping.
22
- *
23
- * There are two exceptions: the chacters & (amp)
24
- * and ' (single quote) do not appear in the table.
25
- * They are meant to appear in the URL as components,
26
- * yet they require special HTML-entity escaping
27
- * to generate valid HTML markup.
28
- *
29
- * All other characters will be escaped to %XX.
30
- *
31
- */
32
- static const char HREF_SAFE[] = {
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
- 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
36
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1,
37
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
38
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
39
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49
- };
50
-
51
- int
52
- houdini_escape_href(gh_buf *ob, const uint8_t *src, size_t size)
53
- {
54
- static const uint8_t hex_chars[] = "0123456789ABCDEF";
55
- size_t i = 0, org;
56
- uint8_t hex_str[3];
57
-
58
- hex_str[0] = '%';
59
-
60
- while (i < size) {
61
- org = i;
62
- while (i < size && HREF_SAFE[src[i]] != 0)
63
- i++;
64
-
65
- if (likely(i > org)) {
66
- if (unlikely(org == 0)) {
67
- if (i >= size)
68
- return 0;
69
-
70
- gh_buf_grow(ob, HOUDINI_ESCAPED_SIZE(size));
71
- }
72
-
73
- gh_buf_put(ob, src + org, i - org);
74
- }
75
-
76
- /* escaping */
77
- if (i >= size)
78
- break;
79
-
80
- switch (src[i]) {
81
- /* amp appears all the time in URLs, but needs
82
- * HTML-entity escaping to be inside an href */
83
- case '&':
84
- gh_buf_PUTS(ob, "&amp;");
85
- break;
86
-
87
- /* the single quote is a valid URL character
88
- * according to the standard; it needs HTML
89
- * entity escaping too */
90
- case '\'':
91
- gh_buf_PUTS(ob, "&#x27;");
92
- break;
93
-
94
- /* the space can be escaped to %20 or a plus
95
- * sign. we're going with the generic escape
96
- * for now. the plus thing is more commonly seen
97
- * when building GET strings */
98
- #if 0
99
- case ' ':
100
- gh_buf_putc(ob, '+');
101
- break;
102
- #endif
103
-
104
- /* every other character goes with a %XX escaping */
105
- default:
106
- hex_str[1] = hex_chars[(src[i] >> 4) & 0xF];
107
- hex_str[2] = hex_chars[src[i] & 0xF];
108
- gh_buf_put(ob, hex_str, 3);
109
- }
110
-
111
- i++;
112
- }
113
-
114
- return 1;
115
- }