/src/libgit2/src/util/str.c
Line | Count | Source |
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 | | |
8 | | #include "str.h" |
9 | | #include "posix.h" |
10 | | #include <ctype.h> |
11 | | |
12 | | /* Used as default value for git_str->ptr so that people can always |
13 | | * assume ptr is non-NULL and zero terminated even for new git_strs. |
14 | | */ |
15 | | char git_str__initstr[1]; |
16 | | |
17 | | char git_str__oom[1]; |
18 | | |
19 | | #define ENSURE_SIZE(b, d) \ |
20 | 95.7k | if ((b)->ptr == git_str__oom || \ |
21 | 95.7k | ((d) > (b)->asize && git_str_grow((b), (d)) < 0))\ |
22 | 95.7k | return -1; |
23 | | |
24 | | |
25 | | int git_str_init(git_str *buf, size_t initial_size) |
26 | 15.1k | { |
27 | 15.1k | buf->asize = 0; |
28 | 15.1k | buf->size = 0; |
29 | 15.1k | buf->ptr = git_str__initstr; |
30 | | |
31 | 15.1k | ENSURE_SIZE(buf, initial_size); |
32 | | |
33 | 15.1k | return 0; |
34 | 15.1k | } |
35 | | |
36 | | int git_str_try_grow( |
37 | | git_str *buf, size_t target_size, bool mark_oom) |
38 | 8.50k | { |
39 | 8.50k | char *new_ptr; |
40 | 8.50k | size_t new_size; |
41 | | |
42 | 8.50k | if (buf->ptr == git_str__oom) |
43 | 0 | return -1; |
44 | | |
45 | 8.50k | if (buf->asize == 0 && buf->size != 0) { |
46 | 0 | git_error_set(GIT_ERROR_INVALID, "cannot grow a borrowed buffer"); |
47 | 0 | return GIT_EINVALID; |
48 | 0 | } |
49 | | |
50 | 8.50k | if (!target_size) |
51 | 0 | target_size = buf->size; |
52 | | |
53 | 8.50k | if (target_size <= buf->asize) |
54 | 0 | return 0; |
55 | | |
56 | 8.50k | if (buf->asize == 0) { |
57 | 7.54k | new_size = target_size; |
58 | 7.54k | new_ptr = NULL; |
59 | 7.54k | } else { |
60 | 966 | new_size = buf->asize; |
61 | | /* |
62 | | * Grow the allocated buffer by 1.5 to allow |
63 | | * re-use of memory holes resulting from the |
64 | | * realloc. If this is still too small, then just |
65 | | * use the target size. |
66 | | */ |
67 | 966 | if ((new_size = (new_size << 1) - (new_size >> 1)) < target_size) |
68 | 111 | new_size = target_size; |
69 | 966 | new_ptr = buf->ptr; |
70 | 966 | } |
71 | | |
72 | | /* round allocation up to multiple of 8 */ |
73 | 8.50k | new_size = (new_size + 7) & ~7; |
74 | | |
75 | 8.50k | if (new_size < buf->size) { |
76 | 0 | if (mark_oom) { |
77 | 0 | if (buf->ptr && buf->ptr != git_str__initstr) |
78 | 0 | git__free(buf->ptr); |
79 | 0 | buf->ptr = git_str__oom; |
80 | 0 | } |
81 | |
|
82 | 0 | git_error_set_oom(); |
83 | 0 | return -1; |
84 | 0 | } |
85 | | |
86 | 8.50k | new_ptr = git__realloc(new_ptr, new_size); |
87 | | |
88 | 8.50k | if (!new_ptr) { |
89 | 0 | if (mark_oom) { |
90 | 0 | if (buf->ptr && (buf->ptr != git_str__initstr)) |
91 | 0 | git__free(buf->ptr); |
92 | 0 | buf->ptr = git_str__oom; |
93 | 0 | } |
94 | 0 | return -1; |
95 | 0 | } |
96 | | |
97 | 8.50k | buf->asize = new_size; |
98 | 8.50k | buf->ptr = new_ptr; |
99 | | |
100 | | /* truncate the existing buffer size if necessary */ |
101 | 8.50k | if (buf->size >= buf->asize) |
102 | 0 | buf->size = buf->asize - 1; |
103 | 8.50k | buf->ptr[buf->size] = '\0'; |
104 | | |
105 | 8.50k | return 0; |
106 | 8.50k | } |
107 | | |
108 | | int git_str_grow(git_str *buffer, size_t target_size) |
109 | 8.50k | { |
110 | 8.50k | return git_str_try_grow(buffer, target_size, true); |
111 | 8.50k | } |
112 | | |
113 | | int git_str_grow_by(git_str *buffer, size_t additional_size) |
114 | 0 | { |
115 | 0 | size_t newsize; |
116 | |
|
117 | 0 | if (GIT_ADD_SIZET_OVERFLOW(&newsize, buffer->size, additional_size)) { |
118 | 0 | buffer->ptr = git_str__oom; |
119 | 0 | return -1; |
120 | 0 | } |
121 | | |
122 | 0 | return git_str_try_grow(buffer, newsize, true); |
123 | 0 | } |
124 | | |
125 | | void git_str_dispose(git_str *buf) |
126 | 8.16k | { |
127 | 8.16k | if (!buf) return; |
128 | | |
129 | 8.16k | if (buf->asize > 0 && buf->ptr != NULL && buf->ptr != git_str__oom) |
130 | 538 | git__free(buf->ptr); |
131 | | |
132 | 8.16k | git_str_init(buf, 0); |
133 | 8.16k | } |
134 | | |
135 | | void git_str_clear(git_str *buf) |
136 | 19.9k | { |
137 | 19.9k | buf->size = 0; |
138 | | |
139 | 19.9k | if (!buf->ptr) { |
140 | 0 | buf->ptr = git_str__initstr; |
141 | 0 | buf->asize = 0; |
142 | 0 | } |
143 | | |
144 | 19.9k | if (buf->asize > 0) |
145 | 19.9k | buf->ptr[0] = '\0'; |
146 | 19.9k | } |
147 | | |
148 | | int git_str_set(git_str *buf, const void *data, size_t len) |
149 | 4 | { |
150 | 4 | size_t alloclen; |
151 | | |
152 | 4 | if (len == 0 || data == NULL) { |
153 | 0 | git_str_clear(buf); |
154 | 4 | } else { |
155 | 4 | if (data != buf->ptr) { |
156 | 4 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, len, 1); |
157 | 4 | ENSURE_SIZE(buf, alloclen); |
158 | 4 | memmove(buf->ptr, data, len); |
159 | 4 | } |
160 | | |
161 | 4 | buf->size = len; |
162 | 4 | if (buf->asize > buf->size) |
163 | 4 | buf->ptr[buf->size] = '\0'; |
164 | | |
165 | 4 | } |
166 | 4 | return 0; |
167 | 4 | } |
168 | | |
169 | | int git_str_sets(git_str *buf, const char *string) |
170 | 4 | { |
171 | 4 | return git_str_set(buf, string, string ? strlen(string) : 0); |
172 | 4 | } |
173 | | |
174 | | int git_str_putc(git_str *buf, char c) |
175 | 8 | { |
176 | 8 | size_t new_size; |
177 | 8 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, 2); |
178 | 8 | ENSURE_SIZE(buf, new_size); |
179 | 8 | buf->ptr[buf->size++] = c; |
180 | 8 | buf->ptr[buf->size] = '\0'; |
181 | 8 | return 0; |
182 | 8 | } |
183 | | |
184 | | int git_str_putcn(git_str *buf, char c, size_t len) |
185 | 0 | { |
186 | 0 | size_t new_size; |
187 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len); |
188 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); |
189 | 0 | ENSURE_SIZE(buf, new_size); |
190 | 0 | memset(buf->ptr + buf->size, c, len); |
191 | 0 | buf->size += len; |
192 | 0 | buf->ptr[buf->size] = '\0'; |
193 | 0 | return 0; |
194 | 0 | } |
195 | | |
196 | | int git_str_put(git_str *buf, const char *data, size_t len) |
197 | 7.24k | { |
198 | 7.24k | if (len) { |
199 | 7.22k | size_t new_size; |
200 | | |
201 | 7.22k | GIT_ASSERT_ARG(data); |
202 | | |
203 | 7.22k | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len); |
204 | 7.22k | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); |
205 | 7.22k | ENSURE_SIZE(buf, new_size); |
206 | 7.22k | memmove(buf->ptr + buf->size, data, len); |
207 | 7.22k | buf->size += len; |
208 | 7.22k | buf->ptr[buf->size] = '\0'; |
209 | 7.22k | } |
210 | 7.24k | return 0; |
211 | 7.24k | } |
212 | | |
213 | | int git_str_puts(git_str *buf, const char *string) |
214 | 6 | { |
215 | 6 | GIT_ASSERT_ARG(string); |
216 | | |
217 | 6 | return git_str_put(buf, string, strlen(string)); |
218 | 6 | } |
219 | | |
220 | | static char hex_encode[] = "0123456789abcdef"; |
221 | | |
222 | | int git_str_encode_hexstr(git_str *str, const char *data, size_t len) |
223 | 0 | { |
224 | 0 | size_t new_size, i; |
225 | 0 | char *s; |
226 | |
|
227 | 0 | GIT_ERROR_CHECK_ALLOC_MULTIPLY(&new_size, len, 2); |
228 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); |
229 | |
|
230 | 0 | if (git_str_grow_by(str, new_size) < 0) |
231 | 0 | return -1; |
232 | | |
233 | 0 | s = str->ptr + str->size; |
234 | |
|
235 | 0 | for (i = 0; i < len; i++) { |
236 | 0 | *s++ = hex_encode[(data[i] & 0xf0) >> 4]; |
237 | 0 | *s++ = hex_encode[(data[i] & 0x0f)]; |
238 | 0 | } |
239 | |
|
240 | 0 | str->size += (len * 2); |
241 | 0 | str->ptr[str->size] = '\0'; |
242 | |
|
243 | 0 | return 0; |
244 | 0 | } |
245 | | |
246 | | static const char base64_encode[] = |
247 | | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
248 | | |
249 | | int git_str_encode_base64(git_str *buf, const char *data, size_t len) |
250 | 0 | { |
251 | 0 | size_t extra = len % 3; |
252 | 0 | uint8_t *write, a, b, c; |
253 | 0 | const uint8_t *read = (const uint8_t *)data; |
254 | 0 | size_t blocks = (len / 3) + !!extra, alloclen; |
255 | |
|
256 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&blocks, blocks, 1); |
257 | 0 | GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 4); |
258 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size); |
259 | |
|
260 | 0 | ENSURE_SIZE(buf, alloclen); |
261 | 0 | write = (uint8_t *)&buf->ptr[buf->size]; |
262 | | |
263 | | /* convert each run of 3 bytes into 4 output bytes */ |
264 | 0 | for (len -= extra; len > 0; len -= 3) { |
265 | 0 | a = *read++; |
266 | 0 | b = *read++; |
267 | 0 | c = *read++; |
268 | |
|
269 | 0 | *write++ = base64_encode[a >> 2]; |
270 | 0 | *write++ = base64_encode[(a & 0x03) << 4 | b >> 4]; |
271 | 0 | *write++ = base64_encode[(b & 0x0f) << 2 | c >> 6]; |
272 | 0 | *write++ = base64_encode[c & 0x3f]; |
273 | 0 | } |
274 | |
|
275 | 0 | if (extra > 0) { |
276 | 0 | a = *read++; |
277 | 0 | b = (extra > 1) ? *read++ : 0; |
278 | |
|
279 | 0 | *write++ = base64_encode[a >> 2]; |
280 | 0 | *write++ = base64_encode[(a & 0x03) << 4 | b >> 4]; |
281 | 0 | *write++ = (extra > 1) ? base64_encode[(b & 0x0f) << 2] : '='; |
282 | 0 | *write++ = '='; |
283 | 0 | } |
284 | |
|
285 | 0 | buf->size = ((char *)write) - buf->ptr; |
286 | 0 | buf->ptr[buf->size] = '\0'; |
287 | |
|
288 | 0 | return 0; |
289 | 0 | } |
290 | | |
291 | | /* The inverse of base64_encode */ |
292 | | static const int8_t base64_decode[] = { |
293 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
294 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
295 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
296 | | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1, |
297 | | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
298 | | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
299 | | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
300 | | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
301 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
302 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
303 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
304 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
305 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
306 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
307 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
308 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
309 | | }; |
310 | | |
311 | | int git_str_decode_base64(git_str *buf, const char *base64, size_t len) |
312 | 0 | { |
313 | 0 | size_t i; |
314 | 0 | int8_t a, b, c, d; |
315 | 0 | size_t orig_size = buf->size, new_size; |
316 | |
|
317 | 0 | if (len % 4) { |
318 | 0 | git_error_set(GIT_ERROR_INVALID, "invalid base64 input"); |
319 | 0 | return -1; |
320 | 0 | } |
321 | | |
322 | 0 | GIT_ASSERT_ARG(len % 4 == 0); |
323 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, (len / 4 * 3), buf->size); |
324 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); |
325 | 0 | ENSURE_SIZE(buf, new_size); |
326 | |
|
327 | 0 | for (i = 0; i < len; i += 4) { |
328 | 0 | if ((a = base64_decode[(unsigned char)base64[i]]) < 0 || |
329 | 0 | (b = base64_decode[(unsigned char)base64[i+1]]) < 0 || |
330 | 0 | (c = base64_decode[(unsigned char)base64[i+2]]) < 0 || |
331 | 0 | (d = base64_decode[(unsigned char)base64[i+3]]) < 0) { |
332 | 0 | buf->size = orig_size; |
333 | 0 | buf->ptr[buf->size] = '\0'; |
334 | |
|
335 | 0 | git_error_set(GIT_ERROR_INVALID, "invalid base64 input"); |
336 | 0 | return -1; |
337 | 0 | } |
338 | | |
339 | 0 | buf->ptr[buf->size++] = ((a << 2) | (b & 0x30) >> 4); |
340 | 0 | buf->ptr[buf->size++] = ((b & 0x0f) << 4) | ((c & 0x3c) >> 2); |
341 | 0 | buf->ptr[buf->size++] = (c & 0x03) << 6 | (d & 0x3f); |
342 | 0 | } |
343 | | |
344 | 0 | buf->ptr[buf->size] = '\0'; |
345 | 0 | return 0; |
346 | 0 | } |
347 | | |
348 | | static const char base85_encode[] = |
349 | | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~"; |
350 | | |
351 | | int git_str_encode_base85(git_str *buf, const char *data, size_t len) |
352 | 0 | { |
353 | 0 | size_t blocks = (len / 4) + !!(len % 4), alloclen; |
354 | |
|
355 | 0 | GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloclen, blocks, 5); |
356 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, buf->size); |
357 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); |
358 | |
|
359 | 0 | ENSURE_SIZE(buf, alloclen); |
360 | |
|
361 | 0 | while (len) { |
362 | 0 | uint32_t acc = 0; |
363 | 0 | char b85[5]; |
364 | 0 | int i; |
365 | |
|
366 | 0 | for (i = 24; i >= 0; i -= 8) { |
367 | 0 | uint8_t ch = *data++; |
368 | 0 | acc |= (uint32_t)ch << i; |
369 | |
|
370 | 0 | if (--len == 0) |
371 | 0 | break; |
372 | 0 | } |
373 | |
|
374 | 0 | for (i = 4; i >= 0; i--) { |
375 | 0 | int val = acc % 85; |
376 | 0 | acc /= 85; |
377 | |
|
378 | 0 | b85[i] = base85_encode[val]; |
379 | 0 | } |
380 | |
|
381 | 0 | for (i = 0; i < 5; i++) |
382 | 0 | buf->ptr[buf->size++] = b85[i]; |
383 | 0 | } |
384 | |
|
385 | 0 | buf->ptr[buf->size] = '\0'; |
386 | |
|
387 | 0 | return 0; |
388 | 0 | } |
389 | | |
390 | | /* The inverse of base85_encode */ |
391 | | static const int8_t base85_decode[] = { |
392 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
393 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
394 | | -1, 63, -1, 64, 65, 66, 67, -1, 68, 69, 70, 71, -1, 72, -1, -1, |
395 | | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1, 73, 74, 75, 76, 77, |
396 | | 78, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, |
397 | | 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, -1, -1, -1, 79, 80, |
398 | | 81, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, |
399 | | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 82, 83, 84, 85, -1, |
400 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
401 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
402 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
403 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
404 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
405 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
406 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
407 | | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
408 | | }; |
409 | | |
410 | | int git_str_decode_base85( |
411 | | git_str *buf, |
412 | | const char *base85, |
413 | | size_t base85_len, |
414 | | size_t output_len) |
415 | 61.5k | { |
416 | 61.5k | size_t orig_size = buf->size, new_size; |
417 | | |
418 | 61.5k | if (base85_len % 5 || |
419 | 61.5k | output_len > base85_len * 4 / 5) { |
420 | 0 | git_error_set(GIT_ERROR_INVALID, "invalid base85 input"); |
421 | 0 | return -1; |
422 | 0 | } |
423 | | |
424 | 61.5k | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, output_len, buf->size); |
425 | 61.5k | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); |
426 | 61.5k | ENSURE_SIZE(buf, new_size); |
427 | | |
428 | 661k | while (output_len) { |
429 | 600k | unsigned acc = 0; |
430 | 600k | int de, cnt = 4; |
431 | 600k | unsigned char ch; |
432 | 2.40M | do { |
433 | 2.40M | ch = *base85++; |
434 | 2.40M | de = base85_decode[ch]; |
435 | 2.40M | if (--de < 0) |
436 | 92 | goto on_error; |
437 | | |
438 | 2.40M | acc = acc * 85 + de; |
439 | 2.40M | } while (--cnt); |
440 | 600k | ch = *base85++; |
441 | 600k | de = base85_decode[ch]; |
442 | 600k | if (--de < 0) |
443 | 11 | goto on_error; |
444 | | |
445 | | /* Detect overflow. */ |
446 | 600k | if (0xffffffff / 85 < acc || |
447 | 600k | 0xffffffff - de < (acc *= 85)) |
448 | 6 | goto on_error; |
449 | | |
450 | 600k | acc += de; |
451 | | |
452 | 600k | cnt = (output_len < 4) ? (int)output_len : 4; |
453 | 600k | output_len -= cnt; |
454 | 2.39M | do { |
455 | 2.39M | acc = (acc << 8) | (acc >> 24); |
456 | 2.39M | buf->ptr[buf->size++] = acc; |
457 | 2.39M | } while (--cnt); |
458 | 600k | } |
459 | | |
460 | 61.4k | buf->ptr[buf->size] = 0; |
461 | | |
462 | 61.4k | return 0; |
463 | | |
464 | 109 | on_error: |
465 | 109 | buf->size = orig_size; |
466 | 109 | buf->ptr[buf->size] = '\0'; |
467 | | |
468 | 109 | git_error_set(GIT_ERROR_INVALID, "invalid base85 input"); |
469 | 109 | return -1; |
470 | 61.5k | } |
471 | | |
472 | 0 | #define HEX_DECODE(c) ((c | 32) % 39 - 9) |
473 | | |
474 | | int git_str_decode_percent( |
475 | | git_str *buf, |
476 | | const char *str, |
477 | | size_t str_len) |
478 | 0 | { |
479 | 0 | size_t str_pos, new_size; |
480 | |
|
481 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, str_len); |
482 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); |
483 | 0 | ENSURE_SIZE(buf, new_size); |
484 | |
|
485 | 0 | for (str_pos = 0; str_pos < str_len; buf->size++, str_pos++) { |
486 | 0 | if (str[str_pos] == '%' && |
487 | 0 | str_len > str_pos + 2 && |
488 | 0 | git__isxdigit(str[str_pos + 1]) && |
489 | 0 | git__isxdigit(str[str_pos + 2])) { |
490 | 0 | buf->ptr[buf->size] = (HEX_DECODE(str[str_pos + 1]) << 4) + |
491 | 0 | HEX_DECODE(str[str_pos + 2]); |
492 | 0 | str_pos += 2; |
493 | 0 | } else { |
494 | 0 | buf->ptr[buf->size] = str[str_pos]; |
495 | 0 | } |
496 | 0 | } |
497 | |
|
498 | 0 | buf->ptr[buf->size] = '\0'; |
499 | 0 | return 0; |
500 | 0 | } |
501 | | |
502 | | int git_str_vprintf(git_str *buf, const char *format, va_list ap) |
503 | 11.8k | { |
504 | 11.8k | size_t expected_size, new_size; |
505 | 11.8k | int len; |
506 | | |
507 | 11.8k | GIT_ERROR_CHECK_ALLOC_MULTIPLY(&expected_size, strlen(format), 2); |
508 | 11.8k | GIT_ERROR_CHECK_ALLOC_ADD(&expected_size, expected_size, buf->size); |
509 | 11.8k | ENSURE_SIZE(buf, expected_size); |
510 | | |
511 | 11.8k | while (1) { |
512 | 11.8k | va_list args; |
513 | 11.8k | va_copy(args, ap); |
514 | | |
515 | 11.8k | len = p_vsnprintf( |
516 | 11.8k | buf->ptr + buf->size, |
517 | 11.8k | buf->asize - buf->size, |
518 | 11.8k | format, args |
519 | 11.8k | ); |
520 | | |
521 | 11.8k | va_end(args); |
522 | | |
523 | 11.8k | if (len < 0) { |
524 | 0 | git__free(buf->ptr); |
525 | 0 | buf->ptr = git_str__oom; |
526 | 0 | return -1; |
527 | 0 | } |
528 | | |
529 | 11.8k | if ((size_t)len + 1 <= buf->asize - buf->size) { |
530 | 11.8k | buf->size += len; |
531 | 11.8k | break; |
532 | 11.8k | } |
533 | | |
534 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, buf->size, len); |
535 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, new_size, 1); |
536 | 0 | ENSURE_SIZE(buf, new_size); |
537 | 0 | } |
538 | | |
539 | 11.8k | return 0; |
540 | 11.8k | } |
541 | | |
542 | | int git_str_printf(git_str *buf, const char *format, ...) |
543 | 0 | { |
544 | 0 | int r; |
545 | 0 | va_list ap; |
546 | |
|
547 | 0 | va_start(ap, format); |
548 | 0 | r = git_str_vprintf(buf, format, ap); |
549 | 0 | va_end(ap); |
550 | |
|
551 | 0 | return r; |
552 | 0 | } |
553 | | |
554 | | int git_str_copy_cstr(char *data, size_t datasize, const git_str *buf) |
555 | 0 | { |
556 | 0 | size_t copylen; |
557 | |
|
558 | 0 | GIT_ASSERT_ARG(data); |
559 | 0 | GIT_ASSERT_ARG(datasize); |
560 | 0 | GIT_ASSERT_ARG(buf); |
561 | | |
562 | 0 | data[0] = '\0'; |
563 | |
|
564 | 0 | if (buf->size == 0 || buf->asize <= 0) |
565 | 0 | return 0; |
566 | | |
567 | 0 | copylen = buf->size; |
568 | 0 | if (copylen > datasize - 1) |
569 | 0 | copylen = datasize - 1; |
570 | 0 | memmove(data, buf->ptr, copylen); |
571 | 0 | data[copylen] = '\0'; |
572 | |
|
573 | 0 | return 0; |
574 | 0 | } |
575 | | |
576 | | void git_str_consume_bytes(git_str *buf, size_t len) |
577 | 0 | { |
578 | 0 | git_str_consume(buf, buf->ptr + len); |
579 | 0 | } |
580 | | |
581 | | void git_str_consume(git_str *buf, const char *end) |
582 | 0 | { |
583 | 0 | if (end > buf->ptr && end <= buf->ptr + buf->size) { |
584 | 0 | size_t consumed = end - buf->ptr; |
585 | 0 | memmove(buf->ptr, end, buf->size - consumed); |
586 | 0 | buf->size -= consumed; |
587 | 0 | buf->ptr[buf->size] = '\0'; |
588 | 0 | } |
589 | 0 | } |
590 | | |
591 | | void git_str_truncate(git_str *buf, size_t len) |
592 | 0 | { |
593 | 0 | if (len >= buf->size) |
594 | 0 | return; |
595 | | |
596 | 0 | buf->size = len; |
597 | 0 | if (buf->size < buf->asize) |
598 | 0 | buf->ptr[buf->size] = '\0'; |
599 | 0 | } |
600 | | |
601 | | void git_str_shorten(git_str *buf, size_t amount) |
602 | 0 | { |
603 | 0 | if (buf->size > amount) |
604 | 0 | git_str_truncate(buf, buf->size - amount); |
605 | 0 | else |
606 | 0 | git_str_clear(buf); |
607 | 0 | } |
608 | | |
609 | | void git_str_truncate_at_char(git_str *buf, char separator) |
610 | 0 | { |
611 | 0 | ssize_t idx = git_str_find(buf, separator); |
612 | 0 | if (idx >= 0) |
613 | 0 | git_str_truncate(buf, (size_t)idx); |
614 | 0 | } |
615 | | |
616 | | void git_str_rtruncate_at_char(git_str *buf, char separator) |
617 | 0 | { |
618 | 0 | ssize_t idx = git_str_rfind_next(buf, separator); |
619 | 0 | git_str_truncate(buf, idx < 0 ? 0 : (size_t)idx); |
620 | 0 | } |
621 | | |
622 | | void git_str_swap(git_str *str_a, git_str *str_b) |
623 | 0 | { |
624 | 0 | git_str t = *str_a; |
625 | 0 | *str_a = *str_b; |
626 | 0 | *str_b = t; |
627 | 0 | } |
628 | | |
629 | | char *git_str_detach(git_str *buf) |
630 | 7.06k | { |
631 | 7.06k | char *data = buf->ptr; |
632 | | |
633 | 7.06k | if (buf->asize == 0 || buf->ptr == git_str__oom) |
634 | 68 | return NULL; |
635 | | |
636 | 6.99k | git_str_init(buf, 0); |
637 | | |
638 | 6.99k | return data; |
639 | 7.06k | } |
640 | | |
641 | | int git_str_attach(git_str *buf, char *ptr, size_t asize) |
642 | 0 | { |
643 | 0 | git_str_dispose(buf); |
644 | |
|
645 | 0 | if (ptr) { |
646 | 0 | buf->ptr = ptr; |
647 | 0 | buf->size = strlen(ptr); |
648 | 0 | if (asize) |
649 | 0 | buf->asize = (asize < buf->size) ? buf->size + 1 : asize; |
650 | 0 | else /* pass 0 to fall back on strlen + 1 */ |
651 | 0 | buf->asize = buf->size + 1; |
652 | 0 | } |
653 | |
|
654 | 0 | ENSURE_SIZE(buf, asize); |
655 | 0 | return 0; |
656 | 0 | } |
657 | | |
658 | | void git_str_attach_notowned(git_str *buf, const char *ptr, size_t size) |
659 | 0 | { |
660 | 0 | if (git_str_is_allocated(buf)) |
661 | 0 | git_str_dispose(buf); |
662 | |
|
663 | 0 | if (!size) { |
664 | 0 | git_str_init(buf, 0); |
665 | 0 | } else { |
666 | 0 | buf->ptr = (char *)ptr; |
667 | 0 | buf->asize = 0; |
668 | 0 | buf->size = size; |
669 | 0 | } |
670 | 0 | } |
671 | | |
672 | | int git_str_join_n(git_str *buf, char separator, int nbuf, ...) |
673 | 0 | { |
674 | 0 | va_list ap; |
675 | 0 | int i; |
676 | 0 | size_t total_size = 0, original_size = buf->size; |
677 | 0 | char *out, *original = buf->ptr; |
678 | |
|
679 | 0 | if (buf->size > 0 && buf->ptr[buf->size - 1] != separator) |
680 | 0 | ++total_size; /* space for initial separator */ |
681 | | |
682 | | /* Make two passes to avoid multiple reallocation */ |
683 | |
|
684 | 0 | va_start(ap, nbuf); |
685 | 0 | for (i = 0; i < nbuf; ++i) { |
686 | 0 | const char *segment; |
687 | 0 | size_t segment_len; |
688 | |
|
689 | 0 | segment = va_arg(ap, const char *); |
690 | 0 | if (!segment) |
691 | 0 | continue; |
692 | | |
693 | 0 | segment_len = strlen(segment); |
694 | |
|
695 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, segment_len); |
696 | |
|
697 | 0 | if (segment_len == 0 || segment[segment_len - 1] != separator) |
698 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, 1); |
699 | 0 | } |
700 | 0 | va_end(ap); |
701 | | |
702 | | /* expand buffer if needed */ |
703 | 0 | if (total_size == 0) |
704 | 0 | return 0; |
705 | | |
706 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&total_size, total_size, 1); |
707 | 0 | if (git_str_grow_by(buf, total_size) < 0) |
708 | 0 | return -1; |
709 | | |
710 | 0 | out = buf->ptr + buf->size; |
711 | | |
712 | | /* append separator to existing buf if needed */ |
713 | 0 | if (buf->size > 0 && out[-1] != separator) |
714 | 0 | *out++ = separator; |
715 | |
|
716 | 0 | va_start(ap, nbuf); |
717 | 0 | for (i = 0; i < nbuf; ++i) { |
718 | 0 | const char *segment; |
719 | 0 | size_t segment_len; |
720 | |
|
721 | 0 | segment = va_arg(ap, const char *); |
722 | 0 | if (!segment) |
723 | 0 | continue; |
724 | | |
725 | | /* deal with join that references buffer's original content */ |
726 | 0 | if (segment >= original && segment < original + original_size) { |
727 | 0 | size_t offset = (segment - original); |
728 | 0 | segment = buf->ptr + offset; |
729 | 0 | segment_len = original_size - offset; |
730 | 0 | } else { |
731 | 0 | segment_len = strlen(segment); |
732 | 0 | } |
733 | | |
734 | | /* skip leading separators */ |
735 | 0 | if (out > buf->ptr && out[-1] == separator) |
736 | 0 | while (segment_len > 0 && *segment == separator) { |
737 | 0 | segment++; |
738 | 0 | segment_len--; |
739 | 0 | } |
740 | | |
741 | | /* copy over next buffer */ |
742 | 0 | if (segment_len > 0) { |
743 | 0 | memmove(out, segment, segment_len); |
744 | 0 | out += segment_len; |
745 | 0 | } |
746 | | |
747 | | /* append trailing separator (except for last item) */ |
748 | 0 | if (i < nbuf - 1 && out > buf->ptr && out[-1] != separator) |
749 | 0 | *out++ = separator; |
750 | 0 | } |
751 | 0 | va_end(ap); |
752 | | |
753 | | /* set size based on num characters actually written */ |
754 | 0 | buf->size = out - buf->ptr; |
755 | 0 | buf->ptr[buf->size] = '\0'; |
756 | |
|
757 | 0 | return 0; |
758 | 0 | } |
759 | | |
760 | | int git_str_join( |
761 | | git_str *buf, |
762 | | char separator, |
763 | | const char *str_a, |
764 | | const char *str_b) |
765 | 2 | { |
766 | 2 | size_t strlen_a = str_a ? strlen(str_a) : 0; |
767 | 2 | size_t strlen_b = strlen(str_b); |
768 | 2 | size_t alloc_len; |
769 | 2 | int need_sep = 0; |
770 | 2 | ssize_t offset_a = -1; |
771 | | |
772 | | /* not safe to have str_b point internally to the buffer */ |
773 | 2 | if (buf->size) |
774 | 2 | GIT_ASSERT_ARG(str_b < buf->ptr || str_b >= buf->ptr + buf->size); |
775 | | |
776 | | /* figure out if we need to insert a separator */ |
777 | 2 | if (separator && strlen_a) { |
778 | 2 | while (*str_b == separator) { str_b++; strlen_b--; } |
779 | 2 | if (str_a[strlen_a - 1] != separator) |
780 | 2 | need_sep = 1; |
781 | 2 | } |
782 | | |
783 | | /* str_a could be part of the buffer */ |
784 | 2 | if (buf->size && str_a >= buf->ptr && str_a < buf->ptr + buf->size) |
785 | 0 | offset_a = str_a - buf->ptr; |
786 | | |
787 | 2 | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, strlen_a, strlen_b); |
788 | 2 | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, need_sep); |
789 | 2 | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1); |
790 | 2 | ENSURE_SIZE(buf, alloc_len); |
791 | | |
792 | | /* fix up internal pointers */ |
793 | 2 | if (offset_a >= 0) |
794 | 0 | str_a = buf->ptr + offset_a; |
795 | | |
796 | | /* do the actual copying */ |
797 | 2 | if (offset_a != 0 && str_a) |
798 | 2 | memmove(buf->ptr, str_a, strlen_a); |
799 | 2 | if (need_sep) |
800 | 2 | buf->ptr[strlen_a] = separator; |
801 | 2 | memcpy(buf->ptr + strlen_a + need_sep, str_b, strlen_b); |
802 | | |
803 | 2 | buf->size = strlen_a + strlen_b + need_sep; |
804 | 2 | buf->ptr[buf->size] = '\0'; |
805 | | |
806 | 2 | return 0; |
807 | 2 | } |
808 | | |
809 | | int git_str_join3( |
810 | | git_str *buf, |
811 | | char separator, |
812 | | const char *str_a, |
813 | | const char *str_b, |
814 | | const char *str_c) |
815 | 0 | { |
816 | 0 | size_t len_a = strlen(str_a), |
817 | 0 | len_b = strlen(str_b), |
818 | 0 | len_c = strlen(str_c), |
819 | 0 | len_total; |
820 | 0 | int sep_a = 0, sep_b = 0; |
821 | 0 | char *tgt; |
822 | | |
823 | | /* for this function, disallow pointers into the existing buffer */ |
824 | 0 | GIT_ASSERT(str_a < buf->ptr || str_a >= buf->ptr + buf->size); |
825 | 0 | GIT_ASSERT(str_b < buf->ptr || str_b >= buf->ptr + buf->size); |
826 | 0 | GIT_ASSERT(str_c < buf->ptr || str_c >= buf->ptr + buf->size); |
827 | | |
828 | 0 | if (separator) { |
829 | 0 | if (len_a > 0) { |
830 | 0 | while (*str_b == separator) { str_b++; len_b--; } |
831 | 0 | sep_a = (str_a[len_a - 1] != separator); |
832 | 0 | } |
833 | 0 | if (len_a > 0 || len_b > 0) |
834 | 0 | while (*str_c == separator) { str_c++; len_c--; } |
835 | 0 | if (len_b > 0) |
836 | 0 | sep_b = (str_b[len_b - 1] != separator); |
837 | 0 | } |
838 | |
|
839 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_a, sep_a); |
840 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, len_b); |
841 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, sep_b); |
842 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, len_c); |
843 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&len_total, len_total, 1); |
844 | 0 | ENSURE_SIZE(buf, len_total); |
845 | |
|
846 | 0 | tgt = buf->ptr; |
847 | |
|
848 | 0 | if (len_a) { |
849 | 0 | memcpy(tgt, str_a, len_a); |
850 | 0 | tgt += len_a; |
851 | 0 | } |
852 | 0 | if (sep_a) |
853 | 0 | *tgt++ = separator; |
854 | 0 | if (len_b) { |
855 | 0 | memcpy(tgt, str_b, len_b); |
856 | 0 | tgt += len_b; |
857 | 0 | } |
858 | 0 | if (sep_b) |
859 | 0 | *tgt++ = separator; |
860 | 0 | if (len_c) |
861 | 0 | memcpy(tgt, str_c, len_c); |
862 | |
|
863 | 0 | buf->size = len_a + sep_a + len_b + sep_b + len_c; |
864 | 0 | buf->ptr[buf->size] = '\0'; |
865 | |
|
866 | 0 | return 0; |
867 | 0 | } |
868 | | |
869 | | void git_str_rtrim(git_str *buf) |
870 | 7.66k | { |
871 | 1.67M | while (buf->size > 0) { |
872 | 1.67M | if (!git__isspace(buf->ptr[buf->size - 1])) |
873 | 7.63k | break; |
874 | | |
875 | 1.66M | buf->size--; |
876 | 1.66M | } |
877 | | |
878 | 7.66k | if (buf->asize > buf->size) |
879 | 7.64k | buf->ptr[buf->size] = '\0'; |
880 | 7.66k | } |
881 | | |
882 | | int git_str_cmp(const git_str *a, const git_str *b) |
883 | 0 | { |
884 | 0 | int result = memcmp(a->ptr, b->ptr, min(a->size, b->size)); |
885 | 0 | return (result != 0) ? result : |
886 | 0 | (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0; |
887 | 0 | } |
888 | | |
889 | | int git_str_splice( |
890 | | git_str *buf, |
891 | | size_t where, |
892 | | size_t nb_to_remove, |
893 | | const char *data, |
894 | | size_t nb_to_insert) |
895 | 0 | { |
896 | 0 | char *splice_loc; |
897 | 0 | size_t new_size, alloc_size; |
898 | |
|
899 | 0 | GIT_ASSERT(buf); |
900 | 0 | GIT_ASSERT(where <= buf->size); |
901 | 0 | GIT_ASSERT(nb_to_remove <= buf->size - where); |
902 | | |
903 | 0 | splice_loc = buf->ptr + where; |
904 | | |
905 | | /* Ported from git.git |
906 | | * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176 |
907 | | */ |
908 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, (buf->size - nb_to_remove), nb_to_insert); |
909 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloc_size, new_size, 1); |
910 | 0 | ENSURE_SIZE(buf, alloc_size); |
911 | |
|
912 | 0 | memmove(splice_loc + nb_to_insert, |
913 | 0 | splice_loc + nb_to_remove, |
914 | 0 | buf->size - where - nb_to_remove); |
915 | |
|
916 | 0 | memcpy(splice_loc, data, nb_to_insert); |
917 | |
|
918 | 0 | buf->size = new_size; |
919 | 0 | buf->ptr[buf->size] = '\0'; |
920 | 0 | return 0; |
921 | 0 | } |
922 | | |
923 | | /* Quote per http://marc.info/?l=git&m=112927316408690&w=2 */ |
924 | | int git_str_quote(git_str *buf) |
925 | 0 | { |
926 | 0 | const char whitespace[] = { 'a', 'b', 't', 'n', 'v', 'f', 'r' }; |
927 | 0 | git_str quoted = GIT_STR_INIT; |
928 | 0 | size_t i = 0; |
929 | 0 | bool quote = false; |
930 | 0 | int error = 0; |
931 | | |
932 | | /* walk to the first char that needs quoting */ |
933 | 0 | if (buf->size && buf->ptr[0] == '!') |
934 | 0 | quote = true; |
935 | |
|
936 | 0 | for (i = 0; !quote && i < buf->size; i++) { |
937 | 0 | if (buf->ptr[i] == '"' || buf->ptr[i] == '\\' || |
938 | 0 | buf->ptr[i] < ' ' || buf->ptr[i] > '~') { |
939 | 0 | quote = true; |
940 | 0 | break; |
941 | 0 | } |
942 | 0 | } |
943 | |
|
944 | 0 | if (!quote) |
945 | 0 | goto done; |
946 | | |
947 | 0 | git_str_putc("ed, '"'); |
948 | 0 | git_str_put("ed, buf->ptr, i); |
949 | |
|
950 | 0 | for (; i < buf->size; i++) { |
951 | | /* whitespace - use the map above, which is ordered by ascii value */ |
952 | 0 | if (buf->ptr[i] >= '\a' && buf->ptr[i] <= '\r') { |
953 | 0 | git_str_putc("ed, '\\'); |
954 | 0 | git_str_putc("ed, whitespace[buf->ptr[i] - '\a']); |
955 | 0 | } |
956 | | |
957 | | /* double quote and backslash must be escaped */ |
958 | 0 | else if (buf->ptr[i] == '"' || buf->ptr[i] == '\\') { |
959 | 0 | git_str_putc("ed, '\\'); |
960 | 0 | git_str_putc("ed, buf->ptr[i]); |
961 | 0 | } |
962 | | |
963 | | /* escape anything unprintable as octal */ |
964 | 0 | else if (buf->ptr[i] != ' ' && |
965 | 0 | (buf->ptr[i] < '!' || buf->ptr[i] > '~')) { |
966 | 0 | git_str_printf("ed, "\\%03o", (unsigned char)buf->ptr[i]); |
967 | 0 | } |
968 | | |
969 | | /* yay, printable! */ |
970 | 0 | else { |
971 | 0 | git_str_putc("ed, buf->ptr[i]); |
972 | 0 | } |
973 | 0 | } |
974 | |
|
975 | 0 | git_str_putc("ed, '"'); |
976 | |
|
977 | 0 | if (git_str_oom("ed)) { |
978 | 0 | error = -1; |
979 | 0 | goto done; |
980 | 0 | } |
981 | | |
982 | 0 | git_str_swap("ed, buf); |
983 | |
|
984 | 0 | done: |
985 | 0 | git_str_dispose("ed); |
986 | 0 | return error; |
987 | 0 | } |
988 | | |
989 | | /* Unquote per http://marc.info/?l=git&m=112927316408690&w=2 */ |
990 | | int git_str_unquote(git_str *buf) |
991 | 440 | { |
992 | 440 | size_t i, j; |
993 | 440 | char ch; |
994 | | |
995 | 440 | git_str_rtrim(buf); |
996 | | |
997 | 440 | if (buf->size < 2 || buf->ptr[0] != '"' || buf->ptr[buf->size-1] != '"') |
998 | 72 | goto invalid; |
999 | | |
1000 | 7.85M | for (i = 0, j = 1; j < buf->size-1; i++, j++) { |
1001 | 7.85M | ch = buf->ptr[j]; |
1002 | | |
1003 | 7.85M | if (ch == '\\') { |
1004 | 97.4k | if (j == buf->size-2) |
1005 | 18 | goto invalid; |
1006 | | |
1007 | 97.4k | ch = buf->ptr[++j]; |
1008 | | |
1009 | 97.4k | switch (ch) { |
1010 | | /* \" or \\ simply copy the char in */ |
1011 | 17.7k | case '"': case '\\': |
1012 | 17.7k | break; |
1013 | | |
1014 | | /* add the appropriate escaped char */ |
1015 | 13.4k | case 'a': ch = '\a'; break; |
1016 | 15.2k | case 'b': ch = '\b'; break; |
1017 | 15.0k | case 'f': ch = '\f'; break; |
1018 | 2.26k | case 'n': ch = '\n'; break; |
1019 | 1.62k | case 'r': ch = '\r'; break; |
1020 | 4.73k | case 't': ch = '\t'; break; |
1021 | 3.75k | case 'v': ch = '\v'; break; |
1022 | | |
1023 | | /* \xyz digits convert to the char*/ |
1024 | 23.5k | case '0': case '1': case '2': case '3': |
1025 | 23.5k | if (j == buf->size-3) { |
1026 | 1 | git_error_set(GIT_ERROR_INVALID, |
1027 | 1 | "truncated quoted character \\%c", ch); |
1028 | 1 | return -1; |
1029 | 1 | } |
1030 | | |
1031 | 23.5k | if (buf->ptr[j+1] < '0' || buf->ptr[j+1] > '7' || |
1032 | 23.4k | buf->ptr[j+2] < '0' || buf->ptr[j+2] > '7') { |
1033 | 126 | git_error_set(GIT_ERROR_INVALID, |
1034 | 126 | "truncated quoted character \\%c%c%c", |
1035 | 126 | buf->ptr[j], buf->ptr[j+1], buf->ptr[j+2]); |
1036 | 126 | return -1; |
1037 | 126 | } |
1038 | | |
1039 | 23.4k | ch = ((buf->ptr[j] - '0') << 6) | |
1040 | 23.4k | ((buf->ptr[j+1] - '0') << 3) | |
1041 | 23.4k | (buf->ptr[j+2] - '0'); |
1042 | 23.4k | j += 2; |
1043 | 23.4k | break; |
1044 | | |
1045 | 43 | default: |
1046 | 43 | git_error_set(GIT_ERROR_INVALID, "invalid quoted character \\%c", ch); |
1047 | 43 | return -1; |
1048 | 97.4k | } |
1049 | 97.4k | } |
1050 | | |
1051 | 7.85M | buf->ptr[i] = ch; |
1052 | 7.85M | } |
1053 | | |
1054 | 180 | buf->ptr[i] = '\0'; |
1055 | 180 | buf->size = i; |
1056 | | |
1057 | 180 | return 0; |
1058 | | |
1059 | 90 | invalid: |
1060 | 90 | git_error_set(GIT_ERROR_INVALID, "invalid quoted line"); |
1061 | 90 | return -1; |
1062 | 368 | } |
1063 | | |
1064 | | int git_str_puts_escaped( |
1065 | | git_str *buf, |
1066 | | const char *string, |
1067 | | const char *esc_chars, |
1068 | | const char *esc_prefix, |
1069 | | const char *esc_suffix) |
1070 | 0 | { |
1071 | 0 | const char *scan; |
1072 | 0 | size_t total = 0, count, alloclen; |
1073 | 0 | size_t esc_prefix_len = esc_prefix ? strlen(esc_prefix) : 0; |
1074 | 0 | size_t esc_suffix_len = esc_suffix ? strlen(esc_suffix) : 0; |
1075 | |
|
1076 | 0 | if (!string) |
1077 | 0 | return 0; |
1078 | | |
1079 | 0 | for (scan = string; *scan; ) { |
1080 | | /* count run of non-escaped characters */ |
1081 | 0 | count = strcspn(scan, esc_chars); |
1082 | 0 | total += count; |
1083 | 0 | scan += count; |
1084 | | /* count run of escaped characters */ |
1085 | 0 | count = strspn(scan, esc_chars); |
1086 | 0 | total += count * (esc_prefix_len + esc_suffix_len + 1); |
1087 | 0 | scan += count; |
1088 | 0 | } |
1089 | |
|
1090 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, total, 1); |
1091 | 0 | if (git_str_grow_by(buf, alloclen) < 0) |
1092 | 0 | return -1; |
1093 | | |
1094 | 0 | for (scan = string; *scan; ) { |
1095 | 0 | count = strcspn(scan, esc_chars); |
1096 | |
|
1097 | 0 | memmove(buf->ptr + buf->size, scan, count); |
1098 | 0 | scan += count; |
1099 | 0 | buf->size += count; |
1100 | |
|
1101 | 0 | for (count = strspn(scan, esc_chars); count > 0; --count) { |
1102 | | /* copy escape prefix sequence */ |
1103 | 0 | if (esc_prefix) { |
1104 | 0 | memmove(buf->ptr + buf->size, esc_prefix, esc_prefix_len); |
1105 | 0 | buf->size += esc_prefix_len; |
1106 | 0 | } |
1107 | | |
1108 | | /* copy character to be escaped */ |
1109 | 0 | buf->ptr[buf->size] = *scan; |
1110 | 0 | buf->size++; |
1111 | 0 | scan++; |
1112 | | |
1113 | | /* copy escape suffix sequence */ |
1114 | 0 | if (esc_suffix) { |
1115 | 0 | memmove(buf->ptr + buf->size, esc_suffix, esc_suffix_len); |
1116 | 0 | buf->size += esc_suffix_len; |
1117 | 0 | } |
1118 | 0 | } |
1119 | 0 | } |
1120 | |
|
1121 | 0 | buf->ptr[buf->size] = '\0'; |
1122 | |
|
1123 | 0 | return 0; |
1124 | 0 | } |
1125 | | |
1126 | | void git_str_unescape(git_str *buf) |
1127 | 0 | { |
1128 | 0 | buf->size = git__unescape(buf->ptr); |
1129 | 0 | } |
1130 | | |
1131 | | int git_str_crlf_to_lf(git_str *tgt, const git_str *src) |
1132 | 0 | { |
1133 | 0 | const char *scan = src->ptr; |
1134 | 0 | const char *scan_end = src->ptr + src->size; |
1135 | 0 | const char *next = memchr(scan, '\r', src->size); |
1136 | 0 | size_t new_size; |
1137 | 0 | char *out; |
1138 | |
|
1139 | 0 | GIT_ASSERT(tgt != src); |
1140 | | |
1141 | 0 | if (!next) |
1142 | 0 | return git_str_set(tgt, src->ptr, src->size); |
1143 | | |
1144 | | /* reduce reallocs while in the loop */ |
1145 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&new_size, src->size, 1); |
1146 | 0 | if (git_str_grow(tgt, new_size) < 0) |
1147 | 0 | return -1; |
1148 | | |
1149 | 0 | out = tgt->ptr; |
1150 | 0 | tgt->size = 0; |
1151 | | |
1152 | | /* Find the next \r and copy whole chunk up to there to tgt */ |
1153 | 0 | for (; next; scan = next + 1, next = memchr(scan, '\r', scan_end - scan)) { |
1154 | 0 | if (next > scan) { |
1155 | 0 | size_t copylen = (size_t)(next - scan); |
1156 | 0 | memcpy(out, scan, copylen); |
1157 | 0 | out += copylen; |
1158 | 0 | } |
1159 | | |
1160 | | /* Do not drop \r unless it is followed by \n */ |
1161 | 0 | if (next + 1 == scan_end || next[1] != '\n') |
1162 | 0 | *out++ = '\r'; |
1163 | 0 | } |
1164 | | |
1165 | | /* Copy remaining input into dest */ |
1166 | 0 | if (scan < scan_end) { |
1167 | 0 | size_t remaining = (size_t)(scan_end - scan); |
1168 | 0 | memcpy(out, scan, remaining); |
1169 | 0 | out += remaining; |
1170 | 0 | } |
1171 | |
|
1172 | 0 | tgt->size = (size_t)(out - tgt->ptr); |
1173 | 0 | tgt->ptr[tgt->size] = '\0'; |
1174 | |
|
1175 | 0 | return 0; |
1176 | 0 | } |
1177 | | |
1178 | | int git_str_lf_to_crlf(git_str *tgt, const git_str *src) |
1179 | 0 | { |
1180 | 0 | const char *start = src->ptr; |
1181 | 0 | const char *end = start + src->size; |
1182 | 0 | const char *scan = start; |
1183 | 0 | const char *next = memchr(scan, '\n', src->size); |
1184 | 0 | size_t alloclen; |
1185 | |
|
1186 | 0 | GIT_ASSERT(tgt != src); |
1187 | | |
1188 | 0 | if (!next) |
1189 | 0 | return git_str_set(tgt, src->ptr, src->size); |
1190 | | |
1191 | | /* attempt to reduce reallocs while in the loop */ |
1192 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, src->size, src->size >> 4); |
1193 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); |
1194 | 0 | if (git_str_grow(tgt, alloclen) < 0) |
1195 | 0 | return -1; |
1196 | 0 | tgt->size = 0; |
1197 | |
|
1198 | 0 | for (; next; scan = next + 1, next = memchr(scan, '\n', end - scan)) { |
1199 | 0 | size_t copylen = next - scan; |
1200 | | |
1201 | | /* if we find mixed line endings, carry on */ |
1202 | 0 | if (copylen && next[-1] == '\r') |
1203 | 0 | copylen--; |
1204 | |
|
1205 | 0 | GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, copylen, 3); |
1206 | 0 | if (git_str_grow_by(tgt, alloclen) < 0) |
1207 | 0 | return -1; |
1208 | | |
1209 | 0 | if (copylen) { |
1210 | 0 | memcpy(tgt->ptr + tgt->size, scan, copylen); |
1211 | 0 | tgt->size += copylen; |
1212 | 0 | } |
1213 | |
|
1214 | 0 | tgt->ptr[tgt->size++] = '\r'; |
1215 | 0 | tgt->ptr[tgt->size++] = '\n'; |
1216 | 0 | } |
1217 | | |
1218 | 0 | tgt->ptr[tgt->size] = '\0'; |
1219 | 0 | return git_str_put(tgt, scan, end - scan); |
1220 | 0 | } |
1221 | | |
1222 | | int git_str_common_prefix(git_str *buf, char *const *const strings, size_t count) |
1223 | 0 | { |
1224 | 0 | size_t i; |
1225 | 0 | const char *str, *pfx; |
1226 | |
|
1227 | 0 | git_str_clear(buf); |
1228 | |
|
1229 | 0 | if (!strings || !count) |
1230 | 0 | return 0; |
1231 | | |
1232 | | /* initialize common prefix to first string */ |
1233 | 0 | if (git_str_sets(buf, strings[0]) < 0) |
1234 | 0 | return -1; |
1235 | | |
1236 | | /* go through the rest of the strings, truncating to shared prefix */ |
1237 | 0 | for (i = 1; i < count; ++i) { |
1238 | |
|
1239 | 0 | for (str = strings[i], pfx = buf->ptr; |
1240 | 0 | *str && *str == *pfx; |
1241 | 0 | str++, pfx++) |
1242 | 0 | /* scanning */; |
1243 | |
|
1244 | 0 | git_str_truncate(buf, pfx - buf->ptr); |
1245 | |
|
1246 | 0 | if (!buf->size) |
1247 | 0 | break; |
1248 | 0 | } |
1249 | |
|
1250 | 0 | return 0; |
1251 | 0 | } |
1252 | | |
1253 | | int git_str_is_binary(const git_str *buf) |
1254 | 0 | { |
1255 | 0 | const char *scan = buf->ptr, *end = buf->ptr + buf->size; |
1256 | 0 | git_str_bom_t bom; |
1257 | 0 | int printable = 0, nonprintable = 0; |
1258 | |
|
1259 | 0 | scan += git_str_detect_bom(&bom, buf); |
1260 | |
|
1261 | 0 | if (bom > GIT_STR_BOM_UTF8) |
1262 | 0 | return 1; |
1263 | | |
1264 | 0 | while (scan < end) { |
1265 | 0 | unsigned char c = *scan++; |
1266 | | |
1267 | | /* Printable characters are those above SPACE (0x1F) excluding DEL, |
1268 | | * and including BS, ESC and FF. |
1269 | | */ |
1270 | 0 | if ((c > 0x1F && c != 127) || c == '\b' || c == '\033' || c == '\014') |
1271 | 0 | printable++; |
1272 | 0 | else if (c == '\0') |
1273 | 0 | return true; |
1274 | 0 | else if (!git__isspace(c)) |
1275 | 0 | nonprintable++; |
1276 | 0 | } |
1277 | | |
1278 | 0 | return ((printable >> 7) < nonprintable); |
1279 | 0 | } |
1280 | | |
1281 | | int git_str_contains_nul(const git_str *buf) |
1282 | 0 | { |
1283 | 0 | return (memchr(buf->ptr, '\0', buf->size) != NULL); |
1284 | 0 | } |
1285 | | |
1286 | | int git_str_detect_bom(git_str_bom_t *bom, const git_str *buf) |
1287 | 0 | { |
1288 | 0 | const char *ptr; |
1289 | 0 | size_t len; |
1290 | |
|
1291 | 0 | *bom = GIT_STR_BOM_NONE; |
1292 | | /* need at least 2 bytes to look for any BOM */ |
1293 | 0 | if (buf->size < 2) |
1294 | 0 | return 0; |
1295 | | |
1296 | 0 | ptr = buf->ptr; |
1297 | 0 | len = buf->size; |
1298 | |
|
1299 | 0 | switch (*ptr++) { |
1300 | 0 | case 0: |
1301 | 0 | if (len >= 4 && ptr[0] == 0 && ptr[1] == '\xFE' && ptr[2] == '\xFF') { |
1302 | 0 | *bom = GIT_STR_BOM_UTF32_BE; |
1303 | 0 | return 4; |
1304 | 0 | } |
1305 | 0 | break; |
1306 | 0 | case '\xEF': |
1307 | 0 | if (len >= 3 && ptr[0] == '\xBB' && ptr[1] == '\xBF') { |
1308 | 0 | *bom = GIT_STR_BOM_UTF8; |
1309 | 0 | return 3; |
1310 | 0 | } |
1311 | 0 | break; |
1312 | 0 | case '\xFE': |
1313 | 0 | if (*ptr == '\xFF') { |
1314 | 0 | *bom = GIT_STR_BOM_UTF16_BE; |
1315 | 0 | return 2; |
1316 | 0 | } |
1317 | 0 | break; |
1318 | 0 | case '\xFF': |
1319 | 0 | if (*ptr != '\xFE') |
1320 | 0 | break; |
1321 | 0 | if (len >= 4 && ptr[1] == 0 && ptr[2] == 0) { |
1322 | 0 | *bom = GIT_STR_BOM_UTF32_LE; |
1323 | 0 | return 4; |
1324 | 0 | } else { |
1325 | 0 | *bom = GIT_STR_BOM_UTF16_LE; |
1326 | 0 | return 2; |
1327 | 0 | } |
1328 | 0 | break; |
1329 | 0 | default: |
1330 | 0 | break; |
1331 | 0 | } |
1332 | | |
1333 | 0 | return 0; |
1334 | 0 | } |
1335 | | |
1336 | | bool git_str_gather_text_stats( |
1337 | | git_str_text_stats *stats, const git_str *buf, bool skip_bom) |
1338 | 0 | { |
1339 | 0 | const char *scan = buf->ptr, *end = buf->ptr + buf->size; |
1340 | 0 | int skip; |
1341 | |
|
1342 | 0 | memset(stats, 0, sizeof(*stats)); |
1343 | | |
1344 | | /* BOM detection */ |
1345 | 0 | skip = git_str_detect_bom(&stats->bom, buf); |
1346 | 0 | if (skip_bom) |
1347 | 0 | scan += skip; |
1348 | | |
1349 | | /* Ignore EOF character */ |
1350 | 0 | if (buf->size > 0 && end[-1] == '\032') |
1351 | 0 | end--; |
1352 | | |
1353 | | /* Counting loop */ |
1354 | 0 | while (scan < end) { |
1355 | 0 | unsigned char c = *scan++; |
1356 | |
|
1357 | 0 | if (c > 0x1F && c != 0x7F) |
1358 | 0 | stats->printable++; |
1359 | 0 | else switch (c) { |
1360 | 0 | case '\0': |
1361 | 0 | stats->nul++; |
1362 | 0 | stats->nonprintable++; |
1363 | 0 | break; |
1364 | 0 | case '\n': |
1365 | 0 | stats->lf++; |
1366 | 0 | break; |
1367 | 0 | case '\r': |
1368 | 0 | stats->cr++; |
1369 | 0 | if (scan < end && *scan == '\n') |
1370 | 0 | stats->crlf++; |
1371 | 0 | break; |
1372 | 0 | case '\t': case '\f': case '\v': case '\b': case 0x1b: /*ESC*/ |
1373 | 0 | stats->printable++; |
1374 | 0 | break; |
1375 | 0 | default: |
1376 | 0 | stats->nonprintable++; |
1377 | 0 | break; |
1378 | 0 | } |
1379 | 0 | } |
1380 | | |
1381 | | /* Treat files with a bare CR as binary */ |
1382 | 0 | return (stats->cr != stats->crlf || stats->nul > 0 || |
1383 | 0 | ((stats->printable >> 7) < stats->nonprintable)); |
1384 | 0 | } |