UNPKG

@codianz/emv-tools

Version:
186 lines 5.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.data_chunk = void 0; class data_chunk { constructor(data, offset, length) { this.m_offset = 0; this.m_length = 0; this.m_data = data; if (data) { this.m_offset = offset !== null && offset !== void 0 ? offset : 0; this.m_length = length !== null && length !== void 0 ? length : data.length; } } static invalid() { return new data_chunk(); } static string_to_uint8array(s) { const charcode_to_hex = (c) => { if (c >= 0x30 && c <= 0x39) { return c - 0x30; } else if (c >= 0x41 && c <= 0x46) { return c - 0x41 + 10; } else if (c >= 0x61 && c <= 0x66) { return c - 0x61 + 10; } return undefined; }; const r = new Uint8Array(s.length / 2); let wp = 0; for (let i = 0; i < s.length; i += 2) { const u = charcode_to_hex(s.charCodeAt(i)); const l = charcode_to_hex(s.charCodeAt(i + 1)); if (u === undefined || l === undefined) { break; } r[wp++] = (u << 4) | l; } return r.subarray(0, wp); } static create_from_hex_string(str) { const s = str.replace(/0x/g, "").replace(/\s/g, "").replace(/,/g, ""); if (s.length % 2 !== 0) { throw new Error("wrong length"); } const data = data_chunk.string_to_uint8array(s); return new data_chunk(data); } static create_from_hex_string_loosey(str) { const s = str.replace(/0x/g, "").replace(/\s/g, "").replace(/,/g, ""); const data = data_chunk.string_to_uint8array(s); return new data_chunk(data); } get valid() { return this.m_data !== undefined; } get empty() { return this.m_length === 0; } get size() { return this.m_length; } static create_from_uint8array(src, offset, length) { return new data_chunk(src, offset, length); } get_range(offset, length) { if (offset < 0 || offset >= this.m_length) { throw new Error("Out of range"); } if (length === undefined) { length = this.m_length - offset; } if (offset + length > this.m_length) { throw new Error("Out of range"); } return new data_chunk(this.m_data, this.m_offset + offset, length); } get_byte(offset) { if (!this.m_data) return undefined; if (offset < 0 || offset >= this.m_length) { return undefined; } return this.m_data[this.m_offset + offset]; } equals(other) { if (!this.m_data || !other.m_data) return false; if (this.m_data === other.m_data && this.m_offset === other.m_offset && this.m_length === other.m_length) { return true; } if (this.m_length !== other.m_length) { return false; } for (let i = 0; i < this.m_length; ++i) { if (this.m_data[this.m_offset + i] !== other.m_data[other.m_offset + i]) { return false; } } return true; } to_hex_string() { let s = ""; if (!this.m_data) return s; for (let i = 0; i < this.m_length; ++i) { s += ("0" + this.m_data[this.m_offset + i].toString(16)).slice(-2); } return s.toUpperCase(); } to_string() { let s = ""; if (!this.m_data) return s; for (let i = 0; i < this.m_length; ++i) { s += String.fromCharCode(this.m_data[this.m_offset + i]); } return s; } to_string_if_printable() { let s = ""; if (!this.m_data) return undefined; for (let i = 0; i < this.m_length; ++i) { const c = this.m_data[this.m_offset + i]; if (0x20 <= c && c <= 0x7E) { s += String.fromCharCode(this.m_data[this.m_offset + i]); } else { return undefined; } } return s; } to_uint8_array() { if (this.valid && this.m_data) { return this.m_data.subarray(this.m_offset, this.m_offset + this.m_length); } else { throw new Error("Invalid data"); } } to_uint8() { if (this.m_data === undefined) { throw new Error("Invalid data"); } return this.m_data[this.m_offset]; } to_uint16() { if (this.m_data === undefined) { throw new Error("Invalid data"); } return (this.m_data[this.m_offset] << 8) | this.m_data[this.m_offset + 1]; } to_uint32() { if (this.m_data === undefined) { throw new Error("Invalid data"); } return (this.m_data[this.m_offset] << 24) | (this.m_data[this.m_offset + 1] << 16) | (this.m_data[this.m_offset + 2] << 8) | this.m_data[this.m_offset + 3]; } [Symbol.iterator]() { let index = 0; const chunk = this; return { next() { if (chunk.m_data === undefined) { return { value: undefined, done: true }; } else if (index < chunk.m_length) { const value = chunk.m_data[chunk.m_offset + index]; index++; return { value, done: false }; } else { return { value: undefined, done: true }; } } }; } get iter() { return this[Symbol.iterator](); } } exports.data_chunk = data_chunk; //# sourceMappingURL=data_chunk.js.map