man-pages/man3/iconv.3.html
2021-03-31 01:06:50 +01:00

282 lines
7.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of ICONV</TITLE>
</HEAD><BODY>
<H1>ICONV</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
iconv - perform character set conversion
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/iconv.h">iconv.h</A>&gt;</B>
<B>size_t iconv(iconv_t </B><I>cd</I><B>,</B>
<B> char **</B><I>inbuf</I><B>, size_t *</B><I>inbytesleft</I><B>,</B>
<B> char **</B><I>outbuf</I><B>, size_t *</B><I>outbytesleft</I><B>);</B>
</PRE>
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>iconv</B>()
function converts a sequence of characters in one character encoding
to a sequence of characters in another character encoding.
The
<I>cd</I>
argument is a conversion descriptor,
previously created by a call to
<B><A HREF="/cgi-bin/man/man2html?3+iconv_open">iconv_open</A></B>(3);
the conversion descriptor defines the character encodings that
<B>iconv</B>()
uses for the conversion.
The
<I>inbuf</I>
argument is the address of a variable that points to
the first character of the input sequence;
<I>inbytesleft</I>
indicates the number of bytes in that buffer.
The
<I>outbuf</I>
argument is the address of a variable that points to
the first byte available in the output buffer;
<I>outbytesleft</I>
indicates the number of bytes available in the output buffer.
<P>
The main case is when <I>inbuf</I> is not NULL and <I>*inbuf</I> is not NULL.
In this case, the
<B>iconv</B>()
function converts the multibyte sequence
starting at <I>*inbuf</I> to a multibyte sequence starting at <I>*outbuf</I>.
At most <I>*inbytesleft</I> bytes, starting at <I>*inbuf</I>, will be read.
At most <I>*outbytesleft</I> bytes, starting at <I>*outbuf</I>, will be written.
<P>
The
<B>iconv</B>()
function converts one multibyte character at a time, and for
each character conversion it increments <I>*inbuf</I> and decrements
<I>*inbytesleft</I> by the number of converted input bytes, it increments
<I>*outbuf</I> and decrements <I>*outbytesleft</I> by the number of converted
output bytes, and it updates the conversion state contained in <I>cd</I>.
If the character encoding of the input is stateful, the
<B>iconv</B>()
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 <I>shift sequence</I>.
The conversion can stop for four reasons:
<DL COMPACT>
<DT id="1">1.<DD>
An invalid multibyte sequence is encountered in the input.
In this case,
it sets <I>errno</I> to <B>EILSEQ</B> and returns
<I>(size_t)&nbsp;-1</I>.
<I>*inbuf</I>
is left pointing to the beginning of the invalid multibyte sequence.
<DT id="2">2.<DD>
The input byte sequence has been entirely converted,
that is, <I>*inbytesleft</I> has gone down to 0.
In this case,
<B>iconv</B>()
returns the number of
nonreversible conversions performed during this call.
<DT id="3">3.<DD>
An incomplete multibyte sequence is encountered in the input, and the
input byte sequence terminates after it.
In this case, it sets <I>errno</I> to
<B>EINVAL</B> and returns
<I>(size_t)&nbsp;-1</I>.
<I>*inbuf</I> is left pointing to the
beginning of the incomplete multibyte sequence.
<DT id="4">4.<DD>
The output buffer has no more room for the next converted character.
In this case, it sets <I>errno</I> to <B>E2BIG</B> and returns
<I>(size_t)&nbsp;-1</I>.
</DL>
<P>
A different case is when <I>inbuf</I> is NULL or <I>*inbuf</I> is NULL, but
<I>outbuf</I> is not NULL and <I>*outbuf</I> is not NULL.
In this case, the
<B>iconv</B>()
function attempts to set <I>cd</I>'s conversion state to the
initial state and store a corresponding shift sequence at <I>*outbuf</I>.
At most <I>*outbytesleft</I> bytes, starting at <I>*outbuf</I>, will be written.
If the output buffer has no more room for this reset sequence, it sets
<I>errno</I> to <B>E2BIG</B> and returns
<I>(size_t)&nbsp;-1</I>.
Otherwise, it increments
<I>*outbuf</I> and decrements <I>*outbytesleft</I> by the number of bytes
written.
<P>
A third case is when <I>inbuf</I> is NULL or <I>*inbuf</I> is NULL, and
<I>outbuf</I> is NULL or <I>*outbuf</I> is NULL.
In this case, the
<B>iconv</B>()
function sets <I>cd</I>'s conversion state to the initial state.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>iconv</B>()
function returns the number of characters converted in a
nonreversible way during this call; reversible conversions are not counted.
In case of error, it sets <I>errno</I> and returns
<I>(size_t)&nbsp;-1</I>.
<A NAME="lbAF">&nbsp;</A>
<H2>ERRORS</H2>
The following errors can occur, among others:
<DL COMPACT>
<DT id="5"><B>E2BIG</B>
<DD>
There is not sufficient room at <I>*outbuf</I>.
<DT id="6"><B>EILSEQ</B>
<DD>
An invalid multibyte sequence has been encountered in the input.
<DT id="7"><B>EINVAL</B>
<DD>
An incomplete multibyte sequence has been encountered in the input.
</DL>
<A NAME="lbAG">&nbsp;</A>
<H2>VERSIONS</H2>
This function is available in glibc since version 2.1.
<A NAME="lbAH">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>iconv</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe race:cd<BR></TD></TR>
</TABLE>
<P>
The
<B>iconv</B>()
function is MT-Safe, as long as callers arrange for
mutual exclusion on the
<I>cd</I>
argument.
<A NAME="lbAI">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAJ">&nbsp;</A>
<H2>NOTES</H2>
In each series of calls to
<B>iconv</B>(),
the last should be one with <I>inbuf</I> or <I>*inbuf</I> equal to NULL,
in order to flush out any partially converted input.
<P>
Although
<I>inbuf</I>
and
<I>outbuf</I>
are typed as
<I>char&nbsp;**</I>,
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
<B>iconv</B>()
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.
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+iconv_close">iconv_close</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+iconv_open">iconv_open</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?8+iconvconfig">iconvconfig</A></B>(8)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="8"><A HREF="#lbAB">NAME</A><DD>
<DT id="9"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="10"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="11"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="12"><A HREF="#lbAF">ERRORS</A><DD>
<DT id="13"><A HREF="#lbAG">VERSIONS</A><DD>
<DT id="14"><A HREF="#lbAH">ATTRIBUTES</A><DD>
<DT id="15"><A HREF="#lbAI">CONFORMING TO</A><DD>
<DT id="16"><A HREF="#lbAJ">NOTES</A><DD>
<DT id="17"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="18"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:46 GMT, March 31, 2021
</BODY>
</HTML>