4 * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
29 static const wchar_t utf8_force_wide
[] = {
195 RB_ENTRY(utf8_item
) index_entry
;
198 RB_ENTRY(utf8_item
) data_entry
;
199 char data
[UTF8_SIZE
];
204 utf8_data_cmp(struct utf8_item
*ui1
, struct utf8_item
*ui2
)
206 if (ui1
->size
< ui2
->size
)
208 if (ui1
->size
> ui2
->size
)
210 return (memcmp(ui1
->data
, ui2
->data
, ui1
->size
));
212 RB_HEAD(utf8_data_tree
, utf8_item
);
213 RB_GENERATE_STATIC(utf8_data_tree
, utf8_item
, data_entry
, utf8_data_cmp
);
214 static struct utf8_data_tree utf8_data_tree
= RB_INITIALIZER(utf8_data_tree
);
217 utf8_index_cmp(struct utf8_item
*ui1
, struct utf8_item
*ui2
)
219 if (ui1
->index
< ui2
->index
)
221 if (ui1
->index
> ui2
->index
)
225 RB_HEAD(utf8_index_tree
, utf8_item
);
226 RB_GENERATE_STATIC(utf8_index_tree
, utf8_item
, index_entry
, utf8_index_cmp
);
227 static struct utf8_index_tree utf8_index_tree
= RB_INITIALIZER(utf8_index_tree
);
229 static u_int utf8_next_index
;
231 #define UTF8_GET_SIZE(uc) (((uc) >> 24) & 0x1f)
232 #define UTF8_GET_WIDTH(uc) (((uc) >> 29) - 1)
234 #define UTF8_SET_SIZE(size) (((utf8_char)(size)) << 24)
235 #define UTF8_SET_WIDTH(width) ((((utf8_char)(width)) + 1) << 29)
237 /* Get a UTF-8 item from data. */
238 static struct utf8_item
*
239 utf8_item_by_data(const u_char
*data
, size_t size
)
243 memcpy(ui
.data
, data
, size
);
246 return (RB_FIND(utf8_data_tree
, &utf8_data_tree
, &ui
));
249 /* Get a UTF-8 item from data. */
250 static struct utf8_item
*
251 utf8_item_by_index(u_int index
)
257 return (RB_FIND(utf8_index_tree
, &utf8_index_tree
, &ui
));
260 /* Add a UTF-8 item. */
262 utf8_put_item(const u_char
*data
, size_t size
, u_int
*index
)
264 struct utf8_item
*ui
;
266 ui
= utf8_item_by_data(data
, size
);
269 log_debug("%s: found %.*s = %u", __func__
, (int)size
, data
,
274 if (utf8_next_index
== 0xffffff + 1)
277 ui
= xcalloc(1, sizeof *ui
);
278 ui
->index
= utf8_next_index
++;
279 RB_INSERT(utf8_index_tree
, &utf8_index_tree
, ui
);
281 memcpy(ui
->data
, data
, size
);
283 RB_INSERT(utf8_data_tree
, &utf8_data_tree
, ui
);
286 log_debug("%s: added %.*s = %u", __func__
, (int)size
, data
, *index
);
291 utf8_table_cmp(const void *vp1
, const void *vp2
)
293 const wchar_t *wc1
= vp1
, *wc2
= vp2
;
302 /* Check if character in table. */
304 utf8_in_table(wchar_t find
, const wchar_t *table
, u_int count
)
308 found
= bsearch(&find
, table
, count
, sizeof *table
, utf8_table_cmp
);
309 return (found
!= NULL
);
312 /* Get UTF-8 character from data. */
314 utf8_from_data(const struct utf8_data
*ud
, utf8_char
*uc
)
319 fatalx("invalid UTF-8 width: %u", ud
->width
);
321 if (ud
->size
> UTF8_SIZE
)
324 index
= (((utf8_char
)ud
->data
[2] << 16)|
325 ((utf8_char
)ud
->data
[1] << 8)|
326 ((utf8_char
)ud
->data
[0]));
327 } else if (utf8_put_item(ud
->data
, ud
->size
, &index
) != 0)
329 *uc
= UTF8_SET_SIZE(ud
->size
)|UTF8_SET_WIDTH(ud
->width
)|index
;
330 log_debug("%s: (%d %d %.*s) -> %08x", __func__
, ud
->width
, ud
->size
,
331 (int)ud
->size
, ud
->data
, *uc
);
336 *uc
= UTF8_SET_SIZE(0)|UTF8_SET_WIDTH(0);
337 else if (ud
->width
== 1)
338 *uc
= UTF8_SET_SIZE(1)|UTF8_SET_WIDTH(1)|0x20;
340 *uc
= UTF8_SET_SIZE(1)|UTF8_SET_WIDTH(1)|0x2020;
344 /* Get UTF-8 data from character. */
346 utf8_to_data(utf8_char uc
, struct utf8_data
*ud
)
348 struct utf8_item
*ui
;
351 memset(ud
, 0, sizeof *ud
);
352 ud
->size
= ud
->have
= UTF8_GET_SIZE(uc
);
353 ud
->width
= UTF8_GET_WIDTH(uc
);
356 ud
->data
[2] = (uc
>> 16);
357 ud
->data
[1] = ((uc
>> 8) & 0xff);
358 ud
->data
[0] = (uc
& 0xff);
360 index
= (uc
& 0xffffff);
361 if ((ui
= utf8_item_by_index(index
)) == NULL
)
362 memset(ud
->data
, ' ', ud
->size
);
364 memcpy(ud
->data
, ui
->data
, ud
->size
);
367 log_debug("%s: %08x -> (%d %d %.*s)", __func__
, uc
, ud
->width
, ud
->size
,
368 (int)ud
->size
, ud
->data
);
371 /* Get UTF-8 character from a single ASCII character. */
373 utf8_build_one(u_char ch
)
375 return (UTF8_SET_SIZE(1)|UTF8_SET_WIDTH(1)|ch
);
378 /* Set a single character. */
380 utf8_set(struct utf8_data
*ud
, u_char ch
)
382 static const struct utf8_data empty
= { { 0 }, 1, 1, 1 };
384 memcpy(ud
, &empty
, sizeof *ud
);
388 /* Copy UTF-8 character. */
390 utf8_copy(struct utf8_data
*to
, const struct utf8_data
*from
)
394 memcpy(to
, from
, sizeof *to
);
396 for (i
= to
->size
; i
< sizeof to
->data
; i
++)
400 /* Get width of Unicode character. */
401 static enum utf8_state
402 utf8_width(struct utf8_data
*ud
, int *width
)
406 if (utf8_towc(ud
, &wc
) != UTF8_DONE
)
408 if (utf8_in_table(wc
, utf8_force_wide
, nitems(utf8_force_wide
))) {
413 *width
= wcwidth(wc
);
414 log_debug("wcwidth(%05X) returned %d", (u_int
)wc
, *width
);
417 * C1 control characters are nonprintable, so they are always
420 *width
= (wc
>= 0x80 && wc
<= 0x9f) ? 0 : 1;
422 if (*width
>= 0 && *width
<= 0xff)
427 /* Convert UTF-8 character to wide character. */
429 utf8_towc(const struct utf8_data
*ud
, wchar_t *wc
)
431 switch (mbtowc(wc
, ud
->data
, ud
->size
)) {
433 log_debug("UTF-8 %.*s, mbtowc() %d", (int)ud
->size
, ud
->data
,
435 mbtowc(NULL
, NULL
, MB_CUR_MAX
);
440 log_debug("UTF-8 %.*s is %05X", (int)ud
->size
, ud
->data
, (u_int
)*wc
);
444 /* Convert wide character to UTF-8 character. */
446 utf8_fromwc(wchar_t wc
, struct utf8_data
*ud
)
450 size
= wctomb(ud
->data
, wc
);
452 log_debug("UTF-8 %d, wctomb() %d", wc
, errno
);
458 ud
->size
= ud
->have
= size
;
459 if (utf8_width(ud
, &width
) == UTF8_DONE
) {
467 * Open UTF-8 sequence.
469 * 11000010-11011111 C2-DF start of 2-byte sequence
470 * 11100000-11101111 E0-EF start of 3-byte sequence
471 * 11110000-11110100 F0-F4 start of 4-byte sequence
474 utf8_open(struct utf8_data
*ud
, u_char ch
)
476 memset(ud
, 0, sizeof *ud
);
477 if (ch
>= 0xc2 && ch
<= 0xdf)
479 else if (ch
>= 0xe0 && ch
<= 0xef)
481 else if (ch
>= 0xf0 && ch
<= 0xf4)
489 /* Append character to UTF-8, closing if finished. */
491 utf8_append(struct utf8_data
*ud
, u_char ch
)
495 if (ud
->have
>= ud
->size
)
496 fatalx("UTF-8 character overflow");
497 if (ud
->size
> sizeof ud
->data
)
498 fatalx("UTF-8 character size too large");
500 if (ud
->have
!= 0 && (ch
& 0xc0) != 0x80)
503 ud
->data
[ud
->have
++] = ch
;
504 if (ud
->have
!= ud
->size
)
507 if (ud
->width
== 0xff)
509 if (utf8_width(ud
, &width
) != UTF8_DONE
)
517 * Encode len characters from src into dst, which is guaranteed to have four
518 * bytes available for each character from src (for \abc or UTF-8) plus space
522 utf8_strvis(char *dst
, const char *src
, size_t len
, int flag
)
525 const char *start
= dst
, *end
= src
+ len
;
526 enum utf8_state more
;
530 if ((more
= utf8_open(&ud
, *src
)) == UTF8_MORE
) {
531 while (++src
< end
&& more
== UTF8_MORE
)
532 more
= utf8_append(&ud
, *src
);
533 if (more
== UTF8_DONE
) {
534 /* UTF-8 character finished. */
535 for (i
= 0; i
< ud
.size
; i
++)
539 /* Not a complete, valid UTF-8 character. */
542 if ((flag
& VIS_DQ
) && src
[0] == '$' && src
< end
- 1) {
543 if (isalpha((u_char
)src
[1]) ||
548 } else if (src
< end
- 1)
549 dst
= vis(dst
, src
[0], flag
, src
[1]);
551 dst
= vis(dst
, src
[0], flag
, '\0');
555 return (dst
- start
);
558 /* Same as utf8_strvis but allocate the buffer. */
560 utf8_stravis(char **dst
, const char *src
, int flag
)
565 buf
= xreallocarray(NULL
, 4, strlen(src
) + 1);
566 len
= utf8_strvis(buf
, src
, strlen(src
), flag
);
568 *dst
= xrealloc(buf
, len
+ 1);
572 /* Same as utf8_strvis but allocate the buffer. */
574 utf8_stravisx(char **dst
, const char *src
, size_t srclen
, int flag
)
579 buf
= xreallocarray(NULL
, 4, srclen
+ 1);
580 len
= utf8_strvis(buf
, src
, srclen
, flag
);
582 *dst
= xrealloc(buf
, len
+ 1);
586 /* Does this string contain anything that isn't valid UTF-8? */
588 utf8_isvalid(const char *s
)
592 enum utf8_state more
;
596 if ((more
= utf8_open(&ud
, *s
)) == UTF8_MORE
) {
597 while (++s
< end
&& more
== UTF8_MORE
)
598 more
= utf8_append(&ud
, *s
);
599 if (more
== UTF8_DONE
)
603 if (*s
< 0x20 || *s
> 0x7e)
611 * Sanitize a string, changing any UTF-8 characters to '_'. Caller should free
612 * the returned string. Anything not valid printable ASCII or UTF-8 is
616 utf8_sanitize(const char *src
)
620 enum utf8_state more
;
624 while (*src
!= '\0') {
625 dst
= xreallocarray(dst
, n
+ 1, sizeof *dst
);
626 if ((more
= utf8_open(&ud
, *src
)) == UTF8_MORE
) {
627 while (*++src
!= '\0' && more
== UTF8_MORE
)
628 more
= utf8_append(&ud
, *src
);
629 if (more
== UTF8_DONE
) {
630 dst
= xreallocarray(dst
, n
+ ud
.width
,
632 for (i
= 0; i
< ud
.width
; i
++)
638 if (*src
> 0x1f && *src
< 0x7f)
644 dst
= xreallocarray(dst
, n
+ 1, sizeof *dst
);
649 /* Get UTF-8 buffer length. */
651 utf8_strlen(const struct utf8_data
*s
)
655 for (i
= 0; s
[i
].size
!= 0; i
++)
660 /* Get UTF-8 string width. */
662 utf8_strwidth(const struct utf8_data
*s
, ssize_t n
)
667 for (i
= 0; s
[i
].size
!= 0; i
++) {
668 if (n
!= -1 && n
== i
)
676 * Convert a string into a buffer of UTF-8 characters. Terminated by size == 0.
680 utf8_fromcstr(const char *src
)
682 struct utf8_data
*dst
= NULL
;
684 enum utf8_state more
;
686 while (*src
!= '\0') {
687 dst
= xreallocarray(dst
, n
+ 1, sizeof *dst
);
688 if ((more
= utf8_open(&dst
[n
], *src
)) == UTF8_MORE
) {
689 while (*++src
!= '\0' && more
== UTF8_MORE
)
690 more
= utf8_append(&dst
[n
], *src
);
691 if (more
== UTF8_DONE
) {
697 utf8_set(&dst
[n
], *src
);
701 dst
= xreallocarray(dst
, n
+ 1, sizeof *dst
);
706 /* Convert from a buffer of UTF-8 characters into a string. Caller frees. */
708 utf8_tocstr(struct utf8_data
*src
)
713 for(; src
->size
!= 0; src
++) {
714 dst
= xreallocarray(dst
, n
+ src
->size
, 1);
715 memcpy(dst
+ n
, src
->data
, src
->size
);
718 dst
= xreallocarray(dst
, n
+ 1, 1);
723 /* Get width of UTF-8 string. */
725 utf8_cstrwidth(const char *s
)
727 struct utf8_data tmp
;
729 enum utf8_state more
;
733 if ((more
= utf8_open(&tmp
, *s
)) == UTF8_MORE
) {
734 while (*++s
!= '\0' && more
== UTF8_MORE
)
735 more
= utf8_append(&tmp
, *s
);
736 if (more
== UTF8_DONE
) {
742 if (*s
> 0x1f && *s
!= 0x7f)
749 /* Pad UTF-8 string to width on the left. Caller frees. */
751 utf8_padcstr(const char *s
, u_int width
)
757 n
= utf8_cstrwidth(s
);
762 out
= xmalloc(slen
+ 1 + (width
- n
));
763 memcpy(out
, s
, slen
);
764 for (i
= n
; i
< width
; i
++)
770 /* Pad UTF-8 string to width on the right. Caller frees. */
772 utf8_rpadcstr(const char *s
, u_int width
)
778 n
= utf8_cstrwidth(s
);
783 out
= xmalloc(slen
+ 1 + (width
- n
));
784 for (i
= 0; i
< width
- n
; i
++)
786 memcpy(out
+ i
, s
, slen
);
787 out
[i
+ slen
] = '\0';
792 utf8_cstrhas(const char *s
, const struct utf8_data
*ud
)
794 struct utf8_data
*copy
, *loop
;
797 copy
= utf8_fromcstr(s
);
798 for (loop
= copy
; loop
->size
!= 0; loop
++) {
799 if (loop
->size
!= ud
->size
)
801 if (memcmp(loop
->data
, ud
->data
, loop
->size
) == 0) {