Kernel Functions for Drivers kiconv(9F)
NAME
kiconv - buffer-based code conversion function
SYNOPSIS
#include
#include
#include
sizet kiconv(kiconvt cd, char **inbuf, sizet *inbytesleft,
char **outbuf, sizet *outbytesleft, int *errno);
INTERFACE LEVEL
Solaris DI specific (Solaris DI).
PARAMETERS
The parameters for the kiconv function are as follows:
cd Code conversion descriptor indicating the
code conversion and conversion state.
inbuf Points to an address of a buffer containing
a sequence of character bytes in fromcode
codeset to be converted. After the conver-
sion, the variable is updated to point to
the byte following the last byte that was
successfully used in the conversion.
inbytesleft 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.
outbuf Points to an address of a buffer where con-
verted 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.
outbytesleft As an input parameter, the number of avail-
able bytes at outbuf where converted charac-
ter bytes can be saved. As an output parame-
ter, the number of bytes still available at
outbuf after the conversion.
SunOS 5.11 Last change: 16 Oct 2007 1
Kernel Functions for Drivers kiconv(9F)
errno Indicates the error when conversion is not
completed or failed. The following are pos-
sible values:
EILSEQ The input conversion was
stopped due to an input byte
that does not belong to the
input codeset.
E2BIG The input conversion was
stopped due to lack of space in
the output buffer.
EINVAL The input conversion was
stopped due to an incomplete
character or shift sequence at
the end of the input buffer.
EBADF The cd input parameter is not a
valid open code conversion
descriptor.
DESCRIPTION
The kiconv() function converts the sequence of characters
from one codeset, in the array specified by inbuf, into a
sequence of corresponding characters in another codeset, in
the array specified by outbuf. The codesets are those speci-
fied in the kiconvopen() call that returned the code
conversion descriptor, cd. The inbuf parameter points to a
variable that points to the first character in the input
buffer and inbytesleft indicates the number of bytes to the
end of the buffer to be converted. The outbuf parameter
points to a variable that points to the first available byte
in the output buffer and outbytesleft indicates the number
of the available bytes to the end of the buffer.
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
SunOS 5.11 Last change: 16 Oct 2007 2
Kernel Functions for Drivers kiconv(9F)
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 previ-
ous successfully converted character. If the input buffer
ends with an incomplete character or shift sequence, conver-
sion 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 decre-
mented 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 con-
verted 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.
RETURN VALUES
The kiconv() function updates the variables pointed to by
the parameters to reflect the extent of the conversion and
returns the number of non-identical conversions performed.
If the entire string in the input buffer is converted, the
value pointed to by inbytesleft is 0. If the input conver-
sion is stopped due to any conditions mentioned above, the
value pointed to by inbytesleft is non-zero and errno is set
to indicate the condition. If such and other error occurs,
kiconv() returns (sizet)-1 and sets errno to indicate the
error.
CONTEXT
kiconv() can be called from user or interrupt context.
EXAMPLES
Example 1 Performing a Simple Conversion
SunOS 5.11 Last change: 16 Oct 2007 3
Kernel Functions for Drivers kiconv(9F)
The following example shows how to perform a simple conver-
sion using kiconv() with a limited size of output buffer:
#include
#include
#include
int
doconversion(char *fromcode, char *tocode, char *inbuf, char *outbuf,
sizet inlen, sizet *outlen)
{
kiconvt cd;
sizet ileft, ret;
int err;
cd = kiconvopen((const char *)tocode, (const char *)fromcode);
if (cd == (kiconvt)-1) {
/* Cannot open conversion. */
return (-1);
}
ret = kiconv(cd, &inbuf, &inlen, &outbuf, outlen, &err);
if (ret == (sizet)-1)
goto doconverrorreturn;
/*
* 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 *)NUL, &ileft, &outbuf, outlen, &err);
if (ret == (sizet)-1)
goto doconverrorreturn;
(void) kiconvclose(cd);
return (0);
doconverrorreturn:
(void) kiconvclose(cd);
/* Need more output buffer. */
if (err == E2BIG)
return (-2);
/* Illegal sequence? */
if (err == EILSEQ)
return (-3);
/* Incomplete character? */
SunOS 5.11 Last change: 16 Oct 2007 4
Kernel Functions for Drivers kiconv(9F)
if (err == EINVAL)
return (-4);
/*
* Bad code conversion descriptor or any other unknown error.
*/
return (-5);
}
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
ATRIBUTE TYPE ATRIBUTE VALUE
Interface Stability Committed
SEE ALSO
iconv(3C), iconvclose(3C), iconvopen(3C), u8strcmp(3C),
u8textprepstr(3C), u8validate(3C), uconvu16tou32(3C),
uconvu16tou8(3C), uconvu32tou16(3C), uconvu32tou8(3C),
uconvu8tou16(3C), uconvu8tou32(3C), attributes(5),
kiconvstr(9F), kiconvclose(9F), kiconvopen(9F),
u8strcmp(9F), u8textprepstr(9F), u8validate(9F),
uconvu16tou32(9F), uconvu16tou8(9F), uconvu32tou16(9F),
uconvu32tou8(9F), uconvu8tou16(9F), uconvu8tou32(9F)
The Unicode Standard:
http:/www.unicode.org/standard/standard.html
NOTES
The iconv(3C) man page also has a good example code that can
be referenced.
SunOS 5.11 Last change: 16 Oct 2007 5
|