blob: eb785875042920b84e56abbd235ef9eb7ef22ac4 [file] [log] [blame]
Johannes Schindelin9e832662006-12-22 22:06:08 +01001#include "git-compat-util.h"
Johannes Schindelina94410c2008-11-10 18:47:00 +01002#include "strbuf.h"
Johannes Schindelin9e832662006-12-22 22:06:08 +01003#include "utf8.h"
4
5/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
6
7struct interval {
John Keepinga68a67d2014-02-16 16:06:04 +00008 ucs_char_t first;
9 ucs_char_t last;
Johannes Schindelin9e832662006-12-22 22:06:08 +010010};
11
Nguyễn Thái Ngọc Duy16406322013-04-19 09:08:52 +100012size_t display_mode_esc_sequence_len(const char *s)
Nguyễn Thái Ngọc Duy4247fe72013-04-19 09:08:44 +100013{
14 const char *p = s;
15 if (*p++ != '\033')
16 return 0;
17 if (*p++ != '[')
18 return 0;
19 while (isdigit(*p) || *p == ';')
20 p++;
21 if (*p++ != 'm')
22 return 0;
23 return p - s;
24}
25
Johannes Schindelin9e832662006-12-22 22:06:08 +010026/* auxiliary function for binary search in interval table */
Junio C Hamanof3fa1832007-11-08 15:35:32 -080027static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
28{
Johannes Schindelin9e832662006-12-22 22:06:08 +010029 int min = 0;
30 int mid;
31
32 if (ucs < table[0].first || ucs > table[max].last)
33 return 0;
34 while (max >= min) {
Derrick Stolee19716b22017-10-08 14:29:37 -040035 mid = min + (max - min) / 2;
Johannes Schindelin9e832662006-12-22 22:06:08 +010036 if (ucs > table[mid].last)
37 min = mid + 1;
38 else if (ucs < table[mid].first)
39 max = mid - 1;
40 else
41 return 1;
42 }
43
44 return 0;
45}
46
47/* The following two functions define the column width of an ISO 10646
48 * character as follows:
49 *
50 * - The null character (U+0000) has a column width of 0.
51 *
52 * - Other C0/C1 control characters and DEL will lead to a return
53 * value of -1.
54 *
55 * - Non-spacing and enclosing combining characters (general
56 * category code Mn or Me in the Unicode database) have a
57 * column width of 0.
58 *
59 * - SOFT HYPHEN (U+00AD) has a column width of 1.
60 *
61 * - Other format characters (general category code Cf in the Unicode
62 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
63 *
64 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
65 * have a column width of 0.
66 *
67 * - Spacing characters in the East Asian Wide (W) or East Asian
68 * Full-width (F) category as defined in Unicode Technical
69 * Report #11 have a column width of 2.
70 *
71 * - All remaining characters (including all printable
72 * ISO 8859-1 and WGL4 characters, Unicode control characters,
73 * etc.) have a column width of 1.
74 *
Ramsay Jones28321142007-03-03 18:28:57 +000075 * This implementation assumes that ucs_char_t characters are encoded
Johannes Schindelin9e832662006-12-22 22:06:08 +010076 * in ISO 10646.
77 */
78
Amos Waterlandb51be132007-05-08 00:46:08 -040079static int git_wcwidth(ucs_char_t ch)
Johannes Schindelin9e832662006-12-22 22:06:08 +010080{
81 /*
82 * Sorted list of non-overlapping intervals of non-spacing characters,
Johannes Schindelin9e832662006-12-22 22:06:08 +010083 */
Stefan Bellere233bef2018-04-10 14:26:17 -070084#include "unicode-width.h"
Johannes Schindelin9e832662006-12-22 22:06:08 +010085
86 /* test for 8-bit control characters */
87 if (ch == 0)
88 return 0;
89 if (ch < 32 || (ch >= 0x7f && ch < 0xa0))
90 return -1;
91
92 /* binary search in table of non-spacing characters */
Torsten Bögershausen9c943892014-05-09 23:51:44 +020093 if (bisearch(ch, zero_width, sizeof(zero_width)
Johannes Schindelin9e832662006-12-22 22:06:08 +010094 / sizeof(struct interval) - 1))
95 return 0;
96
Torsten Bögershausen08460342014-05-09 23:51:38 +020097 /* binary search in table of double width characters */
98 if (bisearch(ch, double_width, sizeof(double_width)
99 / sizeof(struct interval) - 1))
100 return 2;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100101
Torsten Bögershausen08460342014-05-09 23:51:38 +0200102 return 1;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100103}
104
105/*
Junio C Hamano396ccf12008-01-06 19:02:22 -0800106 * Pick one ucs character starting from the location *start points at,
107 * and return it, while updating the *start pointer to point at the
Junio C Hamano44b25b82008-01-02 01:49:58 -0800108 * end of that character. When remainder_p is not NULL, the location
109 * holds the number of bytes remaining in the string that we are allowed
110 * to pick from. Otherwise we are allowed to pick up to the NUL that
111 * would eventually appear in the string. *remainder_p is also reduced
112 * by the number of bytes we have consumed.
Junio C Hamano396ccf12008-01-06 19:02:22 -0800113 *
114 * If the string was not a valid UTF-8, *start pointer is set to NULL
115 * and the return value is undefined.
Johannes Schindelin9e832662006-12-22 22:06:08 +0100116 */
Junio C Hamano5e133b82010-01-11 22:32:29 -0800117static ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p)
Johannes Schindelin9e832662006-12-22 22:06:08 +0100118{
119 unsigned char *s = (unsigned char *)*start;
Ramsay Jones28321142007-03-03 18:28:57 +0000120 ucs_char_t ch;
Junio C Hamano44b25b82008-01-02 01:49:58 -0800121 size_t remainder, incr;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100122
Junio C Hamano44b25b82008-01-02 01:49:58 -0800123 /*
124 * A caller that assumes NUL terminated text can choose
125 * not to bother with the remainder length. We will
126 * stop at the first NUL.
127 */
128 remainder = (remainder_p ? *remainder_p : 999);
129
130 if (remainder < 1) {
131 goto invalid;
132 } else if (*s < 0x80) {
Johannes Schindelin9e832662006-12-22 22:06:08 +0100133 /* 0xxxxxxx */
134 ch = *s;
Junio C Hamano44b25b82008-01-02 01:49:58 -0800135 incr = 1;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100136 } else if ((s[0] & 0xe0) == 0xc0) {
137 /* 110XXXXx 10xxxxxx */
Junio C Hamano44b25b82008-01-02 01:49:58 -0800138 if (remainder < 2 ||
139 (s[1] & 0xc0) != 0x80 ||
140 (s[0] & 0xfe) == 0xc0)
Johannes Schindelin9e832662006-12-22 22:06:08 +0100141 goto invalid;
142 ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
Junio C Hamano44b25b82008-01-02 01:49:58 -0800143 incr = 2;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100144 } else if ((s[0] & 0xf0) == 0xe0) {
145 /* 1110XXXX 10Xxxxxx 10xxxxxx */
Junio C Hamano44b25b82008-01-02 01:49:58 -0800146 if (remainder < 3 ||
147 (s[1] & 0xc0) != 0x80 ||
148 (s[2] & 0xc0) != 0x80 ||
149 /* overlong? */
150 (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) ||
151 /* surrogate? */
152 (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) ||
153 /* U+FFFE or U+FFFF? */
154 (s[0] == 0xef && s[1] == 0xbf &&
155 (s[2] & 0xfe) == 0xbe))
Johannes Schindelin9e832662006-12-22 22:06:08 +0100156 goto invalid;
157 ch = ((s[0] & 0x0f) << 12) |
158 ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
Junio C Hamano44b25b82008-01-02 01:49:58 -0800159 incr = 3;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100160 } else if ((s[0] & 0xf8) == 0xf0) {
161 /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
Junio C Hamano44b25b82008-01-02 01:49:58 -0800162 if (remainder < 4 ||
163 (s[1] & 0xc0) != 0x80 ||
164 (s[2] & 0xc0) != 0x80 ||
165 (s[3] & 0xc0) != 0x80 ||
166 /* overlong? */
167 (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) ||
168 /* > U+10FFFF? */
169 (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4)
Johannes Schindelin9e832662006-12-22 22:06:08 +0100170 goto invalid;
171 ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |
172 ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
Junio C Hamano44b25b82008-01-02 01:49:58 -0800173 incr = 4;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100174 } else {
175invalid:
176 *start = NULL;
177 return 0;
178 }
179
Junio C Hamano44b25b82008-01-02 01:49:58 -0800180 *start += incr;
181 if (remainder_p)
182 *remainder_p = remainder - incr;
Junio C Hamano396ccf12008-01-06 19:02:22 -0800183 return ch;
184}
185
186/*
187 * This function returns the number of columns occupied by the character
188 * pointed to by the variable start. The pointer is updated to point at
Junio C Hamano44b25b82008-01-02 01:49:58 -0800189 * the next character. When remainder_p is not NULL, it points at the
190 * location that stores the number of remaining bytes we can use to pick
191 * a character (see pick_one_utf8_char() above).
Junio C Hamano396ccf12008-01-06 19:02:22 -0800192 */
Junio C Hamano44b25b82008-01-02 01:49:58 -0800193int utf8_width(const char **start, size_t *remainder_p)
Junio C Hamano396ccf12008-01-06 19:02:22 -0800194{
Junio C Hamano44b25b82008-01-02 01:49:58 -0800195 ucs_char_t ch = pick_one_utf8_char(start, remainder_p);
Junio C Hamano396ccf12008-01-06 19:02:22 -0800196 if (!*start)
197 return 0;
Amos Waterlandb51be132007-05-08 00:46:08 -0400198 return git_wcwidth(ch);
Johannes Schindelin9e832662006-12-22 22:06:08 +0100199}
200
Geoffrey Thomas8a9391e2009-01-30 04:41:28 -0500201/*
202 * Returns the total number of columns required by a null-terminated
203 * string, assuming that the string is utf8. Returns strlen() instead
204 * if the string does not look like a valid utf8 string.
205 */
Nguyễn Thái Ngọc Duy2bc1e7e2013-04-19 09:08:45 +1000206int utf8_strnwidth(const char *string, int len, int skip_ansi)
Geoffrey Thomas8a9391e2009-01-30 04:41:28 -0500207{
208 int width = 0;
209 const char *orig = string;
210
Nguyễn Thái Ngọc Duy2bc1e7e2013-04-19 09:08:45 +1000211 if (len == -1)
212 len = strlen(string);
213 while (string && string < orig + len) {
214 int skip;
215 while (skip_ansi &&
216 (skip = display_mode_esc_sequence_len(string)) != 0)
217 string += skip;
Geoffrey Thomas8a9391e2009-01-30 04:41:28 -0500218 width += utf8_width(&string, NULL);
219 }
Nguyễn Thái Ngọc Duy2bc1e7e2013-04-19 09:08:45 +1000220 return string ? width : len;
221}
222
223int utf8_strwidth(const char *string)
224{
225 return utf8_strnwidth(string, -1, 0);
Geoffrey Thomas8a9391e2009-01-30 04:41:28 -0500226}
227
Johannes Schindelin9e832662006-12-22 22:06:08 +0100228int is_utf8(const char *text)
229{
230 while (*text) {
231 if (*text == '\n' || *text == '\t' || *text == '\r') {
232 text++;
233 continue;
234 }
Junio C Hamano44b25b82008-01-02 01:49:58 -0800235 utf8_width(&text, NULL);
Johannes Schindelin9e832662006-12-22 22:06:08 +0100236 if (!text)
237 return 0;
238 }
239 return 1;
240}
241
René Scharfe37bb5d72009-11-22 17:15:29 +0100242static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
243 int indent, int indent2)
244{
245 if (indent < 0)
246 indent = 0;
247 while (*text) {
248 const char *eol = strchrnul(text, '\n');
249 if (*eol == '\n')
250 eol++;
René Scharfe3c0ff442010-02-19 23:15:55 +0100251 strbuf_addchars(buf, ' ', indent);
René Scharfe68ad5e12010-02-19 23:16:45 +0100252 strbuf_add(buf, text, eol - text);
René Scharfe37bb5d72009-11-22 17:15:29 +0100253 text = eol;
254 indent = indent2;
255 }
256}
257
Johannes Schindelin9e832662006-12-22 22:06:08 +0100258/*
259 * Wrap the text, if necessary. The variable indent is the indent for the
260 * first line, indent2 is the indent for all other lines.
Johannes Schindelin094e03b2007-02-27 16:20:31 +0100261 * If indent is negative, assume that already -indent columns have been
262 * consumed (and no extra indent is necessary for the first line).
Johannes Schindelin9e832662006-12-22 22:06:08 +0100263 */
Steffen Prohaskae0db1762012-12-11 06:59:22 +0100264void strbuf_add_wrapped_text(struct strbuf *buf,
René Scharfe462749b2010-02-19 23:20:44 +0100265 const char *text, int indent1, int indent2, int width)
Johannes Schindelin9e832662006-12-22 22:06:08 +0100266{
René Scharfe462749b2010-02-19 23:20:44 +0100267 int indent, w, assume_utf8 = 1;
268 const char *bol, *space, *start = text;
269 size_t orig_len = buf->len;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100270
Junio C Hamano00d39472009-10-18 23:40:35 -0700271 if (width <= 0) {
René Scharfe462749b2010-02-19 23:20:44 +0100272 strbuf_add_indented_text(buf, text, indent1, indent2);
Steffen Prohaskae0db1762012-12-11 06:59:22 +0100273 return;
Junio C Hamano00d39472009-10-18 23:40:35 -0700274 }
275
René Scharfe462749b2010-02-19 23:20:44 +0100276retry:
277 bol = text;
278 w = indent = indent1;
279 space = NULL;
Johannes Schindelin094e03b2007-02-27 16:20:31 +0100280 if (indent < 0) {
281 w = -indent;
282 space = text;
283 }
284
Johannes Schindelin9e832662006-12-22 22:06:08 +0100285 for (;;) {
René Scharfe8a3c63e2009-11-23 23:40:03 +0100286 char c;
287 size_t skip;
288
289 while ((skip = display_mode_esc_sequence_len(text)))
290 text += skip;
291
292 c = *text;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100293 if (!c || isspace(c)) {
Jan H. Schönherr14e1a4e2012-10-18 16:43:28 +0200294 if (w <= width || !space) {
Johannes Schindelin9e832662006-12-22 22:06:08 +0100295 const char *start = bol;
Johannes Schindelinae0b2702007-11-11 14:14:15 +0000296 if (!c && text == start)
Steffen Prohaskae0db1762012-12-11 06:59:22 +0100297 return;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100298 if (space)
299 start = space;
300 else
René Scharfe3c0ff442010-02-19 23:15:55 +0100301 strbuf_addchars(buf, ' ', indent);
René Scharfe68ad5e12010-02-19 23:16:45 +0100302 strbuf_add(buf, start, text - start);
Johannes Schindelin094e03b2007-02-27 16:20:31 +0100303 if (!c)
Steffen Prohaskae0db1762012-12-11 06:59:22 +0100304 return;
Johannes Schindelin9e832662006-12-22 22:06:08 +0100305 space = text;
Johannes Schindelinae0b2702007-11-11 14:14:15 +0000306 if (c == '\t')
307 w |= 0x07;
308 else if (c == '\n') {
309 space++;
310 if (*space == '\n') {
René Scharfe68ad5e12010-02-19 23:16:45 +0100311 strbuf_addch(buf, '\n');
Johannes Schindelinae0b2702007-11-11 14:14:15 +0000312 goto new_line;
313 }
314 else if (!isalnum(*space))
315 goto new_line;
316 else
René Scharfe68ad5e12010-02-19 23:16:45 +0100317 strbuf_addch(buf, ' ');
Johannes Schindelinae0b2702007-11-11 14:14:15 +0000318 }
Johannes Schindelin9e832662006-12-22 22:06:08 +0100319 w++;
320 text++;
321 }
322 else {
Johannes Schindelinae0b2702007-11-11 14:14:15 +0000323new_line:
René Scharfe68ad5e12010-02-19 23:16:45 +0100324 strbuf_addch(buf, '\n');
Johannes Schindelin62273822007-03-02 15:28:00 +0100325 text = bol = space + isspace(*space);
Johannes Schindelin9e832662006-12-22 22:06:08 +0100326 space = NULL;
327 w = indent = indent2;
328 }
329 continue;
330 }
René Scharfe462749b2010-02-19 23:20:44 +0100331 if (assume_utf8) {
Junio C Hamano44b25b82008-01-02 01:49:58 -0800332 w += utf8_width(&text, NULL);
René Scharfe462749b2010-02-19 23:20:44 +0100333 if (!text) {
334 assume_utf8 = 0;
335 text = start;
336 strbuf_setlen(buf, orig_len);
337 goto retry;
338 }
339 } else {
Johannes Schindelin9e832662006-12-22 22:06:08 +0100340 w++;
341 text++;
342 }
343 }
344}
Junio C Hamanob45974a2006-12-23 23:36:55 -0800345
Steffen Prohaskae0db1762012-12-11 06:59:22 +0100346void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
Jeff King98acc832011-02-23 04:50:19 -0500347 int indent, int indent2, int width)
348{
349 char *tmp = xstrndup(data, len);
Steffen Prohaskae0db1762012-12-11 06:59:22 +0100350 strbuf_add_wrapped_text(buf, tmp, indent, indent2, width);
Jeff King98acc832011-02-23 04:50:19 -0500351 free(tmp);
Jeff King98acc832011-02-23 04:50:19 -0500352}
353
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000354void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
355 const char *subst)
356{
357 struct strbuf sb_dst = STRBUF_INIT;
358 char *src = sb_src->buf;
359 char *end = src + sb_src->len;
360 char *dst;
361 int w = 0, subst_len = 0;
362
363 if (subst)
364 subst_len = strlen(subst);
365 strbuf_grow(&sb_dst, sb_src->len + subst_len);
366 dst = sb_dst.buf;
367
368 while (src < end) {
369 char *old;
370 size_t n;
371
372 while ((n = display_mode_esc_sequence_len(src))) {
373 memcpy(dst, src, n);
374 src += n;
375 dst += n;
376 }
377
Nguyễn Thái Ngọc Duy43087592014-08-10 14:05:21 +0700378 if (src >= end)
379 break;
380
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000381 old = src;
382 n = utf8_width((const char**)&src, NULL);
383 if (!src) /* broken utf-8, do nothing */
Rene Scharfe9a012bf2017-08-30 20:20:16 +0200384 goto out;
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000385 if (n && w >= pos && w < pos + width) {
386 if (subst) {
387 memcpy(dst, subst, subst_len);
388 dst += subst_len;
389 subst = NULL;
390 }
391 w += n;
392 continue;
393 }
394 memcpy(dst, old, src - old);
395 dst += src - old;
396 w += n;
397 }
398 strbuf_setlen(&sb_dst, dst - sb_dst.buf);
Ramsay Jones980419b2013-04-27 20:43:36 +0100399 strbuf_swap(sb_src, &sb_dst);
Rene Scharfe9a012bf2017-08-30 20:20:16 +0200400out:
Ramsay Jones980419b2013-04-27 20:43:36 +0100401 strbuf_release(&sb_dst);
Nguyễn Thái Ngọc Duya7f01c62013-04-19 09:08:51 +1000402}
403
Lars Schneider2f0c4a32018-04-15 20:16:04 +0200404/*
405 * Returns true (1) if the src encoding name matches the dst encoding
406 * name directly or one of its alternative names. E.g. UTF-16BE is the
407 * same as UTF16BE.
408 */
409static int same_utf_encoding(const char *src, const char *dst)
410{
411 if (istarts_with(src, "utf") && istarts_with(dst, "utf")) {
412 /* src[3] or dst[3] might be '\0' */
413 int i = (src[3] == '-' ? 4 : 3);
414 int j = (dst[3] == '-' ? 4 : 3);
415 return !strcasecmp(src+i, dst+j);
416 }
417 return 0;
418}
419
Junio C Hamano677cfed2006-12-30 12:20:43 -0800420int is_encoding_utf8(const char *name)
421{
422 if (!name)
423 return 1;
Lars Schneider2f0c4a32018-04-15 20:16:04 +0200424 if (same_utf_encoding("utf-8", name))
Junio C Hamano677cfed2006-12-30 12:20:43 -0800425 return 1;
426 return 0;
427}
428
Junio C Hamano0e18bcd2012-10-18 22:41:56 -0700429int same_encoding(const char *src, const char *dst)
430{
Lars Schneider2f0c4a32018-04-15 20:16:04 +0200431 static const char utf8[] = "UTF-8";
432
433 if (!src)
434 src = utf8;
435 if (!dst)
436 dst = utf8;
437 if (same_utf_encoding(src, dst))
Junio C Hamano0e18bcd2012-10-18 22:41:56 -0700438 return 1;
439 return !strcasecmp(src, dst);
440}
441
Junio C Hamanob45974a2006-12-23 23:36:55 -0800442/*
Jiang Xinc0821962013-02-09 14:31:09 +0800443 * Wrapper for fprintf and returns the total number of columns required
444 * for the printed string, assuming that the string is utf8.
445 */
446int utf8_fprintf(FILE *stream, const char *format, ...)
447{
448 struct strbuf buf = STRBUF_INIT;
449 va_list arg;
450 int columns;
451
452 va_start(arg, format);
453 strbuf_vaddf(&buf, format, arg);
454 va_end(arg);
455
456 columns = fputs(buf.buf, stream);
457 if (0 <= columns) /* keep the error from the I/O */
458 columns = utf8_strwidth(buf.buf);
459 strbuf_release(&buf);
460 return columns;
461}
462
463/*
Junio C Hamanob45974a2006-12-23 23:36:55 -0800464 * Given a buffer and its encoding, return it re-encoded
465 * with iconv. If the conversion fails, returns NULL.
466 */
467#ifndef NO_ICONV
Brandon Casey309dbc82009-06-05 18:36:12 -0500468#if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6))
Ramsay Jonesfd547a92007-03-03 18:29:03 +0000469 typedef const char * iconv_ibp;
470#else
471 typedef char * iconv_ibp;
472#endif
Jeff Kingc7d017d2018-07-24 06:50:33 -0400473char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, size_t *outsz_p)
Junio C Hamanob45974a2006-12-23 23:36:55 -0800474{
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200475 size_t outsz, outalloc;
Ramsay Jonesfd547a92007-03-03 18:29:03 +0000476 char *out, *outpos;
477 iconv_ibp cp;
Junio C Hamanob45974a2006-12-23 23:36:55 -0800478
Junio C Hamanob45974a2006-12-23 23:36:55 -0800479 outsz = insz;
Jeff King77aa03d2018-07-24 06:50:10 -0400480 outalloc = st_add(outsz, 1); /* for terminating NUL */
Junio C Hamanob45974a2006-12-23 23:36:55 -0800481 out = xmalloc(outalloc);
482 outpos = out;
Ramsay Jonesfd547a92007-03-03 18:29:03 +0000483 cp = (iconv_ibp)in;
Junio C Hamanob45974a2006-12-23 23:36:55 -0800484
485 while (1) {
486 size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);
487
John Keepingdf5213b2014-02-16 16:06:03 +0000488 if (cnt == (size_t) -1) {
Junio C Hamanob45974a2006-12-23 23:36:55 -0800489 size_t sofar;
490 if (errno != E2BIG) {
491 free(out);
Junio C Hamanob45974a2006-12-23 23:36:55 -0800492 return NULL;
493 }
494 /* insz has remaining number of bytes.
495 * since we started outsz the same as insz,
496 * it is likely that insz is not enough for
497 * converting the rest.
498 */
499 sofar = outpos - out;
Jeff King77aa03d2018-07-24 06:50:10 -0400500 outalloc = st_add3(sofar, st_mult(insz, 2), 32);
Junio C Hamanob45974a2006-12-23 23:36:55 -0800501 out = xrealloc(out, outalloc);
502 outpos = out + sofar;
503 outsz = outalloc - sofar - 1;
504 }
505 else {
506 *outpos = '\0';
Nguyễn Thái Ngọc Duyb782bba2013-04-19 09:08:46 +1000507 if (outsz_p)
508 *outsz_p = outpos - out;
Junio C Hamanob45974a2006-12-23 23:36:55 -0800509 break;
510 }
511 }
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200512 return out;
513}
514
Junio C Hamano32707412016-09-26 18:09:48 -0700515static const char *fallback_encoding(const char *name)
516{
517 /*
518 * Some platforms do not have the variously spelled variants of
519 * UTF-8, so let's fall back to trying the most official
520 * spelling. We do so only as a fallback in case the platform
521 * does understand the user's spelling, but not our official
522 * one.
523 */
524 if (is_encoding_utf8(name))
525 return "UTF-8";
526
Junio C Hamanodf375582016-09-26 18:09:48 -0700527 /*
528 * Even though latin-1 is still seen in e-mail
529 * headers, some platforms only install ISO-8859-1.
530 */
531 if (!strcasecmp(name, "latin-1"))
532 return "ISO-8859-1";
533
Junio C Hamano32707412016-09-26 18:09:48 -0700534 return name;
535}
536
Jeff Kingc7d017d2018-07-24 06:50:33 -0400537char *reencode_string_len(const char *in, size_t insz,
Nguyễn Thái Ngọc Duyb782bba2013-04-19 09:08:46 +1000538 const char *out_encoding, const char *in_encoding,
Jeff Kingc7d017d2018-07-24 06:50:33 -0400539 size_t *outsz)
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200540{
541 iconv_t conv;
542 char *out;
543
544 if (!in_encoding)
545 return NULL;
Jeff King5c680be2013-02-25 15:31:00 -0500546
Torsten Bögershausen76759c72012-07-08 15:50:25 +0200547 conv = iconv_open(out_encoding, in_encoding);
Jeff King5c680be2013-02-25 15:31:00 -0500548 if (conv == (iconv_t) -1) {
Junio C Hamano32707412016-09-26 18:09:48 -0700549 in_encoding = fallback_encoding(in_encoding);
550 out_encoding = fallback_encoding(out_encoding);
551
Jeff King5c680be2013-02-25 15:31:00 -0500552 conv = iconv_open(out_encoding, in_encoding);
553 if (conv == (iconv_t) -1)
554 return NULL;
555 }
556
Nguyễn Thái Ngọc Duyb782bba2013-04-19 09:08:46 +1000557 out = reencode_string_iconv(in, insz, conv, outsz);
Junio C Hamanob45974a2006-12-23 23:36:55 -0800558 iconv_close(conv);
559 return out;
560}
561#endif
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400562
Lars Schneider10ecb822018-04-15 20:16:05 +0200563static int has_bom_prefix(const char *data, size_t len,
564 const char *bom, size_t bom_len)
565{
566 return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len);
567}
568
Beat Bolli2b647a02018-07-09 21:25:37 +0200569static const char utf16_be_bom[] = {'\xFE', '\xFF'};
570static const char utf16_le_bom[] = {'\xFF', '\xFE'};
571static const char utf32_be_bom[] = {'\0', '\0', '\xFE', '\xFF'};
572static const char utf32_le_bom[] = {'\xFF', '\xFE', '\0', '\0'};
Lars Schneider10ecb822018-04-15 20:16:05 +0200573
574int has_prohibited_utf_bom(const char *enc, const char *data, size_t len)
575{
576 return (
577 (same_utf_encoding("UTF-16BE", enc) ||
578 same_utf_encoding("UTF-16LE", enc)) &&
579 (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
580 has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
581 ) || (
582 (same_utf_encoding("UTF-32BE", enc) ||
583 same_utf_encoding("UTF-32LE", enc)) &&
584 (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
585 has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
586 );
587}
588
Lars Schneiderc6e48652018-04-15 20:16:06 +0200589int is_missing_required_utf_bom(const char *enc, const char *data, size_t len)
590{
591 return (
592 (same_utf_encoding(enc, "UTF-16")) &&
593 !(has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
594 has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
595 ) || (
596 (same_utf_encoding(enc, "UTF-32")) &&
597 !(has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
598 has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
599 );
600}
601
Kirill Smelkov6cd3c052013-03-07 14:55:07 +0400602/*
603 * Returns first character length in bytes for multi-byte `text` according to
604 * `encoding`.
605 *
606 * - The `text` pointer is updated to point at the next character.
607 * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes
608 * we can consume from text, and on exit `*remainder_p` is reduced by returned
609 * character length. Otherwise `text` is treated as limited by NUL.
610 */
611int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding)
612{
613 int chrlen;
614 const char *p = *text;
615 size_t r = (remainder_p ? *remainder_p : SIZE_MAX);
616
617 if (r < 1)
618 return 0;
619
620 if (is_encoding_utf8(encoding)) {
621 pick_one_utf8_char(&p, &r);
622
623 chrlen = p ? (p - *text)
624 : 1 /* not valid UTF-8 -> raw byte sequence */;
625 }
626 else {
627 /*
628 * TODO use iconv to decode one char and obtain its chrlen
629 * for now, let's treat encodings != UTF-8 as one-byte
630 */
631 chrlen = 1;
632 }
633
634 *text += chrlen;
635 if (remainder_p)
636 *remainder_p -= chrlen;
637
638 return chrlen;
639}
Jeff King6162a1d2014-12-15 17:56:59 -0500640
641/*
Jeff King6aaf9562014-12-23 03:45:36 -0500642 * Pick the next char from the stream, ignoring codepoints an HFS+ would.
643 * Note that this is _not_ complete by any means. It's just enough
Jeff King6162a1d2014-12-15 17:56:59 -0500644 * to make is_hfs_dotgit() work, and should not be used otherwise.
645 */
646static ucs_char_t next_hfs_char(const char **in)
647{
648 while (1) {
649 ucs_char_t out = pick_one_utf8_char(in, NULL);
650 /*
651 * check for malformed utf8. Technically this
652 * gets converted to a percent-sequence, but
653 * returning 0 is good enough for is_hfs_dotgit
654 * to realize it cannot be .git
655 */
656 if (!*in)
657 return 0;
658
659 /* these code points are ignored completely */
660 switch (out) {
661 case 0x200c: /* ZERO WIDTH NON-JOINER */
662 case 0x200d: /* ZERO WIDTH JOINER */
663 case 0x200e: /* LEFT-TO-RIGHT MARK */
664 case 0x200f: /* RIGHT-TO-LEFT MARK */
665 case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
666 case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
667 case 0x202c: /* POP DIRECTIONAL FORMATTING */
668 case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
669 case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
670 case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
671 case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
672 case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
673 case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
674 case 0x206e: /* NATIONAL DIGIT SHAPES */
675 case 0x206f: /* NOMINAL DIGIT SHAPES */
676 case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
677 continue;
678 }
679
Jeff King6aaf9562014-12-23 03:45:36 -0500680 return out;
Jeff King6162a1d2014-12-15 17:56:59 -0500681 }
682}
683
Jeff King0fc333b2018-05-02 15:23:45 -0400684static int is_hfs_dot_generic(const char *path,
685 const char *needle, size_t needle_len)
Jeff King6162a1d2014-12-15 17:56:59 -0500686{
687 ucs_char_t c;
688
Jeff King6aaf9562014-12-23 03:45:36 -0500689 c = next_hfs_char(&path);
690 if (c != '.')
691 return 0;
Jeff King6aaf9562014-12-23 03:45:36 -0500692
693 /*
694 * there's a great deal of other case-folding that occurs
Jeff King0fc333b2018-05-02 15:23:45 -0400695 * in HFS+, but this is enough to catch our fairly vanilla
696 * hard-coded needles.
Jeff King6aaf9562014-12-23 03:45:36 -0500697 */
Jeff King0fc333b2018-05-02 15:23:45 -0400698 for (; needle_len > 0; needle++, needle_len--) {
699 c = next_hfs_char(&path);
700
701 /*
702 * We know our needles contain only ASCII, so we clamp here to
703 * make the results of tolower() sane.
704 */
705 if (c > 127)
706 return 0;
707 if (tolower(c) != *needle)
708 return 0;
709 }
710
Jeff King6162a1d2014-12-15 17:56:59 -0500711 c = next_hfs_char(&path);
712 if (c && !is_dir_sep(c))
713 return 0;
714
715 return 1;
716}
Junio C Hamanodde843e2015-04-16 10:45:29 -0700717
Jeff King0fc333b2018-05-02 15:23:45 -0400718/*
719 * Inline wrapper to make sure the compiler resolves strlen() on literals at
720 * compile time.
721 */
722static inline int is_hfs_dot_str(const char *path, const char *needle)
723{
724 return is_hfs_dot_generic(path, needle, strlen(needle));
725}
726
727int is_hfs_dotgit(const char *path)
728{
729 return is_hfs_dot_str(path, "git");
730}
731
732int is_hfs_dotgitmodules(const char *path)
733{
734 return is_hfs_dot_str(path, "gitmodules");
735}
736
737int is_hfs_dotgitignore(const char *path)
738{
739 return is_hfs_dot_str(path, "gitignore");
740}
741
742int is_hfs_dotgitattributes(const char *path)
743{
744 return is_hfs_dot_str(path, "gitattributes");
745}
746
Junio C Hamanodde843e2015-04-16 10:45:29 -0700747const char utf8_bom[] = "\357\273\277";
748
749int skip_utf8_bom(char **text, size_t len)
750{
751 if (len < strlen(utf8_bom) ||
752 memcmp(*text, utf8_bom, strlen(utf8_bom)))
753 return 0;
754 *text += strlen(utf8_bom);
755 return 1;
756}
Karthik Nayak110dcda2015-09-10 21:18:19 +0530757
758void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
759 const char *s)
760{
761 int slen = strlen(s);
762 int display_len = utf8_strnwidth(s, slen, 0);
763 int utf8_compensation = slen - display_len;
764
765 if (display_len >= width) {
766 strbuf_addstr(buf, s);
767 return;
768 }
769
770 if (position == ALIGN_LEFT)
771 strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
772 else if (position == ALIGN_MIDDLE) {
773 int left = (width - display_len) / 2;
774 strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
775 } else if (position == ALIGN_RIGHT)
776 strbuf_addf(buf, "%*s", width + utf8_compensation, s);
777}