Skip to content

Commit 7ef5585

Browse files
committed
fix wrc characters encoding conversion functions
this must return maximal length of converted string if destination buffer is NULL
1 parent 16e9b8b commit 7ef5585

File tree

1 file changed

+41
-63
lines changed

1 file changed

+41
-63
lines changed

bld/rc/rc/c/unitable.c

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -128,44 +128,38 @@ static size_t UTF8StringToMultiByte( size_t len, const char *str, char *buf )
128128
*/
129129
{
130130
size_t ret;
131-
size_t outlen;
132131
unsigned short u;
133132
size_t i;
134133
cvt_chr x;
135134
cvt_chr *p;
136135

137136
ret = 0;
138-
if( len > 0 ) {
139-
if( buf == NULL ) {
140-
outlen = 0;
141-
} else {
142-
outlen = len;
143-
}
144-
for( i = 0; i < len; i++ ) {
145-
u = (unsigned char)*str++;
146-
if( IS_ASCII( u ) ) {
147-
if( ret < outlen ) {
148-
*buf++ = (char)u;
149-
ret++;
150-
}
137+
for( i = 0; i < len; i++ ) {
138+
u = (unsigned char)*str++;
139+
if( !IS_ASCII( u ) ) {
140+
i += getcharUTF8( &str, &u );
141+
x.u = u;
142+
p = bsearch( &x, cvt_table, cvt_table_len, sizeof( cvt_chr ), (int(*)(const void*,const void*))compare_utf8 );
143+
if( p == NULL ) {
144+
printf( "unknown unicode character: 0x%4X\n", x.u );
145+
u = '?';
151146
} else {
152-
i += getcharUTF8( &str, &u );
153-
if( ret < outlen ) {
154-
x.u = u;
155-
p = bsearch( &x, cvt_table, cvt_table_len, sizeof( cvt_chr ), (int(*)(const void*,const void*))compare_utf8 );
156-
if( p == NULL ) {
157-
printf( "unknown unicode character: 0x%4X\n", x.u );
158-
*buf++ = '?';
159-
} else {
160-
if( p->s > 0xFF ) {
147+
if( p->s > 0xFF ) {
148+
if( ret < len ) {
149+
if( buf != NULL ) {
161150
*buf++ = (char)( p->s >> 8 );
162-
ret++;
163151
}
164-
*buf++ = (char)p->s;
152+
ret++;
165153
}
166-
ret++;
167154
}
155+
u = (char)p->s;
156+
}
157+
}
158+
if( ret < len ) {
159+
if( buf != NULL ) {
160+
*buf++ = (char)u;
168161
}
162+
ret++;
169163
}
170164
}
171165
return( ret );
@@ -179,38 +173,28 @@ static size_t UTF8StringToCP1252( size_t len, const char *str, char *buf )
179173
*/
180174
{
181175
size_t ret;
182-
size_t outlen;
183176
unsigned short u;
184177
size_t i;
185178
char c;
186179

187180
ret = 0;
188-
if( len > 0 ) {
189-
if( buf == NULL ) {
190-
outlen = 0;
191-
} else {
192-
outlen = len;
193-
}
194-
for( i = 0; i < len; i++ ) {
195-
u = (unsigned char)*str++;
196-
if( !IS_ASCII( u ) ) {
197-
i += getcharUTF8( &str, &u );
181+
for( i = 0; i < len; i++ ) {
182+
u = (unsigned char)*str++;
183+
if( !IS_ASCII( u ) ) {
184+
i += getcharUTF8( &str, &u );
185+
/*
186+
* UNICODE to CP1252
187+
*/
188+
c = (char)unicode_to_latin1( u );
189+
if( c != 0 ) {
190+
u = c;
198191
}
199-
if( ret < outlen ) {
200-
/*
201-
* UNICODE to CP1252
202-
*/
203-
if( u > 0x7F && u < 0xA0 ) { /* 0x80-0x9F */
204-
c = (char)unicode_to_latin1( u );
205-
if( c == 0 ) {
206-
printf( "unknown unicode character: 0x%4X\n", u );
207-
c = '?';
208-
}
209-
u = c;
210-
}
192+
}
193+
if( ret < len ) {
194+
if( buf != NULL ) {
211195
*buf++ = (char)u;
212-
ret++;
213196
}
197+
ret++;
214198
}
215199
}
216200
return( ret );
@@ -224,27 +208,21 @@ static size_t UTF8StringToUnicode( size_t len, const char *str, char *buf )
224208
*/
225209
{
226210
size_t ret;
227-
size_t outlen;
228211
unsigned short u;
229212
size_t i;
230213

231214
ret = 0;
232-
if( len > 0 ) {
233-
if( buf == NULL ) {
234-
outlen = 0;
235-
} else {
236-
outlen = len;
215+
for( i = 0; i < len; i++ ) {
216+
u = (unsigned char)*str++;
217+
if( !IS_ASCII( u ) ) {
218+
i += getcharUTF8( &str, &u );
237219
}
238-
for( i = 0; i < len; i++ ) {
239-
u = (unsigned char)*str++;
240-
if( !IS_ASCII( u ) ) {
241-
i += getcharUTF8( &str, &u );
242-
}
243-
if( ret < outlen ) {
220+
if( ret < len ) {
221+
if( buf != NULL ) {
244222
*buf++ = (char)u;
245223
*buf++ = (char)( u >> 8 );
246-
ret++;
247224
}
225+
ret++;
248226
}
249227
}
250228
return( ret * 2 );

0 commit comments

Comments
 (0)