Copyright (c) 2007, Sun Microsystems, Inc., All Rights Reserved
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
#include <sys/types.h> #include <sys/errno.h> #include <sys/sunddi.h> size_t kiconv(kiconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft, int *errno);
Code conversion descriptor indicating the code conversion and conversion state.
Points to an address of a buffer containing a sequence of character bytes in fromcode codeset to be converted. After the conversion, the variable is updated to point to the byte following the last byte that was successfully used in the conversion.
As an input parameter, the number of bytes to be converted in inbuf. As an output parameter, the number of bytes in inbuf still not converted after the conversion.
Points to an address of a buffer where converted character bytes in tocode codeset can be saved. After the conversion, the variable is updated to point to the byte following the last byte of converted output data.
As an input parameter, the number of available bytes at outbuf where converted character bytes can be saved. As an output parameter, the number of bytes still available at outbuf after the conversion.
Indicates the error when conversion is not completed or failed. The following are possible values: EILSEQ
The input conversion was stopped due to an input byte that does not belong to the input codeset.
The input conversion was stopped due to lack of space in the output buffer.
The input conversion was stopped due to an incomplete character or shift sequence at the end of the input buffer.
The cd input parameter is not a valid open code conversion descriptor.
For state-dependent encodings, the conversion descriptor cd is placed into its initial shift state by a call for which inbuf is a null pointer, or for which inbuf points to a null pointer. When kiconv() is called in this way, and if outbuf is not a null pointer or a pointer to a null pointer, and outbytesleft points to a positive value, kiconv() places, if any, into the output buffer, the byte sequence to change the output buffer to its initial shift state. If the output buffer is not large enough to hold the entire reset sequence, kiconv() fails and sets errno to E2BIG. Subsequent calls with inbuf as other than a null pointer or a pointer to a null pointer cause the conversion to take place from the current state of the conversion descriptor.
If a sequence of input bytes does not form a valid character in the specified codeset, conversion stops after the previous successfully converted character. If the input buffer ends with an incomplete character or shift sequence, conversion stops after the previous successfully converted bytes. If the output buffer is not large enough to hold the entire converted input, conversion stops just prior to the input bytes that would cause the output buffer to overflow. The variable pointed to by inbuf is updated to point to the byte following the last byte that was successfully used in the conversion. The value pointed to by inbytesleft is decremented to reflect the number of bytes still not converted in the input buffer. The variable pointed to by outbuf is updated to point to the byte following the last byte of converted output data. The value pointed to by outbytesleft is decremented to reflect the number of bytes still available in the output buffer. For state-dependent encodings, the conversion descriptor is updated to reflect the shift state in effect at the end of the last successfully converted byte sequence.
If kiconv() encounters a character in the input buffer that is legal, but for which an identical character does not exist in the target codeset, kiconv() performs an implementation-defined conversion (that is, a non-identical conversion) on this character.
The following example shows how to perform a simple conversion using kiconv() with a limited size of output buffer:
#include <sys/types.h> #include <sys/errno.h> #include <sys/sunddi.h> int doconversion(char *fromcode, char *tocode, char *inbuf, char *outbuf, size_t inlen, size_t *outlen) { kiconv_t cd; size_t ileft, ret; int err; cd = kiconv_open((const char *)tocode, (const char *)fromcode); if (cd == (kiconv_t)-1) { /* Cannot open conversion. */ return (-1); } ret = kiconv(cd, &inbuf, &inlen, &outbuf, outlen, &err); if (ret == (size_t)-1) goto doconv_error_return; /* * Reset the conversion descriptor. This will also * make sure to write to output buffer any saved bytes * in the conversion descriptor state. */ ileft = 0; ret = kiconv(cd, (char *)NULL, &ileft, &outbuf, outlen, &err); if (ret == (size_t)-1) goto doconv_error_return; (void) kiconv_close(cd); return (0); doconv_error_return: (void) kiconv_close(cd); /* Need more output buffer. */ if (err == E2BIG) return (-2); /* Illegal sequence? */ if (err == EILSEQ) return (-3); /* Incomplete character? */ if (err == EINVAL) return (-4); /* * Bad code conversion descriptor or any other unknown error. */ return (-5); }
ATTRIBUTE TYPE ATTRIBUTE VALUE |
Interface Stability Committed |
The Unicode Standard:
http://www.unicode.org/standard/standard.html