[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # unicode.pm --
|
---|
| 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
[7798] | 8 | # Copyright (C) 1999-2004 New Zealand Digital Library Project
|
---|
[537] | 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
| 25 |
|
---|
[71] | 26 | # useful functions for dealing with Unicode
|
---|
| 27 |
|
---|
| 28 | # Unicode strings are stored as arrays of scalars as perl
|
---|
| 29 | # lacks characters are 8-bit (currently)
|
---|
| 30 |
|
---|
| 31 | package unicode;
|
---|
[3767] | 32 |
|
---|
[3834] | 33 | eval {require bytes};
|
---|
[3767] | 34 |
|
---|
[1870] | 35 | use encodings;
|
---|
[15894] | 36 | use strict;
|
---|
[2713] | 37 | use util;
|
---|
[18342] | 38 | use MIME::Base64; # for base64 encoding
|
---|
[71] | 39 |
|
---|
[18245] | 40 | no strict 'refs';
|
---|
| 41 |
|
---|
[1223] | 42 | # ascii2unicode takes an (extended) ascii string (ISO-8859-1)
|
---|
| 43 | # and returns a unicode array.
|
---|
[73] | 44 | sub ascii2unicode {
|
---|
| 45 | my ($in) = @_;
|
---|
| 46 | my $out = [];
|
---|
| 47 |
|
---|
| 48 | my $i = 0;
|
---|
| 49 | my $len = length($in);
|
---|
| 50 | while ($i < $len) {
|
---|
| 51 | push (@$out, ord(substr ($in, $i, 1)));
|
---|
| 52 | $i++;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | return $out;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[1870] | 58 | # ascii2utf8 takes a reference to an (extended) ascii string and returns a
|
---|
| 59 | # UTF-8 encoded string. This is just a faster version of
|
---|
| 60 | # "&unicode2utf8(&ascii2unicode($str));"
|
---|
[7815] | 61 | # "Extended ascii" really means "iso_8859_1"
|
---|
[73] | 62 | sub ascii2utf8 {
|
---|
| 63 | my ($in) = @_;
|
---|
| 64 | my $out = "";
|
---|
| 65 |
|
---|
[2483] | 66 | if (!defined($in)|| !defined($$in)) {
|
---|
| 67 | return $out;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[73] | 70 | my ($c);
|
---|
| 71 | my $i = 0;
|
---|
[1870] | 72 | my $len = length($$in);
|
---|
[73] | 73 | while ($i < $len) {
|
---|
[1870] | 74 | $c = ord (substr ($$in, $i, 1));
|
---|
[73] | 75 | if ($c < 0x80) {
|
---|
| 76 | # ascii character
|
---|
| 77 | $out .= chr ($c);
|
---|
| 78 |
|
---|
| 79 | } else {
|
---|
| 80 | # extended ascii character
|
---|
| 81 | $out .= chr (0xc0 + (($c >> 6) & 0x1f));
|
---|
| 82 | $out .= chr (0x80 + ($c & 0x3f));
|
---|
| 83 | }
|
---|
| 84 | $i++;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | return $out;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[71] | 90 | # unicode2utf8 takes a unicode array as input and encodes it
|
---|
| 91 | # using utf-8
|
---|
| 92 | sub unicode2utf8 {
|
---|
| 93 | my ($in) = @_;
|
---|
| 94 | my $out = "";
|
---|
| 95 |
|
---|
[8716] | 96 | foreach my $num (@$in) {
|
---|
[1868] | 97 | next unless defined $num;
|
---|
[71] | 98 | if ($num < 0x80) {
|
---|
| 99 | $out .= chr ($num);
|
---|
| 100 |
|
---|
| 101 | } elsif ($num < 0x800) {
|
---|
| 102 | $out .= chr (0xc0 + (($num >> 6) & 0x1f));
|
---|
| 103 | $out .= chr (0x80 + ($num & 0x3f));
|
---|
| 104 |
|
---|
| 105 | } elsif ($num < 0xFFFF) {
|
---|
| 106 | $out .= chr (0xe0 + (($num >> 12) & 0xf));
|
---|
| 107 | $out .= chr (0x80 + (($num >> 6) & 0x3f));
|
---|
| 108 | $out .= chr (0x80 + ($num & 0x3f));
|
---|
| 109 |
|
---|
| 110 | } else {
|
---|
| 111 | # error, don't encode anything
|
---|
| 112 | die;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | return $out;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | # utf82unicode takes a utf-8 string and produces a unicode
|
---|
| 119 | # array
|
---|
| 120 | sub utf82unicode {
|
---|
| 121 | my ($in) = @_;
|
---|
| 122 | my $out = [];
|
---|
| 123 |
|
---|
| 124 | my $i = 0;
|
---|
| 125 | my ($c1, $c2, $c3);
|
---|
[8716] | 126 | my $len = length($in);
|
---|
[71] | 127 | while ($i < $len) {
|
---|
| 128 | if (($c1 = ord(substr ($in, $i, 1))) < 0x80) {
|
---|
| 129 | # normal ascii character
|
---|
| 130 | push (@$out, $c1);
|
---|
| 131 |
|
---|
| 132 | } elsif ($c1 < 0xc0) {
|
---|
| 133 | # error, was expecting the first byte of an
|
---|
| 134 | # encoded character. Do nothing.
|
---|
| 135 |
|
---|
| 136 | } elsif ($c1 < 0xe0 && $i+1 < $len) {
|
---|
| 137 | # an encoded character with two bytes
|
---|
| 138 | $c2 = ord (substr ($in, $i+1, 1));
|
---|
| 139 | if ($c2 >= 0x80 && $c2 < 0xc0) {
|
---|
| 140 | # everything looks ok
|
---|
| 141 | push (@$out, ((($c1 & 0x1f) << 6) +
|
---|
| 142 | ($c2 & 0x3f)));
|
---|
| 143 | $i++; # gobbled an extra byte
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | } elsif ($c1 < 0xf0 && $i+2 < $len) {
|
---|
| 147 | # an encoded character with three bytes
|
---|
| 148 | $c2 = ord (substr ($in, $i+1, 1));
|
---|
| 149 | $c3 = ord (substr ($in, $i+2, 1));
|
---|
| 150 | if ($c2 >= 0x80 && $c2 < 0xc0 &&
|
---|
| 151 | $c3 >= 0x80 && $c3 < 0xc0) {
|
---|
| 152 | # everything looks ok
|
---|
| 153 | push (@$out, ((($c1 & 0xf) << 12) +
|
---|
| 154 | (($c2 & 0x3f) << 6) +
|
---|
| 155 | ($c3 & 0x3f)));
|
---|
| 156 |
|
---|
| 157 | $i += 2; # gobbled an extra two bytes
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | } else {
|
---|
| 161 | # error, only decode Unicode characters not full UCS.
|
---|
| 162 | # Do nothing.
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | $i++;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | return $out;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | # unicode2ucs2 takes a unicode array and produces a UCS-2
|
---|
| 172 | # unicode string (every two bytes forms a unicode character)
|
---|
| 173 | sub unicode2ucs2 {
|
---|
| 174 | my ($in) = @_;
|
---|
| 175 | my $out = "";
|
---|
| 176 |
|
---|
[8716] | 177 | foreach my $num (@$in) {
|
---|
[71] | 178 | $out .= chr (($num & 0xff00) >> 8);
|
---|
| 179 | $out .= chr ($num & 0xff);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | return $out;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | # ucs22unicode takes a UCS-2 string and produces a unicode array
|
---|
| 186 | sub ucs22unicode {
|
---|
| 187 | my ($in) = @_;
|
---|
| 188 | my $out = [];
|
---|
| 189 |
|
---|
| 190 | my $i = 0;
|
---|
| 191 | my $len = length ($in);
|
---|
| 192 | while ($i+1 < $len) {
|
---|
| 193 | push (@$out, ord (substr($in, $i, 1)) << 8 +
|
---|
| 194 | ord (substr($in, $i+1, 1)));
|
---|
| 195 |
|
---|
| 196 | $i ++;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | return $out;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[1870] | 202 | # takes a reference to a string and returns a reference to a unicode array
|
---|
| 203 | sub convert2unicode {
|
---|
| 204 | my ($encoding, $textref) = @_;
|
---|
[71] | 205 |
|
---|
[1870] | 206 | if (!defined $encodings::encodings->{$encoding}) {
|
---|
| 207 | print STDERR "unicode::convert2unicode: ERROR: Unsupported encoding ($encoding)\n";
|
---|
| 208 | return [];
|
---|
[1227] | 209 | }
|
---|
| 210 |
|
---|
[1870] | 211 | my $encodename = "$encoding-unicode";
|
---|
| 212 | my $enc_info = $encodings::encodings->{$encoding};
|
---|
| 213 | my $mapfile = &util::filename_cat($ENV{'GSDLHOME'}, "mappings",
|
---|
| 214 | "to_uc", $enc_info->{'mapfile'});
|
---|
| 215 | if (!&loadmapencoding ($encodename, $mapfile)) {
|
---|
[10983] | 216 | print STDERR "unicode: ERROR - could not load encoding $encodename: $! $mapfile\n";
|
---|
[1870] | 217 | return [];
|
---|
[1227] | 218 | }
|
---|
[1870] | 219 |
|
---|
| 220 | if (defined $enc_info->{'converter'}) {
|
---|
| 221 | my $converter = $enc_info->{'converter'};
|
---|
| 222 | return &$converter ($encodename, $textref);
|
---|
| 223 | }
|
---|
[1227] | 224 |
|
---|
[8716] | 225 | if ($unicode::translations{$encodename}->{'count'} == 1) {
|
---|
[1870] | 226 | return &singlebyte2unicode ($encodename, $textref);
|
---|
| 227 | } else {
|
---|
| 228 | return &doublebyte2unicode ($encodename, $textref);
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
[1227] | 231 |
|
---|
[1870] | 232 | # singlebyte2unicode converts simple 8 bit encodings where characters below
|
---|
| 233 | # 0x80 are normal ascii characters and the rest are decoded using the
|
---|
| 234 | # appropriate mapping files.
|
---|
| 235 | #
|
---|
| 236 | # Examples of encodings that may be converted using singlebyte2unicode are
|
---|
[4229] | 237 | # the iso-8859 and windows-125* series.
|
---|
[1870] | 238 | sub singlebyte2unicode {
|
---|
| 239 | my ($encodename, $textref) = @_;
|
---|
[1227] | 240 |
|
---|
[1870] | 241 | my @outtext = ();
|
---|
| 242 | my $len = length($$textref);
|
---|
| 243 | my ($c);
|
---|
| 244 | my $i = 0;
|
---|
[1227] | 245 |
|
---|
[1870] | 246 | while ($i < $len) {
|
---|
| 247 | if (($c = ord(substr($$textref, $i, 1))) < 0x80) {
|
---|
| 248 | # normal ascii character
|
---|
| 249 | push (@outtext, $c);
|
---|
| 250 | } else {
|
---|
| 251 | $c = &transchar ($encodename, $c);
|
---|
| 252 | # put a black square if cannot translate
|
---|
| 253 | $c = 0x25A1 if $c == 0;
|
---|
| 254 | push (@outtext, $c);
|
---|
| 255 | }
|
---|
| 256 | $i ++;
|
---|
[1227] | 257 | }
|
---|
[1870] | 258 | return \@outtext;
|
---|
| 259 | }
|
---|
[1227] | 260 |
|
---|
[1870] | 261 | # doublebyte2unicode converts simple two byte encodings where characters
|
---|
| 262 | # below code point 0x80 are single-byte characters and the rest are
|
---|
| 263 | # double-byte characters.
|
---|
| 264 | #
|
---|
| 265 | # Examples of encodings that may be converted using doublebyte2unicode are
|
---|
| 266 | # CJK encodings like GB encoded Chinese and UHC Korean.
|
---|
| 267 | #
|
---|
| 268 | # Note that no error checking is performed to make sure that the input text
|
---|
| 269 | # is valid for the given encoding.
|
---|
| 270 | #
|
---|
| 271 | # Also, encodings that may contain characters of more than two bytes are
|
---|
| 272 | # not supported (any EUC encoded text may in theory contain 3-byte
|
---|
| 273 | # characters but in practice only one and two byte characters are used).
|
---|
| 274 | sub doublebyte2unicode {
|
---|
| 275 | my ($encodename, $textref) = @_;
|
---|
| 276 |
|
---|
| 277 | my @outtext = ();
|
---|
| 278 | my $len = length($$textref);
|
---|
| 279 | my ($c1, $c2);
|
---|
| 280 | my $i = 0;
|
---|
[1227] | 281 |
|
---|
[1870] | 282 | while ($i < $len) {
|
---|
| 283 | if (($c1 = ord(substr($$textref, $i, 1))) >= 0x80) {
|
---|
| 284 | if ($i+1 < $len) {
|
---|
| 285 | # double-byte character
|
---|
| 286 | $c2 = ord(substr($$textref, $i+1, 1));
|
---|
| 287 | my $c = &transchar ($encodename, ($c1 << 8) | $c2);
|
---|
| 288 | # put a black square if cannot translate
|
---|
| 289 | $c = 0x25A1 if $c == 0;
|
---|
| 290 | push (@outtext, $c);
|
---|
| 291 | $i += 2;
|
---|
| 292 |
|
---|
| 293 | } else {
|
---|
| 294 | # error
|
---|
| 295 | print STDERR "unicode: ERROR missing second half of double-byte character\n";
|
---|
| 296 | $i++;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | } else {
|
---|
| 300 | # single-byte character
|
---|
| 301 | push (@outtext, $c1);
|
---|
| 302 | $i++;
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 | return \@outtext;
|
---|
[1227] | 306 | }
|
---|
| 307 |
|
---|
[1870] | 308 | # Shift-JIS to unicode
|
---|
| 309 | # We can't use doublebyte2unicode for Shift-JIS because it uses some
|
---|
| 310 | # single-byte characters above code point 0x80 (i.e. half-width katakana
|
---|
| 311 | # characters in the range 0xA1-0xDF)
|
---|
| 312 | sub shiftjis2unicode {
|
---|
| 313 | my ($encodename, $textref) = @_;
|
---|
| 314 |
|
---|
| 315 | my @outtext = ();
|
---|
| 316 | my $len = length($$textref);
|
---|
| 317 | my ($c1, $c2);
|
---|
| 318 | my $i = 0;
|
---|
[1227] | 319 |
|
---|
[1870] | 320 | while ($i < $len) {
|
---|
| 321 | $c1 = ord(substr($$textref, $i, 1));
|
---|
[1868] | 322 |
|
---|
[1870] | 323 | if (($c1 >= 0xA1 && $c1 <= 0xDF) || $c1 == 0x5c || $c1 == 0x7E) {
|
---|
| 324 | # Single-byte half-width katakana character or
|
---|
| 325 | # JIS Roman yen or overline characters
|
---|
| 326 | my $c = &transchar ($encodename, $c1);
|
---|
| 327 | # - put a black square if cannot translate
|
---|
| 328 | $c = 0x25A1 if $c == 0;
|
---|
| 329 | push (@outtext, $c);
|
---|
| 330 | $i++;
|
---|
[1868] | 331 |
|
---|
[1870] | 332 | } elsif ($c1 < 0x80) {
|
---|
| 333 | # ASCII
|
---|
| 334 | push (@outtext, $c1);
|
---|
| 335 | $i ++;
|
---|
[1868] | 336 |
|
---|
[1870] | 337 | } elsif ($c1 < 0xEF) {
|
---|
| 338 | if ($i+1 < $len) {
|
---|
| 339 | $c2 = ord(substr($$textref, $i+1, 1));
|
---|
| 340 | if (($c2 >= 0x40 && $c2 <= 0x7E) || ($c2 >= 0x80 && $c2 <= 0xFC)) {
|
---|
| 341 | # Double-byte shift-jis character
|
---|
| 342 | my $c = &transchar ($encodename, ($c1 << 8) | $c2);
|
---|
| 343 | # put a black square if cannot translate
|
---|
| 344 | $c = 0x25A1 if $c == 0;
|
---|
| 345 | push (@outtext, $c);
|
---|
| 346 | } else {
|
---|
| 347 | # error
|
---|
| 348 | print STDERR "unicode: ERROR Invalid Shift-JIS character\n";
|
---|
| 349 | }
|
---|
| 350 | $i += 2;
|
---|
| 351 | } else {
|
---|
| 352 | # error
|
---|
| 353 | print STDERR "unicode: ERROR missing second half of Shift-JIS character\n";
|
---|
| 354 | $i ++;
|
---|
| 355 | }
|
---|
| 356 | } else {
|
---|
| 357 | # error
|
---|
| 358 | print STDERR "unicode: ERROR Invalid Shift-JIS character\n";
|
---|
| 359 | $i ++;
|
---|
| 360 | }
|
---|
| 361 | }
|
---|
| 362 | return \@outtext;
|
---|
| 363 | }
|
---|
[1868] | 364 |
|
---|
[1870] | 365 | sub transchar {
|
---|
| 366 | my ($encoding, $from) = @_;
|
---|
| 367 | my $high = ($from / 256) % 256;
|
---|
| 368 | my $low = $from % 256;
|
---|
[1868] | 369 |
|
---|
[8716] | 370 | return 0 unless defined $unicode::translations{$encoding};
|
---|
[1868] | 371 |
|
---|
[8716] | 372 | my $block = $unicode::translations{$encoding}->{'map'};
|
---|
[1868] | 373 |
|
---|
[1870] | 374 | if (ref ($block->[$high]) ne "ARRAY") {
|
---|
| 375 | return 0;
|
---|
| 376 | }
|
---|
| 377 | return $block->[$high]->[$low];
|
---|
| 378 | }
|
---|
[1868] | 379 |
|
---|
| 380 | # %translations is of the form:
|
---|
| 381 | #
|
---|
[1870] | 382 | # encodings{encodingname-encodingname}->{'map'}->blocktranslation
|
---|
[1868] | 383 | # blocktranslation->[[0-255],[256-511], ..., [65280-65535]]
|
---|
| 384 | #
|
---|
| 385 | # Any of the top translation blocks can point to an undefined
|
---|
| 386 | # value. This data structure aims to allow fast translation and
|
---|
| 387 | # efficient storage.
|
---|
[8716] | 388 | %unicode::translations = ();
|
---|
[1868] | 389 |
|
---|
| 390 | # @array256 is used for initialisation, there must be
|
---|
| 391 | # a better way...
|
---|
[4229] | 392 | # What about this?: @array256 = (0) x 256;
|
---|
[8716] | 393 | @unicode::array256 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
[1868] | 394 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 395 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 396 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 397 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 398 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 399 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 400 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 401 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 402 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 403 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 404 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 405 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 406 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 407 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
---|
| 408 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
|
---|
| 409 |
|
---|
| 410 | # returns 1 if successful, 0 if unsuccessful
|
---|
| 411 | sub loadmapencoding {
|
---|
| 412 | my ($encoding, $mapfile) = @_;
|
---|
| 413 |
|
---|
| 414 | # check to see if the encoding has already been loaded
|
---|
[8716] | 415 | return 1 if (defined $unicode::translations{$encoding});
|
---|
[1868] | 416 |
|
---|
[9967] | 417 | if (! -r $mapfile || -d $mapfile) {
|
---|
| 418 | return 0;
|
---|
| 419 | }
|
---|
[1868] | 420 | return 0 unless open (MAPFILE, $mapfile);
|
---|
| 421 | binmode (MAPFILE);
|
---|
| 422 |
|
---|
[8716] | 423 | $unicode::translations{$encoding} = {'map' => [@unicode::array256], 'count' => 0};
|
---|
| 424 | my $block = $unicode::translations{$encoding};
|
---|
[1868] | 425 |
|
---|
| 426 | my ($in,$i,$j);
|
---|
[9967] | 427 | while (1) {
|
---|
| 428 | my $ret=read(MAPFILE, $in, 1);
|
---|
| 429 | if (!defined($ret)) { # error
|
---|
| 430 | print STDERR "unicode.pm: error reading mapfile: $!\n";
|
---|
| 431 | last;
|
---|
| 432 | }
|
---|
| 433 | if ($ret != 1) { last }
|
---|
[1868] | 434 | $i = unpack ("C", $in);
|
---|
[8716] | 435 | $block->{'map'}->[$i] = [@unicode::array256];
|
---|
[1868] | 436 | for ($j=0; $j<256 && read(MAPFILE, $in, 2)==2; $j++) {
|
---|
| 437 | my ($n1, $n2) = unpack ("CC", $in);
|
---|
[1870] | 438 | $block->{'map'}->[$i]->[$j] = ($n1*256) + $n2;
|
---|
[1868] | 439 | }
|
---|
[1870] | 440 | $block->{'count'} ++;
|
---|
[1868] | 441 | }
|
---|
| 442 |
|
---|
| 443 | close (MAPFILE);
|
---|
| 444 | }
|
---|
| 445 |
|
---|
[4229] | 446 | # unicode2singlebyte converts unicode to simple 8 bit encodings where
|
---|
| 447 | # characters below 0x80 are normal ascii characters and the rest are encoded
|
---|
| 448 | # using the appropriate mapping files.
|
---|
| 449 | #
|
---|
| 450 | # Examples of encodings that may be converted using unicode2singlebyte are
|
---|
| 451 | # the iso-8859 and windows-125* series, KOI8-R (Russian), and the Kazakh encoding.
|
---|
| 452 | sub unicode2singlebyte {
|
---|
| 453 | my ($uniref, $encoding) = @_;
|
---|
| 454 |
|
---|
[2237] | 455 | my $outtext = "";
|
---|
[4229] | 456 | my $encodename = "unicode-$encoding";
|
---|
[9967] | 457 |
|
---|
| 458 | if (!exists $encodings::encodings->{$encoding}) {
|
---|
| 459 | print STDERR "unicode.pm: ERROR - unsupported encoding "
|
---|
| 460 | . "'$encoding' requested\n";
|
---|
| 461 | return "";
|
---|
| 462 | }
|
---|
| 463 |
|
---|
| 464 | my $enc_info = $encodings::encodings->{$encoding};
|
---|
[2237] | 465 | my $mapfile = &util::filename_cat($ENV{'GSDLHOME'}, "mappings",
|
---|
| 466 | "from_uc", $enc_info->{'mapfile'});
|
---|
| 467 | if (!&loadmapencoding ($encodename, $mapfile)) {
|
---|
[10983] | 468 | print STDERR "unicode: ERROR - could not load encoding $encodename: $! $mapfile\n";
|
---|
[2237] | 469 | return "";
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 | foreach my $c (@$uniref) {
|
---|
| 473 | if ($c < 0x80) {
|
---|
| 474 | # normal ascii character
|
---|
| 475 | $outtext .= chr($c);
|
---|
| 476 | } else {
|
---|
| 477 | # extended ascii character
|
---|
| 478 | $c = &transchar ($encodename, $c);
|
---|
| 479 |
|
---|
| 480 | # put a question mark if cannot translate
|
---|
| 481 | if ($c == 0) {
|
---|
| 482 | $outtext .= "?";
|
---|
| 483 | } else {
|
---|
| 484 | $outtext .= chr($c);
|
---|
| 485 | }
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 | return $outtext;
|
---|
| 489 | }
|
---|
| 490 |
|
---|
[7798] | 491 |
|
---|
| 492 | # this makes sure that the referenced input string is utf8 encoded, and
|
---|
| 493 | # will change/remove bytes that aren't.
|
---|
| 494 | # returns 0 if the text was already utf8, or 1 if text modified to become utf8
|
---|
| 495 | sub ensure_utf8 {
|
---|
| 496 | my $stringref=shift;
|
---|
| 497 |
|
---|
[8217] | 498 | if (!defined($stringref) || ref($stringref) ne 'SCALAR') {
|
---|
| 499 | return $stringref;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[7798] | 502 | my $value=$$stringref;
|
---|
| 503 |
|
---|
| 504 | my $non_utf8_found = 0;
|
---|
| 505 | $value =~ m/^/g; # to set \G
|
---|
| 506 | while ($value =~ m!\G.*?([\x80-\xff]+)!sg) {
|
---|
| 507 | my $highbytes=$1;
|
---|
| 508 | my $highbyteslength=length($highbytes);
|
---|
| 509 | # make sure this block of high bytes is utf-8
|
---|
| 510 | $highbytes =~ /^/g; # set pos()
|
---|
| 511 | my $byte_replaced = 0;
|
---|
| 512 | while ($highbytes =~
|
---|
[8903] | 513 | m!\G (?: [\xc0-\xdf][\x80-\xbf] | # 2 byte utf-8
|
---|
[7798] | 514 | [\xe0-\xef][\x80-\xbf]{2} | # 3 byte
|
---|
[8903] | 515 | [\xf0-\xf7][\x80-\xbf]{3} | # 4 byte
|
---|
| 516 | [\xf8-\xfb][\x80-\xbf]{4} | # 5 byte
|
---|
| 517 | [\xfc-\xfd][\x80-\xbf]{5} | # 6 byte
|
---|
[7798] | 518 | )*([\x80-\xff])? !xg
|
---|
| 519 | ) {
|
---|
| 520 | # this highbyte is "out-of-place" for valid utf-8
|
---|
| 521 | my $badbyte=$1;
|
---|
| 522 | if (!defined $badbyte) {next} # hit end of string
|
---|
| 523 | my $pos=pos($highbytes);
|
---|
| 524 | # replace bad byte. assume iso-8859-1 -> utf-8
|
---|
| 525 | # ascii2utf8 does "extended ascii"... ie iso-8859-1
|
---|
| 526 | my $replacement=&unicode::ascii2utf8(\$badbyte);
|
---|
| 527 | substr($highbytes, $pos-1, 1, $replacement);
|
---|
| 528 | # update the position to continue searching (for \G)
|
---|
| 529 | pos($highbytes) = $pos+length($replacement)-1;
|
---|
| 530 | $byte_replaced = 1;
|
---|
| 531 | }
|
---|
| 532 | if ($byte_replaced) {
|
---|
| 533 | # replace this block of high bytes in the $value
|
---|
| 534 | $non_utf8_found = 1;
|
---|
| 535 | my $replength=length($highbytes); # we've changed the length
|
---|
| 536 | my $textpos=pos($value); # pos at end of last match
|
---|
| 537 | # replace bad bytes with good bytes
|
---|
| 538 | substr($value, $textpos-$highbyteslength,
|
---|
| 539 | $highbyteslength, $highbytes);
|
---|
| 540 | # update the position to continue searching (for \G)
|
---|
| 541 | pos($value)=$textpos+($replength-$highbyteslength)+1;
|
---|
| 542 | }
|
---|
| 543 | }
|
---|
| 544 |
|
---|
[7817] | 545 | $$stringref = $value;
|
---|
[7798] | 546 | return $non_utf8_found;
|
---|
| 547 | }
|
---|
| 548 |
|
---|
[16553] | 549 | # Returns true (1) if the given string is utf8 and false (0) if it isn't.
|
---|
| 550 | # Does not modify the string parameter.
|
---|
| 551 | sub check_is_utf8 {
|
---|
| 552 | my $value=shift;
|
---|
| 553 |
|
---|
| 554 | if (!defined($value)) {
|
---|
| 555 | return 0; # not utf8 because it is undefined
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | $value =~ m/^/g; # to set \G
|
---|
| 559 | while ($value =~ m!\G.*?([\x80-\xff]+)!sg) {
|
---|
| 560 | my $highbytes=$1;
|
---|
| 561 | # make sure this block of high bytes is utf-8
|
---|
| 562 | $highbytes =~ /^/g; # set pos()
|
---|
| 563 | while ($highbytes =~
|
---|
| 564 | m!\G (?: [\xc0-\xdf][\x80-\xbf] | # 2 byte utf-8
|
---|
| 565 | [\xe0-\xef][\x80-\xbf]{2} | # 3 byte
|
---|
| 566 | [\xf0-\xf7][\x80-\xbf]{3} | # 4 byte
|
---|
| 567 | [\xf8-\xfb][\x80-\xbf]{4} | # 5 byte
|
---|
| 568 | [\xfc-\xfd][\x80-\xbf]{5} | # 6 byte
|
---|
| 569 | )*([\x80-\xff])? !xg
|
---|
| 570 | ) {
|
---|
| 571 | my $badbyte=$1;
|
---|
| 572 | if (defined $badbyte) { # not end of string
|
---|
| 573 | return 0; # non-utf8 found
|
---|
| 574 | }
|
---|
| 575 | }
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | return 1;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
[16672] | 581 | sub url_encode {
|
---|
| 582 | my ($text) = @_;
|
---|
[18338] | 583 |
|
---|
[16900] | 584 | if (!&is_url_encoded($text)) {
|
---|
[18201] | 585 | $text =~ s/([^A-Z0-9\ \.\-\_])/sprintf("%%%02X", ord($1))/iseg;
|
---|
[16900] | 586 | }
|
---|
[16672] | 587 | return $text;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | sub url_decode {
|
---|
| 591 | my ($text) = @_;
|
---|
| 592 |
|
---|
| 593 | $text =~ s/\%([A-F0-9]{2})/pack('C', hex($1))/ige;
|
---|
| 594 | return $text;
|
---|
| 595 | }
|
---|
| 596 |
|
---|
[16900] | 597 | sub is_url_encoded {
|
---|
| 598 | my ($text) = @_;
|
---|
| 599 | return ($text =~ m/\%([A-F0-9]{2})/);
|
---|
| 600 | }
|
---|
| 601 |
|
---|
[16918] | 602 | # When a filename on the filesystem is already URL-encoded, the
|
---|
[18319] | 603 | # URL to it will have %25s in place of every % sign, so that
|
---|
[16918] | 604 | # URLs in html pages can refer to the URL-encoded filename.
|
---|
| 605 | # This method changes the URL reference back into the actual
|
---|
| 606 | # (URL-encoded) filename on the filesystem by replacing %25 with %.
|
---|
| 607 | sub url_to_filename {
|
---|
| 608 | my ($text) =@_;
|
---|
[18319] | 609 | $text =~ s/%25/%/g if is_url_encoded($text);
|
---|
[16933] | 610 | return $text;
|
---|
[16918] | 611 | }
|
---|
| 612 |
|
---|
| 613 | # When a filename on the filesystem is already URL-encoded, the
|
---|
| 614 | # URL to it will have %25s in in place of every % sign, so that
|
---|
| 615 | # URLs in html pages can refer to the URL-encoded filename.
|
---|
| 616 | # Given a (URL-encoded) filename on the filesystem, this subroutine
|
---|
| 617 | # returns the URL reference string for it by replacing % with %25.
|
---|
| 618 | # The output string will be the same as the input string if the input
|
---|
| 619 | # already contains one or more %25s. This is to prevent processing
|
---|
| 620 | # a url more than once this way.
|
---|
| 621 | sub filename_to_url {
|
---|
[18319] | 622 | my ($text, $rename_method) = @_;
|
---|
[16918] | 623 |
|
---|
[18319] | 624 | if(!defined $rename_method || $rename_method eq "url") {
|
---|
| 625 | if($text !~ m/%25/) {
|
---|
| 626 | $text =~ s/%/%25/g;
|
---|
| 627 | }
|
---|
[16918] | 628 | }
|
---|
[16933] | 629 | return $text;
|
---|
[16918] | 630 | }
|
---|
| 631 |
|
---|
[18342] | 632 | sub base64_encode {
|
---|
| 633 | my ($text) = @_;
|
---|
| 634 | if(!is_safe_filename_chars($text)) { #if(!&is_base64_or_alphanumeric($text)) { # a subset
|
---|
| 635 | $text = &MIME::Base64::encode_base64($text);
|
---|
| 636 | }
|
---|
| 637 | return $text;
|
---|
| 638 | }
|
---|
[16918] | 639 |
|
---|
[18342] | 640 | sub base64_decode {
|
---|
| 641 | my ($text) = @_;
|
---|
| 642 | # If the input fits the base64 pattern, this will try decoding it.
|
---|
| 643 | # Still, this does not guarantee the return value is the 'original'
|
---|
| 644 | if(&is_base64_or_alphanumeric($text)) {
|
---|
| 645 | $text = &MIME::Base64::decode_base64($text);
|
---|
| 646 | }
|
---|
| 647 | return $text;
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | # Returns through if the given string is compatible with base64 (which
|
---|
| 651 | # includes regular ASCII alphanumeric values). This method does not
|
---|
| 652 | # guarantee that base64_decoding will return anything meanigful, since
|
---|
| 653 | # this will return true for any simple alphanumeric ASCII string as well.
|
---|
| 654 | sub is_base64_or_alphanumeric {
|
---|
| 655 | my ($text) = @_;
|
---|
| 656 | # base 64 takes alphanumeric and [+/=]
|
---|
| 657 | # (note that in some cases - and _ replace + and /)
|
---|
| 658 | return ($text =~ m/^[A-Za-z0-9\+\/\=]+$/);
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 | # Returns true if the text is base64 chars or additionally contains - and/or _
|
---|
| 662 | sub is_safe_filename_chars {
|
---|
| 663 | my ($text) = @_;
|
---|
| 664 | return ($text =~ m/^[A-Za-z0-9\+\/\=\-\_]+$/); #alphanumeric,[+/=-_]
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[9125] | 667 | sub substr
|
---|
| 668 | {
|
---|
| 669 | my ($utf8_string, $offset, $length) = @_;
|
---|
| 670 |
|
---|
| 671 | my @unicode_string = @{&utf82unicode($utf8_string)};
|
---|
| 672 | my $unicode_string_length = scalar(@unicode_string);
|
---|
| 673 |
|
---|
| 674 | my $substr_start = $offset;
|
---|
| 675 | if ($substr_start >= $unicode_string_length) {
|
---|
| 676 | return "";
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | my $substr_end = $offset + $length - 1;
|
---|
| 680 | if ($substr_end >= $unicode_string_length) {
|
---|
| 681 | $substr_end = $unicode_string_length - 1;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | my @unicode_substring = @unicode_string[$substr_start..$substr_end];
|
---|
| 685 | return &unicode2utf8(\@unicode_substring);
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 |
|
---|
[71] | 689 | 1;
|
---|