2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
25 .\" This notice shall appear on any product containing this material.
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
43 .\" Copyright (c) 1992, X/Open Company Limited. All Rights Reserved .
44 .\" Portions Copyright (c) 2004, Sun Microsystems, Inc. All Rights Reserved.
46 .TH ICONV 3C "Oct 6, 2004"
48 iconv \- code conversion function
55 \fBextern size_t\fR \fBiconv\fR(\fBiconv_t\fR \fIcd\fR, \fBconst char **restrict\fR \fIinbuf\fR,
56 \fBsize_t *restrict\fR \fIinbytesleft\fR, \fBchar **restrict\fR \fIoutbuf\fR,
57 \fBsize_t *restrict\fR \fIoutbytesleft\fR);
65 \fBsize_t\fR \fBiconv\fR(\fBiconv_t\fR \fIcd\fR, \fBchar **restrict\fR \fIinbuf\fR,
66 \fBsize_t *restrict\fR \fIinbytesleft\fR, \fBchar **restrict\fR \fIoutbuf\fR,
67 \fBsize_t *restrict\fR \fIoutbytesleft\fR);
73 The \fBiconv()\fR function converts the sequence of characters from one code
74 set, in the array specified by \fIinbuf\fR, into a sequence of corresponding
75 characters in another code set, in the array specified by \fIoutbuf\fR. The
76 code sets are those specified in the \fIiconv_open\fR(\|) call that returned
77 the conversion descriptor, \fIcd\fR. The \fIinbuf\fR argument points to a
78 variable that points to the first character in the input buffer and
79 \fIinbytesleft\fR indicates the number of bytes to the end of the buffer to be
80 converted. The \fIoutbuf\fR argument points to a variable that points to the
81 first available byte in the output buffer and \fIoutbytesleft\fR indicates the
82 number of the available bytes to the end of the buffer.
85 For state-dependent encodings, the conversion descriptor \fIcd\fR is placed
86 into its initial shift state by a call for which \fIinbuf\fR is a null pointer,
87 or for which \fIinbuf\fR points to a null pointer. When \fBiconv()\fR is called
88 in this way, and if \fIoutbuf\fR is not a null pointer or a pointer to a null
89 pointer, and \fIoutbytesleft\fR points to a positive value, \fBiconv()\fR will
90 place, into the output buffer, the byte sequence to change the output buffer
91 to its initial shift state. If the output buffer is not large enough to hold
92 the entire reset sequence, \fBiconv()\fR will fail and set \fIerrno\fR to
93 \fBE2BIG\fR. Subsequent calls with \fIinbuf\fR as other than a null pointer or
94 a pointer to a null pointer cause the conversion to take place from the
95 current state of the conversion descriptor.
98 If a sequence of input bytes does not form a valid character in the specified
99 code set, conversion stops after the previous successfully converted
100 character. If the input buffer ends with an incomplete character or shift
101 sequence, conversion stops after the previous successfully converted bytes. If
102 the output buffer is not large enough to hold the entire converted input,
103 conversion stops just prior to the input bytes that would cause the output
104 buffer to overflow. The variable pointed to by \fIinbuf\fR is updated to point
105 to the byte following the last byte successfully used in the conversion. The
106 value pointed to by \fIinbytesleft\fR is decremented to reflect the number of
107 bytes still not converted in the input buffer. The variable pointed to by
108 \fIoutbuf\fR is updated to point to the byte following the last byte of
109 converted output data. The value pointed to by \fIoutbytesleft\fR is
110 decremented to reflect the number of bytes still available in the output
111 buffer. For state-dependent encodings, the conversion descriptor is updated to
112 reflect the shift state in effect at the end of the last successfully
113 converted byte sequence.
116 If \fBiconv()\fR encounters a character in the input buffer that is legal, but
117 for which an identical character does not exist in the target code set,
118 \fBiconv()\fR performs an implementation-defined conversion on this character.
122 The \fBiconv()\fR function updates the variables pointed to by the arguments
123 to reflect the extent of the conversion and returns the number of non-identical
124 conversions performed. If the entire string in the input buffer is converted,
125 the value pointed to by \fIinbytesleft\fR will be \fB0\fR. If the input
126 conversion is stopped due to any conditions mentioned above, the value pointed
127 to by \fIinbytesleft\fR will be non-zero and \fBerrno\fR is set to indicate
128 the condition. If an error occurs \fBiconv()\fR returns \fB(size_t)\fR
129 \fB\(mi1\fR and sets \fIerrno\fR to indicate the error.
133 The \fBiconv()\fR function will fail if:
140 Input conversion stopped due to an input byte that does not belong to the input
150 Input conversion stopped due to lack of space in the output buffer.
159 Input conversion stopped due to an incomplete character or shift sequence at
160 the end of the input buffer.
165 The \fBiconv()\fR function may fail if:
172 The \fIcd\fR argument is not a valid open conversion descriptor.
177 \fBExample 1 \fRUsing the \fBiconv()\fR Functions
180 The following example uses the \fBiconv()\fR functions:
192 * For state-dependent encodings, changes the state of the
193 * conversion descriptor to initial shift state. Also, outputs
194 * the byte sequence to change the state to initial state.
195 * This code is assuming the iconv call for initializing the
196 * state will not fail due to lack of space in the output buffer.
198 #define INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft) \e
204 (void) iconv(cd, &fptr, &ileft, &tptr, &oleft); \e
205 (void) fwrite(to, 1, BUFSIZ - oleft, stdout); \e
209 main(int argc, char **argv)
212 char from[BUFSIZ], to[BUFSIZ];
213 char *from_code, *to_code;
216 size_t ileft, oleft, num, ret;
220 (void) fprintf(stderr,
221 "Usage: %s from_codeset to_codeset\e\en", argv[0]);
228 cd = iconv_open((const char *)to_code, (const char *)from_code);
229 if (cd == (iconv_t)-1) {
233 (void) fprintf(stderr,
234 "iconv_open(%s, %s) failed\e\en", to_code, from_code);
240 (num = fread(from + ileft, 1, BUFSIZ - ileft, stdin))) > 0) {
243 * Input buffer still contains incomplete character
244 * or sequence. However, no more input character.
248 * Initializes the conversion descriptor and outputs
249 * the sequence to change the state to initial state.
251 INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft);
252 (void) iconv_close(cd);
254 (void) fprintf(stderr, "Conversion error\e\en");
263 ret = iconv(cd, &fptr, &ileft, &tptr, &oleft);
264 if (ret != (size_t)-1) {
270 * Outputs converted characters
272 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
279 if (errno == EINVAL) {
281 * Incomplete character or shift sequence
285 * Outputs converted characters
287 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
289 * Copies remaining characters in input buffer
290 * to the top of the input buffer.
292 (void) memmove(from, fptr, ileft);
294 * Tries to fill input buffer from stdin
297 } else if (errno == E2BIG) {
299 * Lack of space in output buffer
303 * Outputs converted characters
305 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
307 * Tries to convert remaining characters in
308 * input buffer with emptied output buffer
311 } else if (errno == EILSEQ) {
313 * Illegal character or shift sequence
317 * Outputs converted characters
319 (void) fwrite(to, 1, BUFSIZ - oleft, stdout);
321 * Initializes the conversion descriptor and
322 * outputs the sequence to change the state to
325 INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft);
326 (void) iconv_close(cd);
328 (void) fprintf(stderr,
329 "Illegal character or sequence\e\en");
331 } else if (errno == EBADF) {
333 * Invalid conversion descriptor.
334 * Actually, this shouldn't happen here.
336 (void) fprintf(stderr, "Conversion error\e\en");
340 * This errno is not defined
342 (void) fprintf(stderr, "iconv error\e\en");
349 * Initializes the conversion descriptor and outputs
350 * the sequence to change the state to initial state.
352 INIT_SHIFT_STATE(cd, fptr, ileft, tptr, oleft);
354 (void) iconv_close(cd);
364 \fB\fB/usr/lib/iconv/*.so\fR\fR
368 conversion modules for 32-bit
374 \fB\fB/usr/lib/iconv/sparcv9/*.so\fR\fR
378 conversion modules for 64-bit sparc
384 \fB\fB/usr/lib/iconv/amd64/*.so\fR\fR
388 conversion modules for 64-bit amd64
394 \fB\fB/usr/lib/iconv/geniconvtbl/binarytables/*.bt\fR\fR
398 conversion binary tables
404 See \fBattributes\fR(5) for descriptions of the following attributes:
412 ATTRIBUTE TYPE ATTRIBUTE VALUE
414 Interface Stability Standard
422 \fBgeniconvtbl\fR(1), \fBiconv\fR(1), \fBiconv_close\fR(3C),
423 \fBiconv_open\fR(3C), \fBgeniconvtbl\fR(4), \fBattributes\fR(5),
424 \fBiconv\fR(5), \fBiconv_unicode\fR(5), \fBstandards\fR(5)