1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
|
/*
@(#) $Id: encnames.c,v 1.11 2005/12/01 10:08:53 yeti Exp $
convert charset and surface names to internal representation and back
Copyright (C) 2000-2003 David Necas (Yeti) <yeti@physics.muni.cz>
This program is free software; you can redistribute it and/or modify it
under the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include "enca.h"
#include "internal.h"
#include "tools/encodings.h"
#define NCHARSETS (ELEMENTS(CHARSET_INFO))
#define NALIASES (ELEMENTS(ALIAS_LIST))
#define NSURFACES (ELEMENTS(SURFACE_INFO))
#define ENCODING_UNKNOWN { ENCA_CS_UNKNOWN, 0 }
/* tolower() and toupper() which never fail. */
#define enca_tolower(c) (enca_isupper(c) ? (c) + ('a' - 'A') : (c))
#define enca_toupper(c) (enca_islower(c) ? (c) - ('a' - 'A') : (c))
static const char *UNKNOWN_CHARSET_NAME = "unknown";
static const char *UNKNOWN_CHARSET_HUMAN = "Unrecognized encoding";
static const char *UNKNOWN_CHARSET_SYM = "???";
/* Surface separator (sometimes we need a character, sometimes a string). */
#define SURF_SEPARATOR '/'
#define SURF_SEPARATOR_STR "/"
/**
* EncaSurfaceInfo:
* @enca: Canonical identifier (#NULL when not applicable).
* @human: Human readable name.
* @bit: Appropriate ENCA_SURFACE_<something>.
*
* Surface information.
**/
struct _EncaSurfaceInfo {
const char *enca;
const char *human;
EncaSurface bit;
};
typedef struct _EncaSurfaceInfo EncaSurfaceInfo;
/* Local prototypes. */
static int squeeze_compare(const char *x,
const char *y);
static int alias_search(const char *const *alist,
int n,
const char *s);
static int check_surface_consistency(EncaSurface surface);
static int count_bits(unsigned long int x);
static int check_encoding_name(const char *name);
/* Surface information. */
static const EncaSurfaceInfo SURFACE_INFO[] = {
{
"CR",
"CR line terminators",
ENCA_SURFACE_EOL_CR
},
{
"LF",
"LF line terminators",
ENCA_SURFACE_EOL_LF
},
{
"CRLF",
"CRLF line terminators",
ENCA_SURFACE_EOL_CRLF
},
{
NULL,
"Mixed line terminators",
ENCA_SURFACE_EOL_MIX
},
{
NULL,
"Surrounded by/intermixed with non-text data",
ENCA_SURFACE_EOL_BIN
},
{
"21",
"Byte order reversed in pairs (1,2 -> 2,1)",
ENCA_SURFACE_PERM_21
},
{
"4321",
"Byte order reversed in quadruples (1,2,3,4 -> 4,3,2,1)",
ENCA_SURFACE_PERM_4321
},
{
NULL,
"Both little and big endian chunks, concatenated",
ENCA_SURFACE_PERM_MIX
},
{
"qp",
"Quoted-printable encoded",
ENCA_SURFACE_QP
},
{
"",
"",
ENCA_SURFACE_REMOVE
},
};
/**
* enca_charset_name:
* @charset: A charset id.
* @whatname: Teh type of name you request.
*
* Translates numeric charset id @charset to some kind of name.
*
* Returns: The requested charset name; #NULL for invalid @whatname or
* @charset, or when @whatname name doesn't exist for charset @charset
* (#ENCA_CS_UNKNOWN is OK).
**/
const char*
enca_charset_name(int charset,
EncaNameStyle whatname)
{
const EncaCharsetInfo *cs;
if (charset == ENCA_CS_UNKNOWN) {
switch (whatname) {
case ENCA_NAME_STYLE_RFC1345:
case ENCA_NAME_STYLE_ENCA:
case ENCA_NAME_STYLE_MIME:
return UNKNOWN_CHARSET_NAME;
case ENCA_NAME_STYLE_HUMAN:
return UNKNOWN_CHARSET_HUMAN;
case ENCA_NAME_STYLE_CSTOCS:
case ENCA_NAME_STYLE_ICONV:
return UNKNOWN_CHARSET_SYM;
default:
return NULL;
}
}
if ((size_t)charset >= NCHARSETS)
return NULL;
cs = CHARSET_INFO + charset;
switch (whatname) {
case ENCA_NAME_STYLE_RFC1345:
return ALIAS_LIST[cs->rfc1345];
case ENCA_NAME_STYLE_HUMAN:
return cs->human;
case ENCA_NAME_STYLE_ENCA:
return ALIAS_LIST[cs->enca];
case ENCA_NAME_STYLE_CSTOCS:
return cs->cstocs < 0 ? NULL : ALIAS_LIST[cs->cstocs];
case ENCA_NAME_STYLE_ICONV:
return cs->iconv < 0 ? NULL : ALIAS_LIST[cs->iconv];
case ENCA_NAME_STYLE_MIME:
return cs->mime < 0 ? NULL : ALIAS_LIST[cs->mime];
default:
return NULL;
}
/* just to placate gcc */
return NULL;
}
/**
* enca_get_charset_aliases:
* @charset: A charset id.
* @n: The number of aliases will be stored here.
*
* Returns list of accepted aliases for charset @charset.
*
* The list of aliases has to be freed by caller; the strings themselves
* must be considered constant and must NOT be freed.
*
* Returns: The list of aliases, storing their number into *@n; #NULL for
* invalid @charset (*@n is zero then).
**/
const char**
enca_get_charset_aliases(int charset,
size_t *n)
{
const char **aliases;
size_t i, j;
/* Compute total length.
* FIXME: The list is known at compile time. */
for (i = *n = 0; i < NALIASES; i++)
if (INDEX_LIST[i] == charset) (*n)++;
/* Create the list. */
aliases = NEW(const char*, *n);
for (i = j = 0; i < NALIASES; i++)
if (INDEX_LIST[i] == charset)
aliases[j++] = ALIAS_LIST[i];
return aliases;
}
/**
* enca_get_surface_name:
* @surface: A surface.
* @whatname: The type of name you request.
*
* Constructs surface name from surface flags @surface.
*
* Returns: The requested surface name; #NULL for invalid @whatname; empty
* string for naming style not supporting surfaces. In all cases, the
* returned string must be freed by caller when no longer used.
**/
char*
enca_get_surface_name(EncaSurface surface,
EncaNameStyle whatname)
{
char *s;
size_t i;
switch (whatname) {
/* these don't know/define surfaces so forget it */
case ENCA_NAME_STYLE_CSTOCS:
case ENCA_NAME_STYLE_RFC1345:
case ENCA_NAME_STYLE_ICONV:
case ENCA_NAME_STYLE_MIME:
s = enca_strdup("");
break;
/* human readable name (each on separate line) */
case ENCA_NAME_STYLE_HUMAN:
s = enca_strdup("");
for (i = 0; i < NSURFACES; i++) {
if (SURFACE_INFO[i].bit & surface) {
s = enca_strappend(s, SURFACE_INFO[i].human, "\n", NULL);
}
}
break;
/* canonical name (/recode style) */
case ENCA_NAME_STYLE_ENCA:
s = enca_strdup("");
for (i = 0; i < NSURFACES; i++) {
if ((SURFACE_INFO[i].bit & surface) && SURFACE_INFO[i].enca != NULL) {
s = enca_strappend(s, SURF_SEPARATOR_STR, SURFACE_INFO[i].enca, NULL);
}
}
break;
default:
s = NULL;
break;
}
return s;
}
/**
* enca_charset_properties:
* @charset: A charset.
*
* Returns charset properties.
*
* Returns: The requested charset properties; zero for invalid @charset.
**/
EncaCharsetFlags
enca_charset_properties(int charset)
{
if ((size_t)charset >= NCHARSETS)
return 0;
return CHARSET_INFO[charset].flags;
}
/**
* enca_charset_natural_surface:
* @charset: A charset.
*
* Returns natural surface of a charset.
*
* Returns: The requested charset natural surface (called `implied' in recode),
* zero for invalid @charset or for charsets with no natural surface.
*
* Natrual surface is the surface one expects for a given charset --
* e.g. CRLF EOLs for IBM/Microsoft charsets, CR EOLs for Macintosh
* charsets and LF EOLs for ISO/Unix charsets.
**/
EncaSurface
enca_charset_natural_surface(int charset)
{
if ((size_t)charset >= NCHARSETS)
return 0;
else
return CHARSET_INFO[charset].nsurface;
}
/**
* enca_number_of_charsets:
*
* Returns number of known charsets.
*
* Charsets idetifiers are assigned successively starting from zero, so last
* charset has identifier enca_number_of_charsets() - 1.
*
* Returns: The number of charsets.
**/
size_t
enca_number_of_charsets(void)
{
return NCHARSETS;
}
/**
* enca_parse_encoding_name:
* @name: An encoding specification.
*
* Transofrms encoding specification charset/surface into numeric #EncaEncoding.
*
* When the charset name is not recognized, surfaces are not parsed at all and
* #ENCA_CS_UNKNOWN is returned as charset. However, unrecognized surfaces are
* considered only a minor problem causing %ENCA_SURFACE_UNKNOWN flag to be
* set in the result, beside recognized surface flags.
*
* Returns: The charset/surface pair.
**/
EncaEncoding
enca_parse_encoding_name(const char *name)
{
EncaEncoding enc = ENCODING_UNKNOWN;
char *p, *q;
if (name == NULL)
return enc;
p = enca_strdup(name);
/* separate pure charset name into p */
q = strchr(p, SURF_SEPARATOR);
if (q != NULL)
*q++ = '\0';
enc.charset = enca_name_to_charset(p);
/* surfaces, ony by one */
while (q != NULL && enc.charset != ENCA_CS_UNKNOWN) {
unsigned int surface;
char *r = strchr(p, SURF_SEPARATOR);
if (r != NULL)
*r++ = '\0';
enc.surface |= surface = enca_name_to_surface(q);
q = r;
}
if (!check_surface_consistency(enc.surface))
enc.surface |= ENCA_SURFACE_UNKNOWN;
free(p);
return enc;
}
/**
* squeeze_compare:
* @x: A string.
* @y: Another string.
*
* Compares two strings taking into account only alphanumeric characters.
*
* Returns: Less than zero, more than zero, or zero, when the first string is
* squeeze-alphabeticaly before, after, or equal to second string.
**/
static int
squeeze_compare(const char *x,
const char *y)
{
if (x == NULL || y == NULL) {
if (x == NULL && y == NULL)
return 0;
if (x == NULL)
return -1;
else
return 1;
}
while (*x != '\0' || *y != '\0') {
while (*x != '\0' && !enca_isalnum(*x))
x++;
while (*y != '\0' && !enca_isalnum(*y))
y++;
if (enca_tolower(*x) != enca_tolower(*y))
return (int)enca_tolower(*x) - (int)enca_tolower(*y);
if (*x != '\0')
x++;
if (*y != '\0')
y++;
}
return 0;
}
#if 0
/**
* stable_compare:
* @x: A string.
* @y: Another string.
*
* Compares two strings taking into account only alphanumeric characters first.
*
* When the strings are equal, compares them normally, too. Zero is thus
* returned for really identical strings only.
*
* Returns: Less than zero, more than zero, or zero, when the first string is
* squeeze-alphabeticaly before, after, or equal to second string.
**/
static int
stable_compare(const char *x,
const char *y)
{
int i;
i = squeeze_compare(x, y);
/* to stabilize the sort */
if (i == 0)
return strcmp(x, y);
return i;
}
#endif
/**
* alias_search:
* @alist: Sorted array of strings.
* @n: Size of @alist.
* @s: String to find.
*
* Finds string @s in stable-sorted array of strings.
*
* Returns: Index of @s in @alist; -1 if not found.
**/
static int
alias_search(const char *const *alist,
int n,
const char *s)
{
int i1 = 0;
int i2 = n-1;
int i;
i = squeeze_compare(s, alist[i1]);
if (i < 0)
return -1;
if (i == 0)
return i1;
i = squeeze_compare(s, alist[i2]);
if (i > 0)
return -1;
if (i == 0)
return i2;
while (i1+1 < i2) {
int im = (i1 + i2)/2;
i = squeeze_compare(s, alist[im]);
if (i == 0)
return im;
if (i > 0)
i1 = im;
else
i2 = im;
}
if (squeeze_compare(s, alist[i1+1]) == 0)
return i1+1;
return -1;
}
/**
* enca_name_to_charset:
* @csname: The charset name.
*
* Transforms charset name to numeric charset id.
*
* Returns: The charset id; #ENCA_CS_UNKNOWN when the name is not recognized.
**/
int
enca_name_to_charset(const char *csname)
{
int i;
if (check_encoding_name(csname) <= 0)
return ENCA_CS_UNKNOWN;
i = alias_search(ALIAS_LIST, NALIASES, csname);
return i < 0 ? ENCA_CS_UNKNOWN : INDEX_LIST[i];
}
/**
* enca_name_to_surface:
* @sname: The surface name.
*
* Transforms surface name to numeric surface id.
*
* Returns: The surface id; %ENCA_SURFACE_UNKNOWN when the name is not
* recognized.
**/
EncaSurface
enca_name_to_surface(const char *sname)
{
size_t i;
if (sname == NULL)
return ENCA_SURFACE_UNKNOWN;
for (i = 0; i < NSURFACES; i++) {
if (SURFACE_INFO[i].enca == NULL || *(SURFACE_INFO[i].enca) == '\0')
continue;
if (squeeze_compare(SURFACE_INFO[i].enca, sname))
return SURFACE_INFO[i].bit;
}
return ENCA_SURFACE_UNKNOWN;
}
/**
* check_surface_consistency:
* @surface: The surface.
*
* Checks whether the specified surface makes sense. Unlike recode we don't
* consider /cr/cr/crlf/cr/lf/lf/crlf a reasonable surface.
*
* Returns: Nonzero when the surface is OK, zero othewise.
**/
static int
check_surface_consistency(EncaSurface surface)
{
return count_bits((unsigned long int)surface & ENCA_SURFACE_MASK_EOL) <= 1
&& count_bits((unsigned long int)surface & ENCA_SURFACE_MASK_PERM) <= 1
&& (surface & ENCA_SURFACE_REMOVE) == 0
&& (surface & ENCA_SURFACE_UNKNOWN) == 0;
}
/**
* count_bits:
* @x: A flag field.
*
* Returns: The number of bits set in @x.
**/
static int
count_bits(unsigned long int x)
{
int i = 0;
while (x != 0) {
if (x & 1UL)
i++;
x >>= 1;
}
return i;
}
/**
* check_encoding_name:
* @name: A charset/surface/encoding name.
*
* Checks whether @name contains only allowed characters and counts the
* number of alphanumeric characters in @name.
*
* Returns: The number of alphanumeric characters in @name; -1 when @name
* is invalid.
**/
static int
check_encoding_name(const char *name)
{
size_t i, n;
if (name == NULL)
return -1;
for (i = n = 0; name[i] != '\0'; i++) {
if (!enca_isname(name[i]))
return -1;
if (enca_isalnum(name[i]))
n++;
}
return n;
}
/***** Documentation *********************************************************/
/**
* EncaSurface:
* @ENCA_SURFACE_EOL_CR: End-of-lines are represented with CR's.
* @ENCA_SURFACE_EOL_LF: End-of-lines are represented with LF's.
* @ENCA_SURFACE_EOL_CRLF: End-of-lines are represented with CRLF's.
* @ENCA_SURFACE_EOL_MIX: Several end-of-line types, mixed.
* @ENCA_SURFACE_EOL_BIN: End-of-line concept not applicable (binary data).
* @ENCA_SURFACE_MASK_EOL: Mask for end-of-line surfaces.
* @ENCA_SURFACE_PERM_21: Odd and even bytes swapped.
* @ENCA_SURFACE_PERM_4321: Reversed byte sequence in 4byte words.
* @ENCA_SURFACE_PERM_MIX: Chunks with both endianess, concatenated.
* @ENCA_SURFACE_MASK_PERM: Mask for permutation surfaces.
* @ENCA_SURFACE_QP: Quoted printables.
* @ENCA_SURFACE_HZ: HZ encoded.
* @ENCA_SURFACE_REMOVE: Recode `remove' surface.
* @ENCA_SURFACE_UNKNOWN: Unknown surface.
* @ENCA_SURFACE_MASK_ALL: Mask for all bits, withnout #ENCA_SURFACE_UNKNOWN.
*
* Surface flags.
**/
/**
* EncaNameStyle:
* @ENCA_NAME_STYLE_ENCA: Default, implicit charset name in Enca.
* @ENCA_NAME_STYLE_RFC1345: RFC 1345 or otherwise canonical charset name.
* @ENCA_NAME_STYLE_CSTOCS: Cstocs charset name (may not exist).
* @ENCA_NAME_STYLE_ICONV: Iconv charset name (may not exist).
* @ENCA_NAME_STYLE_HUMAN: Human comprehensible description.
* @ENCA_NAME_STYLE_MIME: Preferred MIME name (may not exist).
*
* Charset naming styles and conventions.
**/
/**
* EncaCharsetFlags:
* @ENCA_CHARSET_7BIT: Characters are represented with 7bit characters.
* @ENCA_CHARSET_8BIT: Characters are represented with bytes.
* @ENCA_CHARSET_16BIT: Characters are represented with 2byte words.
* @ENCA_CHARSET_32BIT: Characters are represented with 4byte words.
* @ENCA_CHARSET_FIXED: One characters consists of one fundamental piece.
* @ENCA_CHARSET_VARIABLE: One character consists of variable number of
* fundamental pieces.
* @ENCA_CHARSET_BINARY: Charset is binary from ASCII viewpoint.
* @ENCA_CHARSET_REGULAR: Language dependent (8bit) charset.
* @ENCA_CHARSET_MULTIBYTE: Multibyte charset.
*
* Charset properties.
*
* Flags %ENCA_CHARSET_7BIT, %ENCA_CHARSET_8BIT, %ENCA_CHARSET_16BIT,
* %ENCA_CHARSET_32BIT tell how many bits a `fundamental piece' consists of.
* This is different from bits per character; r.g. UTF-8 consists of 8bit
* pieces (bytes), but character can be composed from 1 to 6 of them.
**/
/**
* ENCA_CS_UNKNOWN:
*
* Unknown character set id.
*
* Use enca_charset_is_known() to check for unknown charset instead of direct
* comparsion.
**/
/**
* EncaEncoding:
* @charset: Numeric charset identifier.
* @surface: Surface flags.
*
* Encoding, i.e. charset and surface.
*
* This is what enca_analyse() and enca_analyse_const() return.
*
* The @charset field is an opaque numerical charset identifier, which has no
* meaning outside Enca library.
* You will probably want to use it only as enca_charset_name() argument.
* It is only guaranteed not to change meaning
* during program execution time; change of its interpretation (e.g. due to
* addition of new charsets) is not considered API change.
*
* The @surface field is a combination of #EncaSurface flags. You may want
* to ignore it completely; you should use enca_set_interpreted_surfaces()
* to disable weird surfaces then.
**/
/**
* enca_charset_is_known:
* @cs: Charset id.
*
* Expands to nonzero when the charset is known (i.e. it's not
* ENCA_CS_UNKNOWN).
**/
/**
* enca_charset_is_7bit:
* @cs: Charset id.
*
* Expands to nonzero when characters are represented with 7bit characters.
**/
/**
* enca_charset_is_8bit:
* @cs: Charset id.
*
* Expands to nonzero when characters are represented with bytes.
**/
/**
* enca_charset_is_16bit:
* @cs: Charset id.
*
* Expands to nonzero when characters are represented with 2byte words.
**/
/**
* enca_charset_is_32bit:
* @cs: Charset id.
*
* Expands to nonzero when characters are represented with 4byte words.
**/
/**
* enca_charset_is_fixed:
* @cs: Charset id.
*
* Expands to nonzero when one characters consists of one fundamental piece.
**/
/**
* enca_charset_is_variable:
* @cs: Charset id.
*
* Expands to nonzero when one character consists of variable number of
* fundamental pieces.
**/
/**
* enca_charset_is_binary:
* @cs: Charset id.
*
* Expands to nonzero when charset is binary from ASCII viewpoint.
**/
/**
* enca_charset_is_regular:
* @cs: Charset id.
*
* Expands to nonzero when charset is language dependent (8bit) charset.
**/
/**
* enca_charset_is_multibyte:
* @cs: Charset id.
*
* Expands to nonzero when charset is multibyte.
**/
/* vim: ts=2 sw=2 et
*/
|