1package Encode::Unicode; 2 3use strict; 4use warnings; 5 6our $VERSION = do { my @r = ( q$Revision: 2.20 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r }; 7 8use XSLoader; 9XSLoader::load( __PACKAGE__, $VERSION ); 10 11# 12# Object Generator 8 transcoders all at once! 13# 14 15use Encode (); 16 17our %BOM_Unknown = map { $_ => 1 } qw(UTF-16 UTF-32); 18 19for my $name ( 20 qw(UTF-16 UTF-16BE UTF-16LE 21 UTF-32 UTF-32BE UTF-32LE 22 UCS-2BE UCS-2LE) 23 ) 24{ 25 my ( $size, $endian, $ucs2, $mask ); 26 $name =~ /^(\w+)-(\d+)(\w*)$/o; 27 if ( $ucs2 = ( $1 eq 'UCS' ) ) { 28 $size = 2; 29 } 30 else { 31 $size = $2 / 8; 32 } 33 $endian = ( $3 eq 'BE' ) ? 'n' : ( $3 eq 'LE' ) ? 'v' : ''; 34 $size == 4 and $endian = uc($endian); 35 36 my $obj = bless { 37 Name => $name, 38 size => $size, 39 endian => $endian, 40 ucs2 => $ucs2, 41 } => __PACKAGE__; 42 Encode::define_encoding($obj, $name); 43} 44 45use parent qw(Encode::Encoding); 46 47sub renew { 48 my $self = shift; 49 $BOM_Unknown{ $self->name } or return $self; 50 my $clone = bless {%$self} => ref($self); 51 $clone->{renewed}++; # so the caller knows it is renewed. 52 return $clone; 53} 54 551; 56__END__ 57 58=head1 NAME 59 60Encode::Unicode -- Various Unicode Transformation Formats 61 62=cut 63 64=head1 SYNOPSIS 65 66 use Encode qw/encode decode/; 67 $ucs2 = encode("UCS-2BE", $utf8); 68 $utf8 = decode("UCS-2BE", $ucs2); 69 70=head1 ABSTRACT 71 72This module implements all Character Encoding Schemes of Unicode that 73are officially documented by Unicode Consortium (except, of course, 74for UTF-8, which is a native format in perl). 75 76=over 4 77 78=item L<http://www.unicode.org/glossary/> says: 79 80I<Character Encoding Scheme> A character encoding form plus byte 81serialization. There are Seven character encoding schemes in Unicode: 82UTF-8, UTF-16, UTF-16BE, UTF-16LE, UTF-32 (UCS-4), UTF-32BE (UCS-4BE) and 83UTF-32LE (UCS-4LE), and UTF-7. 84 85Since UTF-7 is a 7-bit (re)encoded version of UTF-16BE, It is not part of 86Unicode's Character Encoding Scheme. It is separately implemented in 87Encode::Unicode::UTF7. For details see L<Encode::Unicode::UTF7>. 88 89=item Quick Reference 90 91 Decodes from ord(N) Encodes chr(N) to... 92 octet/char BOM S.P d800-dfff ord > 0xffff \x{1abcd} == 93 ---------------+-----------------+------------------------------ 94 UCS-2BE 2 N N is bogus Not Available 95 UCS-2LE 2 N N bogus Not Available 96 UTF-16 2/4 Y Y is S.P S.P BE/LE 97 UTF-16BE 2/4 N Y S.P S.P 0xd82a,0xdfcd 98 UTF-16LE 2/4 N Y S.P S.P 0x2ad8,0xcddf 99 UTF-32 4 Y - is bogus As is BE/LE 100 UTF-32BE 4 N - bogus As is 0x0001abcd 101 UTF-32LE 4 N - bogus As is 0xcdab0100 102 UTF-8 1-4 - - bogus >= 4 octets \xf0\x9a\af\8d 103 ---------------+-----------------+------------------------------ 104 105=back 106 107=head1 Size, Endianness, and BOM 108 109You can categorize these CES by 3 criteria: size of each character, 110endianness, and Byte Order Mark. 111 112=head2 by size 113 114UCS-2 is a fixed-length encoding with each character taking 16 bits. 115It B<does not> support I<surrogate pairs>. When a surrogate pair 116is encountered during decode(), its place is filled with \x{FFFD} 117if I<CHECK> is 0, or the routine croaks if I<CHECK> is 1. When a 118character whose ord value is larger than 0xFFFF is encountered, 119its place is filled with \x{FFFD} if I<CHECK> is 0, or the routine 120croaks if I<CHECK> is 1. 121 122UTF-16 is almost the same as UCS-2 but it supports I<surrogate pairs>. 123When it encounters a high surrogate (0xD800-0xDBFF), it fetches the 124following low surrogate (0xDC00-0xDFFF) and C<desurrogate>s them to 125form a character. Bogus surrogates result in death. When \x{10000} 126or above is encountered during encode(), it C<ensurrogate>s them and 127pushes the surrogate pair to the output stream. 128 129UTF-32 (UCS-4) is a fixed-length encoding with each character taking 32 bits. 130Since it is 32-bit, there is no need for I<surrogate pairs>. 131 132=head2 by endianness 133 134The first (and now failed) goal of Unicode was to map all character 135repertoires into a fixed-length integer so that programmers are happy. 136Since each character is either a I<short> or I<long> in C, you have to 137pay attention to the endianness of each platform when you pass data 138to one another. 139 140Anything marked as BE is Big Endian (or network byte order) and LE is 141Little Endian (aka VAX byte order). For anything not marked either 142BE or LE, a character called Byte Order Mark (BOM) indicating the 143endianness is prepended to the string. 144 145CAVEAT: Though BOM in utf8 (\xEF\xBB\xBF) is valid, it is meaningless 146and as of this writing Encode suite just leave it as is (\x{FeFF}). 147 148=over 4 149 150=item BOM as integer when fetched in network byte order 151 152 16 32 bits/char 153 ------------------------- 154 BE 0xFeFF 0x0000FeFF 155 LE 0xFFFe 0xFFFe0000 156 ------------------------- 157 158=back 159 160This modules handles the BOM as follows. 161 162=over 4 163 164=item * 165 166When BE or LE is explicitly stated as the name of encoding, BOM is 167simply treated as a normal character (ZERO WIDTH NO-BREAK SPACE). 168 169=item * 170 171When BE or LE is omitted during decode(), it checks if BOM is at the 172beginning of the string; if one is found, the endianness is set to 173what the BOM says. 174 175=item * 176 177Default Byte Order 178 179When no BOM is found, Encode 2.76 and blow croaked. Since Encode 1802.77, it falls back to BE accordingly to RFC2781 and the Unicode 181Standard version 8.0 182 183=item * 184 185When BE or LE is omitted during encode(), it returns a BE-encoded 186string with BOM prepended. So when you want to encode a whole text 187file, make sure you encode() the whole text at once, not line by line 188or each line, not file, will have a BOM prepended. 189 190=item * 191 192C<UCS-2> is an exception. Unlike others, this is an alias of UCS-2BE. 193UCS-2 is already registered by IANA and others that way. 194 195=back 196 197=head1 Surrogate Pairs 198 199To say the least, surrogate pairs were the biggest mistake of the 200Unicode Consortium. But according to the late Douglas Adams in I<The 201Hitchhiker's Guide to the Galaxy> Trilogy, C<In the beginning the 202Universe was created. This has made a lot of people very angry and 203been widely regarded as a bad move>. Their mistake was not of this 204magnitude so let's forgive them. 205 206(I don't dare make any comparison with Unicode Consortium and the 207Vogons here ;) Or, comparing Encode to Babel Fish is completely 208appropriate -- if you can only stick this into your ear :) 209 210Surrogate pairs were born when the Unicode Consortium finally 211admitted that 16 bits were not big enough to hold all the world's 212character repertoires. But they already made UCS-2 16-bit. What 213do we do? 214 215Back then, the range 0xD800-0xDFFF was not allocated. Let's split 216that range in half and use the first half to represent the C<upper 217half of a character> and the second half to represent the C<lower 218half of a character>. That way, you can represent 1024 * 1024 = 2191048576 more characters. Now we can store character ranges up to 220\x{10ffff} even with 16-bit encodings. This pair of half-character is 221now called a I<surrogate pair> and UTF-16 is the name of the encoding 222that embraces them. 223 224Here is a formula to ensurrogate a Unicode character \x{10000} and 225above; 226 227 $hi = ($uni - 0x10000) / 0x400 + 0xD800; 228 $lo = ($uni - 0x10000) % 0x400 + 0xDC00; 229 230And to desurrogate; 231 232 $uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00); 233 234Note this move has made \x{D800}-\x{DFFF} into a forbidden zone but 235perl does not prohibit the use of characters within this range. To perl, 236every one of \x{0000_0000} up to \x{ffff_ffff} (*) is I<a character>. 237 238 (*) or \x{ffff_ffff_ffff_ffff} if your perl is compiled with 64-bit 239 integer support! 240 241=head1 Error Checking 242 243Unlike most encodings which accept various ways to handle errors, 244Unicode encodings simply croaks. 245 246 % perl -MEncode -e'$_ = "\xfe\xff\xd8\xd9\xda\xdb\0\n"' \ 247 -e'Encode::from_to($_, "utf16","shift_jis", 0); print' 248 UTF-16:Malformed LO surrogate d8d9 at /path/to/Encode.pm line 184. 249 % perl -MEncode -e'$a = "BOM missing"' \ 250 -e' Encode::from_to($a, "utf16", "shift_jis", 0); print' 251 UTF-16:Unrecognised BOM 424f at /path/to/Encode.pm line 184. 252 253Unlike other encodings where mappings are not one-to-one against 254Unicode, UTFs are supposed to map 100% against one another. So Encode 255is more strict on UTFs. 256 257Consider that "division by zero" of Encode :) 258 259=head1 SEE ALSO 260 261L<Encode>, L<Encode::Unicode::UTF7>, L<https://www.unicode.org/glossary/>, 262L<https://www.unicode.org/faq/utf_bom.html>, 263 264RFC 2781 L<http://www.ietf.org/rfc/rfc2781.txt>, 265 266The whole Unicode standard L<https://www.unicode.org/standard/standard.html> 267 268Ch. 6 pp. 275 of C<Programming Perl (3rd Edition)> 269by Tom Christiansen, brian d foy & Larry Wall; 270O'Reilly & Associates; ISBN 978-0-596-00492-7 271 272=cut 273