@n8n/imap 0.8.19 → 0.9.0

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.
Files changed (44) hide show
  1. package/LICENSE.md +88 -0
  2. package/LICENSE_EE.md +27 -0
  3. package/dist/ImapSimple.d.ts +16 -0
  4. package/dist/ImapSimple.js +120 -0
  5. package/dist/ImapSimple.js.map +1 -0
  6. package/dist/PartData.d.ts +24 -0
  7. package/dist/PartData.js +109 -0
  8. package/dist/PartData.js.map +1 -0
  9. package/dist/build.tsbuildinfo +1 -0
  10. package/dist/errors.d.ts +12 -0
  11. package/dist/errors.js +30 -0
  12. package/dist/errors.js.map +1 -0
  13. package/dist/helpers/getMessage.d.ts +3 -0
  14. package/dist/helpers/getMessage.js +37 -0
  15. package/dist/helpers/getMessage.js.map +1 -0
  16. package/dist/index.d.ts +7 -0
  17. package/dist/index.js +85 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/types.d.ts +30 -0
  20. package/dist/types.js +3 -0
  21. package/dist/types.js.map +1 -0
  22. package/package.json +42 -18
  23. package/LICENSE +0 -19
  24. package/README.md +0 -754
  25. package/deps/encoding/encoding-indexes.js +0 -73
  26. package/deps/encoding/encoding.js +0 -2340
  27. package/deps/test/utf7-imap.js +0 -48
  28. package/deps/test/utf7-rfc2152.js +0 -78
  29. package/deps/utf7.js +0 -104
  30. package/index.d.ts +0 -396
  31. package/lib/Connection.js +0 -2131
  32. package/lib/Parser.js +0 -1026
  33. package/test/test-connection-fetch-dup.js +0 -92
  34. package/test/test-connection-fetch-frag.js +0 -92
  35. package/test/test-connection-fetch-spillover.js +0 -176
  36. package/test/test-connection-fetch-stringbody.js +0 -104
  37. package/test/test-connection-idle-normal.js +0 -144
  38. package/test/test-connection-idle-order.js +0 -138
  39. package/test/test-parse-bodystructure.js +0 -83
  40. package/test/test-parse-envelope-addrs.js +0 -117
  41. package/test/test-parse-expr.js +0 -92
  42. package/test/test-parse-header.js +0 -109
  43. package/test/test-parser.js +0 -543
  44. package/test/test.js +0 -4
