Skip to content

Commit

Permalink
NXDT: Add ZLT packet validation check
Browse files Browse the repository at this point in the history
NXDT: Update BufferedStreams: add try-with-resources practice instead of handling steam.close() calls. (See https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html).

Thanks DarkMatterCore :)
  • Loading branch information
developersu committed Aug 14, 2020
1 parent dff7063 commit ab1b5b1
Showing 1 changed file with 61 additions and 47 deletions.
108 changes: 61 additions & 47 deletions src/main/java/nsusbloader/Utilities/nxdumptool/NxdtUsbAbi1.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class NxdtUsbAbi1 {
private final String saveToPath;
private final NxdtTask parent;

private boolean isWindows;
private final boolean isWindows;
private boolean isWindows10;

private static final int NXDT_MAX_DIRECTIVE_SIZE = 0x1000;
Expand Down Expand Up @@ -77,6 +77,8 @@ class NxdtUsbAbi1 {
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00 };

private static final long W_MAX_PACKET_SIZE = 0x200;

public NxdtUsbAbi1(DeviceHandle handler,
ILogPrinter logPrinter,
String saveToPath,
Expand Down Expand Up @@ -136,7 +138,7 @@ private void readLoop(){
logPrinter.print(e.getMessage(), EMsgType.INFO);
logPrinter.print("Terminating now", EMsgType.FAIL);
}
};
}

private boolean isInvalidDirective(byte[] message) throws Exception{
if (message.length < 0x10){
Expand Down Expand Up @@ -256,52 +258,66 @@ private void createPath(String path) throws Exception{
}

private void dumpFile(File file, long size) throws Exception{
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false));

byte[] readBuffer;
long received = 0;
int bufferSize;

while (received < size){
readBuffer = readUsbFile();
bos.write(readBuffer);
bufferSize = readBuffer.length;
received += bufferSize;
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false))) {
byte[] readBuffer;
long received = 0;
int bufferSize;

boolean zlt_expected = isAligned(size);

while (received < size) {
readBuffer = readUsbFile();
bos.write(readBuffer);
bufferSize = readBuffer.length;
received += bufferSize;
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
}

if (zlt_expected) {
logPrinter.print("Finishing with ZLT packet request", EMsgType.INFO);
readUsbFile();
}
} finally {
logPrinter.updateProgress(1.0);
}
logPrinter.updateProgress(1.0);
bos.close();
}

// @see https://bugs.openjdk.java.net/browse/JDK-8146538
private void dumpFileOnWindowsTen(File file, long size) throws Exception{
FileOutputStream fos = new FileOutputStream(file, true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
FileDescriptor fd = fos.getFD();

byte[] readBuffer;
long received = 0;
int bufferSize;
try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
FileDescriptor fd = fos.getFD();
byte[] readBuffer;
long received = 0;
int bufferSize;

while (received < size){
readBuffer = readUsbFile();
bos.write(readBuffer);
fd.sync(); // Fixes flushing under Windows (unharmful for other OS)
bufferSize = readBuffer.length;
received += bufferSize;
boolean zlt_expected = isAligned(size);

logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
}
while (received < size) {
readBuffer = readUsbFile();
bos.write(readBuffer);
fd.sync(); // Fixes flushing under Windows (unharmful for other OS)
bufferSize = readBuffer.length;
received += bufferSize;

logPrinter.updateProgress(1.0);
bos.close();
logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0);
}

if (zlt_expected) {
logPrinter.print("Finishing with ZLT packet request", EMsgType.INFO);
readUsbFile();
}
} finally {
logPrinter.updateProgress(1.0);
}
}
/** Handle Zero-length terminator **/
private boolean isAligned(long size){
return ((size & (W_MAX_PACKET_SIZE - 1)) == 0);
}

/**
* Sending any byte array to USB device
* @return 'false' if no issues
* 'true' if errors happened
* */
/** Sending any byte array to USB device **/
private void writeUsb(byte[] message) throws Exception{
ByteBuffer writeBuffer = ByteBuffer.allocateDirect(message.length);
writeBuffer.put(message);
Expand All @@ -312,18 +328,16 @@ private void writeUsb(byte[] message) throws Exception{

int result = LibUsb.bulkTransfer(handlerNS, (byte) 0x01, writeBuffer, writeBufTransferred, 5050);

switch (result){
case LibUsb.SUCCESS:
if (writeBufTransferred.get() == message.length)
return;
throw new Exception("Data transfer issue [write]" +
"\n Requested: "+message.length+
"\n Transferred: "+writeBufTransferred.get());
default:
throw new Exception("Data transfer issue [write]" +
"\n Returned: "+ UsbErrorCodes.getErrCode(result) +
"\n (execution stopped)");
if (result == LibUsb.SUCCESS) {
if (writeBufTransferred.get() == message.length)
return;
throw new Exception("Data transfer issue [write]" +
"\n Requested: " + message.length +
"\n Transferred: " + writeBufTransferred.get());
}
throw new Exception("Data transfer issue [write]" +
"\n Returned: " + UsbErrorCodes.getErrCode(result) +
"\n (execution stopped)");

}
/**
Expand Down

0 comments on commit ab1b5b1

Please sign in to comment.