readFile method

Future<Uint8List> readFile(
  1. int fid
)

Returns raw EF file bytes of selected DF identified by fid from MRTD. Can throw ICCError in case when file doesn't exist, read errors or SM session is not established but required to read file. Can throw ComProviderError in case connection with MRTD is lost.

Implementation

Future<Uint8List> readFile(final int fid) async {
  _log.debug("Reading file fid=0x${Utils.intToBin(fid).hex()}");
  if (fid > 0xFFFF) {
    throw MrtdApiError("Invalid fid=0x${Utils.intToBin(fid).hex()}");
  }

  // Select EF file first
  final efId = Uint8List(2);
  ByteData.view(efId.buffer).setUint16(0, fid);
  await icc.selectEF(efId: efId, p2: _defaultSelectP2);

  // Read chunk of file to obtain file length
  final chunk1 = await icc.readBinary(offset: 0, ne: _readAheadLength);
  final dtl = TLV.decodeTagAndLength(chunk1.data!);

  // Read the rest of the file
  final length = dtl.length.value - (chunk1.data!.length - dtl.encodedLen);
  final chunk2 =
      await _readBinary(offset: chunk1.data!.length, length: length);

  final rawFile = Uint8List.fromList(chunk1.data! + chunk2);
  assert(rawFile.length == dtl.encodedLen + dtl.length.value);
  return rawFile;
}