1ICONV(3) Linux Programmer's Manual ICONV(3)
2
3
4
6 iconv - perform character set conversion
7
9 #include <iconv.h>
10
11 size_t iconv(iconv_t cd,
12 char **inbuf, size_t *inbytesleft,
13 char **outbuf, size_t *outbytesleft);
14
16 The iconv() function converts a sequence of characters in one character
17 encoding to a sequence of characters in another character encoding.
18 The cd argument is a conversion descriptor, previously created by a
19 call to iconv_open(3); the conversion descriptor defines the character
20 encodings that iconv() uses for the conversion. The inbuf argument is
21 the address of a variable that points to the first character of the
22 input sequence; inbytesleft indicates the number of bytes in that buf‐
23 fer. The outbuf argument is the address of a variable that points to
24 the first byte available in the output buffer; outbytesleft indicates
25 the number of bytes available in the output buffer.
26
27 The main case is when inbuf is not NULL and *inbuf is not NULL. In
28 this case, the iconv() function converts the multibyte sequence start‐
29 ing at *inbuf to a multibyte sequence starting at *outbuf. At most
30 *inbytesleft bytes, starting at *inbuf, will be read. At most *out‐
31 bytesleft bytes, starting at *outbuf, will be written.
32
33 The iconv() function converts one multibyte character at a time, and
34 for each character conversion it increments *inbuf and decrements
35 *inbytesleft by the number of converted input bytes, it increments
36 *outbuf and decrements *outbytesleft by the number of converted output
37 bytes, and it updates the conversion state contained in cd. If the
38 character encoding of the input is stateful, the iconv() function can
39 also convert a sequence of input bytes to an update to the conversion
40 state without producing any output bytes; such input is called a shift
41 sequence. The conversion can stop for four reasons:
42
43 1. An invalid multibyte sequence is encountered in the input. In this
44 case, it sets errno to EILSEQ and returns (size_t) -1. *inbuf is
45 left pointing to the beginning of the invalid multibyte sequence.
46
47 2. The input byte sequence has been entirely converted, that is,
48 *inbytesleft has gone down to 0. In this case, iconv() returns the
49 number of nonreversible conversions performed during this call.
50
51 3. An incomplete multibyte sequence is encountered in the input, and
52 the input byte sequence terminates after it. In this case, it sets
53 errno to EINVAL and returns (size_t) -1. *inbuf is left pointing to
54 the beginning of the incomplete multibyte sequence.
55
56 4. The output buffer has no more room for the next converted character.
57 In this case, it sets errno to E2BIG and returns (size_t) -1.
58
59 A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is
60 not NULL and *outbuf is not NULL. In this case, the iconv() function
61 attempts to set cd's conversion state to the initial state and store a
62 corresponding shift sequence at *outbuf. At most *outbytesleft bytes,
63 starting at *outbuf, will be written. If the output buffer has no more
64 room for this reset sequence, it sets errno to E2BIG and returns
65 (size_t) -1. Otherwise, it increments *outbuf and decrements *out‐
66 bytesleft by the number of bytes written.
67
68 A third case is when inbuf is NULL or *inbuf is NULL, and outbuf is
69 NULL or *outbuf is NULL. In this case, the iconv() function sets cd's
70 conversion state to the initial state.
71
73 The iconv() function returns the number of characters converted in a
74 nonreversible way during this call; reversible conversions are not
75 counted. In case of error, it sets errno and returns (size_t) -1.
76
78 The following errors can occur, among others:
79
80 E2BIG There is not sufficient room at *outbuf.
81
82 EILSEQ An invalid multibyte sequence has been encountered in the input.
83
84 EINVAL An incomplete multibyte sequence has been encountered in the
85 input.
86
88 This function is available in glibc since version 2.1.
89
91 For an explanation of the terms used in this section, see
92 attributes(7).
93
94 ┌──────────┬───────────────┬─────────────────┐
95 │Interface │ Attribute │ Value │
96 ├──────────┼───────────────┼─────────────────┤
97 │iconv() │ Thread safety │ MT-Safe race:cd │
98 └──────────┴───────────────┴─────────────────┘
99 The iconv() function is MT-Safe, as long as callers arrange for mutual
100 exclusion on the cd argument.
101
103 POSIX.1-2001, POSIX.1-2008.
104
106 In each series of calls to iconv(), the last should be one with inbuf
107 or *inbuf equal to NULL, in order to flush out any partially converted
108 input.
109
110 Although inbuf and outbuf are typed as char **, this does not mean that
111 the objects they point can be interpreted as C strings or as arrays of
112 characters: the interpretation of character byte sequences is handled
113 internally by the conversion functions. In some encodings, a zero byte
114 may be a valid part of a multibyte character.
115
116 The caller of iconv() must ensure that the pointers passed to the func‐
117 tion are suitable for accessing characters in the appropriate character
118 set. This includes ensuring correct alignment on platforms that have
119 tight restrictions on alignment.
120
122 iconv_close(3), iconv_open(3), iconvconfig(8)
123
125 This page is part of release 4.15 of the Linux man-pages project. A
126 description of the project, information about reporting bugs, and the
127 latest version of this page, can be found at
128 https://www.kernel.org/doc/man-pages/.
129
130
131
132GNU 2017-09-15 ICONV(3)