annotate lib/iconv.c @ 17409:26a04e61f560

stdio: use __REDIRECT for fwrite, fwrite_unlocked * lib/stdio.in.h (fwrite): When working around bug 11959, use __REDIRECT rather than '#define fwrite(...) ... fwrite (...) ...'. This is a more-targeted way to fix the -Wunused-value issue with clang, and it works with GCC too. Problem with targeting reported by Eric Blake in <http://lists.gnu.org/archive/html/bug-gnulib/2013-05/msg00067.html>. (fwrite_unlocked): Treat like fwrite. I ran into this issue while debugging the fwrite issue.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 15 May 2013 15:52:42 -0700
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Character set conversion.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16366
diff changeset
2 Copyright (C) 1999-2001, 2007, 2009-2013 Free Software Foundation, Inc.
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License along
16366
bb182ee4a09d maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #include <config.h>
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 /* Specification. */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <iconv.h>
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <stddef.h>
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #if REPLACE_ICONV_UTF
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 # include <errno.h>
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 # include <stdint.h>
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 # include <stdlib.h>
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 # include "unistr.h"
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 # ifndef uintptr_t
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 # define uintptr_t unsigned long
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 # endif
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 #endif
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 #if REPLACE_ICONV_UTF
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 /* UTF-{16,32}{BE,LE} converters taken from GNU libiconv 1.11. */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* Return code if invalid. (xxx_mbtowc) */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 # define RET_ILSEQ -1
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 /* Return code if no bytes were read. (xxx_mbtowc) */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 # define RET_TOOFEW -2
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 /* Return code if invalid. (xxx_wctomb) */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 # define RET_ILUNI -1
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 # define RET_TOOSMALL -2
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 /*
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 * UTF-16BE
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 /* Specification: RFC 2781 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 utf16be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 if (n >= 2)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 ucs4_t wc = (s[0] << 8) + s[1];
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 if (wc >= 0xd800 && wc < 0xdc00)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
61 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
62 if (n >= 4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
63 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
64 ucs4_t wc2 = (s[2] << 8) + s[3];
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
65 if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
66 return RET_ILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
67 *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
68 return 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
69 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
70 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 else if (wc >= 0xdc00 && wc < 0xe000)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
72 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
73 return RET_ILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
74 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
76 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
77 *pwc = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
78 return 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
79 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 return RET_TOOFEW;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 utf16be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 if (!(wc >= 0xd800 && wc < 0xe000))
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 if (wc < 0x10000)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
90 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
91 if (n >= 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
92 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
93 r[0] = (unsigned char) (wc >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
94 r[1] = (unsigned char) wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
95 return 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
96 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
97 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
98 return RET_TOOSMALL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
99 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 else if (wc < 0x110000)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
101 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
102 if (n >= 4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
103 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
104 ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
105 ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
106 r[0] = (unsigned char) (wc1 >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
107 r[1] = (unsigned char) wc1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
108 r[2] = (unsigned char) (wc2 >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
109 r[3] = (unsigned char) wc2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
110 return 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
111 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
112 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
113 return RET_TOOSMALL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
114 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116 return RET_ILUNI;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 /*
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 * UTF-16LE
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 /* Specification: RFC 2781 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 utf16le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 if (n >= 2)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 ucs4_t wc = s[0] + (s[1] << 8);
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 if (wc >= 0xd800 && wc < 0xdc00)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
132 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
133 if (n >= 4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
134 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
135 ucs4_t wc2 = s[2] + (s[3] << 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
136 if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
137 return RET_ILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
138 *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
139 return 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
140 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
141 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 else if (wc >= 0xdc00 && wc < 0xe000)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
143 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
144 return RET_ILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
145 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
147 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
148 *pwc = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
149 return 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
150 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 return RET_TOOFEW;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 utf16le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 if (!(wc >= 0xd800 && wc < 0xe000))
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 if (wc < 0x10000)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
161 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
162 if (n >= 2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
163 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
164 r[0] = (unsigned char) wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
165 r[1] = (unsigned char) (wc >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
166 return 2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
167 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
168 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
169 return RET_TOOSMALL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
170 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 else if (wc < 0x110000)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
172 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
173 if (n >= 4)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
174 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
175 ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
176 ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
177 r[0] = (unsigned char) wc1;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
178 r[1] = (unsigned char) (wc1 >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
179 r[2] = (unsigned char) wc2;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
180 r[3] = (unsigned char) (wc2 >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
181 return 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
182 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
183 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
184 return RET_TOOSMALL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
185 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 return RET_ILUNI;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 /*
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191 * UTF-32BE
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 /* Specification: Unicode 3.1 Standard Annex #19 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 utf32be_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 if (n >= 4)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 ucs4_t wc = (s[0] << 24) + (s[1] << 16) + (s[2] << 8) + s[3];
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
203 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
204 *pwc = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
205 return 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
206 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
208 return RET_ILSEQ;
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 return RET_TOOFEW;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 utf32be_wctomb (unsigned char *r, ucs4_t wc, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 if (n >= 4)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
219 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
220 r[0] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
221 r[1] = (unsigned char) (wc >> 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
222 r[2] = (unsigned char) (wc >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
223 r[3] = (unsigned char) wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
224 return 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
225 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
227 return RET_TOOSMALL;
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 return RET_ILUNI;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 /*
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 * UTF-32LE
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 /* Specification: Unicode 3.1 Standard Annex #19 */
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 utf32le_mbtowc (ucs4_t *pwc, const unsigned char *s, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 if (n >= 4)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 ucs4_t wc = s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24);
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
245 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
246 *pwc = wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
247 return 4;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
248 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
250 return RET_ILSEQ;
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 return RET_TOOFEW;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 static int
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 utf32le_wctomb (unsigned char *r, ucs4_t wc, size_t n)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 if (wc < 0x110000 && !(wc >= 0xd800 && wc < 0xe000))
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 if (n >= 4)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
261 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
262 r[0] = (unsigned char) wc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
263 r[1] = (unsigned char) (wc >> 8);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
264 r[2] = (unsigned char) (wc >> 16);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
265 r[3] = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
266 return 4;
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 else
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
269 return RET_TOOSMALL;
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271 return RET_ILUNI;
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 #endif
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 size_t
9349
0c6048a8aaed Use rpl_ prefix for functions, so as to avoid endless recursions in weird cases.
Bruno Haible <bruno@clisp.org>
parents: 9341
diff changeset
277 rpl_iconv (iconv_t cd,
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
278 ICONV_CONST char **inbuf, size_t *inbytesleft,
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
279 char **outbuf, size_t *outbytesleft)
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 #undef iconv
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 #if REPLACE_ICONV_UTF
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283 switch ((uintptr_t) cd)
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 {
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
286 int (*xxx_wctomb) (unsigned char *, ucs4_t, size_t);
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
288 case (uintptr_t) _ICONV_UTF8_UTF16BE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
289 xxx_wctomb = utf16be_wctomb;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
290 goto loop_from_utf8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
291 case (uintptr_t) _ICONV_UTF8_UTF16LE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
292 xxx_wctomb = utf16le_wctomb;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
293 goto loop_from_utf8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
294 case (uintptr_t) _ICONV_UTF8_UTF32BE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
295 xxx_wctomb = utf32be_wctomb;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
296 goto loop_from_utf8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
297 case (uintptr_t) _ICONV_UTF8_UTF32LE:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
298 xxx_wctomb = utf32le_wctomb;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
299 goto loop_from_utf8;
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 loop_from_utf8:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
302 if (inbuf == NULL || *inbuf == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
303 return 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
304 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
305 ICONV_CONST char *inptr = *inbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
306 size_t inleft = *inbytesleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
307 char *outptr = *outbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
308 size_t outleft = *outbytesleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
309 size_t res = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
310 while (inleft > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
311 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
312 ucs4_t uc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
313 int m = u8_mbtoucr (&uc, (const uint8_t *) inptr, inleft);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
314 if (m <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
315 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
316 if (m == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
317 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
318 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
319 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
320 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
321 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
322 if (m == -2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
323 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
324 errno = EINVAL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
325 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
326 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
327 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
328 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
329 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
330 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
331 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
332 int n = xxx_wctomb ((uint8_t *) outptr, uc, outleft);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
333 if (n < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
334 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
335 if (n == RET_ILUNI)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
336 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
337 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
338 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
339 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
340 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
341 if (n == RET_TOOSMALL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
342 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
343 errno = E2BIG;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
344 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
345 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
346 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
347 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
348 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
349 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
350 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
351 inptr += m;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
352 inleft -= m;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
353 outptr += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
354 outleft -= n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
355 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
356 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
357 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
358 *inbuf = inptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
359 *inbytesleft = inleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
360 *outbuf = outptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
361 *outbytesleft = outleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
362 return res;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
363 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 {
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
367 int (*xxx_mbtowc) (ucs4_t *, const unsigned char *, size_t);
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
369 case (uintptr_t) _ICONV_UTF16BE_UTF8:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
370 xxx_mbtowc = utf16be_mbtowc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
371 goto loop_to_utf8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
372 case (uintptr_t) _ICONV_UTF16LE_UTF8:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
373 xxx_mbtowc = utf16le_mbtowc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
374 goto loop_to_utf8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
375 case (uintptr_t) _ICONV_UTF32BE_UTF8:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
376 xxx_mbtowc = utf32be_mbtowc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
377 goto loop_to_utf8;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
378 case (uintptr_t) _ICONV_UTF32LE_UTF8:
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
379 xxx_mbtowc = utf32le_mbtowc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
380 goto loop_to_utf8;
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 loop_to_utf8:
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
383 if (inbuf == NULL || *inbuf == NULL)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
384 return 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
385 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
386 ICONV_CONST char *inptr = *inbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
387 size_t inleft = *inbytesleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
388 char *outptr = *outbuf;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
389 size_t outleft = *outbytesleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
390 size_t res = 0;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
391 while (inleft > 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
392 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
393 ucs4_t uc;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
394 int m = xxx_mbtowc (&uc, (const uint8_t *) inptr, inleft);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
395 if (m <= 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
396 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
397 if (m == RET_ILSEQ)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
398 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
399 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
400 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
401 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
402 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
403 if (m == RET_TOOFEW)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
404 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
405 errno = EINVAL;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
406 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
407 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
408 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
409 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
410 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
411 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
412 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
413 int n = u8_uctomb ((uint8_t *) outptr, uc, outleft);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
414 if (n < 0)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
415 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
416 if (n == -1)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
417 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
418 errno = EILSEQ;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
419 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
420 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
421 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
422 if (n == -2)
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
423 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
424 errno = E2BIG;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
425 res = (size_t)(-1);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
426 break;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
427 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
428 abort ();
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
429 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
430 else
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
431 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
432 inptr += m;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
433 inleft -= m;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
434 outptr += n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
435 outleft -= n;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
436 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
437 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
438 }
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
439 *inbuf = inptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
440 *inbytesleft = inleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
441 *outbuf = outptr;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
442 *outbytesleft = outleft;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
443 return res;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 9349
diff changeset
444 }
9341
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
445 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
446 }
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 #endif
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 return iconv (cd, inbuf, inbytesleft, outbuf, outbytesleft);
212d6f041290 New module 'iconv_open-utf': Enhance iconv_open to support UTF-{16,32}{BE,LE}.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 }