X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=utf8.c;fp=utf8.c;h=35faa6cc4bd52ba2fca0354bb6436900093d44b1;hp=c25a84ba2a3a7a6e7f5d65d4214a432895685348;hb=90c36546d834608e1ce96f4142ef9a1f6b405a7c;hpb=0d52617ab7fc6b2d78b5835ad5230edaf5aafa4f diff --git a/utf8.c b/utf8.c index c25a84b..35faa6c 100644 --- a/utf8.c +++ b/utf8.c @@ -19,60 +19,30 @@ #define EILSEQ EINVAL #endif -int mutt_wctoutf8 (char *s, unsigned int c) +int mutt_wctoutf8(char *p, unsigned int c) { - if (c < (1 << 7)) { - if (s) - *s++ = c; - return 1; - } - else if (c < (1 << 11)) { - if (s) { - *s++ = 0xc0 | (c >> 6); - *s++ = 0x80 | (c & 0x3f); - } - return 2; - } - else if (c < (1 << 16)) { - if (s) { - *s++ = 0xe0 | (c >> 12); - *s++ = 0x80 | ((c >> 6) & 0x3f); - *s++ = 0x80 | (c & 0x3f); - } - return 3; - } - else if (c < (1 << 21)) { - if (s) { - *s++ = 0xf0 | (c >> 18); - *s++ = 0x80 | ((c >> 12) & 0x3f); - *s++ = 0x80 | ((c >> 6) & 0x3f); - *s++ = 0x80 | (c & 0x3f); - } - return 4; - } - else if (c < (1 << 26)) { - if (s) { - *s++ = 0xf8 | (c >> 24); - *s++ = 0x80 | ((c >> 18) & 0x3f); - *s++ = 0x80 | ((c >> 12) & 0x3f); - *s++ = 0x80 | ((c >> 6) & 0x3f); - *s++ = 0x80 | (c & 0x3f); + static unsigned char const mark[7] = { + 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC + }; + + int bytes; + + if (c >= 0x200000) { + errno = EILSEQ; + return -1; } - return 5; - } - else if (c < (1 << 31)) { - if (s) { - *s++ = 0xfc | (c >> 30); - *s++ = 0x80 | ((c >> 24) & 0x3f); - *s++ = 0x80 | ((c >> 18) & 0x3f); - *s++ = 0x80 | ((c >> 12) & 0x3f); - *s++ = 0x80 | ((c >> 6) & 0x3f); - *s++ = 0x80 | (c & 0x3f); + + bytes = 1 + (c >= 0x80) + (c >= 0x800) + (c >= 0x10000); + p += bytes; + + switch (bytes) { + case 4: *--p = (c | 0x80) & 0xbf; c >>= 6; + case 3: *--p = (c | 0x80) & 0xbf; c >>= 6; + case 2: *--p = (c | 0x80) & 0xbf; c >>= 6; + case 1: *--p = c | mark[bytes]; } - return 6; - } - errno = EILSEQ; - return -1; + + return bytes; } #endif /* !HAVE_WC_FUNCS */