[341] | 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. SCS
|
---|
| 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 | size_t ret = iconv((iconv_t)cd,
|
---|
| 157 | (char **)inbuf, inbytesleft,
|
---|
| 158 | outbuf, outbytesleft);
|
---|
| 159 | if (ret == (size_t)-1) {
|
---|
| 160 | int saved_errno = errno;
|
---|
| 161 | iconv(cd, NULL, NULL, NULL, NULL);
|
---|
| 162 | errno = saved_errno;
|
---|
| 163 | }
|
---|
| 164 | #ifdef __OS2__
|
---|
| 165 | /* Workaround for path separator on OS/2 */
|
---|
| 166 | else
|
---|
| 167 | {
|
---|
| 168 | if( (strstr(to_name, "949") != NULL) || /* Korean CP */
|
---|
| 169 | (strstr(to_name, "932") != NULL) || /* Japanese CP */
|
---|
| 170 | (strstr(to_name, "942") != NULL) || /* Japanese CP */
|
---|
| 171 | (strstr(to_name, "943") != NULL) ) /* Japanese CP */
|
---|
| 172 | {
|
---|
| 173 | while(( char * )outbuf_uc < *outbuf )
|
---|
| 174 | {
|
---|
| 175 | if( *outbuf_uc == 0x20a9 || /* Korean WON */
|
---|
| 176 | *outbuf_uc == 0x00a5 ) /* Japanese YEN */
|
---|
| 177 | *outbuf_uc = '\\';
|
---|
| 178 |
|
---|
| 179 | outbuf_uc++;
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | #endif
|
---|
| 184 | return ret;
|
---|
| 185 | }
|
---|
| 186 | #endif
|
---|
| 187 |
|
---|
| 188 | /**
|
---|
| 189 | * This is a simple portable iconv() implementaion.
|
---|
| 190 | *
|
---|
| 191 | * It only knows about a very small number of character sets - just
|
---|
| 192 | * enough that Samba works on systems that don't have iconv.
|
---|
| 193 | **/
|
---|
| 194 | size_t smb_iconv(smb_iconv_t cd,
|
---|
| 195 | const char **inbuf, size_t *inbytesleft,
|
---|
| 196 | char **outbuf, size_t *outbytesleft)
|
---|
| 197 | {
|
---|
| 198 | char cvtbuf[2048];
|
---|
| 199 | char *bufp = cvtbuf;
|
---|
| 200 | size_t bufsize;
|
---|
| 201 |
|
---|
| 202 | #ifdef __OS2__
|
---|
| 203 | save_toname(cd->to_name, 0);
|
---|
| 204 | #endif
|
---|
| 205 |
|
---|
| 206 | /* in many cases we can go direct */
|
---|
| 207 | if (cd->direct) {
|
---|
| 208 | return cd->direct(cd->cd_direct,
|
---|
| 209 | inbuf, inbytesleft, outbuf, outbytesleft);
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | /* otherwise we have to do it chunks at a time */
|
---|
| 214 | while (*inbytesleft > 0) {
|
---|
| 215 | bufp = cvtbuf;
|
---|
| 216 | bufsize = sizeof(cvtbuf);
|
---|
| 217 |
|
---|
| 218 | if (cd->pull(cd->cd_pull,
|
---|
| 219 | inbuf, inbytesleft, &bufp, &bufsize) == -1
|
---|
| 220 | && errno != E2BIG) return -1;
|
---|
| 221 |
|
---|
| 222 | bufp = cvtbuf;
|
---|
| 223 | bufsize = sizeof(cvtbuf) - bufsize;
|
---|
| 224 |
|
---|
| 225 | if (cd->push(cd->cd_push,
|
---|
| 226 | (const char **)&bufp, &bufsize,
|
---|
| 227 | outbuf, outbytesleft) == -1) return -1;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | return 0;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 | static bool is_utf16(const char *name)
|
---|
| 235 | {
|
---|
| 236 | return strcasecmp(name, "UCS-2LE") == 0 ||
|
---|
| 237 | strcasecmp(name, "UTF-16LE") == 0;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /*
|
---|
| 241 | simple iconv_open() wrapper
|
---|
| 242 | */
|
---|
| 243 | smb_iconv_t smb_iconv_open(const char *tocode, const char *fromcode)
|
---|
| 244 | {
|
---|
| 245 | smb_iconv_t ret;
|
---|
| 246 | struct charset_functions *from, *to;
|
---|
| 247 |
|
---|
| 248 | lazy_initialize_iconv();
|
---|
| 249 | from = charsets;
|
---|
| 250 | to = charsets;
|
---|
| 251 |
|
---|
| 252 | ret = SMB_MALLOC_P(struct _smb_iconv_t);
|
---|
| 253 | if (!ret) {
|
---|
| 254 | errno = ENOMEM;
|
---|
| 255 | return (smb_iconv_t)-1;
|
---|
| 256 | }
|
---|
| 257 | memset(ret, 0, sizeof(struct _smb_iconv_t));
|
---|
| 258 |
|
---|
| 259 | ret->from_name = SMB_STRDUP(fromcode);
|
---|
| 260 | ret->to_name = SMB_STRDUP(tocode);
|
---|
| 261 |
|
---|
| 262 | /* check for the simplest null conversion */
|
---|
| 263 | if (strcasecmp(fromcode, tocode) == 0) {
|
---|
| 264 | ret->direct = iconv_copy;
|
---|
| 265 | return ret;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /* check if we have a builtin function for this conversion */
|
---|
| 269 | from = find_charset_functions(fromcode);
|
---|
| 270 | if(from)ret->pull = from->pull;
|
---|
| 271 |
|
---|
| 272 | to = find_charset_functions(tocode);
|
---|
| 273 | if(to)ret->push = to->push;
|
---|
| 274 |
|
---|
| 275 | /* check if we can use iconv for this conversion */
|
---|
| 276 | #ifdef HAVE_NATIVE_ICONV
|
---|
| 277 | if (!ret->pull) {
|
---|
| 278 | ret->cd_pull = iconv_open("UTF-16LE", fromcode);
|
---|
| 279 | if (ret->cd_pull == (iconv_t)-1)
|
---|
| 280 | ret->cd_pull = iconv_open("UCS-2LE", fromcode);
|
---|
| 281 | if (ret->cd_pull != (iconv_t)-1)
|
---|
| 282 | ret->pull = sys_iconv;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | if (!ret->push) {
|
---|
| 286 | ret->cd_push = iconv_open(tocode, "UTF-16LE");
|
---|
| 287 | if (ret->cd_push == (iconv_t)-1)
|
---|
| 288 | ret->cd_push = iconv_open(tocode, "UCS-2LE");
|
---|
| 289 | if (ret->cd_push != (iconv_t)-1)
|
---|
| 290 | ret->push = sys_iconv;
|
---|
| 291 | }
|
---|
| 292 | #endif
|
---|
| 293 |
|
---|
| 294 | /* check if there is a module available that can do this conversion */
|
---|
| 295 | if (!ret->pull && NT_STATUS_IS_OK(smb_probe_module("charset", fromcode))) {
|
---|
| 296 | if(!(from = find_charset_functions(fromcode)))
|
---|
| 297 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", fromcode, fromcode));
|
---|
| 298 | else
|
---|
| 299 | ret->pull = from->pull;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | if (!ret->push && NT_STATUS_IS_OK(smb_probe_module("charset", tocode))) {
|
---|
| 303 | if(!(to = find_charset_functions(tocode)))
|
---|
| 304 | DEBUG(0, ("Module %s doesn't provide charset %s!\n", tocode, tocode));
|
---|
| 305 | else
|
---|
| 306 | ret->push = to->push;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | if (!ret->push || !ret->pull) {
|
---|
| 310 | SAFE_FREE(ret->from_name);
|
---|
| 311 | SAFE_FREE(ret->to_name);
|
---|
| 312 | SAFE_FREE(ret);
|
---|
| 313 | errno = EINVAL;
|
---|
| 314 | return (smb_iconv_t)-1;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /* check for conversion to/from ucs2 */
|
---|
| 318 | if (is_utf16(fromcode) && to) {
|
---|
| 319 | ret->direct = to->push;
|
---|
| 320 | ret->push = ret->pull = NULL;
|
---|
| 321 | return ret;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | if (is_utf16(tocode) && from) {
|
---|
| 325 | ret->direct = from->pull;
|
---|
| 326 | ret->push = ret->pull = NULL;
|
---|
| 327 | return ret;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | /* Check if we can do the conversion direct */
|
---|
| 331 | #ifdef HAVE_NATIVE_ICONV
|
---|
| 332 | if (is_utf16(fromcode)) {
|
---|
| 333 | ret->direct = sys_iconv;
|
---|
| 334 | ret->cd_direct = ret->cd_push;
|
---|
| 335 | ret->cd_push = NULL;
|
---|
| 336 | return ret;
|
---|
| 337 | }
|
---|
| 338 | if (is_utf16(tocode)) {
|
---|
| 339 | ret->direct = sys_iconv;
|
---|
| 340 | ret->cd_direct = ret->cd_pull;
|
---|
| 341 | ret->cd_pull = NULL;
|
---|
| 342 | return ret;
|
---|
| 343 | }
|
---|
| 344 | #endif
|
---|
| 345 |
|
---|
| 346 | return ret;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /*
|
---|
| 350 | simple iconv_close() wrapper
|
---|
| 351 | */
|
---|
| 352 | int smb_iconv_close (smb_iconv_t cd)
|
---|
| 353 | {
|
---|
| 354 | #ifdef HAVE_NATIVE_ICONV
|
---|
| 355 | if (cd->cd_direct) iconv_close((iconv_t)cd->cd_direct);
|
---|
| 356 | if (cd->cd_pull) iconv_close((iconv_t)cd->cd_pull);
|
---|
| 357 | if (cd->cd_push) iconv_close((iconv_t)cd->cd_push);
|
---|
| 358 | #endif
|
---|
| 359 |
|
---|
| 360 | SAFE_FREE(cd->from_name);
|
---|
| 361 | SAFE_FREE(cd->to_name);
|
---|
| 362 |
|
---|
| 363 | memset(cd, 0, sizeof(*cd));
|
---|
| 364 | SAFE_FREE(cd);
|
---|
| 365 | return 0;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 |
|
---|
| 369 | /**********************************************************************
|
---|
| 370 | the following functions implement the builtin character sets in Samba
|
---|
| 371 | and also the "test" character sets that are designed to test
|
---|
| 372 | multi-byte character set support for english users
|
---|
| 373 | ***********************************************************************/
|
---|
| 374 |
|
---|
| 375 | static size_t ascii_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 376 | char **outbuf, size_t *outbytesleft)
|
---|
| 377 | {
|
---|
| 378 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
---|
| 379 | (*outbuf)[0] = (*inbuf)[0];
|
---|
| 380 | (*outbuf)[1] = 0;
|
---|
| 381 | (*inbytesleft) -= 1;
|
---|
| 382 | (*outbytesleft) -= 2;
|
---|
| 383 | (*inbuf) += 1;
|
---|
| 384 | (*outbuf) += 2;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | if (*inbytesleft > 0) {
|
---|
| 388 | errno = E2BIG;
|
---|
| 389 | return -1;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | return 0;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | static size_t ascii_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 396 | char **outbuf, size_t *outbytesleft)
|
---|
| 397 | {
|
---|
| 398 | int ir_count=0;
|
---|
| 399 |
|
---|
| 400 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
| 401 | (*outbuf)[0] = (*inbuf)[0] & 0x7F;
|
---|
| 402 | if ((*inbuf)[1]) ir_count++;
|
---|
| 403 | (*inbytesleft) -= 2;
|
---|
| 404 | (*outbytesleft) -= 1;
|
---|
| 405 | (*inbuf) += 2;
|
---|
| 406 | (*outbuf) += 1;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | if (*inbytesleft == 1) {
|
---|
| 410 | errno = EINVAL;
|
---|
| 411 | return -1;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | if (*inbytesleft > 1) {
|
---|
| 415 | errno = E2BIG;
|
---|
| 416 | return -1;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | return ir_count;
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | static size_t latin1_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 423 | char **outbuf, size_t *outbytesleft)
|
---|
| 424 | {
|
---|
| 425 | int ir_count=0;
|
---|
| 426 |
|
---|
| 427 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
| 428 | (*outbuf)[0] = (*inbuf)[0];
|
---|
| 429 | if ((*inbuf)[1]) ir_count++;
|
---|
| 430 | (*inbytesleft) -= 2;
|
---|
| 431 | (*outbytesleft) -= 1;
|
---|
| 432 | (*inbuf) += 2;
|
---|
| 433 | (*outbuf) += 1;
|
---|
| 434 | }
|
---|
| 435 |
|
---|
| 436 | if (*inbytesleft == 1) {
|
---|
| 437 | errno = EINVAL;
|
---|
| 438 | return -1;
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | if (*inbytesleft > 1) {
|
---|
| 442 | errno = E2BIG;
|
---|
| 443 | return -1;
|
---|
| 444 | }
|
---|
| 445 |
|
---|
| 446 | return ir_count;
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | static size_t ucs2hex_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 450 | char **outbuf, size_t *outbytesleft)
|
---|
| 451 | {
|
---|
| 452 | while (*inbytesleft >= 1 && *outbytesleft >= 2) {
|
---|
| 453 | unsigned v;
|
---|
| 454 |
|
---|
| 455 | if ((*inbuf)[0] != '@') {
|
---|
| 456 | /* seven bit ascii case */
|
---|
| 457 | (*outbuf)[0] = (*inbuf)[0];
|
---|
| 458 | (*outbuf)[1] = 0;
|
---|
| 459 | (*inbytesleft) -= 1;
|
---|
| 460 | (*outbytesleft) -= 2;
|
---|
| 461 | (*inbuf) += 1;
|
---|
| 462 | (*outbuf) += 2;
|
---|
| 463 | continue;
|
---|
| 464 | }
|
---|
| 465 | /* it's a hex character */
|
---|
| 466 | if (*inbytesleft < 5) {
|
---|
| 467 | errno = EINVAL;
|
---|
| 468 | return -1;
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | if (sscanf(&(*inbuf)[1], "%04x", &v) != 1) {
|
---|
| 472 | errno = EILSEQ;
|
---|
| 473 | return -1;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | (*outbuf)[0] = v&0xff;
|
---|
| 477 | (*outbuf)[1] = v>>8;
|
---|
| 478 | (*inbytesleft) -= 5;
|
---|
| 479 | (*outbytesleft) -= 2;
|
---|
| 480 | (*inbuf) += 5;
|
---|
| 481 | (*outbuf) += 2;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | if (*inbytesleft > 0) {
|
---|
| 485 | errno = E2BIG;
|
---|
| 486 | return -1;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | return 0;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | static size_t ucs2hex_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 493 | char **outbuf, size_t *outbytesleft)
|
---|
| 494 | {
|
---|
| 495 | while (*inbytesleft >= 2 && *outbytesleft >= 1) {
|
---|
| 496 | char buf[6];
|
---|
| 497 |
|
---|
| 498 | if ((*inbuf)[1] == 0 &&
|
---|
| 499 | ((*inbuf)[0] & 0x80) == 0 &&
|
---|
| 500 | (*inbuf)[0] != '@') {
|
---|
| 501 | (*outbuf)[0] = (*inbuf)[0];
|
---|
| 502 | (*inbytesleft) -= 2;
|
---|
| 503 | (*outbytesleft) -= 1;
|
---|
| 504 | (*inbuf) += 2;
|
---|
| 505 | (*outbuf) += 1;
|
---|
| 506 | continue;
|
---|
| 507 | }
|
---|
| 508 | if (*outbytesleft < 5) {
|
---|
| 509 | errno = E2BIG;
|
---|
| 510 | return -1;
|
---|
| 511 | }
|
---|
| 512 | snprintf(buf, 6, "@%04x", SVAL(*inbuf, 0));
|
---|
| 513 | memcpy(*outbuf, buf, 5);
|
---|
| 514 | (*inbytesleft) -= 2;
|
---|
| 515 | (*outbytesleft) -= 5;
|
---|
| 516 | (*inbuf) += 2;
|
---|
| 517 | (*outbuf) += 5;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | if (*inbytesleft == 1) {
|
---|
| 521 | errno = EINVAL;
|
---|
| 522 | return -1;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
| 525 | if (*inbytesleft > 1) {
|
---|
| 526 | errno = E2BIG;
|
---|
| 527 | return -1;
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | return 0;
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | static size_t iconv_swab(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 534 | char **outbuf, size_t *outbytesleft)
|
---|
| 535 | {
|
---|
| 536 | int n;
|
---|
| 537 |
|
---|
| 538 | n = MIN(*inbytesleft, *outbytesleft);
|
---|
| 539 |
|
---|
| 540 | swab(*inbuf, *outbuf, (n&~1));
|
---|
| 541 | if (n&1) {
|
---|
| 542 | (*outbuf)[n-1] = 0;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | (*inbytesleft) -= n;
|
---|
| 546 | (*outbytesleft) -= n;
|
---|
| 547 | (*inbuf) += n;
|
---|
| 548 | (*outbuf) += n;
|
---|
| 549 |
|
---|
| 550 | if (*inbytesleft > 0) {
|
---|
| 551 | errno = E2BIG;
|
---|
| 552 | return -1;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | return 0;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | static size_t iconv_copy(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 559 | char **outbuf, size_t *outbytesleft)
|
---|
| 560 | {
|
---|
| 561 | int n;
|
---|
| 562 |
|
---|
| 563 | n = MIN(*inbytesleft, *outbytesleft);
|
---|
| 564 |
|
---|
| 565 | memmove(*outbuf, *inbuf, n);
|
---|
| 566 |
|
---|
| 567 | (*inbytesleft) -= n;
|
---|
| 568 | (*outbytesleft) -= n;
|
---|
| 569 | (*inbuf) += n;
|
---|
| 570 | (*outbuf) += n;
|
---|
| 571 |
|
---|
| 572 | if (*inbytesleft > 0) {
|
---|
| 573 | errno = E2BIG;
|
---|
| 574 | return -1;
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | return 0;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | static size_t utf8_pull(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 581 | char **outbuf, size_t *outbytesleft)
|
---|
| 582 | {
|
---|
| 583 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
---|
| 584 | const uint8 *c = (const uint8 *)*inbuf;
|
---|
| 585 | uint8 *uc = (uint8 *)*outbuf;
|
---|
| 586 |
|
---|
| 587 | while (in_left >= 1 && out_left >= 2) {
|
---|
| 588 | unsigned int codepoint;
|
---|
| 589 |
|
---|
| 590 | if ((c[0] & 0x80) == 0) {
|
---|
| 591 | uc[0] = c[0];
|
---|
| 592 | uc[1] = 0;
|
---|
| 593 | c += 1;
|
---|
| 594 | in_left -= 1;
|
---|
| 595 | out_left -= 2;
|
---|
| 596 | uc += 2;
|
---|
| 597 | continue;
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | if ((c[0] & 0xe0) == 0xc0) {
|
---|
| 601 | if (in_left < 2 ||
|
---|
| 602 | (c[1] & 0xc0) != 0x80) {
|
---|
| 603 | errno = EILSEQ;
|
---|
| 604 | goto error;
|
---|
| 605 | }
|
---|
| 606 | codepoint = (c[1]&0x3f) | ((c[0]&0x1f)<<6);
|
---|
| 607 | if (codepoint < 0x80) {
|
---|
| 608 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
| 609 | errno = EILSEQ;
|
---|
| 610 | goto error;
|
---|
| 611 | }
|
---|
| 612 | uc[1] = codepoint >> 8;
|
---|
| 613 | uc[0] = codepoint & 0xff;
|
---|
| 614 | c += 2;
|
---|
| 615 | in_left -= 2;
|
---|
| 616 | out_left -= 2;
|
---|
| 617 | uc += 2;
|
---|
| 618 | continue;
|
---|
| 619 | }
|
---|
| 620 |
|
---|
| 621 | if ((c[0] & 0xf0) == 0xe0) {
|
---|
| 622 | if (in_left < 3 ||
|
---|
| 623 | (c[1] & 0xc0) != 0x80 ||
|
---|
| 624 | (c[2] & 0xc0) != 0x80) {
|
---|
| 625 | errno = EILSEQ;
|
---|
| 626 | goto error;
|
---|
| 627 | }
|
---|
| 628 | codepoint = (c[2]&0x3f) | ((c[1]&0x3f)<<6) | ((c[0]&0xf)<<12);
|
---|
| 629 | if (codepoint < 0x800) {
|
---|
| 630 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
| 631 | errno = EILSEQ;
|
---|
| 632 | goto error;
|
---|
| 633 | }
|
---|
| 634 | uc[1] = codepoint >> 8;
|
---|
| 635 | uc[0] = codepoint & 0xff;
|
---|
| 636 | c += 3;
|
---|
| 637 | in_left -= 3;
|
---|
| 638 | out_left -= 2;
|
---|
| 639 | uc += 2;
|
---|
| 640 | continue;
|
---|
| 641 | }
|
---|
| 642 |
|
---|
| 643 | if ((c[0] & 0xf8) == 0xf0) {
|
---|
| 644 | if (in_left < 4 ||
|
---|
| 645 | (c[1] & 0xc0) != 0x80 ||
|
---|
| 646 | (c[2] & 0xc0) != 0x80 ||
|
---|
| 647 | (c[3] & 0xc0) != 0x80) {
|
---|
| 648 | errno = EILSEQ;
|
---|
| 649 | goto error;
|
---|
| 650 | }
|
---|
| 651 | codepoint =
|
---|
| 652 | (c[3]&0x3f) |
|
---|
| 653 | ((c[2]&0x3f)<<6) |
|
---|
| 654 | ((c[1]&0x3f)<<12) |
|
---|
| 655 | ((c[0]&0x7)<<18);
|
---|
| 656 | if (codepoint < 0x10000 || codepoint > 0x10ffff) {
|
---|
| 657 | /* don't accept UTF-8 characters that are not minimally packed */
|
---|
| 658 | errno = EILSEQ;
|
---|
| 659 | goto error;
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | codepoint -= 0x10000;
|
---|
| 663 |
|
---|
| 664 | if (out_left < 4) {
|
---|
| 665 | errno = E2BIG;
|
---|
| 666 | goto error;
|
---|
| 667 | }
|
---|
| 668 |
|
---|
| 669 | uc[0] = (codepoint>>10) & 0xFF;
|
---|
| 670 | uc[1] = (codepoint>>18) | 0xd8;
|
---|
| 671 | uc[2] = codepoint & 0xFF;
|
---|
| 672 | uc[3] = ((codepoint>>8) & 0x3) | 0xdc;
|
---|
| 673 | c += 4;
|
---|
| 674 | in_left -= 4;
|
---|
| 675 | out_left -= 4;
|
---|
| 676 | uc += 4;
|
---|
| 677 | continue;
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | /* we don't handle 5 byte sequences */
|
---|
| 681 | errno = EINVAL;
|
---|
| 682 | goto error;
|
---|
| 683 | }
|
---|
| 684 |
|
---|
| 685 | if (in_left > 0) {
|
---|
| 686 | errno = E2BIG;
|
---|
| 687 | goto error;
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | *inbytesleft = in_left;
|
---|
| 691 | *outbytesleft = out_left;
|
---|
| 692 | *inbuf = (char *)c;
|
---|
| 693 | *outbuf = (char *)uc;
|
---|
| 694 | return 0;
|
---|
| 695 |
|
---|
| 696 | error:
|
---|
| 697 | *inbytesleft = in_left;
|
---|
| 698 | *outbytesleft = out_left;
|
---|
| 699 | *inbuf = (char *)c;
|
---|
| 700 | *outbuf = (char *)uc;
|
---|
| 701 | return -1;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | static size_t utf8_push(void *cd, const char **inbuf, size_t *inbytesleft,
|
---|
| 705 | char **outbuf, size_t *outbytesleft)
|
---|
| 706 | {
|
---|
| 707 | size_t in_left=*inbytesleft, out_left=*outbytesleft;
|
---|
| 708 | uint8 *c = (uint8 *)*outbuf;
|
---|
| 709 | const uint8 *uc = (const uint8 *)*inbuf;
|
---|
| 710 |
|
---|
| 711 | while (in_left >= 2 && out_left >= 1) {
|
---|
| 712 | unsigned int codepoint;
|
---|
| 713 |
|
---|
| 714 | if (uc[1] == 0 && !(uc[0] & 0x80)) {
|
---|
| 715 | /* simplest case */
|
---|
| 716 | c[0] = uc[0];
|
---|
| 717 | in_left -= 2;
|
---|
| 718 | out_left -= 1;
|
---|
| 719 | uc += 2;
|
---|
| 720 | c += 1;
|
---|
| 721 | continue;
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 | if ((uc[1]&0xf8) == 0) {
|
---|
| 725 | /* next simplest case */
|
---|
| 726 | if (out_left < 2) {
|
---|
| 727 | errno = E2BIG;
|
---|
| 728 | goto error;
|
---|
| 729 | }
|
---|
| 730 | c[0] = 0xc0 | (uc[0]>>6) | (uc[1]<<2);
|
---|
| 731 | c[1] = 0x80 | (uc[0] & 0x3f);
|
---|
| 732 | in_left -= 2;
|
---|
| 733 | out_left -= 2;
|
---|
| 734 | uc += 2;
|
---|
| 735 | c += 2;
|
---|
| 736 | continue;
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | if ((uc[1] & 0xfc) == 0xdc) {
|
---|
| 740 | /* its the second part of a 4 byte sequence. Illegal */
|
---|
| 741 | if (in_left < 4) {
|
---|
| 742 | errno = EINVAL;
|
---|
| 743 | } else {
|
---|
| 744 | errno = EILSEQ;
|
---|
| 745 | }
|
---|
| 746 | goto error;
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | if ((uc[1] & 0xfc) != 0xd8) {
|
---|
| 750 | codepoint = uc[0] | (uc[1]<<8);
|
---|
| 751 | if (out_left < 3) {
|
---|
| 752 | errno = E2BIG;
|
---|
| 753 | goto error;
|
---|
| 754 | }
|
---|
| 755 | c[0] = 0xe0 | (codepoint >> 12);
|
---|
| 756 | c[1] = 0x80 | ((codepoint >> 6) & 0x3f);
|
---|
| 757 | c[2] = 0x80 | (codepoint & 0x3f);
|
---|
| 758 |
|
---|
| 759 | in_left -= 2;
|
---|
| 760 | out_left -= 3;
|
---|
| 761 | uc += 2;
|
---|
| 762 | c += 3;
|
---|
| 763 | continue;
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | /* its the first part of a 4 byte sequence */
|
---|
| 767 | if (in_left < 4) {
|
---|
| 768 | errno = EINVAL;
|
---|
| 769 | goto error;
|
---|
| 770 | }
|
---|
| 771 | if ((uc[3] & 0xfc) != 0xdc) {
|
---|
| 772 | errno = EILSEQ;
|
---|
| 773 | goto error;
|
---|
| 774 | }
|
---|
| 775 | codepoint = 0x10000 + (uc[2] | ((uc[3] & 0x3)<<8) |
|
---|
| 776 | (uc[0]<<10) | ((uc[1] & 0x3)<<18));
|
---|
| 777 |
|
---|
| 778 | if (out_left < 4) {
|
---|
| 779 | errno = E2BIG;
|
---|
| 780 | goto error;
|
---|
| 781 | }
|
---|
| 782 | c[0] = 0xf0 | (codepoint >> 18);
|
---|
| 783 | c[1] = 0x80 | ((codepoint >> 12) & 0x3f);
|
---|
| 784 | c[2] = 0x80 | ((codepoint >> 6) & 0x3f);
|
---|
| 785 | c[3] = 0x80 | (codepoint & 0x3f);
|
---|
| 786 |
|
---|
| 787 | in_left -= 4;
|
---|
| 788 | out_left -= 4;
|
---|
| 789 | uc += 4;
|
---|
| 790 | c += 4;
|
---|
| 791 | }
|
---|
| 792 |
|
---|
| 793 | if (in_left == 1) {
|
---|
| 794 | errno = EINVAL;
|
---|
| 795 | goto error;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
| 798 | if (in_left > 1) {
|
---|
| 799 | errno = E2BIG;
|
---|
| 800 | goto error;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | *inbytesleft = in_left;
|
---|
| 804 | *outbytesleft = out_left;
|
---|
| 805 | *inbuf = (char *)uc;
|
---|
| 806 | *outbuf = (char *)c;
|
---|
| 807 |
|
---|
| 808 | return 0;
|
---|
| 809 |
|
---|
| 810 | error:
|
---|
| 811 | *inbytesleft = in_left;
|
---|
| 812 | *outbytesleft = out_left;
|
---|
| 813 | *inbuf = (char *)uc;
|
---|
| 814 | *outbuf = (char *)c;
|
---|
| 815 | return -1;
|
---|
| 816 | }
|
---|
| 817 |
|
---|