2015-02-16 00:41:59 -05:00
|
|
|
/* utf8.c -- implements utf8.h, converts between unicode and UTF-8 */
|
|
|
|
|
2019-11-05 22:24:18 -05:00
|
|
|
#define _XOPEN_SOURCE /* wcwidth in wchar.h */
|
|
|
|
|
2012-07-10 19:21:35 -04:00
|
|
|
#include "utf8.h"
|
|
|
|
|
2015-02-08 01:26:07 -05:00
|
|
|
#include <assert.h>
|
2019-11-05 22:24:18 -05:00
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display width of UTF-8 character
|
|
|
|
*/
|
2020-06-24 04:38:03 -04:00
|
|
|
int utf8_width( unicode_t c) {
|
2020-06-16 02:09:44 -04:00
|
|
|
#if CYGWIN
|
2020-06-24 04:38:03 -04:00
|
|
|
assert( sizeof( wchar_t) == 2) ; /* wcwidth only supports UTF-16 */
|
|
|
|
return (c < 0x10000) ? wcwidth( (wchar_t) c) : -1 ;
|
2019-11-06 00:31:05 -05:00
|
|
|
#else
|
2020-06-24 04:38:03 -04:00
|
|
|
return wcwidth( (wchar_t) c) ;
|
2019-11-06 00:31:05 -05:00
|
|
|
#endif
|
2019-11-05 22:24:18 -05:00
|
|
|
}
|
2015-02-08 01:26:07 -05:00
|
|
|
|
2012-07-10 19:21:35 -04:00
|
|
|
/*
|
|
|
|
* utf8_to_unicode()
|
|
|
|
*
|
|
|
|
* Convert a UTF-8 sequence to its unicode value, and return the length of
|
|
|
|
* the sequence in bytes.
|
|
|
|
*
|
|
|
|
* NOTE! Invalid UTF-8 will be converted to a one-byte sequence, so you can
|
|
|
|
* either use it as-is (ie as Latin1) or you can check for invalid UTF-8
|
|
|
|
* by checking for a length of 1 and a result > 127.
|
|
|
|
*
|
|
|
|
* NOTE 2! This does *not* verify things like minimality. So overlong forms
|
|
|
|
* are happily accepted and decoded, as are the various "invalid values".
|
|
|
|
*/
|
2019-08-12 21:14:08 -04:00
|
|
|
unsigned utf8_to_unicode( const char *line, unsigned index, unsigned len,
|
2017-05-16 00:13:12 -04:00
|
|
|
unicode_t *res) {
|
2015-02-10 05:09:59 -05:00
|
|
|
unicode_t value ;
|
2017-05-07 02:05:47 -04:00
|
|
|
unsigned c ;
|
2015-02-16 00:41:59 -05:00
|
|
|
unsigned bytes, mask, i;
|
2012-07-10 19:21:35 -04:00
|
|
|
|
2017-05-07 02:05:47 -04:00
|
|
|
if( index >= len)
|
|
|
|
return 0 ;
|
2017-05-16 00:13:12 -04:00
|
|
|
|
2017-05-07 02:05:47 -04:00
|
|
|
*res = c = line[ index] & 0xFFU ;
|
2012-07-10 19:21:35 -04:00
|
|
|
|
2015-02-10 05:09:59 -05:00
|
|
|
/*
|
|
|
|
* 0xxxxxxx is valid one byte utf8
|
|
|
|
* 10xxxxxx is invalid UTF-8 start byte, we assume it is Latin1
|
|
|
|
* 1100000x is start of overlong encoding sequence
|
|
|
|
* Sequence longer than 4 bytes are invalid
|
|
|
|
* Last valid code is 0x10FFFF, encoding start with 0xF4
|
|
|
|
*/
|
|
|
|
if( c <= 0xC1 || c > 0xF4)
|
|
|
|
return 1;
|
2012-07-10 19:21:35 -04:00
|
|
|
|
2015-02-10 05:09:59 -05:00
|
|
|
/* Ok, it's 11xxxxxx, do a stupid decode */
|
|
|
|
mask = 0x20;
|
|
|
|
bytes = 2;
|
2015-02-16 00:41:59 -05:00
|
|
|
while( (c & mask) != 0) {
|
2015-02-10 05:09:59 -05:00
|
|
|
bytes++;
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
2012-07-10 19:21:35 -04:00
|
|
|
|
2015-02-16 00:41:59 -05:00
|
|
|
/* bytes is in range [2..4] as c was in range [C2..F4] */
|
2015-02-10 05:09:59 -05:00
|
|
|
len -= index;
|
|
|
|
if (bytes > len)
|
|
|
|
return 1;
|
2012-07-10 19:21:35 -04:00
|
|
|
|
2015-02-10 05:09:59 -05:00
|
|
|
value = c & (mask-1);
|
2012-07-10 19:21:35 -04:00
|
|
|
|
2015-02-10 05:09:59 -05:00
|
|
|
/* Ok, do the bytes */
|
|
|
|
line += index;
|
|
|
|
for (i = 1; i < bytes; i++) {
|
2015-02-16 00:41:59 -05:00
|
|
|
c = line[i] & 0xFFU ;
|
2015-02-10 05:09:59 -05:00
|
|
|
if ((c & 0xc0) != 0x80)
|
|
|
|
return 1;
|
|
|
|
value = (value << 6) | (c & 0x3f);
|
|
|
|
}
|
2015-02-03 23:37:57 -05:00
|
|
|
|
2015-02-10 05:09:59 -05:00
|
|
|
if( value > 0x10FFFF) /* Avoid 110000 - 13FFFF */
|
|
|
|
return 1 ;
|
|
|
|
|
|
|
|
*res = value;
|
|
|
|
return bytes;
|
2012-07-10 19:21:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unicode_to_utf8()
|
|
|
|
*
|
|
|
|
* Convert a unicode value to its canonical utf-8 sequence.
|
|
|
|
*
|
|
|
|
* NOTE! This does not check for - or care about - the "invalid" unicode
|
|
|
|
* values. Also, converting a utf-8 sequence to unicode and back does
|
|
|
|
* *not* guarantee the same sequence, since this generates the shortest
|
|
|
|
* possible sequence, while utf8_to_unicode() accepts both Latin1 and
|
|
|
|
* overlong utf-8 sequences.
|
|
|
|
*/
|
2015-02-08 01:26:07 -05:00
|
|
|
unsigned unicode_to_utf8( unicode_t c, char *utf8) {
|
2015-02-16 00:41:59 -05:00
|
|
|
unsigned bytes = 1 ;
|
2012-07-10 19:21:35 -04:00
|
|
|
|
2015-02-10 05:09:59 -05:00
|
|
|
assert( c <= 0x10FFFF) ;
|
2015-02-12 00:15:45 -05:00
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
if( c > 0x10FFFF) /* Let's assume this is due to sign extension */
|
|
|
|
c &= 0xFF ;
|
|
|
|
#endif
|
|
|
|
|
2015-02-16 00:41:59 -05:00
|
|
|
if( c <= 0x7f)
|
|
|
|
*utf8 = (char) c ;
|
|
|
|
else {
|
|
|
|
unsigned prefix = 0x40 ;
|
|
|
|
char *p = utf8 ;
|
2015-02-10 05:09:59 -05:00
|
|
|
do {
|
2015-02-16 00:41:59 -05:00
|
|
|
*p++ = (char) (0x80 + (c & 0x3f)) ;
|
|
|
|
bytes++ ;
|
|
|
|
prefix >>= 1 ;
|
|
|
|
c >>= 6 ;
|
2015-02-10 05:09:59 -05:00
|
|
|
} while( c >= prefix) ;
|
2015-02-16 00:41:59 -05:00
|
|
|
|
2017-05-04 22:49:02 -04:00
|
|
|
*p-- = *utf8 ;
|
|
|
|
*utf8++ = (char) (c - 2 * prefix) ;
|
|
|
|
if( utf8 < p) { /* swap middle two bytes if 4 bytes utf-8 code */
|
|
|
|
char c = *p ;
|
|
|
|
*p = *utf8 ;
|
|
|
|
*utf8 = c ;
|
|
|
|
}
|
2015-02-10 05:09:59 -05:00
|
|
|
}
|
2015-02-16 00:41:59 -05:00
|
|
|
|
|
|
|
return bytes ;
|
2012-07-10 19:21:35 -04:00
|
|
|
}
|
2015-02-16 00:41:59 -05:00
|
|
|
|
2019-08-12 09:41:51 -04:00
|
|
|
unsigned utf8_revdelta( unsigned char *p, unsigned pos) {
|
|
|
|
unsigned delta = 0 ;
|
|
|
|
|
|
|
|
if( (*p & 0xC0) == 0x80) {
|
|
|
|
unsigned char c ;
|
|
|
|
|
|
|
|
c = *--p ;
|
|
|
|
if( (c & 0xE0) == 0xC0) /* valid 2 bytes unicode seq */
|
|
|
|
delta = 1 ;
|
|
|
|
else if( ((c & 0xC0) == 0x80) && (pos > 1)) {
|
|
|
|
c = *--p ;
|
|
|
|
if( (c & 0xF0) == 0xE0) /* valid 3 bytes unicode seq */
|
|
|
|
delta = 2 ;
|
|
|
|
else if( ((c & 0xC0) == 0x80) && (pos > 2))
|
|
|
|
if( (p[ -1] & 0xF8) == 0xF0) /* valid 4 bytes unicode seq */
|
|
|
|
delta = 3 ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return delta ;
|
|
|
|
}
|
|
|
|
|
2015-02-16 00:41:59 -05:00
|
|
|
|
|
|
|
/* end of utf8.c */
|