1iconv(3) Library Functions Manual iconv(3)
2
3
4
6 iconv - perform character set conversion
7
9 Standard C library (libc, -lc)
10
12 #include <iconv.h>
13
14 size_t iconv(iconv_t cd,
15 char **restrict inbuf, size_t *restrict inbytesleft,
16 char **restrict outbuf, size_t *restrict outbytesleft);
17
19 The iconv() function converts a sequence of characters in one character
20 encoding to a sequence of characters in another character encoding.
21 The cd argument is a conversion descriptor, previously created by a
22 call to iconv_open(3); the conversion descriptor defines the character
23 encodings that iconv() uses for the conversion. The inbuf argument is
24 the address of a variable that points to the first character of the in‐
25 put sequence; inbytesleft indicates the number of bytes in that buffer.
26 The outbuf argument is the address of a variable that points to the
27 first byte available in the output buffer; outbytesleft indicates the
28 number of bytes available in the output buffer.
29
30 The main case is when inbuf is not NULL and *inbuf is not NULL. In
31 this case, the iconv() function converts the multibyte sequence start‐
32 ing at *inbuf to a multibyte sequence starting at *outbuf. At most
33 *inbytesleft bytes, starting at *inbuf, will be read. At most *out‐
34 bytesleft bytes, starting at *outbuf, will be written.
35
36 The iconv() function converts one multibyte character at a time, and
37 for each character conversion it increments *inbuf and decrements *in‐
38 bytesleft by the number of converted input bytes, it increments *outbuf
39 and decrements *outbytesleft by the number of converted output bytes,
40 and it updates the conversion state contained in cd. If the character
41 encoding of the input is stateful, the iconv() function can also con‐
42 vert a sequence of input bytes to an update to the conversion state
43 without producing any output bytes; such input is called a shift se‐
44 quence. The conversion can stop for five reasons:
45
46 • An invalid multibyte sequence is encountered in the input. In this
47 case, it sets errno to EILSEQ and returns (size_t) -1. *inbuf is
48 left pointing to the beginning of the invalid multibyte sequence.
49
50 • A multibyte sequence is encountered that is valid but that cannot be
51 translated to the character encoding of the output. This condition
52 depends on the implementation and on the conversion descriptor. In
53 the GNU C library and GNU libiconv, if cd was created without the
54 suffix //TRANSLIT or //IGNORE, the conversion is strict: lossy con‐
55 versions produce this condition. If the suffix //TRANSLIT was spec‐
56 ified, transliteration can avoid this condition in some cases. In
57 the musl C library, this condition cannot occur because a conversion
58 to '*' is used as a fallback. In the FreeBSD, NetBSD, and Solaris
59 implementations of iconv(), this condition cannot occur either, be‐
60 cause a conversion to '?' is used as a fallback. When this condi‐
61 tion is met, iconv() sets errno to EILSEQ and returns (size_t) -1.
62 *inbuf is left pointing to the beginning of the unconvertible multi‐
63 byte sequence.
64
65 • The input byte sequence has been entirely converted, that is, *in‐
66 bytesleft has gone down to 0. In this case, iconv() returns the
67 number of nonreversible conversions performed during this call.
68
69 • An incomplete multibyte sequence is encountered in the input, and
70 the input byte sequence terminates after it. In this case, it sets
71 errno to EINVAL and returns (size_t) -1. *inbuf is left pointing to
72 the beginning of the incomplete multibyte sequence.
73
74 • The output buffer has no more room for the next converted character.
75 In this case, it sets errno to E2BIG and returns (size_t) -1.
76
77 A different case is when inbuf is NULL or *inbuf is NULL, but outbuf is
78 not NULL and *outbuf is not NULL. In this case, the iconv() function
79 attempts to set cd's conversion state to the initial state and store a
80 corresponding shift sequence at *outbuf. At most *outbytesleft bytes,
81 starting at *outbuf, will be written. If the output buffer has no more
82 room for this reset sequence, it sets errno to E2BIG and returns
83 (size_t) -1. Otherwise, it increments *outbuf and decrements *out‐
84 bytesleft by the number of bytes written.
85
86 A third case is when inbuf is NULL or *inbuf is NULL, and outbuf is
87 NULL or *outbuf is NULL. In this case, the iconv() function sets cd's
88 conversion state to the initial state.
89
91 The iconv() function returns the number of characters converted in a
92 nonreversible way during this call; reversible conversions are not
93 counted. In case of error, iconv() returns (size_t) -1 and sets errno
94 to indicate the error.
95
97 The following errors can occur, among others:
98
99 E2BIG There is not sufficient room at *outbuf.
100
101 EILSEQ An invalid multibyte sequence has been encountered in the input.
102
103 EINVAL An incomplete multibyte sequence has been encountered in the in‐
104 put.
105
107 For an explanation of the terms used in this section, see at‐
108 tributes(7).
109
110 ┌────────────────────────────────────┬───────────────┬─────────────────┐
111 │Interface │ Attribute │ Value │
112 ├────────────────────────────────────┼───────────────┼─────────────────┤
113 │iconv() │ Thread safety │ MT-Safe race:cd │
114 └────────────────────────────────────┴───────────────┴─────────────────┘
115
116 The iconv() function is MT-Safe, as long as callers arrange for mutual
117 exclusion on the cd argument.
118
120 POSIX.1-2008.
121
123 glibc 2.1. POSIX.1-2001.
124
126 In each series of calls to iconv(), the last should be one with inbuf
127 or *inbuf equal to NULL, in order to flush out any partially converted
128 input.
129
130 Although inbuf and outbuf are typed as char **, this does not mean that
131 the objects they point can be interpreted as C strings or as arrays of
132 characters: the interpretation of character byte sequences is handled
133 internally by the conversion functions. In some encodings, a zero byte
134 may be a valid part of a multibyte character.
135
136 The caller of iconv() must ensure that the pointers passed to the
137 function are suitable for accessing characters in the appropriate
138 character set. This includes ensuring correct alignment on platforms
139 that have tight restrictions on alignment.
140
142 iconv_close(3), iconv_open(3), iconvconfig(8)
143
144
145
146Linux man-pages 6.05 2023-07-20 iconv(3)