init
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
import { FieldInstance } from '../enums';
|
||||
import { SerializedType } from '../types/serialized-type';
|
||||
import { Buffer } from 'buffer/';
|
||||
/**
|
||||
* BinaryParser is used to compute fields and values from a HexString
|
||||
*/
|
||||
declare class BinaryParser {
|
||||
private bytes;
|
||||
/**
|
||||
* Initialize bytes to a hex string
|
||||
*
|
||||
* @param hexBytes a hex string
|
||||
*/
|
||||
constructor(hexBytes: string);
|
||||
/**
|
||||
* Peek the first byte of the BinaryParser
|
||||
*
|
||||
* @returns The first byte of the BinaryParser
|
||||
*/
|
||||
peek(): number;
|
||||
/**
|
||||
* Consume the first n bytes of the BinaryParser
|
||||
*
|
||||
* @param n the number of bytes to skip
|
||||
*/
|
||||
skip(n: number): void;
|
||||
/**
|
||||
* read the first n bytes from the BinaryParser
|
||||
*
|
||||
* @param n The number of bytes to read
|
||||
* @return The bytes
|
||||
*/
|
||||
read(n: number): Buffer;
|
||||
/**
|
||||
* Read an integer of given size
|
||||
*
|
||||
* @param n The number of bytes to read
|
||||
* @return The number represented by those bytes
|
||||
*/
|
||||
readUIntN(n: number): number;
|
||||
readUInt8(): number;
|
||||
readUInt16(): number;
|
||||
readUInt32(): number;
|
||||
size(): number;
|
||||
end(customEnd?: number): boolean;
|
||||
/**
|
||||
* Reads variable length encoded bytes
|
||||
*
|
||||
* @return The variable length bytes
|
||||
*/
|
||||
readVariableLength(): Buffer;
|
||||
/**
|
||||
* Reads the length of the variable length encoded bytes
|
||||
*
|
||||
* @return The length of the variable length encoded bytes
|
||||
*/
|
||||
readVariableLengthLength(): number;
|
||||
/**
|
||||
* Reads the field ordinal from the BinaryParser
|
||||
*
|
||||
* @return Field ordinal
|
||||
*/
|
||||
readFieldOrdinal(): number;
|
||||
/**
|
||||
* Read the field from the BinaryParser
|
||||
*
|
||||
* @return The field represented by the bytes at the head of the BinaryParser
|
||||
*/
|
||||
readField(): FieldInstance;
|
||||
/**
|
||||
* Read a given type from the BinaryParser
|
||||
*
|
||||
* @param type The type that you want to read from the BinaryParser
|
||||
* @return The instance of that type read from the BinaryParser
|
||||
*/
|
||||
readType(type: typeof SerializedType): SerializedType;
|
||||
/**
|
||||
* Get the type associated with a given field
|
||||
*
|
||||
* @param field The field that you wan to get the type of
|
||||
* @return The type associated with the given field
|
||||
*/
|
||||
typeForField(field: FieldInstance): typeof SerializedType;
|
||||
/**
|
||||
* Read value of the type specified by field from the BinaryParser
|
||||
*
|
||||
* @param field The field that you want to get the associated value for
|
||||
* @return The value associated with the given field
|
||||
*/
|
||||
readFieldValue(field: FieldInstance): SerializedType;
|
||||
/**
|
||||
* Get the next field and value from the BinaryParser
|
||||
*
|
||||
* @return The field and value
|
||||
*/
|
||||
readFieldAndValue(): [FieldInstance, SerializedType];
|
||||
}
|
||||
export { BinaryParser };
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.BinaryParser = void 0;
|
||||
const assert = __importStar(require("assert"));
|
||||
const enums_1 = require("../enums");
|
||||
const buffer_1 = require("buffer/");
|
||||
/**
|
||||
* BinaryParser is used to compute fields and values from a HexString
|
||||
*/
|
||||
class BinaryParser {
|
||||
/**
|
||||
* Initialize bytes to a hex string
|
||||
*
|
||||
* @param hexBytes a hex string
|
||||
*/
|
||||
constructor(hexBytes) {
|
||||
this.bytes = buffer_1.Buffer.from(hexBytes, 'hex');
|
||||
}
|
||||
/**
|
||||
* Peek the first byte of the BinaryParser
|
||||
*
|
||||
* @returns The first byte of the BinaryParser
|
||||
*/
|
||||
peek() {
|
||||
assert.ok(this.bytes.byteLength !== 0);
|
||||
return this.bytes[0];
|
||||
}
|
||||
/**
|
||||
* Consume the first n bytes of the BinaryParser
|
||||
*
|
||||
* @param n the number of bytes to skip
|
||||
*/
|
||||
skip(n) {
|
||||
assert.ok(n <= this.bytes.byteLength);
|
||||
this.bytes = this.bytes.slice(n);
|
||||
}
|
||||
/**
|
||||
* read the first n bytes from the BinaryParser
|
||||
*
|
||||
* @param n The number of bytes to read
|
||||
* @return The bytes
|
||||
*/
|
||||
read(n) {
|
||||
assert.ok(n <= this.bytes.byteLength);
|
||||
const slice = this.bytes.slice(0, n);
|
||||
this.skip(n);
|
||||
return slice;
|
||||
}
|
||||
/**
|
||||
* Read an integer of given size
|
||||
*
|
||||
* @param n The number of bytes to read
|
||||
* @return The number represented by those bytes
|
||||
*/
|
||||
readUIntN(n) {
|
||||
assert.ok(0 < n && n <= 4, 'invalid n');
|
||||
return this.read(n).reduce((a, b) => (a << 8) | b) >>> 0;
|
||||
}
|
||||
readUInt8() {
|
||||
return this.readUIntN(1);
|
||||
}
|
||||
readUInt16() {
|
||||
return this.readUIntN(2);
|
||||
}
|
||||
readUInt32() {
|
||||
return this.readUIntN(4);
|
||||
}
|
||||
size() {
|
||||
return this.bytes.byteLength;
|
||||
}
|
||||
end(customEnd) {
|
||||
const length = this.bytes.byteLength;
|
||||
return length === 0 || (customEnd !== undefined && length <= customEnd);
|
||||
}
|
||||
/**
|
||||
* Reads variable length encoded bytes
|
||||
*
|
||||
* @return The variable length bytes
|
||||
*/
|
||||
readVariableLength() {
|
||||
return this.read(this.readVariableLengthLength());
|
||||
}
|
||||
/**
|
||||
* Reads the length of the variable length encoded bytes
|
||||
*
|
||||
* @return The length of the variable length encoded bytes
|
||||
*/
|
||||
readVariableLengthLength() {
|
||||
const b1 = this.readUInt8();
|
||||
if (b1 <= 192) {
|
||||
return b1;
|
||||
}
|
||||
else if (b1 <= 240) {
|
||||
const b2 = this.readUInt8();
|
||||
return 193 + (b1 - 193) * 256 + b2;
|
||||
}
|
||||
else if (b1 <= 254) {
|
||||
const b2 = this.readUInt8();
|
||||
const b3 = this.readUInt8();
|
||||
return 12481 + (b1 - 241) * 65536 + b2 * 256 + b3;
|
||||
}
|
||||
throw new Error('Invalid variable length indicator');
|
||||
}
|
||||
/**
|
||||
* Reads the field ordinal from the BinaryParser
|
||||
*
|
||||
* @return Field ordinal
|
||||
*/
|
||||
readFieldOrdinal() {
|
||||
let type = this.readUInt8();
|
||||
let nth = type & 15;
|
||||
type >>= 4;
|
||||
if (type === 0) {
|
||||
type = this.readUInt8();
|
||||
if (type === 0 || type < 16) {
|
||||
throw new Error('Cannot read FieldOrdinal, type_code out of range');
|
||||
}
|
||||
}
|
||||
if (nth === 0) {
|
||||
nth = this.readUInt8();
|
||||
if (nth === 0 || nth < 16) {
|
||||
throw new Error('Cannot read FieldOrdinal, field_code out of range');
|
||||
}
|
||||
}
|
||||
return (type << 16) | nth;
|
||||
}
|
||||
/**
|
||||
* Read the field from the BinaryParser
|
||||
*
|
||||
* @return The field represented by the bytes at the head of the BinaryParser
|
||||
*/
|
||||
readField() {
|
||||
return enums_1.Field.fromString(this.readFieldOrdinal().toString());
|
||||
}
|
||||
/**
|
||||
* Read a given type from the BinaryParser
|
||||
*
|
||||
* @param type The type that you want to read from the BinaryParser
|
||||
* @return The instance of that type read from the BinaryParser
|
||||
*/
|
||||
readType(type) {
|
||||
return type.fromParser(this);
|
||||
}
|
||||
/**
|
||||
* Get the type associated with a given field
|
||||
*
|
||||
* @param field The field that you wan to get the type of
|
||||
* @return The type associated with the given field
|
||||
*/
|
||||
typeForField(field) {
|
||||
return field.associatedType;
|
||||
}
|
||||
/**
|
||||
* Read value of the type specified by field from the BinaryParser
|
||||
*
|
||||
* @param field The field that you want to get the associated value for
|
||||
* @return The value associated with the given field
|
||||
*/
|
||||
readFieldValue(field) {
|
||||
const type = this.typeForField(field);
|
||||
if (!type) {
|
||||
throw new Error(`unsupported: (${field.name}, ${field.type.name})`);
|
||||
}
|
||||
const sizeHint = field.isVariableLengthEncoded
|
||||
? this.readVariableLengthLength()
|
||||
: undefined;
|
||||
const value = type.fromParser(this, sizeHint);
|
||||
if (value === undefined) {
|
||||
throw new Error(`fromParser for (${field.name}, ${field.type.name}) -> undefined `);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
/**
|
||||
* Get the next field and value from the BinaryParser
|
||||
*
|
||||
* @return The field and value
|
||||
*/
|
||||
readFieldAndValue() {
|
||||
const field = this.readField();
|
||||
return [field, this.readFieldValue(field)];
|
||||
}
|
||||
}
|
||||
exports.BinaryParser = BinaryParser;
|
||||
//# sourceMappingURL=binary-parser.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"binary-parser.js","sourceRoot":"","sources":["../../src/serdes/binary-parser.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAgC;AAChC,oCAA+C;AAE/C,oCAAgC;AAEhC;;GAEG;AACH,MAAM,YAAY;IAGhB;;;;OAIG;IACH,YAAY,QAAgB;QAC1B,IAAI,CAAC,KAAK,GAAG,eAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;IAC3C,CAAC;IAED;;;;OAIG;IACH,IAAI;QACF,MAAM,CAAC,EAAE,CAAC,IAA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,CACb,mBAAmB,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,IAAI,iBAAiB,CACnE,CAAA;SACF;QACD,OAAO,KAAK,CAAA;IACd,CAAC;IAED;;;;OAIG;IACH,iBAAiB;QACf,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,EAAE,CAAA;QAC9B,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,CAAA;IAC5C,CAAC;CACF;AAEQ,oCAAY"}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
import { FieldInstance } from '../enums';
|
||||
import { SerializedType } from '../types/serialized-type';
|
||||
import { Buffer } from 'buffer/';
|
||||
/**
|
||||
* Bytes list is a collection of buffer objects
|
||||
*/
|
||||
declare class BytesList {
|
||||
private bytesArray;
|
||||
/**
|
||||
* Get the total number of bytes in the BytesList
|
||||
*
|
||||
* @return the number of bytes
|
||||
*/
|
||||
getLength(): number;
|
||||
/**
|
||||
* Put bytes in the BytesList
|
||||
*
|
||||
* @param bytesArg A Buffer
|
||||
* @return this BytesList
|
||||
*/
|
||||
put(bytesArg: Buffer): BytesList;
|
||||
/**
|
||||
* Write this BytesList to the back of another bytes list
|
||||
*
|
||||
* @param list The BytesList to write to
|
||||
*/
|
||||
toBytesSink(list: BytesList): void;
|
||||
toBytes(): Buffer;
|
||||
toHex(): string;
|
||||
}
|
||||
/**
|
||||
* BinarySerializer is used to write fields and values to buffers
|
||||
*/
|
||||
declare class BinarySerializer {
|
||||
private sink;
|
||||
constructor(sink: BytesList);
|
||||
/**
|
||||
* Write a value to this BinarySerializer
|
||||
*
|
||||
* @param value a SerializedType value
|
||||
*/
|
||||
write(value: SerializedType): void;
|
||||
/**
|
||||
* Write bytes to this BinarySerializer
|
||||
*
|
||||
* @param bytes the bytes to write
|
||||
*/
|
||||
put(bytes: Buffer): void;
|
||||
/**
|
||||
* Write a value of a given type to this BinarySerializer
|
||||
*
|
||||
* @param type the type to write
|
||||
* @param value a value of that type
|
||||
*/
|
||||
writeType(type: typeof SerializedType, value: SerializedType): void;
|
||||
/**
|
||||
* Write BytesList to this BinarySerializer
|
||||
*
|
||||
* @param bl BytesList to write to BinarySerializer
|
||||
*/
|
||||
writeBytesList(bl: BytesList): void;
|
||||
/**
|
||||
* Calculate the header of Variable Length encoded bytes
|
||||
*
|
||||
* @param length the length of the bytes
|
||||
*/
|
||||
private encodeVariableLength;
|
||||
/**
|
||||
* Write field and value to BinarySerializer
|
||||
*
|
||||
* @param field field to write to BinarySerializer
|
||||
* @param value value to write to BinarySerializer
|
||||
*/
|
||||
writeFieldAndValue(field: FieldInstance, value: SerializedType, isUnlModifyWorkaround?: boolean): void;
|
||||
/**
|
||||
* Write a variable length encoded value to the BinarySerializer
|
||||
*
|
||||
* @param value length encoded value to write to BytesList
|
||||
*/
|
||||
writeLengthEncoded(value: SerializedType, isUnlModifyWorkaround?: boolean): void;
|
||||
}
|
||||
export { BytesList, BinarySerializer };
|
||||
+172
@@ -0,0 +1,172 @@
|
||||
"use strict";
|
||||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||||
}
|
||||
Object.defineProperty(o, k2, desc);
|
||||
}) : (function(o, m, k, k2) {
|
||||
if (k2 === undefined) k2 = k;
|
||||
o[k2] = m[k];
|
||||
}));
|
||||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||||
}) : function(o, v) {
|
||||
o["default"] = v;
|
||||
});
|
||||
var __importStar = (this && this.__importStar) || function (mod) {
|
||||
if (mod && mod.__esModule) return mod;
|
||||
var result = {};
|
||||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.BinarySerializer = exports.BytesList = void 0;
|
||||
const assert = __importStar(require("assert"));
|
||||
const buffer_1 = require("buffer/");
|
||||
/**
|
||||
* Bytes list is a collection of buffer objects
|
||||
*/
|
||||
class BytesList {
|
||||
constructor() {
|
||||
this.bytesArray = [];
|
||||
}
|
||||
/**
|
||||
* Get the total number of bytes in the BytesList
|
||||
*
|
||||
* @return the number of bytes
|
||||
*/
|
||||
getLength() {
|
||||
return buffer_1.Buffer.concat(this.bytesArray).byteLength;
|
||||
}
|
||||
/**
|
||||
* Put bytes in the BytesList
|
||||
*
|
||||
* @param bytesArg A Buffer
|
||||
* @return this BytesList
|
||||
*/
|
||||
put(bytesArg) {
|
||||
const bytes = buffer_1.Buffer.from(bytesArg); // Temporary, to catch instances of Uint8Array being passed in
|
||||
this.bytesArray.push(bytes);
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Write this BytesList to the back of another bytes list
|
||||
*
|
||||
* @param list The BytesList to write to
|
||||
*/
|
||||
toBytesSink(list) {
|
||||
list.put(this.toBytes());
|
||||
}
|
||||
toBytes() {
|
||||
return buffer_1.Buffer.concat(this.bytesArray);
|
||||
}
|
||||
toHex() {
|
||||
return this.toBytes().toString('hex').toUpperCase();
|
||||
}
|
||||
}
|
||||
exports.BytesList = BytesList;
|
||||
/**
|
||||
* BinarySerializer is used to write fields and values to buffers
|
||||
*/
|
||||
class BinarySerializer {
|
||||
constructor(sink) {
|
||||
this.sink = new BytesList();
|
||||
this.sink = sink;
|
||||
}
|
||||
/**
|
||||
* Write a value to this BinarySerializer
|
||||
*
|
||||
* @param value a SerializedType value
|
||||
*/
|
||||
write(value) {
|
||||
value.toBytesSink(this.sink);
|
||||
}
|
||||
/**
|
||||
* Write bytes to this BinarySerializer
|
||||
*
|
||||
* @param bytes the bytes to write
|
||||
*/
|
||||
put(bytes) {
|
||||
this.sink.put(bytes);
|
||||
}
|
||||
/**
|
||||
* Write a value of a given type to this BinarySerializer
|
||||
*
|
||||
* @param type the type to write
|
||||
* @param value a value of that type
|
||||
*/
|
||||
writeType(type, value) {
|
||||
this.write(type.from(value));
|
||||
}
|
||||
/**
|
||||
* Write BytesList to this BinarySerializer
|
||||
*
|
||||
* @param bl BytesList to write to BinarySerializer
|
||||
*/
|
||||
writeBytesList(bl) {
|
||||
bl.toBytesSink(this.sink);
|
||||
}
|
||||
/**
|
||||
* Calculate the header of Variable Length encoded bytes
|
||||
*
|
||||
* @param length the length of the bytes
|
||||
*/
|
||||
encodeVariableLength(length) {
|
||||
const lenBytes = buffer_1.Buffer.alloc(3);
|
||||
if (length <= 192) {
|
||||
lenBytes[0] = length;
|
||||
return lenBytes.slice(0, 1);
|
||||
}
|
||||
else if (length <= 12480) {
|
||||
length -= 193;
|
||||
lenBytes[0] = 193 + (length >>> 8);
|
||||
lenBytes[1] = length & 0xff;
|
||||
return lenBytes.slice(0, 2);
|
||||
}
|
||||
else if (length <= 918744) {
|
||||
length -= 12481;
|
||||
lenBytes[0] = 241 + (length >>> 16);
|
||||
lenBytes[1] = (length >> 8) & 0xff;
|
||||
lenBytes[2] = length & 0xff;
|
||||
return lenBytes.slice(0, 3);
|
||||
}
|
||||
throw new Error('Overflow error');
|
||||
}
|
||||
/**
|
||||
* Write field and value to BinarySerializer
|
||||
*
|
||||
* @param field field to write to BinarySerializer
|
||||
* @param value value to write to BinarySerializer
|
||||
*/
|
||||
writeFieldAndValue(field, value, isUnlModifyWorkaround = false) {
|
||||
const associatedValue = field.associatedType.from(value);
|
||||
assert.ok(associatedValue.toBytesSink !== undefined);
|
||||
assert.ok(field.name !== undefined);
|
||||
this.sink.put(field.header);
|
||||
if (field.isVariableLengthEncoded) {
|
||||
this.writeLengthEncoded(associatedValue, isUnlModifyWorkaround);
|
||||
}
|
||||
else {
|
||||
associatedValue.toBytesSink(this.sink);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Write a variable length encoded value to the BinarySerializer
|
||||
*
|
||||
* @param value length encoded value to write to BytesList
|
||||
*/
|
||||
writeLengthEncoded(value, isUnlModifyWorkaround = false) {
|
||||
const bytes = new BytesList();
|
||||
if (!isUnlModifyWorkaround) {
|
||||
// this part doesn't happen for the Account field in a UNLModify transaction
|
||||
value.toBytesSink(bytes);
|
||||
}
|
||||
this.put(this.encodeVariableLength(bytes.getLength()));
|
||||
this.writeBytesList(bytes);
|
||||
}
|
||||
}
|
||||
exports.BinarySerializer = BinarySerializer;
|
||||
//# sourceMappingURL=binary-serializer.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"binary-serializer.js","sourceRoot":"","sources":["../../src/serdes/binary-serializer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAgC;AAGhC,oCAAgC;AAEhC;;GAEG;AACH,MAAM,SAAS;IAAf;QACU,eAAU,GAAkB,EAAE,CAAA;IAuCxC,CAAC;IArCC;;;;OAIG;IACI,SAAS;QACd,OAAO,eAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,UAAU,CAAA;IAClD,CAAC;IAED;;;;;OAKG;IACI,GAAG,CAAC,QAAgB;QACzB,MAAM,KAAK,GAAG,eAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA,CAAC,8DAA8D;QAClG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC3B,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;;;OAIG;IACI,WAAW,CAAC,IAAe;QAChC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAA;IAC1B,CAAC;IAEM,OAAO;QACZ,OAAO,eAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IACvC,CAAC;IAED,KAAK;QACH,OAAO,IAAI,CAAC,OAAO,EAAE,CA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