@loaders.gl/polyfills 4.0.0-alpha.4 → 4.0.0-alpha.6

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