bson 6.10.4 → 7.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3301 +0,0 @@
1
- // This is free and unencumbered software released into the public domain.
2
- // See LICENSE.md for more information.
3
-
4
- /**
5
- * @fileoverview Global |this| required for resolving indexes in node.
6
- * @suppress {globalThis}
7
- */
8
- (function(global) {
9
- 'use strict';
10
-
11
- //
12
- // Utilities
13
- //
14
-
15
- /**
16
- * @param {number} a The number to test.
17
- * @param {number} min The minimum value in the range, inclusive.
18
- * @param {number} max The maximum value in the range, inclusive.
19
- * @return {boolean} True if a >= min and a <= max.
20
- */
21
- function inRange(a, min, max) {
22
- return min <= a && a <= max;
23
- }
24
-
25
- /**
26
- * @param {!Array.<*>} array The array to check.
27
- * @param {*} item The item to look for in the array.
28
- * @return {boolean} True if the item appears in the array.
29
- */
30
- function includes(array, item) {
31
- return array.indexOf(item) !== -1;
32
- }
33
-
34
- var floor = Math.floor;
35
-
36
- /**
37
- * @param {*} o
38
- * @return {Object}
39
- */
40
- function ToDictionary(o) {
41
- if (o === undefined) return {};
42
- if (o === Object(o)) return o;
43
- throw TypeError('Could not convert argument to dictionary');
44
- }
45
-
46
- /**
47
- * @param {string} string Input string of UTF-16 code units.
48
- * @return {!Array.<number>} Code points.
49
- */
50
- function stringToCodePoints(string) {
51
- // https://heycam.github.io/webidl/#dfn-obtain-unicode
52
-
53
- // 1. Let S be the DOMString value.
54
- var s = String(string);
55
-
56
- // 2. Let n be the length of S.
57
- var n = s.length;
58
-
59
- // 3. Initialize i to 0.
60
- var i = 0;
61
-
62
- // 4. Initialize U to be an empty sequence of Unicode characters.
63
- var u = [];
64
-
65
- // 5. While i < n:
66
- while (i < n) {
67
-
68
- // 1. Let c be the code unit in S at index i.
69
- var c = s.charCodeAt(i);
70
-
71
- // 2. Depending on the value of c:
72
-
73
- // c < 0xD800 or c > 0xDFFF
74
- if (c < 0xD800 || c > 0xDFFF) {
75
- // Append to U the Unicode character with code point c.
76
- u.push(c);
77
- }
78
-
79
- // 0xDC00 ≤ c ≤ 0xDFFF
80
- else if (0xDC00 <= c && c <= 0xDFFF) {
81
- // Append to U a U+FFFD REPLACEMENT CHARACTER.
82
- u.push(0xFFFD);
83
- }
84
-
85
- // 0xD800 ≤ c ≤ 0xDBFF
86
- else if (0xD800 <= c && c <= 0xDBFF) {
87
- // 1. If i = n−1, then append to U a U+FFFD REPLACEMENT
88
- // CHARACTER.
89
- if (i === n - 1) {
90
- u.push(0xFFFD);
91
- }
92
- // 2. Otherwise, i < n−1:
93
- else {
94
- // 1. Let d be the code unit in S at index i+1.
95
- var d = s.charCodeAt(i + 1);
96
-
97
- // 2. If 0xDC00 ≤ d ≤ 0xDFFF, then:
98
- if (0xDC00 <= d && d <= 0xDFFF) {
99
- // 1. Let a be c & 0x3FF.
100
- var a = c & 0x3FF;
101
-
102
- // 2. Let b be d & 0x3FF.
103
- var b = d & 0x3FF;
104
-
105
- // 3. Append to U the Unicode character with code point
106
- // 2^16+2^10*a+b.
107
- u.push(0x10000 + (a << 10) + b);
108
-
109
- // 4. Set i to i+1.
110
- i += 1;
111
- }
112
-
113
- // 3. Otherwise, d < 0xDC00 or d > 0xDFFF. Append to U a
114
- // U+FFFD REPLACEMENT CHARACTER.
115
- else {
116
- u.push(0xFFFD);
117
- }
118
- }
119
- }
120
-
121
- // 3. Set i to i+1.
122
- i += 1;
123
- }
124
-
125
- // 6. Return U.
126
- return u;
127
- }
128
-
129
- /**
130
- * @param {!Array.<number>} code_points Array of code points.
131
- * @return {string} string String of UTF-16 code units.
132
- */
133
- function codePointsToString(code_points) {
134
- var s = '';
135
- for (var i = 0; i < code_points.length; ++i) {
136
- var cp = code_points[i];
137
- if (cp <= 0xFFFF) {
138
- s += String.fromCharCode(cp);
139
- } else {
140
- cp -= 0x10000;
141
- s += String.fromCharCode((cp >> 10) + 0xD800,
142
- (cp & 0x3FF) + 0xDC00);
143
- }
144
- }
145
- return s;
146
- }
147
-
148
-
149
- //
150
- // Implementation of Encoding specification
151
- // https://encoding.spec.whatwg.org/
152
- //
153
-
154
- //
155
- // 4. Terminology
156
- //
157
-
158
- /**
159
- * An ASCII byte is a byte in the range 0x00 to 0x7F, inclusive.
160
- * @param {number} a The number to test.
161
- * @return {boolean} True if a is in the range 0x00 to 0x7F, inclusive.
162
- */
163
- function isASCIIByte(a) {
164
- return 0x00 <= a && a <= 0x7F;
165
- }
166
-
167
- /**
168
- * An ASCII code point is a code point in the range U+0000 to
169
- * U+007F, inclusive.
170
- */
171
- var isASCIICodePoint = isASCIIByte;
172
-
173
-
174
- /**
175
- * End-of-stream is a special token that signifies no more tokens
176
- * are in the stream.
177
- * @const
178
- */ var end_of_stream = -1;
179
-
180
- /**
181
- * A stream represents an ordered sequence of tokens.
182
- *
183
- * @constructor
184
- * @param {!(Array.<number>|Uint8Array)} tokens Array of tokens that provide
185
- * the stream.
186
- */
187
- function Stream(tokens) {
188
- /** @type {!Array.<number>} */
189
- this.tokens = [].slice.call(tokens);
190
- // Reversed as push/pop is more efficient than shift/unshift.
191
- this.tokens.reverse();
192
- }
193
-
194
- Stream.prototype = {
195
- /**
196
- * @return {boolean} True if end-of-stream has been hit.
197
- */
198
- endOfStream: function() {
199
- return !this.tokens.length;
200
- },
201
-
202
- /**
203
- * When a token is read from a stream, the first token in the
204
- * stream must be returned and subsequently removed, and
205
- * end-of-stream must be returned otherwise.
206
- *
207
- * @return {number} Get the next token from the stream, or
208
- * end_of_stream.
209
- */
210
- read: function() {
211
- if (!this.tokens.length)
212
- return end_of_stream;
213
- return this.tokens.pop();
214
- },
215
-
216
- /**
217
- * When one or more tokens are prepended to a stream, those tokens
218
- * must be inserted, in given order, before the first token in the
219
- * stream.
220
- *
221
- * @param {(number|!Array.<number>)} token The token(s) to prepend to the
222
- * stream.
223
- */
224
- prepend: function(token) {
225
- if (Array.isArray(token)) {
226
- var tokens = /**@type {!Array.<number>}*/(token);
227
- while (tokens.length)
228
- this.tokens.push(tokens.pop());
229
- } else {
230
- this.tokens.push(token);
231
- }
232
- },
233
-
234
- /**
235
- * When one or more tokens are pushed to a stream, those tokens
236
- * must be inserted, in given order, after the last token in the
237
- * stream.
238
- *
239
- * @param {(number|!Array.<number>)} token The tokens(s) to push to the
240
- * stream.
241
- */
242
- push: function(token) {
243
- if (Array.isArray(token)) {
244
- var tokens = /**@type {!Array.<number>}*/(token);
245
- while (tokens.length)
246
- this.tokens.unshift(tokens.shift());
247
- } else {
248
- this.tokens.unshift(token);
249
- }
250
- }
251
- };
252
-
253
- //
254
- // 5. Encodings
255
- //
256
-
257
- // 5.1 Encoders and decoders
258
-
259
- /** @const */
260
- var finished = -1;
261
-
262
- /**
263
- * @param {boolean} fatal If true, decoding errors raise an exception.
264
- * @param {number=} opt_code_point Override the standard fallback code point.
265
- * @return {number} The code point to insert on a decoding error.
266
- */
267
- function decoderError(fatal, opt_code_point) {
268
- if (fatal)
269
- throw TypeError('Decoder error');
270
- return opt_code_point || 0xFFFD;
271
- }
272
-
273
- /**
274
- * @param {number} code_point The code point that could not be encoded.
275
- * @return {number} Always throws, no value is actually returned.
276
- */
277
- function encoderError(code_point) {
278
- throw TypeError('The code point ' + code_point + ' could not be encoded.');
279
- }
280
-
281
- /** @interface */
282
- function Decoder() {}
283
- Decoder.prototype = {
284
- /**
285
- * @param {Stream} stream The stream of bytes being decoded.
286
- * @param {number} bite The next byte read from the stream.
287
- * @return {?(number|!Array.<number>)} The next code point(s)
288
- * decoded, or null if not enough data exists in the input
289
- * stream to decode a complete code point, or |finished|.
290
- */
291
- handler: function(stream, bite) {}
292
- };
293
-
294
- /** @interface */
295
- function Encoder() {}
296
- Encoder.prototype = {
297
- /**
298
- * @param {Stream} stream The stream of code points being encoded.
299
- * @param {number} code_point Next code point read from the stream.
300
- * @return {(number|!Array.<number>)} Byte(s) to emit, or |finished|.
301
- */
302
- handler: function(stream, code_point) {}
303
- };
304
-
305
- // 5.2 Names and labels
306
-
307
- // TODO: Define @typedef for Encoding: {name:string,labels:Array.<string>}
308
- // https://github.com/google/closure-compiler/issues/247
309
-
310
- /**
311
- * @param {string} label The encoding label.
312
- * @return {?{name:string,labels:Array.<string>}}
313
- */
314
- function getEncoding(label) {
315
- // 1. Remove any leading and trailing ASCII whitespace from label.
316
- label = String(label).trim().toLowerCase();
317
-
318
- // 2. If label is an ASCII case-insensitive match for any of the
319
- // labels listed in the table below, return the corresponding
320
- // encoding, and failure otherwise.
321
- if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
322
- return label_to_encoding[label];
323
- }
324
- return null;
325
- }
326
-
327
- /**
328
- * Encodings table: https://encoding.spec.whatwg.org/encodings.json
329
- * @const
330
- * @type {!Array.<{
331
- * heading: string,
332
- * encodings: Array.<{name:string,labels:Array.<string>}>
333
- * }>}
334
- */
335
- var encodings = [
336
- {
337
- "encodings": [
338
- {
339
- "labels": [
340
- "unicode-1-1-utf-8",
341
- "utf-8",
342
- "utf8"
343
- ],
344
- "name": "UTF-8"
345
- }
346
- ],
347
- "heading": "The Encoding"
348
- },
349
- {
350
- "encodings": [
351
- {
352
- "labels": [
353
- "866",
354
- "cp866",
355
- "csibm866",
356
- "ibm866"
357
- ],
358
- "name": "IBM866"
359
- },
360
- {
361
- "labels": [
362
- "csisolatin2",
363
- "iso-8859-2",
364
- "iso-ir-101",
365
- "iso8859-2",
366
- "iso88592",
367
- "iso_8859-2",
368
- "iso_8859-2:1987",
369
- "l2",
370
- "latin2"
371
- ],
372
- "name": "ISO-8859-2"
373
- },
374
- {
375
- "labels": [
376
- "csisolatin3",
377
- "iso-8859-3",
378
- "iso-ir-109",
379
- "iso8859-3",
380
- "iso88593",
381
- "iso_8859-3",
382
- "iso_8859-3:1988",
383
- "l3",
384
- "latin3"
385
- ],
386
- "name": "ISO-8859-3"
387
- },
388
- {
389
- "labels": [
390
- "csisolatin4",
391
- "iso-8859-4",
392
- "iso-ir-110",
393
- "iso8859-4",
394
- "iso88594",
395
- "iso_8859-4",
396
- "iso_8859-4:1988",
397
- "l4",
398
- "latin4"
399
- ],
400
- "name": "ISO-8859-4"
401
- },
402
- {
403
- "labels": [
404
- "csisolatincyrillic",
405
- "cyrillic",
406
- "iso-8859-5",
407
- "iso-ir-144",
408
- "iso8859-5",
409
- "iso88595",
410
- "iso_8859-5",
411
- "iso_8859-5:1988"
412
- ],
413
- "name": "ISO-8859-5"
414
- },
415
- {
416
- "labels": [
417
- "arabic",
418
- "asmo-708",
419
- "csiso88596e",
420
- "csiso88596i",
421
- "csisolatinarabic",
422
- "ecma-114",
423
- "iso-8859-6",
424
- "iso-8859-6-e",
425
- "iso-8859-6-i",
426
- "iso-ir-127",
427
- "iso8859-6",
428
- "iso88596",
429
- "iso_8859-6",
430
- "iso_8859-6:1987"
431
- ],
432
- "name": "ISO-8859-6"
433
- },
434
- {
435
- "labels": [
436
- "csisolatingreek",
437
- "ecma-118",
438
- "elot_928",
439
- "greek",
440
- "greek8",
441
- "iso-8859-7",
442
- "iso-ir-126",
443
- "iso8859-7",
444
- "iso88597",
445
- "iso_8859-7",
446
- "iso_8859-7:1987",
447
- "sun_eu_greek"
448
- ],
449
- "name": "ISO-8859-7"
450
- },
451
- {
452
- "labels": [
453
- "csiso88598e",
454
- "csisolatinhebrew",
455
- "hebrew",
456
- "iso-8859-8",
457
- "iso-8859-8-e",
458
- "iso-ir-138",
459
- "iso8859-8",
460
- "iso88598",
461
- "iso_8859-8",
462
- "iso_8859-8:1988",
463
- "visual"
464
- ],
465
- "name": "ISO-8859-8"
466
- },
467
- {
468
- "labels": [
469
- "csiso88598i",
470
- "iso-8859-8-i",
471
- "logical"
472
- ],
473
- "name": "ISO-8859-8-I"
474
- },
475
- {
476
- "labels": [
477
- "csisolatin6",
478
- "iso-8859-10",
479
- "iso-ir-157",
480
- "iso8859-10",
481
- "iso885910",
482
- "l6",
483
- "latin6"
484
- ],
485
- "name": "ISO-8859-10"
486
- },
487
- {
488
- "labels": [
489
- "iso-8859-13",
490
- "iso8859-13",
491
- "iso885913"
492
- ],
493
- "name": "ISO-8859-13"
494
- },
495
- {
496
- "labels": [
497
- "iso-8859-14",
498
- "iso8859-14",
499
- "iso885914"
500
- ],
501
- "name": "ISO-8859-14"
502
- },
503
- {
504
- "labels": [
505
- "csisolatin9",
506
- "iso-8859-15",
507
- "iso8859-15",
508
- "iso885915",
509
- "iso_8859-15",
510
- "l9"
511
- ],
512
- "name": "ISO-8859-15"
513
- },
514
- {
515
- "labels": [
516
- "iso-8859-16"
517
- ],
518
- "name": "ISO-8859-16"
519
- },
520
- {
521
- "labels": [
522
- "cskoi8r",
523
- "koi",
524
- "koi8",
525
- "koi8-r",
526
- "koi8_r"
527
- ],
528
- "name": "KOI8-R"
529
- },
530
- {
531
- "labels": [
532
- "koi8-ru",
533
- "koi8-u"
534
- ],
535
- "name": "KOI8-U"
536
- },
537
- {
538
- "labels": [
539
- "csmacintosh",
540
- "mac",
541
- "macintosh",
542
- "x-mac-roman"
543
- ],
544
- "name": "macintosh"
545
- },
546
- {
547
- "labels": [
548
- "dos-874",
549
- "iso-8859-11",
550
- "iso8859-11",
551
- "iso885911",
552
- "tis-620",
553
- "windows-874"
554
- ],
555
- "name": "windows-874"
556
- },
557
- {
558
- "labels": [
559
- "cp1250",
560
- "windows-1250",
561
- "x-cp1250"
562
- ],
563
- "name": "windows-1250"
564
- },
565
- {
566
- "labels": [
567
- "cp1251",
568
- "windows-1251",
569
- "x-cp1251"
570
- ],
571
- "name": "windows-1251"
572
- },
573
- {
574
- "labels": [
575
- "ansi_x3.4-1968",
576
- "ascii",
577
- "cp1252",
578
- "cp819",
579
- "csisolatin1",
580
- "ibm819",
581
- "iso-8859-1",
582
- "iso-ir-100",
583
- "iso8859-1",
584
- "iso88591",
585
- "iso_8859-1",
586
- "iso_8859-1:1987",
587
- "l1",
588
- "latin1",
589
- "us-ascii",
590
- "windows-1252",
591
- "x-cp1252"
592
- ],
593
- "name": "windows-1252"
594
- },
595
- {
596
- "labels": [
597
- "cp1253",
598
- "windows-1253",
599
- "x-cp1253"
600
- ],
601
- "name": "windows-1253"
602
- },
603
- {
604
- "labels": [
605
- "cp1254",
606
- "csisolatin5",
607
- "iso-8859-9",
608
- "iso-ir-148",
609
- "iso8859-9",
610
- "iso88599",
611
- "iso_8859-9",
612
- "iso_8859-9:1989",
613
- "l5",
614
- "latin5",
615
- "windows-1254",
616
- "x-cp1254"
617
- ],
618
- "name": "windows-1254"
619
- },
620
- {
621
- "labels": [
622
- "cp1255",
623
- "windows-1255",
624
- "x-cp1255"
625
- ],
626
- "name": "windows-1255"
627
- },
628
- {
629
- "labels": [
630
- "cp1256",
631
- "windows-1256",
632
- "x-cp1256"
633
- ],
634
- "name": "windows-1256"
635
- },
636
- {
637
- "labels": [
638
- "cp1257",
639
- "windows-1257",
640
- "x-cp1257"
641
- ],
642
- "name": "windows-1257"
643
- },
644
- {
645
- "labels": [
646
- "cp1258",
647
- "windows-1258",
648
- "x-cp1258"
649
- ],
650
- "name": "windows-1258"
651
- },
652
- {
653
- "labels": [
654
- "x-mac-cyrillic",
655
- "x-mac-ukrainian"
656
- ],
657
- "name": "x-mac-cyrillic"
658
- }
659
- ],
660
- "heading": "Legacy single-byte encodings"
661
- },
662
- {
663
- "encodings": [
664
- {
665
- "labels": [
666
- "chinese",
667
- "csgb2312",
668
- "csiso58gb231280",
669
- "gb2312",
670
- "gb_2312",
671
- "gb_2312-80",
672
- "gbk",
673
- "iso-ir-58",
674
- "x-gbk"
675
- ],
676
- "name": "GBK"
677
- },
678
- {
679
- "labels": [
680
- "gb18030"
681
- ],
682
- "name": "gb18030"
683
- }
684
- ],
685
- "heading": "Legacy multi-byte Chinese (simplified) encodings"
686
- },
687
- {
688
- "encodings": [
689
- {
690
- "labels": [
691
- "big5",
692
- "big5-hkscs",
693
- "cn-big5",
694
- "csbig5",
695
- "x-x-big5"
696
- ],
697
- "name": "Big5"
698
- }
699
- ],
700
- "heading": "Legacy multi-byte Chinese (traditional) encodings"
701
- },
702
- {
703
- "encodings": [
704
- {
705
- "labels": [
706
- "cseucpkdfmtjapanese",
707
- "euc-jp",
708
- "x-euc-jp"
709
- ],
710
- "name": "EUC-JP"
711
- },
712
- {
713
- "labels": [
714
- "csiso2022jp",
715
- "iso-2022-jp"
716
- ],
717
- "name": "ISO-2022-JP"
718
- },
719
- {
720
- "labels": [
721
- "csshiftjis",
722
- "ms932",
723
- "ms_kanji",
724
- "shift-jis",
725
- "shift_jis",
726
- "sjis",
727
- "windows-31j",
728
- "x-sjis"
729
- ],
730
- "name": "Shift_JIS"
731
- }
732
- ],
733
- "heading": "Legacy multi-byte Japanese encodings"
734
- },
735
- {
736
- "encodings": [
737
- {
738
- "labels": [
739
- "cseuckr",
740
- "csksc56011987",
741
- "euc-kr",
742
- "iso-ir-149",
743
- "korean",
744
- "ks_c_5601-1987",
745
- "ks_c_5601-1989",
746
- "ksc5601",
747
- "ksc_5601",
748
- "windows-949"
749
- ],
750
- "name": "EUC-KR"
751
- }
752
- ],
753
- "heading": "Legacy multi-byte Korean encodings"
754
- },
755
- {
756
- "encodings": [
757
- {
758
- "labels": [
759
- "csiso2022kr",
760
- "hz-gb-2312",
761
- "iso-2022-cn",
762
- "iso-2022-cn-ext",
763
- "iso-2022-kr"
764
- ],
765
- "name": "replacement"
766
- },
767
- {
768
- "labels": [
769
- "utf-16be"
770
- ],
771
- "name": "UTF-16BE"
772
- },
773
- {
774
- "labels": [
775
- "utf-16",
776
- "utf-16le"
777
- ],
778
- "name": "UTF-16LE"
779
- },
780
- {
781
- "labels": [
782
- "x-user-defined"
783
- ],
784
- "name": "x-user-defined"
785
- }
786
- ],
787
- "heading": "Legacy miscellaneous encodings"
788
- }
789
- ];
790
-
791
- // Label to encoding registry.
792
- /** @type {Object.<string,{name:string,labels:Array.<string>}>} */
793
- var label_to_encoding = {};
794
- encodings.forEach(function(category) {
795
- category.encodings.forEach(function(encoding) {
796
- encoding.labels.forEach(function(label) {
797
- label_to_encoding[label] = encoding;
798
- });
799
- });
800
- });
801
-
802
- // Registry of of encoder/decoder factories, by encoding name.
803
- /** @type {Object.<string, function({fatal:boolean}): Encoder>} */
804
- var encoders = {};
805
- /** @type {Object.<string, function({fatal:boolean}): Decoder>} */
806
- var decoders = {};
807
-
808
- //
809
- // 6. Indexes
810
- //
811
-
812
- /**
813
- * @param {number} pointer The |pointer| to search for.
814
- * @param {(!Array.<?number>|undefined)} index The |index| to search within.
815
- * @return {?number} The code point corresponding to |pointer| in |index|,
816
- * or null if |code point| is not in |index|.
817
- */
818
- function indexCodePointFor(pointer, index) {
819
- if (!index) return null;
820
- return index[pointer] || null;
821
- }
822
-
823
- /**
824
- * @param {number} code_point The |code point| to search for.
825
- * @param {!Array.<?number>} index The |index| to search within.
826
- * @return {?number} The first pointer corresponding to |code point| in
827
- * |index|, or null if |code point| is not in |index|.
828
- */
829
- function indexPointerFor(code_point, index) {
830
- var pointer = index.indexOf(code_point);
831
- return pointer === -1 ? null : pointer;
832
- }
833
-
834
- /**
835
- * @param {string} name Name of the index.
836
- * @return {(!Array.<number>|!Array.<Array.<number>>)}
837
- * */
838
- function index(name) {
839
- if (!('encoding-indexes' in global)) {
840
- throw Error("Indexes missing." +
841
- " Did you forget to include encoding-indexes.js first?");
842
- }
843
- return global['encoding-indexes'][name];
844
- }
845
-
846
- /**
847
- * @param {number} pointer The |pointer| to search for in the gb18030 index.
848
- * @return {?number} The code point corresponding to |pointer| in |index|,
849
- * or null if |code point| is not in the gb18030 index.
850
- */
851
- function indexGB18030RangesCodePointFor(pointer) {
852
- // 1. If pointer is greater than 39419 and less than 189000, or
853
- // pointer is greater than 1237575, return null.
854
- if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575))
855
- return null;
856
-
857
- // 2. If pointer is 7457, return code point U+E7C7.
858
- if (pointer === 7457) return 0xE7C7;
859
-
860
- // 3. Let offset be the last pointer in index gb18030 ranges that
861
- // is equal to or less than pointer and let code point offset be
862
- // its corresponding code point.
863
- var offset = 0;
864
- var code_point_offset = 0;
865
- var idx = index('gb18030-ranges');
866
- var i;
867
- for (i = 0; i < idx.length; ++i) {
868
- /** @type {!Array.<number>} */
869
- var entry = idx[i];
870
- if (entry[0] <= pointer) {
871
- offset = entry[0];
872
- code_point_offset = entry[1];
873
- } else {
874
- break;
875
- }
876
- }
877
-
878
- // 4. Return a code point whose value is code point offset +
879
- // pointer − offset.
880
- return code_point_offset + pointer - offset;
881
- }
882
-
883
- /**
884
- * @param {number} code_point The |code point| to locate in the gb18030 index.
885
- * @return {number} The first pointer corresponding to |code point| in the
886
- * gb18030 index.
887
- */
888
- function indexGB18030RangesPointerFor(code_point) {
889
- // 1. If code point is U+E7C7, return pointer 7457.
890
- if (code_point === 0xE7C7) return 7457;
891
-
892
- // 2. Let offset be the last code point in index gb18030 ranges
893
- // that is equal to or less than code point and let pointer offset
894
- // be its corresponding pointer.
895
- var offset = 0;
896
- var pointer_offset = 0;
897
- var idx = index('gb18030-ranges');
898
- var i;
899
- for (i = 0; i < idx.length; ++i) {
900
- /** @type {!Array.<number>} */
901
- var entry = idx[i];
902
- if (entry[1] <= code_point) {
903
- offset = entry[1];
904
- pointer_offset = entry[0];
905
- } else {
906
- break;
907
- }
908
- }
909
-
910
- // 3. Return a pointer whose value is pointer offset + code point
911
- // − offset.
912
- return pointer_offset + code_point - offset;
913
- }
914
-
915
- /**
916
- * @param {number} code_point The |code_point| to search for in the Shift_JIS
917
- * index.
918
- * @return {?number} The code point corresponding to |pointer| in |index|,
919
- * or null if |code point| is not in the Shift_JIS index.
920
- */
921
- function indexShiftJISPointerFor(code_point) {
922
- // 1. Let index be index jis0208 excluding all entries whose
923
- // pointer is in the range 8272 to 8835, inclusive.
924
- shift_jis_index = shift_jis_index ||
925
- index('jis0208').map(function(code_point, pointer) {
926
- return inRange(pointer, 8272, 8835) ? null : code_point;
927
- });
928
- var index_ = shift_jis_index;
929
-
930
- // 2. Return the index pointer for code point in index.
931
- return index_.indexOf(code_point);
932
- }
933
- var shift_jis_index;
934
-
935
- /**
936
- * @param {number} code_point The |code_point| to search for in the big5
937
- * index.
938
- * @return {?number} The code point corresponding to |pointer| in |index|,
939
- * or null if |code point| is not in the big5 index.
940
- */
941
- function indexBig5PointerFor(code_point) {
942
- // 1. Let index be index Big5 excluding all entries whose pointer
943
- big5_index_no_hkscs = big5_index_no_hkscs ||
944
- index('big5').map(function(code_point, pointer) {
945
- return (pointer < (0xA1 - 0x81) * 157) ? null : code_point;
946
- });
947
- var index_ = big5_index_no_hkscs;
948
-
949
- // 2. If code point is U+2550, U+255E, U+2561, U+256A, U+5341, or
950
- // U+5345, return the last pointer corresponding to code point in
951
- // index.
952
- if (code_point === 0x2550 || code_point === 0x255E ||
953
- code_point === 0x2561 || code_point === 0x256A ||
954
- code_point === 0x5341 || code_point === 0x5345) {
955
- return index_.lastIndexOf(code_point);
956
- }
957
-
958
- // 3. Return the index pointer for code point in index.
959
- return indexPointerFor(code_point, index_);
960
- }
961
- var big5_index_no_hkscs;
962
-
963
- //
964
- // 8. API
965
- //
966
-
967
- /** @const */ var DEFAULT_ENCODING = 'utf-8';
968
-
969
- // 8.1 Interface TextDecoder
970
-
971
- /**
972
- * @constructor
973
- * @param {string=} label The label of the encoding;
974
- * defaults to 'utf-8'.
975
- * @param {Object=} options
976
- */
977
- function TextDecoder(label, options) {
978
- // Web IDL conventions
979
- if (!(this instanceof TextDecoder))
980
- throw TypeError('Called as a function. Did you forget \'new\'?');
981
- label = label !== undefined ? String(label) : DEFAULT_ENCODING;
982
- options = ToDictionary(options);
983
-
984
- // A TextDecoder object has an associated encoding, decoder,
985
- // stream, ignore BOM flag (initially unset), BOM seen flag
986
- // (initially unset), error mode (initially replacement), and do
987
- // not flush flag (initially unset).
988
-
989
- /** @private */
990
- this._encoding = null;
991
- /** @private @type {?Decoder} */
992
- this._decoder = null;
993
- /** @private @type {boolean} */
994
- this._ignoreBOM = false;
995
- /** @private @type {boolean} */
996
- this._BOMseen = false;
997
- /** @private @type {string} */
998
- this._error_mode = 'replacement';
999
- /** @private @type {boolean} */
1000
- this._do_not_flush = false;
1001
-
1002
-
1003
- // 1. Let encoding be the result of getting an encoding from
1004
- // label.
1005
- var encoding = getEncoding(label);
1006
-
1007
- // 2. If encoding is failure or replacement, throw a RangeError.
1008
- if (encoding === null || encoding.name === 'replacement')
1009
- throw RangeError('Unknown encoding: ' + label);
1010
- if (!decoders[encoding.name]) {
1011
- throw Error('Decoder not present.' +
1012
- ' Did you forget to include encoding-indexes.js first?');
1013
- }
1014
-
1015
- // 3. Let dec be a new TextDecoder object.
1016
- var dec = this;
1017
-
1018
- // 4. Set dec's encoding to encoding.
1019
- dec._encoding = encoding;
1020
-
1021
- // 5. If options's fatal member is true, set dec's error mode to
1022
- // fatal.
1023
- if (Boolean(options['fatal']))
1024
- dec._error_mode = 'fatal';
1025
-
1026
- // 6. If options's ignoreBOM member is true, set dec's ignore BOM
1027
- // flag.
1028
- if (Boolean(options['ignoreBOM']))
1029
- dec._ignoreBOM = true;
1030
-
1031
- // For pre-ES5 runtimes:
1032
- if (!Object.defineProperty) {
1033
- this.encoding = dec._encoding.name.toLowerCase();
1034
- this.fatal = dec._error_mode === 'fatal';
1035
- this.ignoreBOM = dec._ignoreBOM;
1036
- }
1037
-
1038
- // 7. Return dec.
1039
- return dec;
1040
- }
1041
-
1042
- if (Object.defineProperty) {
1043
- // The encoding attribute's getter must return encoding's name.
1044
- Object.defineProperty(TextDecoder.prototype, 'encoding', {
1045
- /** @this {TextDecoder} */
1046
- get: function() { return this._encoding.name.toLowerCase(); }
1047
- });
1048
-
1049
- // The fatal attribute's getter must return true if error mode
1050
- // is fatal, and false otherwise.
1051
- Object.defineProperty(TextDecoder.prototype, 'fatal', {
1052
- /** @this {TextDecoder} */
1053
- get: function() { return this._error_mode === 'fatal'; }
1054
- });
1055
-
1056
- // The ignoreBOM attribute's getter must return true if ignore
1057
- // BOM flag is set, and false otherwise.
1058
- Object.defineProperty(TextDecoder.prototype, 'ignoreBOM', {
1059
- /** @this {TextDecoder} */
1060
- get: function() { return this._ignoreBOM; }
1061
- });
1062
- }
1063
-
1064
- /**
1065
- * @param {BufferSource=} input The buffer of bytes to decode.
1066
- * @param {Object=} options
1067
- * @return {string} The decoded string.
1068
- */
1069
- TextDecoder.prototype.decode = function decode(input, options) {
1070
- var bytes;
1071
- if (typeof input === 'object' && input instanceof ArrayBuffer) {
1072
- bytes = new Uint8Array(input);
1073
- } else if (typeof input === 'object' && 'buffer' in input &&
1074
- input.buffer instanceof ArrayBuffer) {
1075
- bytes = new Uint8Array(input.buffer,
1076
- input.byteOffset,
1077
- input.byteLength);
1078
- } else {
1079
- bytes = new Uint8Array(0);
1080
- }
1081
-
1082
- options = ToDictionary(options);
1083
-
1084
- // 1. If the do not flush flag is unset, set decoder to a new
1085
- // encoding's decoder, set stream to a new stream, and unset the
1086
- // BOM seen flag.
1087
- if (!this._do_not_flush) {
1088
- this._decoder = decoders[this._encoding.name]({
1089
- fatal: this._error_mode === 'fatal'});
1090
- this._BOMseen = false;
1091
- }
1092
-
1093
- // 2. If options's stream is true, set the do not flush flag, and
1094
- // unset the do not flush flag otherwise.
1095
- this._do_not_flush = Boolean(options['stream']);
1096
-
1097
- // 3. If input is given, push a copy of input to stream.
1098
- // TODO: Align with spec algorithm - maintain stream on instance.
1099
- var input_stream = new Stream(bytes);
1100
-
1101
- // 4. Let output be a new stream.
1102
- var output = [];
1103
-
1104
- /** @type {?(number|!Array.<number>)} */
1105
- var result;
1106
-
1107
- // 5. While true:
1108
- while (true) {
1109
- // 1. Let token be the result of reading from stream.
1110
- var token = input_stream.read();
1111
-
1112
- // 2. If token is end-of-stream and the do not flush flag is
1113
- // set, return output, serialized.
1114
- // TODO: Align with spec algorithm.
1115
- if (token === end_of_stream)
1116
- break;
1117
-
1118
- // 3. Otherwise, run these subsubsteps:
1119
-
1120
- // 1. Let result be the result of processing token for decoder,
1121
- // stream, output, and error mode.
1122
- result = this._decoder.handler(input_stream, token);
1123
-
1124
- // 2. If result is finished, return output, serialized.
1125
- if (result === finished)
1126
- break;
1127
-
1128
- if (result !== null) {
1129
- if (Array.isArray(result))
1130
- output.push.apply(output, /**@type {!Array.<number>}*/(result));
1131
- else
1132
- output.push(result);
1133
- }
1134
-
1135
- // 3. Otherwise, if result is error, throw a TypeError.
1136
- // (Thrown in handler)
1137
-
1138
- // 4. Otherwise, do nothing.
1139
- }
1140
- // TODO: Align with spec algorithm.
1141
- if (!this._do_not_flush) {
1142
- do {
1143
- result = this._decoder.handler(input_stream, input_stream.read());
1144
- if (result === finished)
1145
- break;
1146
- if (result === null)
1147
- continue;
1148
- if (Array.isArray(result))
1149
- output.push.apply(output, /**@type {!Array.<number>}*/(result));
1150
- else
1151
- output.push(result);
1152
- } while (!input_stream.endOfStream());
1153
- this._decoder = null;
1154
- }
1155
-
1156
- // A TextDecoder object also has an associated serialize stream
1157
- // algorithm...
1158
- /**
1159
- * @param {!Array.<number>} stream
1160
- * @return {string}
1161
- * @this {TextDecoder}
1162
- */
1163
- function serializeStream(stream) {
1164
- // 1. Let token be the result of reading from stream.
1165
- // (Done in-place on array, rather than as a stream)
1166
-
1167
- // 2. If encoding is UTF-8, UTF-16BE, or UTF-16LE, and ignore
1168
- // BOM flag and BOM seen flag are unset, run these subsubsteps:
1169
- if (includes(['UTF-8', 'UTF-16LE', 'UTF-16BE'], this._encoding.name) &&
1170
- !this._ignoreBOM && !this._BOMseen) {
1171
- if (stream.length > 0 && stream[0] === 0xFEFF) {
1172
- // 1. If token is U+FEFF, set BOM seen flag.
1173
- this._BOMseen = true;
1174
- stream.shift();
1175
- } else if (stream.length > 0) {
1176
- // 2. Otherwise, if token is not end-of-stream, set BOM seen
1177
- // flag and append token to stream.
1178
- this._BOMseen = true;
1179
- } else {
1180
- // 3. Otherwise, if token is not end-of-stream, append token
1181
- // to output.
1182
- // (no-op)
1183
- }
1184
- }
1185
- // 4. Otherwise, return output.
1186
- return codePointsToString(stream);
1187
- }
1188
-
1189
- return serializeStream.call(this, output);
1190
- };
1191
-
1192
- // 8.2 Interface TextEncoder
1193
-
1194
- /**
1195
- * @constructor
1196
- * @param {string=} label The label of the encoding. NONSTANDARD.
1197
- * @param {Object=} options NONSTANDARD.
1198
- */
1199
- function TextEncoder(label, options) {
1200
- // Web IDL conventions
1201
- if (!(this instanceof TextEncoder))
1202
- throw TypeError('Called as a function. Did you forget \'new\'?');
1203
- options = ToDictionary(options);
1204
-
1205
- // A TextEncoder object has an associated encoding and encoder.
1206
-
1207
- /** @private */
1208
- this._encoding = null;
1209
- /** @private @type {?Encoder} */
1210
- this._encoder = null;
1211
-
1212
- // Non-standard
1213
- /** @private @type {boolean} */
1214
- this._do_not_flush = false;
1215
- /** @private @type {string} */
1216
- this._fatal = Boolean(options['fatal']) ? 'fatal' : 'replacement';
1217
-
1218
- // 1. Let enc be a new TextEncoder object.
1219
- var enc = this;
1220
-
1221
- // 2. Set enc's encoding to UTF-8's encoder.
1222
- if (Boolean(options['NONSTANDARD_allowLegacyEncoding'])) {
1223
- // NONSTANDARD behavior.
1224
- label = label !== undefined ? String(label) : DEFAULT_ENCODING;
1225
- var encoding = getEncoding(label);
1226
- if (encoding === null || encoding.name === 'replacement')
1227
- throw RangeError('Unknown encoding: ' + label);
1228
- if (!encoders[encoding.name]) {
1229
- throw Error('Encoder not present.' +
1230
- ' Did you forget to include encoding-indexes.js first?');
1231
- }
1232
- enc._encoding = encoding;
1233
- } else {
1234
- // Standard behavior.
1235
- enc._encoding = getEncoding('utf-8');
1236
-
1237
- if (label !== undefined && 'console' in global) {
1238
- console.warn('TextEncoder constructor called with encoding label, '
1239
- + 'which is ignored.');
1240
- }
1241
- }
1242
-
1243
- // For pre-ES5 runtimes:
1244
- if (!Object.defineProperty)
1245
- this.encoding = enc._encoding.name.toLowerCase();
1246
-
1247
- // 3. Return enc.
1248
- return enc;
1249
- }
1250
-
1251
- if (Object.defineProperty) {
1252
- // The encoding attribute's getter must return encoding's name.
1253
- Object.defineProperty(TextEncoder.prototype, 'encoding', {
1254
- /** @this {TextEncoder} */
1255
- get: function() { return this._encoding.name.toLowerCase(); }
1256
- });
1257
- }
1258
-
1259
- /**
1260
- * @param {string=} opt_string The string to encode.
1261
- * @param {Object=} options
1262
- * @return {!Uint8Array} Encoded bytes, as a Uint8Array.
1263
- */
1264
- TextEncoder.prototype.encode = function encode(opt_string, options) {
1265
- opt_string = opt_string === undefined ? '' : String(opt_string);
1266
- options = ToDictionary(options);
1267
-
1268
- // NOTE: This option is nonstandard. None of the encodings
1269
- // permitted for encoding (i.e. UTF-8, UTF-16) are stateful when
1270
- // the input is a USVString so streaming is not necessary.
1271
- if (!this._do_not_flush)
1272
- this._encoder = encoders[this._encoding.name]({
1273
- fatal: this._fatal === 'fatal'});
1274
- this._do_not_flush = Boolean(options['stream']);
1275
-
1276
- // 1. Convert input to a stream.
1277
- var input = new Stream(stringToCodePoints(opt_string));
1278
-
1279
- // 2. Let output be a new stream
1280
- var output = [];
1281
-
1282
- /** @type {?(number|!Array.<number>)} */
1283
- var result;
1284
- // 3. While true, run these substeps:
1285
- while (true) {
1286
- // 1. Let token be the result of reading from input.
1287
- var token = input.read();
1288
- if (token === end_of_stream)
1289
- break;
1290
- // 2. Let result be the result of processing token for encoder,
1291
- // input, output.
1292
- result = this._encoder.handler(input, token);
1293
- if (result === finished)
1294
- break;
1295
- if (Array.isArray(result))
1296
- output.push.apply(output, /**@type {!Array.<number>}*/(result));
1297
- else
1298
- output.push(result);
1299
- }
1300
- // TODO: Align with spec algorithm.
1301
- if (!this._do_not_flush) {
1302
- while (true) {
1303
- result = this._encoder.handler(input, input.read());
1304
- if (result === finished)
1305
- break;
1306
- if (Array.isArray(result))
1307
- output.push.apply(output, /**@type {!Array.<number>}*/(result));
1308
- else
1309
- output.push(result);
1310
- }
1311
- this._encoder = null;
1312
- }
1313
- // 3. If result is finished, convert output into a byte sequence,
1314
- // and then return a Uint8Array object wrapping an ArrayBuffer
1315
- // containing output.
1316
- return new Uint8Array(output);
1317
- };
1318
-
1319
-
1320
- //
1321
- // 9. The encoding
1322
- //
1323
-
1324
- // 9.1 utf-8
1325
-
1326
- // 9.1.1 utf-8 decoder
1327
- /**
1328
- * @constructor
1329
- * @implements {Decoder}
1330
- * @param {{fatal: boolean}} options
1331
- */
1332
- function UTF8Decoder(options) {
1333
- var fatal = options.fatal;
1334
-
1335
- // utf-8's decoder's has an associated utf-8 code point, utf-8
1336
- // bytes seen, and utf-8 bytes needed (all initially 0), a utf-8
1337
- // lower boundary (initially 0x80), and a utf-8 upper boundary
1338
- // (initially 0xBF).
1339
- var /** @type {number} */ utf8_code_point = 0,
1340
- /** @type {number} */ utf8_bytes_seen = 0,
1341
- /** @type {number} */ utf8_bytes_needed = 0,
1342
- /** @type {number} */ utf8_lower_boundary = 0x80,
1343
- /** @type {number} */ utf8_upper_boundary = 0xBF;
1344
-
1345
- /**
1346
- * @param {Stream} stream The stream of bytes being decoded.
1347
- * @param {number} bite The next byte read from the stream.
1348
- * @return {?(number|!Array.<number>)} The next code point(s)
1349
- * decoded, or null if not enough data exists in the input
1350
- * stream to decode a complete code point.
1351
- */
1352
- this.handler = function(stream, bite) {
1353
- // 1. If byte is end-of-stream and utf-8 bytes needed is not 0,
1354
- // set utf-8 bytes needed to 0 and return error.
1355
- if (bite === end_of_stream && utf8_bytes_needed !== 0) {
1356
- utf8_bytes_needed = 0;
1357
- return decoderError(fatal);
1358
- }
1359
-
1360
- // 2. If byte is end-of-stream, return finished.
1361
- if (bite === end_of_stream)
1362
- return finished;
1363
-
1364
- // 3. If utf-8 bytes needed is 0, based on byte:
1365
- if (utf8_bytes_needed === 0) {
1366
-
1367
- // 0x00 to 0x7F
1368
- if (inRange(bite, 0x00, 0x7F)) {
1369
- // Return a code point whose value is byte.
1370
- return bite;
1371
- }
1372
-
1373
- // 0xC2 to 0xDF
1374
- else if (inRange(bite, 0xC2, 0xDF)) {
1375
- // 1. Set utf-8 bytes needed to 1.
1376
- utf8_bytes_needed = 1;
1377
-
1378
- // 2. Set UTF-8 code point to byte & 0x1F.
1379
- utf8_code_point = bite & 0x1F;
1380
- }
1381
-
1382
- // 0xE0 to 0xEF
1383
- else if (inRange(bite, 0xE0, 0xEF)) {
1384
- // 1. If byte is 0xE0, set utf-8 lower boundary to 0xA0.
1385
- if (bite === 0xE0)
1386
- utf8_lower_boundary = 0xA0;
1387
- // 2. If byte is 0xED, set utf-8 upper boundary to 0x9F.
1388
- if (bite === 0xED)
1389
- utf8_upper_boundary = 0x9F;
1390
- // 3. Set utf-8 bytes needed to 2.
1391
- utf8_bytes_needed = 2;
1392
- // 4. Set UTF-8 code point to byte & 0xF.
1393
- utf8_code_point = bite & 0xF;
1394
- }
1395
-
1396
- // 0xF0 to 0xF4
1397
- else if (inRange(bite, 0xF0, 0xF4)) {
1398
- // 1. If byte is 0xF0, set utf-8 lower boundary to 0x90.
1399
- if (bite === 0xF0)
1400
- utf8_lower_boundary = 0x90;
1401
- // 2. If byte is 0xF4, set utf-8 upper boundary to 0x8F.
1402
- if (bite === 0xF4)
1403
- utf8_upper_boundary = 0x8F;
1404
- // 3. Set utf-8 bytes needed to 3.
1405
- utf8_bytes_needed = 3;
1406
- // 4. Set UTF-8 code point to byte & 0x7.
1407
- utf8_code_point = bite & 0x7;
1408
- }
1409
-
1410
- // Otherwise
1411
- else {
1412
- // Return error.
1413
- return decoderError(fatal);
1414
- }
1415
-
1416
- // Return continue.
1417
- return null;
1418
- }
1419
-
1420
- // 4. If byte is not in the range utf-8 lower boundary to utf-8
1421
- // upper boundary, inclusive, run these substeps:
1422
- if (!inRange(bite, utf8_lower_boundary, utf8_upper_boundary)) {
1423
-
1424
- // 1. Set utf-8 code point, utf-8 bytes needed, and utf-8
1425
- // bytes seen to 0, set utf-8 lower boundary to 0x80, and set
1426
- // utf-8 upper boundary to 0xBF.
1427
- utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
1428
- utf8_lower_boundary = 0x80;
1429
- utf8_upper_boundary = 0xBF;
1430
-
1431
- // 2. Prepend byte to stream.
1432
- stream.prepend(bite);
1433
-
1434
- // 3. Return error.
1435
- return decoderError(fatal);
1436
- }
1437
-
1438
- // 5. Set utf-8 lower boundary to 0x80 and utf-8 upper boundary
1439
- // to 0xBF.
1440
- utf8_lower_boundary = 0x80;
1441
- utf8_upper_boundary = 0xBF;
1442
-
1443
- // 6. Set UTF-8 code point to (UTF-8 code point << 6) | (byte &
1444
- // 0x3F)
1445
- utf8_code_point = (utf8_code_point << 6) | (bite & 0x3F);
1446
-
1447
- // 7. Increase utf-8 bytes seen by one.
1448
- utf8_bytes_seen += 1;
1449
-
1450
- // 8. If utf-8 bytes seen is not equal to utf-8 bytes needed,
1451
- // continue.
1452
- if (utf8_bytes_seen !== utf8_bytes_needed)
1453
- return null;
1454
-
1455
- // 9. Let code point be utf-8 code point.
1456
- var code_point = utf8_code_point;
1457
-
1458
- // 10. Set utf-8 code point, utf-8 bytes needed, and utf-8 bytes
1459
- // seen to 0.
1460
- utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
1461
-
1462
- // 11. Return a code point whose value is code point.
1463
- return code_point;
1464
- };
1465
- }
1466
-
1467
- // 9.1.2 utf-8 encoder
1468
- /**
1469
- * @constructor
1470
- * @implements {Encoder}
1471
- * @param {{fatal: boolean}} options
1472
- */
1473
- function UTF8Encoder(options) {
1474
- var fatal = options.fatal;
1475
- /**
1476
- * @param {Stream} stream Input stream.
1477
- * @param {number} code_point Next code point read from the stream.
1478
- * @return {(number|!Array.<number>)} Byte(s) to emit.
1479
- */
1480
- this.handler = function(stream, code_point) {
1481
- // 1. If code point is end-of-stream, return finished.
1482
- if (code_point === end_of_stream)
1483
- return finished;
1484
-
1485
- // 2. If code point is an ASCII code point, return a byte whose
1486
- // value is code point.
1487
- if (isASCIICodePoint(code_point))
1488
- return code_point;
1489
-
1490
- // 3. Set count and offset based on the range code point is in:
1491
- var count, offset;
1492
- // U+0080 to U+07FF, inclusive:
1493
- if (inRange(code_point, 0x0080, 0x07FF)) {
1494
- // 1 and 0xC0
1495
- count = 1;
1496
- offset = 0xC0;
1497
- }
1498
- // U+0800 to U+FFFF, inclusive:
1499
- else if (inRange(code_point, 0x0800, 0xFFFF)) {
1500
- // 2 and 0xE0
1501
- count = 2;
1502
- offset = 0xE0;
1503
- }
1504
- // U+10000 to U+10FFFF, inclusive:
1505
- else if (inRange(code_point, 0x10000, 0x10FFFF)) {
1506
- // 3 and 0xF0
1507
- count = 3;
1508
- offset = 0xF0;
1509
- }
1510
-
1511
- // 4. Let bytes be a byte sequence whose first byte is (code
1512
- // point >> (6 × count)) + offset.
1513
- var bytes = [(code_point >> (6 * count)) + offset];
1514
-
1515
- // 5. Run these substeps while count is greater than 0:
1516
- while (count > 0) {
1517
-
1518
- // 1. Set temp to code point >> (6 × (count − 1)).
1519
- var temp = code_point >> (6 * (count - 1));
1520
-
1521
- // 2. Append to bytes 0x80 | (temp & 0x3F).
1522
- bytes.push(0x80 | (temp & 0x3F));
1523
-
1524
- // 3. Decrease count by one.
1525
- count -= 1;
1526
- }
1527
-
1528
- // 6. Return bytes bytes, in order.
1529
- return bytes;
1530
- };
1531
- }
1532
-
1533
- /** @param {{fatal: boolean}} options */
1534
- encoders['UTF-8'] = function(options) {
1535
- return new UTF8Encoder(options);
1536
- };
1537
- /** @param {{fatal: boolean}} options */
1538
- decoders['UTF-8'] = function(options) {
1539
- return new UTF8Decoder(options);
1540
- };
1541
-
1542
- //
1543
- // 10. Legacy single-byte encodings
1544
- //
1545
-
1546
- // 10.1 single-byte decoder
1547
- /**
1548
- * @constructor
1549
- * @implements {Decoder}
1550
- * @param {!Array.<number>} index The encoding index.
1551
- * @param {{fatal: boolean}} options
1552
- */
1553
- function SingleByteDecoder(index, options) {
1554
- var fatal = options.fatal;
1555
- /**
1556
- * @param {Stream} stream The stream of bytes being decoded.
1557
- * @param {number} bite The next byte read from the stream.
1558
- * @return {?(number|!Array.<number>)} The next code point(s)
1559
- * decoded, or null if not enough data exists in the input
1560
- * stream to decode a complete code point.
1561
- */
1562
- this.handler = function(stream, bite) {
1563
- // 1. If byte is end-of-stream, return finished.
1564
- if (bite === end_of_stream)
1565
- return finished;
1566
-
1567
- // 2. If byte is an ASCII byte, return a code point whose value
1568
- // is byte.
1569
- if (isASCIIByte(bite))
1570
- return bite;
1571
-
1572
- // 3. Let code point be the index code point for byte − 0x80 in
1573
- // index single-byte.
1574
- var code_point = index[bite - 0x80];
1575
-
1576
- // 4. If code point is null, return error.
1577
- if (code_point === null)
1578
- return decoderError(fatal);
1579
-
1580
- // 5. Return a code point whose value is code point.
1581
- return code_point;
1582
- };
1583
- }
1584
-
1585
- // 10.2 single-byte encoder
1586
- /**
1587
- * @constructor
1588
- * @implements {Encoder}
1589
- * @param {!Array.<?number>} index The encoding index.
1590
- * @param {{fatal: boolean}} options
1591
- */
1592
- function SingleByteEncoder(index, options) {
1593
- var fatal = options.fatal;
1594
- /**
1595
- * @param {Stream} stream Input stream.
1596
- * @param {number} code_point Next code point read from the stream.
1597
- * @return {(number|!Array.<number>)} Byte(s) to emit.
1598
- */
1599
- this.handler = function(stream, code_point) {
1600
- // 1. If code point is end-of-stream, return finished.
1601
- if (code_point === end_of_stream)
1602
- return finished;
1603
-
1604
- // 2. If code point is an ASCII code point, return a byte whose
1605
- // value is code point.
1606
- if (isASCIICodePoint(code_point))
1607
- return code_point;
1608
-
1609
- // 3. Let pointer be the index pointer for code point in index
1610
- // single-byte.
1611
- var pointer = indexPointerFor(code_point, index);
1612
-
1613
- // 4. If pointer is null, return error with code point.
1614
- if (pointer === null)
1615
- encoderError(code_point);
1616
-
1617
- // 5. Return a byte whose value is pointer + 0x80.
1618
- return pointer + 0x80;
1619
- };
1620
- }
1621
-
1622
- (function() {
1623
- if (!('encoding-indexes' in global))
1624
- return;
1625
- encodings.forEach(function(category) {
1626
- if (category.heading !== 'Legacy single-byte encodings')
1627
- return;
1628
- category.encodings.forEach(function(encoding) {
1629
- var name = encoding.name;
1630
- var idx = index(name.toLowerCase());
1631
- /** @param {{fatal: boolean}} options */
1632
- decoders[name] = function(options) {
1633
- return new SingleByteDecoder(idx, options);
1634
- };
1635
- /** @param {{fatal: boolean}} options */
1636
- encoders[name] = function(options) {
1637
- return new SingleByteEncoder(idx, options);
1638
- };
1639
- });
1640
- });
1641
- }());
1642
-
1643
- //
1644
- // 11. Legacy multi-byte Chinese (simplified) encodings
1645
- //
1646
-
1647
- // 11.1 gbk
1648
-
1649
- // 11.1.1 gbk decoder
1650
- // gbk's decoder is gb18030's decoder.
1651
- /** @param {{fatal: boolean}} options */
1652
- decoders['GBK'] = function(options) {
1653
- return new GB18030Decoder(options);
1654
- };
1655
-
1656
- // 11.1.2 gbk encoder
1657
- // gbk's encoder is gb18030's encoder with its gbk flag set.
1658
- /** @param {{fatal: boolean}} options */
1659
- encoders['GBK'] = function(options) {
1660
- return new GB18030Encoder(options, true);
1661
- };
1662
-
1663
- // 11.2 gb18030
1664
-
1665
- // 11.2.1 gb18030 decoder
1666
- /**
1667
- * @constructor
1668
- * @implements {Decoder}
1669
- * @param {{fatal: boolean}} options
1670
- */
1671
- function GB18030Decoder(options) {
1672
- var fatal = options.fatal;
1673
- // gb18030's decoder has an associated gb18030 first, gb18030
1674
- // second, and gb18030 third (all initially 0x00).
1675
- var /** @type {number} */ gb18030_first = 0x00,
1676
- /** @type {number} */ gb18030_second = 0x00,
1677
- /** @type {number} */ gb18030_third = 0x00;
1678
- /**
1679
- * @param {Stream} stream The stream of bytes being decoded.
1680
- * @param {number} bite The next byte read from the stream.
1681
- * @return {?(number|!Array.<number>)} The next code point(s)
1682
- * decoded, or null if not enough data exists in the input
1683
- * stream to decode a complete code point.
1684
- */
1685
- this.handler = function(stream, bite) {
1686
- // 1. If byte is end-of-stream and gb18030 first, gb18030
1687
- // second, and gb18030 third are 0x00, return finished.
1688
- if (bite === end_of_stream && gb18030_first === 0x00 &&
1689
- gb18030_second === 0x00 && gb18030_third === 0x00) {
1690
- return finished;
1691
- }
1692
- // 2. If byte is end-of-stream, and gb18030 first, gb18030
1693
- // second, or gb18030 third is not 0x00, set gb18030 first,
1694
- // gb18030 second, and gb18030 third to 0x00, and return error.
1695
- if (bite === end_of_stream &&
1696
- (gb18030_first !== 0x00 || gb18030_second !== 0x00 ||
1697
- gb18030_third !== 0x00)) {
1698
- gb18030_first = 0x00;
1699
- gb18030_second = 0x00;
1700
- gb18030_third = 0x00;
1701
- decoderError(fatal);
1702
- }
1703
- var code_point;
1704
- // 3. If gb18030 third is not 0x00, run these substeps:
1705
- if (gb18030_third !== 0x00) {
1706
- // 1. Let code point be null.
1707
- code_point = null;
1708
- // 2. If byte is in the range 0x30 to 0x39, inclusive, set
1709
- // code point to the index gb18030 ranges code point for
1710
- // (((gb18030 first − 0x81) × 10 + gb18030 second − 0x30) ×
1711
- // 126 + gb18030 third − 0x81) × 10 + byte − 0x30.
1712
- if (inRange(bite, 0x30, 0x39)) {
1713
- code_point = indexGB18030RangesCodePointFor(
1714
- (((gb18030_first - 0x81) * 10 + gb18030_second - 0x30) * 126 +
1715
- gb18030_third - 0x81) * 10 + bite - 0x30);
1716
- }
1717
-
1718
- // 3. Let buffer be a byte sequence consisting of gb18030
1719
- // second, gb18030 third, and byte, in order.
1720
- var buffer = [gb18030_second, gb18030_third, bite];
1721
-
1722
- // 4. Set gb18030 first, gb18030 second, and gb18030 third to
1723
- // 0x00.
1724
- gb18030_first = 0x00;
1725
- gb18030_second = 0x00;
1726
- gb18030_third = 0x00;
1727
-
1728
- // 5. If code point is null, prepend buffer to stream and
1729
- // return error.
1730
- if (code_point === null) {
1731
- stream.prepend(buffer);
1732
- return decoderError(fatal);
1733
- }
1734
-
1735
- // 6. Return a code point whose value is code point.
1736
- return code_point;
1737
- }
1738
-
1739
- // 4. If gb18030 second is not 0x00, run these substeps:
1740
- if (gb18030_second !== 0x00) {
1741
-
1742
- // 1. If byte is in the range 0x81 to 0xFE, inclusive, set
1743
- // gb18030 third to byte and return continue.
1744
- if (inRange(bite, 0x81, 0xFE)) {
1745
- gb18030_third = bite;
1746
- return null;
1747
- }
1748
-
1749
- // 2. Prepend gb18030 second followed by byte to stream, set
1750
- // gb18030 first and gb18030 second to 0x00, and return error.
1751
- stream.prepend([gb18030_second, bite]);
1752
- gb18030_first = 0x00;
1753
- gb18030_second = 0x00;
1754
- return decoderError(fatal);
1755
- }
1756
-
1757
- // 5. If gb18030 first is not 0x00, run these substeps:
1758
- if (gb18030_first !== 0x00) {
1759
-
1760
- // 1. If byte is in the range 0x30 to 0x39, inclusive, set
1761
- // gb18030 second to byte and return continue.
1762
- if (inRange(bite, 0x30, 0x39)) {
1763
- gb18030_second = bite;
1764
- return null;
1765
- }
1766
-
1767
- // 2. Let lead be gb18030 first, let pointer be null, and set
1768
- // gb18030 first to 0x00.
1769
- var lead = gb18030_first;
1770
- var pointer = null;
1771
- gb18030_first = 0x00;
1772
-
1773
- // 3. Let offset be 0x40 if byte is less than 0x7F and 0x41
1774
- // otherwise.
1775
- var offset = bite < 0x7F ? 0x40 : 0x41;
1776
-
1777
- // 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
1778
- // to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 +
1779
- // (byte − offset).
1780
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE))
1781
- pointer = (lead - 0x81) * 190 + (bite - offset);
1782
-
1783
- // 5. Let code point be null if pointer is null and the index
1784
- // code point for pointer in index gb18030 otherwise.
1785
- code_point = pointer === null ? null :
1786
- indexCodePointFor(pointer, index('gb18030'));
1787
-
1788
- // 6. If code point is null and byte is an ASCII byte, prepend
1789
- // byte to stream.
1790
- if (code_point === null && isASCIIByte(bite))
1791
- stream.prepend(bite);
1792
-
1793
- // 7. If code point is null, return error.
1794
- if (code_point === null)
1795
- return decoderError(fatal);
1796
-
1797
- // 8. Return a code point whose value is code point.
1798
- return code_point;
1799
- }
1800
-
1801
- // 6. If byte is an ASCII byte, return a code point whose value
1802
- // is byte.
1803
- if (isASCIIByte(bite))
1804
- return bite;
1805
-
1806
- // 7. If byte is 0x80, return code point U+20AC.
1807
- if (bite === 0x80)
1808
- return 0x20AC;
1809
-
1810
- // 8. If byte is in the range 0x81 to 0xFE, inclusive, set
1811
- // gb18030 first to byte and return continue.
1812
- if (inRange(bite, 0x81, 0xFE)) {
1813
- gb18030_first = bite;
1814
- return null;
1815
- }
1816
-
1817
- // 9. Return error.
1818
- return decoderError(fatal);
1819
- };
1820
- }
1821
-
1822
- // 11.2.2 gb18030 encoder
1823
- /**
1824
- * @constructor
1825
- * @implements {Encoder}
1826
- * @param {{fatal: boolean}} options
1827
- * @param {boolean=} gbk_flag
1828
- */
1829
- function GB18030Encoder(options, gbk_flag) {
1830
- var fatal = options.fatal;
1831
- // gb18030's decoder has an associated gbk flag (initially unset).
1832
- /**
1833
- * @param {Stream} stream Input stream.
1834
- * @param {number} code_point Next code point read from the stream.
1835
- * @return {(number|!Array.<number>)} Byte(s) to emit.
1836
- */
1837
- this.handler = function(stream, code_point) {
1838
- // 1. If code point is end-of-stream, return finished.
1839
- if (code_point === end_of_stream)
1840
- return finished;
1841
-
1842
- // 2. If code point is an ASCII code point, return a byte whose
1843
- // value is code point.
1844
- if (isASCIICodePoint(code_point))
1845
- return code_point;
1846
-
1847
- // 3. If code point is U+E5E5, return error with code point.
1848
- if (code_point === 0xE5E5)
1849
- return encoderError(code_point);
1850
-
1851
- // 4. If the gbk flag is set and code point is U+20AC, return
1852
- // byte 0x80.
1853
- if (gbk_flag && code_point === 0x20AC)
1854
- return 0x80;
1855
-
1856
- // 5. Let pointer be the index pointer for code point in index
1857
- // gb18030.
1858
- var pointer = indexPointerFor(code_point, index('gb18030'));
1859
-
1860
- // 6. If pointer is not null, run these substeps:
1861
- if (pointer !== null) {
1862
-
1863
- // 1. Let lead be floor(pointer / 190) + 0x81.
1864
- var lead = floor(pointer / 190) + 0x81;
1865
-
1866
- // 2. Let trail be pointer % 190.
1867
- var trail = pointer % 190;
1868
-
1869
- // 3. Let offset be 0x40 if trail is less than 0x3F and 0x41 otherwise.
1870
- var offset = trail < 0x3F ? 0x40 : 0x41;
1871
-
1872
- // 4. Return two bytes whose values are lead and trail + offset.
1873
- return [lead, trail + offset];
1874
- }
1875
-
1876
- // 7. If gbk flag is set, return error with code point.
1877
- if (gbk_flag)
1878
- return encoderError(code_point);
1879
-
1880
- // 8. Set pointer to the index gb18030 ranges pointer for code
1881
- // point.
1882
- pointer = indexGB18030RangesPointerFor(code_point);
1883
-
1884
- // 9. Let byte1 be floor(pointer / 10 / 126 / 10).
1885
- var byte1 = floor(pointer / 10 / 126 / 10);
1886
-
1887
- // 10. Set pointer to pointer − byte1 × 10 × 126 × 10.
1888
- pointer = pointer - byte1 * 10 * 126 * 10;
1889
-
1890
- // 11. Let byte2 be floor(pointer / 10 / 126).
1891
- var byte2 = floor(pointer / 10 / 126);
1892
-
1893
- // 12. Set pointer to pointer − byte2 × 10 × 126.
1894
- pointer = pointer - byte2 * 10 * 126;
1895
-
1896
- // 13. Let byte3 be floor(pointer / 10).
1897
- var byte3 = floor(pointer / 10);
1898
-
1899
- // 14. Let byte4 be pointer − byte3 × 10.
1900
- var byte4 = pointer - byte3 * 10;
1901
-
1902
- // 15. Return four bytes whose values are byte1 + 0x81, byte2 +
1903
- // 0x30, byte3 + 0x81, byte4 + 0x30.
1904
- return [byte1 + 0x81,
1905
- byte2 + 0x30,
1906
- byte3 + 0x81,
1907
- byte4 + 0x30];
1908
- };
1909
- }
1910
-
1911
- /** @param {{fatal: boolean}} options */
1912
- encoders['gb18030'] = function(options) {
1913
- return new GB18030Encoder(options);
1914
- };
1915
- /** @param {{fatal: boolean}} options */
1916
- decoders['gb18030'] = function(options) {
1917
- return new GB18030Decoder(options);
1918
- };
1919
-
1920
-
1921
- //
1922
- // 12. Legacy multi-byte Chinese (traditional) encodings
1923
- //
1924
-
1925
- // 12.1 Big5
1926
-
1927
- // 12.1.1 Big5 decoder
1928
- /**
1929
- * @constructor
1930
- * @implements {Decoder}
1931
- * @param {{fatal: boolean}} options
1932
- */
1933
- function Big5Decoder(options) {
1934
- var fatal = options.fatal;
1935
- // Big5's decoder has an associated Big5 lead (initially 0x00).
1936
- var /** @type {number} */ Big5_lead = 0x00;
1937
-
1938
- /**
1939
- * @param {Stream} stream The stream of bytes being decoded.
1940
- * @param {number} bite The next byte read from the stream.
1941
- * @return {?(number|!Array.<number>)} The next code point(s)
1942
- * decoded, or null if not enough data exists in the input
1943
- * stream to decode a complete code point.
1944
- */
1945
- this.handler = function(stream, bite) {
1946
- // 1. If byte is end-of-stream and Big5 lead is not 0x00, set
1947
- // Big5 lead to 0x00 and return error.
1948
- if (bite === end_of_stream && Big5_lead !== 0x00) {
1949
- Big5_lead = 0x00;
1950
- return decoderError(fatal);
1951
- }
1952
-
1953
- // 2. If byte is end-of-stream and Big5 lead is 0x00, return
1954
- // finished.
1955
- if (bite === end_of_stream && Big5_lead === 0x00)
1956
- return finished;
1957
-
1958
- // 3. If Big5 lead is not 0x00, let lead be Big5 lead, let
1959
- // pointer be null, set Big5 lead to 0x00, and then run these
1960
- // substeps:
1961
- if (Big5_lead !== 0x00) {
1962
- var lead = Big5_lead;
1963
- var pointer = null;
1964
- Big5_lead = 0x00;
1965
-
1966
- // 1. Let offset be 0x40 if byte is less than 0x7F and 0x62
1967
- // otherwise.
1968
- var offset = bite < 0x7F ? 0x40 : 0x62;
1969
-
1970
- // 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1
1971
- // to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 +
1972
- // (byte − offset).
1973
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE))
1974
- pointer = (lead - 0x81) * 157 + (bite - offset);
1975
-
1976
- // 3. If there is a row in the table below whose first column
1977
- // is pointer, return the two code points listed in its second
1978
- // column
1979
- // Pointer | Code points
1980
- // --------+--------------
1981
- // 1133 | U+00CA U+0304
1982
- // 1135 | U+00CA U+030C
1983
- // 1164 | U+00EA U+0304
1984
- // 1166 | U+00EA U+030C
1985
- switch (pointer) {
1986
- case 1133: return [0x00CA, 0x0304];
1987
- case 1135: return [0x00CA, 0x030C];
1988
- case 1164: return [0x00EA, 0x0304];
1989
- case 1166: return [0x00EA, 0x030C];
1990
- }
1991
-
1992
- // 4. Let code point be null if pointer is null and the index
1993
- // code point for pointer in index Big5 otherwise.
1994
- var code_point = (pointer === null) ? null :
1995
- indexCodePointFor(pointer, index('big5'));
1996
-
1997
- // 5. If code point is null and byte is an ASCII byte, prepend
1998
- // byte to stream.
1999
- if (code_point === null && isASCIIByte(bite))
2000
- stream.prepend(bite);
2001
-
2002
- // 6. If code point is null, return error.
2003
- if (code_point === null)
2004
- return decoderError(fatal);
2005
-
2006
- // 7. Return a code point whose value is code point.
2007
- return code_point;
2008
- }
2009
-
2010
- // 4. If byte is an ASCII byte, return a code point whose value
2011
- // is byte.
2012
- if (isASCIIByte(bite))
2013
- return bite;
2014
-
2015
- // 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5
2016
- // lead to byte and return continue.
2017
- if (inRange(bite, 0x81, 0xFE)) {
2018
- Big5_lead = bite;
2019
- return null;
2020
- }
2021
-
2022
- // 6. Return error.
2023
- return decoderError(fatal);
2024
- };
2025
- }
2026
-
2027
- // 12.1.2 Big5 encoder
2028
- /**
2029
- * @constructor
2030
- * @implements {Encoder}
2031
- * @param {{fatal: boolean}} options
2032
- */
2033
- function Big5Encoder(options) {
2034
- var fatal = options.fatal;
2035
- /**
2036
- * @param {Stream} stream Input stream.
2037
- * @param {number} code_point Next code point read from the stream.
2038
- * @return {(number|!Array.<number>)} Byte(s) to emit.
2039
- */
2040
- this.handler = function(stream, code_point) {
2041
- // 1. If code point is end-of-stream, return finished.
2042
- if (code_point === end_of_stream)
2043
- return finished;
2044
-
2045
- // 2. If code point is an ASCII code point, return a byte whose
2046
- // value is code point.
2047
- if (isASCIICodePoint(code_point))
2048
- return code_point;
2049
-
2050
- // 3. Let pointer be the index Big5 pointer for code point.
2051
- var pointer = indexBig5PointerFor(code_point);
2052
-
2053
- // 4. If pointer is null, return error with code point.
2054
- if (pointer === null)
2055
- return encoderError(code_point);
2056
-
2057
- // 5. Let lead be floor(pointer / 157) + 0x81.
2058
- var lead = floor(pointer / 157) + 0x81;
2059
-
2060
- // 6. If lead is less than 0xA1, return error with code point.
2061
- if (lead < 0xA1)
2062
- return encoderError(code_point);
2063
-
2064
- // 7. Let trail be pointer % 157.
2065
- var trail = pointer % 157;
2066
-
2067
- // 8. Let offset be 0x40 if trail is less than 0x3F and 0x62
2068
- // otherwise.
2069
- var offset = trail < 0x3F ? 0x40 : 0x62;
2070
-
2071
- // Return two bytes whose values are lead and trail + offset.
2072
- return [lead, trail + offset];
2073
- };
2074
- }
2075
-
2076
- /** @param {{fatal: boolean}} options */
2077
- encoders['Big5'] = function(options) {
2078
- return new Big5Encoder(options);
2079
- };
2080
- /** @param {{fatal: boolean}} options */
2081
- decoders['Big5'] = function(options) {
2082
- return new Big5Decoder(options);
2083
- };
2084
-
2085
-
2086
- //
2087
- // 13. Legacy multi-byte Japanese encodings
2088
- //
2089
-
2090
- // 13.1 euc-jp
2091
-
2092
- // 13.1.1 euc-jp decoder
2093
- /**
2094
- * @constructor
2095
- * @implements {Decoder}
2096
- * @param {{fatal: boolean}} options
2097
- */
2098
- function EUCJPDecoder(options) {
2099
- var fatal = options.fatal;
2100
-
2101
- // euc-jp's decoder has an associated euc-jp jis0212 flag
2102
- // (initially unset) and euc-jp lead (initially 0x00).
2103
- var /** @type {boolean} */ eucjp_jis0212_flag = false,
2104
- /** @type {number} */ eucjp_lead = 0x00;
2105
-
2106
- /**
2107
- * @param {Stream} stream The stream of bytes being decoded.
2108
- * @param {number} bite The next byte read from the stream.
2109
- * @return {?(number|!Array.<number>)} The next code point(s)
2110
- * decoded, or null if not enough data exists in the input
2111
- * stream to decode a complete code point.
2112
- */
2113
- this.handler = function(stream, bite) {
2114
- // 1. If byte is end-of-stream and euc-jp lead is not 0x00, set
2115
- // euc-jp lead to 0x00, and return error.
2116
- if (bite === end_of_stream && eucjp_lead !== 0x00) {
2117
- eucjp_lead = 0x00;
2118
- return decoderError(fatal);
2119
- }
2120
-
2121
- // 2. If byte is end-of-stream and euc-jp lead is 0x00, return
2122
- // finished.
2123
- if (bite === end_of_stream && eucjp_lead === 0x00)
2124
- return finished;
2125
-
2126
- // 3. If euc-jp lead is 0x8E and byte is in the range 0xA1 to
2127
- // 0xDF, inclusive, set euc-jp lead to 0x00 and return a code
2128
- // point whose value is 0xFF61 − 0xA1 + byte.
2129
- if (eucjp_lead === 0x8E && inRange(bite, 0xA1, 0xDF)) {
2130
- eucjp_lead = 0x00;
2131
- return 0xFF61 - 0xA1 + bite;
2132
- }
2133
-
2134
- // 4. If euc-jp lead is 0x8F and byte is in the range 0xA1 to
2135
- // 0xFE, inclusive, set the euc-jp jis0212 flag, set euc-jp lead
2136
- // to byte, and return continue.
2137
- if (eucjp_lead === 0x8F && inRange(bite, 0xA1, 0xFE)) {
2138
- eucjp_jis0212_flag = true;
2139
- eucjp_lead = bite;
2140
- return null;
2141
- }
2142
-
2143
- // 5. If euc-jp lead is not 0x00, let lead be euc-jp lead, set
2144
- // euc-jp lead to 0x00, and run these substeps:
2145
- if (eucjp_lead !== 0x00) {
2146
- var lead = eucjp_lead;
2147
- eucjp_lead = 0x00;
2148
-
2149
- // 1. Let code point be null.
2150
- var code_point = null;
2151
-
2152
- // 2. If lead and byte are both in the range 0xA1 to 0xFE,
2153
- // inclusive, set code point to the index code point for (lead
2154
- // − 0xA1) × 94 + byte − 0xA1 in index jis0208 if the euc-jp
2155
- // jis0212 flag is unset and in index jis0212 otherwise.
2156
- if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
2157
- code_point = indexCodePointFor(
2158
- (lead - 0xA1) * 94 + (bite - 0xA1),
2159
- index(!eucjp_jis0212_flag ? 'jis0208' : 'jis0212'));
2160
- }
2161
-
2162
- // 3. Unset the euc-jp jis0212 flag.
2163
- eucjp_jis0212_flag = false;
2164
-
2165
- // 4. If byte is not in the range 0xA1 to 0xFE, inclusive,
2166
- // prepend byte to stream.
2167
- if (!inRange(bite, 0xA1, 0xFE))
2168
- stream.prepend(bite);
2169
-
2170
- // 5. If code point is null, return error.
2171
- if (code_point === null)
2172
- return decoderError(fatal);
2173
-
2174
- // 6. Return a code point whose value is code point.
2175
- return code_point;
2176
- }
2177
-
2178
- // 6. If byte is an ASCII byte, return a code point whose value
2179
- // is byte.
2180
- if (isASCIIByte(bite))
2181
- return bite;
2182
-
2183
- // 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE,
2184
- // inclusive, set euc-jp lead to byte and return continue.
2185
- if (bite === 0x8E || bite === 0x8F || inRange(bite, 0xA1, 0xFE)) {
2186
- eucjp_lead = bite;
2187
- return null;
2188
- }
2189
-
2190
- // 8. Return error.
2191
- return decoderError(fatal);
2192
- };
2193
- }
2194
-
2195
- // 13.1.2 euc-jp encoder
2196
- /**
2197
- * @constructor
2198
- * @implements {Encoder}
2199
- * @param {{fatal: boolean}} options
2200
- */
2201
- function EUCJPEncoder(options) {
2202
- var fatal = options.fatal;
2203
- /**
2204
- * @param {Stream} stream Input stream.
2205
- * @param {number} code_point Next code point read from the stream.
2206
- * @return {(number|!Array.<number>)} Byte(s) to emit.
2207
- */
2208
- this.handler = function(stream, code_point) {
2209
- // 1. If code point is end-of-stream, return finished.
2210
- if (code_point === end_of_stream)
2211
- return finished;
2212
-
2213
- // 2. If code point is an ASCII code point, return a byte whose
2214
- // value is code point.
2215
- if (isASCIICodePoint(code_point))
2216
- return code_point;
2217
-
2218
- // 3. If code point is U+00A5, return byte 0x5C.
2219
- if (code_point === 0x00A5)
2220
- return 0x5C;
2221
-
2222
- // 4. If code point is U+203E, return byte 0x7E.
2223
- if (code_point === 0x203E)
2224
- return 0x7E;
2225
-
2226
- // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
2227
- // return two bytes whose values are 0x8E and code point −
2228
- // 0xFF61 + 0xA1.
2229
- if (inRange(code_point, 0xFF61, 0xFF9F))
2230
- return [0x8E, code_point - 0xFF61 + 0xA1];
2231
-
2232
- // 6. If code point is U+2212, set it to U+FF0D.
2233
- if (code_point === 0x2212)
2234
- code_point = 0xFF0D;
2235
-
2236
- // 7. Let pointer be the index pointer for code point in index
2237
- // jis0208.
2238
- var pointer = indexPointerFor(code_point, index('jis0208'));
2239
-
2240
- // 8. If pointer is null, return error with code point.
2241
- if (pointer === null)
2242
- return encoderError(code_point);
2243
-
2244
- // 9. Let lead be floor(pointer / 94) + 0xA1.
2245
- var lead = floor(pointer / 94) + 0xA1;
2246
-
2247
- // 10. Let trail be pointer % 94 + 0xA1.
2248
- var trail = pointer % 94 + 0xA1;
2249
-
2250
- // 11. Return two bytes whose values are lead and trail.
2251
- return [lead, trail];
2252
- };
2253
- }
2254
-
2255
- /** @param {{fatal: boolean}} options */
2256
- encoders['EUC-JP'] = function(options) {
2257
- return new EUCJPEncoder(options);
2258
- };
2259
- /** @param {{fatal: boolean}} options */
2260
- decoders['EUC-JP'] = function(options) {
2261
- return new EUCJPDecoder(options);
2262
- };
2263
-
2264
- // 13.2 iso-2022-jp
2265
-
2266
- // 13.2.1 iso-2022-jp decoder
2267
- /**
2268
- * @constructor
2269
- * @implements {Decoder}
2270
- * @param {{fatal: boolean}} options
2271
- */
2272
- function ISO2022JPDecoder(options) {
2273
- var fatal = options.fatal;
2274
- /** @enum */
2275
- var states = {
2276
- ASCII: 0,
2277
- Roman: 1,
2278
- Katakana: 2,
2279
- LeadByte: 3,
2280
- TrailByte: 4,
2281
- EscapeStart: 5,
2282
- Escape: 6
2283
- };
2284
- // iso-2022-jp's decoder has an associated iso-2022-jp decoder
2285
- // state (initially ASCII), iso-2022-jp decoder output state
2286
- // (initially ASCII), iso-2022-jp lead (initially 0x00), and
2287
- // iso-2022-jp output flag (initially unset).
2288
- var /** @type {number} */ iso2022jp_decoder_state = states.ASCII,
2289
- /** @type {number} */ iso2022jp_decoder_output_state = states.ASCII,
2290
- /** @type {number} */ iso2022jp_lead = 0x00,
2291
- /** @type {boolean} */ iso2022jp_output_flag = false;
2292
- /**
2293
- * @param {Stream} stream The stream of bytes being decoded.
2294
- * @param {number} bite The next byte read from the stream.
2295
- * @return {?(number|!Array.<number>)} The next code point(s)
2296
- * decoded, or null if not enough data exists in the input
2297
- * stream to decode a complete code point.
2298
- */
2299
- this.handler = function(stream, bite) {
2300
- // switching on iso-2022-jp decoder state:
2301
- switch (iso2022jp_decoder_state) {
2302
- default:
2303
- case states.ASCII:
2304
- // ASCII
2305
- // Based on byte:
2306
-
2307
- // 0x1B
2308
- if (bite === 0x1B) {
2309
- // Set iso-2022-jp decoder state to escape start and return
2310
- // continue.
2311
- iso2022jp_decoder_state = states.EscapeStart;
2312
- return null;
2313
- }
2314
-
2315
- // 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B
2316
- if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E
2317
- && bite !== 0x0F && bite !== 0x1B) {
2318
- // Unset the iso-2022-jp output flag and return a code point
2319
- // whose value is byte.
2320
- iso2022jp_output_flag = false;
2321
- return bite;
2322
- }
2323
-
2324
- // end-of-stream
2325
- if (bite === end_of_stream) {
2326
- // Return finished.
2327
- return finished;
2328
- }
2329
-
2330
- // Otherwise
2331
- // Unset the iso-2022-jp output flag and return error.
2332
- iso2022jp_output_flag = false;
2333
- return decoderError(fatal);
2334
-
2335
- case states.Roman:
2336
- // Roman
2337
- // Based on byte:
2338
-
2339
- // 0x1B
2340
- if (bite === 0x1B) {
2341
- // Set iso-2022-jp decoder state to escape start and return
2342
- // continue.
2343
- iso2022jp_decoder_state = states.EscapeStart;
2344
- return null;
2345
- }
2346
-
2347
- // 0x5C
2348
- if (bite === 0x5C) {
2349
- // Unset the iso-2022-jp output flag and return code point
2350
- // U+00A5.
2351
- iso2022jp_output_flag = false;
2352
- return 0x00A5;
2353
- }
2354
-
2355
- // 0x7E
2356
- if (bite === 0x7E) {
2357
- // Unset the iso-2022-jp output flag and return code point
2358
- // U+203E.
2359
- iso2022jp_output_flag = false;
2360
- return 0x203E;
2361
- }
2362
-
2363
- // 0x00 to 0x7F, excluding 0x0E, 0x0F, 0x1B, 0x5C, and 0x7E
2364
- if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E && bite !== 0x0F
2365
- && bite !== 0x1B && bite !== 0x5C && bite !== 0x7E) {
2366
- // Unset the iso-2022-jp output flag and return a code point
2367
- // whose value is byte.
2368
- iso2022jp_output_flag = false;
2369
- return bite;
2370
- }
2371
-
2372
- // end-of-stream
2373
- if (bite === end_of_stream) {
2374
- // Return finished.
2375
- return finished;
2376
- }
2377
-
2378
- // Otherwise
2379
- // Unset the iso-2022-jp output flag and return error.
2380
- iso2022jp_output_flag = false;
2381
- return decoderError(fatal);
2382
-
2383
- case states.Katakana:
2384
- // Katakana
2385
- // Based on byte:
2386
-
2387
- // 0x1B
2388
- if (bite === 0x1B) {
2389
- // Set iso-2022-jp decoder state to escape start and return
2390
- // continue.
2391
- iso2022jp_decoder_state = states.EscapeStart;
2392
- return null;
2393
- }
2394
-
2395
- // 0x21 to 0x5F
2396
- if (inRange(bite, 0x21, 0x5F)) {
2397
- // Unset the iso-2022-jp output flag and return a code point
2398
- // whose value is 0xFF61 − 0x21 + byte.
2399
- iso2022jp_output_flag = false;
2400
- return 0xFF61 - 0x21 + bite;
2401
- }
2402
-
2403
- // end-of-stream
2404
- if (bite === end_of_stream) {
2405
- // Return finished.
2406
- return finished;
2407
- }
2408
-
2409
- // Otherwise
2410
- // Unset the iso-2022-jp output flag and return error.
2411
- iso2022jp_output_flag = false;
2412
- return decoderError(fatal);
2413
-
2414
- case states.LeadByte:
2415
- // Lead byte
2416
- // Based on byte:
2417
-
2418
- // 0x1B
2419
- if (bite === 0x1B) {
2420
- // Set iso-2022-jp decoder state to escape start and return
2421
- // continue.
2422
- iso2022jp_decoder_state = states.EscapeStart;
2423
- return null;
2424
- }
2425
-
2426
- // 0x21 to 0x7E
2427
- if (inRange(bite, 0x21, 0x7E)) {
2428
- // Unset the iso-2022-jp output flag, set iso-2022-jp lead
2429
- // to byte, iso-2022-jp decoder state to trail byte, and
2430
- // return continue.
2431
- iso2022jp_output_flag = false;
2432
- iso2022jp_lead = bite;
2433
- iso2022jp_decoder_state = states.TrailByte;
2434
- return null;
2435
- }
2436
-
2437
- // end-of-stream
2438
- if (bite === end_of_stream) {
2439
- // Return finished.
2440
- return finished;
2441
- }
2442
-
2443
- // Otherwise
2444
- // Unset the iso-2022-jp output flag and return error.
2445
- iso2022jp_output_flag = false;
2446
- return decoderError(fatal);
2447
-
2448
- case states.TrailByte:
2449
- // Trail byte
2450
- // Based on byte:
2451
-
2452
- // 0x1B
2453
- if (bite === 0x1B) {
2454
- // Set iso-2022-jp decoder state to escape start and return
2455
- // continue.
2456
- iso2022jp_decoder_state = states.EscapeStart;
2457
- return decoderError(fatal);
2458
- }
2459
-
2460
- // 0x21 to 0x7E
2461
- if (inRange(bite, 0x21, 0x7E)) {
2462
- // 1. Set the iso-2022-jp decoder state to lead byte.
2463
- iso2022jp_decoder_state = states.LeadByte;
2464
-
2465
- // 2. Let pointer be (iso-2022-jp lead − 0x21) × 94 + byte − 0x21.
2466
- var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
2467
-
2468
- // 3. Let code point be the index code point for pointer in
2469
- // index jis0208.
2470
- var code_point = indexCodePointFor(pointer, index('jis0208'));
2471
-
2472
- // 4. If code point is null, return error.
2473
- if (code_point === null)
2474
- return decoderError(fatal);
2475
-
2476
- // 5. Return a code point whose value is code point.
2477
- return code_point;
2478
- }
2479
-
2480
- // end-of-stream
2481
- if (bite === end_of_stream) {
2482
- // Set the iso-2022-jp decoder state to lead byte, prepend
2483
- // byte to stream, and return error.
2484
- iso2022jp_decoder_state = states.LeadByte;
2485
- stream.prepend(bite);
2486
- return decoderError(fatal);
2487
- }
2488
-
2489
- // Otherwise
2490
- // Set iso-2022-jp decoder state to lead byte and return
2491
- // error.
2492
- iso2022jp_decoder_state = states.LeadByte;
2493
- return decoderError(fatal);
2494
-
2495
- case states.EscapeStart:
2496
- // Escape start
2497
-
2498
- // 1. If byte is either 0x24 or 0x28, set iso-2022-jp lead to
2499
- // byte, iso-2022-jp decoder state to escape, and return
2500
- // continue.
2501
- if (bite === 0x24 || bite === 0x28) {
2502
- iso2022jp_lead = bite;
2503
- iso2022jp_decoder_state = states.Escape;
2504
- return null;
2505
- }
2506
-
2507
- // 2. Prepend byte to stream.
2508
- stream.prepend(bite);
2509
-
2510
- // 3. Unset the iso-2022-jp output flag, set iso-2022-jp
2511
- // decoder state to iso-2022-jp decoder output state, and
2512
- // return error.
2513
- iso2022jp_output_flag = false;
2514
- iso2022jp_decoder_state = iso2022jp_decoder_output_state;
2515
- return decoderError(fatal);
2516
-
2517
- case states.Escape:
2518
- // Escape
2519
-
2520
- // 1. Let lead be iso-2022-jp lead and set iso-2022-jp lead to
2521
- // 0x00.
2522
- var lead = iso2022jp_lead;
2523
- iso2022jp_lead = 0x00;
2524
-
2525
- // 2. Let state be null.
2526
- var state = null;
2527
-
2528
- // 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
2529
- if (lead === 0x28 && bite === 0x42)
2530
- state = states.ASCII;
2531
-
2532
- // 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
2533
- if (lead === 0x28 && bite === 0x4A)
2534
- state = states.Roman;
2535
-
2536
- // 5. If lead is 0x28 and byte is 0x49, set state to Katakana.
2537
- if (lead === 0x28 && bite === 0x49)
2538
- state = states.Katakana;
2539
-
2540
- // 6. If lead is 0x24 and byte is either 0x40 or 0x42, set
2541
- // state to lead byte.
2542
- if (lead === 0x24 && (bite === 0x40 || bite === 0x42))
2543
- state = states.LeadByte;
2544
-
2545
- // 7. If state is non-null, run these substeps:
2546
- if (state !== null) {
2547
- // 1. Set iso-2022-jp decoder state and iso-2022-jp decoder
2548
- // output state to states.
2549
- iso2022jp_decoder_state = iso2022jp_decoder_state = state;
2550
-
2551
- // 2. Let output flag be the iso-2022-jp output flag.
2552
- var output_flag = iso2022jp_output_flag;
2553
-
2554
- // 3. Set the iso-2022-jp output flag.
2555
- iso2022jp_output_flag = true;
2556
-
2557
- // 4. Return continue, if output flag is unset, and error
2558
- // otherwise.
2559
- return !output_flag ? null : decoderError(fatal);
2560
- }
2561
-
2562
- // 8. Prepend lead and byte to stream.
2563
- stream.prepend([lead, bite]);
2564
-
2565
- // 9. Unset the iso-2022-jp output flag, set iso-2022-jp
2566
- // decoder state to iso-2022-jp decoder output state and
2567
- // return error.
2568
- iso2022jp_output_flag = false;
2569
- iso2022jp_decoder_state = iso2022jp_decoder_output_state;
2570
- return decoderError(fatal);
2571
- }
2572
- };
2573
- }
2574
-
2575
- // 13.2.2 iso-2022-jp encoder
2576
- /**
2577
- * @constructor
2578
- * @implements {Encoder}
2579
- * @param {{fatal: boolean}} options
2580
- */
2581
- function ISO2022JPEncoder(options) {
2582
- var fatal = options.fatal;
2583
- // iso-2022-jp's encoder has an associated iso-2022-jp encoder
2584
- // state which is one of ASCII, Roman, and jis0208 (initially
2585
- // ASCII).
2586
- /** @enum */
2587
- var states = {
2588
- ASCII: 0,
2589
- Roman: 1,
2590
- jis0208: 2
2591
- };
2592
- var /** @type {number} */ iso2022jp_state = states.ASCII;
2593
- /**
2594
- * @param {Stream} stream Input stream.
2595
- * @param {number} code_point Next code point read from the stream.
2596
- * @return {(number|!Array.<number>)} Byte(s) to emit.
2597
- */
2598
- this.handler = function(stream, code_point) {
2599
- // 1. If code point is end-of-stream and iso-2022-jp encoder
2600
- // state is not ASCII, prepend code point to stream, set
2601
- // iso-2022-jp encoder state to ASCII, and return three bytes
2602
- // 0x1B 0x28 0x42.
2603
- if (code_point === end_of_stream &&
2604
- iso2022jp_state !== states.ASCII) {
2605
- stream.prepend(code_point);
2606
- iso2022jp_state = states.ASCII;
2607
- return [0x1B, 0x28, 0x42];
2608
- }
2609
-
2610
- // 2. If code point is end-of-stream and iso-2022-jp encoder
2611
- // state is ASCII, return finished.
2612
- if (code_point === end_of_stream && iso2022jp_state === states.ASCII)
2613
- return finished;
2614
-
2615
- // 3. If ISO-2022-JP encoder state is ASCII or Roman, and code
2616
- // point is U+000E, U+000F, or U+001B, return error with U+FFFD.
2617
- if ((iso2022jp_state === states.ASCII ||
2618
- iso2022jp_state === states.Roman) &&
2619
- (code_point === 0x000E || code_point === 0x000F ||
2620
- code_point === 0x001B)) {
2621
- return encoderError(0xFFFD);
2622
- }
2623
-
2624
- // 4. If iso-2022-jp encoder state is ASCII and code point is an
2625
- // ASCII code point, return a byte whose value is code point.
2626
- if (iso2022jp_state === states.ASCII &&
2627
- isASCIICodePoint(code_point))
2628
- return code_point;
2629
-
2630
- // 5. If iso-2022-jp encoder state is Roman and code point is an
2631
- // ASCII code point, excluding U+005C and U+007E, or is U+00A5
2632
- // or U+203E, run these substeps:
2633
- if (iso2022jp_state === states.Roman &&
2634
- ((isASCIICodePoint(code_point) &&
2635
- code_point !== 0x005C && code_point !== 0x007E) ||
2636
- (code_point == 0x00A5 || code_point == 0x203E))) {
2637
-
2638
- // 1. If code point is an ASCII code point, return a byte
2639
- // whose value is code point.
2640
- if (isASCIICodePoint(code_point))
2641
- return code_point;
2642
-
2643
- // 2. If code point is U+00A5, return byte 0x5C.
2644
- if (code_point === 0x00A5)
2645
- return 0x5C;
2646
-
2647
- // 3. If code point is U+203E, return byte 0x7E.
2648
- if (code_point === 0x203E)
2649
- return 0x7E;
2650
- }
2651
-
2652
- // 6. If code point is an ASCII code point, and iso-2022-jp
2653
- // encoder state is not ASCII, prepend code point to stream, set
2654
- // iso-2022-jp encoder state to ASCII, and return three bytes
2655
- // 0x1B 0x28 0x42.
2656
- if (isASCIICodePoint(code_point) &&
2657
- iso2022jp_state !== states.ASCII) {
2658
- stream.prepend(code_point);
2659
- iso2022jp_state = states.ASCII;
2660
- return [0x1B, 0x28, 0x42];
2661
- }
2662
-
2663
- // 7. If code point is either U+00A5 or U+203E, and iso-2022-jp
2664
- // encoder state is not Roman, prepend code point to stream, set
2665
- // iso-2022-jp encoder state to Roman, and return three bytes
2666
- // 0x1B 0x28 0x4A.
2667
- if ((code_point === 0x00A5 || code_point === 0x203E) &&
2668
- iso2022jp_state !== states.Roman) {
2669
- stream.prepend(code_point);
2670
- iso2022jp_state = states.Roman;
2671
- return [0x1B, 0x28, 0x4A];
2672
- }
2673
-
2674
- // 8. If code point is U+2212, set it to U+FF0D.
2675
- if (code_point === 0x2212)
2676
- code_point = 0xFF0D;
2677
-
2678
- // 9. Let pointer be the index pointer for code point in index
2679
- // jis0208.
2680
- var pointer = indexPointerFor(code_point, index('jis0208'));
2681
-
2682
- // 10. If pointer is null, return error with code point.
2683
- if (pointer === null)
2684
- return encoderError(code_point);
2685
-
2686
- // 11. If iso-2022-jp encoder state is not jis0208, prepend code
2687
- // point to stream, set iso-2022-jp encoder state to jis0208,
2688
- // and return three bytes 0x1B 0x24 0x42.
2689
- if (iso2022jp_state !== states.jis0208) {
2690
- stream.prepend(code_point);
2691
- iso2022jp_state = states.jis0208;
2692
- return [0x1B, 0x24, 0x42];
2693
- }
2694
-
2695
- // 12. Let lead be floor(pointer / 94) + 0x21.
2696
- var lead = floor(pointer / 94) + 0x21;
2697
-
2698
- // 13. Let trail be pointer % 94 + 0x21.
2699
- var trail = pointer % 94 + 0x21;
2700
-
2701
- // 14. Return two bytes whose values are lead and trail.
2702
- return [lead, trail];
2703
- };
2704
- }
2705
-
2706
- /** @param {{fatal: boolean}} options */
2707
- encoders['ISO-2022-JP'] = function(options) {
2708
- return new ISO2022JPEncoder(options);
2709
- };
2710
- /** @param {{fatal: boolean}} options */
2711
- decoders['ISO-2022-JP'] = function(options) {
2712
- return new ISO2022JPDecoder(options);
2713
- };
2714
-
2715
- // 13.3 Shift_JIS
2716
-
2717
- // 13.3.1 Shift_JIS decoder
2718
- /**
2719
- * @constructor
2720
- * @implements {Decoder}
2721
- * @param {{fatal: boolean}} options
2722
- */
2723
- function ShiftJISDecoder(options) {
2724
- var fatal = options.fatal;
2725
- // Shift_JIS's decoder has an associated Shift_JIS lead (initially
2726
- // 0x00).
2727
- var /** @type {number} */ Shift_JIS_lead = 0x00;
2728
- /**
2729
- * @param {Stream} stream The stream of bytes being decoded.
2730
- * @param {number} bite The next byte read from the stream.
2731
- * @return {?(number|!Array.<number>)} The next code point(s)
2732
- * decoded, or null if not enough data exists in the input
2733
- * stream to decode a complete code point.
2734
- */
2735
- this.handler = function(stream, bite) {
2736
- // 1. If byte is end-of-stream and Shift_JIS lead is not 0x00,
2737
- // set Shift_JIS lead to 0x00 and return error.
2738
- if (bite === end_of_stream && Shift_JIS_lead !== 0x00) {
2739
- Shift_JIS_lead = 0x00;
2740
- return decoderError(fatal);
2741
- }
2742
-
2743
- // 2. If byte is end-of-stream and Shift_JIS lead is 0x00,
2744
- // return finished.
2745
- if (bite === end_of_stream && Shift_JIS_lead === 0x00)
2746
- return finished;
2747
-
2748
- // 3. If Shift_JIS lead is not 0x00, let lead be Shift_JIS lead,
2749
- // let pointer be null, set Shift_JIS lead to 0x00, and then run
2750
- // these substeps:
2751
- if (Shift_JIS_lead !== 0x00) {
2752
- var lead = Shift_JIS_lead;
2753
- var pointer = null;
2754
- Shift_JIS_lead = 0x00;
2755
-
2756
- // 1. Let offset be 0x40, if byte is less than 0x7F, and 0x41
2757
- // otherwise.
2758
- var offset = (bite < 0x7F) ? 0x40 : 0x41;
2759
-
2760
- // 2. Let lead offset be 0x81, if lead is less than 0xA0, and
2761
- // 0xC1 otherwise.
2762
- var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
2763
-
2764
- // 3. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
2765
- // to 0xFC, inclusive, set pointer to (lead − lead offset) ×
2766
- // 188 + byte − offset.
2767
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC))
2768
- pointer = (lead - lead_offset) * 188 + bite - offset;
2769
-
2770
- // 4. If pointer is in the range 8836 to 10715, inclusive,
2771
- // return a code point whose value is 0xE000 − 8836 + pointer.
2772
- if (inRange(pointer, 8836, 10715))
2773
- return 0xE000 - 8836 + pointer;
2774
-
2775
- // 5. Let code point be null, if pointer is null, and the
2776
- // index code point for pointer in index jis0208 otherwise.
2777
- var code_point = (pointer === null) ? null :
2778
- indexCodePointFor(pointer, index('jis0208'));
2779
-
2780
- // 6. If code point is null and byte is an ASCII byte, prepend
2781
- // byte to stream.
2782
- if (code_point === null && isASCIIByte(bite))
2783
- stream.prepend(bite);
2784
-
2785
- // 7. If code point is null, return error.
2786
- if (code_point === null)
2787
- return decoderError(fatal);
2788
-
2789
- // 8. Return a code point whose value is code point.
2790
- return code_point;
2791
- }
2792
-
2793
- // 4. If byte is an ASCII byte or 0x80, return a code point
2794
- // whose value is byte.
2795
- if (isASCIIByte(bite) || bite === 0x80)
2796
- return bite;
2797
-
2798
- // 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a
2799
- // code point whose value is 0xFF61 − 0xA1 + byte.
2800
- if (inRange(bite, 0xA1, 0xDF))
2801
- return 0xFF61 - 0xA1 + bite;
2802
-
2803
- // 6. If byte is in the range 0x81 to 0x9F, inclusive, or 0xE0
2804
- // to 0xFC, inclusive, set Shift_JIS lead to byte and return
2805
- // continue.
2806
- if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
2807
- Shift_JIS_lead = bite;
2808
- return null;
2809
- }
2810
-
2811
- // 7. Return error.
2812
- return decoderError(fatal);
2813
- };
2814
- }
2815
-
2816
- // 13.3.2 Shift_JIS encoder
2817
- /**
2818
- * @constructor
2819
- * @implements {Encoder}
2820
- * @param {{fatal: boolean}} options
2821
- */
2822
- function ShiftJISEncoder(options) {
2823
- var fatal = options.fatal;
2824
- /**
2825
- * @param {Stream} stream Input stream.
2826
- * @param {number} code_point Next code point read from the stream.
2827
- * @return {(number|!Array.<number>)} Byte(s) to emit.
2828
- */
2829
- this.handler = function(stream, code_point) {
2830
- // 1. If code point is end-of-stream, return finished.
2831
- if (code_point === end_of_stream)
2832
- return finished;
2833
-
2834
- // 2. If code point is an ASCII code point or U+0080, return a
2835
- // byte whose value is code point.
2836
- if (isASCIICodePoint(code_point) || code_point === 0x0080)
2837
- return code_point;
2838
-
2839
- // 3. If code point is U+00A5, return byte 0x5C.
2840
- if (code_point === 0x00A5)
2841
- return 0x5C;
2842
-
2843
- // 4. If code point is U+203E, return byte 0x7E.
2844
- if (code_point === 0x203E)
2845
- return 0x7E;
2846
-
2847
- // 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
2848
- // return a byte whose value is code point − 0xFF61 + 0xA1.
2849
- if (inRange(code_point, 0xFF61, 0xFF9F))
2850
- return code_point - 0xFF61 + 0xA1;
2851
-
2852
- // 6. If code point is U+2212, set it to U+FF0D.
2853
- if (code_point === 0x2212)
2854
- code_point = 0xFF0D;
2855
-
2856
- // 7. Let pointer be the index Shift_JIS pointer for code point.
2857
- var pointer = indexShiftJISPointerFor(code_point);
2858
-
2859
- // 8. If pointer is null, return error with code point.
2860
- if (pointer === null)
2861
- return encoderError(code_point);
2862
-
2863
- // 9. Let lead be floor(pointer / 188).
2864
- var lead = floor(pointer / 188);
2865
-
2866
- // 10. Let lead offset be 0x81, if lead is less than 0x1F, and
2867
- // 0xC1 otherwise.
2868
- var lead_offset = (lead < 0x1F) ? 0x81 : 0xC1;
2869
-
2870
- // 11. Let trail be pointer % 188.
2871
- var trail = pointer % 188;
2872
-
2873
- // 12. Let offset be 0x40, if trail is less than 0x3F, and 0x41
2874
- // otherwise.
2875
- var offset = (trail < 0x3F) ? 0x40 : 0x41;
2876
-
2877
- // 13. Return two bytes whose values are lead + lead offset and
2878
- // trail + offset.
2879
- return [lead + lead_offset, trail + offset];
2880
- };
2881
- }
2882
-
2883
- /** @param {{fatal: boolean}} options */
2884
- encoders['Shift_JIS'] = function(options) {
2885
- return new ShiftJISEncoder(options);
2886
- };
2887
- /** @param {{fatal: boolean}} options */
2888
- decoders['Shift_JIS'] = function(options) {
2889
- return new ShiftJISDecoder(options);
2890
- };
2891
-
2892
- //
2893
- // 14. Legacy multi-byte Korean encodings
2894
- //
2895
-
2896
- // 14.1 euc-kr
2897
-
2898
- // 14.1.1 euc-kr decoder
2899
- /**
2900
- * @constructor
2901
- * @implements {Decoder}
2902
- * @param {{fatal: boolean}} options
2903
- */
2904
- function EUCKRDecoder(options) {
2905
- var fatal = options.fatal;
2906
-
2907
- // euc-kr's decoder has an associated euc-kr lead (initially 0x00).
2908
- var /** @type {number} */ euckr_lead = 0x00;
2909
- /**
2910
- * @param {Stream} stream The stream of bytes being decoded.
2911
- * @param {number} bite The next byte read from the stream.
2912
- * @return {?(number|!Array.<number>)} The next code point(s)
2913
- * decoded, or null if not enough data exists in the input
2914
- * stream to decode a complete code point.
2915
- */
2916
- this.handler = function(stream, bite) {
2917
- // 1. If byte is end-of-stream and euc-kr lead is not 0x00, set
2918
- // euc-kr lead to 0x00 and return error.
2919
- if (bite === end_of_stream && euckr_lead !== 0) {
2920
- euckr_lead = 0x00;
2921
- return decoderError(fatal);
2922
- }
2923
-
2924
- // 2. If byte is end-of-stream and euc-kr lead is 0x00, return
2925
- // finished.
2926
- if (bite === end_of_stream && euckr_lead === 0)
2927
- return finished;
2928
-
2929
- // 3. If euc-kr lead is not 0x00, let lead be euc-kr lead, let
2930
- // pointer be null, set euc-kr lead to 0x00, and then run these
2931
- // substeps:
2932
- if (euckr_lead !== 0x00) {
2933
- var lead = euckr_lead;
2934
- var pointer = null;
2935
- euckr_lead = 0x00;
2936
-
2937
- // 1. If byte is in the range 0x41 to 0xFE, inclusive, set
2938
- // pointer to (lead − 0x81) × 190 + (byte − 0x41).
2939
- if (inRange(bite, 0x41, 0xFE))
2940
- pointer = (lead - 0x81) * 190 + (bite - 0x41);
2941
-
2942
- // 2. Let code point be null, if pointer is null, and the
2943
- // index code point for pointer in index euc-kr otherwise.
2944
- var code_point = (pointer === null)
2945
- ? null : indexCodePointFor(pointer, index('euc-kr'));
2946
-
2947
- // 3. If code point is null and byte is an ASCII byte, prepend
2948
- // byte to stream.
2949
- if (pointer === null && isASCIIByte(bite))
2950
- stream.prepend(bite);
2951
-
2952
- // 4. If code point is null, return error.
2953
- if (code_point === null)
2954
- return decoderError(fatal);
2955
-
2956
- // 5. Return a code point whose value is code point.
2957
- return code_point;
2958
- }
2959
-
2960
- // 4. If byte is an ASCII byte, return a code point whose value
2961
- // is byte.
2962
- if (isASCIIByte(bite))
2963
- return bite;
2964
-
2965
- // 5. If byte is in the range 0x81 to 0xFE, inclusive, set
2966
- // euc-kr lead to byte and return continue.
2967
- if (inRange(bite, 0x81, 0xFE)) {
2968
- euckr_lead = bite;
2969
- return null;
2970
- }
2971
-
2972
- // 6. Return error.
2973
- return decoderError(fatal);
2974
- };
2975
- }
2976
-
2977
- // 14.1.2 euc-kr encoder
2978
- /**
2979
- * @constructor
2980
- * @implements {Encoder}
2981
- * @param {{fatal: boolean}} options
2982
- */
2983
- function EUCKREncoder(options) {
2984
- var fatal = options.fatal;
2985
- /**
2986
- * @param {Stream} stream Input stream.
2987
- * @param {number} code_point Next code point read from the stream.
2988
- * @return {(number|!Array.<number>)} Byte(s) to emit.
2989
- */
2990
- this.handler = function(stream, code_point) {
2991
- // 1. If code point is end-of-stream, return finished.
2992
- if (code_point === end_of_stream)
2993
- return finished;
2994
-
2995
- // 2. If code point is an ASCII code point, return a byte whose
2996
- // value is code point.
2997
- if (isASCIICodePoint(code_point))
2998
- return code_point;
2999
-
3000
- // 3. Let pointer be the index pointer for code point in index
3001
- // euc-kr.
3002
- var pointer = indexPointerFor(code_point, index('euc-kr'));
3003
-
3004
- // 4. If pointer is null, return error with code point.
3005
- if (pointer === null)
3006
- return encoderError(code_point);
3007
-
3008
- // 5. Let lead be floor(pointer / 190) + 0x81.
3009
- var lead = floor(pointer / 190) + 0x81;
3010
-
3011
- // 6. Let trail be pointer % 190 + 0x41.
3012
- var trail = (pointer % 190) + 0x41;
3013
-
3014
- // 7. Return two bytes whose values are lead and trail.
3015
- return [lead, trail];
3016
- };
3017
- }
3018
-
3019
- /** @param {{fatal: boolean}} options */
3020
- encoders['EUC-KR'] = function(options) {
3021
- return new EUCKREncoder(options);
3022
- };
3023
- /** @param {{fatal: boolean}} options */
3024
- decoders['EUC-KR'] = function(options) {
3025
- return new EUCKRDecoder(options);
3026
- };
3027
-
3028
-
3029
- //
3030
- // 15. Legacy miscellaneous encodings
3031
- //
3032
-
3033
- // 15.1 replacement
3034
-
3035
- // Not needed - API throws RangeError
3036
-
3037
- // 15.2 Common infrastructure for utf-16be and utf-16le
3038
-
3039
- /**
3040
- * @param {number} code_unit
3041
- * @param {boolean} utf16be
3042
- * @return {!Array.<number>} bytes
3043
- */
3044
- function convertCodeUnitToBytes(code_unit, utf16be) {
3045
- // 1. Let byte1 be code unit >> 8.
3046
- var byte1 = code_unit >> 8;
3047
-
3048
- // 2. Let byte2 be code unit & 0x00FF.
3049
- var byte2 = code_unit & 0x00FF;
3050
-
3051
- // 3. Then return the bytes in order:
3052
- // utf-16be flag is set: byte1, then byte2.
3053
- if (utf16be)
3054
- return [byte1, byte2];
3055
- // utf-16be flag is unset: byte2, then byte1.
3056
- return [byte2, byte1];
3057
- }
3058
-
3059
- // 15.2.1 shared utf-16 decoder
3060
- /**
3061
- * @constructor
3062
- * @implements {Decoder}
3063
- * @param {boolean} utf16_be True if big-endian, false if little-endian.
3064
- * @param {{fatal: boolean}} options
3065
- */
3066
- function UTF16Decoder(utf16_be, options) {
3067
- var fatal = options.fatal;
3068
- var /** @type {?number} */ utf16_lead_byte = null,
3069
- /** @type {?number} */ utf16_lead_surrogate = null;
3070
- /**
3071
- * @param {Stream} stream The stream of bytes being decoded.
3072
- * @param {number} bite The next byte read from the stream.
3073
- * @return {?(number|!Array.<number>)} The next code point(s)
3074
- * decoded, or null if not enough data exists in the input
3075
- * stream to decode a complete code point.
3076
- */
3077
- this.handler = function(stream, bite) {
3078
- // 1. If byte is end-of-stream and either utf-16 lead byte or
3079
- // utf-16 lead surrogate is not null, set utf-16 lead byte and
3080
- // utf-16 lead surrogate to null, and return error.
3081
- if (bite === end_of_stream && (utf16_lead_byte !== null ||
3082
- utf16_lead_surrogate !== null)) {
3083
- return decoderError(fatal);
3084
- }
3085
-
3086
- // 2. If byte is end-of-stream and utf-16 lead byte and utf-16
3087
- // lead surrogate are null, return finished.
3088
- if (bite === end_of_stream && utf16_lead_byte === null &&
3089
- utf16_lead_surrogate === null) {
3090
- return finished;
3091
- }
3092
-
3093
- // 3. If utf-16 lead byte is null, set utf-16 lead byte to byte
3094
- // and return continue.
3095
- if (utf16_lead_byte === null) {
3096
- utf16_lead_byte = bite;
3097
- return null;
3098
- }
3099
-
3100
- // 4. Let code unit be the result of:
3101
- var code_unit;
3102
- if (utf16_be) {
3103
- // utf-16be decoder flag is set
3104
- // (utf-16 lead byte << 8) + byte.
3105
- code_unit = (utf16_lead_byte << 8) + bite;
3106
- } else {
3107
- // utf-16be decoder flag is unset
3108
- // (byte << 8) + utf-16 lead byte.
3109
- code_unit = (bite << 8) + utf16_lead_byte;
3110
- }
3111
- // Then set utf-16 lead byte to null.
3112
- utf16_lead_byte = null;
3113
-
3114
- // 5. If utf-16 lead surrogate is not null, let lead surrogate
3115
- // be utf-16 lead surrogate, set utf-16 lead surrogate to null,
3116
- // and then run these substeps:
3117
- if (utf16_lead_surrogate !== null) {
3118
- var lead_surrogate = utf16_lead_surrogate;
3119
- utf16_lead_surrogate = null;
3120
-
3121
- // 1. If code unit is in the range U+DC00 to U+DFFF,
3122
- // inclusive, return a code point whose value is 0x10000 +
3123
- // ((lead surrogate − 0xD800) << 10) + (code unit − 0xDC00).
3124
- if (inRange(code_unit, 0xDC00, 0xDFFF)) {
3125
- return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
3126
- (code_unit - 0xDC00);
3127
- }
3128
-
3129
- // 2. Prepend the sequence resulting of converting code unit
3130
- // to bytes using utf-16be decoder flag to stream and return
3131
- // error.
3132
- stream.prepend(convertCodeUnitToBytes(code_unit, utf16_be));
3133
- return decoderError(fatal);
3134
- }
3135
-
3136
- // 6. If code unit is in the range U+D800 to U+DBFF, inclusive,
3137
- // set utf-16 lead surrogate to code unit and return continue.
3138
- if (inRange(code_unit, 0xD800, 0xDBFF)) {
3139
- utf16_lead_surrogate = code_unit;
3140
- return null;
3141
- }
3142
-
3143
- // 7. If code unit is in the range U+DC00 to U+DFFF, inclusive,
3144
- // return error.
3145
- if (inRange(code_unit, 0xDC00, 0xDFFF))
3146
- return decoderError(fatal);
3147
-
3148
- // 8. Return code point code unit.
3149
- return code_unit;
3150
- };
3151
- }
3152
-
3153
- // 15.2.2 shared utf-16 encoder
3154
- /**
3155
- * @constructor
3156
- * @implements {Encoder}
3157
- * @param {boolean} utf16_be True if big-endian, false if little-endian.
3158
- * @param {{fatal: boolean}} options
3159
- */
3160
- function UTF16Encoder(utf16_be, options) {
3161
- var fatal = options.fatal;
3162
- /**
3163
- * @param {Stream} stream Input stream.
3164
- * @param {number} code_point Next code point read from the stream.
3165
- * @return {(number|!Array.<number>)} Byte(s) to emit.
3166
- */
3167
- this.handler = function(stream, code_point) {
3168
- // 1. If code point is end-of-stream, return finished.
3169
- if (code_point === end_of_stream)
3170
- return finished;
3171
-
3172
- // 2. If code point is in the range U+0000 to U+FFFF, inclusive,
3173
- // return the sequence resulting of converting code point to
3174
- // bytes using utf-16be encoder flag.
3175
- if (inRange(code_point, 0x0000, 0xFFFF))
3176
- return convertCodeUnitToBytes(code_point, utf16_be);
3177
-
3178
- // 3. Let lead be ((code point − 0x10000) >> 10) + 0xD800,
3179
- // converted to bytes using utf-16be encoder flag.
3180
- var lead = convertCodeUnitToBytes(
3181
- ((code_point - 0x10000) >> 10) + 0xD800, utf16_be);
3182
-
3183
- // 4. Let trail be ((code point − 0x10000) & 0x3FF) + 0xDC00,
3184
- // converted to bytes using utf-16be encoder flag.
3185
- var trail = convertCodeUnitToBytes(
3186
- ((code_point - 0x10000) & 0x3FF) + 0xDC00, utf16_be);
3187
-
3188
- // 5. Return a byte sequence of lead followed by trail.
3189
- return lead.concat(trail);
3190
- };
3191
- }
3192
-
3193
- // 15.3 utf-16be
3194
- // 15.3.1 utf-16be decoder
3195
- /** @param {{fatal: boolean}} options */
3196
- encoders['UTF-16BE'] = function(options) {
3197
- return new UTF16Encoder(true, options);
3198
- };
3199
- // 15.3.2 utf-16be encoder
3200
- /** @param {{fatal: boolean}} options */
3201
- decoders['UTF-16BE'] = function(options) {
3202
- return new UTF16Decoder(true, options);
3203
- };
3204
-
3205
- // 15.4 utf-16le
3206
- // 15.4.1 utf-16le decoder
3207
- /** @param {{fatal: boolean}} options */
3208
- encoders['UTF-16LE'] = function(options) {
3209
- return new UTF16Encoder(false, options);
3210
- };
3211
- // 15.4.2 utf-16le encoder
3212
- /** @param {{fatal: boolean}} options */
3213
- decoders['UTF-16LE'] = function(options) {
3214
- return new UTF16Decoder(false, options);
3215
- };
3216
-
3217
- // 15.5 x-user-defined
3218
-
3219
- // 15.5.1 x-user-defined decoder
3220
- /**
3221
- * @constructor
3222
- * @implements {Decoder}
3223
- * @param {{fatal: boolean}} options
3224
- */
3225
- function XUserDefinedDecoder(options) {
3226
- var fatal = options.fatal;
3227
- /**
3228
- * @param {Stream} stream The stream of bytes being decoded.
3229
- * @param {number} bite The next byte read from the stream.
3230
- * @return {?(number|!Array.<number>)} The next code point(s)
3231
- * decoded, or null if not enough data exists in the input
3232
- * stream to decode a complete code point.
3233
- */
3234
- this.handler = function(stream, bite) {
3235
- // 1. If byte is end-of-stream, return finished.
3236
- if (bite === end_of_stream)
3237
- return finished;
3238
-
3239
- // 2. If byte is an ASCII byte, return a code point whose value
3240
- // is byte.
3241
- if (isASCIIByte(bite))
3242
- return bite;
3243
-
3244
- // 3. Return a code point whose value is 0xF780 + byte − 0x80.
3245
- return 0xF780 + bite - 0x80;
3246
- };
3247
- }
3248
-
3249
- // 15.5.2 x-user-defined encoder
3250
- /**
3251
- * @constructor
3252
- * @implements {Encoder}
3253
- * @param {{fatal: boolean}} options
3254
- */
3255
- function XUserDefinedEncoder(options) {
3256
- var fatal = options.fatal;
3257
- /**
3258
- * @param {Stream} stream Input stream.
3259
- * @param {number} code_point Next code point read from the stream.
3260
- * @return {(number|!Array.<number>)} Byte(s) to emit.
3261
- */
3262
- this.handler = function(stream, code_point) {
3263
- // 1.If code point is end-of-stream, return finished.
3264
- if (code_point === end_of_stream)
3265
- return finished;
3266
-
3267
- // 2. If code point is an ASCII code point, return a byte whose
3268
- // value is code point.
3269
- if (isASCIICodePoint(code_point))
3270
- return code_point;
3271
-
3272
- // 3. If code point is in the range U+F780 to U+F7FF, inclusive,
3273
- // return a byte whose value is code point − 0xF780 + 0x80.
3274
- if (inRange(code_point, 0xF780, 0xF7FF))
3275
- return code_point - 0xF780 + 0x80;
3276
-
3277
- // 4. Return error with code point.
3278
- return encoderError(code_point);
3279
- };
3280
- }
3281
-
3282
- /** @param {{fatal: boolean}} options */
3283
- encoders['x-user-defined'] = function(options) {
3284
- return new XUserDefinedEncoder(options);
3285
- };
3286
- /** @param {{fatal: boolean}} options */
3287
- decoders['x-user-defined'] = function(options) {
3288
- return new XUserDefinedDecoder(options);
3289
- };
3290
-
3291
- if (typeof module !== "undefined" && module.exports) {
3292
- module.exports = {
3293
- TextEncoder: TextEncoder,
3294
- TextDecoder: TextDecoder,
3295
- EncodingIndexes: require("./encoding-indexes.js")["encoding-indexes"]
3296
- };
3297
- }
3298
-
3299
- // For strict environments where `this` inside the global scope
3300
- // is `undefined`, take a pure object instead
3301
- }(this || {}));