1kiconv(9F) Kernel Functions for Drivers kiconv(9F)
2
3
4
6 kiconv - buffer-based code conversion function
7
9 #include <sys/types.h>
10 #include <sys/errno.h>
11 #include <sys/sunddi.h>
12
13
14
15 size_t kiconv(kiconv_t cd, char **inbuf, size_t *inbytesleft,
16 char **outbuf, size_t *outbytesleft, int *errno);
17
18
20 Solaris DDI specific (Solaris DDI).
21
23 The parameters for the kiconv function are as follows:
24
25 cd Code conversion descriptor indicating the code conver‐
26 sion and conversion state.
27
28
29 inbuf Points to an address of a buffer containing a sequence
30 of character bytes in fromcode codeset to be converted.
31 After the conversion, the variable is updated to point
32 to the byte following the last byte that was success‐
33 fully used in the conversion.
34
35
36 inbytesleft As an input parameter, the number of bytes to be con‐
37 verted in inbuf. As an output parameter, the number of
38 bytes in inbuf still not converted after the conver‐
39 sion.
40
41
42 outbuf Points to an address of a buffer where converted char‐
43 acter bytes in tocode codeset can be saved. After the
44 conversion, the variable is updated to point to the
45 byte following the last byte of converted output data.
46
47
48 outbytesleft As an input parameter, the number of available bytes at
49 outbuf where converted character bytes can be saved. As
50 an output parameter, the number of bytes still avail‐
51 able at outbuf after the conversion.
52
53
54 errno Indicates the error when conversion is not completed or
55 failed. The following are possible values:
56
57 EILSEQ The input conversion was stopped due to an
58 input byte that does not belong to the
59 input codeset.
60
61
62 E2BIG The input conversion was stopped due to
63 lack of space in the output buffer.
64
65
66 EINVAL The input conversion was stopped due to an
67 incomplete character or shift sequence at
68 the end of the input buffer.
69
70
71 EBADF The cd input parameter is not a valid open
72 code conversion descriptor.
73
74
75
77 The kiconv() function converts the sequence of characters from one
78 codeset, in the array specified by inbuf, into a sequence of corre‐
79 sponding characters in another codeset, in the array specified by out‐
80 buf. The codesets are those specified in the kiconv_open() call that
81 returned the code conversion descriptor, cd. The inbuf parameter points
82 to a variable that points to the first character in the input buffer
83 and inbytesleft indicates the number of bytes to the end of the buffer
84 to be converted. The outbuf parameter points to a variable that points
85 to the first available byte in the output buffer and outbytesleft indi‐
86 cates the number of the available bytes to the end of the buffer.
87
88
89 For state-dependent encodings, the conversion descriptor cd is placed
90 into its initial shift state by a call for which inbuf is a null
91 pointer, or for which inbuf points to a null pointer. When kiconv() is
92 called in this way, and if outbuf is not a null pointer or a pointer to
93 a null pointer, and outbytesleft points to a positive value, kiconv()
94 places, if any, into the output buffer, the byte sequence to change the
95 output buffer to its initial shift state. If the output buffer is not
96 large enough to hold the entire reset sequence, kiconv() fails and sets
97 errno to E2BIG. Subsequent calls with inbuf as other than a null
98 pointer or a pointer to a null pointer cause the conversion to take
99 place from the current state of the conversion descriptor.
100
101
102 If a sequence of input bytes does not form a valid character in the
103 specified codeset, conversion stops after the previous successfully
104 converted character. If the input buffer ends with an incomplete char‐
105 acter or shift sequence, conversion stops after the previous success‐
106 fully converted bytes. If the output buffer is not large enough to hold
107 the entire converted input, conversion stops just prior to the input
108 bytes that would cause the output buffer to overflow. The variable
109 pointed to by inbuf is updated to point to the byte following the last
110 byte that was successfully used in the conversion. The value pointed to
111 by inbytesleft is decremented to reflect the number of bytes still not
112 converted in the input buffer. The variable pointed to by outbuf is
113 updated to point to the byte following the last byte of converted out‐
114 put data. The value pointed to by outbytesleft is decremented to
115 reflect the number of bytes still available in the output buffer. For
116 state-dependent encodings, the conversion descriptor is updated to
117 reflect the shift state in effect at the end of the last successfully
118 converted byte sequence.
119
120
121 If kiconv() encounters a character in the input buffer that is legal,
122 but for which an identical character does not exist in the target code‐
123 set, kiconv() performs an implementation-defined conversion (that is, a
124 non-identical conversion) on this character.
125
127 The kiconv() function updates the variables pointed to by the parame‐
128 ters to reflect the extent of the conversion and returns the number of
129 non-identical conversions performed. If the entire string in the input
130 buffer is converted, the value pointed to by inbytesleft is 0. If the
131 input conversion is stopped due to any conditions mentioned above, the
132 value pointed to by inbytesleft is non-zero and errno is set to indi‐
133 cate the condition. If such and other error occurs, kiconv() returns
134 (size_t)-1 and sets errno to indicate the error.
135
137 kiconv() can be called from user or interrupt context.
138
140 Example 1 Performing a Simple Conversion
141
142
143 The following example shows how to perform a simple conversion using
144 kiconv() with a limited size of output buffer:
145
146
147 #include <sys/types.h>
148 #include <sys/errno.h>
149 #include <sys/sunddi.h>
150
151 int
152 doconversion(char *fromcode, char *tocode, char *inbuf, char *outbuf,
153 size_t inlen, size_t *outlen)
154 {
155 kiconv_t cd;
156 size_t ileft, ret;
157 int err;
158
159 cd = kiconv_open((const char *)tocode, (const char *)fromcode);
160 if (cd == (kiconv_t)-1) {
161 /* Cannot open conversion. */
162 return (-1);
163 }
164
165 ret = kiconv(cd, &inbuf, &inlen, &outbuf, outlen, &err);
166 if (ret == (size_t)-1)
167 goto doconv_error_return;
168
169 /*
170 * Reset the conversion descriptor. This will also
171 * make sure to write to output buffer any saved bytes
172 * in the conversion descriptor state.
173 */
174 ileft = 0;
175 ret = kiconv(cd, (char *)NULL, &ileft, &outbuf, outlen, &err);
176 if (ret == (size_t)-1)
177 goto doconv_error_return;
178
179 (void) kiconv_close(cd);
180
181 return (0);
182
183 doconv_error_return:
184 (void) kiconv_close(cd);
185
186 /* Need more output buffer. */
187 if (err == E2BIG)
188 return (-2);
189
190 /* Illegal sequence? */
191 if (err == EILSEQ)
192 return (-3);
193
194 /* Incomplete character? */
195 if (err == EINVAL)
196 return (-4);
197
198 /*
199 * Bad code conversion descriptor or any other unknown error.
200 */
201 return (-5);
202 }
203
204
206 See attributes(5) for descriptions of the following attributes:
207
208
209
210
211 ┌─────────────────────────────┬─────────────────────────────┐
212 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
213 ├─────────────────────────────┼─────────────────────────────┤
214 │Interface Stability │Committed │
215 └─────────────────────────────┴─────────────────────────────┘
216
218 iconv(3C), iconv_close(3C), iconv_open(3C), u8_strcmp(3C),
219 u8_textprep_str(3C), u8_validate(3C), uconv_u16tou32(3C),
220 uconv_u16tou8(3C), uconv_u32tou16(3C), uconv_u32tou8(3C),
221 uconv_u8tou16(3C), uconv_u8tou32(3C), attributes(5), kiconvstr(9F),
222 kiconv_close(9F), kiconv_open(9F), u8_strcmp(9F), u8_textprep_str(9F),
223 u8_validate(9F), uconv_u16tou32(9F), uconv_u16tou8(9F),
224 uconv_u32tou16(9F), uconv_u32tou8(9F), uconv_u8tou16(9F),
225 uconv_u8tou32(9F)
226
227
228 The Unicode Standard:
229
230
231 http://www.unicode.org/standard/standard.html
232
234 The iconv(3C) man page also has a good example code that can be refer‐
235 enced.
236
237
238
239SunOS 5.11 16 Oct 2007 kiconv(9F)