11
22#include <tutf8e.h>
33
4- #include <sys/errno.h>
5-
64/* Determine the input length and UTF8 encoded length of NUL-terminated input string */
7- /* return ENOENT if input character is not convertable */
8- /* return 0 for success */
5+ /* return TUTF8E_INVALID if input character is not convertable */
6+ /* return TUTF8E_OK for success */
97
108int tutf8e_string_length (const uint16_t * table , const char * input , size_t * ilen , size_t * olen )
119{
@@ -23,14 +21,14 @@ int tutf8e_string_length(const uint16_t *table, const char *input, size_t *ilen,
2321 * olen += 3 ;
2422 continue ;
2523 }
26- return ENOENT ;
24+ return TUTF8E_INVALID ;
2725 }
28- return 0 ;
26+ return TUTF8E_OK ;
2927}
3028
3129/* Determine the length of the UTF8 encoding of given input string and table */
32- /* return ENOENT if input character is not convertable */
33- /* return 0 for success */
30+ /* return TUTF8E_INVALID if input character is not convertable */
31+ /* return TUTF8E_OK for success */
3432
3533int tutf8e_buffer_length (const uint16_t * table , const char * input , size_t ilen , size_t * length )
3634{
@@ -48,16 +46,16 @@ int tutf8e_buffer_length(const uint16_t *table, const char *input, size_t ilen,
4846 * length += 3 ;
4947 continue ;
5048 }
51- return ENOENT ;
49+ return TUTF8E_INVALID ;
5250 }
53- return 0 ;
51+ return TUTF8E_OK ;
5452}
5553
56- /* UTF8 encode the given input string and table */
57- /* olen input is output buffer size, output is encoded length */
58- /* return E2BIG if output buffer insuficient */
59- /* return ENOENT if input character is not convertable */
60- /* return 0 for success */
54+ /* UTF8 encode the given input string and table */
55+ /* olen input is output buffer size, output is encoded length */
56+ /* return TUTF8E_TOOLONG if output buffer insuficient */
57+ /* return TUTF8E_INVALID if input character is not convertable */
58+ /* return TUTF8E_OK for success */
6159
6260int tutf8e_buffer_encode (const uint16_t * table , const char * input , size_t ilen , char * output , size_t * olen )
6361{
@@ -66,28 +64,28 @@ int tutf8e_buffer_encode(const uint16_t *table, const char *input, size_t ilen,
6664 for (const unsigned char * i = (const unsigned char * ) input ; ilen ; ++ i , -- ilen ) {
6765 const uint16_t c = table [* i ];
6866 if (c < 0x80 ) {
69- if (left < 1 ) return E2BIG ;
67+ if (left < 1 ) return TUTF8E_TOOLONG ;
7068 * (o ++ ) = c ;
7169 left -= 1 ;
7270 continue ;
7371 }
7472 if (c < 0x800 ) {
75- if (left < 2 ) return E2BIG ;
73+ if (left < 2 ) return TUTF8E_TOOLONG ;
7674 * (o ++ ) = 0xc0 | (c >>6 );
7775 * (o ++ ) = 0x80 | (c & 0x3f );
7876 left -= 2 ;
7977 continue ;
8078 }
8179 if (c < 0xffff ) {
82- if (left < 3 ) return E2BIG ;
80+ if (left < 3 ) return TUTF8E_TOOLONG ;
8381 * (o ++ ) = 0xe0 | (c >>12 );
8482 * (o ++ ) = 0x80 | ((c >>6 )& 0x3f );
8583 * (o ++ ) = 0x80 | (c & 0x3f );
8684 left -= 3 ;
8785 continue ;
8886 }
89- return ENOENT ;
87+ return TUTF8E_INVALID ;
9088 }
9189 * olen -= left ;
92- return 0 ;
90+ return TUTF8E_OK ;
9391}
0 commit comments