From 040245fe79b2bf1483a17d9ffbafe813a9f6407f Mon Sep 17 00:00:00 2001 From: Martin Paljak Date: Tue, 5 Mar 2024 00:00:42 +0200 Subject: [PATCH] Add small helper to parse export file versions for #92 --- .../pro/javacard/sdk/ExportFileHelper.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 capfile/src/main/java/pro/javacard/sdk/ExportFileHelper.java diff --git a/capfile/src/main/java/pro/javacard/sdk/ExportFileHelper.java b/capfile/src/main/java/pro/javacard/sdk/ExportFileHelper.java new file mode 100644 index 0000000..2958964 --- /dev/null +++ b/capfile/src/main/java/pro/javacard/sdk/ExportFileHelper.java @@ -0,0 +1,36 @@ +package pro.javacard.sdk; + +import java.io.DataInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; + +public final class ExportFileHelper { + + public enum ExportFileVersion { + V21, V23 + } + + private ExportFileHelper() { + } + + public static Optional getVersion(Path path) throws IOException { + try (DataInputStream dis = new DataInputStream(Files.newInputStream(path))) { + int magic = dis.readInt(); + + byte minor = dis.readByte(); + byte major = dis.readByte(); + + if (magic != 0x00FACADE) + return Optional.empty(); + if (major != 2) + throw new IOException("Invalid major version: " + major); + if (minor == 1) + return Optional.of(ExportFileVersion.V21); + if (minor == 3) + return Optional.of(ExportFileVersion.V23); + throw new IOException("Invalid minor version: " + minor); + } + } +}