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 **restrict inbuf, size_t *restrict inbytesleft,
13 char **restrict outbuf, size_t *restrict 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 in‐
22 put sequence; inbytesleft indicates the number of bytes in that buffer.
23 The outbuf argument is the address of a variable that points to the
24 first byte available in the output buffer; outbytesleft indicates the
25 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 *in‐
35 bytesleft by the number of converted input bytes, it increments *outbuf
36 and decrements *outbytesleft by the number of converted output bytes,
37 and it updates the conversion state contained in cd. If the character
38 encoding of the input is stateful, the iconv() function can also con‐
39 vert a sequence of input bytes to an update to the conversion state
40 without producing any output bytes; such input is called a shift se‐
41 quence. 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, *in‐
48 bytesleft 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, iconv() returns (size_t) -1 and sets errno
76 to indicate the error.
77
79 The following errors can occur, among others:
80
81 E2BIG There is not sufficient room at *outbuf.
82
83 EILSEQ An invalid multibyte sequence has been encountered in the input.
84
85 EINVAL An incomplete multibyte sequence has been encountered in the in‐
86 put.
87
89 This function is available in glibc since version 2.1.
90
92 For an explanation of the terms used in this section, see at‐
93 tributes(7).
94
95 ┌────────────────────────────────────┬───────────────┬─────────────────┐
96 │Interface │ Attribute │ Value │
97 ├────────────────────────────────────┼───────────────┼─────────────────┤
98 │iconv() │ Thread safety │ MT-Safe race:cd │
99 └────────────────────────────────────┴───────────────┴─────────────────┘
100
101 The iconv() function is MT-Safe, as long as callers arrange for mutual
102 exclusion on the cd argument.
103
105 POSIX.1-2001, POSIX.1-2008.
106
108 In each series of calls to iconv(), the last should be one with inbuf
109 or *inbuf equal to NULL, in order to flush out any partially converted
110 input.
111
112 Although inbuf and outbuf are typed as char **, this does not mean that
113 the objects they point can be interpreted as C strings or as arrays of
114 characters: the interpretation of character byte sequences is handled
115 internally by the conversion functions. In some encodings, a zero byte
116 may be a valid part of a multibyte character.
117
118 The caller of iconv() must ensure that the pointers passed to the func‐
119 tion are suitable for accessing characters in the appropriate character
120 set. This includes ensuring correct alignment on platforms that have
121 tight restrictions on alignment.
122
124 iconv_close(3), iconv_open(3), iconvconfig(8)
125
127 This page is part of release 5.13 of the Linux man-pages project. A
128 description of the project, information about reporting bugs, and the
129 latest version of this page, can be found at
130 https://www.kernel.org/doc/man-pages/.
131
132
133
134GNU 2021-03-22 ICONV(3)