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