https://project.mdnd-it.cc/work_packages/94
This commit is contained in:
2025-08-23 04:25:28 +02:00
parent 725516ad6c
commit 19cfa031d0
25823 changed files with 1095587 additions and 2801760 deletions
@@ -0,0 +1,24 @@
import { Stream } from "../../common/Stream";
/**
* @constructor
* @implements {Decoder}
* @param {boolean} utf16_be True if big-endian, false if little-endian.
* @param {{fatal: boolean}} options
*/
export declare class UTF16Decoder {
private utf16_be;
readonly fatal: boolean;
utf16_lead_byte: any;
utf16_lead_surrogate: any;
constructor(utf16_be: boolean, options: {
fatal: boolean;
});
/**
* @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.
*/
handler(stream: Stream, bite: number): (number | Array<number>) | null;
}
@@ -0,0 +1,97 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var encodings_1 = require("../../encoding/encodings");
var finished_1 = require("../../encoding/finished");
var terminology_1 = require("../../encoding/terminology");
var utilities_1 = require("../../encoding/utilities");
var converCodeUnitToBytes_1 = require("./converCodeUnitToBytes");
/**
* @constructor
* @implements {Decoder}
* @param {boolean} utf16_be True if big-endian, false if little-endian.
* @param {{fatal: boolean}} options
*/
var UTF16Decoder = /** @class */ (function () {
function UTF16Decoder(utf16_be, options) {
this.utf16_be = utf16_be;
this.fatal = options.fatal;
/** @type {?number} */ this.utf16_lead_byte = null;
/** @type {?number} */ this.utf16_lead_surrogate = null;
}
/**
* @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.
*/
UTF16Decoder.prototype.handler = function (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 === terminology_1.end_of_stream && (this.utf16_lead_byte !== null ||
this.utf16_lead_surrogate !== null)) {
return encodings_1.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 === terminology_1.end_of_stream && this.utf16_lead_byte === null &&
this.utf16_lead_surrogate === null) {
return finished_1.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:
var 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) {
var 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 (utilities_1.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(converCodeUnitToBytes_1.convertCodeUnitToBytes(code_unit, this.utf16_be));
return encodings_1.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 (utilities_1.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 (utilities_1.inRange(code_unit, 0xDC00, 0xDFFF))
return encodings_1.decoderError(this.fatal);
// 8. Return code point code unit.
return code_unit;
};
return UTF16Decoder;
}());
exports.UTF16Decoder = UTF16Decoder;
//# sourceMappingURL=UTF16Decoder.js.map
@@ -0,0 +1 @@
{"version":3,"file":"UTF16Decoder.js","sourceRoot":"","sources":["../../../../src/coders/utf-16/UTF16Decoder.ts"],"names":[],"mappings":";;AACA,sDAAwD;AACxD,oDAAmD;AACnD,0DAA2D;AAC3D,sDAAmD;AACnD,iEAAiE;AAGjE;;;;;GAKG;AACH;IAOE,sBAAoB,QAAiB,EAAE,OAA4B;QAA/C,aAAQ,GAAR,QAAQ,CAAS;QACnC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;QACzB,sBAAsB,CAAC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;QACnD,sBAAsB,CAAC,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC;IAC5D,CAAC;IAED;;;;;;OAMG;IACH,8BAAO,GAAP,UAAQ,MAAc,EAAE,IAAY;QAClC,6DAA6D;QAC7D,8DAA8D;QAC9D,mDAAmD;QACnD,IAAI,IAAI,KAAK,2BAAa,IAAI,CAAC,IAAI,CAAC,eAAe,KAAK,IAAI;YAC1D,IAAI,CAAC,oBAAoB,KAAK,IAAI,CAAC,EAAE;YACrC,OAAO,wBAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SACjC;QAED,8DAA8D;QAC9D,4CAA4C;QAC5C,IAAI,IAAI,KAAK,2BAAa,IAAI,IAAI,CAAC,eAAe,KAAK,IAAI;YACzD,IAAI,CAAC,oBAAoB,KAAK,IAAI,EAAE;YACpC,OAAO,mBAAQ,CAAC;SACjB;QAED,+DAA+D;QAC/D,uBAAuB;QACvB,IAAI,IAAI,CAAC,eAAe,KAAK,IAAI,EAAE;YACjC,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC;YAC5B,OAAO,IAAI,CAAC;SACb;QAED,qCAAqC;QACrC,IAAI,SAAiB,CAAC;QACtB,IAAI,IAAI,CAAC,QAAQ,E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ElC,kCAAkC;QAClC,OAAO,SAAS,CAAC;IACnB,CAAC;IACH,mBAAC;AAAD,CAAC,AA9FD,IA8FC;AA9FY,oCAAY"}
@@ -0,0 +1,20 @@
import { Stream } from "../../common";
/**
* @constructor
* @implements {Encoder}
* @param {boolean} utf16_be True if big-endian, false if little-endian.
* @param {{fatal: boolean}} options
*/
export declare class UTF16Encoder {
private utf16_be;
readonly fatal: boolean;
constructor(utf16_be: boolean, options: {
fatal: boolean;
});
/**
* @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: Stream, code_point: number): (number | Array<number>);
}
@@ -0,0 +1,44 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var finished_1 = require("../../encoding/finished");
var terminology_1 = require("../../encoding/terminology");
var utilities_1 = require("../../encoding/utilities");
var converCodeUnitToBytes_1 = require("./converCodeUnitToBytes");
/**
* @constructor
* @implements {Encoder}
* @param {boolean} utf16_be True if big-endian, false if little-endian.
* @param {{fatal: boolean}} options
*/
var UTF16Encoder = /** @class */ (function () {
function UTF16Encoder(utf16_be, options) {
this.utf16_be = utf16_be;
this.fatal = options.fatal;
}
/**
* @param {Stream} stream Input stream.
* @param {number} code_point Next code point read from the stream.
* @return {(number|!Array.<number>)} Byte(s) to emit.
*/
UTF16Encoder.prototype.handler = function (stream, code_point) {
// 1. If code point is end-of-stream, return finished.
if (code_point === terminology_1.end_of_stream)
return finished_1.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 (utilities_1.inRange(code_point, 0x0000, 0xFFFF))
return converCodeUnitToBytes_1.convertCodeUnitToBytes(code_point, this.utf16_be);
// 3. Let lead be ((code point 0x10000) >> 10) + 0xD800,
// converted to bytes using utf-16be encoder flag.
var lead = converCodeUnitToBytes_1.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.
var trail = converCodeUnitToBytes_1.convertCodeUnitToBytes(((code_point - 0x10000) & 0x3FF) + 0xDC00, this.utf16_be);
// 5. Return a byte sequence of lead followed by trail.
return lead.concat(trail);
};
return UTF16Encoder;
}());
exports.UTF16Encoder = UTF16Encoder;
//# sourceMappingURL=UTF16Encoder.js.map
@@ -0,0 +1 @@
{"version":3,"file":"UTF16Encoder.js","sourceRoot":"","sources":["../../../../src/coders/utf-16/UTF16Encoder.ts"],"names":[],"mappings":";;AACA,oDAAmD;AACnD,0DAA2D;AAC3D,sDAAmD;AACnD,iEAAiE;AAEjE;;;;;GAKG;AACH;IAIE,sBAAoB,QAAiB,EAAE,OAA4B;QAA/C,aAAQ,GAAR,QAAQ,CAAS;QACnC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IAC7B,CAAC;IAED;;;;OAIG;IACH,8BAAO,GAAP,UAAQ,MAAc,EAAE,UAAkB;QACxC,sDAAsD;QACtD,IAAI,UAAU,KAAK,2BAAa;YAC9B,OAAO,mBAAQ,CAAC;QAElB,gEAAgE;QAChE,4DAA4D;QAC5D,qCAAqC;QACrC,IAAI,mBAAO,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,CAAC;YACrC,OAAO,8CAAsB,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAE3D,0DAA0D;QAC1D,kDAAkD;QAClD,IAAM,IAAI,GAAG,8CAAsB,CACjC,CAAC,CAAC,UAAU,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,GAAG,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAE1D,6DAA6D;QAC7D,kDAAkD;QAClD,IAAM,KAAK,GAAG,8CAAsB,CAClC,CAAC,CAAC,UAAU,GAAG,OAAO,CAAC,GAAG,KAAK,CAAC,GAAG,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAE5D,uDAAuD;QACvD,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC5B,CAAC;IACH,mBAAC;AAAD,CAAC,AArCD,IAqCC;AArCY,oCAAY"}
@@ -0,0 +1,6 @@
/**
* @param {number} code_unit
* @param {boolean} utf16be
* @return {!Array.<number>} bytes
*/
export declare function convertCodeUnitToBytes(code_unit: number, utf16be: boolean): Array<number>;
@@ -0,0 +1,21 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @param {number} code_unit
* @param {boolean} utf16be
* @return {!Array.<number>} bytes
*/
function convertCodeUnitToBytes(code_unit, utf16be) {
// 1. Let byte1 be code unit >> 8.
var byte1 = code_unit >> 8;
// 2. Let byte2 be code unit & 0x00FF.
var byte2 = code_unit & 0x00FF;
// 3. Then return the bytes in order:
// utf-16be flag is set: byte1, then byte2.
if (utf16be)
return [byte1, byte2];
// utf-16be flag is unset: byte2, then byte1.
return [byte2, byte1];
}
exports.convertCodeUnitToBytes = convertCodeUnitToBytes;
//# sourceMappingURL=converCodeUnitToBytes.js.map
@@ -0,0 +1 @@
{"version":3,"file":"converCodeUnitToBytes.js","sourceRoot":"","sources":["../../../../src/coders/utf-16/converCodeUnitToBytes.ts"],"names":[],"mappings":";;AAAA;;;;GAIG;AACH,SAAgB,sBAAsB,CAAC,SAAiB,EAAE,OAAgB;IACxE,kCAAkC;IAClC,IAAM,KAAK,GAAG,SAAS,IAAI,CAAC,CAAC;IAE7B,sCAAsC;IACtC,IAAM,KAAK,GAAG,SAAS,GAAG,MAAM,CAAC;IAEjC,qCAAqC;IACrC,2CAA2C;IAC3C,IAAI,OAAO;QACT,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACxB,6CAA6C;IAC7C,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AACxB,CAAC;AAbD,wDAaC"}
@@ -0,0 +1,2 @@
export * from './UTF16Decoder';
export * from './UTF16Encoder';
@@ -0,0 +1,8 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./UTF16Decoder"));
__export(require("./UTF16Encoder"));
//# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/coders/utf-16/index.ts"],"names":[],"mappings":";;;;;AAAA,oCAA+B;AAC/B,oCAA+B"}