From 6d4bed20a9e2ba5088a8d42b56ec7628d60cbb56 Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Sun, 10 May 2020 19:33:46 +0300 Subject: [PATCH] Some bug fixes --- .../Controllers/NxdtController.java | 6 +++- src/main/java/nsusbloader/NSLMain.java | 2 ++ src/main/java/nsusbloader/RainbowHexDump.java | 8 ++--- .../nsusbloader/Utilities/NxdtUsbAbi1.java | 32 +++++++++++-------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/main/java/nsusbloader/Controllers/NxdtController.java b/src/main/java/nsusbloader/Controllers/NxdtController.java index cc758c2..74ba291 100644 --- a/src/main/java/nsusbloader/Controllers/NxdtController.java +++ b/src/main/java/nsusbloader/Controllers/NxdtController.java @@ -52,7 +52,11 @@ public class NxdtController implements Initializable { public void initialize(URL url, ResourceBundle resourceBundle) { this.rb = resourceBundle; - saveToLocationLbl.setText(AppPreferences.getInstance().getNXDTSaveToLocation()); + File saveToValidator = new File(AppPreferences.getInstance().getNXDTSaveToLocation()); + if (saveToValidator.exists()) + saveToLocationLbl.setText(saveToValidator.getAbsolutePath()); + else + saveToLocationLbl.setText(System.getProperty("user.home")); btnDumpStopImage = new Region(); btnDumpStopImage.getStyleClass().add("regionDump"); diff --git a/src/main/java/nsusbloader/NSLMain.java b/src/main/java/nsusbloader/NSLMain.java index 8e2585d..1c0810e 100644 --- a/src/main/java/nsusbloader/NSLMain.java +++ b/src/main/java/nsusbloader/NSLMain.java @@ -26,6 +26,8 @@ import javafx.scene.image.Image; import javafx.stage.Stage; import nsusbloader.Controllers.NSLMainController; +import java.io.File; +import java.nio.file.Paths; import java.util.Locale; import java.util.ResourceBundle; diff --git a/src/main/java/nsusbloader/RainbowHexDump.java b/src/main/java/nsusbloader/RainbowHexDump.java index d3131ad..ebdd277 100644 --- a/src/main/java/nsusbloader/RainbowHexDump.java +++ b/src/main/java/nsusbloader/RainbowHexDump.java @@ -41,7 +41,7 @@ public class RainbowHexDump { System.out.println(">"+ANSI_RED+byteArray.length+ANSI_RESET); for (byte b: byteArray) System.out.print(String.format("%02x ", b)); - //System.out.println(); + System.out.println(); System.out.print("\t\t\t" + new String(byteArray, StandardCharsets.UTF_8) + "\n"); @@ -54,8 +54,7 @@ public class RainbowHexDump { for (byte b: byteArray) System.out.print(String.format("%02x ", b)); System.out.println(); - System.out.print("\t\t\t" - + new String(byteArray, StandardCharsets.UTF_8) + System.out.print(new String(byteArray, StandardCharsets.UTF_8) + "\n"); } @@ -66,8 +65,7 @@ public class RainbowHexDump { System.out.println(">"+ANSI_RED+byteArray.length+ANSI_RESET); for (byte b: byteArray) System.out.print(String.format("%02x ", b)); - System.out.print("\t\t\t" - + new String(byteArray, StandardCharsets.UTF_16LE) + System.out.print(new String(byteArray, StandardCharsets.UTF_16LE) + "\n"); } } diff --git a/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java b/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java index 10dd560..4cdd800 100644 --- a/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java +++ b/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java @@ -31,6 +31,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; import java.util.Arrays; class NxdtUsbAbi1 { @@ -104,7 +105,7 @@ class NxdtUsbAbi1 { while (true){ directive = readUsbDirective(); - RainbowHexDump.hexDumpUTF8ForWin(directive); + RainbowHexDump.hexDumpUTF8(directive); if (isInvalidDirective(directive)) continue; @@ -132,7 +133,7 @@ class NxdtUsbAbi1 { } catch (Exception e){ e.printStackTrace(); - logPrinter.print(e.getCause()+" "+e.getMessage(), EMsgType.INFO); + logPrinter.print(e.getMessage(), EMsgType.INFO); logPrinter.print("Terminating now", EMsgType.FAIL); } }; @@ -145,17 +146,24 @@ class NxdtUsbAbi1 { } if (message.length < 0x10){ writeUsb(USBSTATUS_MALFORMED_REQUEST); - logPrinter.print("Invalid directive size. "+message.length+" bytes received.", EMsgType.FAIL); + logPrinter.print("Directive is too small to be a command. Only "+message.length+" bytes received.", EMsgType.FAIL); + return true; + } + + int payloadSize = getLEint(message, 0x8); + if (payloadSize + 0x10 != message.length){ + writeUsb(USBSTATUS_MALFORMED_REQUEST); + logPrinter.print("Invalid directive size. "+message.length+" bytes received while "+payloadSize+" expected.", EMsgType.FAIL); return true; } return false; } private void performHandshake(byte[] message) throws Exception{ - final byte versionMajor = message[10]; - final byte versionMinor = message[11]; - final byte versionMicro = message[12]; - final byte versionABI = message[13]; + final byte versionMajor = message[0x10]; + final byte versionMinor = message[0x11]; + final byte versionMicro = message[0x12]; + final byte versionABI = message[0x13]; logPrinter.print("nxdumptool v"+versionMajor+"."+versionMinor+"."+versionMicro+" ABI v"+versionABI, EMsgType.INFO); @@ -185,7 +193,6 @@ class NxdtUsbAbi1 { filename = saveToPath + filename; File fileToDump = new File(filename); - // Check if enough space if (fileToDump.getParentFile().getFreeSpace() <= fileSize){ writeUsb(USBSTATUS_HOSTIOERROR); @@ -193,7 +200,7 @@ class NxdtUsbAbi1 { return; } // Check if FS is NOT read-only - if (! fileToDump.canWrite()){ + if (! (fileToDump.canWrite() || fileToDump.createNewFile()) ){ writeUsb(USBSTATUS_HOSTIOERROR); logPrinter.print("Unable to write into selected volume: "+fileToDump.getAbsolutePath(), EMsgType.FAIL); return; @@ -234,7 +241,7 @@ class NxdtUsbAbi1 { } private void dumpFile(File file, long size) throws Exception{ - RandomAccessFile raf = new RandomAccessFile(file, "rw"); + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false)); byte[] readBuffer; long received = 0; @@ -242,7 +249,7 @@ class NxdtUsbAbi1 { while (received < size){ readBuffer = readUsbFile(); - raf.write(readBuffer); + bos.write(readBuffer); bufferSize = readBuffer.length; received += bufferSize; @@ -250,10 +257,9 @@ class NxdtUsbAbi1 { } logPrinter.updateProgress(1.0); - raf.close(); + bos.close(); } - /** * Sending any byte array to USB device * @return 'false' if no issues