1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
'\" t
.\" Copyright, the authors of the Linux man-pages project
.\"
.\" SPDX-License-Identifier: GPL-2.0-or-later
.\"
.TH iconv 3 2025-05-17 "Linux man-pages (unreleased)"
.SH NAME
iconv \- perform character set conversion
.SH LIBRARY
Standard C library
.RI ( libc ,\~ \-lc )
.SH SYNOPSIS
.nf
.B #include <iconv.h>
.P
.BI "size_t iconv(iconv_t " cd ,
.BI " char **restrict " inbuf ", size_t *restrict " inbytesleft ,
.BI " char **restrict " outbuf ", size_t *restrict " outbytesleft );
.fi
.SH DESCRIPTION
The
.BR iconv ()
function converts a sequence of characters in one character encoding
to a sequence of characters in another character encoding.
The
.I cd
argument is a conversion descriptor,
previously created by a call to
.BR iconv_open (3);
the conversion descriptor defines the character encodings that
.BR iconv ()
uses for the conversion.
The
.I inbuf
argument is the address of a variable that points to
the first character of the input sequence;
.I inbytesleft
indicates the number of bytes in that buffer.
The
.I outbuf
argument is the address of a variable that points to
the first byte available in the output buffer;
.I outbytesleft
indicates the number of bytes available in the output buffer.
.P
The main case is when
.I inbuf
is not NULL and
.I *inbuf
is not NULL.
In this case, the
.BR iconv ()
function converts the multibyte sequence
starting at
.I *inbuf
to a multibyte sequence starting at
.IR *outbuf .
At most
.I *inbytesleft
bytes, starting at
.IR *inbuf ,
will be read.
At most
.I *outbytesleft
bytes, starting at
.IR *outbuf ,
will be written.
.P
The
.BR iconv ()
function converts one multibyte character at a time,
and for each character conversion it increments
.I *inbuf
and decrements
.I *inbytesleft
by the number of converted input bytes,
it increments
.I *outbuf
and decrements
.I *outbytesleft
by the number of converted output bytes,
and it updates the conversion state contained in
.IR cd .
If the character encoding of the input is stateful, the
.BR iconv ()
function can also convert a sequence of input bytes
to an update to the conversion state without producing any output bytes;
such input is called a
.IR shift\~sequence .
The conversion can stop for five reasons:
.IP \[bu] 3
An invalid multibyte sequence is encountered in the input.
In this case,
it sets
.I errno
to
.B EILSEQ
and returns
.IR "(size_t)\ \-1" .
.I *inbuf
is left pointing to the beginning of the invalid multibyte sequence.
.IP \[bu]
A multibyte sequence is encountered that is valid but that
cannot be translated to the character encoding of the output.
This condition depends on the implementation and on the conversion descriptor.
In the GNU C library and GNU libiconv, if
.I cd
was created without the suffix
.B //TRANSLIT
or
.BR //IGNORE ,
the conversion is strict:
lossy conversions produce this condition.
If the suffix
.B //TRANSLIT
was specified,
transliteration can avoid this condition in some cases.
In the musl C library,
this condition cannot occur because a conversion to
.B \[aq]*\[aq]
is used as a fallback.
In the FreeBSD, NetBSD, and Solaris implementations of
.BR iconv (),
this condition cannot occur either,
because a conversion to
.B \[aq]?\[aq]
is used as a fallback.
When this condition is met,
.BR iconv ()
sets
.I errno
to
.B EILSEQ
and returns
.IR "(size_t)\ \-1" .
.I *inbuf
is left pointing to the beginning of the unconvertible multibyte sequence.
.IP \[bu]
The input byte sequence has been entirely converted,
that is,
.I *inbytesleft
has gone down to 0.
In this case,
.BR iconv ()
returns the number of
nonreversible conversions performed during this call.
.IP \[bu]
An incomplete multibyte sequence is encountered in the input, and the
input byte sequence terminates after it.
In this case, it sets
.I errno
to
.B EINVAL
and returns
.IR "(size_t)\ \-1" .
.I *inbuf
is left pointing to the
beginning of the incomplete multibyte sequence.
.IP \[bu]
The output buffer has no more room for the next converted character.
In this case, it sets
.I errno
to
.B E2BIG
and returns
.IR "(size_t)\ \-1" .
.P
A different case is when
.I inbuf
is NULL or
.I *inbuf
is NULL, but
.I outbuf
is not NULL and
.I *outbuf
is not NULL.
In this case, the
.BR iconv ()
function attempts to set
.IR cd 's
conversion state to the
initial state and store a corresponding shift sequence at
.IR *outbuf .
At most
.I *outbytesleft
bytes, starting at
.IR *outbuf ,
will be written.
If the output buffer has no more room for this reset sequence, it sets
.I errno
to
.B E2BIG
and returns
.IR "(size_t)\ \-1" .
Otherwise, it increments
.I *outbuf
and decrements
.I *outbytesleft
by the number of bytes
written.
.P
A third case is when
.I inbuf
is NULL or
.I *inbuf
is NULL, and
.I outbuf
is NULL or
.I *outbuf
is NULL.
In this case, the
.BR iconv ()
function sets
.IR cd 's
conversion state to the initial state.
.SH RETURN VALUE
The
.BR iconv ()
function returns the number of characters converted in a
nonreversible way during this call; reversible conversions are not counted.
In case of error,
.BR iconv ()
returns
.I (size_t)\ \-1
and sets
.I errno
to indicate the error.
.SH ERRORS
The following errors can occur, among others:
.TP
.B E2BIG
There is not sufficient room at
.IR *outbuf .
.TP
.B EILSEQ
An invalid multibyte sequence has been encountered in the input.
.TP
.B EINVAL
An incomplete multibyte sequence has been encountered in the input.
.SH ATTRIBUTES
For an explanation of the terms used in this section, see
.BR attributes (7).
.TS
allbox;
lbx lb lb
l l l.
Interface Attribute Value
T{
.na
.nh
.BR iconv ()
T} Thread safety MT-Safe race:cd
.TE
.P
The
.BR iconv ()
function is MT-Safe, as long as callers arrange for
mutual exclusion on the
.I cd
argument.
.SH STANDARDS
POSIX.1-2008.
.SH HISTORY
glibc 2.1.
POSIX.1-2001.
.SH NOTES
In each series of calls to
.BR iconv (),
the last should be one with
.I inbuf
or
.I *inbuf
equal to NULL,
in order to flush out any partially converted input.
.P
Although
.I inbuf
and
.I outbuf
are typed as
.IR "char\ **" ,
this does not mean that the objects they point can be interpreted
as C strings or as arrays of characters:
the interpretation of character byte sequences is
handled internally by the conversion functions.
In some encodings, a zero byte may be a valid part of a multibyte character.
.P
The caller of
.BR iconv ()
must ensure that the pointers passed to the function are suitable
for accessing characters in the appropriate character set.
This includes ensuring correct alignment on platforms that have
tight restrictions on alignment.
.SH SEE ALSO
.BR iconv_close (3),
.BR iconv_open (3),
.BR iconvconfig (8)
|