@@ -1,2340 +0,0 @@
1
- /*
2
- Modifications for better node.js integration:
3
- Copyright 2013 Brian White. All rights reserved.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to
7
- deal in the Software without restriction, including without limitation the
8
- rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9
- sell copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21
- IN THE SOFTWARE.
22
- */
23
- /*
24
- Original source code:
25
- Copyright 2012 Joshua Bell
26
-
27
- Licensed under the Apache License, Version 2.0 (the "License");
28
- you may not use this file except in compliance with the License.
29
- You may obtain a copy of the License at
30
-
31
- http://www.apache.org/licenses/LICENSE-2.0
32
-
33
- Unless required by applicable law or agreed to in writing, software
34
- distributed under the License is distributed on an "AS IS" BASIS,
35
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36
- See the License for the specific language governing permissions and
37
- limitations under the License.
38
- */
39
-
40
- //
41
- // Utilities
42
- //
43
-
44
- /**
45
- * @param {number} a The number to test.
46
- * @param {number} min The minimum value in the range, inclusive.
47
- * @param {number} max The maximum value in the range, inclusive.
48
- * @return {boolean} True if a >= min and a <= max.
49
- */
50
- function inRange(a, min, max) {
51
- return min <= a && a <= max;
52
- }
53
-
54
- /**
55
- * @param {number} n The numerator.
56
- * @param {number} d The denominator.
57
- * @return {number} The result of the integer division of n by d.
58
- */
59
- function div(n, d) {
60
- return Math.floor(n / d);
61
- }
62
-
63
-
64
- //
65
- // Implementation of Encoding specification
66
- // http://dvcs.w3.org/hg/encoding/raw-file/tip/Overview.html
67
- //
68
-
69
- //
70
- // 3. Terminology
71
- //
72
-
73
- //
74
- // 4. Encodings
75
- //
76
-
77
- /** @const */ var EOF_byte = -1;
78
- /** @const */ var EOF_code_point = -1;
79
-
80
- /**
81
- * @constructor
82
- * @param {Buffer} bytes Array of bytes that provide the stream.
83
- */
84
- function ByteInputStream(bytes) {
85
- /** @type {number} */
86
- var pos = 0;
87
-
88
- /** @return {number} Get the next byte from the stream. */
89
- this.get = function() {
90
- return (pos >= bytes.length) ? EOF_byte : Number(bytes[pos]);
91
- };
92
-
93
- /** @param {number} n Number (positive or negative) by which to
94
- * offset the byte pointer. */
95
- this.offset = function(n) {
96
- pos += n;
97
- if (pos < 0) {
98
- throw new Error('Seeking past start of the buffer');
99
- }
100
- if (pos > bytes.length) {
101
- throw new Error('Seeking past EOF');
102
- }
103
- };
104
-
105
- /**
106
- * @param {Array.<number>} test Array of bytes to compare against.
107
- * @return {boolean} True if the start of the stream matches the test
108
- * bytes.
109
- */
110
- this.match = function(test) {
111
- if (test.length > pos + bytes.length) {
112
- return false;
113
- }
114
- var i;
115
- for (i = 0; i < test.length; i += 1) {
116
- if (Number(bytes[pos + i]) !== test[i]) {
117
- return false;
118
- }
119
- }
120
- return true;
121
- };
122
- }
123
-
124
- /**
125
- * @constructor
126
- * @param {Array.<number>} bytes The array to write bytes into.
127
- */
128
- function ByteOutputStream(bytes) {
129
- /** @type {number} */
130
- var pos = 0;
131
-
132
- /**
133
- * @param {...number} var_args The byte or bytes to emit into the stream.
134
- * @return {number} The last byte emitted.
135
- */
136
- this.emit = function(var_args) {
137
- /** @type {number} */
138
- var last = EOF_byte;
139
- var i;
140
- for (i = 0; i < arguments.length; ++i) {
141
- last = Number(arguments[i]);
142
- bytes[pos++] = last;
143
- }
144
- return last;
145
- };
146
- }
147
-
148
- /**
149
- * @constructor
150
- * @param {string} string The source of code units for the stream.
151
- */
152
- function CodePointInputStream(string) {
153
- /**
154
- * @param {string} string Input string of UTF-16 code units.
155
- * @return {Array.<number>} Code points.
156
- */
157
- function stringToCodePoints(string) {
158
- /** @type {Array.<number>} */
159
- var cps = [];
160
- // Based on http://www.w3.org/TR/WebIDL/#idl-DOMString
161
- var i = 0, n = string.length;
162
- while (i < string.length) {
163
- var c = string.charCodeAt(i);
164
- if (!inRange(c, 0xD800, 0xDFFF)) {
165
- cps.push(c);
166
- } else if (inRange(c, 0xDC00, 0xDFFF)) {
167
- cps.push(0xFFFD);
168
- } else { // (inRange(cu, 0xD800, 0xDBFF))
169
- if (i === n - 1) {
170
- cps.push(0xFFFD);
171
- } else {
172
- var d = string.charCodeAt(i + 1);
173
- if (inRange(d, 0xDC00, 0xDFFF)) {
174
- var a = c & 0x3FF;
175
- var b = d & 0x3FF;
176
- i += 1;
177
- cps.push(0x10000 + (a << 10) + b);
178
- } else {
179
- cps.push(0xFFFD);
180
- }
181
- }
182
- }
183
- i += 1;
184
- }
185
- return cps;
186
- }
187
-
188
- /** @type {number} */
189
- var pos = 0;
190
- /** @type {Array.<number>} */
191
- var cps = stringToCodePoints(string);
192
-
193
- /** @param {number} n The number of bytes (positive or negative)
194
- * to advance the code point pointer by.*/
195
- this.offset = function(n) {
196
- pos += n;
197
- if (pos < 0) {
198
- throw new Error('Seeking past start of the buffer');
199
- }
200
- if (pos > cps.length) {
201
- throw new Error('Seeking past EOF');
202
- }
203
- };
204
-
205
-
206
- /** @return {number} Get the next code point from the stream. */
207
- this.get = function() {
208
- if (pos >= cps.length) {
209
- return EOF_code_point;
210
- }
211
- return cps[pos];
212
- };
213
- }
214
-
215
- /**
216
- * @constructor
217
- */
218
- function CodePointOutputStream() {
219
- /** @type {string} */
220
- var string = '';
221
-
222
- /** @return {string} The accumulated string. */
223
- this.string = function() {
224
- return string;
225
- };
226
-
227
- /** @param {number} c The code point to encode into the stream. */
228
- this.emit = function(c) {
229
- if (c <= 0xFFFF) {
230
- string += String.fromCharCode(c);
231
- } else {
232
- c -= 0x10000;
233
- string += String.fromCharCode(0xD800 + ((c >> 10) & 0x3ff));
234
- string += String.fromCharCode(0xDC00 + (c & 0x3ff));
235
- }
236
- };
237
- }
238
-
239
- /**
240
- * @constructor
241
- * @param {string} message Description of the error.
242
- */
243
- function EncodingError(message) {
244
- this.name = 'EncodingError';
245
- this.message = message;
246
- this.code = 0;
247
- }
248
- EncodingError.prototype = Error.prototype;
249
-
250
- /**
251
- * @param {boolean} fatal If true, decoding errors raise an exception.
252
- * @param {number=} opt_code_point Override the standard fallback code point.
253
- * @return {number} The code point to insert on a decoding error.
254
- */
255
- function decoderError(fatal, opt_code_point) {
256
- if (fatal) {
257
- throw new EncodingError('Decoder error');
258
- }
259
- return opt_code_point || 0xFFFD;
260
- }
261
-
262
- /**
263
- * @param {number} code_point The code point that could not be encoded.
264
- */
265
- function encoderError(code_point) {
266
- throw new EncodingError('The code point ' + code_point +
267
- ' could not be encoded.');
268
- }
269
-
270
- /**
271
- * @param {string} label The encoding label.
272
- * @return {?{name:string,labels:Array.<string>}}
273
- */
274
- function getEncoding(label) {
275
- label = String(label).trim().toLowerCase();
276
- if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
277
- return label_to_encoding[label];
278
- }
279
- return null;
280
- }
281
-
282
- /** @type {Array.<{encodings: Array.<{name:string,labels:Array.<string>}>,
283
- * heading: string}>} */
284
- var encodings = [
285
- {
286
- "encodings": [
287
- {
288
- "labels": [
289
- "unicode-1-1-utf-8",
290
- "utf-8",
291
- "utf8"
292
- ],
293
- "name": "utf-8"
294
- }
295
- ],
296
- "heading": "The Encoding"
297
- },
298
- {
299
- "encodings": [
300
- {
301
- "labels": [
302
- "864",
303
- "cp864",
304
- "csibm864",
305
- "ibm864"
306
- ],
307
- "name": "ibm864"
308
- },
309
- {
310
- "labels": [
311
- "866",
312
- "cp866",
313
- "csibm866",
314
- "ibm866"
315
- ],
316
- "name": "ibm866"
317
- },
318
- {
319
- "labels": [
320
- "csisolatin2",
321
- "iso-8859-2",
322
- "iso-ir-101",
323
- "iso8859-2",
324
- "iso88592",
325
- "iso_8859-2",
326
- "iso_8859-2:1987",
327
- "l2",
328
- "latin2"
329
- ],
330
- "name": "iso-8859-2"
331
- },
332
- {
333
- "labels": [
334
- "csisolatin3",
335
- "iso-8859-3",
336
- "iso-ir-109",
337
- "iso8859-3",
338
- "iso88593",
339
- "iso_8859-3",
340
- "iso_8859-3:1988",
341
- "l3",
342
- "latin3"
343
- ],
344
- "name": "iso-8859-3"
345
- },
346
- {
347
- "labels": [
348
- "csisolatin4",
349
- "iso-8859-4",
350
- "iso-ir-110",
351
- "iso8859-4",
352
- "iso88594",
353
- "iso_8859-4",
354
- "iso_8859-4:1988",
355
- "l4",
356
- "latin4"
357
- ],
358
- "name": "iso-8859-4"
359
- },
360
- {
361
- "labels": [
362
- "csisolatincyrillic",
363
- "cyrillic",
364
- "iso-8859-5",
365
- "iso-ir-144",
366
- "iso8859-5",
367
- "iso88595",
368
- "iso_8859-5",
369
- "iso_8859-5:1988"
370
- ],
371
- "name": "iso-8859-5"
372
- },
373
- {
374
- "labels": [
375
- "arabic",
376
- "asmo-708",
377
- "csiso88596e",
378
- "csiso88596i",
379
- "csisolatinarabic",
380
- "ecma-114",
381
- "iso-8859-6",
382
- "iso-8859-6-e",
383
- "iso-8859-6-i",
384
- "iso-ir-127",
385
- "iso8859-6",
386
- "iso88596",
387
- "iso_8859-6",
388
- "iso_8859-6:1987"
389
- ],
390
- "name": "iso-8859-6"
391
- },
392
- {
393
- "labels": [
394
- "csisolatingreek",
395
- "ecma-118",
396
- "elot_928",
397
- "greek",
398
- "greek8",
399
- "iso-8859-7",
400
- "iso-ir-126",
401
- "iso8859-7",
402
- "iso88597",
403
- "iso_8859-7",
404
- "iso_8859-7:1987",
405
- "sun_eu_greek"
406
- ],
407
- "name": "iso-8859-7"
408
- },
409
- {
410
- "labels": [
411
- "csiso88598e",
412
- "csisolatinhebrew",
413
- "hebrew",
414
- "iso-8859-8",
415
- "iso-8859-8-e",
416
- "iso-ir-138",
417
- "iso8859-8",
418
- "iso88598",
419
- "iso_8859-8",
420
- "iso_8859-8:1988",
421
- "visual"
422
- ],
423
- "name": "iso-8859-8"
424
- },
425
- {
426
- "labels": [
427
- "csiso88598i",
428
- "iso-8859-8-i",
429
- "logical"
430
- ],
431
- "name": "iso-8859-8-i"
432
- },
433
- {
434
- "labels": [
435
- "csisolatin6",
436
- "iso-8859-10",
437
- "iso-ir-157",
438
- "iso8859-10",
439
- "iso885910",
440
- "l6",
441
- "latin6"
442
- ],
443
- "name": "iso-8859-10"
444
- },
445
- {
446
- "labels": [
447
- "iso-8859-13",
448
- "iso8859-13",
449
- "iso885913"
450
- ],
451
- "name": "iso-8859-13"
452
- },
453
- {
454
- "labels": [
455
- "iso-8859-14",
456
- "iso8859-14",
457
- "iso885914"
458
- ],
459
- "name": "iso-8859-14"
460
- },
461
- {
462
- "labels": [
463
- "csisolatin9",
464
- "iso-8859-15",
465
- "iso8859-15",
466
- "iso885915",
467
- "iso_8859-15",
468
- "l9"
469
- ],
470
- "name": "iso-8859-15"
471
- },
472
- {
473
- "labels": [
474
- "iso-8859-16"
475
- ],
476
- "name": "iso-8859-16"
477
- },
478
- {
479
- "labels": [
480
- "cskoi8r",
481
- "koi",
482
- "koi8",
483
- "koi8-r",
484
- "koi8_r"
485
- ],
486
- "name": "koi8-r"
487
- },
488
- {
489
- "labels": [
490
- "koi8-u"
491
- ],
492
- "name": "koi8-u"
493
- },
494
- {
495
- "labels": [
496
- "csmacintosh",
497
- "mac",
498
- "macintosh",
499
- "x-mac-roman"
500
- ],
501
- "name": "macintosh"
502
- },
503
- {
504
- "labels": [
505
- "dos-874",
506
- "iso-8859-11",
507
- "iso8859-11",
508
- "iso885911",
509
- "tis-620",
510
- "windows-874"
511
- ],
512
- "name": "windows-874"
513
- },
514
- {
515
- "labels": [
516
- "cp1250",
517
- "windows-1250",
518
- "x-cp1250"
519
- ],
520
- "name": "windows-1250"
521
- },
522
- {
523
- "labels": [
524
- "cp1251",
525
- "windows-1251",
526
- "x-cp1251"
527
- ],
528
- "name": "windows-1251"
529
- },
530
- {
531
- "labels": [
532
- "ansi_x3.4-1968",
533
- "ascii",
534
- "cp1252",
535
- "cp819",
536
- "csisolatin1",
537
- "ibm819",
538
- "iso-8859-1",
539
- "iso-ir-100",
540
- "iso8859-1",
541
- "iso88591",
542
- "iso_8859-1",
543
- "iso_8859-1:1987",
544
- "l1",
545
- "latin1",
546
- "us-ascii",
547
- "windows-1252",
548
- "x-cp1252"
549
- ],
550
- "name": "windows-1252"
551
- },
552
- {
553
- "labels": [
554
- "cp1253",
555
- "windows-1253",
556
- "x-cp1253"
557
- ],
558
- "name": "windows-1253"
559
- },
560
- {
561
- "labels": [
562
- "cp1254",
563
- "csisolatin5",
564
- "iso-8859-9",
565
- "iso-ir-148",
566
- "iso8859-9",
567
- "iso88599",
568
- "iso_8859-9",
569
- "iso_8859-9:1989",
570
- "l5",
571
- "latin5",
572
- "windows-1254",
573
- "x-cp1254"
574
- ],
575
- "name": "windows-1254"
576
- },
577
- {
578
- "labels": [
579
- "cp1255",
580
- "windows-1255",
581
- "x-cp1255"
582
- ],
583
- "name": "windows-1255"
584
- },
585
- {
586
- "labels": [
587
- "cp1256",
588
- "windows-1256",
589
- "x-cp1256"
590
- ],
591
- "name": "windows-1256"
592
- },
593
- {
594
- "labels": [
595
- "cp1257",
596
- "windows-1257",
597
- "x-cp1257"
598
- ],
599
- "name": "windows-1257"
600
- },
601
- {
602
- "labels": [
603
- "cp1258",
604
- "windows-1258",
605
- "x-cp1258"
606
- ],
607
- "name": "windows-1258"
608
- },
609
- {
610
- "labels": [
611
- "x-mac-cyrillic",
612
- "x-mac-ukrainian"
613
- ],
614
- "name": "x-mac-cyrillic"
615
- }
616
- ],
617
- "heading": "Legacy single-byte encodings"
618
- },
619
- {
620
- "encodings": [
621
- {
622
- "labels": [
623
- "chinese",
624
- "csgb2312",
625
- "csiso58gb231280",
626
- "gb2312",
627
- "gb_2312",
628
- "gb_2312-80",
629
- "gbk",
630
- "iso-ir-58",
631
- "x-gbk"
632
- ],
633
- "name": "gbk"
634
- },
635
- {
636
- "labels": [
637
- "gb18030"
638
- ],
639
- "name": "gb18030"
640
- },
641
- {
642
- "labels": [
643
- "hz-gb-2312"
644
- ],
645
- "name": "hz-gb-2312"
646
- }
647
- ],
648
- "heading": "Legacy multi-byte Chinese (simplified) encodings"
649
- },
650
- {
651
- "encodings": [
652
- {
653
- "labels": [
654
- "big5",
655
- "big5-hkscs",
656
- "cn-big5",
657
- "csbig5",
658
- "x-x-big5"
659
- ],
660
- "name": "big5"
661
- }
662
- ],
663
- "heading": "Legacy multi-byte Chinese (traditional) encodings"
664
- },
665
- {
666
- "encodings": [
667
- {
668
- "labels": [
669
- "cseucpkdfmtjapanese",
670
- "euc-jp",
671
- "x-euc-jp"
672
- ],
673
- "name": "euc-jp"
674
- },
675
- {
676
- "labels": [
677
- "csiso2022jp",
678
- "iso-2022-jp"
679
- ],
680
- "name": "iso-2022-jp"
681
- },
682
- {
683
- "labels": [
684
- "csshiftjis",
685
- "ms_kanji",
686
- "shift-jis",
687
- "shift_jis",
688
- "sjis",
689
- "windows-31j",
690
- "x-sjis"
691
- ],
692
- "name": "shift_jis"
693
- }
694
- ],
695
- "heading": "Legacy multi-byte Japanese encodings"
696
- },
697
- {
698
- "encodings": [
699
- {
700
- "labels": [
701
- "cseuckr",
702
- "csksc56011987",
703
- "euc-kr",
704
- "iso-ir-149",
705
- "korean",
706
- "ks_c_5601-1987",
707
- "ks_c_5601-1989",
708
- "ksc5601",
709
- "ksc_5601",
710
- "windows-949"
711
- ],
712
- "name": "euc-kr"
713
- }
714
- ],
715
- "heading": "Legacy multi-byte Korean encodings"
716
- },
717
- {
718
- "encodings": [
719
- {
720
- "labels": [
721
- "csiso2022kr",
722
- "iso-2022-kr",
723
- "iso-2022-cn",
724
- "iso-2022-cn-ext"
725
- ],
726
- "name": "replacement"
727
- },
728
- {
729
- "labels": [
730
- "utf-16be"
731
- ],
732
- "name": "utf-16be"
733
- },
734
- {
735
- "labels": [
736
- "utf-16",
737
- "utf-16le"
738
- ],
739
- "name": "utf-16le"
740
- },
741
- {
742
- "labels": [
743
- "x-user-defined"
744
- ],
745
- "name": "x-user-defined"
746
- }
747
- ],
748
- "heading": "Legacy miscellaneous encodings"
749
- }
750
- ];
751
-
752
- var name_to_encoding = {};
753
- var label_to_encoding = {};
754
- encodings.forEach(function(category) {
755
- category.encodings.forEach(function(encoding) {
756
- name_to_encoding[encoding.name] = encoding;
757
- encoding.labels.forEach(function(label) {
758
- label_to_encoding[label] = encoding;
759
- });
760
- });
761
- });
762
-
763
- //
764
- // 5. Indexes
765
- //
766
-
767
- /**
768
- * @param {number} pointer The |pointer| to search for.
769
- * @param {Array.<?number>} index The |index| to search within.
770
- * @return {?number} The code point corresponding to |pointer| in |index|,
771
- * or null if |code point| is not in |index|.
772
- */
773
- function indexCodePointFor(pointer, index) {
774
- return (index || [])[pointer] || null;
775
- }
776
-
777
- /**
778
- * @param {number} code_point The |code point| to search for.
779
- * @param {Array.<?number>} index The |index| to search within.
780
- * @return {?number} The first pointer corresponding to |code point| in
781
- * |index|, or null if |code point| is not in |index|.
782
- */
783
- function indexPointerFor(code_point, index) {
784
- var pointer = index.indexOf(code_point);
785
- return pointer === -1 ? null : pointer;
786
- }
787
-
788
- /** @type {Object.<string, (Array.<number>|Array.<Array.<number>>)>} */
789
- var indexes = require('./encoding-indexes');
790
-
791
- /**
792
- * @param {number} pointer The |pointer| to search for in the gb18030 index.
793
- * @return {?number} The code point corresponding to |pointer| in |index|,
794
- * or null if |code point| is not in the gb18030 index.
795
- */
796
- function indexGB18030CodePointFor(pointer) {
797
- if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575)) {
798
- return null;
799
- }
800
- var /** @type {number} */ offset = 0,
801
- /** @type {number} */ code_point_offset = 0,
802
- /** @type {Array.<Array.<number>>} */ index = indexes['gb18030'];
803
- var i;
804
- for (i = 0; i < index.length; ++i) {
805
- var entry = index[i];
806
- if (entry[0] <= pointer) {
807
- offset = entry[0];
808
- code_point_offset = entry[1];
809
- } else {
810
- break;
811
- }
812
- }
813
- return code_point_offset + pointer - offset;
814
- }
815
-
816
- /**
817
- * @param {number} code_point The |code point| to locate in the gb18030 index.
818
- * @return {number} The first pointer corresponding to |code point| in the
819
- * gb18030 index.
820
- */
821
- function indexGB18030PointerFor(code_point) {
822
- var /** @type {number} */ offset = 0,
823
- /** @type {number} */ pointer_offset = 0,
824
- /** @type {Array.<Array.<number>>} */ index = indexes['gb18030'];
825
- var i;
826
- for (i = 0; i < index.length; ++i) {
827
- var entry = index[i];
828
- if (entry[1] <= code_point) {
829
- offset = entry[1];
830
- pointer_offset = entry[0];
831
- } else {
832
- break;
833
- }
834
- }
835
- return pointer_offset + code_point - offset;
836
- }
837
-
838
- //
839
- // 7. The encoding
840
- //
841
-
842
- // 7.1 utf-8
843
-
844
- /**
845
- * @constructor
846
- * @param {{fatal: boolean}} options
847
- */
848
- function UTF8Decoder(options) {
849
- var fatal = options.fatal;
850
- var /** @type {number} */ utf8_code_point = 0,
851
- /** @type {number} */ utf8_bytes_needed = 0,
852
- /** @type {number} */ utf8_bytes_seen = 0,
853
- /** @type {number} */ utf8_lower_boundary = 0;
854
-
855
- /**
856
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
857
- * @return {?number} The next code point decoded, or null if not enough
858
- * data exists in the input stream to decode a complete code point.
859
- */
860
- this.decode = function(byte_pointer) {
861
- var bite = byte_pointer.get();
862
- if (bite === EOF_byte) {
863
- if (utf8_bytes_needed !== 0) {
864
- return decoderError(fatal);
865
- }
866
- return EOF_code_point;
867
- }
868
- byte_pointer.offset(1);
869
-
870
- if (utf8_bytes_needed === 0) {
871
- if (inRange(bite, 0x00, 0x7F)) {
872
- return bite;
873
- }
874
- if (inRange(bite, 0xC2, 0xDF)) {
875
- utf8_bytes_needed = 1;
876
- utf8_lower_boundary = 0x80;
877
- utf8_code_point = bite - 0xC0;
878
- } else if (inRange(bite, 0xE0, 0xEF)) {
879
- utf8_bytes_needed = 2;
880
- utf8_lower_boundary = 0x800;
881
- utf8_code_point = bite - 0xE0;
882
- } else if (inRange(bite, 0xF0, 0xF4)) {
883
- utf8_bytes_needed = 3;
884
- utf8_lower_boundary = 0x10000;
885
- utf8_code_point = bite - 0xF0;
886
- } else {
887
- return decoderError(fatal);
888
- }
889
- utf8_code_point = utf8_code_point * Math.pow(64, utf8_bytes_needed);
890
- return null;
891
- }
892
- if (!inRange(bite, 0x80, 0xBF)) {
893
- utf8_code_point = 0;
894
- utf8_bytes_needed = 0;
895
- utf8_bytes_seen = 0;
896
- utf8_lower_boundary = 0;
897
- byte_pointer.offset(-1);
898
- return decoderError(fatal);
899
- }
900
- utf8_bytes_seen += 1;
901
- utf8_code_point = utf8_code_point + (bite - 0x80) *
902
- Math.pow(64, utf8_bytes_needed - utf8_bytes_seen);
903
- if (utf8_bytes_seen !== utf8_bytes_needed) {
904
- return null;
905
- }
906
- var code_point = utf8_code_point;
907
- var lower_boundary = utf8_lower_boundary;
908
- utf8_code_point = 0;
909
- utf8_bytes_needed = 0;
910
- utf8_bytes_seen = 0;
911
- utf8_lower_boundary = 0;
912
- if (inRange(code_point, lower_boundary, 0x10FFFF) &&
913
- !inRange(code_point, 0xD800, 0xDFFF)) {
914
- return code_point;
915
- }
916
- return decoderError(fatal);
917
- };
918
- }
919
-
920
- /**
921
- * @constructor
922
- * @param {{fatal: boolean}} options
923
- */
924
- function UTF8Encoder(options) {
925
- var fatal = options.fatal;
926
- /**
927
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
928
- * @param {CodePointInputStream} code_point_pointer Input stream.
929
- * @return {number} The last byte emitted.
930
- */
931
- this.encode = function(output_byte_stream, code_point_pointer) {
932
- var code_point = code_point_pointer.get();
933
- if (code_point === EOF_code_point) {
934
- return EOF_byte;
935
- }
936
- code_point_pointer.offset(1);
937
- if (inRange(code_point, 0xD800, 0xDFFF)) {
938
- return encoderError(code_point);
939
- }
940
- if (inRange(code_point, 0x0000, 0x007f)) {
941
- return output_byte_stream.emit(code_point);
942
- }
943
- var count, offset;
944
- if (inRange(code_point, 0x0080, 0x07FF)) {
945
- count = 1;
946
- offset = 0xC0;
947
- } else if (inRange(code_point, 0x0800, 0xFFFF)) {
948
- count = 2;
949
- offset = 0xE0;
950
- } else if (inRange(code_point, 0x10000, 0x10FFFF)) {
951
- count = 3;
952
- offset = 0xF0;
953
- }
954
- var result = output_byte_stream.emit(
955
- div(code_point, Math.pow(64, count)) + offset);
956
- while (count > 0) {
957
- var temp = div(code_point, Math.pow(64, count - 1));
958
- result = output_byte_stream.emit(0x80 + (temp % 64));
959
- count -= 1;
960
- }
961
- return result;
962
- };
963
- }
964
-
965
- name_to_encoding['utf-8'].getEncoder = function(options) {
966
- return new UTF8Encoder(options);
967
- };
968
- name_to_encoding['utf-8'].getDecoder = function(options) {
969
- return new UTF8Decoder(options);
970
- };
971
-
972
- //
973
- // 8. Legacy single-byte encodings
974
- //
975
-
976
- /**
977
- * @constructor
978
- * @param {Array.<number>} index The encoding index.
979
- * @param {{fatal: boolean}} options
980
- */
981
- function SingleByteDecoder(index, options) {
982
- var fatal = options.fatal;
983
- /**
984
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
985
- * @return {?number} The next code point decoded, or null if not enough
986
- * data exists in the input stream to decode a complete code point.
987
- */
988
- this.decode = function(byte_pointer) {
989
- var bite = byte_pointer.get();
990
- if (bite === EOF_byte) {
991
- return EOF_code_point;
992
- }
993
- byte_pointer.offset(1);
994
- if (inRange(bite, 0x00, 0x7F)) {
995
- return bite;
996
- }
997
- var code_point = index[bite - 0x80];
998
- if (code_point === null) {
999
- return decoderError(fatal);
1000
- }
1001
- return code_point;
1002
- };
1003
- }
1004
-
1005
- /**
1006
- * @constructor
1007
- * @param {Array.<?number>} index The encoding index.
1008
- * @param {{fatal: boolean}} options
1009
- */
1010
- function SingleByteEncoder(index, options) {
1011
- var fatal = options.fatal;
1012
- /**
1013
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
1014
- * @param {CodePointInputStream} code_point_pointer Input stream.
1015
- * @return {number} The last byte emitted.
1016
- */
1017
- this.encode = function(output_byte_stream, code_point_pointer) {
1018
- var code_point = code_point_pointer.get();
1019
- if (code_point === EOF_code_point) {
1020
- return EOF_byte;
1021
- }
1022
- code_point_pointer.offset(1);
1023
- if (inRange(code_point, 0x0000, 0x007F)) {
1024
- return output_byte_stream.emit(code_point);
1025
- }
1026
- var pointer = indexPointerFor(code_point, index);
1027
- if (pointer === null) {
1028
- encoderError(code_point);
1029
- }
1030
- return output_byte_stream.emit(pointer + 0x80);
1031
- };
1032
- }
1033
-
1034
- (function() {
1035
- encodings.forEach(function(category) {
1036
- if (category.heading !== 'Legacy single-byte encodings')
1037
- return;
1038
- category.encodings.forEach(function(encoding) {
1039
- var index = indexes[encoding.name];
1040
- encoding.getDecoder = function(options) {
1041
- return new SingleByteDecoder(index, options);
1042
- };
1043
- encoding.getEncoder = function(options) {
1044
- return new SingleByteEncoder(index, options);
1045
- };
1046
- });
1047
- });
1048
- }());
1049
-
1050
- //
1051
- // 9. Legacy multi-byte Chinese (simplified) encodings
1052
- //
1053
-
1054
- // 9.1 gbk
1055
-
1056
- /**
1057
- * @constructor
1058
- * @param {boolean} gb18030 True if decoding gb18030, false otherwise.
1059
- * @param {{fatal: boolean}} options
1060
- */
1061
- function GBKDecoder(gb18030, options) {
1062
- var fatal = options.fatal;
1063
- var /** @type {number} */ gbk_first = 0x00,
1064
- /** @type {number} */ gbk_second = 0x00,
1065
- /** @type {number} */ gbk_third = 0x00;
1066
- /**
1067
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
1068
- * @return {?number} The next code point decoded, or null if not enough
1069
- * data exists in the input stream to decode a complete code point.
1070
- */
1071
- this.decode = function(byte_pointer) {
1072
- var bite = byte_pointer.get();
1073
- if (bite === EOF_byte && gbk_first === 0x00 &&
1074
- gbk_second === 0x00 && gbk_third === 0x00) {
1075
- return EOF_code_point;
1076
- }
1077
- if (bite === EOF_byte &&
1078
- (gbk_first !== 0x00 || gbk_second !== 0x00 || gbk_third !== 0x00)) {
1079
- gbk_first = 0x00;
1080
- gbk_second = 0x00;
1081
- gbk_third = 0x00;
1082
- decoderError(fatal);
1083
- }
1084
- byte_pointer.offset(1);
1085
- var code_point;
1086
- if (gbk_third !== 0x00) {
1087
- code_point = null;
1088
- if (inRange(bite, 0x30, 0x39)) {
1089
- code_point = indexGB18030CodePointFor(
1090
- (((gbk_first - 0x81) * 10 + (gbk_second - 0x30)) * 126 +
1091
- (gbk_third - 0x81)) * 10 + bite - 0x30);
1092
- }
1093
- gbk_first = 0x00;
1094
- gbk_second = 0x00;
1095
- gbk_third = 0x00;
1096
- if (code_point === null) {
1097
- byte_pointer.offset(-3);
1098
- return decoderError(fatal);
1099
- }
1100
- return code_point;
1101
- }
1102
- if (gbk_second !== 0x00) {
1103
- if (inRange(bite, 0x81, 0xFE)) {
1104
- gbk_third = bite;
1105
- return null;
1106
- }
1107
- byte_pointer.offset(-2);
1108
- gbk_first = 0x00;
1109
- gbk_second = 0x00;
1110
- return decoderError(fatal);
1111
- }
1112
- if (gbk_first !== 0x00) {
1113
- if (inRange(bite, 0x30, 0x39) && gb18030) {
1114
- gbk_second = bite;
1115
- return null;
1116
- }
1117
- var lead = gbk_first;
1118
- var pointer = null;
1119
- gbk_first = 0x00;
1120
- var offset = bite < 0x7F ? 0x40 : 0x41;
1121
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE)) {
1122
- pointer = (lead - 0x81) * 190 + (bite - offset);
1123
- }
1124
- code_point = pointer === null ? null :
1125
- indexCodePointFor(pointer, indexes['gbk']);
1126
- if (pointer === null) {
1127
- byte_pointer.offset(-1);
1128
- }
1129
- if (code_point === null) {
1130
- return decoderError(fatal);
1131
- }
1132
- return code_point;
1133
- }
1134
- if (inRange(bite, 0x00, 0x7F)) {
1135
- return bite;
1136
- }
1137
- if (bite === 0x80) {
1138
- return 0x20AC;
1139
- }
1140
- if (inRange(bite, 0x81, 0xFE)) {
1141
- gbk_first = bite;
1142
- return null;
1143
- }
1144
- return decoderError(fatal);
1145
- };
1146
- }
1147
-
1148
- /**
1149
- * @constructor
1150
- * @param {boolean} gb18030 True if decoding gb18030, false otherwise.
1151
- * @param {{fatal: boolean}} options
1152
- */
1153
- function GBKEncoder(gb18030, options) {
1154
- var fatal = options.fatal;
1155
- /**
1156
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
1157
- * @param {CodePointInputStream} code_point_pointer Input stream.
1158
- * @return {number} The last byte emitted.
1159
- */
1160
- this.encode = function(output_byte_stream, code_point_pointer) {
1161
- var code_point = code_point_pointer.get();
1162
- if (code_point === EOF_code_point) {
1163
- return EOF_byte;
1164
- }
1165
- code_point_pointer.offset(1);
1166
- if (inRange(code_point, 0x0000, 0x007F)) {
1167
- return output_byte_stream.emit(code_point);
1168
- }
1169
- var pointer = indexPointerFor(code_point, indexes['gbk']);
1170
- if (pointer !== null) {
1171
- var lead = div(pointer, 190) + 0x81;
1172
- var trail = pointer % 190;
1173
- var offset = trail < 0x3F ? 0x40 : 0x41;
1174
- return output_byte_stream.emit(lead, trail + offset);
1175
- }
1176
- if (pointer === null && !gb18030) {
1177
- return encoderError(code_point);
1178
- }
1179
- pointer = indexGB18030PointerFor(code_point);
1180
- var byte1 = div(div(div(pointer, 10), 126), 10);
1181
- pointer = pointer - byte1 * 10 * 126 * 10;
1182
- var byte2 = div(div(pointer, 10), 126);
1183
- pointer = pointer - byte2 * 10 * 126;
1184
- var byte3 = div(pointer, 10);
1185
- var byte4 = pointer - byte3 * 10;
1186
- return output_byte_stream.emit(byte1 + 0x81,
1187
- byte2 + 0x30,
1188
- byte3 + 0x81,
1189
- byte4 + 0x30);
1190
- };
1191
- }
1192
-
1193
- name_to_encoding['gbk'].getEncoder = function(options) {
1194
- return new GBKEncoder(false, options);
1195
- };
1196
- name_to_encoding['gbk'].getDecoder = function(options) {
1197
- return new GBKDecoder(false, options);
1198
- };
1199
-
1200
- // 9.2 gb18030
1201
- name_to_encoding['gb18030'].getEncoder = function(options) {
1202
- return new GBKEncoder(true, options);
1203
- };
1204
- name_to_encoding['gb18030'].getDecoder = function(options) {
1205
- return new GBKDecoder(true, options);
1206
- };
1207
-
1208
- // 9.3 hz-gb-2312
1209
-
1210
- /**
1211
- * @constructor
1212
- * @param {{fatal: boolean}} options
1213
- */
1214
- function HZGB2312Decoder(options) {
1215
- var fatal = options.fatal;
1216
- var /** @type {boolean} */ hzgb2312 = false,
1217
- /** @type {number} */ hzgb2312_lead = 0x00;
1218
- /**
1219
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
1220
- * @return {?number} The next code point decoded, or null if not enough
1221
- * data exists in the input stream to decode a complete code point.
1222
- */
1223
- this.decode = function(byte_pointer) {
1224
- var bite = byte_pointer.get();
1225
- if (bite === EOF_byte && hzgb2312_lead === 0x00) {
1226
- return EOF_code_point;
1227
- }
1228
- if (bite === EOF_byte && hzgb2312_lead !== 0x00) {
1229
- hzgb2312_lead = 0x00;
1230
- return decoderError(fatal);
1231
- }
1232
- byte_pointer.offset(1);
1233
- if (hzgb2312_lead === 0x7E) {
1234
- hzgb2312_lead = 0x00;
1235
- if (bite === 0x7B) {
1236
- hzgb2312 = true;
1237
- return null;
1238
- }
1239
- if (bite === 0x7D) {
1240
- hzgb2312 = false;
1241
- return null;
1242
- }
1243
- if (bite === 0x7E) {
1244
- return 0x007E;
1245
- }
1246
- if (bite === 0x0A) {
1247
- return null;
1248
- }
1249
- byte_pointer.offset(-1);
1250
- return decoderError(fatal);
1251
- }
1252
- if (hzgb2312_lead !== 0x00) {
1253
- var lead = hzgb2312_lead;
1254
- hzgb2312_lead = 0x00;
1255
- var code_point = null;
1256
- if (inRange(bite, 0x21, 0x7E)) {
1257
- code_point = indexCodePointFor((lead - 1) * 190 +
1258
- (bite + 0x3F), indexes['gbk']);
1259
- }
1260
- if (bite === 0x0A) {
1261
- hzgb2312 = false;
1262
- }
1263
- if (code_point === null) {
1264
- return decoderError(fatal);
1265
- }
1266
- return code_point;
1267
- }
1268
- if (bite === 0x7E) {
1269
- hzgb2312_lead = 0x7E;
1270
- return null;
1271
- }
1272
- if (hzgb2312) {
1273
- if (inRange(bite, 0x20, 0x7F)) {
1274
- hzgb2312_lead = bite;
1275
- return null;
1276
- }
1277
- if (bite === 0x0A) {
1278
- hzgb2312 = false;
1279
- }
1280
- return decoderError(fatal);
1281
- }
1282
- if (inRange(bite, 0x00, 0x7F)) {
1283
- return bite;
1284
- }
1285
- return decoderError(fatal);
1286
- };
1287
- }
1288
-
1289
- /**
1290
- * @constructor
1291
- * @param {{fatal: boolean}} options
1292
- */
1293
- function HZGB2312Encoder(options) {
1294
- var fatal = options.fatal;
1295
- var hzgb2312 = false;
1296
- /**
1297
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
1298
- * @param {CodePointInputStream} code_point_pointer Input stream.
1299
- * @return {number} The last byte emitted.
1300
- */
1301
- this.encode = function(output_byte_stream, code_point_pointer) {
1302
- var code_point = code_point_pointer.get();
1303
- if (code_point === EOF_code_point) {
1304
- return EOF_byte;
1305
- }
1306
- code_point_pointer.offset(1);
1307
- if (inRange(code_point, 0x0000, 0x007F) && hzgb2312) {
1308
- code_point_pointer.offset(-1);
1309
- hzgb2312 = false;
1310
- return output_byte_stream.emit(0x7E, 0x7D);
1311
- }
1312
- if (code_point === 0x007E) {
1313
- return output_byte_stream.emit(0x7E, 0x7E);
1314
- }
1315
- if (inRange(code_point, 0x0000, 0x007F)) {
1316
- return output_byte_stream.emit(code_point);
1317
- }
1318
- if (!hzgb2312) {
1319
- code_point_pointer.offset(-1);
1320
- hzgb2312 = true;
1321
- return output_byte_stream.emit(0x7E, 0x7B);
1322
- }
1323
- var pointer = indexPointerFor(code_point, indexes['gbk']);
1324
- if (pointer === null) {
1325
- return encoderError(code_point);
1326
- }
1327
- var lead = div(pointer, 190) + 1;
1328
- var trail = pointer % 190 - 0x3F;
1329
- if (!inRange(lead, 0x21, 0x7E) || !inRange(trail, 0x21, 0x7E)) {
1330
- return encoderError(code_point);
1331
- }
1332
- return output_byte_stream.emit(lead, trail);
1333
- };
1334
- }
1335
-
1336
- name_to_encoding['hz-gb-2312'].getEncoder = function(options) {
1337
- return new HZGB2312Encoder(options);
1338
- };
1339
- name_to_encoding['hz-gb-2312'].getDecoder = function(options) {
1340
- return new HZGB2312Decoder(options);
1341
- };
1342
-
1343
- //
1344
- // 10. Legacy multi-byte Chinese (traditional) encodings
1345
- //
1346
-
1347
- // 10.1 big5
1348
-
1349
- /**
1350
- * @constructor
1351
- * @param {{fatal: boolean}} options
1352
- */
1353
- function Big5Decoder(options) {
1354
- var fatal = options.fatal;
1355
- var /** @type {number} */ big5_lead = 0x00,
1356
- /** @type {?number} */ big5_pending = null;
1357
-
1358
- /**
1359
- * @param {ByteInputStream} byte_pointer The byte steram to decode.
1360
- * @return {?number} The next code point decoded, or null if not enough
1361
- * data exists in the input stream to decode a complete code point.
1362
- */
1363
- this.decode = function(byte_pointer) {
1364
- // NOTE: Hack to support emitting two code points
1365
- if (big5_pending !== null) {
1366
- var pending = big5_pending;
1367
- big5_pending = null;
1368
- return pending;
1369
- }
1370
- var bite = byte_pointer.get();
1371
- if (bite === EOF_byte && big5_lead === 0x00) {
1372
- return EOF_code_point;
1373
- }
1374
- if (bite === EOF_byte && big5_lead !== 0x00) {
1375
- big5_lead = 0x00;
1376
- return decoderError(fatal);
1377
- }
1378
- byte_pointer.offset(1);
1379
- if (big5_lead !== 0x00) {
1380
- var lead = big5_lead;
1381
- var pointer = null;
1382
- big5_lead = 0x00;
1383
- var offset = bite < 0x7F ? 0x40 : 0x62;
1384
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE)) {
1385
- pointer = (lead - 0x81) * 157 + (bite - offset);
1386
- }
1387
- if (pointer === 1133) {
1388
- big5_pending = 0x0304;
1389
- return 0x00CA;
1390
- }
1391
- if (pointer === 1135) {
1392
- big5_pending = 0x030C;
1393
- return 0x00CA;
1394
- }
1395
- if (pointer === 1164) {
1396
- big5_pending = 0x0304;
1397
- return 0x00EA;
1398
- }
1399
- if (pointer === 1166) {
1400
- big5_pending = 0x030C;
1401
- return 0x00EA;
1402
- }
1403
- var code_point = (pointer === null) ? null :
1404
- indexCodePointFor(pointer, indexes['big5']);
1405
- if (pointer === null) {
1406
- byte_pointer.offset(-1);
1407
- }
1408
- if (code_point === null) {
1409
- return decoderError(fatal);
1410
- }
1411
- return code_point;
1412
- }
1413
- if (inRange(bite, 0x00, 0x7F)) {
1414
- return bite;
1415
- }
1416
- if (inRange(bite, 0x81, 0xFE)) {
1417
- big5_lead = bite;
1418
- return null;
1419
- }
1420
- return decoderError(fatal);
1421
- };
1422
- }
1423
-
1424
- /**
1425
- * @constructor
1426
- * @param {{fatal: boolean}} options
1427
- */
1428
- function Big5Encoder(options) {
1429
- var fatal = options.fatal;
1430
- /**
1431
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
1432
- * @param {CodePointInputStream} code_point_pointer Input stream.
1433
- * @return {number} The last byte emitted.
1434
- */
1435
- this.encode = function(output_byte_stream, code_point_pointer) {
1436
- var code_point = code_point_pointer.get();
1437
- if (code_point === EOF_code_point) {
1438
- return EOF_byte;
1439
- }
1440
- code_point_pointer.offset(1);
1441
- if (inRange(code_point, 0x0000, 0x007F)) {
1442
- return output_byte_stream.emit(code_point);
1443
- }
1444
- var pointer = indexPointerFor(code_point, indexes['big5']);
1445
- if (pointer === null) {
1446
- return encoderError(code_point);
1447
- }
1448
- var lead = div(pointer, 157) + 0x81;
1449
- //if (lead < 0xA1) {
1450
- // return encoderError(code_point);
1451
- //}
1452
- var trail = pointer % 157;
1453
- var offset = trail < 0x3F ? 0x40 : 0x62;
1454
- return output_byte_stream.emit(lead, trail + offset);
1455
- };
1456
- }
1457
-
1458
- name_to_encoding['big5'].getEncoder = function(options) {
1459
- return new Big5Encoder(options);
1460
- };
1461
- name_to_encoding['big5'].getDecoder = function(options) {
1462
- return new Big5Decoder(options);
1463
- };
1464
-
1465
-
1466
- //
1467
- // 11. Legacy multi-byte Japanese encodings
1468
- //
1469
-
1470
- // 11.1 euc.jp
1471
-
1472
- /**
1473
- * @constructor
1474
- * @param {{fatal: boolean}} options
1475
- */
1476
- function EUCJPDecoder(options) {
1477
- var fatal = options.fatal;
1478
- var /** @type {number} */ eucjp_first = 0x00,
1479
- /** @type {number} */ eucjp_second = 0x00;
1480
- /**
1481
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
1482
- * @return {?number} The next code point decoded, or null if not enough
1483
- * data exists in the input stream to decode a complete code point.
1484
- */
1485
- this.decode = function(byte_pointer) {
1486
- var bite = byte_pointer.get();
1487
- if (bite === EOF_byte) {
1488
- if (eucjp_first === 0x00 && eucjp_second === 0x00) {
1489
- return EOF_code_point;
1490
- }
1491
- eucjp_first = 0x00;
1492
- eucjp_second = 0x00;
1493
- return decoderError(fatal);
1494
- }
1495
- byte_pointer.offset(1);
1496
-
1497
- var lead, code_point;
1498
- if (eucjp_second !== 0x00) {
1499
- lead = eucjp_second;
1500
- eucjp_second = 0x00;
1501
- code_point = null;
1502
- if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
1503
- code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
1504
- indexes['jis0212']);
1505
- }
1506
- if (!inRange(bite, 0xA1, 0xFE)) {
1507
- byte_pointer.offset(-1);
1508
- }
1509
- if (code_point === null) {
1510
- return decoderError(fatal);
1511
- }
1512
- return code_point;
1513
- }
1514
- if (eucjp_first === 0x8E && inRange(bite, 0xA1, 0xDF)) {
1515
- eucjp_first = 0x00;
1516
- return 0xFF61 + bite - 0xA1;
1517
- }
1518
- if (eucjp_first === 0x8F && inRange(bite, 0xA1, 0xFE)) {
1519
- eucjp_first = 0x00;
1520
- eucjp_second = bite;
1521
- return null;
1522
- }
1523
- if (eucjp_first !== 0x00) {
1524
- lead = eucjp_first;
1525
- eucjp_first = 0x00;
1526
- code_point = null;
1527
- if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
1528
- code_point = indexCodePointFor((lead - 0xA1) * 94 + bite - 0xA1,
1529
- indexes['jis0208']);
1530
- }
1531
- if (!inRange(bite, 0xA1, 0xFE)) {
1532
- byte_pointer.offset(-1);
1533
- }
1534
- if (code_point === null) {
1535
- return decoderError(fatal);
1536
- }
1537
- return code_point;
1538
- }
1539
- if (inRange(bite, 0x00, 0x7F)) {
1540
- return bite;
1541
- }
1542
- if (bite === 0x8E || bite === 0x8F || (inRange(bite, 0xA1, 0xFE))) {
1543
- eucjp_first = bite;
1544
- return null;
1545
- }
1546
- return decoderError(fatal);
1547
- };
1548
- }
1549
-
1550
- /**
1551
- * @constructor
1552
- * @param {{fatal: boolean}} options
1553
- */
1554
- function EUCJPEncoder(options) {
1555
- var fatal = options.fatal;
1556
- /**
1557
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
1558
- * @param {CodePointInputStream} code_point_pointer Input stream.
1559
- * @return {number} The last byte emitted.
1560
- */
1561
- this.encode = function(output_byte_stream, code_point_pointer) {
1562
- var code_point = code_point_pointer.get();
1563
- if (code_point === EOF_code_point) {
1564
- return EOF_byte;
1565
- }
1566
- code_point_pointer.offset(1);
1567
- if (inRange(code_point, 0x0000, 0x007F)) {
1568
- return output_byte_stream.emit(code_point);
1569
- }
1570
- if (code_point === 0x00A5) {
1571
- return output_byte_stream.emit(0x5C);
1572
- }
1573
- if (code_point === 0x203E) {
1574
- return output_byte_stream.emit(0x7E);
1575
- }
1576
- if (inRange(code_point, 0xFF61, 0xFF9F)) {
1577
- return output_byte_stream.emit(0x8E, code_point - 0xFF61 + 0xA1);
1578
- }
1579
-
1580
- var pointer = indexPointerFor(code_point, indexes['jis0208']);
1581
- if (pointer === null) {
1582
- return encoderError(code_point);
1583
- }
1584
- var lead = div(pointer, 94) + 0xA1;
1585
- var trail = pointer % 94 + 0xA1;
1586
- return output_byte_stream.emit(lead, trail);
1587
- };
1588
- }
1589
-
1590
- name_to_encoding['euc-jp'].getEncoder = function(options) {
1591
- return new EUCJPEncoder(options);
1592
- };
1593
- name_to_encoding['euc-jp'].getDecoder = function(options) {
1594
- return new EUCJPDecoder(options);
1595
- };
1596
-
1597
- // 11.2 iso-2022-jp
1598
-
1599
- /**
1600
- * @constructor
1601
- * @param {{fatal: boolean}} options
1602
- */
1603
- function ISO2022JPDecoder(options) {
1604
- var fatal = options.fatal;
1605
- /** @enum */
1606
- var state = {
1607
- ASCII: 0,
1608
- escape_start: 1,
1609
- escape_middle: 2,
1610
- escape_final: 3,
1611
- lead: 4,
1612
- trail: 5,
1613
- Katakana: 6
1614
- };
1615
- var /** @type {number} */ iso2022jp_state = state.ASCII,
1616
- /** @type {boolean} */ iso2022jp_jis0212 = false,
1617
- /** @type {number} */ iso2022jp_lead = 0x00;
1618
- /**
1619
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
1620
- * @return {?number} The next code point decoded, or null if not enough
1621
- * data exists in the input stream to decode a complete code point.
1622
- */
1623
- this.decode = function(byte_pointer) {
1624
- var bite = byte_pointer.get();
1625
- if (bite !== EOF_byte) {
1626
- byte_pointer.offset(1);
1627
- }
1628
- switch (iso2022jp_state) {
1629
- default:
1630
- case state.ASCII:
1631
- if (bite === 0x1B) {
1632
- iso2022jp_state = state.escape_start;
1633
- return null;
1634
- }
1635
- if (inRange(bite, 0x00, 0x7F)) {
1636
- return bite;
1637
- }
1638
- if (bite === EOF_byte) {
1639
- return EOF_code_point;
1640
- }
1641
- return decoderError(fatal);
1642
-
1643
- case state.escape_start:
1644
- if (bite === 0x24 || bite === 0x28) {
1645
- iso2022jp_lead = bite;
1646
- iso2022jp_state = state.escape_middle;
1647
- return null;
1648
- }
1649
- if (bite !== EOF_byte) {
1650
- byte_pointer.offset(-1);
1651
- }
1652
- iso2022jp_state = state.ASCII;
1653
- return decoderError(fatal);
1654
-
1655
- case state.escape_middle:
1656
- var lead = iso2022jp_lead;
1657
- iso2022jp_lead = 0x00;
1658
- if (lead === 0x24 && (bite === 0x40 || bite === 0x42)) {
1659
- iso2022jp_jis0212 = false;
1660
- iso2022jp_state = state.lead;
1661
- return null;
1662
- }
1663
- if (lead === 0x24 && bite === 0x28) {
1664
- iso2022jp_state = state.escape_final;
1665
- return null;
1666
- }
1667
- if (lead === 0x28 && (bite === 0x42 || bite === 0x4A)) {
1668
- iso2022jp_state = state.ASCII;
1669
- return null;
1670
- }
1671
- if (lead === 0x28 && bite === 0x49) {
1672
- iso2022jp_state = state.Katakana;
1673
- return null;
1674
- }
1675
- if (bite === EOF_byte) {
1676
- byte_pointer.offset(-1);
1677
- } else {
1678
- byte_pointer.offset(-2);
1679
- }
1680
- iso2022jp_state = state.ASCII;
1681
- return decoderError(fatal);
1682
-
1683
- case state.escape_final:
1684
- if (bite === 0x44) {
1685
- iso2022jp_jis0212 = true;
1686
- iso2022jp_state = state.lead;
1687
- return null;
1688
- }
1689
- if (bite === EOF_byte) {
1690
- byte_pointer.offset(-2);
1691
- } else {
1692
- byte_pointer.offset(-3);
1693
- }
1694
- iso2022jp_state = state.ASCII;
1695
- return decoderError(fatal);
1696
-
1697
- case state.lead:
1698
- if (bite === 0x0A) {
1699
- iso2022jp_state = state.ASCII;
1700
- return decoderError(fatal, 0x000A);
1701
- }
1702
- if (bite === 0x1B) {
1703
- iso2022jp_state = state.escape_start;
1704
- return null;
1705
- }
1706
- if (bite === EOF_byte) {
1707
- return EOF_code_point;
1708
- }
1709
- iso2022jp_lead = bite;
1710
- iso2022jp_state = state.trail;
1711
- return null;
1712
-
1713
- case state.trail:
1714
- iso2022jp_state = state.lead;
1715
- if (bite === EOF_byte) {
1716
- return decoderError(fatal);
1717
- }
1718
- var code_point = null;
1719
- var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
1720
- if (inRange(iso2022jp_lead, 0x21, 0x7E) &&
1721
- inRange(bite, 0x21, 0x7E)) {
1722
- code_point = (iso2022jp_jis0212 === false) ?
1723
- indexCodePointFor(pointer, indexes['jis0208']) :
1724
- indexCodePointFor(pointer, indexes['jis0212']);
1725
- }
1726
- if (code_point === null) {
1727
- return decoderError(fatal);
1728
- }
1729
- return code_point;
1730
-
1731
- case state.Katakana:
1732
- if (bite === 0x1B) {
1733
- iso2022jp_state = state.escape_start;
1734
- return null;
1735
- }
1736
- if (inRange(bite, 0x21, 0x5F)) {
1737
- return 0xFF61 + bite - 0x21;
1738
- }
1739
- if (bite === EOF_byte) {
1740
- return EOF_code_point;
1741
- }
1742
- return decoderError(fatal);
1743
- }
1744
- };
1745
- }
1746
-
1747
- /**
1748
- * @constructor
1749
- * @param {{fatal: boolean}} options
1750
- */
1751
- function ISO2022JPEncoder(options) {
1752
- var fatal = options.fatal;
1753
- /** @enum */
1754
- var state = {
1755
- ASCII: 0,
1756
- lead: 1,
1757
- Katakana: 2
1758
- };
1759
- var /** @type {number} */ iso2022jp_state = state.ASCII;
1760
- /**
1761
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
1762
- * @param {CodePointInputStream} code_point_pointer Input stream.
1763
- * @return {number} The last byte emitted.
1764
- */
1765
- this.encode = function(output_byte_stream, code_point_pointer) {
1766
- var code_point = code_point_pointer.get();
1767
- if (code_point === EOF_code_point) {
1768
- return EOF_byte;
1769
- }
1770
- code_point_pointer.offset(1);
1771
- if ((inRange(code_point, 0x0000, 0x007F) ||
1772
- code_point === 0x00A5 || code_point === 0x203E) &&
1773
- iso2022jp_state !== state.ASCII) {
1774
- code_point_pointer.offset(-1);
1775
- iso2022jp_state = state.ASCII;
1776
- return output_byte_stream.emit(0x1B, 0x28, 0x42);
1777
- }
1778
- if (inRange(code_point, 0x0000, 0x007F)) {
1779
- return output_byte_stream.emit(code_point);
1780
- }
1781
- if (code_point === 0x00A5) {
1782
- return output_byte_stream.emit(0x5C);
1783
- }
1784
- if (code_point === 0x203E) {
1785
- return output_byte_stream.emit(0x7E);
1786
- }
1787
- if (inRange(code_point, 0xFF61, 0xFF9F) &&
1788
- iso2022jp_state !== state.Katakana) {
1789
- code_point_pointer.offset(-1);
1790
- iso2022jp_state = state.Katakana;
1791
- return output_byte_stream.emit(0x1B, 0x28, 0x49);
1792
- }
1793
- if (inRange(code_point, 0xFF61, 0xFF9F)) {
1794
- return output_byte_stream.emit(code_point - 0xFF61 - 0x21);
1795
- }
1796
- if (iso2022jp_state !== state.lead) {
1797
- code_point_pointer.offset(-1);
1798
- iso2022jp_state = state.lead;
1799
- return output_byte_stream.emit(0x1B, 0x24, 0x42);
1800
- }
1801
- var pointer = indexPointerFor(code_point, indexes['jis0208']);
1802
- if (pointer === null) {
1803
- return encoderError(code_point);
1804
- }
1805
- var lead = div(pointer, 94) + 0x21;
1806
- var trail = pointer % 94 + 0x21;
1807
- return output_byte_stream.emit(lead, trail);
1808
- };
1809
- }
1810
-
1811
- name_to_encoding['iso-2022-jp'].getEncoder = function(options) {
1812
- return new ISO2022JPEncoder(options);
1813
- };
1814
- name_to_encoding['iso-2022-jp'].getDecoder = function(options) {
1815
- return new ISO2022JPDecoder(options);
1816
- };
1817
-
1818
- // 11.3 shift_jis
1819
-
1820
- /**
1821
- * @constructor
1822
- * @param {{fatal: boolean}} options
1823
- */
1824
- function ShiftJISDecoder(options) {
1825
- var fatal = options.fatal;
1826
- var /** @type {number} */ shiftjis_lead = 0x00;
1827
- /**
1828
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
1829
- * @return {?number} The next code point decoded, or null if not enough
1830
- * data exists in the input stream to decode a complete code point.
1831
- */
1832
- this.decode = function(byte_pointer) {
1833
- var bite = byte_pointer.get();
1834
- if (bite === EOF_byte && shiftjis_lead === 0x00) {
1835
- return EOF_code_point;
1836
- }
1837
- if (bite === EOF_byte && shiftjis_lead !== 0x00) {
1838
- shiftjis_lead = 0x00;
1839
- return decoderError(fatal);
1840
- }
1841
- byte_pointer.offset(1);
1842
- if (shiftjis_lead !== 0x00) {
1843
- var lead = shiftjis_lead;
1844
- shiftjis_lead = 0x00;
1845
- if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC)) {
1846
- var offset = (bite < 0x7F) ? 0x40 : 0x41;
1847
- var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
1848
- var code_point = indexCodePointFor((lead - lead_offset) * 188 +
1849
- bite - offset, indexes['jis0208']);
1850
- if (code_point === null) {
1851
- return decoderError(fatal);
1852
- }
1853
- return code_point;
1854
- }
1855
- byte_pointer.offset(-1);
1856
- return decoderError(fatal);
1857
- }
1858
- if (inRange(bite, 0x00, 0x80)) {
1859
- return bite;
1860
- }
1861
- if (inRange(bite, 0xA1, 0xDF)) {
1862
- return 0xFF61 + bite - 0xA1;
1863
- }
1864
- if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
1865
- shiftjis_lead = bite;
1866
- return null;
1867
- }
1868
- return decoderError(fatal);
1869
- };
1870
- }
1871
-
1872
- /**
1873
- * @constructor
1874
- * @param {{fatal: boolean}} options
1875
- */
1876
- function ShiftJISEncoder(options) {
1877
- var fatal = options.fatal;
1878
- /**
1879
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
1880
- * @param {CodePointInputStream} code_point_pointer Input stream.
1881
- * @return {number} The last byte emitted.
1882
- */
1883
- this.encode = function(output_byte_stream, code_point_pointer) {
1884
- var code_point = code_point_pointer.get();
1885
- if (code_point === EOF_code_point) {
1886
- return EOF_byte;
1887
- }
1888
- code_point_pointer.offset(1);
1889
- if (inRange(code_point, 0x0000, 0x0080)) {
1890
- return output_byte_stream.emit(code_point);
1891
- }
1892
- if (code_point === 0x00A5) {
1893
- return output_byte_stream.emit(0x5C);
1894
- }
1895
- if (code_point === 0x203E) {
1896
- return output_byte_stream.emit(0x7E);
1897
- }
1898
- if (inRange(code_point, 0xFF61, 0xFF9F)) {
1899
- return output_byte_stream.emit(code_point - 0xFF61 + 0xA1);
1900
- }
1901
- var pointer = indexPointerFor(code_point, indexes['jis0208']);
1902
- if (pointer === null) {
1903
- return encoderError(code_point);
1904
- }
1905
- var lead = div(pointer, 188);
1906
- var lead_offset = lead < 0x1F ? 0x81 : 0xC1;
1907
- var trail = pointer % 188;
1908
- var offset = trail < 0x3F ? 0x40 : 0x41;
1909
- return output_byte_stream.emit(lead + lead_offset, trail + offset);
1910
- };
1911
- }
1912
-
1913
- name_to_encoding['shift_jis'].getEncoder = function(options) {
1914
- return new ShiftJISEncoder(options);
1915
- };
1916
- name_to_encoding['shift_jis'].getDecoder = function(options) {
1917
- return new ShiftJISDecoder(options);
1918
- };
1919
-
1920
- //
1921
- // 12. Legacy multi-byte Korean encodings
1922
- //
1923
-
1924
- // 12.1 euc-kr
1925
-
1926
- /**
1927
- * @constructor
1928
- * @param {{fatal: boolean}} options
1929
- */
1930
- function EUCKRDecoder(options) {
1931
- var fatal = options.fatal;
1932
- var /** @type {number} */ euckr_lead = 0x00;
1933
- /**
1934
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
1935
- * @return {?number} The next code point decoded, or null if not enough
1936
- * data exists in the input stream to decode a complete code point.
1937
- */
1938
- this.decode = function(byte_pointer) {
1939
- var bite = byte_pointer.get();
1940
- if (bite === EOF_byte && euckr_lead === 0) {
1941
- return EOF_code_point;
1942
- }
1943
- if (bite === EOF_byte && euckr_lead !== 0) {
1944
- euckr_lead = 0x00;
1945
- return decoderError(fatal);
1946
- }
1947
- byte_pointer.offset(1);
1948
- if (euckr_lead !== 0x00) {
1949
- var lead = euckr_lead;
1950
- var pointer = null;
1951
- euckr_lead = 0x00;
1952
-
1953
- if (inRange(lead, 0x81, 0xC6)) {
1954
- var temp = (26 + 26 + 126) * (lead - 0x81);
1955
- if (inRange(bite, 0x41, 0x5A)) {
1956
- pointer = temp + bite - 0x41;
1957
- } else if (inRange(bite, 0x61, 0x7A)) {
1958
- pointer = temp + 26 + bite - 0x61;
1959
- } else if (inRange(bite, 0x81, 0xFE)) {
1960
- pointer = temp + 26 + 26 + bite - 0x81;
1961
- }
1962
- }
1963
-
1964
- if (inRange(lead, 0xC7, 0xFD) && inRange(bite, 0xA1, 0xFE)) {
1965
- pointer = (26 + 26 + 126) * (0xC7 - 0x81) + (lead - 0xC7) * 94 +
1966
- (bite - 0xA1);
1967
- }
1968
-
1969
- var code_point = (pointer === null) ? null :
1970
- indexCodePointFor(pointer, indexes['euc-kr']);
1971
- if (pointer === null) {
1972
- byte_pointer.offset(-1);
1973
- }
1974
- if (code_point === null) {
1975
- return decoderError(fatal);
1976
- }
1977
- return code_point;
1978
- }
1979
-
1980
- if (inRange(bite, 0x00, 0x7F)) {
1981
- return bite;
1982
- }
1983
-
1984
- if (inRange(bite, 0x81, 0xFD)) {
1985
- euckr_lead = bite;
1986
- return null;
1987
- }
1988
-
1989
- return decoderError(fatal);
1990
- };
1991
- }
1992
-
1993
- /**
1994
- * @constructor
1995
- * @param {{fatal: boolean}} options
1996
- */
1997
- function EUCKREncoder(options) {
1998
- var fatal = options.fatal;
1999
- /**
2000
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
2001
- * @param {CodePointInputStream} code_point_pointer Input stream.
2002
- * @return {number} The last byte emitted.
2003
- */
2004
- this.encode = function(output_byte_stream, code_point_pointer) {
2005
- var code_point = code_point_pointer.get();
2006
- if (code_point === EOF_code_point) {
2007
- return EOF_byte;
2008
- }
2009
- code_point_pointer.offset(1);
2010
- if (inRange(code_point, 0x0000, 0x007F)) {
2011
- return output_byte_stream.emit(code_point);
2012
- }
2013
- var pointer = indexPointerFor(code_point, indexes['euc-kr']);
2014
- if (pointer === null) {
2015
- return encoderError(code_point);
2016
- }
2017
- var lead, trail;
2018
- if (pointer < ((26 + 26 + 126) * (0xC7 - 0x81))) {
2019
- lead = div(pointer, (26 + 26 + 126)) + 0x81;
2020
- trail = pointer % (26 + 26 + 126);
2021
- var offset = trail < 26 ? 0x41 : trail < 26 + 26 ? 0x47 : 0x4D;
2022
- return output_byte_stream.emit(lead, trail + offset);
2023
- }
2024
- pointer = pointer - (26 + 26 + 126) * (0xC7 - 0x81);
2025
- lead = div(pointer, 94) + 0xC7;
2026
- trail = pointer % 94 + 0xA1;
2027
- return output_byte_stream.emit(lead, trail);
2028
- };
2029
- }
2030
-
2031
- name_to_encoding['euc-kr'].getEncoder = function(options) {
2032
- return new EUCKREncoder(options);
2033
- };
2034
- name_to_encoding['euc-kr'].getDecoder = function(options) {
2035
- return new EUCKRDecoder(options);
2036
- };
2037
-
2038
-
2039
- //
2040
- // 13. Legacy utf-16 encodings
2041
- //
2042
-
2043
- // 13.1 utf-16
2044
-
2045
- /**
2046
- * @constructor
2047
- * @param {boolean} utf16_be True if big-endian, false if little-endian.
2048
- * @param {{fatal: boolean}} options
2049
- */
2050
- function UTF16Decoder(utf16_be, options) {
2051
- var fatal = options.fatal;
2052
- var /** @type {?number} */ utf16_lead_byte = null,
2053
- /** @type {?number} */ utf16_lead_surrogate = null;
2054
- /**
2055
- * @param {ByteInputStream} byte_pointer The byte stream to decode.
2056
- * @return {?number} The next code point decoded, or null if not enough
2057
- * data exists in the input stream to decode a complete code point.
2058
- */
2059
- this.decode = function(byte_pointer) {
2060
- var bite = byte_pointer.get();
2061
- if (bite === EOF_byte && utf16_lead_byte === null &&
2062
- utf16_lead_surrogate === null) {
2063
- return EOF_code_point;
2064
- }
2065
- if (bite === EOF_byte && (utf16_lead_byte !== null ||
2066
- utf16_lead_surrogate !== null)) {
2067
- return decoderError(fatal);
2068
- }
2069
- byte_pointer.offset(1);
2070
- if (utf16_lead_byte === null) {
2071
- utf16_lead_byte = bite;
2072
- return null;
2073
- }
2074
- var code_point;
2075
- if (utf16_be) {
2076
- code_point = (utf16_lead_byte << 8) + bite;
2077
- } else {
2078
- code_point = (bite << 8) + utf16_lead_byte;
2079
- }
2080
- utf16_lead_byte = null;
2081
- if (utf16_lead_surrogate !== null) {
2082
- var lead_surrogate = utf16_lead_surrogate;
2083
- utf16_lead_surrogate = null;
2084
- if (inRange(code_point, 0xDC00, 0xDFFF)) {
2085
- return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
2086
- (code_point - 0xDC00);
2087
- }
2088
- byte_pointer.offset(-2);
2089
- return decoderError(fatal);
2090
- }
2091
- if (inRange(code_point, 0xD800, 0xDBFF)) {
2092
- utf16_lead_surrogate = code_point;
2093
- return null;
2094
- }
2095
- if (inRange(code_point, 0xDC00, 0xDFFF)) {
2096
- return decoderError(fatal);
2097
- }
2098
- return code_point;
2099
- };
2100
- }
2101
-
2102
- /**
2103
- * @constructor
2104
- * @param {boolean} utf16_be True if big-endian, false if little-endian.
2105
- * @param {{fatal: boolean}} options
2106
- */
2107
- function UTF16Encoder(utf16_be, options) {
2108
- var fatal = options.fatal;
2109
- /**
2110
- * @param {ByteOutputStream} output_byte_stream Output byte stream.
2111
- * @param {CodePointInputStream} code_point_pointer Input stream.
2112
- * @return {number} The last byte emitted.
2113
- */
2114
- this.encode = function(output_byte_stream, code_point_pointer) {
2115
- function convert_to_bytes(code_unit) {
2116
- var byte1 = code_unit >> 8;
2117
- var byte2 = code_unit & 0x00FF;
2118
- if (utf16_be) {
2119
- return output_byte_stream.emit(byte1, byte2);
2120
- }
2121
- return output_byte_stream.emit(byte2, byte1);
2122
- }
2123
- var code_point = code_point_pointer.get();
2124
- if (code_point === EOF_code_point) {
2125
- return EOF_byte;
2126
- }
2127
- code_point_pointer.offset(1);
2128
- if (inRange(code_point, 0xD800, 0xDFFF)) {
2129
- encoderError(code_point);
2130
- }
2131
- if (code_point <= 0xFFFF) {
2132
- return convert_to_bytes(code_point);
2133
- }
2134
- var lead = div((code_point - 0x10000), 0x400) + 0xD800;
2135
- var trail = ((code_point - 0x10000) % 0x400) + 0xDC00;
2136
- convert_to_bytes(lead);
2137
- return convert_to_bytes(trail);
2138
- };
2139
- }
2140
-
2141
- name_to_encoding['utf-16le'].getEncoder = function(options) {
2142
- return new UTF16Encoder(false, options);
2143
- };
2144
- name_to_encoding['utf-16le'].getDecoder = function(options) {
2145
- return new UTF16Decoder(false, options);
2146
- };
2147
-
2148
- // 13.2 utf-16be
2149
- name_to_encoding['utf-16be'].getEncoder = function(options) {
2150
- return new UTF16Encoder(true, options);
2151
- };
2152
- name_to_encoding['utf-16be'].getDecoder = function(options) {
2153
- return new UTF16Decoder(true, options);
2154
- };
2155
-
2156
-
2157
- // NOTE: currently unused
2158
- /**
2159
- * @param {string} label The encoding label.
2160
- * @param {ByteInputStream} input_stream The byte stream to test.
2161
- */
2162
- function detectEncoding(label, input_stream) {
2163
- if (input_stream.match([0xFF, 0xFE])) {
2164
- input_stream.offset(2);
2165
- return 'utf-16le';
2166
- }
2167
- if (input_stream.match([0xFE, 0xFF])) {
2168
- input_stream.offset(2);
2169
- return 'utf-16be';
2170
- }
2171
- if (input_stream.match([0xEF, 0xBB, 0xBF])) {
2172
- input_stream.offset(3);
2173
- return 'utf-8';
2174
- }
2175
- return label;
2176
- }
2177
-
2178
- //
2179
- // Implementation of Text Encoding Web API
2180
- //
2181
-
2182
- /** @const */ var DEFAULT_ENCODING = 'utf-8';
2183
-
2184
- /**
2185
- * @constructor
2186
- * @param {string=} opt_encoding The label of the encoding;
2187
- * defaults to 'utf-8'.
2188
- * @param {{fatal: boolean}=} options
2189
- */
2190
- function TextEncoder(opt_encoding, options) {
2191
- if (!(this instanceof TextEncoder)) {
2192
- return new TextEncoder(opt_encoding, options);
2193
- }
2194
- opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
2195
- options = Object(options);
2196
- /** @private */
2197
- this._encoding = getEncoding(opt_encoding);
2198
- if (this._encoding === null || (this._encoding.name !== 'utf-8' &&
2199
- this._encoding.name !== 'utf-16le' &&
2200
- this._encoding.name !== 'utf-16be'))
2201
- throw new TypeError('Unknown encoding: ' + opt_encoding);
2202
- /** @private @type {boolean} */
2203
- this._streaming = false;
2204
- /** @private */
2205
- this._encoder = null;
2206
- /** @private @type {{fatal: boolean}=} */
2207
- this._options = { fatal: Boolean(options.fatal) };
2208
-
2209
- if (Object.defineProperty) {
2210
- Object.defineProperty(
2211
- this, 'encoding',
2212
- { get: function() { return this._encoding.name; } });
2213
- } else {
2214
- this.encoding = this._encoding.name;
2215
- }
2216
-
2217
- return this;
2218
- }
2219
-
2220
- TextEncoder.prototype = {
2221
- /**
2222
- * @param {string=} opt_string The string to encode.
2223
- * @param {{stream: boolean}=} options
2224
- */
2225
- encode: function encode(opt_string, options) {
2226
- opt_string = opt_string ? String(opt_string) : '';
2227
- options = Object(options);
2228
- // TODO: any options?
2229
- if (!this._streaming) {
2230
- this._encoder = this._encoding.getEncoder(this._options);
2231
- }
2232
- this._streaming = Boolean(options.stream);
2233
-
2234
- var bytes = [];
2235
- var output_stream = new ByteOutputStream(bytes);
2236
- var input_stream = new CodePointInputStream(opt_string);
2237
- while (input_stream.get() !== EOF_code_point) {
2238
- this._encoder.encode(output_stream, input_stream);
2239
- }
2240
- if (!this._streaming) {
2241
- var last_byte;
2242
- do {
2243
- last_byte = this._encoder.encode(output_stream, input_stream);
2244
- } while (last_byte !== EOF_byte);
2245
- this._encoder = null;
2246
- }
2247
- return new Buffer(bytes);
2248
- }
2249
- };
2250
-
2251
-
2252
- /**
2253
- * @constructor
2254
- * @param {string=} opt_encoding The label of the encoding;
2255
- * defaults to 'utf-8'.
2256
- * @param {{fatal: boolean}=} options
2257
- */
2258
- function TextDecoder(opt_encoding, options) {
2259
- if (!(this instanceof TextDecoder)) {
2260
- return new TextDecoder(opt_encoding, options);
2261
- }
2262
- opt_encoding = opt_encoding ? String(opt_encoding) : DEFAULT_ENCODING;
2263
- options = Object(options);
2264
- /** @private */
2265
- this._encoding = getEncoding(opt_encoding);
2266
- if (this._encoding === null)
2267
- throw new TypeError('Unknown encoding: ' + opt_encoding);
2268
-
2269
- /** @private @type {boolean} */
2270
- this._streaming = false;
2271
- /** @private */
2272
- this._decoder = null;
2273
- /** @private @type {{fatal: boolean}=} */
2274
- this._options = { fatal: Boolean(options.fatal) };
2275
-
2276
- if (Object.defineProperty) {
2277
- Object.defineProperty(
2278
- this, 'encoding',
2279
- { get: function() { return this._encoding.name; } });
2280
- } else {
2281
- this.encoding = this._encoding.name;
2282
- }
2283
-
2284
- return this;
2285
- }
2286
-
2287
- // TODO: Issue if input byte stream is offset by decoder
2288
- // TODO: BOM detection will not work if stream header spans multiple calls
2289
- // (last N bytes of previous stream may need to be retained?)
2290
- TextDecoder.prototype = {
2291
- /**
2292
- * @param {Buffer=} buf The buffer of bytes to decode.
2293
- * @param {{stream: boolean}=} options
2294
- */
2295
- decode: function decode(buf, options) {
2296
- options = Object(options);
2297
-
2298
- if (!this._streaming) {
2299
- this._decoder = this._encoding.getDecoder(this._options);
2300
- this._BOMseen = false;
2301
- }
2302
- this._streaming = Boolean(options.stream);
2303
-
2304
- var input_stream = new ByteInputStream(buf);
2305
-
2306
- var output_stream = new CodePointOutputStream(), code_point;
2307
- while (input_stream.get() !== EOF_byte) {
2308
- code_point = this._decoder.decode(input_stream);
2309
- if (code_point !== null && code_point !== EOF_code_point) {
2310
- output_stream.emit(code_point);
2311
- }
2312
- }
2313
- if (!this._streaming) {
2314
- do {
2315
- code_point = this._decoder.decode(input_stream);
2316
- if (code_point !== null && code_point !== EOF_code_point) {
2317
- output_stream.emit(code_point);
2318
- }
2319
- } while (code_point !== EOF_code_point &&
2320
- input_stream.get() != EOF_byte);
2321
- this._decoder = null;
2322
- }
2323
-
2324
- var result = output_stream.string();
2325
- if (!this._BOMseen && result.length) {
2326
- this._BOMseen = true;
2327
- if (UTFs.indexOf(this.encoding) !== -1 &&
2328
- result.charCodeAt(0) === 0xFEFF) {
2329
- result = result.substring(1);
2330
- }
2331
- }
2332
-
2333
- return result;
2334
- }
2335
- };
2336
-
2337
- var UTFs = ['utf-8', 'utf-16le', 'utf-16be'];
2338
- exports.TextEncoder = TextEncoder;
2339
- exports.TextDecoder = TextDecoder;
2340
- exports.encodingExists = getEncoding;