Initial commit
This commit is contained in:
+157
@@ -0,0 +1,157 @@
|
||||
const { inRange, decoderError, encoderError, isASCIICodePoint,
|
||||
end_of_stream, finished, isASCIIByte, floor } = require('../utils');
|
||||
const index = require('../indexes'); const { indexBig5PointerFor, indexCodePointFor } = index;
|
||||
|
||||
//
|
||||
// 12. Legacy multi-byte Chinese (traditional) encodings
|
||||
//
|
||||
|
||||
// 12.1 Big5
|
||||
|
||||
// 12.1.1 Big5 decoder
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class Big5Decoder {
|
||||
constructor(options) {
|
||||
const { fatal } = options
|
||||
this.fatal = fatal
|
||||
// Big5's decoder has an associated Big5 lead (initially 0x00).
|
||||
this.Big5_lead = 0x00
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream and Big5 lead is not 0x00, set
|
||||
// Big5 lead to 0x00 and return error.
|
||||
if (bite === end_of_stream && this.Big5_lead !== 0x00) {
|
||||
this.Big5_lead = 0x00
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 2. If byte is end-of-stream and Big5 lead is 0x00, return
|
||||
// finished.
|
||||
if (bite === end_of_stream && this.Big5_lead === 0x00)
|
||||
return finished
|
||||
|
||||
// 3. If Big5 lead is not 0x00, let lead be Big5 lead, let
|
||||
// pointer be null, set Big5 lead to 0x00, and then run these
|
||||
// substeps:
|
||||
if (this.Big5_lead !== 0x00) {
|
||||
const lead = this.Big5_lead
|
||||
let pointer = null
|
||||
this.Big5_lead = 0x00
|
||||
|
||||
// 1. Let offset be 0x40 if byte is less than 0x7F and 0x62
|
||||
// otherwise.
|
||||
const offset = bite < 0x7F ? 0x40 : 0x62
|
||||
|
||||
// 2. If byte is in the range 0x40 to 0x7E, inclusive, or 0xA1
|
||||
// to 0xFE, inclusive, set pointer to (lead − 0x81) × 157 +
|
||||
// (byte − offset).
|
||||
if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE))
|
||||
pointer = (lead - 0x81) * 157 + (bite - offset)
|
||||
|
||||
// 3. If there is a row in the table below whose first column
|
||||
// is pointer, return the two code points listed in its second
|
||||
// column
|
||||
// Pointer | Code points
|
||||
// --------+--------------
|
||||
// 1133 | U+00CA U+0304
|
||||
// 1135 | U+00CA U+030C
|
||||
// 1164 | U+00EA U+0304
|
||||
// 1166 | U+00EA U+030C
|
||||
switch (pointer) {
|
||||
case 1133: return [0x00CA, 0x0304]
|
||||
case 1135: return [0x00CA, 0x030C]
|
||||
case 1164: return [0x00EA, 0x0304]
|
||||
case 1166: return [0x00EA, 0x030C]
|
||||
}
|
||||
|
||||
// 4. Let code point be null if pointer is null and the index
|
||||
// code point for pointer in index Big5 otherwise.
|
||||
const code_point = (pointer === null) ? null :
|
||||
indexCodePointFor(pointer, index('big5'))
|
||||
|
||||
// 5. If code point is null and byte is an ASCII byte, prepend
|
||||
// byte to stream.
|
||||
if (code_point === null && isASCIIByte(bite))
|
||||
stream.prepend(bite)
|
||||
|
||||
// 6. If code point is null, return error.
|
||||
if (code_point === null)
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 7. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
|
||||
// 4. If byte is an ASCII byte, return a code point whose value
|
||||
// is byte.
|
||||
if (isASCIIByte(bite))
|
||||
return bite
|
||||
|
||||
// 5. If byte is in the range 0x81 to 0xFE, inclusive, set Big5
|
||||
// lead to byte and return continue.
|
||||
if (inRange(bite, 0x81, 0xFE)) {
|
||||
this.Big5_lead = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 6. Return error.
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 12.1.2 Big5 encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class Big5Encoder {
|
||||
constructor() {
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
*/
|
||||
this.handler = function(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point, return a byte whose
|
||||
// value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 3. Let pointer be the index Big5 pointer for code point.
|
||||
const pointer = indexBig5PointerFor(code_point)
|
||||
|
||||
// 4. If pointer is null, return error with code point.
|
||||
if (pointer === null)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 5. Let lead be floor(pointer / 157) + 0x81.
|
||||
const lead = floor(pointer / 157) + 0x81
|
||||
|
||||
// 6. If lead is less than 0xA1, return error with code point.
|
||||
if (lead < 0xA1)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 7. Let trail be pointer % 157.
|
||||
const trail = pointer % 157
|
||||
|
||||
// 8. Let offset be 0x40 if trail is less than 0x3F and 0x62
|
||||
// otherwise.
|
||||
const offset = trail < 0x3F ? 0x40 : 0x62
|
||||
|
||||
// Return two bytes whose values are lead and trail + offset.
|
||||
return [lead, trail + offset]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.Big5Decoder = Big5Decoder
|
||||
module.exports.Big5Encoder = Big5Encoder
|
||||
+169
@@ -0,0 +1,169 @@
|
||||
const { inRange, decoderError, encoderError, isASCIICodePoint,
|
||||
end_of_stream, finished, isASCIIByte, floor } = require('../utils');
|
||||
const index = require('../indexes'); const { indexCodePointFor, indexPointerFor } = index;
|
||||
|
||||
//
|
||||
// 13. Legacy multi-byte Japanese encodings
|
||||
//
|
||||
|
||||
// 13.1 euc-jp
|
||||
|
||||
// 13.1.1 euc-jp decoder
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class EUCJPDecoder {
|
||||
constructor(options) {
|
||||
const { fatal } = options
|
||||
this.fatal = fatal
|
||||
|
||||
// euc-jp's decoder has an associated euc-jp jis0212 flag
|
||||
// (initially unset) and euc-jp lead (initially 0x00).
|
||||
this.eucjp_jis0212_flag = false
|
||||
this.eucjp_lead = 0x00
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream and euc-jp lead is not 0x00, set
|
||||
// euc-jp lead to 0x00, and return error.
|
||||
if (bite === end_of_stream && this.eucjp_lead !== 0x00) {
|
||||
this.eucjp_lead = 0x00
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 2. If byte is end-of-stream and euc-jp lead is 0x00, return
|
||||
// finished.
|
||||
if (bite === end_of_stream && this.eucjp_lead === 0x00)
|
||||
return finished
|
||||
|
||||
// 3. If euc-jp lead is 0x8E and byte is in the range 0xA1 to
|
||||
// 0xDF, inclusive, set euc-jp lead to 0x00 and return a code
|
||||
// point whose value is 0xFF61 − 0xA1 + byte.
|
||||
if (this.eucjp_lead === 0x8E && inRange(bite, 0xA1, 0xDF)) {
|
||||
this.eucjp_lead = 0x00
|
||||
return 0xFF61 - 0xA1 + bite
|
||||
}
|
||||
|
||||
// 4. If euc-jp lead is 0x8F and byte is in the range 0xA1 to
|
||||
// 0xFE, inclusive, set the euc-jp jis0212 flag, set euc-jp lead
|
||||
// to byte, and return continue.
|
||||
if (this.eucjp_lead === 0x8F && inRange(bite, 0xA1, 0xFE)) {
|
||||
this.eucjp_jis0212_flag = true
|
||||
this.eucjp_lead = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 5. If euc-jp lead is not 0x00, let lead be euc-jp lead, set
|
||||
// euc-jp lead to 0x00, and run these substeps:
|
||||
if (this.eucjp_lead !== 0x00) {
|
||||
const lead = this.eucjp_lead
|
||||
this.eucjp_lead = 0x00
|
||||
|
||||
// 1. Let code point be null.
|
||||
let code_point = null
|
||||
|
||||
// 2. If lead and byte are both in the range 0xA1 to 0xFE,
|
||||
// inclusive, set code point to the index code point for (lead
|
||||
// − 0xA1) × 94 + byte − 0xA1 in index jis0208 if the euc-jp
|
||||
// jis0212 flag is unset and in index jis0212 otherwise.
|
||||
if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
|
||||
code_point = indexCodePointFor(
|
||||
(lead - 0xA1) * 94 + (bite - 0xA1),
|
||||
index(!this.eucjp_jis0212_flag ? 'jis0208' : 'jis0212'))
|
||||
}
|
||||
|
||||
// 3. Unset the euc-jp jis0212 flag.
|
||||
this.eucjp_jis0212_flag = false
|
||||
|
||||
// 4. If byte is not in the range 0xA1 to 0xFE, inclusive,
|
||||
// prepend byte to stream.
|
||||
if (!inRange(bite, 0xA1, 0xFE))
|
||||
stream.prepend(bite)
|
||||
|
||||
// 5. If code point is null, return error.
|
||||
if (code_point === null)
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 6. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
|
||||
// 6. If byte is an ASCII byte, return a code point whose value
|
||||
// is byte.
|
||||
if (isASCIIByte(bite))
|
||||
return bite
|
||||
|
||||
// 7. If byte is 0x8E, 0x8F, or in the range 0xA1 to 0xFE,
|
||||
// inclusive, set euc-jp lead to byte and return continue.
|
||||
if (bite === 0x8E || bite === 0x8F || inRange(bite, 0xA1, 0xFE)) {
|
||||
this.eucjp_lead = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 8. Return error.
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
}
|
||||
|
||||
// 13.1.2 euc-jp encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class EUCJPEncoder {
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point, return a byte whose
|
||||
// value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 3. If code point is U+00A5, return byte 0x5C.
|
||||
if (code_point === 0x00A5)
|
||||
return 0x5C
|
||||
|
||||
// 4. If code point is U+203E, return byte 0x7E.
|
||||
if (code_point === 0x203E)
|
||||
return 0x7E
|
||||
|
||||
// 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
|
||||
// return two bytes whose values are 0x8E and code point −
|
||||
// 0xFF61 + 0xA1.
|
||||
if (inRange(code_point, 0xFF61, 0xFF9F))
|
||||
return [0x8E, code_point - 0xFF61 + 0xA1]
|
||||
|
||||
// 6. If code point is U+2212, set it to U+FF0D.
|
||||
if (code_point === 0x2212)
|
||||
code_point = 0xFF0D
|
||||
|
||||
// 7. Let pointer be the index pointer for code point in index
|
||||
// jis0208.
|
||||
const pointer = indexPointerFor(code_point, index('jis0208'))
|
||||
|
||||
// 8. If pointer is null, return error with code point.
|
||||
if (pointer === null)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 9. Let lead be floor(pointer / 94) + 0xA1.
|
||||
const lead = floor(pointer / 94) + 0xA1
|
||||
|
||||
// 10. Let trail be pointer % 94 + 0xA1.
|
||||
const trail = pointer % 94 + 0xA1
|
||||
|
||||
// 11. Return two bytes whose values are lead and trail.
|
||||
return [lead, trail]
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.EUCJPDecoder = EUCJPDecoder
|
||||
module.exports.EUCJPEncoder = EUCJPEncoder
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
const { inRange, decoderError, encoderError, isASCIICodePoint,
|
||||
end_of_stream, finished, isASCIIByte, floor } = require('../utils');
|
||||
const index = require('../indexes'); const { indexCodePointFor, indexPointerFor } = index;
|
||||
|
||||
//
|
||||
// 14. Legacy multi-byte Korean encodings
|
||||
//
|
||||
|
||||
// 14.1 euc-kr
|
||||
|
||||
// 14.1.1 euc-kr decoder
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class EUCKRDecoder {
|
||||
constructor(options) {
|
||||
const { fatal } = options
|
||||
this.fatal = fatal
|
||||
// euc-kr's decoder has an associated euc-kr lead (initially 0x00).
|
||||
this.euckr_lead = 0x00
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream and euc-kr lead is not 0x00, set
|
||||
// euc-kr lead to 0x00 and return error.
|
||||
if (bite === end_of_stream && this.euckr_lead !== 0) {
|
||||
this.euckr_lead = 0x00
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 2. If byte is end-of-stream and euc-kr lead is 0x00, return
|
||||
// finished.
|
||||
if (bite === end_of_stream && this.euckr_lead === 0)
|
||||
return finished
|
||||
|
||||
// 3. If euc-kr lead is not 0x00, let lead be euc-kr lead, let
|
||||
// pointer be null, set euc-kr lead to 0x00, and then run these
|
||||
// substeps:
|
||||
if (this.euckr_lead !== 0x00) {
|
||||
const lead = this.euckr_lead
|
||||
let pointer = null
|
||||
this.euckr_lead = 0x00
|
||||
|
||||
// 1. If byte is in the range 0x41 to 0xFE, inclusive, set
|
||||
// pointer to (lead − 0x81) × 190 + (byte − 0x41).
|
||||
if (inRange(bite, 0x41, 0xFE))
|
||||
pointer = (lead - 0x81) * 190 + (bite - 0x41)
|
||||
|
||||
// 2. Let code point be null, if pointer is null, and the
|
||||
// index code point for pointer in index euc-kr otherwise.
|
||||
const code_point = (pointer === null)
|
||||
? null : indexCodePointFor(pointer, index('euc-kr'))
|
||||
|
||||
// 3. If code point is null and byte is an ASCII byte, prepend
|
||||
// byte to stream.
|
||||
if (pointer === null && isASCIIByte(bite))
|
||||
stream.prepend(bite)
|
||||
|
||||
// 4. If code point is null, return error.
|
||||
if (code_point === null)
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 5. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
|
||||
// 4. If byte is an ASCII byte, return a code point whose value
|
||||
// is byte.
|
||||
if (isASCIIByte(bite))
|
||||
return bite
|
||||
|
||||
// 5. If byte is in the range 0x81 to 0xFE, inclusive, set
|
||||
// euc-kr lead to byte and return continue.
|
||||
if (inRange(bite, 0x81, 0xFE)) {
|
||||
this.euckr_lead = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 6. Return error.
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
}
|
||||
|
||||
// 14.1.2 euc-kr encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class EUCKREncoder {
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
* @return {(number|!Array.<number>)} Byte(s) to emit.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point, return a byte whose
|
||||
// value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 3. Let pointer be the index pointer for code point in index
|
||||
// euc-kr.
|
||||
const pointer = indexPointerFor(code_point, index('euc-kr'))
|
||||
|
||||
// 4. If pointer is null, return error with code point.
|
||||
if (pointer === null)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 5. Let lead be floor(pointer / 190) + 0x81.
|
||||
const lead = floor(pointer / 190) + 0x81
|
||||
|
||||
// 6. Let trail be pointer % 190 + 0x41.
|
||||
const trail = (pointer % 190) + 0x41
|
||||
|
||||
// 7. Return two bytes whose values are lead and trail.
|
||||
return [lead, trail]
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.EUCKRDecoder = EUCKRDecoder
|
||||
module.exports.EUCKREncoder = EUCKREncoder
|
||||
+255
@@ -0,0 +1,255 @@
|
||||
const { inRange, decoderError, encoderError, isASCIICodePoint,
|
||||
end_of_stream, finished, isASCIIByte, floor } = require('../utils');
|
||||
const index = require('../indexes'); const {
|
||||
indexGB18030RangesCodePointFor, indexGB18030RangesPointerFor,
|
||||
indexCodePointFor, indexPointerFor } = index;
|
||||
|
||||
// 11.2 gb18030
|
||||
|
||||
// 11.2.1 gb18030 decoder
|
||||
/**
|
||||
* @constructor
|
||||
* @implements {Decoder}
|
||||
* @param {{fatal: boolean}} options
|
||||
*/
|
||||
class GB18030Decoder {
|
||||
constructor(options) {
|
||||
const { fatal } = options
|
||||
this.fatal = fatal
|
||||
// gb18030's decoder has an associated gb18030 first, gb18030
|
||||
// second, and gb18030 third (all initially 0x00).
|
||||
this.gb18030_first = 0x00
|
||||
this.gb18030_second = 0x00,
|
||||
this.gb18030_third = 0x00
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
* @return The next code point(s) decoded, or null if not enough data exists in the input stream to decode a complete code point.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream and gb18030 first, gb18030
|
||||
// second, and gb18030 third are 0x00, return finished.
|
||||
if (bite === end_of_stream && this.gb18030_first === 0x00 &&
|
||||
this.gb18030_second === 0x00 && this.gb18030_third === 0x00) {
|
||||
return finished
|
||||
}
|
||||
// 2. If byte is end-of-stream, and gb18030 first, gb18030
|
||||
// second, or gb18030 third is not 0x00, set gb18030 first,
|
||||
// gb18030 second, and gb18030 third to 0x00, and return error.
|
||||
if (bite === end_of_stream &&
|
||||
(this.gb18030_first !== 0x00 || this.gb18030_second !== 0x00 ||
|
||||
this.gb18030_third !== 0x00)) {
|
||||
this.gb18030_first = 0x00
|
||||
this.gb18030_second = 0x00
|
||||
this.gb18030_third = 0x00
|
||||
decoderError(this.fatal)
|
||||
}
|
||||
var code_point
|
||||
// 3. If gb18030 third is not 0x00, run these substeps:
|
||||
if (this.gb18030_third !== 0x00) {
|
||||
// 1. Let code point be null.
|
||||
code_point = null
|
||||
// 2. If byte is in the range 0x30 to 0x39, inclusive, set
|
||||
// code point to the index gb18030 ranges code point for
|
||||
// (((gb18030 first − 0x81) × 10 + gb18030 second − 0x30) ×
|
||||
// 126 + gb18030 third − 0x81) × 10 + byte − 0x30.
|
||||
if (inRange(bite, 0x30, 0x39)) {
|
||||
code_point = indexGB18030RangesCodePointFor(
|
||||
(((this.gb18030_first - 0x81) * 10 + this.gb18030_second - 0x30) * 126 +
|
||||
this.gb18030_third - 0x81) * 10 + bite - 0x30)
|
||||
}
|
||||
|
||||
// 3. Let buffer be a byte sequence consisting of gb18030
|
||||
// second, gb18030 third, and byte, in order.
|
||||
var buffer = [this.gb18030_second, this.gb18030_third, bite]
|
||||
|
||||
// 4. Set gb18030 first, gb18030 second, and gb18030 third to
|
||||
// 0x00.
|
||||
this.gb18030_first = 0x00
|
||||
this.gb18030_second = 0x00
|
||||
this.gb18030_third = 0x00
|
||||
|
||||
// 5. If code point is null, prepend buffer to stream and
|
||||
// return error.
|
||||
if (code_point === null) {
|
||||
stream.prepend(buffer)
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 6. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
|
||||
// 4. If gb18030 second is not 0x00, run these substeps:
|
||||
if (this.gb18030_second !== 0x00) {
|
||||
// 1. If byte is in the range 0x81 to 0xFE, inclusive, set
|
||||
// gb18030 third to byte and return continue.
|
||||
if (inRange(bite, 0x81, 0xFE)) {
|
||||
this.gb18030_third = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 2. Prepend gb18030 second followed by byte to stream, set
|
||||
// gb18030 first and gb18030 second to 0x00, and return error.
|
||||
stream.prepend([this.gb18030_second, bite])
|
||||
this.gb18030_first = 0x00
|
||||
this.gb18030_second = 0x00
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 5. If gb18030 first is not 0x00, run these substeps:
|
||||
if (this.gb18030_first !== 0x00) {
|
||||
// 1. If byte is in the range 0x30 to 0x39, inclusive, set
|
||||
// gb18030 second to byte and return continue.
|
||||
if (inRange(bite, 0x30, 0x39)) {
|
||||
this.gb18030_second = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 2. Let lead be gb18030 first, let pointer be null, and set
|
||||
// gb18030 first to 0x00.
|
||||
var lead = this.gb18030_first
|
||||
var pointer = null
|
||||
this.gb18030_first = 0x00
|
||||
|
||||
// 3. Let offset be 0x40 if byte is less than 0x7F and 0x41
|
||||
// otherwise.
|
||||
var offset = bite < 0x7F ? 0x40 : 0x41
|
||||
|
||||
// 4. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
|
||||
// to 0xFE, inclusive, set pointer to (lead − 0x81) × 190 +
|
||||
// (byte − offset).
|
||||
if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE))
|
||||
pointer = (lead - 0x81) * 190 + (bite - offset)
|
||||
|
||||
// 5. Let code point be null if pointer is null and the index
|
||||
// code point for pointer in index gb18030 otherwise.
|
||||
code_point = pointer === null ? null :
|
||||
indexCodePointFor(pointer, index('gb18030'))
|
||||
|
||||
// 6. If code point is null and byte is an ASCII byte, prepend
|
||||
// byte to stream.
|
||||
if (code_point === null && isASCIIByte(bite))
|
||||
stream.prepend(bite)
|
||||
|
||||
// 7. If code point is null, return error.
|
||||
if (code_point === null)
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 8. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
|
||||
// 6. If byte is an ASCII byte, return a code point whose value
|
||||
// is byte.
|
||||
if (isASCIIByte(bite))
|
||||
return bite
|
||||
|
||||
// 7. If byte is 0x80, return code point U+20AC.
|
||||
if (bite === 0x80)
|
||||
return 0x20AC
|
||||
|
||||
// 8. If byte is in the range 0x81 to 0xFE, inclusive, set
|
||||
// gb18030 first to byte and return continue.
|
||||
if (inRange(bite, 0x81, 0xFE)) {
|
||||
this.gb18030_first = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 9. Return error.
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
}
|
||||
|
||||
// 11.2.2 gb18030 encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class GB18030Encoder {
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
* @return Byte(s) to emit.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point, return a byte whose
|
||||
// value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 3. If code point is U+E5E5, return error with code point.
|
||||
if (code_point === 0xE5E5)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 4. If the gbk flag is set and code point is U+20AC, return
|
||||
// byte 0x80.
|
||||
if (this.gbk_flag && code_point === 0x20AC)
|
||||
return 0x80
|
||||
|
||||
// 5. Let pointer be the index pointer for code point in index
|
||||
// gb18030.
|
||||
var pointer = indexPointerFor(code_point, index('gb18030'))
|
||||
|
||||
// 6. If pointer is not null, run these substeps:
|
||||
if (pointer !== null) {
|
||||
// 1. Let lead be floor(pointer / 190) + 0x81.
|
||||
var lead = floor(pointer / 190) + 0x81
|
||||
|
||||
// 2. Let trail be pointer % 190.
|
||||
var trail = pointer % 190
|
||||
|
||||
// 3. Let offset be 0x40 if trail is less than 0x3F and 0x41 otherwise.
|
||||
var offset = trail < 0x3F ? 0x40 : 0x41
|
||||
|
||||
// 4. Return two bytes whose values are lead and trail + offset.
|
||||
return [lead, trail + offset]
|
||||
}
|
||||
|
||||
// 7. If gbk flag is set, return error with code point.
|
||||
if (this.gbk_flag)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 8. Set pointer to the index gb18030 ranges pointer for code
|
||||
// point.
|
||||
pointer = indexGB18030RangesPointerFor(code_point)
|
||||
|
||||
// 9. Let byte1 be floor(pointer / 10 / 126 / 10).
|
||||
var byte1 = floor(pointer / 10 / 126 / 10)
|
||||
|
||||
// 10. Set pointer to pointer − byte1 × 10 × 126 × 10.
|
||||
pointer = pointer - byte1 * 10 * 126 * 10
|
||||
|
||||
// 11. Let byte2 be floor(pointer / 10 / 126).
|
||||
var byte2 = floor(pointer / 10 / 126)
|
||||
|
||||
// 12. Set pointer to pointer − byte2 × 10 × 126.
|
||||
pointer = pointer - byte2 * 10 * 126
|
||||
|
||||
// 13. Let byte3 be floor(pointer / 10).
|
||||
var byte3 = floor(pointer / 10)
|
||||
|
||||
// 14. Let byte4 be pointer − byte3 × 10.
|
||||
var byte4 = pointer - byte3 * 10
|
||||
|
||||
// 15. Return four bytes whose values are byte1 + 0x81, byte2 +
|
||||
// 0x30, byte3 + 0x81, byte4 + 0x30.
|
||||
return [byte1 + 0x81,
|
||||
byte2 + 0x30,
|
||||
byte3 + 0x81,
|
||||
byte4 + 0x30]
|
||||
}
|
||||
|
||||
constructor(options = {}, gbk_flag = false) {
|
||||
// gb18030's decoder has an associated gbk flag (initially unset).
|
||||
this.gbk_flag = gbk_flag
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports.GB18030Decoder = GB18030Decoder
|
||||
module.exports.GB18030Encoder = GB18030Encoder
|
||||
+444
@@ -0,0 +1,444 @@
|
||||
const { inRange, decoderError, encoderError, isASCIICodePoint,
|
||||
end_of_stream, finished, floor } = require('../utils');
|
||||
const index = require('../indexes'); const { indexCodePointFor, indexPointerFor } = index;
|
||||
|
||||
// 13.2 iso-2022-jp
|
||||
|
||||
// 13.2.1 iso-2022-jp decoder
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class ISO2022JPDecoder {
|
||||
constructor(options) {
|
||||
const { fatal } = options
|
||||
this.fatal = fatal
|
||||
/** @enum */
|
||||
this.states = {
|
||||
ASCII: 0,
|
||||
Roman: 1,
|
||||
Katakana: 2,
|
||||
LeadByte: 3,
|
||||
TrailByte: 4,
|
||||
EscapeStart: 5,
|
||||
Escape: 6,
|
||||
}
|
||||
// iso-2022-jp's decoder has an associated iso-2022-jp decoder
|
||||
// state (initially ASCII), iso-2022-jp decoder output state
|
||||
// (initially ASCII), iso-2022-jp lead (initially 0x00), and
|
||||
// iso-2022-jp output flag (initially unset).
|
||||
this.iso2022jp_decoder_state = this.states.ASCII
|
||||
this.iso2022jp_decoder_output_state = this.states.ASCII,
|
||||
this.iso2022jp_lead = 0x00
|
||||
this.iso2022jp_output_flag = false
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// switching on iso-2022-jp decoder state:
|
||||
switch (this.iso2022jp_decoder_state) {
|
||||
default:
|
||||
case this.states.ASCII:
|
||||
// ASCII
|
||||
// Based on byte:
|
||||
|
||||
// 0x1B
|
||||
if (bite === 0x1B) {
|
||||
// Set iso-2022-jp decoder state to escape start and return
|
||||
// continue.
|
||||
this.iso2022jp_decoder_state = this.states.EscapeStart
|
||||
return null
|
||||
}
|
||||
|
||||
// 0x00 to 0x7F, excluding 0x0E, 0x0F, and 0x1B
|
||||
if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E
|
||||
&& bite !== 0x0F && bite !== 0x1B) {
|
||||
// Unset the iso-2022-jp output flag and return a code point
|
||||
// whose value is byte.
|
||||
this.iso2022jp_output_flag = false
|
||||
return bite
|
||||
}
|
||||
|
||||
// end-of-stream
|
||||
if (bite === end_of_stream) {
|
||||
// Return finished.
|
||||
return finished
|
||||
}
|
||||
|
||||
// Otherwise
|
||||
// Unset the iso-2022-jp output flag and return error.
|
||||
this.iso2022jp_output_flag = false
|
||||
return decoderError(this.fatal)
|
||||
|
||||
case this.states.Roman:
|
||||
// Roman
|
||||
// Based on byte:
|
||||
|
||||
// 0x1B
|
||||
if (bite === 0x1B) {
|
||||
// Set iso-2022-jp decoder state to escape start and return
|
||||
// continue.
|
||||
this.iso2022jp_decoder_state = this.states.EscapeStart
|
||||
return null
|
||||
}
|
||||
|
||||
// 0x5C
|
||||
if (bite === 0x5C) {
|
||||
// Unset the iso-2022-jp output flag and return code point
|
||||
// U+00A5.
|
||||
this.iso2022jp_output_flag = false
|
||||
return 0x00A5
|
||||
}
|
||||
|
||||
// 0x7E
|
||||
if (bite === 0x7E) {
|
||||
// Unset the iso-2022-jp output flag and return code point
|
||||
// U+203E.
|
||||
this.iso2022jp_output_flag = false
|
||||
return 0x203E
|
||||
}
|
||||
|
||||
// 0x00 to 0x7F, excluding 0x0E, 0x0F, 0x1B, 0x5C, and 0x7E
|
||||
if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E && bite !== 0x0F
|
||||
&& bite !== 0x1B && bite !== 0x5C && bite !== 0x7E) {
|
||||
// Unset the iso-2022-jp output flag and return a code point
|
||||
// whose value is byte.
|
||||
this.iso2022jp_output_flag = false
|
||||
return bite
|
||||
}
|
||||
|
||||
// end-of-stream
|
||||
if (bite === end_of_stream) {
|
||||
// Return finished.
|
||||
return finished
|
||||
}
|
||||
|
||||
// Otherwise
|
||||
// Unset the iso-2022-jp output flag and return error.
|
||||
this.iso2022jp_output_flag = false
|
||||
return decoderError(this.fatal)
|
||||
|
||||
case this.states.Katakana:
|
||||
// Katakana
|
||||
// Based on byte:
|
||||
|
||||
// 0x1B
|
||||
if (bite === 0x1B) {
|
||||
// Set iso-2022-jp decoder state to escape start and return
|
||||
// continue.
|
||||
this.iso2022jp_decoder_state = this.states.EscapeStart
|
||||
return null
|
||||
}
|
||||
|
||||
// 0x21 to 0x5F
|
||||
if (inRange(bite, 0x21, 0x5F)) {
|
||||
// Unset the iso-2022-jp output flag and return a code point
|
||||
// whose value is 0xFF61 − 0x21 + byte.
|
||||
this.iso2022jp_output_flag = false
|
||||
return 0xFF61 - 0x21 + bite
|
||||
}
|
||||
|
||||
// end-of-stream
|
||||
if (bite === end_of_stream) {
|
||||
// Return finished.
|
||||
return finished
|
||||
}
|
||||
|
||||
// Otherwise
|
||||
// Unset the iso-2022-jp output flag and return error.
|
||||
this.iso2022jp_output_flag = false
|
||||
return decoderError(this.fatal)
|
||||
|
||||
case this.states.LeadByte:
|
||||
// Lead byte
|
||||
// Based on byte:
|
||||
|
||||
// 0x1B
|
||||
if (bite === 0x1B) {
|
||||
// Set iso-2022-jp decoder state to escape start and return
|
||||
// continue.
|
||||
this.iso2022jp_decoder_state = this.states.EscapeStart
|
||||
return null
|
||||
}
|
||||
|
||||
// 0x21 to 0x7E
|
||||
if (inRange(bite, 0x21, 0x7E)) {
|
||||
// Unset the iso-2022-jp output flag, set iso-2022-jp lead
|
||||
// to byte, iso-2022-jp decoder state to trail byte, and
|
||||
// return continue.
|
||||
this.iso2022jp_output_flag = false
|
||||
this.iso2022jp_lead = bite
|
||||
this.iso2022jp_decoder_state = this.states.TrailByte
|
||||
return null
|
||||
}
|
||||
|
||||
// end-of-stream
|
||||
if (bite === end_of_stream) {
|
||||
// Return finished.
|
||||
return finished
|
||||
}
|
||||
|
||||
// Otherwise
|
||||
// Unset the iso-2022-jp output flag and return error.
|
||||
this.iso2022jp_output_flag = false
|
||||
return decoderError(this.fatal)
|
||||
|
||||
case this.states.TrailByte:
|
||||
// Trail byte
|
||||
// Based on byte:
|
||||
|
||||
// 0x1B
|
||||
if (bite === 0x1B) {
|
||||
// Set iso-2022-jp decoder state to escape start and return
|
||||
// continue.
|
||||
this.iso2022jp_decoder_state = this.states.EscapeStart
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 0x21 to 0x7E
|
||||
if (inRange(bite, 0x21, 0x7E)) {
|
||||
// 1. Set the iso-2022-jp decoder state to lead byte.
|
||||
this.iso2022jp_decoder_state = this.states.LeadByte
|
||||
|
||||
// 2. Let pointer be (iso-2022-jp lead − 0x21) × 94 + byte − 0x21.
|
||||
const pointer = (this.iso2022jp_lead - 0x21) * 94 + bite - 0x21
|
||||
|
||||
// 3. Let code point be the index code point for pointer in
|
||||
// index jis0208.
|
||||
const code_point = indexCodePointFor(pointer, index('jis0208'))
|
||||
|
||||
// 4. If code point is null, return error.
|
||||
if (code_point === null)
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 5. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
|
||||
// end-of-stream
|
||||
if (bite === end_of_stream) {
|
||||
// Set the iso-2022-jp decoder state to lead byte, prepend
|
||||
// byte to stream, and return error.
|
||||
this.iso2022jp_decoder_state = this.states.LeadByte
|
||||
stream.prepend(bite)
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// Otherwise
|
||||
// Set iso-2022-jp decoder state to lead byte and return
|
||||
// error.
|
||||
this.iso2022jp_decoder_state = this.states.LeadByte
|
||||
return decoderError(this.fatal)
|
||||
|
||||
case this.states.EscapeStart:
|
||||
// Escape start
|
||||
|
||||
// 1. If byte is either 0x24 or 0x28, set iso-2022-jp lead to
|
||||
// byte, iso-2022-jp decoder state to escape, and return
|
||||
// continue.
|
||||
if (bite === 0x24 || bite === 0x28) {
|
||||
this.iso2022jp_lead = bite
|
||||
this.iso2022jp_decoder_state = this.states.Escape
|
||||
return null
|
||||
}
|
||||
|
||||
// 2. Prepend byte to stream.
|
||||
stream.prepend(bite)
|
||||
|
||||
// 3. Unset the iso-2022-jp output flag, set iso-2022-jp
|
||||
// decoder state to iso-2022-jp decoder output state, and
|
||||
// return error.
|
||||
this.iso2022jp_output_flag = false
|
||||
this.iso2022jp_decoder_state = this.iso2022jp_decoder_output_state
|
||||
return decoderError(this.fatal)
|
||||
|
||||
case this.states.Escape: {
|
||||
// Escape
|
||||
|
||||
// 1. Let lead be iso-2022-jp lead and set iso-2022-jp lead to
|
||||
// 0x00.
|
||||
const lead = this.iso2022jp_lead
|
||||
this.iso2022jp_lead = 0x00
|
||||
|
||||
// 2. Let state be null.
|
||||
let state = null
|
||||
|
||||
// 3. If lead is 0x28 and byte is 0x42, set state to ASCII.
|
||||
if (lead === 0x28 && bite === 0x42)
|
||||
state = this.states.ASCII
|
||||
|
||||
// 4. If lead is 0x28 and byte is 0x4A, set state to Roman.
|
||||
if (lead === 0x28 && bite === 0x4A)
|
||||
state = this.states.Roman
|
||||
|
||||
// 5. If lead is 0x28 and byte is 0x49, set state to Katakana.
|
||||
if (lead === 0x28 && bite === 0x49)
|
||||
state = this.states.Katakana
|
||||
|
||||
// 6. If lead is 0x24 and byte is either 0x40 or 0x42, set
|
||||
// state to lead byte.
|
||||
if (lead === 0x24 && (bite === 0x40 || bite === 0x42))
|
||||
state = this.states.LeadByte
|
||||
|
||||
// 7. If state is non-null, run these substeps:
|
||||
if (state !== null) {
|
||||
// 1. Set iso-2022-jp decoder state and iso-2022-jp decoder
|
||||
// output state to this.states.
|
||||
this.iso2022jp_decoder_state = this.iso2022jp_decoder_state = state
|
||||
|
||||
// 2. Let output flag be the iso-2022-jp output flag.
|
||||
const output_flag = this.iso2022jp_output_flag
|
||||
|
||||
// 3. Set the iso-2022-jp output flag.
|
||||
this.iso2022jp_output_flag = true
|
||||
|
||||
// 4. Return continue, if output flag is unset, and error
|
||||
// otherwise.
|
||||
return !output_flag ? null : decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 8. Prepend lead and byte to stream.
|
||||
stream.prepend([lead, bite])
|
||||
|
||||
// 9. Unset the iso-2022-jp output flag, set iso-2022-jp
|
||||
// decoder state to iso-2022-jp decoder output state and
|
||||
// return error.
|
||||
this.iso2022jp_output_flag = false
|
||||
this.iso2022jp_decoder_state = this.iso2022jp_decoder_output_state
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 13.2.2 iso-2022-jp encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class ISO2022JPEncoder {
|
||||
constructor() {
|
||||
// iso-2022-jp's encoder has an associated iso-2022-jp encoder
|
||||
// state which is one of ASCII, Roman, and jis0208 (initially
|
||||
// ASCII).
|
||||
/** @enum */
|
||||
this.states = {
|
||||
ASCII: 0,
|
||||
Roman: 1,
|
||||
jis0208: 2,
|
||||
}
|
||||
this.iso2022jp_state = this.states.ASCII
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1. If code point is end-of-stream and iso-2022-jp encoder
|
||||
// state is not ASCII, prepend code point to stream, set
|
||||
// iso-2022-jp encoder state to ASCII, and return three bytes
|
||||
// 0x1B 0x28 0x42.
|
||||
if (code_point === end_of_stream &&
|
||||
this.iso2022jp_state !== this.states.ASCII) {
|
||||
stream.prepend(code_point)
|
||||
this.iso2022jp_state = this.states.ASCII
|
||||
return [0x1B, 0x28, 0x42]
|
||||
}
|
||||
|
||||
// 2. If code point is end-of-stream and iso-2022-jp encoder
|
||||
// state is ASCII, return finished.
|
||||
if (code_point === end_of_stream && this.iso2022jp_state === this.states.ASCII)
|
||||
return finished
|
||||
|
||||
// 3. If ISO-2022-JP encoder state is ASCII or Roman, and code
|
||||
// point is U+000E, U+000F, or U+001B, return error with U+FFFD.
|
||||
if ((this.iso2022jp_state === this.states.ASCII ||
|
||||
this.iso2022jp_state === this.states.Roman) &&
|
||||
(code_point === 0x000E || code_point === 0x000F ||
|
||||
code_point === 0x001B)) {
|
||||
return encoderError(0xFFFD)
|
||||
}
|
||||
|
||||
// 4. If iso-2022-jp encoder state is ASCII and code point is an
|
||||
// ASCII code point, return a byte whose value is code point.
|
||||
if (this.iso2022jp_state === this.states.ASCII &&
|
||||
isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 5. If iso-2022-jp encoder state is Roman and code point is an
|
||||
// ASCII code point, excluding U+005C and U+007E, or is U+00A5
|
||||
// or U+203E, run these substeps:
|
||||
if (this.iso2022jp_state === this.states.Roman &&
|
||||
((isASCIICodePoint(code_point) &&
|
||||
code_point !== 0x005C && code_point !== 0x007E) ||
|
||||
(code_point == 0x00A5 || code_point == 0x203E))) {
|
||||
// 1. If code point is an ASCII code point, return a byte
|
||||
// whose value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 2. If code point is U+00A5, return byte 0x5C.
|
||||
if (code_point === 0x00A5)
|
||||
return 0x5C
|
||||
|
||||
// 3. If code point is U+203E, return byte 0x7E.
|
||||
if (code_point === 0x203E)
|
||||
return 0x7E
|
||||
}
|
||||
|
||||
// 6. If code point is an ASCII code point, and iso-2022-jp
|
||||
// encoder state is not ASCII, prepend code point to stream, set
|
||||
// iso-2022-jp encoder state to ASCII, and return three bytes
|
||||
// 0x1B 0x28 0x42.
|
||||
if (isASCIICodePoint(code_point) &&
|
||||
this.iso2022jp_state !== this.states.ASCII) {
|
||||
stream.prepend(code_point)
|
||||
this.iso2022jp_state = this.states.ASCII
|
||||
return [0x1B, 0x28, 0x42]
|
||||
}
|
||||
|
||||
// 7. If code point is either U+00A5 or U+203E, and iso-2022-jp
|
||||
// encoder state is not Roman, prepend code point to stream, set
|
||||
// iso-2022-jp encoder state to Roman, and return three bytes
|
||||
// 0x1B 0x28 0x4A.
|
||||
if ((code_point === 0x00A5 || code_point === 0x203E) &&
|
||||
this.iso2022jp_state !== this.states.Roman) {
|
||||
stream.prepend(code_point)
|
||||
this.iso2022jp_state = this.states.Roman
|
||||
return [0x1B, 0x28, 0x4A]
|
||||
}
|
||||
|
||||
// 8. If code point is U+2212, set it to U+FF0D.
|
||||
if (code_point === 0x2212)
|
||||
code_point = 0xFF0D
|
||||
|
||||
// 9. Let pointer be the index pointer for code point in index
|
||||
// jis0208.
|
||||
const pointer = indexPointerFor(code_point, index('jis0208'))
|
||||
|
||||
// 10. If pointer is null, return error with code point.
|
||||
if (pointer === null)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 11. If iso-2022-jp encoder state is not jis0208, prepend code
|
||||
// point to stream, set iso-2022-jp encoder state to jis0208,
|
||||
// and return three bytes 0x1B 0x24 0x42.
|
||||
if (this.iso2022jp_state !== this.states.jis0208) {
|
||||
stream.prepend(code_point)
|
||||
this.iso2022jp_state = this.states.jis0208
|
||||
return [0x1B, 0x24, 0x42]
|
||||
}
|
||||
|
||||
// 12. Let lead be floor(pointer / 94) + 0x21.
|
||||
const lead = floor(pointer / 94) + 0x21
|
||||
|
||||
// 13. Let trail be pointer % 94 + 0x21.
|
||||
const trail = pointer % 94 + 0x21
|
||||
|
||||
// 14. Return two bytes whose values are lead and trail.
|
||||
return [lead, trail]
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.ISO2022JPDecoder = ISO2022JPDecoder
|
||||
module.exports.ISO2022JPEncoder = ISO2022JPEncoder
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
const { inRange, decoderError, encoderError, floor, isASCIICodePoint, isASCIIByte,
|
||||
end_of_stream, finished } = require('../utils');
|
||||
const index = require('../indexes'); const { indexCodePointFor, indexShiftJISPointerFor } = index;
|
||||
|
||||
|
||||
// 13.3 Shift_JIS
|
||||
|
||||
// 13.3.1 Shift_JIS decoder
|
||||
/**
|
||||
* @constructor
|
||||
* @implements {Decoder}
|
||||
* @param {{fatal: boolean}} options
|
||||
*/
|
||||
class ShiftJISDecoder {
|
||||
constructor(options) {
|
||||
const { fatal } = options
|
||||
this.fatal = fatal
|
||||
// Shift_JIS's decoder has an associated Shift_JIS lead (initially
|
||||
// 0x00).
|
||||
this.Shift_JIS_lead = 0x00
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream and Shift_JIS lead is not 0x00,
|
||||
// set Shift_JIS lead to 0x00 and return error.
|
||||
if (bite === end_of_stream && this.Shift_JIS_lead !== 0x00) {
|
||||
this.Shift_JIS_lead = 0x00
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 2. If byte is end-of-stream and Shift_JIS lead is 0x00,
|
||||
// return finished.
|
||||
if (bite === end_of_stream && this.Shift_JIS_lead === 0x00)
|
||||
return finished
|
||||
|
||||
// 3. If Shift_JIS lead is not 0x00, let lead be Shift_JIS lead,
|
||||
// let pointer be null, set Shift_JIS lead to 0x00, and then run
|
||||
// these substeps:
|
||||
if (this.Shift_JIS_lead !== 0x00) {
|
||||
var lead = this.Shift_JIS_lead
|
||||
var pointer = null
|
||||
this.Shift_JIS_lead = 0x00
|
||||
|
||||
// 1. Let offset be 0x40, if byte is less than 0x7F, and 0x41
|
||||
// otherwise.
|
||||
var offset = (bite < 0x7F) ? 0x40 : 0x41
|
||||
|
||||
// 2. Let lead offset be 0x81, if lead is less than 0xA0, and
|
||||
// 0xC1 otherwise.
|
||||
var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1
|
||||
|
||||
// 3. If byte is in the range 0x40 to 0x7E, inclusive, or 0x80
|
||||
// to 0xFC, inclusive, set pointer to (lead − lead offset) ×
|
||||
// 188 + byte − offset.
|
||||
if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC))
|
||||
pointer = (lead - lead_offset) * 188 + bite - offset
|
||||
|
||||
// 4. If pointer is in the range 8836 to 10715, inclusive,
|
||||
// return a code point whose value is 0xE000 − 8836 + pointer.
|
||||
if (inRange(pointer, 8836, 10715))
|
||||
return 0xE000 - 8836 + pointer
|
||||
|
||||
// 5. Let code point be null, if pointer is null, and the
|
||||
// index code point for pointer in index jis0208 otherwise.
|
||||
var code_point = (pointer === null) ? null :
|
||||
indexCodePointFor(pointer, index('jis0208'))
|
||||
|
||||
// 6. If code point is null and byte is an ASCII byte, prepend
|
||||
// byte to stream.
|
||||
if (code_point === null && isASCIIByte(bite))
|
||||
stream.prepend(bite)
|
||||
|
||||
// 7. If code point is null, return error.
|
||||
if (code_point === null)
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 8. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
|
||||
// 4. If byte is an ASCII byte or 0x80, return a code point
|
||||
// whose value is byte.
|
||||
if (isASCIIByte(bite) || bite === 0x80)
|
||||
return bite
|
||||
|
||||
// 5. If byte is in the range 0xA1 to 0xDF, inclusive, return a
|
||||
// code point whose value is 0xFF61 − 0xA1 + byte.
|
||||
if (inRange(bite, 0xA1, 0xDF))
|
||||
return 0xFF61 - 0xA1 + bite
|
||||
|
||||
// 6. If byte is in the range 0x81 to 0x9F, inclusive, or 0xE0
|
||||
// to 0xFC, inclusive, set Shift_JIS lead to byte and return
|
||||
// continue.
|
||||
if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
|
||||
this.Shift_JIS_lead = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 7. Return error.
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
}
|
||||
|
||||
// 13.3.2 Shift_JIS encoder
|
||||
/**
|
||||
* @constructor
|
||||
* @implements {Encoder}
|
||||
* @param {{fatal: boolean}} options
|
||||
*/
|
||||
class ShiftJISEncoder {
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point or U+0080, return a
|
||||
// byte whose value is code point.
|
||||
if (isASCIICodePoint(code_point) || code_point === 0x0080)
|
||||
return code_point
|
||||
|
||||
// 3. If code point is U+00A5, return byte 0x5C.
|
||||
if (code_point === 0x00A5)
|
||||
return 0x5C
|
||||
|
||||
// 4. If code point is U+203E, return byte 0x7E.
|
||||
if (code_point === 0x203E)
|
||||
return 0x7E
|
||||
|
||||
// 5. If code point is in the range U+FF61 to U+FF9F, inclusive,
|
||||
// return a byte whose value is code point − 0xFF61 + 0xA1.
|
||||
if (inRange(code_point, 0xFF61, 0xFF9F))
|
||||
return code_point - 0xFF61 + 0xA1
|
||||
|
||||
// 6. If code point is U+2212, set it to U+FF0D.
|
||||
if (code_point === 0x2212)
|
||||
code_point = 0xFF0D
|
||||
|
||||
// 7. Let pointer be the index Shift_JIS pointer for code point.
|
||||
var pointer = indexShiftJISPointerFor(code_point)
|
||||
|
||||
// 8. If pointer is null, return error with code point.
|
||||
if (pointer === null)
|
||||
return encoderError(code_point)
|
||||
|
||||
// 9. Let lead be floor(pointer / 188).
|
||||
var lead = floor(pointer / 188)
|
||||
|
||||
// 10. Let lead offset be 0x81, if lead is less than 0x1F, and
|
||||
// 0xC1 otherwise.
|
||||
var lead_offset = (lead < 0x1F) ? 0x81 : 0xC1
|
||||
|
||||
// 11. Let trail be pointer % 188.
|
||||
var trail = pointer % 188
|
||||
|
||||
// 12. Let offset be 0x40, if trail is less than 0x3F, and 0x41
|
||||
// otherwise.
|
||||
var offset = (trail < 0x3F) ? 0x40 : 0x41
|
||||
|
||||
// 13. Return two bytes whose values are lead + lead offset and
|
||||
// trail + offset.
|
||||
return [lead + lead_offset, trail + offset]
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.ShiftJISDecoder = ShiftJISDecoder
|
||||
module.exports.ShiftJISEncoder = ShiftJISEncoder
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
const { end_of_stream, finished, isASCIIByte, decoderError, encoderError, isASCIICodePoint } = require('../utils');
|
||||
const { indexPointerFor } = require('../indexes');
|
||||
|
||||
//
|
||||
// 10. Legacy single-byte encodings
|
||||
//
|
||||
|
||||
// 10.1 single-byte decoder
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class SingleByteDecoder {
|
||||
/**
|
||||
* @param {!Array.<number>} index The encoding index.
|
||||
* @param {{fatal: boolean}} options
|
||||
*/
|
||||
constructor(index, options) {
|
||||
const { fatal } = options
|
||||
this.fatal = fatal
|
||||
this.index = index
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream, return finished.
|
||||
if (bite === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If byte is an ASCII byte, return a code point whose value
|
||||
// is byte.
|
||||
if (isASCIIByte(bite))
|
||||
return bite
|
||||
|
||||
// 3. Let code point be the index code point for byte − 0x80 in
|
||||
// index single-byte.
|
||||
var code_point = this.index[bite - 0x80]
|
||||
|
||||
// 4. If code point is null, return error.
|
||||
if (code_point === null)
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 5. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
}
|
||||
|
||||
// 10.2 single-byte encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class SingleByteEncoder {
|
||||
/**
|
||||
* @param {!Array.<?number>} index The encoding index.
|
||||
*/
|
||||
constructor(index) {
|
||||
this.index = index
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
* @return {(number|!Array.<number>)} Byte(s) to emit.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point, return a byte whose
|
||||
// value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 3. Let pointer be the index pointer for code point in index
|
||||
// single-byte.
|
||||
const pointer = indexPointerFor(code_point, this.index)
|
||||
|
||||
// 4. If pointer is null, return error with code point.
|
||||
if (pointer === null)
|
||||
encoderError(code_point)
|
||||
|
||||
// 5. Return a byte whose value is pointer + 0x80.
|
||||
return pointer + 0x80
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.SingleByteDecoder = SingleByteDecoder
|
||||
module.exports.SingleByteEncoder = SingleByteEncoder
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
const { inRange, decoderError, end_of_stream, finished, convertCodeUnitToBytes } = require('../utils');
|
||||
|
||||
// 15.2.1 shared utf-16 decoder
|
||||
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class UTF16Decoder {
|
||||
/**
|
||||
* @param {boolean} utf16_be True if big-endian, false if little-endian.
|
||||
* @param {{fatal: boolean}} options
|
||||
*/
|
||||
constructor(utf16_be, options) {
|
||||
const { fatal } = options
|
||||
this.utf16_be = utf16_be
|
||||
this.fatal = fatal
|
||||
this.utf16_lead_byte = null
|
||||
this.utf16_lead_surrogate = null
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream and either utf-16 lead byte or
|
||||
// utf-16 lead surrogate is not null, set utf-16 lead byte and
|
||||
// utf-16 lead surrogate to null, and return error.
|
||||
if (bite === end_of_stream && (this.utf16_lead_byte !== null ||
|
||||
this.utf16_lead_surrogate !== null)) {
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 2. If byte is end-of-stream and utf-16 lead byte and utf-16
|
||||
// lead surrogate are null, return finished.
|
||||
if (bite === end_of_stream && this.utf16_lead_byte === null &&
|
||||
this.utf16_lead_surrogate === null) {
|
||||
return finished
|
||||
}
|
||||
|
||||
// 3. If utf-16 lead byte is null, set utf-16 lead byte to byte
|
||||
// and return continue.
|
||||
if (this.utf16_lead_byte === null) {
|
||||
this.utf16_lead_byte = bite
|
||||
return null
|
||||
}
|
||||
|
||||
// 4. Let code unit be the result of:
|
||||
let code_unit
|
||||
if (this.utf16_be) {
|
||||
// utf-16be decoder flag is set
|
||||
// (utf-16 lead byte << 8) + byte.
|
||||
code_unit = (this.utf16_lead_byte << 8) + bite
|
||||
} else {
|
||||
// utf-16be decoder flag is unset
|
||||
// (byte << 8) + utf-16 lead byte.
|
||||
code_unit = (bite << 8) + this.utf16_lead_byte
|
||||
}
|
||||
// Then set utf-16 lead byte to null.
|
||||
this.utf16_lead_byte = null
|
||||
|
||||
// 5. If utf-16 lead surrogate is not null, let lead surrogate
|
||||
// be utf-16 lead surrogate, set utf-16 lead surrogate to null,
|
||||
// and then run these substeps:
|
||||
if (this.utf16_lead_surrogate !== null) {
|
||||
const lead_surrogate = this.utf16_lead_surrogate
|
||||
this.utf16_lead_surrogate = null
|
||||
|
||||
// 1. If code unit is in the range U+DC00 to U+DFFF,
|
||||
// inclusive, return a code point whose value is 0x10000 +
|
||||
// ((lead surrogate − 0xD800) << 10) + (code unit − 0xDC00).
|
||||
if (inRange(code_unit, 0xDC00, 0xDFFF)) {
|
||||
return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
|
||||
(code_unit - 0xDC00)
|
||||
}
|
||||
|
||||
// 2. Prepend the sequence resulting of converting code unit
|
||||
// to bytes using utf-16be decoder flag to stream and return
|
||||
// error.
|
||||
stream.prepend(convertCodeUnitToBytes(code_unit, this.utf16_be))
|
||||
return decoderError(this.fatal)
|
||||
}
|
||||
|
||||
// 6. If code unit is in the range U+D800 to U+DBFF, inclusive,
|
||||
// set utf-16 lead surrogate to code unit and return continue.
|
||||
if (inRange(code_unit, 0xD800, 0xDBFF)) {
|
||||
this.utf16_lead_surrogate = code_unit
|
||||
return null
|
||||
}
|
||||
|
||||
// 7. If code unit is in the range U+DC00 to U+DFFF, inclusive,
|
||||
// return error.
|
||||
if (inRange(code_unit, 0xDC00, 0xDFFF))
|
||||
return decoderError(this.fatal)
|
||||
|
||||
// 8. Return code point code unit.
|
||||
return code_unit
|
||||
}
|
||||
}
|
||||
|
||||
// 15.2.2 shared utf-16 encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class UTF16Encoder {
|
||||
/**
|
||||
* @param {boolean} [utf16_be] True if big-endian, false if little-endian.
|
||||
*/
|
||||
constructor(utf16_be = false) {
|
||||
this.utf16_be = utf16_be
|
||||
}
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is in the range U+0000 to U+FFFF, inclusive,
|
||||
// return the sequence resulting of converting code point to
|
||||
// bytes using utf-16be encoder flag.
|
||||
if (inRange(code_point, 0x0000, 0xFFFF))
|
||||
return convertCodeUnitToBytes(code_point, this.utf16_be)
|
||||
|
||||
// 3. Let lead be ((code point − 0x10000) >> 10) + 0xD800,
|
||||
// converted to bytes using utf-16be encoder flag.
|
||||
const lead = convertCodeUnitToBytes(
|
||||
((code_point - 0x10000) >> 10) + 0xD800, this.utf16_be)
|
||||
|
||||
// 4. Let trail be ((code point − 0x10000) & 0x3FF) + 0xDC00,
|
||||
// converted to bytes using utf-16be encoder flag.
|
||||
const trail = convertCodeUnitToBytes(
|
||||
((code_point - 0x10000) & 0x3FF) + 0xDC00, this.utf16_be)
|
||||
|
||||
// 5. Return a byte sequence of lead followed by trail.
|
||||
return lead.concat(trail)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.UTF16Decoder = UTF16Decoder
|
||||
module.exports.UTF16Encoder = UTF16Encoder
|
||||
+211
@@ -0,0 +1,211 @@
|
||||
const { inRange, decoderError, isASCIICodePoint,
|
||||
end_of_stream, finished } = require('../utils');
|
||||
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class UTF8Decoder {
|
||||
/**
|
||||
* @param {{fatal: boolean}} options
|
||||
*/
|
||||
constructor(options) {
|
||||
const { fatal } = options
|
||||
|
||||
// utf-8's decoder's has an associated utf-8 code point, utf-8
|
||||
// bytes seen, and utf-8 bytes needed (all initially 0), a utf-8
|
||||
// lower boundary (initially 0x80), and a utf-8 upper boundary
|
||||
// (initially 0xBF).
|
||||
let /** @type {number} */ utf8_code_point = 0,
|
||||
/** @type {number} */ utf8_bytes_seen = 0,
|
||||
/** @type {number} */ utf8_bytes_needed = 0,
|
||||
/** @type {number} */ utf8_lower_boundary = 0x80,
|
||||
/** @type {number} */ utf8_upper_boundary = 0xBF
|
||||
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
* @return {?(number|!Array.<number>)} The next code point(s)
|
||||
* decoded, or null if not enough data exists in the input
|
||||
* stream to decode a complete code point.
|
||||
*/
|
||||
this.handler = function(stream, bite) {
|
||||
// 1. If byte is end-of-stream and utf-8 bytes needed is not 0,
|
||||
// set utf-8 bytes needed to 0 and return error.
|
||||
if (bite === end_of_stream && utf8_bytes_needed !== 0) {
|
||||
utf8_bytes_needed = 0
|
||||
return decoderError(fatal)
|
||||
}
|
||||
|
||||
// 2. If byte is end-of-stream, return finished.
|
||||
if (bite === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 3. If utf-8 bytes needed is 0, based on byte:
|
||||
if (utf8_bytes_needed === 0) {
|
||||
// 0x00 to 0x7F
|
||||
if (inRange(bite, 0x00, 0x7F)) {
|
||||
// Return a code point whose value is byte.
|
||||
return bite
|
||||
}
|
||||
|
||||
// 0xC2 to 0xDF
|
||||
else if (inRange(bite, 0xC2, 0xDF)) {
|
||||
// 1. Set utf-8 bytes needed to 1.
|
||||
utf8_bytes_needed = 1
|
||||
|
||||
// 2. Set UTF-8 code point to byte & 0x1F.
|
||||
utf8_code_point = bite & 0x1F
|
||||
}
|
||||
|
||||
// 0xE0 to 0xEF
|
||||
else if (inRange(bite, 0xE0, 0xEF)) {
|
||||
// 1. If byte is 0xE0, set utf-8 lower boundary to 0xA0.
|
||||
if (bite === 0xE0)
|
||||
utf8_lower_boundary = 0xA0
|
||||
// 2. If byte is 0xED, set utf-8 upper boundary to 0x9F.
|
||||
if (bite === 0xED)
|
||||
utf8_upper_boundary = 0x9F
|
||||
// 3. Set utf-8 bytes needed to 2.
|
||||
utf8_bytes_needed = 2
|
||||
// 4. Set UTF-8 code point to byte & 0xF.
|
||||
utf8_code_point = bite & 0xF
|
||||
}
|
||||
|
||||
// 0xF0 to 0xF4
|
||||
else if (inRange(bite, 0xF0, 0xF4)) {
|
||||
// 1. If byte is 0xF0, set utf-8 lower boundary to 0x90.
|
||||
if (bite === 0xF0)
|
||||
utf8_lower_boundary = 0x90
|
||||
// 2. If byte is 0xF4, set utf-8 upper boundary to 0x8F.
|
||||
if (bite === 0xF4)
|
||||
utf8_upper_boundary = 0x8F
|
||||
// 3. Set utf-8 bytes needed to 3.
|
||||
utf8_bytes_needed = 3
|
||||
// 4. Set UTF-8 code point to byte & 0x7.
|
||||
utf8_code_point = bite & 0x7
|
||||
}
|
||||
|
||||
// Otherwise
|
||||
else {
|
||||
// Return error.
|
||||
return decoderError(fatal)
|
||||
}
|
||||
|
||||
// Return continue.
|
||||
return null
|
||||
}
|
||||
|
||||
// 4. If byte is not in the range utf-8 lower boundary to utf-8
|
||||
// upper boundary, inclusive, run these substeps:
|
||||
if (!inRange(bite, utf8_lower_boundary, utf8_upper_boundary)) {
|
||||
// 1. Set utf-8 code point, utf-8 bytes needed, and utf-8
|
||||
// bytes seen to 0, set utf-8 lower boundary to 0x80, and set
|
||||
// utf-8 upper boundary to 0xBF.
|
||||
utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0
|
||||
utf8_lower_boundary = 0x80
|
||||
utf8_upper_boundary = 0xBF
|
||||
|
||||
// 2. Prepend byte to stream.
|
||||
stream.prepend(bite)
|
||||
|
||||
// 3. Return error.
|
||||
return decoderError(fatal)
|
||||
}
|
||||
|
||||
// 5. Set utf-8 lower boundary to 0x80 and utf-8 upper boundary
|
||||
// to 0xBF.
|
||||
utf8_lower_boundary = 0x80
|
||||
utf8_upper_boundary = 0xBF
|
||||
|
||||
// 6. Set UTF-8 code point to (UTF-8 code point << 6) | (byte &
|
||||
// 0x3F)
|
||||
utf8_code_point = (utf8_code_point << 6) | (bite & 0x3F)
|
||||
|
||||
// 7. Increase utf-8 bytes seen by one.
|
||||
utf8_bytes_seen += 1
|
||||
|
||||
// 8. If utf-8 bytes seen is not equal to utf-8 bytes needed,
|
||||
// continue.
|
||||
if (utf8_bytes_seen !== utf8_bytes_needed)
|
||||
return null
|
||||
|
||||
// 9. Let code point be utf-8 code point.
|
||||
var code_point = utf8_code_point
|
||||
|
||||
// 10. Set utf-8 code point, utf-8 bytes needed, and utf-8 bytes
|
||||
// seen to 0.
|
||||
utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0
|
||||
|
||||
// 11. Return a code point whose value is code point.
|
||||
return code_point
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 9.1.2 utf-8 encoder
|
||||
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class UTF8Encoder {
|
||||
constructor() {
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
* @return {(number|!Array.<number>)} Byte(s) to emit.
|
||||
*/
|
||||
this.handler = function(stream, code_point) {
|
||||
// 1. If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point, return a byte whose
|
||||
// value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 3. Set count and offset based on the range code point is in:
|
||||
var count, offset
|
||||
// U+0080 to U+07FF, inclusive:
|
||||
if (inRange(code_point, 0x0080, 0x07FF)) {
|
||||
// 1 and 0xC0
|
||||
count = 1
|
||||
offset = 0xC0
|
||||
}
|
||||
// U+0800 to U+FFFF, inclusive:
|
||||
else if (inRange(code_point, 0x0800, 0xFFFF)) {
|
||||
// 2 and 0xE0
|
||||
count = 2
|
||||
offset = 0xE0
|
||||
}
|
||||
// U+10000 to U+10FFFF, inclusive:
|
||||
else if (inRange(code_point, 0x10000, 0x10FFFF)) {
|
||||
// 3 and 0xF0
|
||||
count = 3
|
||||
offset = 0xF0
|
||||
}
|
||||
|
||||
// 4. Let bytes be a byte sequence whose first byte is (code
|
||||
// point >> (6 × count)) + offset.
|
||||
var bytes = [(code_point >> (6 * count)) + offset]
|
||||
|
||||
// 5. Run these substeps while count is greater than 0:
|
||||
while (count > 0) {
|
||||
// 1. Set temp to code point >> (6 × (count − 1)).
|
||||
var temp = code_point >> (6 * (count - 1))
|
||||
|
||||
// 2. Append to bytes 0x80 | (temp & 0x3F).
|
||||
bytes.push(0x80 | (temp & 0x3F))
|
||||
|
||||
// 3. Decrease count by one.
|
||||
count -= 1
|
||||
}
|
||||
|
||||
// 6. Return bytes bytes, in order.
|
||||
return bytes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.UTF8Decoder = UTF8Decoder
|
||||
module.exports.UTF8Encoder = UTF8Encoder
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
const { inRange, encoderError, end_of_stream, finished, isASCIIByte, isASCIICodePoint } = require('../utils');
|
||||
|
||||
// 15.5 x-user-defined
|
||||
|
||||
// 15.5.1 x-user-defined decoder
|
||||
/**
|
||||
* @implements {Decoder}
|
||||
*/
|
||||
class XUserDefinedDecoder {
|
||||
/**
|
||||
* @param {Stream} stream The stream of bytes being decoded.
|
||||
* @param {number} bite The next byte read from the stream.
|
||||
*/
|
||||
handler(stream, bite) {
|
||||
// 1. If byte is end-of-stream, return finished.
|
||||
if (bite === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If byte is an ASCII byte, return a code point whose value
|
||||
// is byte.
|
||||
if (isASCIIByte(bite))
|
||||
return bite
|
||||
|
||||
// 3. Return a code point whose value is 0xF780 + byte − 0x80.
|
||||
return 0xF780 + bite - 0x80
|
||||
}
|
||||
}
|
||||
|
||||
// 15.5.2 x-user-defined encoder
|
||||
/**
|
||||
* @implements {Encoder}
|
||||
*/
|
||||
class XUserDefinedEncoder {
|
||||
/**
|
||||
* @param {Stream} stream Input stream.
|
||||
* @param {number} code_point Next code point read from the stream.
|
||||
*/
|
||||
handler(stream, code_point) {
|
||||
// 1.If code point is end-of-stream, return finished.
|
||||
if (code_point === end_of_stream)
|
||||
return finished
|
||||
|
||||
// 2. If code point is an ASCII code point, return a byte whose
|
||||
// value is code point.
|
||||
if (isASCIICodePoint(code_point))
|
||||
return code_point
|
||||
|
||||
// 3. If code point is in the range U+F780 to U+F7FF, inclusive,
|
||||
// return a byte whose value is code point − 0xF780 + 0x80.
|
||||
if (inRange(code_point, 0xF780, 0xF7FF))
|
||||
return code_point - 0xF780 + 0x80
|
||||
|
||||
// 4. Return error with code point.
|
||||
return encoderError(code_point)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.XUserDefinedDecoder = XUserDefinedDecoder
|
||||
module.exports.XUserDefinedEncoder = XUserDefinedEncoder
|
||||
Reference in New Issue
Block a user