Skip to content

Commit

Permalink
Add small helper to parse export file versions for #92
Browse files Browse the repository at this point in the history
  • Loading branch information
martinpaljak committed Mar 4, 2024
1 parent 1ddbba8 commit 521fef5
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions capfile/src/main/java/pro/javacard/sdk/ExportFileHelper.java
Original file line number Diff line number Diff line change
@@ -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<ExportFileVersion> 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);
}
}
}

0 comments on commit 521fef5

Please sign in to comment.