| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | minimal iconv implementation
|
|---|
| 4 | Copyright (C) Andrew Tridgell 2001
|
|---|
| 5 | Copyright (C) Jelmer Vernooij 2002,2003
|
|---|
| 6 |
|
|---|
| 7 | This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 10 | (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | This program is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | GNU General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #include "includes.h"
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * We have to use strcasecmp here as the character conversions
|
|---|
| 25 | * haven't been initialised yet. JRA.
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | #undef strcasecmp
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * @file
|
|---|
| 32 | *
|
|---|
| 33 | * @brief Samba wrapper/stub for iconv character set conversion.
|
|---|
| 34 | *
|
|---|
| 35 | * iconv is the XPG2 interface for converting between character
|
|---|
| 36 | * encodings. This file provides a Samba wrapper around it, and also
|
|---|
| 37 | * a simple reimplementation that is used if the system does not
|
|---|
| 38 | * implement iconv.
|
|---|
| 39 | *
|
|---|
| 40 | * Samba only works with encodings that are supersets of ASCII: ascii
|
|---|
| 41 | * characters like whitespace can be tested for directly, multibyte
|
|---|
| 42 | * sequences start with a byte with the high bit set, and strings are
|
|---|
| 43 | * terminated by a nul byte.
|
|---|
| 44 | *
|
|---|
| 45 | * Note that the only function provided by iconv is conversion between
|
|---|
| 46 | * characters. It doesn't directly support operations like
|
|---|
| 47 | * uppercasing or comparison. We have to convert to UCS-2 and compare
|
|---|
| 48 | * there.
|
|---|
| 49 | *
|
|---|
| 50 | * @sa Samba Developers Guide
|
|---|
| 51 | **/
|
|---|
| 52 |
|
|---|
| 53 | static_decl_charset;
|
|---|
| 54 |
|
|---|
| 55 | static size_t ascii_pull(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 56 | static size_t ascii_push(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 57 | static size_t latin1_push(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 58 | static size_t utf8_pull(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 59 | static size_t utf8_push(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 60 | static size_t ucs2hex_pull(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 61 | static size_t ucs2hex_push(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 62 | static size_t iconv_copy(void *,const char **, size_t *, char **, size_t *);
|
|---|
| 63 | static size_t iconv_swab (void *,const char **, size_t *, char **, size_t *);
|
|---|
| 64 |
|
|---|
| 65 | static struct charset_functions builtin_functions[] = {
|
|---|
| 66 | /* windows is really neither UCS-2 not UTF-16 */
|
|---|
| 67 | {"UCS-2LE", iconv_copy, iconv_copy},
|
|---|
| 68 | {"UTF-16LE", iconv_copy, iconv_copy},
|
|---|
| 69 | {"UCS-2BE", iconv_swab, iconv_swab},
|
|---|
| 70 | {"UTF-16BE", iconv_swab, iconv_swab},
|
|---|
| 71 |
|
|---|
| 72 | /* we include the UTF-8 alias to cope with differing locale settings */
|
|---|
| 73 | {"UTF8", utf8_pull, utf8_push},
|
|---|
| 74 | {"UTF-8", utf8_pull, utf8_push},
|
|---|
| 75 | {"ASCII", ascii_pull, ascii_push},
|
|---|
| 76 | {"646", ascii_pull, ascii_push},
|
|---|
| 77 | {"ISO-8859-1", ascii_pull, latin1_push},
|
|---|
| 78 | {"UCS2-HEX", ucs2hex_pull, ucs2hex_push},
|
|---|
| 79 | {NULL, NULL, NULL}
|
|---|
| 80 | };
|
|---|
| 81 |
|
|---|
| 82 | static struct charset_functions *charsets = NULL;
|
|---|
| 83 |
|
|---|
| 84 | static struct charset_functions *find_charset_functions(const char *name)
|
|---|
| 85 | {
|
|---|
| 86 | struct charset_functions *c = charsets;
|
|---|
| 87 |
|
|---|
| 88 | while(c) {
|
|---|
| 89 | if (strcasecmp(name, c->name) == 0) {
|
|---|
| 90 | return c;
|
|---|
| 91 | }
|
|---|
| 92 | c = c->next;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | return NULL;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | NTSTATUS smb_register_charset(struct charset_functions *funcs)
|
|---|
| 99 | {
|
|---|
| 100 | if (!funcs) {
|
|---|
| 101 | return NT_STATUS_INVALID_PARAMETER;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | DEBUG(5, ("Attempting to register new charset %s\n", funcs->name));
|
|---|
| 105 | /* Check whether we already have this charset... */
|
|---|
| 106 | if (find_charset_functions(funcs->name)) {
|
|---|
| 107 | DEBUG(0, ("Duplicate charset %s, not registering\n", funcs->name));
|
|---|
| 108 | return NT_STATUS_OBJECT_NAME_COLLISION;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | funcs->next = funcs->prev = NULL;
|
|---|
| 112 | DEBUG(5, ("Registered charset %s\n", funcs->name));
|
|---|
| 113 | DLIST_ADD(charsets, funcs);
|
|---|
| 114 | return NT_STATUS_OK;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | static void lazy_initialize_iconv(void)
|
|---|
| 118 | {
|
|---|
| 119 | static bool initialized;
|
|---|
| 120 | int i;
|
|---|
| 121 |
|
|---|
| 122 | if (!initialized) {
|
|---|
| 123 | initialized = True;
|
|---|
| 124 | for(i = 0; builtin_functions[i].name; i++)
|
|---|
| 125 | smb_register_charset(&builtin_functions[i]);
|
|---|
| 126 | static_init_charset;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | #ifdef __OS2__
|
|---|
| 131 | // i could have done a static variable w/o this function. but i feel it's nicer this way.
|
|---|
| 132 | // the purpose of this function is to save the to_name to get the korean and japanese code set working
|
|---|
| 133 | char * save_toname(char *toname, bool what)
|
|---|
| 134 | {
|
|---|
| 135 | static char *to_name=NULL;
|
|---|
| 136 |
|
|---|
| 137 | if ( what == 0 )
|
|---|
| 138 | to_name = SMB_STRDUP(toname);
|
|---|
| 139 |
|
|---|
| 140 | return to_name;
|
|---|
| 141 | }
|
|---|
| 142 | #endif
|
|---|
| 143 |
|
|---|
| 144 | #ifdef HAVE_NATIVE_ICONV
|
|---|
| 145 | /* if there was an error then reset the internal state,
|
|---|
| 146 | this ensures that we don't have a shift state remaining for
|
|---|
| 147 | character sets like SJIS */
|
|---|
| 148 | static size_t sys_iconv(void *cd,
|
|---|
| 149 | const char **inbuf, size_t *inbytesleft,
|
|---|
| 150 | char **outbuf, size_t *outbytesleft)
|
|---|
| 151 | {
|
|---|
| 152 | #ifdef __OS2__
|
|---|
| 153 | uint16 *outbuf_uc = ( uint16 * )*outbuf;
|
|---|
| 154 | char *to_name = save_toname(NULL, 1);
|
|---|
| 155 | #endif
|
|---|
| 156 |
|
|---|
| 157 | size_t ret = iconv((iconv_t)cd,
|
|---|
| 158 | (void *)inbuf, inbytesleft,
|
|---|
| 159 | outbuf, outbytesleft);
|
|---|
| 160 | if (ret == (size_t)-1) {
|
|---|
| 161 | int saved_errno = errno;
|
|---|
| 162 | iconv(cd, NULL, NULL, NULL, NULL);
|
|---|
| 163 | errno = saved_errno;
|
|---|
| 164 | }
|
|---|
| 165 | #ifdef __OS2__
|
|---|
| 166 | /* Workaround for path separator on OS/2 */
|
|---|
| 167 | else
|
|---|
| 168 | {
|
|---|
| 169 | if( (strstr(to_name, "949") != NULL) || /* Korean CP */
|
|---|
| 170 | (strstr(to_name, "932") != NULL) || /* Japanese CP */
|
|---|
| 171 | (strstr(to_name, "942") != NULL) || /* Japanese CP */
|
|---|
| 172 | (strstr(to_name, "943") != NULL) ) /* Japanese CP */
|
|---|
| 173 | {
|
|---|
| 174 | while(( char * )outbuf_uc < *outbuf )
|
|---|
| 175 | {
|
|---|
| 176 | if( *outbuf_uc == 0x20a9 || /* Korean WON */
|
|---|
| 177 | *outbuf_uc == 0x00a5 ) /* Japanese YEN */
|
|---|
| 178 | *outbuf_uc = '\\';
|
|---|
| 179 |
|
|---|
| 180 | outbuf_uc++;
|
|---|
| 181 | }
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 | #endif
|
|---|
| 185 |
|
|---|
| 186 | return ret;
|
|---|
| 187 | }
|
|---|
| 188 | #endif
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * This is a simple portable iconv() implementaion.
|
|---|
| 192 | *
|
|---|
| 193 | * It only knows about a very small number of character sets - just
|
|---|
| 194 | * enough that Samba works on systems that don't have iconv.
|
|---|
| 195 | **/
|
|---|
| 196 | size_t smb_iconv(smb_iconv_t cd,
|
|---|
| 197 | const char **inbuf, size_t *inbytesleft,
|
|---|
| 198 | char **outbuf, size_t *outbytesleft)
|
|---|
| 199 | {
|
|---|
| 200 | char cvtbuf[2048];
|
|---|
| 201 | char *bufp = cvtbuf;
|
|---|
| 202 | size_t bufsize;
|
|---|
| 203 | #ifdef __OS2__
|
|---|
| 204 | save_toname(cd->to_name, 0);
|
|---|
| 205 | #endif
|
|---|
| 206 |
|
|---|
| 207 | /* in many cases we can go direct */
|
|---|
| 208 | if (cd->direct) {
|
|---|
| 209 | return cd->direct(cd->cd_direct,
|
|---|
| 210 | inbuf, inbytesleft, outbuf, outbytesleft);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 | /* otherwise we have to do it chunks at a time */
|
|---|
| 215 | while (*inbytesleft > 0) {
|
|---|
| 216 | bufp = cvtbuf;
|
|---|
| 217 | bufsize = sizeof(cvtbuf);
|
|---|
| 218 |
|
|---|
| 219 | if (cd->pull(cd->cd_pull,
|
|---|
| 220 | inbuf, inbytesleft, &bufp, &bufsize) == -1
|
|---|
| 221 | && errno != E2BIG) return -1;
|
|---|
| 222 |
|
|---|
| 223 | bufp = cvtbuf;
|
|---|
| 224 | bufsize = sizeof(cvtbuf) - bufsize;
|
|---|
| 225 |
|
|---|
| 226 | if (cd->push(cd->cd_push,
|
|---|
| 227 | (const char **)&bufp, &bufsize,
|
|---|
| 228 | outbuf, outbytesleft) == -1) return -1;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | return 0;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 | static bool is_utf16(const char *name)
|
|---|
| 236 | {
|
|---|
| 237 | return strcasecmp(name, "UCS-2LE") == 0 ||
|
|---|
| 238 | strcasecmp(name, "UTF-16LE") == 0;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /*
|
|---|
| 242 | simple iconv_open() wrapper
|
|---|
| 243 | */
|
|---|
| 244 | smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
|
|---|
| 245 | {
|
|---|
| 246 | smb_iconv_t ret;
|
|---|
| 247 | struct charset_functions *from, *to;
|
|---|
| 248 |
|
|---|
| 249 | lazy_initialize_iconv();
|
|---|
| 250 | from = charsets;
|
|---|
| 251 | to = charsets;
|
|---|
| 252 |
|
|---|
| 253 | ret = SMB_MALLOC_P(struct smb_iconv_s);
|
|---|
| 254 | if (!ret) {
|
|---|
| 255 | errno = ENOMEM;
|
|---|
| 256 | return (smb_iconv_t)-1;
|
|---|
| 257 | }
|
|---|
| 258 | memset(ret, 0, sizeof(struct smb_iconv_s));
|
|---|
| 259 |
|
|---|
| 260 | ret->from_name = SMB_STRDUP(fromcode);
|
|---|
| 261 | ret->to_name = SMB_STRDUP(tocode);
|
|---|
| 262 |
|
|---|
| 263 | /* check for the simplest null conversion */
|
|---|
| 264 | if (strcasecmp(fromcode, tocode) == 0) {
|
|---|
| 265 | ret->direct = iconv_copy;
|
|---|
| 266 | return ret;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /* check if we have a builtin function for this conversion */
|
|---|
| 270 | from = find_charset_functions(fromcode);
|
|---|
| 271 | if(from)ret->pull = from->pull;
|
|---|
| 272 |
|
|---|
| 273 | to = find_charset_functions(tocode);
|
|---|
| 274 | if(to)ret->push = to->push;
|
|---|
| 275 |
|
|---|
| 276 | /* check if we can use iconv for this conversion */
|
|---|
| 277 | #ifdef HAVE_NATIVE_ICONV
|
|---|
| 278 | if (!ret->pull) {
|
|---|
| 279 | ret->cd_pull = iconv_open("UTF-16LE", fromcode);
|
|---|
| 280 | if (ret->cd_pull == (iconv_t)-1)
|
|---|
| 281 | ret->cd_pull = iconv_open("UCS-2LE", fromcode);
|
|---|
| 282 | if (ret->cd_pull != (iconv_t)-1)
|
|---|
| 283 | ret->pull = sys_iconv;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | if (!ret->push) {
|
|---|
| 287 | ret->cd_push = iconv_open(tocode, "UTF-16LE");
|
|---|
| 288 | if (ret->cd_push == (iconv_t)-1)
|
|---|
| 289 | ret->cd_push = iconv_open(tocode, "UCS-2LE");
|
|---|
| 290 | if (ret->cd_push != (iconv_t)-1)
|
|---|
| 291 | ret->push = sys_iconv;
|
|---|
| 292 | }
|
|---|
| 293 | #endif
|
|---|
| 294 |
|
|---|
| 295 | /* check if there is a module available that can do this conversion */
|
|---|
| 296 | if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
|
|---|
| 297 | if(!(from = find_charset_functions(fromcode)))
|
|---|
| 298 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
|
|---|
| 299 | else
|
|---|
| 300 | ret->pull = from->pull;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
|
|---|
| 304 | if(!(to = find_charset_functions(tocode)))
|
|---|
| 305 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
|
|---|
| 306 | else
|
|---|
| 307 | ret->push = to->push;
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | if (!ret->push || !ret->pull) {
|
|---|
| 311 | SAFE_FREE(ret->from_name);
|
|---|
| 312 | SAFE_FREE(ret->to_name);
|
|---|
| 313 | SAFE_FREE(ret);
|
|---|
| 314 | errno = EINVAL;
|
|---|
| 315 | return (smb_iconv_t)-1;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | /* check for conversion to/from ucs2 */
|
|---|
| 319 | if (is_utf16(fromcode) && to) {
|
|---|
| 320 | ret->direct = to->push;
|
|---|
| 321 | ret->push = ret->pull = NULL;
|
|---|
| 322 | return ret;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | if (is_utf16(tocode) && from) {
|
|---|
| 326 | ret->direct = from->pull;
|
|---|
| 327 | ret->push = ret->pull = NULL;
|
|---|
| 328 | return ret;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | /* Check if we can do the conversion direct */
|
|---|
| 332 | #ifdef HAVE_NATIVE_ICONV
|
|---|
| 333 | if (is_utf16(fromcode)) {
|
|---|
| 334 | ret->direct = sys_iconv;
|
|---|
| 335 | ret->cd_direct = ret->cd_push;
|
|---|
| 336 | ret->cd_push = NULL;
|
|---|
| 337 | return ret;
|
|---|
| 338 | }
|
|---|
| 339 | if (is_utf16(tocode)) {
|
|---|
| 340 | ret->direct = sys_iconv;
|
|---|
| 341 | ret->cd_direct = ret->cd_pull;
|
|---|
| 342 | ret->cd_pull = NULL;
|
|---|
| 343 | return ret;
|
|---|
| 344 | }
|
|---|
| 345 | #endif
|
|---|
| 346 |
|
|---|
| 347 | return ret;
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /*
|
|---|
| 351 | simple iconv_close() wrapper
|
|---|
| 352 | */
|
|---|
| 353 | int smb_iconv_close (smb_iconv_t cd)
|
|---|
| 354 | {
|
|---|
| 355 | #ifdef HAVE_NATIVE_ICONV
|
|---|
| 356 | if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
|
|---|
| 357 | if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
|
|---|
| 358 | if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
|
|---|
| 359 | #endif
|
|---|
| 360 |
|
|---|
| 361 | SAFE_FREE(cd->from_name);
|
|---|
| 362 | SAFE_FREE(cd->to_name);
|
|---|
| 363 |
|
|---|
| 364 | memset(cd, 0, sizeof(*cd));
|
|---|
| 365 | SAFE_FREE(cd);
|
|---|
| 366 | return 0;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 | /**********************************************************************
|
|---|
| 371 | the following functions implement the builtin character sets in Samba
|
|---|
| 372 | and also the "test" character sets that are designed to test
|
|---|
| 373 | multi-byte character set support for english users
|
|---|
| 374 | ***********************************************************************/
|
|---|
| 375 |
|
|---|
| 376 | static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 377 | char **outbuf, size_t *outbytesleft)
|
|---|
| 378 | {
|
|---|
| 379 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
|---|
| 380 | (*outbuf)[0] = (*inbuf)[0];
|
|---|
| 381 | (*outbuf)[1] = 0;
|
|---|
| 382 | (*inbytesleft) -= 1;
|
|---|
| 383 | (*outbytesleft) -= 2;
|
|---|
| 384 | (*inbuf) += 1;
|
|---|
| 385 | (*outbuf) += 2;
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | if (*inbytesleft > 0) {
|
|---|
| 389 | errno = E2BIG;
|
|---|
| 390 | return -1;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | return 0;
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 397 | char **outbuf, size_t *outbytesleft)
|
|---|
| 398 | {
|
|---|
| 399 | int ir_count=0;
|
|---|
| 400 |
|
|---|
| 401 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
|---|
| 402 | (*outbuf)[0] = (*inbuf)[0] & 0x7F;
|
|---|
| 403 | if ((*inbuf)[1]) ir_count++;
|
|---|
| 404 | (*inbytesleft) -= 2;
|
|---|
| 405 | (*outbytesleft) -= 1;
|
|---|
| 406 | (*inbuf) += 2;
|
|---|
| 407 | (*outbuf) += 1;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | if (*inbytesleft == 1) {
|
|---|
| 411 | errno = EINVAL;
|
|---|
| 412 | return -1;
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | if (*inbytesleft > 1) {
|
|---|
| 416 | errno = E2BIG;
|
|---|
| 417 | return -1;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | return ir_count;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 424 | char **outbuf, size_t *outbytesleft)
|
|---|
| 425 | {
|
|---|
| 426 | int ir_count=0;
|
|---|
| 427 |
|
|---|
| 428 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
|---|
| 429 | (*outbuf)[0] = (*inbuf)[0];
|
|---|
| 430 | if ((*inbuf)[1]) ir_count++;
|
|---|
| 431 | (*inbytesleft) -= 2;
|
|---|
| 432 | (*outbytesleft) -= 1;
|
|---|
| 433 | (*inbuf) += 2;
|
|---|
| 434 | (*outbuf) += 1;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | if (*inbytesleft == 1) {
|
|---|
| 438 | errno = EINVAL;
|
|---|
| 439 | return -1;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | if (*inbytesleft > 1) {
|
|---|
| 443 | errno = E2BIG;
|
|---|
| 444 | return -1;
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | return ir_count;
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 451 | char **outbuf, size_t *outbytesleft)
|
|---|
| 452 | {
|
|---|
| 453 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
|---|
| 454 | unsigned v;
|
|---|
| 455 |
|
|---|
| 456 | if ((*inbuf)[0] != '@') {
|
|---|
| 457 | /* seven bit ascii case */
|
|---|
| 458 | (*outbuf)[0] = (*inbuf)[0];
|
|---|
| 459 | (*outbuf)[1] = 0;
|
|---|
| 460 | (*inbytesleft) -= 1;
|
|---|
| 461 | (*outbytesleft) -= 2;
|
|---|
| 462 | (*inbuf) += 1;
|
|---|
| 463 | (*outbuf) += 2;
|
|---|
| 464 | continue;
|
|---|
| 465 | }
|
|---|
| 466 | /* it's a hex character */
|
|---|
| 467 | if (*inbytesleft < 5) {
|
|---|
| 468 | errno = EINVAL;
|
|---|
| 469 | return -1;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
|
|---|
| 473 | errno = EILSEQ;
|
|---|
| 474 | return -1;
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | (*outbuf)[0] = v&0xff;
|
|---|
| 478 | (*outbuf)[1] = v>>8;
|
|---|
| 479 | (*inbytesleft) -= 5;
|
|---|
| 480 | (*outbytesleft) -= 2;
|
|---|
| 481 | (*inbuf) += 5;
|
|---|
| 482 | (*outbuf) += 2;
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | if (*inbytesleft > 0) {
|
|---|
| 486 | errno = E2BIG;
|
|---|
| 487 | return -1;
|
|---|
| 488 | }
|
|---|
| 489 |
|
|---|
| 490 | return 0;
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 494 | char **outbuf, size_t *outbytesleft)
|
|---|
| 495 | {
|
|---|
| 496 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
|---|
| 497 | char buf[6];
|
|---|
| 498 |
|
|---|
| 499 | if ((*inbuf)[1] == 0 &&
|
|---|
| 500 | ((*inbuf)[0] & 0x80) == 0 &&
|
|---|
| 501 | (*inbuf)[0] != '@') {
|
|---|
| 502 | (*outbuf)[0] = (*inbuf)[0];
|
|---|
| 503 | (*inbytesleft) -= 2;
|
|---|
| 504 | (*outbytesleft) -= 1;
|
|---|
| 505 | (*inbuf) += 2;
|
|---|
| 506 | (*outbuf) += 1;
|
|---|
| 507 | continue;
|
|---|
| 508 | }
|
|---|
| 509 | if (*outbytesleft < 5) {
|
|---|
| 510 | errno = E2BIG;
|
|---|
| 511 | return -1;
|
|---|
| 512 | }
|
|---|
| 513 | snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
|
|---|
| 514 | memcpy(*outbuf, buf, 5);
|
|---|
| 515 | (*inbytesleft) -= 2;
|
|---|
| 516 | (*outbytesleft) -= 5;
|
|---|
| 517 | (*inbuf) += 2;
|
|---|
| 518 | (*outbuf) += 5;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | if (*inbytesleft == 1) {
|
|---|
| 522 | errno = EINVAL;
|
|---|
| 523 | return -1;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | if (*inbytesleft > 1) {
|
|---|
| 527 | errno = E2BIG;
|
|---|
| 528 | return -1;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | return 0;
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 535 | char **outbuf, size_t *outbytesleft)
|
|---|
| 536 | {
|
|---|
| 537 | int n;
|
|---|
| 538 |
|
|---|
| 539 | n = MIN(*inbytesleft, *outbytesleft);
|
|---|
| 540 |
|
|---|
| 541 | swab(*inbuf, *outbuf, (n&~1));
|
|---|
| 542 | if (n&1) {
|
|---|
| 543 | (*outbuf)[n-1] = 0;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 | (*inbytesleft) -= n;
|
|---|
| 547 | (*outbytesleft) -= n;
|
|---|
| 548 | (*inbuf) += n;
|
|---|
| 549 | (*outbuf) += n;
|
|---|
| 550 |
|
|---|
| 551 | if (*inbytesleft > 0) {
|
|---|
| 552 | errno = E2BIG;
|
|---|
| 553 | return -1;
|
|---|
| 554 | }
|
|---|
| 555 |
|
|---|
| 556 | return 0;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 560 | char **outbuf, size_t *outbytesleft)
|
|---|
| 561 | {
|
|---|
| 562 | int n;
|
|---|
| 563 |
|
|---|
| 564 | n = MIN(*inbytesleft, *outbytesleft);
|
|---|
| 565 |
|
|---|
| 566 | memmove(*outbuf, *inbuf, n);
|
|---|
| 567 |
|
|---|
| 568 | (*inbytesleft) -= n;
|
|---|
| 569 | (*outbytesleft) -= n;
|
|---|
| 570 | (*inbuf) += n;
|
|---|
| 571 | (*outbuf) += n;
|
|---|
| 572 |
|
|---|
| 573 | if (*inbytesleft > 0) {
|
|---|
| 574 | errno = E2BIG;
|
|---|
| 575 | return -1;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | return 0;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 582 | char **outbuf, size_t *outbytesleft)
|
|---|
| 583 | {
|
|---|
| 584 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
|---|
| 585 | const uint8 *c = (const uint8 *)*inbuf;
|
|---|
| 586 | uint8 *uc = (uint8 *)*outbuf;
|
|---|
| 587 |
|
|---|
| 588 | while (in_left >= 1 && out_left >= 2) {
|
|---|
| 589 | unsigned int codepoint;
|
|---|
| 590 |
|
|---|
| 591 | if ((c[0] & 0x80) == 0) {
|
|---|
| 592 | uc[0] = c[0];
|
|---|
| 593 | uc[1] = 0;
|
|---|
| 594 | c += 1;
|
|---|
| 595 | in_left -= 1;
|
|---|
| 596 | out_left -= 2;
|
|---|
| 597 | uc += 2;
|
|---|
| 598 | continue;
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | if ((c[0] & 0xe0) == 0xc0) {
|
|---|
| 602 | if (in_left < 2 ||
|
|---|
| 603 | (c[1] & 0xc0) != 0x80) {
|
|---|
| 604 | errno = EILSEQ;
|
|---|
| 605 | goto error;
|
|---|
| 606 | }
|
|---|
| 607 | codepoint = (c[1]&0x3f) | ((c[0]&0x1f)<<6);
|
|---|
| 608 | if (codepoint < 0x80) {
|
|---|
| 609 | /* don't accept UTF-8 characters that are not minimally packed */
|
|---|
| 610 | errno = EILSEQ;
|
|---|
| 611 | goto error;
|
|---|
| 612 | }
|
|---|
| 613 | uc[1] = codepoint >> 8;
|
|---|
| 614 | uc[0] = codepoint & 0xff;
|
|---|
| 615 | c += 2;
|
|---|
| 616 | in_left -= 2;
|
|---|
| 617 | out_left -= 2;
|
|---|
| 618 | uc += 2;
|
|---|
| 619 | continue;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | if ((c[0] & 0xf0) == 0xe0) {
|
|---|
| 623 | if (in_left < 3 ||
|
|---|
| 624 | (c[1] & 0xc0) != 0x80 ||
|
|---|
| 625 | (c[2] & 0xc0) != 0x80) {
|
|---|
| 626 | errno = EILSEQ;
|
|---|
| 627 | goto error;
|
|---|
| 628 | }
|
|---|
| 629 | codepoint = (c[2]&0x3f) | ((c[1]&0x3f)<<6) | ((c[0]&0xf)<<12);
|
|---|
| 630 | if (codepoint < 0x800) {
|
|---|
| 631 | /* don't accept UTF-8 characters that are not minimally packed */
|
|---|
| 632 | errno = EILSEQ;
|
|---|
| 633 | goto error;
|
|---|
| 634 | }
|
|---|
| 635 | uc[1] = codepoint >> 8;
|
|---|
| 636 | uc[0] = codepoint & 0xff;
|
|---|
| 637 | c += 3;
|
|---|
| 638 | in_left -= 3;
|
|---|
| 639 | out_left -= 2;
|
|---|
| 640 | uc += 2;
|
|---|
| 641 | continue;
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | if ((c[0] & 0xf8) == 0xf0) {
|
|---|
| 645 | if (in_left < 4 ||
|
|---|
| 646 | (c[1] & 0xc0) != 0x80 ||
|
|---|
| 647 | (c[2] & 0xc0) != 0x80 ||
|
|---|
| 648 | (c[3] & 0xc0) != 0x80) {
|
|---|
| 649 | errno = EILSEQ;
|
|---|
| 650 | goto error;
|
|---|
| 651 | }
|
|---|
| 652 | codepoint =
|
|---|
| 653 | (c[3]&0x3f) |
|
|---|
| 654 | ((c[2]&0x3f)<<6) |
|
|---|
| 655 | ((c[1]&0x3f)<<12) |
|
|---|
| 656 | ((c[0]&0x7)<<18);
|
|---|
| 657 | if (codepoint < 0x10000 || codepoint > 0x10ffff) {
|
|---|
| 658 | /* don't accept UTF-8 characters that are not minimally packed */
|
|---|
| 659 | errno = EILSEQ;
|
|---|
| 660 | goto error;
|
|---|
| 661 | }
|
|---|
| 662 |
|
|---|
| 663 | codepoint -= 0x10000;
|
|---|
| 664 |
|
|---|
| 665 | if (out_left < 4) {
|
|---|
| 666 | errno = E2BIG;
|
|---|
| 667 | goto error;
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | uc[0] = (codepoint>>10) & 0xFF;
|
|---|
| 671 | uc[1] = (codepoint>>18) | 0xd8;
|
|---|
| 672 | uc[2] = codepoint & 0xFF;
|
|---|
| 673 | uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
|
|---|
| 674 | c += 4;
|
|---|
| 675 | in_left -= 4;
|
|---|
| 676 | out_left -= 4;
|
|---|
| 677 | uc += 4;
|
|---|
| 678 | continue;
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | /* we don't handle 5 byte sequences */
|
|---|
| 682 | errno = EINVAL;
|
|---|
| 683 | goto error;
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 | if (in_left > 0) {
|
|---|
| 687 | errno = E2BIG;
|
|---|
| 688 | goto error;
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | *inbytesleft = in_left;
|
|---|
| 692 | *outbytesleft = out_left;
|
|---|
| 693 | *inbuf = (char *)c;
|
|---|
| 694 | *outbuf = (char *)uc;
|
|---|
| 695 | return 0;
|
|---|
| 696 |
|
|---|
| 697 | error:
|
|---|
| 698 | *inbytesleft = in_left;
|
|---|
| 699 | *outbytesleft = out_left;
|
|---|
| 700 | *inbuf = (char *)c;
|
|---|
| 701 | *outbuf = (char *)uc;
|
|---|
| 702 | return -1;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
|---|
| 706 | char **outbuf, size_t *outbytesleft)
|
|---|
| 707 | {
|
|---|
| 708 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
|---|
| 709 | uint8 *c = (uint8 *)*outbuf;
|
|---|
| 710 | const uint8 *uc = (const uint8 *)*inbuf;
|
|---|
| 711 |
|
|---|
| 712 | while (in_left >= 2 && out_left >= 1) {
|
|---|
| 713 | unsigned int codepoint;
|
|---|
| 714 |
|
|---|
| 715 | if (uc[1] == 0 && !(uc[0] & 0x80)) {
|
|---|
| 716 | /* simplest case */
|
|---|
| 717 | c[0] = uc[0];
|
|---|
| 718 | in_left -= 2;
|
|---|
| 719 | out_left -= 1;
|
|---|
| 720 | uc += 2;
|
|---|
| 721 | c += 1;
|
|---|
| 722 | continue;
|
|---|
| 723 | }
|
|---|
| 724 |
|
|---|
| 725 | if ((uc[1]&0xf8) == 0) {
|
|---|
| 726 | /* next simplest case */
|
|---|
| 727 | if (out_left < 2) {
|
|---|
| 728 | errno = E2BIG;
|
|---|
| 729 | goto error;
|
|---|
| 730 | }
|
|---|
| 731 | c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
|
|---|
| 732 | c[1] = 0x80 | (uc[0] & 0x3f);
|
|---|
| 733 | in_left -= 2;
|
|---|
| 734 | out_left -= 2;
|
|---|
| 735 | uc += 2;
|
|---|
| 736 | c += 2;
|
|---|
| 737 | continue;
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | if ((uc[1] & 0xfc) == 0xdc) {
|
|---|
| 741 | /* its the second part of a 4 byte sequence. Illegal */
|
|---|
| 742 | if (in_left < 4) {
|
|---|
| 743 | errno = EINVAL;
|
|---|
| 744 | } else {
|
|---|
| 745 | errno = EILSEQ;
|
|---|
| 746 | }
|
|---|
| 747 | goto error;
|
|---|
| 748 | }
|
|---|
| 749 |
|
|---|
| 750 | if ((uc[1] & 0xfc) != 0xd8) {
|
|---|
| 751 | codepoint = uc[0] | (uc[1]<<8);
|
|---|
| 752 | if (out_left < 3) {
|
|---|
| 753 | errno = E2BIG;
|
|---|
| 754 | goto error;
|
|---|
| 755 | }
|
|---|
| 756 | c[0] = 0xe0 | (codepoint >> 12);
|
|---|
| 757 | c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
|
|---|
| 758 | c[2] = 0x80 | (codepoint & 0x3f);
|
|---|
| 759 |
|
|---|
| 760 | in_left -= 2;
|
|---|
| 761 | out_left -= 3;
|
|---|
| 762 | uc += 2;
|
|---|
| 763 | c += 3;
|
|---|
| 764 | continue;
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | /* its the first part of a 4 byte sequence */
|
|---|
| 768 | if (in_left < 4) {
|
|---|
| 769 | errno = EINVAL;
|
|---|
| 770 | goto error;
|
|---|
| 771 | }
|
|---|
| 772 | if ((uc[3] & 0xfc) != 0xdc) {
|
|---|
| 773 | errno = EILSEQ;
|
|---|
| 774 | goto error;
|
|---|
| 775 | }
|
|---|
| 776 | codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
|
|---|
| 777 | (uc[0]<<10) | ((uc[1] & 0x3)<<18));
|
|---|
| 778 |
|
|---|
| 779 | if (out_left < 4) {
|
|---|
| 780 | errno = E2BIG;
|
|---|
| 781 | goto error;
|
|---|
| 782 | }
|
|---|
| 783 | c[0] = 0xf0 | (codepoint >> 18);
|
|---|
| 784 | c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
|
|---|
| 785 | c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
|
|---|
| 786 | c[3] = 0x80 | (codepoint & 0x3f);
|
|---|
| 787 |
|
|---|
| 788 | in_left -= 4;
|
|---|
| 789 | out_left -= 4;
|
|---|
| 790 | uc += 4;
|
|---|
| 791 | c += 4;
|
|---|
| 792 | }
|
|---|
| 793 |
|
|---|
| 794 | if (in_left == 1) {
|
|---|
| 795 | errno = EINVAL;
|
|---|
| 796 | goto error;
|
|---|
| 797 | }
|
|---|
| 798 |
|
|---|
| 799 | if (in_left > 1) {
|
|---|
| 800 | errno = E2BIG;
|
|---|
| 801 | goto error;
|
|---|
| 802 | }
|
|---|
| 803 |
|
|---|
| 804 | *inbytesleft = in_left;
|
|---|
| 805 | *outbytesleft = out_left;
|
|---|
| 806 | *inbuf = (char *)uc;
|
|---|
| 807 | *outbuf = (char *)c;
|
|---|
| 808 |
|
|---|
| 809 | return 0;
|
|---|
| 810 |
|
|---|
| 811 | error:
|
|---|
| 812 | *inbytesleft = in_left;
|
|---|
| 813 | *outbytesleft = out_left;
|
|---|
| 814 | *inbuf = (char *)uc;
|
|---|
| 815 | *outbuf = (char *)c;
|
|---|
| 816 | return -1;
|
|---|
| 817 | }
|
|---|
| 818 |
|
|---|