Branch
Hash :
cf10b545
Author :
Date :
2025-05-31T00:29:46
Document the 'const char **' vs. 'char **' problem. Reported by Braden Ganetsky <braden.ganetsky@gmail.com> in <https://lists.gnu.org/archive/html/bug-gnu-libiconv/2025-05/msg00003.html>. * man/iconv.3 (SYNOPSIS, CONFORMING TO): List also the POSIX-compatible declaration. Explain the background. Mention AM_ICONV.
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
.\" Copyright (c) Free Software Foundation, Inc.
.\"
.\" This is free documentation; you can redistribute it and/or
.\" modify it under the terms of the GNU General Public License as
.\" published by the Free Software Foundation; either version 3 of
.\" the License, or (at your option) any later version.
.\"
.\" References consulted:
.\" GNU glibc-2 source code and manual
.\" OpenGroup's Single Unix specification http://www.UNIX-systems.org/online.html
.\"
.TH ICONV 3 "May 30, 2025" "GNU"
.SH NAME
iconv \- perform character set conversion
.SH SYNOPSIS
.nf
.B #include <iconv.h>
.sp
.BI "size_t iconv (iconv_t " cd ,
.BI " const char* * " inbuf ", size_t * "inbytesleft ,
.BI " char* * " outbuf ", size_t * "outbytesleft );
.sp
or (for POSIX compatibility)
.sp
.BI "size_t iconv (iconv_t " cd ,
.BI " char* * " inbuf ", size_t * "inbytesleft ,
.BI " char* * " outbuf ", size_t * "outbytesleft );
.fi
.SH DESCRIPTION
The argument \fIcd\fP must be a conversion descriptor created using the
function \fBiconv_open\fP.
.PP
The main case is when \fIinbuf\fP is not NULL and \fI*inbuf\fP is not NULL.
In this case, the \fBiconv\fP function converts the multibyte sequence
starting at \fI*inbuf\fP to a multibyte sequence starting at \fI*outbuf\fP.
At most \fI*inbytesleft\fP bytes, starting at \fI*inbuf\fP, will be read.
At most \fI*outbytesleft\fP bytes, starting at \fI*outbuf\fP, will be written.
.PP
The \fBiconv\fP function converts one multibyte character at a time, and for
each character conversion it increments \fI*inbuf\fP and decrements
\fI*inbytesleft\fP by the number of converted input bytes, it increments
\fI*outbuf\fP and decrements \fI*outbytesleft\fP by the number of converted
output bytes, and it updates the conversion state contained in \fIcd\fP.
If the character encoding of the input is stateful, the \fBiconv\fP function
can also convert a sequence of input bytes to an update of the conversion state
without producing any output bytes; such input is called a \fIshift sequence\fP.
The conversion can stop for five reasons:
.PP
1. An invalid multibyte sequence is encountered in the input. In this case
it sets \fBerrno\fP to \fBEILSEQ\fP and returns (size_t)(\-1). \fI*inbuf\fP
is left pointing to the beginning of the invalid multibyte sequence.
.PP
2. 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 \fIcd\fP was created without the
suffix \fB//TRANSLIT\fP or \fB//IGNORE\fP or \fB//NON_IDENTICAL_DISCARD\fP,
the conversion is strict: lossy conversions produce this condition.
If the suffix \fB//TRANSLIT\fP was specified, transliteration can avoid this
condition in some cases.
In the musl C library, this condition cannot occur because a conversion to
\fB\[aq]*\[aq]\fP is used as a fallback.
In the FreeBSD, NetBSD, and Solaris implementations of \fBiconv\fP, this
condition cannot occur either, because a conversion to \fB\[aq]?\[aq]\fP is
used as a fallback.
When this condition is met, the \fBiconv\fP function sets \fBerrno\fP to
\fBEILSEQ\fP and returns (size_t)(\-1).
\fI*inbuf\fP is left pointing to the beginning of the unconvertible multibyte
sequence.
.PP
3. The input byte sequence has been entirely converted, i.e. \fI*inbytesleft\fP
has gone down to 0. In this case \fBiconv\fP returns the number of
non-reversible conversions performed during this call.
.PP
4. An incomplete multibyte sequence is encountered in the input, and the
input byte sequence terminates after it. In this case it sets \fBerrno\fP to
\fBEINVAL\fP and returns (size_t)(\-1). \fI*inbuf\fP is left pointing to the
beginning of the incomplete multibyte sequence.
.PP
5. The output buffer has no more room for the next converted character. In
this case it sets \fBerrno\fP to \fBE2BIG\fP and returns (size_t)(\-1).
.PP
A different case is when \fIinbuf\fP is NULL or \fI*inbuf\fP is NULL, but
\fIoutbuf\fP is not NULL and \fI*outbuf\fP is not NULL. In this case, the
\fBiconv\fP function attempts to set \fIcd\fP's conversion state to the
initial state and store a corresponding shift sequence at \fI*outbuf\fP.
At most \fI*outbytesleft\fP bytes, starting at \fI*outbuf\fP, will be written.
If the output buffer has no more room for this reset sequence, it sets
\fBerrno\fP to \fBE2BIG\fP and returns (size_t)(\-1). Otherwise it increments
\fI*outbuf\fP and decrements \fI*outbytesleft\fP by the number of bytes
written.
.PP
A third case is when \fIinbuf\fP is NULL or \fI*inbuf\fP is NULL, and
\fIoutbuf\fP is NULL or \fI*outbuf\fP is NULL. In this case, the \fBiconv\fP
function sets \fIcd\fP's conversion state to the initial state.
.SH "RETURN VALUE"
The \fBiconv\fP function returns the number of characters converted in a
non-reversible way during this call; reversible conversions are not counted.
In case of error, it sets \fBerrno\fP and returns (size_t)(\-1).
.SH ERRORS
The following errors can occur, among others:
.TP
.B E2BIG
There is not sufficient room at \fI*outbuf\fP.
.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 "CONFORMING TO"
POSIX:2024
.PP
Note:
In the functions \fBiconv\fP, \fBexecv\fP, \fBexecve\fP, \fBexecvp\fP,
POSIX uses the type
\fBchar **\fP in place of the more correct \fBconst char **\fP,
thus forcing programs which use \fBconst\fP as usual
to add a cast in the second argument of an \fBiconv\fP invocation.
GNU libiconv uses \fBconst char **\fP or \fBchar **\fP,
depending on the platform.
.PP
Packages that use the GNU Build System can determine
which of the two choices the declaration of \fBiconv\fP in \fB<iconv.h>\fP
actually uses.
To do this, the package can use the Autoconf macro \fBAM_ICONV\fP,
documented in the GNU gettext manual and
also available from the module \fBiconv\fP in GNU gnulib,
and then use the C preprocessor symbol \fBICONV_CONST\fP.
.SH NOTES
In each series of calls to the \fBiconv\fP function,
the last should be one with \fIinbuf\fP or \fI*inbuf\fP equal to NULL,
in order to complete the conversion of any partially converted input.
Although \fIinbuf\fP and \fIoutbuf\fP are typed as
\fBconst char **\fP and \fBchar **\fP, respectively,
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.
The caller of the \fBiconv\fP function
must ensure that the pointers passed to the function are suitable
for accessing characters in the appropriate character set.
For the encodings \fBUCS-2-INTERNAL\fP, \fBUCS-4-INTERNAL\fP, and \fBwchar_t\fP,
this includes ensuring correct alignment.
.SH "SEE ALSO"
.BR iconv_open (3),
.BR iconvctl (3),
.BR iconv_close (3)