@li0ard/tsemrtd
Version:
simple library for eMRTD. supports browsers, node, bun and more!
31 lines (30 loc) • 1.02 kB
JavaScript
import { TLV } from "@li0ard/tinytlv";
import { Enums } from "./index";
/**
* Class for working with DG5 (Displayed image)
*/
export class DG5 {
readImage(tlv) {
if (parseInt(tlv.tag, 16) != 0x5f40)
throw new Error(`Invalid object tag "0x${tlv.tag}", expected 0x5f40`);
return tlv.byteValue;
}
/**
* Get displayed image
* @param data Data of EF.DG5 file
*/
static load(data) {
let tlv = TLV.parse(data);
if (parseInt(tlv.tag, 16) != Enums.TAGS.DG5)
throw new Error(`Invalid DG5 tag "0x${tlv.tag}", expected 0x${Enums.TAGS.DG5.toString(16)}`);
let bict = tlv.childs[0];
if (parseInt(bict.tag, 16) != 0x02)
throw new Error(`Invalid object tag "0x${bict.tag}", expected 0x02`);
let bitCount = parseInt(bict.value, 16);
let results = [];
for (let i = 0; i < bitCount; i++) {
results.push(new DG5().readImage(tlv.childs[i + 1]));
}
return results;
}
}