OSDN Git Service

3f051a13e4cfd352e7c63e1fcdbde110e5ad3af3
[linuxjm/LDP_man-pages.git] / original / man3 / iconv.3
1 .\" Copyright (c) Bruno Haible <haible@clisp.cons.org>
2 .\"
3 .\" %%%LICENSE_START(GPLv2+_DOC_ONEPARA)
4 .\" This is free documentation; you can redistribute it and/or
5 .\" modify it under the terms of the GNU General Public License as
6 .\" published by the Free Software Foundation; either version 2 of
7 .\" the License, or (at your option) any later version.
8 .\" %%%LICENSE_END
9 .\"
10 .\" References consulted:
11 .\"   GNU glibc-2 source code and manual
12 .\"   OpenGroup's Single UNIX specification
13 .\"     http://www.UNIX-systems.org/online.html
14 .\"
15 .\" 2000-06-30 correction by Yuichi SATO <sato@complex.eng.hokudai.ac.jp>
16 .\" 2000-11-15 aeb, fixed prototype
17 .\"
18 .TH ICONV 3 2012-05-10 "GNU" "Linux Programmer's Manual"
19 .SH NAME
20 iconv \- perform character set conversion
21 .SH SYNOPSIS
22 .nf
23 .B #include <iconv.h>
24 .sp
25 .BI "size_t iconv(iconv_t " cd ,
26 .BI "             char **" inbuf ", size_t *" inbytesleft ,
27 .BI "             char **" outbuf ", size_t *" outbytesleft );
28 .fi
29 .SH DESCRIPTION
30 The
31 .BR iconv ()
32 function converts a sequence of characters in one character encoding
33 to a sequence of characters in another character encoding.
34 The
35 .I cd
36 argument is a conversion descriptor,
37 previously created by a call to
38 .BR iconv_open (3);
39 the conversion descriptor defines the character encodings that
40 .BR iconv ()
41 uses for the conversion.
42 The
43 .I inbuf
44 argument is the address of a variable that points to
45 the first character of the input sequence;
46 .I inbytesleft
47 indicates the number of bytes in that buffer.
48 The
49 .I outbuf
50 argument is the address of a variable that points to
51 the first byte available in the output buffer;
52 .I outbytesleft
53 indicates the number of bytes available in the output buffer.
54 .PP
55 The main case is when \fIinbuf\fP is not NULL and \fI*inbuf\fP is not NULL.
56 In this case, the
57 .BR iconv ()
58 function converts the multibyte sequence
59 starting at \fI*inbuf\fP to a multibyte sequence starting at \fI*outbuf\fP.
60 At most \fI*inbytesleft\fP bytes, starting at \fI*inbuf\fP, will be read.
61 At most \fI*outbytesleft\fP bytes, starting at \fI*outbuf\fP, will be written.
62 .PP
63 The
64 .BR iconv ()
65 function converts one multibyte character at a time, and for
66 each character conversion it increments \fI*inbuf\fP and decrements
67 \fI*inbytesleft\fP by the number of converted input bytes, it increments
68 \fI*outbuf\fP and decrements \fI*outbytesleft\fP by the number of converted
69 output bytes, and it updates the conversion state contained in \fIcd\fP.
70 If the character encoding of the input is stateful, the
71 .BR iconv ()
72 function can also convert a sequence of input bytes
73 to an update to the conversion state without producing any output bytes;
74 such input is called a \fIshift sequence\fP.
75 The conversion can stop for four reasons:
76 .IP 1. 3
77 An invalid multibyte sequence is encountered in the input.
78 In this case,
79 it sets \fIerrno\fP to \fBEILSEQ\fP and returns
80 .IR (size_t)\ \-1 .
81 \fI*inbuf\fP
82 is left pointing to the beginning of the invalid multibyte sequence.
83 .IP 2.
84 The input byte sequence has been entirely converted,
85 that is, \fI*inbytesleft\fP has gone down to 0.
86 In this case,
87 .BR iconv ()
88 returns the number of
89 nonreversible conversions performed during this call.
90 .IP 3.
91 An incomplete multibyte sequence is encountered in the input, and the
92 input byte sequence terminates after it.
93 In this case, it sets \fIerrno\fP to
94 \fBEINVAL\fP and returns
95 .IR (size_t)\ \-1 .
96 \fI*inbuf\fP is left pointing to the
97 beginning of the incomplete multibyte sequence.
98 .IP 4.
99 The output buffer has no more room for the next converted character.
100 In this case, it sets \fIerrno\fP to \fBE2BIG\fP and returns
101 .IR (size_t)\ \-1 .
102 .PP
103 A different case is when \fIinbuf\fP is NULL or \fI*inbuf\fP is NULL, but
104 \fIoutbuf\fP is not NULL and \fI*outbuf\fP is not NULL.
105 In this case, the
106 .BR iconv ()
107 function attempts to set \fIcd\fP's conversion state to the
108 initial state and store a corresponding shift sequence at \fI*outbuf\fP.
109 At most \fI*outbytesleft\fP bytes, starting at \fI*outbuf\fP, will be written.
110 If the output buffer has no more room for this reset sequence, it sets
111 \fIerrno\fP to \fBE2BIG\fP and returns
112 .IR (size_t)\ \-1 .
113 Otherwise, it increments
114 \fI*outbuf\fP and decrements \fI*outbytesleft\fP by the number of bytes
115 written.
116 .PP
117 A third case is when \fIinbuf\fP is NULL or \fI*inbuf\fP is NULL, and
118 \fIoutbuf\fP is NULL or \fI*outbuf\fP is NULL.
119 In this case, the
120 .BR iconv ()
121 function sets \fIcd\fP's conversion state to the initial state.
122 .SH RETURN VALUE
123 The
124 .BR iconv ()
125 function returns the number of characters converted in a
126 nonreversible way during this call; reversible conversions are not counted.
127 In case of error, it sets \fIerrno\fP and returns
128 .IR (size_t)\ \-1 .
129 .SH ERRORS
130 The following errors can occur, among others:
131 .TP
132 .B E2BIG
133 There is not sufficient room at \fI*outbuf\fP.
134 .TP
135 .B EILSEQ
136 An invalid multibyte sequence has been encountered in the input.
137 .TP
138 .B EINVAL
139 An incomplete multibyte sequence has been encountered in the input.
140 .SH VERSIONS
141 This function is available in glibc since version 2.1.
142 .SH CONFORMING TO
143 POSIX.1-2001.
144 .SH NOTES
145 Although
146 .I inbuf
147 and
148 .I outbuf
149 are typed as
150 .IR "char\ **" ,
151 this does not mean that the objects they point can be interpreted
152 as C strings or as arrays of characters:
153 the interpretation of character byte sequences is
154 handled internally by the conversion functions.
155 In some encodings, a zero byte may be a valid part of a multibyte character.
156
157 The caller of
158 .BR iconv ()
159 must ensure that the pointers passed to the function are suitable
160 for accessing characters in the appropriate character set.
161 This includes ensuring correct alignment on platforms that have
162 tight restrictions on alignment.
163 .SH SEE ALSO
164 .BR iconv_close (3),
165 .BR iconv_open (3)
166 .SH COLOPHON
167 This page is part of release 3.67 of the Linux
168 .I man-pages
169 project.
170 A description of the project,
171 information about reporting bugs,
172 and the latest version of this page,
173 can be found at
174 \%http://www.kernel.org/doc/man\-pages/.