Skip to content

Latest commit

 

History

History
829 lines (797 loc) · 18.1 KB

SDL_iconv.c

File metadata and controls

829 lines (797 loc) · 18.1 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
/* This file contains portable iconv functions for SDL */
#include "SDL_stdinc.h"
#include "SDL_endian.h"
#ifdef HAVE_ICONV
#include <errno.h>
size_t SDL_iconv(SDL_iconv_t cd,
Jun 28, 2007
Jun 28, 2007
34
const char **inbuf, size_t *inbytesleft,
35
36
char **outbuf, size_t *outbytesleft)
{
Jun 28, 2007
Jun 28, 2007
37
38
39
40
41
size_t retCode;
#ifdef ICONV_REALLY_MODIFIES_INBUF
if ( inbuf && *inbuf && inbytesleft ) {
char *tmp = SDL_stack_alloc(char, *inbytesleft);
char *ptr = tmp;
Jun 28, 2007
Jun 28, 2007
42
SDL_memcpy(tmp, inbuf, *inbytesleft);
Jun 28, 2007
Jun 28, 2007
43
44
45
46
47
48
49
50
51
retCode = iconv(cd, &ptr, inbytesleft, outbuf, outbytesleft);
inbuf += (ptr - tmp);
SDL_stack_free(tmp);
} else {
retCode = iconv(cd, NULL, inbytesleft, outbuf, outbytesleft);
}
#else
retCode = iconv(cd, (char **)inbuf, inbytesleft, outbuf, outbytesleft);
#endif
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
if ( retCode == (size_t)-1 ) {
switch(errno) {
case E2BIG:
return SDL_ICONV_E2BIG;
case EILSEQ:
return SDL_ICONV_EILSEQ;
case EINVAL:
return SDL_ICONV_EINVAL;
default:
return SDL_ICONV_ERROR;
}
}
return retCode;
}
#else
Mar 13, 2006
Mar 13, 2006
69
70
71
72
/* Lots of useful information on Unicode at:
http://www.cl.cam.ac.uk/~mgk25/unicode.html
*/
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#define UNICODE_BOM 0xFEFF
#define UNKNOWN_ASCII '?'
#define UNKNOWN_UNICODE 0xFFFD
enum {
ENCODING_UNKNOWN,
ENCODING_ASCII,
ENCODING_LATIN1,
ENCODING_UTF8,
ENCODING_UTF16, /* Needs byte order marker */
ENCODING_UTF16BE,
ENCODING_UTF16LE,
ENCODING_UTF32, /* Needs byte order marker */
ENCODING_UTF32BE,
ENCODING_UTF32LE,
ENCODING_UCS2, /* Native byte order assumed */
ENCODING_UCS4, /* Native byte order assumed */
};
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define ENCODING_UTF16NATIVE ENCODING_UTF16BE
#define ENCODING_UTF32NATIVE ENCODING_UTF32BE
#else
#define ENCODING_UTF16NATIVE ENCODING_UTF16LE
#define ENCODING_UTF32NATIVE ENCODING_UTF32LE
#endif
struct _SDL_iconv_t
{
int src_fmt;
int dst_fmt;
};
static struct {
const char *name;
int format;
} encodings[] = {
{ "ASCII", ENCODING_ASCII },
{ "US-ASCII", ENCODING_ASCII },
{ "LATIN1", ENCODING_LATIN1 },
{ "ISO-8859-1", ENCODING_LATIN1 },
{ "UTF8", ENCODING_UTF8 },
{ "UTF-8", ENCODING_UTF8 },
{ "UTF16", ENCODING_UTF16 },
{ "UTF-16", ENCODING_UTF16 },
{ "UTF16BE", ENCODING_UTF16BE },
{ "UTF-16BE", ENCODING_UTF16BE },
{ "UTF16LE", ENCODING_UTF16LE },
{ "UTF-16LE", ENCODING_UTF16LE },
{ "UTF32", ENCODING_UTF32 },
{ "UTF-32", ENCODING_UTF32 },
{ "UTF32BE", ENCODING_UTF32BE },
{ "UTF-32BE", ENCODING_UTF32BE },
{ "UTF32LE", ENCODING_UTF32LE },
{ "UTF-32LE", ENCODING_UTF32LE },
{ "UCS2", ENCODING_UCS2 },
{ "UCS-2", ENCODING_UCS2 },
{ "UCS4", ENCODING_UCS4 },
{ "UCS-4", ENCODING_UCS4 },
};
SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
{
int src_fmt = ENCODING_UNKNOWN;
int dst_fmt = ENCODING_UNKNOWN;
int i;
for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {
src_fmt = encodings[i].format;
if ( dst_fmt != ENCODING_UNKNOWN ) {
break;
}
}
if ( SDL_strcasecmp(tocode, encodings[i].name) == 0 ) {
dst_fmt = encodings[i].format;
if ( src_fmt != ENCODING_UNKNOWN ) {
break;
}
}
}
if ( src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN ) {
SDL_iconv_t cd = (SDL_iconv_t)SDL_malloc(sizeof(*cd));
if ( cd ) {
cd->src_fmt = src_fmt;
cd->dst_fmt = dst_fmt;
return cd;
}
}
return (SDL_iconv_t)-1;
}
size_t SDL_iconv(SDL_iconv_t cd,
Feb 16, 2007
Feb 16, 2007
166
const char **inbuf, size_t *inbytesleft,
167
168
169
char **outbuf, size_t *outbytesleft)
{
/* For simplicity, we'll convert everything to and from UCS-4 */
Feb 16, 2007
Feb 16, 2007
170
171
const char *src;
char *dst;
172
size_t srclen, dstlen;
May 17, 2006
May 17, 2006
173
Uint32 ch = 0;
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
size_t total;
if ( !inbuf || !*inbuf ) {
/* Reset the context */
return 0;
}
if ( !outbuf || !*outbuf || !outbytesleft || !*outbytesleft ) {
return SDL_ICONV_E2BIG;
}
src = *inbuf;
srclen = (inbytesleft ? *inbytesleft : 0);
dst = *outbuf;
dstlen = *outbytesleft;
switch ( cd->src_fmt ) {
case ENCODING_UTF16:
/* Scan for a byte order marker */
{
Uint8 *p = (Uint8 *)src;
size_t n = srclen / 2;
while ( n ) {
if ( p[0] == 0xFF && p[1] == 0xFE ) {
cd->src_fmt = ENCODING_UTF16BE;
break;
} else if ( p[0] == 0xFE && p[1] == 0xFF ) {
cd->src_fmt = ENCODING_UTF16LE;
break;
}
p += 2;
--n;
}
if ( n == 0 ) {
/* We can't tell, default to host order */
cd->src_fmt = ENCODING_UTF16NATIVE;
}
}
break;
case ENCODING_UTF32:
/* Scan for a byte order marker */
{
Uint8 *p = (Uint8 *)src;
size_t n = srclen / 4;
while ( n ) {
if ( p[0] == 0xFF && p[1] == 0xFE &&
p[2] == 0x00 && p[3] == 0x00 ) {
cd->src_fmt = ENCODING_UTF32BE;
break;
} else if ( p[0] == 0x00 && p[1] == 0x00 &&
p[2] == 0xFE && p[3] == 0xFF ) {
cd->src_fmt = ENCODING_UTF32LE;
break;
}
p += 4;
--n;
}
if ( n == 0 ) {
/* We can't tell, default to host order */
cd->src_fmt = ENCODING_UTF32NATIVE;
}
}
break;
}
switch ( cd->dst_fmt ) {
case ENCODING_UTF16:
/* Default to host order, need to add byte order marker */
if ( dstlen < 2 ) {
return SDL_ICONV_E2BIG;
}
*(Uint16 *)dst = UNICODE_BOM;
dst += 2;
dstlen -= 2;
cd->dst_fmt = ENCODING_UTF16NATIVE;
break;
case ENCODING_UTF32:
/* Default to host order, need to add byte order marker */
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
*(Uint32 *)dst = UNICODE_BOM;
dst += 4;
dstlen -= 4;
cd->dst_fmt = ENCODING_UTF32NATIVE;
break;
}
total = 0;
while ( srclen > 0 ) {
/* Decode a character */
switch ( cd->src_fmt ) {
case ENCODING_ASCII:
{
Uint8 *p = (Uint8 *)src;
ch = (Uint32)(p[0] & 0x7F);
++src;
--srclen;
}
break;
case ENCODING_LATIN1:
{
Uint8 *p = (Uint8 *)src;
ch = (Uint32)p[0];
++src;
--srclen;
}
break;
case ENCODING_UTF8: /* RFC 3629 */
{
Uint8 *p = (Uint8 *)src;
size_t left = 0;
SDL_bool overlong = SDL_FALSE;
if ( p[0] >= 0xFC ) {
if ( (p[0] & 0xFE) != 0xFC ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( p[0] == 0xFC ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x01);
left = 5;
}
} else if ( p[0] >= 0xF8 ) {
if ( (p[0] & 0xFC) != 0xF8 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( p[0] == 0xF8 ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x03);
left = 4;
}
} else if ( p[0] >= 0xF0 ) {
if ( (p[0] & 0xF8) != 0xF0 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( p[0] == 0xF0 ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x07);
left = 3;
}
} else if ( p[0] >= 0xE0 ) {
if ( (p[0] & 0xF0) != 0xE0 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( p[0] == 0xE0 ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x0F);
left = 2;
}
} else if ( p[0] >= 0xC0 ) {
if ( (p[0] & 0xE0) != 0xC0 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
if ( (p[0] & 0xCE) == 0xC0 ) {
overlong = SDL_TRUE;
}
ch = (Uint32)(p[0] & 0x1F);
left = 1;
}
} else {
if ( (p[0] & 0x80) != 0x00 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
} else {
ch = (Uint32)p[0];
}
}
++src;
--srclen;
if ( srclen < left ) {
return SDL_ICONV_EINVAL;
}
while ( left-- ) {
++p;
if ( (p[0] & 0xC0) != 0x80 ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
ch <<= 6;
ch |= (p[0] & 0x3F);
++src;
--srclen;
}
if ( overlong ) {
/* Potential security risk
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
}
if ( (ch >= 0xD800 && ch <= 0xDFFF) ||
Mar 13, 2006
Mar 13, 2006
386
387
(ch == 0xFFFE || ch == 0xFFFF) ||
ch > 0x10FFFF ) {
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
}
}
break;
case ENCODING_UTF16BE: /* RFC 2781 */
{
Uint8 *p = (Uint8 *)src;
Uint16 W1, W2;
if ( srclen < 2 ) {
return SDL_ICONV_EINVAL;
}
Mar 13, 2006
Mar 13, 2006
402
403
W1 = ((Uint16)p[0] << 8) |
(Uint16)p[1];
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
src += 2;
srclen -= 2;
if ( W1 < 0xD800 || W1 > 0xDFFF ) {
ch = (Uint32)W1;
break;
}
if ( W1 > 0xDBFF ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
if ( srclen < 2 ) {
return SDL_ICONV_EINVAL;
}
Mar 13, 2006
Mar 13, 2006
420
p = (Uint8 *)src;
Mar 13, 2006
Mar 13, 2006
421
422
W2 = ((Uint16)p[0] << 8) |
(Uint16)p[1];
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
src += 2;
srclen -= 2;
if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
ch = (((Uint32)(W1 & 0x3FF) << 10) |
(Uint32)(W2 & 0x3FF)) + 0x10000;
}
break;
case ENCODING_UTF16LE: /* RFC 2781 */
{
Uint8 *p = (Uint8 *)src;
Uint16 W1, W2;
if ( srclen < 2 ) {
return SDL_ICONV_EINVAL;
}
Mar 13, 2006
Mar 13, 2006
443
444
W1 = ((Uint16)p[1] << 8) |
(Uint16)p[0];
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
src += 2;
srclen -= 2;
if ( W1 < 0xD800 || W1 > 0xDFFF ) {
ch = (Uint32)W1;
break;
}
if ( W1 > 0xDBFF ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
if ( srclen < 2 ) {
return SDL_ICONV_EINVAL;
}
Mar 13, 2006
Mar 13, 2006
461
p = (Uint8 *)src;
Mar 13, 2006
Mar 13, 2006
462
463
W2 = ((Uint16)p[1] << 8) |
(Uint16)p[0];
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
src += 2;
srclen -= 2;
if ( W2 < 0xDC00 || W2 > 0xDFFF ) {
/* Skip illegal sequences
return SDL_ICONV_EILSEQ;
*/
ch = UNKNOWN_UNICODE;
break;
}
ch = (((Uint32)(W1 & 0x3FF) << 10) |
(Uint32)(W2 & 0x3FF)) + 0x10000;
}
break;
case ENCODING_UTF32BE:
{
Uint8 *p = (Uint8 *)src;
if ( srclen < 4 ) {
return SDL_ICONV_EINVAL;
}
ch = ((Uint32)p[0] << 24) |
((Uint32)p[1] << 16) |
((Uint32)p[2] << 8) |
(Uint32)p[3];
src += 4;
srclen -= 4;
}
break;
case ENCODING_UTF32LE:
{
Uint8 *p = (Uint8 *)src;
if ( srclen < 4 ) {
return SDL_ICONV_EINVAL;
}
ch = ((Uint32)p[3] << 24) |
((Uint32)p[2] << 16) |
((Uint32)p[1] << 8) |
(Uint32)p[0];
src += 4;
srclen -= 4;
}
break;
case ENCODING_UCS2:
{
Uint16 *p = (Uint16 *)src;
if ( srclen < 2 ) {
return SDL_ICONV_EINVAL;
}
ch = *p;
src += 2;
srclen -= 2;
}
break;
case ENCODING_UCS4:
{
Uint32 *p = (Uint32 *)src;
if ( srclen < 4 ) {
return SDL_ICONV_EINVAL;
}
ch = *p;
src += 4;
srclen -= 4;
}
break;
}
/* Encode a character */
switch ( cd->dst_fmt ) {
case ENCODING_ASCII:
{
Uint8 *p = (Uint8 *)dst;
if ( dstlen < 1 ) {
return SDL_ICONV_E2BIG;
}
if ( ch > 0x7F ) {
*p = UNKNOWN_ASCII;
} else {
*p = (Uint8)ch;
}
++dst;
--dstlen;
}
break;
case ENCODING_LATIN1:
{
Uint8 *p = (Uint8 *)dst;
if ( dstlen < 1 ) {
return SDL_ICONV_E2BIG;
}
if ( ch > 0xFF ) {
*p = UNKNOWN_ASCII;
} else {
*p = (Uint8)ch;
}
++dst;
--dstlen;
}
break;
case ENCODING_UTF8: /* RFC 3629 */
{
Uint8 *p = (Uint8 *)dst;
Mar 13, 2006
Mar 13, 2006
564
if ( ch > 0x10FFFF ) {
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
ch = UNKNOWN_UNICODE;
}
if ( ch <= 0x7F ) {
if ( dstlen < 1 ) {
return SDL_ICONV_E2BIG;
}
*p = (Uint8)ch;
++dst;
--dstlen;
} else if ( ch <= 0x7FF ) {
if ( dstlen < 2 ) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xC0 | (Uint8)((ch >> 6) & 0x1F);
p[1] = 0x80 | (Uint8)(ch & 0x3F);
dst += 2;
dstlen -= 2;
} else if ( ch <= 0xFFFF ) {
if ( dstlen < 3 ) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xE0 | (Uint8)((ch >> 12) & 0x0F);
p[1] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[2] = 0x80 | (Uint8)(ch & 0x3F);
dst += 3;
dstlen -= 3;
} else if ( ch <= 0x1FFFFF ) {
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xF0 | (Uint8)((ch >> 18) & 0x07);
p[1] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
p[2] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[3] = 0x80 | (Uint8)(ch & 0x3F);
dst += 4;
dstlen -= 4;
} else if ( ch <= 0x3FFFFFF ) {
if ( dstlen < 5 ) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xF8 | (Uint8)((ch >> 24) & 0x03);
p[1] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
p[2] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
p[3] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[4] = 0x80 | (Uint8)(ch & 0x3F);
dst += 5;
dstlen -= 5;
} else {
if ( dstlen < 6 ) {
return SDL_ICONV_E2BIG;
}
p[0] = 0xFC | (Uint8)((ch >> 30) & 0x01);
p[1] = 0x80 | (Uint8)((ch >> 24) & 0x3F);
p[2] = 0x80 | (Uint8)((ch >> 18) & 0x3F);
p[3] = 0x80 | (Uint8)((ch >> 12) & 0x3F);
p[4] = 0x80 | (Uint8)((ch >> 6) & 0x3F);
p[5] = 0x80 | (Uint8)(ch & 0x3F);
dst += 6;
dstlen -= 6;
}
}
break;
case ENCODING_UTF16BE: /* RFC 2781 */
{
Uint8 *p = (Uint8 *)dst;
if ( ch > 0x10FFFF ) {
ch = UNKNOWN_UNICODE;
}
if ( ch < 0x10000 ) {
if ( dstlen < 2 ) {
return SDL_ICONV_E2BIG;
}
p[0] = (Uint8)(ch >> 8);
p[1] = (Uint8)ch;
dst += 2;
dstlen -= 2;
} else {
Uint16 W1, W2;
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
ch = ch - 0x10000;
W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
p[0] = (Uint8)(W1 >> 8);
p[1] = (Uint8)W1;
p[2] = (Uint8)(W2 >> 8);
p[3] = (Uint8)W2;
dst += 4;
dstlen -= 4;
}
}
break;
case ENCODING_UTF16LE: /* RFC 2781 */
{
Uint8 *p = (Uint8 *)dst;
if ( ch > 0x10FFFF ) {
ch = UNKNOWN_UNICODE;
}
if ( ch < 0x10000 ) {
if ( dstlen < 2 ) {
return SDL_ICONV_E2BIG;
}
p[1] = (Uint8)(ch >> 8);
p[0] = (Uint8)ch;
dst += 2;
dstlen -= 2;
} else {
Uint16 W1, W2;
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
ch = ch - 0x10000;
W1 = 0xD800 | (Uint16)((ch >> 10) & 0x3FF);
W2 = 0xDC00 | (Uint16)(ch & 0x3FF);
p[1] = (Uint8)(W1 >> 8);
p[0] = (Uint8)W1;
p[3] = (Uint8)(W2 >> 8);
p[2] = (Uint8)W2;
dst += 4;
dstlen -= 4;
}
}
break;
case ENCODING_UTF32BE:
{
Uint8 *p = (Uint8 *)dst;
Mar 13, 2006
Mar 13, 2006
692
if ( ch > 0x10FFFF ) {
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
ch = UNKNOWN_UNICODE;
}
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
p[0] = (Uint8)(ch >> 24);
p[1] = (Uint8)(ch >> 16);
p[2] = (Uint8)(ch >> 8);
p[3] = (Uint8)ch;
dst += 4;
dstlen -= 4;
}
break;
case ENCODING_UTF32LE:
{
Uint8 *p = (Uint8 *)dst;
Mar 13, 2006
Mar 13, 2006
709
if ( ch > 0x10FFFF ) {
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
ch = UNKNOWN_UNICODE;
}
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
p[3] = (Uint8)(ch >> 24);
p[2] = (Uint8)(ch >> 16);
p[1] = (Uint8)(ch >> 8);
p[0] = (Uint8)ch;
dst += 4;
dstlen -= 4;
}
break;
case ENCODING_UCS2:
{
Uint16 *p = (Uint16 *)dst;
if ( ch > 0xFFFF ) {
ch = UNKNOWN_UNICODE;
}
if ( dstlen < 2 ) {
return SDL_ICONV_E2BIG;
}
*p = (Uint16)ch;
dst += 2;
dstlen -= 2;
}
break;
case ENCODING_UCS4:
{
Uint32 *p = (Uint32 *)dst;
if ( ch > 0x7FFFFFFF ) {
ch = UNKNOWN_UNICODE;
}
if ( dstlen < 4 ) {
return SDL_ICONV_E2BIG;
}
*p = ch;
dst += 4;
dstlen -= 4;
}
break;
}
/* Update state */
*inbuf = src;
*inbytesleft = srclen;
*outbuf = dst;
*outbytesleft = dstlen;
++total;
}
return total;
}
int SDL_iconv_close(SDL_iconv_t cd)
{
if ( cd && cd != (SDL_iconv_t)-1 ) {
SDL_free(cd);
}
return 0;
}
#endif /* !HAVE_ICONV */
Feb 16, 2007
Feb 16, 2007
773
char *SDL_iconv_string(const char *tocode, const char *fromcode, const char *inbuf, size_t inbytesleft)
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
{
SDL_iconv_t cd;
char *string;
size_t stringsize;
char *outbuf;
size_t outbytesleft;
size_t retCode = 0;
cd = SDL_iconv_open(tocode, fromcode);
if ( cd == (SDL_iconv_t)-1 ) {
return NULL;
}
stringsize = inbytesleft > 4 ? inbytesleft : 4;
string = SDL_malloc(stringsize);
if ( !string ) {
SDL_iconv_close(cd);
return NULL;
}
outbuf = string;
outbytesleft = stringsize;
SDL_memset(outbuf, 0, 4);
while ( inbytesleft > 0 ) {
retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
switch (retCode) {
case SDL_ICONV_E2BIG:
{
char *oldstring = string;
stringsize *= 2;
string = SDL_realloc(string, stringsize);
if ( !string ) {
SDL_iconv_close(cd);
return NULL;
}
outbuf = string + (outbuf - oldstring);
outbytesleft = stringsize - (outbuf - string);
SDL_memset(outbuf, 0, 4);
}
break;
case SDL_ICONV_EILSEQ:
/* Try skipping some input data - not perfect, but... */
++inbuf;
--inbytesleft;
break;
case SDL_ICONV_EINVAL:
case SDL_ICONV_ERROR:
/* We can't continue... */
inbytesleft = 0;
break;
}
}
SDL_iconv_close(cd);
return string;
}