Merge pull request #100 from ThomasFok/fix/validate_coordinate

acx - validation of the track/sector/block parameters of read/write/dump commands
This commit is contained in:
A2 Geek 2023-02-19 17:21:55 -06:00 committed by GitHub
commit 60e30a962b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,6 +20,7 @@
package io.github.applecommander.acx.arggroup;
import com.webcodepro.applecommander.storage.Disk;
import com.webcodepro.applecommander.storage.physical.ImageOrder;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Option;
@ -69,10 +70,29 @@ public class CoordinateSelection {
public boolean isBootSector() {
return track == 0 && sector == 0;
}
public void validateTrackAndSector(Disk disk) throws IllegalArgumentException {
final int tracksPerDisk = disk.getImageOrder().getTracksPerDisk();
final int sectorsPerTrack = disk.getImageOrder().getSectorsPerTrack();
if (track < 0 || track >= tracksPerDisk) {
String errormsg = String.format("The track number(%d) is out of range(0-%d) on this image.", track, tracksPerDisk-1);
throw new IllegalArgumentException(errormsg);
}
if (sector < 0 || sector >= sectorsPerTrack) {
String errormsg = String.format("The sector number(%d) is out of range(0-%d) on this image.", sector, sectorsPerTrack-1);
throw new IllegalArgumentException(errormsg);
}
}
public byte[] read(Disk disk) {
validateTrackAndSector(disk);
return disk.readSector(track, sector);
}
public void write(Disk disk, byte[] data) {
validateTrackAndSector(disk);
disk.writeSector(track, sector, data);
}
}
@ -83,10 +103,23 @@ public class CoordinateSelection {
public boolean isBootBlock() {
return block == 0;
}
public void validateBlockNum(Disk disk) throws IllegalArgumentException {
final int blocksOnDevice = disk.getImageOrder().getBlocksOnDevice();
if (block < 0 || block >= blocksOnDevice) {
String errormsg = String.format("The block number(%d) is out of range(0-%d) on this image.", block, blocksOnDevice-1);
throw new IllegalArgumentException(errormsg);
}
}
public byte[] read(Disk disk) {
validateBlockNum(disk);
return disk.readBlock(block);
}
public void write(Disk disk, byte[] data) {
validateBlockNum(disk);
disk.writeBlock(block, data);
}
}