Skip to content

Commit aefdd27

Browse files
committed
tutf8e: Build/interface fixup for portability
Signed-off-by: Nigel Stewart <[email protected]>
1 parent 53acedc commit aefdd27

File tree

3 files changed

+44
-40
lines changed

3 files changed

+44
-40
lines changed

lib/tutf8e/codegen.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212
src.write('''
1313
#include <tutf8e.h>
1414
15-
#include <sys/errno.h>
16-
1715
/* Determine the input length and UTF8 encoded length of NUL-terminated input string */
18-
/* return ENOENT if input character is not convertable */
19-
/* return 0 for success */
16+
/* return TUTF8E_INVALID if input character is not convertable */
17+
/* return TUTF8E_OK for success */
2018
2119
int tutf8e_string_length(const uint16_t *table, const char *input, size_t *ilen, size_t *olen)
2220
{
@@ -34,14 +32,14 @@
3432
*olen += 3;
3533
continue;
3634
}
37-
return ENOENT;
35+
return TUTF8E_INVALID;
3836
}
39-
return 0;
37+
return TUTF8E_OK;
4038
}
4139
4240
/* Determine the length of the UTF8 encoding of given input string and table */
43-
/* return ENOENT if input character is not convertable */
44-
/* return 0 for success */
41+
/* return TUTF8E_INVALID if input character is not convertable */
42+
/* return TUTF8E_OK for success */
4543
4644
int tutf8e_buffer_length(const uint16_t *table, const char *input, size_t ilen, size_t *length)
4745
{
@@ -59,16 +57,16 @@
5957
*length += 3;
6058
continue;
6159
}
62-
return ENOENT;
60+
return TUTF8E_INVALID;
6361
}
64-
return 0;
62+
return TUTF8E_OK;
6563
}
6664
67-
/* UTF8 encode the given input string and table */
68-
/* olen input is output buffer size, output is encoded length */
69-
/* return E2BIG if output buffer insuficient */
70-
/* return ENOENT if input character is not convertable */
71-
/* return 0 for success */
65+
/* UTF8 encode the given input string and table */
66+
/* olen input is output buffer size, output is encoded length */
67+
/* return TUTF8E_TOOLONG if output buffer insuficient */
68+
/* return TUTF8E_INVALID if input character is not convertable */
69+
/* return TUTF8E_OK for success */
7270
7371
int tutf8e_buffer_encode(const uint16_t *table, const char *input, size_t ilen, char *output, size_t *olen)
7472
{
@@ -77,30 +75,30 @@
7775
for (const unsigned char *i = (const unsigned char *) input; ilen; ++i, --ilen) {
7876
const uint16_t c = table[*i];
7977
if (c<0x80) {
80-
if (left<1) return E2BIG;
78+
if (left<1) return TUTF8E_TOOLONG;
8179
*(o++) = c;
8280
left -= 1;
8381
continue;
8482
}
8583
if (c<0x800) {
86-
if (left<2) return E2BIG;
84+
if (left<2) return TUTF8E_TOOLONG;
8785
*(o++) = 0xc0 | (c>>6);
8886
*(o++) = 0x80 | (c&0x3f);
8987
left -= 2;
9088
continue;
9189
}
9290
if (c<0xffff) {
93-
if (left<3) return E2BIG;
91+
if (left<3) return TUTF8E_TOOLONG;
9492
*(o++) = 0xe0 | (c>>12);
9593
*(o++) = 0x80 | ((c>>6)&0x3f);
9694
*(o++) = 0x80 | (c&0x3f);
9795
left -= 3;
9896
continue;
9997
}
100-
return ENOENT;
98+
return TUTF8E_INVALID;
10199
}
102100
*olen -= left;
103-
return 0;
101+
return TUTF8E_OK;
104102
}
105103
''')
106104

@@ -119,6 +117,10 @@
119117
extern int tutf8e_buffer_encode(const uint16_t *table, const char *i, size_t ilen, char *output, size_t *olen);
120118
121119
/* External API */
120+
121+
#define TUTF8E_OK 0 /* Success */
122+
#define TUTF8E_INVALID 1 /* Invalid input character */
123+
#define TUTF8E_TOOLONG 2 /* Insufficient output buffer */
122124
''')
123125

124126
include.write('\n/* Encode NUL-terminated string to UTF8 */\n')

lib/tutf8e/include/tutf8e.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ extern int tutf8e_buffer_encode(const uint16_t *table, const char *i, size_t ile
1212

1313
/* External API */
1414

15+
#define TUTF8E_OK 0 /* Success */
16+
#define TUTF8E_INVALID 1 /* Invalid input character */
17+
#define TUTF8E_TOOLONG 2 /* Insufficient output buffer */
18+
1519
/* Encode NUL-terminated string to UTF8 */
1620
extern int tutf8e_string_encode_iso_8859_1 (char *output, size_t olen, const char *input);
1721
extern int tutf8e_string_encode_iso_8859_10 (char *output, size_t olen, const char *input);

lib/tutf8e/src/tutf8e.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
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

108
int 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

3533
int 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

6260
int 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

Comments
 (0)