Line | Count | Source |
1 | | #define DISABLE_SIGN_COMPARE_WARNINGS |
2 | | |
3 | | #include "git-compat-util.h" |
4 | | #include "strbuf.h" |
5 | | #include "utf8.h" |
6 | | |
7 | | /* This code is originally from https://www.cl.cam.ac.uk/~mgk25/ucs/ */ |
8 | | |
9 | | static const char utf16_be_bom[] = {'\xFE', '\xFF'}; |
10 | | static const char utf16_le_bom[] = {'\xFF', '\xFE'}; |
11 | | static const char utf32_be_bom[] = {'\0', '\0', '\xFE', '\xFF'}; |
12 | | static const char utf32_le_bom[] = {'\xFF', '\xFE', '\0', '\0'}; |
13 | | |
14 | | struct interval { |
15 | | ucs_char_t first; |
16 | | ucs_char_t last; |
17 | | }; |
18 | | |
19 | | size_t display_mode_esc_sequence_len(const char *s) |
20 | 0 | { |
21 | 0 | const char *p = s; |
22 | 0 | if (*p++ != '\033') |
23 | 0 | return 0; |
24 | 0 | if (*p++ != '[') |
25 | 0 | return 0; |
26 | 0 | while (isdigit(*p) || *p == ';') |
27 | 0 | p++; |
28 | 0 | if (*p++ != 'm') |
29 | 0 | return 0; |
30 | 0 | return p - s; |
31 | 0 | } |
32 | | |
33 | | /* auxiliary function for binary search in interval table */ |
34 | | static int bisearch(ucs_char_t ucs, const struct interval *table, int max) |
35 | 0 | { |
36 | 0 | int min = 0; |
37 | 0 | int mid; |
38 | |
|
39 | 0 | if (ucs < table[0].first || ucs > table[max].last) |
40 | 0 | return 0; |
41 | 0 | while (max >= min) { |
42 | 0 | mid = min + (max - min) / 2; |
43 | 0 | if (ucs > table[mid].last) |
44 | 0 | min = mid + 1; |
45 | 0 | else if (ucs < table[mid].first) |
46 | 0 | max = mid - 1; |
47 | 0 | else |
48 | 0 | return 1; |
49 | 0 | } |
50 | | |
51 | 0 | return 0; |
52 | 0 | } |
53 | | |
54 | | /* The following two functions define the column width of an ISO 10646 |
55 | | * character as follows: |
56 | | * |
57 | | * - The null character (U+0000) has a column width of 0. |
58 | | * |
59 | | * - Other C0/C1 control characters and DEL will lead to a return |
60 | | * value of -1. |
61 | | * |
62 | | * - Non-spacing and enclosing combining characters (general |
63 | | * category code Mn or Me in the Unicode database) have a |
64 | | * column width of 0. |
65 | | * |
66 | | * - SOFT HYPHEN (U+00AD) has a column width of 1. |
67 | | * |
68 | | * - Other format characters (general category code Cf in the Unicode |
69 | | * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0. |
70 | | * |
71 | | * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) |
72 | | * have a column width of 0. |
73 | | * |
74 | | * - Spacing characters in the East Asian Wide (W) or East Asian |
75 | | * Full-width (F) category as defined in Unicode Technical |
76 | | * Report #11 have a column width of 2. |
77 | | * |
78 | | * - All remaining characters (including all printable |
79 | | * ISO 8859-1 and WGL4 characters, Unicode control characters, |
80 | | * etc.) have a column width of 1. |
81 | | * |
82 | | * This implementation assumes that ucs_char_t characters are encoded |
83 | | * in ISO 10646. |
84 | | */ |
85 | | |
86 | | static int git_wcwidth(ucs_char_t ch) |
87 | 0 | { |
88 | | /* |
89 | | * Sorted list of non-overlapping intervals of non-spacing characters, |
90 | | */ |
91 | 0 | #include "unicode-width.h" |
92 | | |
93 | | /* test for 8-bit control characters */ |
94 | 0 | if (ch == 0) |
95 | 0 | return 0; |
96 | 0 | if (ch < 32 || (ch >= 0x7f && ch < 0xa0)) |
97 | 0 | return -1; |
98 | | |
99 | | /* binary search in table of non-spacing characters */ |
100 | 0 | if (bisearch(ch, zero_width, ARRAY_SIZE(zero_width) - 1)) |
101 | 0 | return 0; |
102 | | |
103 | | /* binary search in table of double width characters */ |
104 | 0 | if (bisearch(ch, double_width, ARRAY_SIZE(double_width) - 1)) |
105 | 0 | return 2; |
106 | | |
107 | 0 | return 1; |
108 | 0 | } |
109 | | |
110 | | /* |
111 | | * Pick one ucs character starting from the location *start points at, |
112 | | * and return it, while updating the *start pointer to point at the |
113 | | * end of that character. When remainder_p is not NULL, the location |
114 | | * holds the number of bytes remaining in the string that we are allowed |
115 | | * to pick from. Otherwise we are allowed to pick up to the NUL that |
116 | | * would eventually appear in the string. *remainder_p is also reduced |
117 | | * by the number of bytes we have consumed. |
118 | | * |
119 | | * If the string was not a valid UTF-8, *start pointer is set to NULL |
120 | | * and the return value is undefined. |
121 | | */ |
122 | | static ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p) |
123 | 0 | { |
124 | 0 | unsigned char *s = (unsigned char *)*start; |
125 | 0 | ucs_char_t ch; |
126 | 0 | size_t remainder, incr; |
127 | | |
128 | | /* |
129 | | * A caller that assumes NUL terminated text can choose |
130 | | * not to bother with the remainder length. We will |
131 | | * stop at the first NUL. |
132 | | */ |
133 | 0 | remainder = (remainder_p ? *remainder_p : 999); |
134 | |
|
135 | 0 | if (remainder < 1) { |
136 | 0 | goto invalid; |
137 | 0 | } else if (*s < 0x80) { |
138 | | /* 0xxxxxxx */ |
139 | 0 | ch = *s; |
140 | 0 | incr = 1; |
141 | 0 | } else if ((s[0] & 0xe0) == 0xc0) { |
142 | | /* 110XXXXx 10xxxxxx */ |
143 | 0 | if (remainder < 2 || |
144 | 0 | (s[1] & 0xc0) != 0x80 || |
145 | 0 | (s[0] & 0xfe) == 0xc0) |
146 | 0 | goto invalid; |
147 | 0 | ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
148 | 0 | incr = 2; |
149 | 0 | } else if ((s[0] & 0xf0) == 0xe0) { |
150 | | /* 1110XXXX 10Xxxxxx 10xxxxxx */ |
151 | 0 | if (remainder < 3 || |
152 | 0 | (s[1] & 0xc0) != 0x80 || |
153 | 0 | (s[2] & 0xc0) != 0x80 || |
154 | | /* overlong? */ |
155 | 0 | (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) || |
156 | | /* surrogate? */ |
157 | 0 | (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) || |
158 | | /* U+FFFE or U+FFFF? */ |
159 | 0 | (s[0] == 0xef && s[1] == 0xbf && |
160 | 0 | (s[2] & 0xfe) == 0xbe)) |
161 | 0 | goto invalid; |
162 | 0 | ch = ((s[0] & 0x0f) << 12) | |
163 | 0 | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f); |
164 | 0 | incr = 3; |
165 | 0 | } else if ((s[0] & 0xf8) == 0xf0) { |
166 | | /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */ |
167 | 0 | if (remainder < 4 || |
168 | 0 | (s[1] & 0xc0) != 0x80 || |
169 | 0 | (s[2] & 0xc0) != 0x80 || |
170 | 0 | (s[3] & 0xc0) != 0x80 || |
171 | | /* overlong? */ |
172 | 0 | (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || |
173 | | /* > U+10FFFF? */ |
174 | 0 | (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4) |
175 | 0 | goto invalid; |
176 | 0 | ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) | |
177 | 0 | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f); |
178 | 0 | incr = 4; |
179 | 0 | } else { |
180 | 0 | invalid: |
181 | 0 | *start = NULL; |
182 | 0 | return 0; |
183 | 0 | } |
184 | | |
185 | 0 | *start += incr; |
186 | 0 | if (remainder_p) |
187 | 0 | *remainder_p = remainder - incr; |
188 | 0 | return ch; |
189 | 0 | } |
190 | | |
191 | | /* |
192 | | * This function returns the number of columns occupied by the character |
193 | | * pointed to by the variable start. The pointer is updated to point at |
194 | | * the next character. When remainder_p is not NULL, it points at the |
195 | | * location that stores the number of remaining bytes we can use to pick |
196 | | * a character (see pick_one_utf8_char() above). |
197 | | */ |
198 | | int utf8_width(const char **start, size_t *remainder_p) |
199 | 0 | { |
200 | 0 | ucs_char_t ch = pick_one_utf8_char(start, remainder_p); |
201 | 0 | if (!*start) |
202 | 0 | return 0; |
203 | 0 | return git_wcwidth(ch); |
204 | 0 | } |
205 | | |
206 | | /* |
207 | | * Returns the total number of columns required by a null-terminated |
208 | | * string, assuming that the string is utf8. Returns strlen() instead |
209 | | * if the string does not look like a valid utf8 string. |
210 | | */ |
211 | | int utf8_strnwidth(const char *string, size_t len, int skip_ansi) |
212 | 0 | { |
213 | 0 | const char *orig = string; |
214 | 0 | size_t width = 0; |
215 | |
|
216 | 0 | while (string && string < orig + len) { |
217 | 0 | int glyph_width; |
218 | 0 | size_t skip; |
219 | |
|
220 | 0 | while (skip_ansi && |
221 | 0 | (skip = display_mode_esc_sequence_len(string)) != 0) |
222 | 0 | string += skip; |
223 | |
|
224 | 0 | glyph_width = utf8_width(&string, NULL); |
225 | 0 | if (glyph_width > 0) |
226 | 0 | width += glyph_width; |
227 | 0 | } |
228 | | |
229 | | /* |
230 | | * TODO: fix the interface of this function and `utf8_strwidth()` to |
231 | | * return `size_t` instead of `int`. |
232 | | */ |
233 | 0 | return cast_size_t_to_int(string ? width : len); |
234 | 0 | } |
235 | | |
236 | | int utf8_strwidth(const char *string) |
237 | 0 | { |
238 | 0 | return utf8_strnwidth(string, strlen(string), 0); |
239 | 0 | } |
240 | | |
241 | | int is_utf8(const char *text) |
242 | 0 | { |
243 | 0 | while (*text) { |
244 | 0 | if (*text == '\n' || *text == '\t' || *text == '\r') { |
245 | 0 | text++; |
246 | 0 | continue; |
247 | 0 | } |
248 | 0 | utf8_width(&text, NULL); |
249 | 0 | if (!text) |
250 | 0 | return 0; |
251 | 0 | } |
252 | 0 | return 1; |
253 | 0 | } |
254 | | |
255 | | static void strbuf_add_indented_text(struct strbuf *buf, const char *text, |
256 | | int indent, int indent2) |
257 | 0 | { |
258 | 0 | if (indent < 0) |
259 | 0 | indent = 0; |
260 | 0 | while (*text) { |
261 | 0 | const char *eol = strchrnul(text, '\n'); |
262 | 0 | if (*eol == '\n') |
263 | 0 | eol++; |
264 | 0 | strbuf_addchars(buf, ' ', indent); |
265 | 0 | strbuf_add(buf, text, eol - text); |
266 | 0 | text = eol; |
267 | 0 | indent = indent2; |
268 | 0 | } |
269 | 0 | } |
270 | | |
271 | | /* |
272 | | * Wrap the text, if necessary. The variable indent is the indent for the |
273 | | * first line, indent2 is the indent for all other lines. |
274 | | * If indent is negative, assume that already -indent columns have been |
275 | | * consumed (and no extra indent is necessary for the first line). |
276 | | */ |
277 | | void strbuf_add_wrapped_text(struct strbuf *buf, |
278 | | const char *text, int indent1, int indent2, int width) |
279 | 0 | { |
280 | 0 | int indent, w, assume_utf8 = 1; |
281 | 0 | const char *bol, *space, *start = text; |
282 | 0 | size_t orig_len = buf->len; |
283 | |
|
284 | 0 | if (width <= 0) { |
285 | 0 | strbuf_add_indented_text(buf, text, indent1, indent2); |
286 | 0 | return; |
287 | 0 | } |
288 | | |
289 | 0 | retry: |
290 | 0 | bol = text; |
291 | 0 | w = indent = indent1; |
292 | 0 | space = NULL; |
293 | 0 | if (indent < 0) { |
294 | 0 | w = -indent; |
295 | 0 | space = text; |
296 | 0 | } |
297 | |
|
298 | 0 | for (;;) { |
299 | 0 | char c; |
300 | 0 | size_t skip; |
301 | |
|
302 | 0 | while ((skip = display_mode_esc_sequence_len(text))) |
303 | 0 | text += skip; |
304 | |
|
305 | 0 | c = *text; |
306 | 0 | if (!c || isspace(c)) { |
307 | 0 | if (w <= width || !space) { |
308 | 0 | const char *start = bol; |
309 | 0 | if (!c && text == start) |
310 | 0 | return; |
311 | 0 | if (space) |
312 | 0 | start = space; |
313 | 0 | else |
314 | 0 | strbuf_addchars(buf, ' ', indent); |
315 | 0 | strbuf_add(buf, start, text - start); |
316 | 0 | if (!c) |
317 | 0 | return; |
318 | 0 | space = text; |
319 | 0 | if (c == '\t') |
320 | 0 | w |= 0x07; |
321 | 0 | else if (c == '\n') { |
322 | 0 | space++; |
323 | 0 | if (*space == '\n') { |
324 | 0 | strbuf_addch(buf, '\n'); |
325 | 0 | goto new_line; |
326 | 0 | } |
327 | 0 | else if (!isalnum(*space)) |
328 | 0 | goto new_line; |
329 | 0 | else |
330 | 0 | strbuf_addch(buf, ' '); |
331 | 0 | } |
332 | 0 | w++; |
333 | 0 | text++; |
334 | 0 | } |
335 | 0 | else { |
336 | 0 | new_line: |
337 | 0 | strbuf_addch(buf, '\n'); |
338 | 0 | text = bol = space + isspace(*space); |
339 | 0 | space = NULL; |
340 | 0 | w = indent = indent2; |
341 | 0 | } |
342 | 0 | continue; |
343 | 0 | } |
344 | 0 | if (assume_utf8) { |
345 | 0 | w += utf8_width(&text, NULL); |
346 | 0 | if (!text) { |
347 | 0 | assume_utf8 = 0; |
348 | 0 | text = start; |
349 | 0 | strbuf_setlen(buf, orig_len); |
350 | 0 | goto retry; |
351 | 0 | } |
352 | 0 | } else { |
353 | 0 | w++; |
354 | 0 | text++; |
355 | 0 | } |
356 | 0 | } |
357 | 0 | } |
358 | | |
359 | | void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len, |
360 | | int indent, int indent2, int width) |
361 | 0 | { |
362 | 0 | char *tmp = xstrndup(data, len); |
363 | 0 | strbuf_add_wrapped_text(buf, tmp, indent, indent2, width); |
364 | 0 | free(tmp); |
365 | 0 | } |
366 | | |
367 | | void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width, |
368 | | const char *subst) |
369 | 0 | { |
370 | 0 | const char *src = sb_src->buf, *end = sb_src->buf + sb_src->len; |
371 | 0 | struct strbuf dst; |
372 | 0 | int w = 0; |
373 | |
|
374 | 0 | strbuf_init(&dst, sb_src->len); |
375 | |
|
376 | 0 | while (src < end) { |
377 | 0 | const char *old; |
378 | 0 | int glyph_width; |
379 | 0 | size_t n; |
380 | |
|
381 | 0 | while ((n = display_mode_esc_sequence_len(src))) { |
382 | 0 | strbuf_add(&dst, src, n); |
383 | 0 | src += n; |
384 | 0 | } |
385 | |
|
386 | 0 | if (src >= end) |
387 | 0 | break; |
388 | | |
389 | 0 | old = src; |
390 | 0 | glyph_width = utf8_width((const char**)&src, NULL); |
391 | 0 | if (!src) /* broken utf-8, do nothing */ |
392 | 0 | goto out; |
393 | | |
394 | | /* |
395 | | * In case we see a control character we copy it into the |
396 | | * buffer, but don't add it to the width. |
397 | | */ |
398 | 0 | if (glyph_width < 0) |
399 | 0 | glyph_width = 0; |
400 | |
|
401 | 0 | if (glyph_width && w >= pos && w < pos + width) { |
402 | 0 | if (subst) { |
403 | 0 | strbuf_addstr(&dst, subst); |
404 | 0 | subst = NULL; |
405 | 0 | } |
406 | 0 | } else { |
407 | 0 | strbuf_add(&dst, old, src - old); |
408 | 0 | } |
409 | |
|
410 | 0 | w += glyph_width; |
411 | 0 | } |
412 | | |
413 | 0 | strbuf_swap(sb_src, &dst); |
414 | 0 | out: |
415 | 0 | strbuf_release(&dst); |
416 | 0 | } |
417 | | |
418 | | /* |
419 | | * Returns true (1) if the src encoding name matches the dst encoding |
420 | | * name directly or one of its alternative names. E.g. UTF-16BE is the |
421 | | * same as UTF16BE. |
422 | | */ |
423 | | static int same_utf_encoding(const char *src, const char *dst) |
424 | 0 | { |
425 | 0 | if (skip_iprefix(src, "utf", &src) && skip_iprefix(dst, "utf", &dst)) { |
426 | 0 | skip_prefix(src, "-", &src); |
427 | 0 | skip_prefix(dst, "-", &dst); |
428 | 0 | return !strcasecmp(src, dst); |
429 | 0 | } |
430 | 0 | return 0; |
431 | 0 | } |
432 | | |
433 | | int is_encoding_utf8(const char *name) |
434 | 0 | { |
435 | 0 | if (!name) |
436 | 0 | return 1; |
437 | 0 | if (same_utf_encoding("utf-8", name)) |
438 | 0 | return 1; |
439 | 0 | return 0; |
440 | 0 | } |
441 | | |
442 | | int same_encoding(const char *src, const char *dst) |
443 | 0 | { |
444 | 0 | static const char utf8[] = "UTF-8"; |
445 | |
|
446 | 0 | if (!src) |
447 | 0 | src = utf8; |
448 | 0 | if (!dst) |
449 | 0 | dst = utf8; |
450 | 0 | if (same_utf_encoding(src, dst)) |
451 | 0 | return 1; |
452 | 0 | return !strcasecmp(src, dst); |
453 | 0 | } |
454 | | |
455 | | /* |
456 | | * Wrapper for fprintf and returns the total number of columns required |
457 | | * for the printed string, assuming that the string is utf8. |
458 | | */ |
459 | | int utf8_fprintf(FILE *stream, const char *format, ...) |
460 | 0 | { |
461 | 0 | struct strbuf buf = STRBUF_INIT; |
462 | 0 | va_list arg; |
463 | 0 | int columns; |
464 | |
|
465 | 0 | va_start(arg, format); |
466 | 0 | strbuf_vaddf(&buf, format, arg); |
467 | 0 | va_end(arg); |
468 | |
|
469 | 0 | columns = fputs(buf.buf, stream); |
470 | 0 | if (0 <= columns) /* keep the error from the I/O */ |
471 | 0 | columns = utf8_strwidth(buf.buf); |
472 | 0 | strbuf_release(&buf); |
473 | 0 | return columns; |
474 | 0 | } |
475 | | |
476 | | /* |
477 | | * Given a buffer and its encoding, return it re-encoded |
478 | | * with iconv. If the conversion fails, returns NULL. |
479 | | */ |
480 | | #ifndef NO_ICONV |
481 | | #if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6)) |
482 | | typedef const char * iconv_ibp; |
483 | | #else |
484 | | typedef char * iconv_ibp; |
485 | | #endif |
486 | | char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, |
487 | | size_t bom_len, size_t *outsz_p) |
488 | 0 | { |
489 | 0 | size_t outsz, outalloc; |
490 | 0 | char *out, *outpos; |
491 | 0 | iconv_ibp cp; |
492 | |
|
493 | 0 | outsz = insz; |
494 | 0 | outalloc = st_add(outsz, 1 + bom_len); /* for terminating NUL */ |
495 | 0 | out = xmalloc(outalloc); |
496 | 0 | outpos = out + bom_len; |
497 | 0 | cp = (iconv_ibp)in; |
498 | |
|
499 | 0 | while (1) { |
500 | 0 | size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz); |
501 | |
|
502 | 0 | if (cnt == (size_t) -1) { |
503 | 0 | size_t sofar; |
504 | 0 | if (errno != E2BIG) { |
505 | 0 | free(out); |
506 | 0 | return NULL; |
507 | 0 | } |
508 | | /* insz has remaining number of bytes. |
509 | | * since we started outsz the same as insz, |
510 | | * it is likely that insz is not enough for |
511 | | * converting the rest. |
512 | | */ |
513 | 0 | sofar = outpos - out; |
514 | 0 | outalloc = st_add3(sofar, st_mult(insz, 2), 32); |
515 | 0 | out = xrealloc(out, outalloc); |
516 | 0 | outpos = out + sofar; |
517 | 0 | outsz = outalloc - sofar - 1; |
518 | 0 | } |
519 | 0 | else { |
520 | 0 | *outpos = '\0'; |
521 | 0 | if (outsz_p) |
522 | 0 | *outsz_p = outpos - out; |
523 | 0 | break; |
524 | 0 | } |
525 | 0 | } |
526 | 0 | return out; |
527 | 0 | } |
528 | | |
529 | | static const char *fallback_encoding(const char *name) |
530 | 0 | { |
531 | | /* |
532 | | * Some platforms do not have the variously spelled variants of |
533 | | * UTF-8, so let's fall back to trying the most official |
534 | | * spelling. We do so only as a fallback in case the platform |
535 | | * does understand the user's spelling, but not our official |
536 | | * one. |
537 | | */ |
538 | 0 | if (is_encoding_utf8(name)) |
539 | 0 | return "UTF-8"; |
540 | | |
541 | | /* |
542 | | * Even though latin-1 is still seen in e-mail |
543 | | * headers, some platforms only install ISO-8859-1. |
544 | | */ |
545 | 0 | if (!strcasecmp(name, "latin-1")) |
546 | 0 | return "ISO-8859-1"; |
547 | | |
548 | 0 | return name; |
549 | 0 | } |
550 | | |
551 | | char *reencode_string_len(const char *in, size_t insz, |
552 | | const char *out_encoding, const char *in_encoding, |
553 | | size_t *outsz) |
554 | 0 | { |
555 | 0 | iconv_t conv; |
556 | 0 | char *out; |
557 | 0 | const char *bom_str = NULL; |
558 | 0 | size_t bom_len = 0; |
559 | |
|
560 | 0 | if (!in_encoding) |
561 | 0 | return NULL; |
562 | | |
563 | | /* UTF-16LE-BOM is the same as UTF-16 for reading */ |
564 | 0 | if (same_utf_encoding("UTF-16LE-BOM", in_encoding)) |
565 | 0 | in_encoding = "UTF-16"; |
566 | | |
567 | | /* |
568 | | * For writing, UTF-16 iconv typically creates "UTF-16BE-BOM" |
569 | | * Some users under Windows want the little endian version |
570 | | * |
571 | | * We handle UTF-16 and UTF-32 ourselves only if the platform does not |
572 | | * provide a BOM (which we require), since we want to match the behavior |
573 | | * of the system tools and libc as much as possible. |
574 | | */ |
575 | 0 | if (same_utf_encoding("UTF-16LE-BOM", out_encoding)) { |
576 | 0 | bom_str = utf16_le_bom; |
577 | 0 | bom_len = sizeof(utf16_le_bom); |
578 | 0 | out_encoding = "UTF-16LE"; |
579 | 0 | } else if (same_utf_encoding("UTF-16BE-BOM", out_encoding)) { |
580 | 0 | bom_str = utf16_be_bom; |
581 | 0 | bom_len = sizeof(utf16_be_bom); |
582 | 0 | out_encoding = "UTF-16BE"; |
583 | | #ifdef ICONV_OMITS_BOM |
584 | | } else if (same_utf_encoding("UTF-16", out_encoding)) { |
585 | | bom_str = utf16_be_bom; |
586 | | bom_len = sizeof(utf16_be_bom); |
587 | | out_encoding = "UTF-16BE"; |
588 | | } else if (same_utf_encoding("UTF-32", out_encoding)) { |
589 | | bom_str = utf32_be_bom; |
590 | | bom_len = sizeof(utf32_be_bom); |
591 | | out_encoding = "UTF-32BE"; |
592 | | #endif |
593 | 0 | } |
594 | |
|
595 | 0 | conv = iconv_open(out_encoding, in_encoding); |
596 | 0 | if (conv == (iconv_t) -1) { |
597 | 0 | in_encoding = fallback_encoding(in_encoding); |
598 | 0 | out_encoding = fallback_encoding(out_encoding); |
599 | |
|
600 | 0 | conv = iconv_open(out_encoding, in_encoding); |
601 | 0 | if (conv == (iconv_t) -1) |
602 | 0 | return NULL; |
603 | 0 | } |
604 | 0 | out = reencode_string_iconv(in, insz, conv, bom_len, outsz); |
605 | 0 | iconv_close(conv); |
606 | 0 | if (out && bom_str && bom_len) |
607 | 0 | memcpy(out, bom_str, bom_len); |
608 | 0 | return out; |
609 | 0 | } |
610 | | #endif |
611 | | |
612 | | static int has_bom_prefix(const char *data, size_t len, |
613 | | const char *bom, size_t bom_len) |
614 | 0 | { |
615 | 0 | return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len); |
616 | 0 | } |
617 | | |
618 | | int has_prohibited_utf_bom(const char *enc, const char *data, size_t len) |
619 | 0 | { |
620 | 0 | return ( |
621 | 0 | (same_utf_encoding("UTF-16BE", enc) || |
622 | 0 | same_utf_encoding("UTF-16LE", enc)) && |
623 | 0 | (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) || |
624 | 0 | has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom))) |
625 | 0 | ) || ( |
626 | 0 | (same_utf_encoding("UTF-32BE", enc) || |
627 | 0 | same_utf_encoding("UTF-32LE", enc)) && |
628 | 0 | (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) || |
629 | 0 | has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom))) |
630 | 0 | ); |
631 | 0 | } |
632 | | |
633 | | int is_missing_required_utf_bom(const char *enc, const char *data, size_t len) |
634 | 0 | { |
635 | 0 | return ( |
636 | 0 | (same_utf_encoding(enc, "UTF-16")) && |
637 | 0 | !(has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) || |
638 | 0 | has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom))) |
639 | 0 | ) || ( |
640 | 0 | (same_utf_encoding(enc, "UTF-32")) && |
641 | 0 | !(has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) || |
642 | 0 | has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom))) |
643 | 0 | ); |
644 | 0 | } |
645 | | |
646 | | /* |
647 | | * Returns first character length in bytes for multi-byte `text` according to |
648 | | * `encoding`. |
649 | | * |
650 | | * - The `text` pointer is updated to point at the next character. |
651 | | * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes |
652 | | * we can consume from text, and on exit `*remainder_p` is reduced by returned |
653 | | * character length. Otherwise `text` is treated as limited by NUL. |
654 | | */ |
655 | | int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding) |
656 | 0 | { |
657 | 0 | int chrlen; |
658 | 0 | const char *p = *text; |
659 | 0 | size_t r = (remainder_p ? *remainder_p : SIZE_MAX); |
660 | |
|
661 | 0 | if (r < 1) |
662 | 0 | return 0; |
663 | | |
664 | 0 | if (is_encoding_utf8(encoding)) { |
665 | 0 | pick_one_utf8_char(&p, &r); |
666 | |
|
667 | 0 | chrlen = p ? (p - *text) |
668 | 0 | : 1 /* not valid UTF-8 -> raw byte sequence */; |
669 | 0 | } |
670 | 0 | else { |
671 | | /* |
672 | | * TODO use iconv to decode one char and obtain its chrlen |
673 | | * for now, let's treat encodings != UTF-8 as one-byte |
674 | | */ |
675 | 0 | chrlen = 1; |
676 | 0 | } |
677 | |
|
678 | 0 | *text += chrlen; |
679 | 0 | if (remainder_p) |
680 | 0 | *remainder_p -= chrlen; |
681 | |
|
682 | 0 | return chrlen; |
683 | 0 | } |
684 | | |
685 | | /* |
686 | | * Pick the next char from the stream, ignoring codepoints an HFS+ would. |
687 | | * Note that this is _not_ complete by any means. It's just enough |
688 | | * to make is_hfs_dotgit() work, and should not be used otherwise. |
689 | | */ |
690 | | static ucs_char_t next_hfs_char(const char **in) |
691 | 0 | { |
692 | 0 | while (1) { |
693 | 0 | ucs_char_t out = pick_one_utf8_char(in, NULL); |
694 | | /* |
695 | | * check for malformed utf8. Technically this |
696 | | * gets converted to a percent-sequence, but |
697 | | * returning 0 is good enough for is_hfs_dotgit |
698 | | * to realize it cannot be .git |
699 | | */ |
700 | 0 | if (!*in) |
701 | 0 | return 0; |
702 | | |
703 | | /* these code points are ignored completely */ |
704 | 0 | switch (out) { |
705 | 0 | case 0x200c: /* ZERO WIDTH NON-JOINER */ |
706 | 0 | case 0x200d: /* ZERO WIDTH JOINER */ |
707 | 0 | case 0x200e: /* LEFT-TO-RIGHT MARK */ |
708 | 0 | case 0x200f: /* RIGHT-TO-LEFT MARK */ |
709 | 0 | case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */ |
710 | 0 | case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */ |
711 | 0 | case 0x202c: /* POP DIRECTIONAL FORMATTING */ |
712 | 0 | case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */ |
713 | 0 | case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */ |
714 | 0 | case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */ |
715 | 0 | case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */ |
716 | 0 | case 0x206c: /* INHIBIT ARABIC FORM SHAPING */ |
717 | 0 | case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */ |
718 | 0 | case 0x206e: /* NATIONAL DIGIT SHAPES */ |
719 | 0 | case 0x206f: /* NOMINAL DIGIT SHAPES */ |
720 | 0 | case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */ |
721 | 0 | continue; |
722 | 0 | } |
723 | | |
724 | 0 | return out; |
725 | 0 | } |
726 | 0 | } |
727 | | |
728 | | static int is_hfs_dot_generic(const char *path, |
729 | | const char *needle, size_t needle_len) |
730 | 0 | { |
731 | 0 | ucs_char_t c; |
732 | |
|
733 | 0 | c = next_hfs_char(&path); |
734 | 0 | if (c != '.') |
735 | 0 | return 0; |
736 | | |
737 | | /* |
738 | | * there's a great deal of other case-folding that occurs |
739 | | * in HFS+, but this is enough to catch our fairly vanilla |
740 | | * hard-coded needles. |
741 | | */ |
742 | 0 | for (; needle_len > 0; needle++, needle_len--) { |
743 | 0 | c = next_hfs_char(&path); |
744 | | |
745 | | /* |
746 | | * We know our needles contain only ASCII, so we clamp here to |
747 | | * make the results of tolower() sane. |
748 | | */ |
749 | 0 | if (c > 127) |
750 | 0 | return 0; |
751 | 0 | if (tolower(c) != *needle) |
752 | 0 | return 0; |
753 | 0 | } |
754 | | |
755 | 0 | c = next_hfs_char(&path); |
756 | 0 | if (c && !is_dir_sep(c)) |
757 | 0 | return 0; |
758 | | |
759 | 0 | return 1; |
760 | 0 | } |
761 | | |
762 | | /* |
763 | | * Inline wrapper to make sure the compiler resolves strlen() on literals at |
764 | | * compile time. |
765 | | */ |
766 | | static inline int is_hfs_dot_str(const char *path, const char *needle) |
767 | 0 | { |
768 | 0 | return is_hfs_dot_generic(path, needle, strlen(needle)); |
769 | 0 | } |
770 | | |
771 | | int is_hfs_dotgit(const char *path) |
772 | 0 | { |
773 | 0 | return is_hfs_dot_str(path, "git"); |
774 | 0 | } |
775 | | |
776 | | int is_hfs_dotgitmodules(const char *path) |
777 | 0 | { |
778 | 0 | return is_hfs_dot_str(path, "gitmodules"); |
779 | 0 | } |
780 | | |
781 | | int is_hfs_dotgitignore(const char *path) |
782 | 0 | { |
783 | 0 | return is_hfs_dot_str(path, "gitignore"); |
784 | 0 | } |
785 | | |
786 | | int is_hfs_dotgitattributes(const char *path) |
787 | 0 | { |
788 | 0 | return is_hfs_dot_str(path, "gitattributes"); |
789 | 0 | } |
790 | | |
791 | | int is_hfs_dotmailmap(const char *path) |
792 | 0 | { |
793 | 0 | return is_hfs_dot_str(path, "mailmap"); |
794 | 0 | } |
795 | | |
796 | | const char utf8_bom[] = "\357\273\277"; |
797 | | |
798 | | int skip_utf8_bom(char **text, size_t len) |
799 | 0 | { |
800 | 0 | if (len < strlen(utf8_bom) || |
801 | 0 | memcmp(*text, utf8_bom, strlen(utf8_bom))) |
802 | 0 | return 0; |
803 | 0 | *text += strlen(utf8_bom); |
804 | 0 | return 1; |
805 | 0 | } |
806 | | |
807 | | void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width, |
808 | | const char *s) |
809 | 0 | { |
810 | 0 | size_t slen = strlen(s); |
811 | 0 | int display_len = utf8_strnwidth(s, slen, 0); |
812 | 0 | int utf8_compensation = slen - display_len; |
813 | |
|
814 | 0 | if (display_len >= width) { |
815 | 0 | strbuf_addstr(buf, s); |
816 | 0 | return; |
817 | 0 | } |
818 | | |
819 | 0 | if (position == ALIGN_LEFT) |
820 | 0 | strbuf_addf(buf, "%-*s", width + utf8_compensation, s); |
821 | 0 | else if (position == ALIGN_MIDDLE) { |
822 | 0 | int left = (width - display_len) / 2; |
823 | 0 | strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s); |
824 | 0 | } else if (position == ALIGN_RIGHT) |
825 | 0 | strbuf_addf(buf, "%*s", width + utf8_compensation, s); |
826 | 0 | } |