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 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
|
/*
* uconv version 0.4.9
* Dec 10, 2001 yoshidam supports the tainted status
* Nov 23, 2001 yoshidam appends shortest_flag
* Nov 24, 1999 yoshidam appends eliminate_zwnbsp_flag
* Nov 5, 1999 yoshidam supports SJIS
* Feb 22, 1999 yoshidam
* Jul 24, 1998 yoshidam
* Jun 30, 1998 yoshidam
*
*/
#include "ruby.h"
#include "rubyio.h"
#include <stdio.h>
#include "uconv.h"
#include "ustring.h"
/*#define UTF32*/
#define REPLACEMENT_CHAR (0xFFFD)
static VALUE mUconv;
static int eliminate_zwnbsp_flag = 1;
static int shortest_flag = 1;
static int replace_invalid = 0;
static VALUE eUconvError;
/* Convert UTF-8 to UTF-16-LE (byte order: 21) */
static int
_u8tou16(unsigned char* in, UString* out)
{
unsigned int u = 0;
size_t inlen;
UStr_alloc(out);
inlen = strlen(in);
while (inlen > 0) {
unsigned char c = in[0];
if ((c & 0x80) == 0) { /* 0b0nnnnnnn (7bit) */
if (c == 0)
rb_warn("input may not be UTF-8 text!");
UStr_addChar2(out, c, 0);
in++;
inlen--;
}
else if ((c & 0xe0) == 0xc0 && /* 0b110nnnnn (11bit) */
inlen >= 2 &&
(in[1] & 0xc0) == 0x80) {
if (shortest_flag && (c == 0xc0 || c == 0xc1)) {
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
in += 2;
inlen -= 2;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 31) << 6) | (in[1] & 63);
UStr_addChar2(out, u & 0xff, u >> 8);
in += 2;
inlen -= 2;
}
else if ((c & 0xf0) == 0xe0 && /* 0b1110nnnn (16bit) */
inlen >= 3 &&
(in[1] & 0xc0) == 0x80 &&
(in[2] & 0xc0) == 0x80) {
if (shortest_flag && c == 0xe0 && in[1] < 0xa0) {
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
in += 3;
inlen -= 3;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 15) << 12) | ((in[1] & 63) << 6) | (in[2] & 63);
/* surrogate chars */
if (u >= 0xd800 && u <= 0xdfff) {
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
in += 3;
inlen -= 3;
continue;
}
else
rb_raise(eUconvError, "none-UTF-16 char detected (%04x)", u);
}
UStr_addChar2(out, u & 0xff, u >> 8);
in += 3;
inlen -= 3;
}
else if ((c & 0xf8) == 0xf0 && /* 0b11110nnn (21bit) */
inlen >= 4 &&
(in[1] & 0xc0) == 0x80 &&
(in[2] & 0xc0) == 0x80 &&
(in[3] & 0xc0) == 0x80) {
if (shortest_flag && c == 0xf0 && in[1] < 0x90) {
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
in += 4;
inlen -= 4;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 7) << 18) | ((in[1] & 63) << 12) |
((in[2] & 63) << 6) | (in[3] & 63);
if (u < 0x10000)
UStr_addChar2(out, u & 255, u >> 8);
else if (u < 0x110000) {
unsigned int high = ((u - 0x10000) >> 10) | 0xd800;
unsigned int low = (u & 1023) | 0xdc00;
UStr_addChar4(out, high & 255, high >> 8, low & 255, low >> 8);
}
else {
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
in += 4;
inlen -= 4;
continue;
}
else
rb_raise(eUconvError, "none-UTF-16 char detected (%04x)", u);
}
// UStr_addWCharToU16LE(out, u);
in += 4;
inlen -= 4;
}
else {
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
in++;
inlen--;
}
else
rb_raise(eUconvError, "illegal UTF-8 sequence (%02x)", c);
}
}
return out->len;
}
/* Convert UTF-8 to UCS-4-LE (byte order: 4321) */
static int
_u8tou4(unsigned char* in, UString* out)
{
unsigned int u = 0;
size_t inlen;
UStr_alloc(out);
inlen = strlen(in);
while (inlen > 0) {
unsigned char c = in[0];
if ((c & 0x80) == 0) { /* 0b0nnnnnnn (7bit) */
if (c == 0)
rb_warn("input may not be UTF-8 text!");
UStr_addChar4(out, c, 0, 0, 0);
in++;
inlen--;
}
else if ((c & 0xe0) == 0xc0 && /* 0b110nnnnn (11bit) */
inlen >= 2 &&
(in[1] & 0xc0) == 0x80) {
if (shortest_flag && (c == 0xc0 || c == 0xc1)) {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in += 2;
inlen -= 2;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 31) << 6) | (in[1] & 63);
UStr_addChar4(out, u & 0xff, u >> 8, 0, 0);
in += 2;
inlen -= 2;
}
else if ((c & 0xf0) == 0xe0 && /* 0b1110nnnn (16bit) */
inlen >= 3 &&
(in[1] & 0xc0) == 0x80 &&
(in[2] & 0xc0) == 0x80) {
if (shortest_flag && c == 0xe0 && in[1] < 0xa0) {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in += 3;
inlen -= 3;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 15) << 12) | ((in[1] & 63) << 6) | (in[2] & 63);
/* surrogate chars */
if (u >= 0xd800 && u <= 0xdfff) {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in += 3;
inlen -= 3;
continue;
}
else
rb_raise(eUconvError, "none-UTF-16 char detected (%04x)", u);
}
UStr_addChar4(out, u & 0xff, u >> 8, 0, 0);
in += 3;
inlen -= 3;
}
else if ((c & 0xf8) == 0xf0 && /* 0b11110nnn (21bit) */
inlen >= 4 &&
(in[1] & 0xc0) == 0x80 &&
(in[2] & 0xc0) == 0x80 &&
(in[3] & 0xc0) == 0x80) {
if (shortest_flag && c == 0xf0 && in[1] < 0x90) {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in += 4;
inlen -= 4;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 7) << 18) | ((in[1] & 63) << 12) |
((in[2] & 63) << 6) | (in[3] & 63);
#ifdef UTF32
if (u < 0x110000) { /* UTF-32 ???? */
#endif /* UTF32 */
UStr_addChar4(out, u & 0xff, (u >> 8) & 0xff,
(u >>16) & 0xff, u >> 24);
#ifdef UTF32
}
else {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in += 4;
inlen -= 4;
continue;
}
else
rb_raise(eUconvError, "none-UTF-16 char detected (%04x)", u);
}
#endif /* UTF32 */
in += 4;
inlen -= 4;
}
#ifndef UTF32
else if ((c & 0xfc) == 0xf8 && /* 0b111110nn (26bit) */
inlen >= 5 &&
(in[1] & 0xc0) == 0x80 &&
(in[2] & 0xc0) == 0x80 &&
(in[3] & 0xc0) == 0x80 &&
(in[4] & 0xc0) == 0x80) {
if (shortest_flag && c == 0xf8 && in[1] < 0x88) {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in += 5;
inlen -= 5;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 3) << 24) | ((in[1] & 63) << 18) |
((in[2] & 63) << 12) | ((in[3] & 63) << 6) | (in[4] & 63);
UStr_addChar4(out, u & 0xff, (u >> 8) & 0xff,
(u >>16) & 0xff, u >> 24);
in += 5;
inlen -= 5;
}
else if ((c & 0xfe) == 0xfc && /* 0b1111110n (31bit) */
inlen >= 6 &&
(in[1] & 0xc0) == 0x80 &&
(in[2] & 0xc0) == 0x80 &&
(in[3] & 0xc0) == 0x80 &&
(in[4] & 0xc0) == 0x80 &&
(in[5] & 0xc0) == 0x80) {
if (shortest_flag && c == 0xfc && in[1] < 0x84) {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in += 6;
inlen -= 6;
continue;
}
else
rb_raise(eUconvError, "non-shortest UTF-8 sequence (%02x)", c);
}
u = ((c & 1) << 30) | ((in[1] & 63) << 24) |
((in[2] & 63) << 18) | ((in[3] & 63) << 12) |
((in[4] & 63) << 6) | (in[5] & 63);
UStr_addChar4(out, u & 0xff, (u >> 8) & 0xff,
(u >>16) & 0xff, u >> 24);
in += 6;
inlen -= 6;
}
#endif /* !UTF32 */
else {
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
in++;
inlen--;
}
else
rb_raise(eUconvError, "illegal UTF-8 sequence (%02x)", c);
}
}
return out->len;
}
/* Convert UTF-16-LE (byte order: 21) to UTF-8 */
static int
_u16tou8(unsigned char* in, int len, UString* out, int eliminate_zwnbsp)
{
int i;
UStr_alloc(out);
if (len < 2) return 0;
for (i = 0; i < len; i += 2) {
unsigned int c = in[i] | (in[i+1] << 8);
if (eliminate_zwnbsp && c == 0xfeff) { /* byte order mark */
continue;
}
else if (c < 128) { /* 0x0000-0x00FF */
UStr_addChar(out, c);
}
else if (c < 2048) { /* 0x0100-0x07FF */
unsigned char b2 = c & 63;
unsigned char b1 = c >> 6;
UStr_addChar2(out, b1 | 192, b2 | 128);
}
else if (c >= 0xdc00 && c <= 0xdfff) { /* sole low surrogate */
if (replace_invalid) {
UStr_addWChar(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "invalid surrogate detected");
}
else if (c >= 0xd800 && c <= 0xdbff) { /* high surrogate */
unsigned int low;
unsigned char b1, b2, b3, b4;
if (i + 4 > len) { /* not enough length */
if (replace_invalid) {
UStr_addWChar(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "invalid surrogate detected");
}
low = in[i+2] | (in[i+3] << 8);
if (low < 0xdc00 || low > 0xdfff) { /* not low surrogate */
if (replace_invalid) {
UStr_addWChar(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "invalid surrogate detected");
}
c = (((c & 1023)) << 10 | (low & 1023)) + 0x10000;
b4 = c & 63;
b3 = (c >> 6) & 63;
b2 = (c >> 12) & 63;
b1 = c >> 18;
UStr_addChar4(out, b1 | 240, b2 | 128, b3 | 128, b4 | 128);
i += 2;
}
else { /* 0x0800-0xFFFF */
unsigned char b3 = c & 63;
unsigned char b2 = (c >> 6) & 63;
unsigned char b1 = c >> 12;
UStr_addChar3(out, b1 | 224, b2 | 128, b3 | 128);
}
}
return out->len;
}
/* Convert UCS-4-LE (byte order: 4321) to UTF-8 */
static int
_u4tou8(unsigned char* in, int len, UString* out, int eliminate_zwnbsp)
{
int i;
UStr_alloc(out);
if (len < 4) return 0;
for (i = 0; i < len; i += 4) {
unsigned int c = in[i] | (in[i+1] << 8) |
(in[i+2] << 16) | (in[i+3] << 24);
if (eliminate_zwnbsp && c == 0xfeff) { /* byte order mark */
continue;
}
else if (c < 128) { /* 0x0000-0x00FF */
UStr_addChar(out, c);
}
else if (c < 2048) { /* 0x0100-0x07FF */
unsigned char b2 = c & 63;
unsigned char b1 = c >> 6;
UStr_addChar2(out, b1 | 192, b2 | 128);
}
else if (c >= 0xd800 && c <=0xdfff) { /* surrogate chars */
if (replace_invalid) {
UStr_addWChar(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "illegal char detected (%04x)", c);
}
else if (c < 0x10000) { /* 0x0800-0xFFFF */
unsigned char b3 = c & 63;
unsigned char b2 = (c >> 6) & 63;
unsigned char b1 = c >> 12;
UStr_addChar3(out, b1 | 224, b2 | 128, b3 | 128);
}
#ifdef UTF32
else if (c < 0x110000) { /* 0x00010000-0x0010FFFF */
#else
else if (c < 0x200000) { /* 0x00010000-0x001FFFFF */
#endif
unsigned char b4 = c & 63;
unsigned char b3 = (c >> 6) & 63;
unsigned char b2 = (c >> 12) & 63;
unsigned char b1 = c >> 18;
UStr_addChar4(out, b1 | 240, b2 | 128, b3 | 128, b4 | 128);
}
#ifndef UTF32
else if (c < 0x4000000) { /* 0x00200000-0x03FFFFFF */
unsigned char b5 = c & 63;
unsigned char b4 = (c >> 6) & 63;
unsigned char b3 = (c >> 12) & 63;
unsigned char b2 = (c >> 18) & 63;
unsigned char b1 = c >> 24;
UStr_addChar5(out, b1 | 248, b2 | 128, b3 | 128, b4 | 128, b5 | 128);
}
else if (c < 0x80000000) { /* 0x04000000-0x7FFFFFFF */
unsigned char b6 = c & 63;
unsigned char b5 = (c >> 6) & 63;
unsigned char b4 = (c >> 12) & 63;
unsigned char b3 = (c >> 18) & 63;
unsigned char b2 = (c >> 24) & 63;
unsigned char b1 = (c >> 30) & 63;
UStr_addChar6(out, b1 | 252, b2 | 128, b3 | 128,
b4 | 128, b5 | 128, b6 | 128);
}
#endif /* !UTF32 */
else {
if (replace_invalid)
UStr_addWChar(out, replace_invalid);
else
rb_raise(eUconvError, "non-UCS char detected");
}
}
return out->len;
}
/* Convert UCS-4-LE (byte order: 4321) to UTF-16-LE (byte order: 21) */
/* 10000-10FFFF -> D800-DBFF:DC00-DFFF */
static int
_u4tou16(unsigned char* in, int len, UString* out)
{
int i;
UStr_alloc(out);
if (len < 4) return 0;
for (i = 0; i < len; i += 4) {
unsigned int c = in[i] | (in[i+1] << 8) |
(in[i+2] << 16) | (in[i+3] << 24);
if (c >= 0xd800 && c <= 0xdfff) { /* surrogate chars */
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "none-UTF-16 char detected (%04x)", c);
}
else if (c < 0x10000) { /* 0x0000-0xFFFF */
UStr_addChar2(out, in[i], in[i+1]);
}
else if (c < 0x110000) { /* 0x00010000-0x0010FFFF */
unsigned int high = ((c - 0x10000) >> 10) | 0xd800;
unsigned int low = (c & 1023) | 0xdc00;
UStr_addChar4(out, high & 255, high >> 8, low & 255, low >> 8);
}
else {
if (replace_invalid) {
UStr_addWCharToU16LE(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "non-UTF-16 char detected");
}
}
return out->len;
}
/* Convert UTF-16-LE (byte order: 21) to UCS-4-LE (byte order: 4321) */
/* D800-DBFF:DC00-DFFF -> 10000-10FFFF */
static int
_u16tou4(unsigned char* in, int len, UString* out)
{
int i;
UStr_alloc(out);
if (len < 2) return 0;
for (i = 0; i < len; i += 2) {
unsigned int c = in[i] | (in[i+1] << 8);
if (c >= 0xdc00 && c <= 0xdfff) { /* sole low surrogate */
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "invalid surrogate detected");
}
else if (c >= 0xd800 && c <= 0xdbff) { /* high surrogate */
unsigned int low;
if (i + 4 > len) { /* not enough length */
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "invalid surrogate detected");
}
low = in[i+2] | (in[i+3] << 8);
if (low < 0xdc00 || low > 0xdfff) { /* not low surrogate */
if (replace_invalid) {
UStr_addWCharToU32LE(out, replace_invalid);
continue;
}
else
rb_raise(eUconvError, "invalid surrogate detected");
}
c = (((c & 1023)) << 10 | (low & 1023)) + 0x10000;
UStr_addChar4(out, c & 255, (c >> 8) & 255, c >> 16, 0);
i += 2;
}
else {
UStr_addChar4(out, in[i], in[i+1], 0, 0);
}
}
return out->len;
}
#ifdef USE_EUC
static unsigned short
unknown_euc_handler(const unsigned char* seq)
{
ID mid = rb_intern("unknown_euc_handler");
VALUE ret;
if (!rb_method_boundp(CLASS_OF(mUconv), mid, 0))
return '?';
ret = rb_funcall((VALUE)mUconv, mid, 1, rb_str_new2((char*)seq));
Check_Type(ret, T_FIXNUM);
return FIX2INT(ret);
}
#endif /* USE_EUC */
#ifdef USE_SJIS
static unsigned short
unknown_sjis_handler(const unsigned char* seq)
{
ID mid = rb_intern("unknown_sjis_handler");
VALUE ret;
if (!rb_method_boundp(CLASS_OF(mUconv), mid, 0))
return '?';
ret = rb_funcall((VALUE)mUconv, mid, 1, rb_str_new2((char*)seq));
Check_Type(ret, T_FIXNUM);
return FIX2INT(ret);
}
#endif /* USE_SJIS */
static unsigned char*
unknown_unicode_handler(unsigned short code)
{
ID mid = rb_intern("unknown_unicode_handler");
VALUE ret;
unsigned char* estr;
if (!rb_method_boundp(CLASS_OF(mUconv), mid, 0)) {
estr = (unsigned char*)malloc(2);
estr[0] = '?';
estr[1] = '\0';
return estr;
}
ret = rb_funcall((VALUE)mUconv, mid, 1, INT2FIX(code));
Check_Type(ret, T_STRING);
estr = (unsigned char*)malloc(RSTRING(ret)->len + 1);
memcpy(estr, RSTRING(ret)->ptr, RSTRING(ret)->len + 1);
return estr;
}
#ifdef USE_EUC
static VALUE
uconv_u2toeuc(VALUE obj, VALUE wstr)
{
int len;
unsigned char* u;
VALUE ret;
UString e;
Check_Type(wstr, T_STRING);
u = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
u2e_conv2(u, len, &e, unknown_unicode_handler);
ret = rb_str_new((char*)e.str, e.len);
UStr_free(&e);
OBJ_INFECT(ret, wstr);
return ret;
}
static VALUE
uconv_euctou2(VALUE obj, VALUE estr)
{
int len;
unsigned char* e;
UString u;
VALUE ret;
Check_Type(estr, T_STRING);
e = (unsigned char*)(RSTRING(estr)->ptr);
len = e2u_conv2(e, &u, unknown_euc_handler);
ret = rb_str_new((char*)u.str, u.len);
UStr_free(&u);
OBJ_INFECT(ret, estr);
return ret;
}
#endif /* USE_EUC */
#ifdef USE_SJIS
static VALUE
uconv_u2tosjis(VALUE obj, VALUE wstr)
{
int len;
unsigned char* u;
VALUE ret;
UString s;
Check_Type(wstr, T_STRING);
u = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
u2s_conv2(u, len, &s, unknown_unicode_handler);
ret = rb_str_new((char*)s.str, s.len);
UStr_free(&s);
OBJ_INFECT(ret, wstr);
return ret;
}
static VALUE
uconv_sjistou2(VALUE obj, VALUE sstr)
{
int len;
unsigned char* s;
UString u;
VALUE ret;
Check_Type(sstr, T_STRING);
s = (unsigned char*)(RSTRING(sstr)->ptr);
len = s2u_conv2(s, &u, unknown_sjis_handler);
ret = rb_str_new((char*)u.str, u.len);
UStr_free(&u);
OBJ_INFECT(ret, sstr);
return ret;
}
#endif /* USE_SJIS */
static VALUE
uconv_u2swap(VALUE obj, VALUE wstr)
{
int len;
int i;
unsigned char* u;
unsigned char* out;
VALUE ret;
Check_Type(wstr, T_STRING);
u = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
if (len < 2) return Qnil;
ret = rb_str_new(NULL, len);
out = (unsigned char*)(RSTRING(ret)->ptr);
for (i = 0; i < len; i+=2) {
out[i] = u[i+1];
out[i+1] = u[i];
}
OBJ_INFECT(ret, wstr);
return ret;
}
static VALUE
uconv_u2swap_b(VALUE obj, VALUE wstr)
{
int len;
int i;
unsigned char* u;
Check_Type(wstr, T_STRING);
rb_str_modify(wstr);
u = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
if (len < 2) return Qnil;
for (i = 0; i < len; i+=2) {
register unsigned char tmp = u[i+1];
u[i+1] = u[i];
u[i] = tmp;
}
return wstr;
}
static VALUE
uconv_u4swap(VALUE obj, VALUE wstr)
{
int len;
int i;
unsigned char* u;
unsigned char* out;
VALUE ret;
Check_Type(wstr, T_STRING);
u = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
if (len < 4) return Qnil;
ret = rb_str_new(NULL, len);
out = (unsigned char*)(RSTRING(ret)->ptr);
for (i = 0; i < len; i+=4) {
out[i] = u[i+3];
out[i+1] = u[i+2];
out[i+2] = u[i+1];
out[i+3] = u[i];
}
OBJ_INFECT(ret, wstr);
return ret;
}
static VALUE
uconv_u4swap_b(VALUE obj, VALUE wstr)
{
int len;
int i;
unsigned char* u;
Check_Type(wstr, T_STRING);
rb_str_modify(wstr);
u = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
if (len < 4) return Qnil;
for (i = 0; i < len; i+=4) {
register unsigned char tmp1 = u[i];
register unsigned char tmp2 = u[i+1];
u[i] = u[i+3];
u[i+1] = u[i+2];
u[i+2] = tmp2;
u[i+3] = tmp1;
}
return wstr;
}
static VALUE
uconv_u8tou16(VALUE obj, VALUE ustr)
{
unsigned char* in;
UString out;
VALUE ret;
Check_Type(ustr, T_STRING);
in = (unsigned char*)(RSTRING(ustr)->ptr);
_u8tou16(in, &out);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
OBJ_INFECT(ret, ustr);
return ret;
}
static VALUE
uconv_u16tou8(VALUE obj, VALUE wstr)
{
int len;
unsigned char* in;
UString out;
VALUE ret;
Check_Type(wstr, T_STRING);
in = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
_u16tou8(in, len, &out, eliminate_zwnbsp_flag);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
OBJ_INFECT(ret, wstr);
return ret;
}
static VALUE
uconv_u8tou4(VALUE obj, VALUE ustr)
{
unsigned char* in;
UString out;
VALUE ret;
Check_Type(ustr, T_STRING);
in = (unsigned char*)(RSTRING(ustr)->ptr);
_u8tou4(in, &out);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
OBJ_INFECT(ret, ustr);
return ret;
}
static VALUE
uconv_u4tou8(VALUE obj, VALUE wstr)
{
int len;
unsigned char* in;
UString out;
VALUE ret;
Check_Type(wstr, T_STRING);
in = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
_u4tou8(in, len, &out, eliminate_zwnbsp_flag);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
OBJ_INFECT(ret, wstr);
return ret;
}
static VALUE
uconv_u16tou4(VALUE obj, VALUE wstr)
{
int len;
unsigned char* in;
UString out;
VALUE ret;
Check_Type(wstr, T_STRING);
in = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
_u16tou4(in, len, &out);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
OBJ_INFECT(ret, wstr);
return ret;
}
static VALUE
uconv_u4tou16(VALUE obj, VALUE wstr)
{
int len;
unsigned char* in;
UString out;
VALUE ret;
Check_Type(wstr, T_STRING);
in = (unsigned char*)(RSTRING(wstr)->ptr);
len = RSTRING(wstr)->len;
_u4tou16(in, len, &out);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
OBJ_INFECT(ret, wstr);
return ret;
}
#ifdef USE_EUC
static VALUE
uconv_u8toeuc(VALUE obj, VALUE ustr)
{
unsigned char* in;
UString out;
UString e;
VALUE ret;
Check_Type(ustr, T_STRING);
in = (unsigned char*)(RSTRING(ustr)->ptr);
_u8tou16(in, &out);
u2e_conv2(out.str, out.len, &e, unknown_unicode_handler);
ret = rb_str_new((char*)e.str, e.len);
UStr_free(&e);
UStr_free(&out);
OBJ_INFECT(ret, ustr);
return ret;
}
static VALUE
uconv_euctou8(VALUE obj, VALUE estr)
{
int len;
unsigned char* e;
UString in;
UString out;
VALUE ret;
Check_Type(estr, T_STRING);
e = (unsigned char*)(RSTRING(estr)->ptr);
len = RSTRING(estr)->len;
e2u_conv2(e, &in, unknown_euc_handler);
_u16tou8(in.str, in.len, &out, 1);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
UStr_free(&in);
OBJ_INFECT(ret, estr);
return ret;
}
#endif /* USE_EUC */
#ifdef USE_SJIS
static VALUE
uconv_u8tosjis(VALUE obj, VALUE ustr)
{
unsigned char* in;
UString out;
UString s;
VALUE ret;
Check_Type(ustr, T_STRING);
in = (unsigned char*)(RSTRING(ustr)->ptr);
_u8tou16(in, &out);
u2s_conv2(out.str, out.len, &s, unknown_unicode_handler);
ret = rb_str_new((char*)s.str, s.len);
UStr_free(&s);
UStr_free(&out);
OBJ_INFECT(ret, ustr);
return ret;
}
static VALUE
uconv_sjistou8(VALUE obj, VALUE sstr)
{
int len;
unsigned char* s;
UString in;
UString out;
VALUE ret;
Check_Type(sstr, T_STRING);
s = (unsigned char*)(RSTRING(sstr)->ptr);
len = RSTRING(sstr)->len;
s2u_conv2(s, &in, unknown_sjis_handler);
_u16tou8(in.str, in.len, &out, 1);
ret = rb_str_new((char*)out.str, out.len);
UStr_free(&out);
UStr_free(&in);
OBJ_INFECT(ret, sstr);
return ret;
}
#endif /* USE_SJIS */
static VALUE
get_eliminate_zwnbsp_flag(VALUE obj)
{
if (eliminate_zwnbsp_flag == 0) return Qfalse;
return Qtrue;
}
static VALUE
set_eliminate_zwnbsp_flag(VALUE obj, VALUE flag)
{
if (flag == Qtrue) {
eliminate_zwnbsp_flag = 1;
}
else if (flag == Qfalse) {
eliminate_zwnbsp_flag = 0;
}
else {
rb_raise(rb_eTypeError, "wrong argument type");
}
return flag;
}
static VALUE
get_shortest_flag(VALUE obj)
{
if (shortest_flag == 0) return Qfalse;
return Qtrue;
}
static VALUE
set_shortest_flag(VALUE obj, VALUE flag)
{
if (flag == Qtrue) {
shortest_flag = 1;
}
else if (flag == Qfalse) {
shortest_flag = 0;
}
else {
rb_raise(rb_eTypeError, "wrong argument type");
}
return flag;
}
static VALUE
get_replace_invalid(VALUE obj)
{
if (replace_invalid == 0) return Qnil;
return INT2NUM(replace_invalid);
}
static VALUE
set_replace_invalid(VALUE obj, VALUE uchar)
{
if (uchar == Qnil) {
replace_invalid = 0;
}
else {
replace_invalid = NUM2INT(uchar);
}
return uchar;
}
void
Init_uconv()
{
if (rb_const_defined(rb_cObject, rb_intern("Uconv")) == Qtrue)
mUconv = rb_const_get(rb_cObject, rb_intern("Uconv"));
else
mUconv = rb_define_module("Uconv");
eUconvError = rb_define_class_under(mUconv,
"Error", rb_eStandardError);
#ifdef USE_EUC
rb_define_module_function(mUconv, "u16toeuc", uconv_u2toeuc, 1);
rb_define_module_function(mUconv, "euctou16", uconv_euctou2, 1);
rb_define_module_function(mUconv, "u2toeuc", uconv_u2toeuc, 1);
rb_define_module_function(mUconv, "euctou2", uconv_euctou2, 1);
rb_define_module_function(mUconv, "u8toeuc", uconv_u8toeuc, 1);
rb_define_module_function(mUconv, "euctou8", uconv_euctou8, 1);
#endif
#ifdef USE_SJIS
rb_define_module_function(mUconv, "u16tosjis", uconv_u2tosjis, 1);
rb_define_module_function(mUconv, "sjistou16", uconv_sjistou2, 1);
rb_define_module_function(mUconv, "u2tosjis", uconv_u2tosjis, 1);
rb_define_module_function(mUconv, "sjistou2", uconv_sjistou2, 1);
rb_define_module_function(mUconv, "u8tosjis", uconv_u8tosjis, 1);
rb_define_module_function(mUconv, "sjistou8", uconv_sjistou8, 1);
#endif
rb_define_module_function(mUconv, "u16swap", uconv_u2swap, 1);
rb_define_module_function(mUconv, "u16swap!", uconv_u2swap_b, 1);
rb_define_module_function(mUconv, "u2swap", uconv_u2swap, 1);
rb_define_module_function(mUconv, "u2swap!", uconv_u2swap_b, 1);
rb_define_module_function(mUconv, "u4swap", uconv_u4swap, 1);
rb_define_module_function(mUconv, "u4swap!", uconv_u4swap_b, 1);
rb_define_module_function(mUconv, "u8tou16", uconv_u8tou16, 1);
rb_define_module_function(mUconv, "u8tou2", uconv_u8tou16, 1);
rb_define_module_function(mUconv, "u16tou8", uconv_u16tou8, 1);
rb_define_module_function(mUconv, "u2tou8", uconv_u16tou8, 1);
rb_define_module_function(mUconv, "u8tou4", uconv_u8tou4, 1);
rb_define_module_function(mUconv, "u4tou8", uconv_u4tou8, 1);
rb_define_module_function(mUconv, "u16tou4", uconv_u16tou4, 1);
rb_define_module_function(mUconv, "u4tou16", uconv_u4tou16, 1);
rb_define_module_function(mUconv, "eliminate_zwnbsp",
get_eliminate_zwnbsp_flag, 0);
rb_define_module_function(mUconv, "eliminate_zwnbsp=",
set_eliminate_zwnbsp_flag, 1);
rb_define_module_function(mUconv, "shortest",
get_shortest_flag, 0);
rb_define_module_function(mUconv, "shortest=",
set_shortest_flag, 1);
rb_define_module_function(mUconv, "replace_invalid",
get_replace_invalid, 0);
rb_define_module_function(mUconv, "replace_invalid=",
set_replace_invalid, 1);
rb_define_const(mUconv, "REPLACEMENT_CHAR",
INT2FIX(REPLACEMENT_CHAR));
}
|