mirror of
https://github.com/AppleCommander/AppleCommander.git
synced 2024-12-22 08:30:35 +00:00
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:
commit
60e30a962b
@ -20,6 +20,7 @@
|
|||||||
package io.github.applecommander.acx.arggroup;
|
package io.github.applecommander.acx.arggroup;
|
||||||
|
|
||||||
import com.webcodepro.applecommander.storage.Disk;
|
import com.webcodepro.applecommander.storage.Disk;
|
||||||
|
import com.webcodepro.applecommander.storage.physical.ImageOrder;
|
||||||
|
|
||||||
import picocli.CommandLine.ArgGroup;
|
import picocli.CommandLine.ArgGroup;
|
||||||
import picocli.CommandLine.Option;
|
import picocli.CommandLine.Option;
|
||||||
@ -69,10 +70,29 @@ public class CoordinateSelection {
|
|||||||
public boolean isBootSector() {
|
public boolean isBootSector() {
|
||||||
return track == 0 && sector == 0;
|
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) {
|
public byte[] read(Disk disk) {
|
||||||
|
validateTrackAndSector(disk);
|
||||||
return disk.readSector(track, sector);
|
return disk.readSector(track, sector);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(Disk disk, byte[] data) {
|
public void write(Disk disk, byte[] data) {
|
||||||
|
validateTrackAndSector(disk);
|
||||||
disk.writeSector(track, sector, data);
|
disk.writeSector(track, sector, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,10 +103,23 @@ public class CoordinateSelection {
|
|||||||
public boolean isBootBlock() {
|
public boolean isBootBlock() {
|
||||||
return block == 0;
|
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) {
|
public byte[] read(Disk disk) {
|
||||||
|
validateBlockNum(disk);
|
||||||
return disk.readBlock(block);
|
return disk.readBlock(block);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void write(Disk disk, byte[] data) {
|
public void write(Disk disk, byte[] data) {
|
||||||
|
validateBlockNum(disk);
|
||||||
disk.writeBlock(block, data);
|
disk.writeBlock(block, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user