Code cleanup

This commit is contained in:
Maschell 2016-10-23 15:34:57 +02:00
parent b818b0d1fe
commit 305cc7e843
11 changed files with 383 additions and 291 deletions

View File

@ -1,28 +0,0 @@
package de.mas.wupclient;
public class IoctlvResult{
private int resultValue;
private byte[][] data;
public IoctlvResult(int result, byte[][] data) {
setData(data);
setResultValue(result);
}
public byte[][] getData() {
return data;
}
public void setData(byte[][] data) {
this.data = data;
}
public int getResultValue() {
return resultValue;
}
public void setResultValue(int resultValue) {
this.resultValue = resultValue;
}
}

View File

@ -1,41 +0,0 @@
package de.mas.wupclient;
public class Result {
private int resultValue;
private int int_value;
private byte[] data;
public Result(int result, byte[] data){
setData(data);
setResultValue(result);
}
public Result(int resultValue2, int int_value) {
setResultValue(resultValue2);
setInt_value(int_value);
}
public int getResultValue() {
return resultValue;
}
public void setResultValue(int resultValue) {
this.resultValue = resultValue;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public int getInt_value() {
return int_value;
}
public void setInt_value(int int_value) {
this.int_value = int_value;
}
}

View File

@ -1,16 +1,37 @@
package de.mas.wupclient;
import java.io.IOException;
import java.util.List;
import de.mas.wupclient.client.WUPClient;
import de.mas.wupclient.client.operations.FSAOperations;
import de.mas.wupclient.client.operations.UtilOperations;
import de.mas.wupclient.client.utils.FEntry;
public class Starter {
public static void main(String args[]){
WUPClient w = new WUPClient("192.168.0.035");
try {
w.ls("/vol/storage_mlc01/");
UtilOperations util = UtilOperations.UtilOperationsFactory(w);
FSAOperations fsa = FSAOperations.FSAOperationsFactory(w);
//List<FEntry> result = util.ls("/vol/storage_mlc01/",true);
printLSRecursive("/vol/storage_mlc01/sys/title/00050010/1004c200/",util);
w.FSA_Close(w.get_fsa_handle());
w.closeSocket();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void printLSRecursive(String path,UtilOperations util) throws IOException{
List<FEntry> result = util.ls(path,true);
for(FEntry entry : result){
if(entry.isFile()){
System.out.println(path + entry.getFilename());
}else{
String newPath = path + entry.getFilename() + "/";
System.out.println(newPath);
printLSRecursive(newPath, util);
}
}
}
}

View File

@ -1,4 +1,4 @@
package de.mas.wupclient;
package de.mas.wupclient.client;
import java.io.DataOutputStream;
import java.io.IOException;
@ -6,9 +6,9 @@ import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import de.mas.wupclient.client.utils.Result;
public class WUPClient {
private String IP;
@ -23,7 +23,7 @@ public class WUPClient {
setCwd("/vol/storage_mlc01");
}
public Result send(int command, byte[] data) throws IOException{
public Result<byte[]> send(int command, byte[] data) throws IOException{
DataOutputStream outToServer = new DataOutputStream(getSocket().getOutputStream());
ByteBuffer destByteBuffer = ByteBuffer.allocate(data.length + 4);
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
@ -41,7 +41,7 @@ public class WUPClient {
int size = getSocket().getInputStream().read(result);
destByteBuffer.put(result, 0, size);
int returnValue = destByteBuffer.getInt();
return new Result(returnValue,Arrays.copyOfRange(result, 4,result.length));
return new Result<byte[]>(returnValue,Arrays.copyOfRange(result, 4,result.length));
}
public byte[] read(int addr, int len) throws IOException{
@ -49,7 +49,7 @@ public class WUPClient {
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
destByteBuffer.putInt(addr);
destByteBuffer.putInt(len);
Result result = send(1,destByteBuffer.array());
Result<byte[]> result = send(1,destByteBuffer.array());
if(result.getResultValue() == 0){
return result.getData();
}else{
@ -64,7 +64,7 @@ public class WUPClient {
destByteBuffer.putInt(addr);
destByteBuffer.put(data);
Result result = send(0,destByteBuffer.array());
Result<byte[]> result = send(0,destByteBuffer.array());
if(result.getResultValue() == 0){
return result.getResultValue();
}else{
@ -81,7 +81,7 @@ public class WUPClient {
destByteBuffer.putInt(arguments[i]);
}
Result result = send(2,destByteBuffer.array());
Result<byte[]> result = send(2,destByteBuffer.array());
if(result.getResultValue() == 0){
destByteBuffer.clear();
destByteBuffer = ByteBuffer.allocate(result.getData().length);
@ -96,7 +96,7 @@ public class WUPClient {
}
public int kill() throws IOException{
Result result = send(3,new byte[0]);
Result<byte[]> result = send(3,new byte[0]);
return result.getResultValue();
}
@ -106,7 +106,7 @@ public class WUPClient {
destByteBuffer.putInt(dest);
destByteBuffer.putInt(source);
destByteBuffer.putInt(len);
Result result = send(4,destByteBuffer.array());
Result<byte[]> result = send(4,destByteBuffer.array());
if(result.getResultValue() == 0){
return result.getResultValue();
}else{
@ -188,94 +188,6 @@ public class WUPClient {
buffer[0] = handle;
return svc(0x34,buffer);
}
public Result ioctl(int handle,int cmd, byte[] inbuf,int outbuf_size) throws IOException{
int in_address = load_buffer(inbuf);
byte[] out_data = new byte[0];
int ret;
if(outbuf_size > 0){
int out_address = alloc(outbuf_size);
int[] buffer = new int[6];
buffer[0] = handle;
buffer[1] = cmd;
buffer[2] = in_address;
buffer[3] = inbuf.length;
buffer[4] = out_address;
buffer[5] = outbuf_size;
ret = svc(0x38, buffer);
out_data = read(out_address, outbuf_size);
free(out_address);
}else{
int[] buffer = new int[6];
buffer[0] = handle;
buffer[1] = cmd;
buffer[2] = in_address;
buffer[3] = inbuf.length;
buffer[4] = 0;
buffer[5] = 0;
ret = svc(0x38, buffer);
}
free(in_address);
return new Result(ret,out_data);
}
public int iovec(int[][] inputData) throws IOException{
ByteBuffer destByteBuffer = ByteBuffer.allocate(inputData.length * (0x04*3));
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
for (int[] foo : inputData){
destByteBuffer.putInt(foo[0]);
destByteBuffer.putInt(foo[1]);
destByteBuffer.putInt(0);
}
return load_buffer(destByteBuffer.array());
}
/**
* UNTESTED!
*/
public IoctlvResult ioctlv(int handle, int cmd,byte[][] inbufs,int[] outbuf_sizes,int[][] ínbufs_ptr,int[][] outbufs_ptr) throws IOException{
int new_inbufs[][] = new int[inbufs.length][2];
int i = 0;
for(byte[] data : inbufs){
new_inbufs[i][0] = load_buffer(data, 0x40);
new_inbufs[i][1] = data.length;
i++;
}
int new_outbufs[][] = new int[outbuf_sizes.length][2];
i = 0;
for(int cur_size : outbuf_sizes){
new_outbufs[i][0] = alloc(cur_size, 0x40);
new_outbufs[i][1] = cur_size;
i++;
}
int[][] iovecs_buffer = Utils.concatAll(new_inbufs,ínbufs_ptr,outbufs_ptr,new_outbufs);
int iovecs = iovec(iovecs_buffer);
int[] buffer = new int[5];
buffer[0] = handle;
buffer[1] = cmd;
buffer[2] = new_inbufs.length + ínbufs_ptr.length;
buffer[3] = new_outbufs.length + outbufs_ptr.length;
buffer[4] = iovecs;
int ret = svc(0x39, buffer);
byte[][] out_data = new byte[new_outbufs.length][];
i=0;
for (int[] foo : new_outbufs){
out_data[i] = read(foo[0],foo[1]);
i++;
}
i=0;
int[][] free_buffer = Utils.concatAll(new_inbufs,new_outbufs);
for (int[] foo : free_buffer){
free(foo[0]);
i++;
}
free(iovecs);
return new IoctlvResult(ret,out_data);
}
public int get_fsa_handle() throws IOException{
if(getFsaHandle()== -1){
@ -284,56 +196,6 @@ public class WUPClient {
return getFsaHandle();
}
public Result FSA_OpenDir(int handle, String path) throws IOException{
byte[] inbuffer = new byte[0x520];
byte[] string = path.getBytes();
System.arraycopy(string , 0, inbuffer, 4, string.length);
inbuffer[string.length + 4] = 0;
Result res = ioctl(handle, 0x0A, inbuffer, 0x293);
byte[] data = res.getData();
ByteBuffer destByteBuffer = ByteBuffer.allocate(data.length);
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
destByteBuffer.put(data);
return new Result(res.getResultValue(),destByteBuffer.getInt(4));
}
public byte[] intToByteArray(int number){
ByteBuffer destByteBuffer = ByteBuffer.allocate(4);
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
destByteBuffer.putInt(number);
return destByteBuffer.array();
}
private ReadDirReturn FSA_ReadDir(int fsa_handle, int dirhandle) throws IOException {
byte[] inbuffer = new byte[0x520];
System.arraycopy(intToByteArray(dirhandle), 0, inbuffer, 4, 4);
Result res = ioctl(fsa_handle, 0x0B, inbuffer, 0x293);
byte[] rawdata = res.getData();
byte[] data = new byte[rawdata.length-4];
System.arraycopy(rawdata, 4, data, 0, rawdata.length-4);
byte[] unknowndata = new byte[0x64];
System.arraycopy(data, 0, unknowndata, 0, 0x64);
int i = 0;
while(data[0x64 + i] != 0 && (i +0x64) < data.length){
i++;
}
byte[] stringData = new byte[i];
boolean isFile = false;
if(unknowndata[0] == 0) isFile =true;
System.arraycopy(data, 0x64, stringData, 0, i);
if(res.getResultValue() == 0){
return new ReadDirReturn(res.getResultValue(),new String(stringData, "UTF-8"),isFile,unknowndata);
}else{
return new ReadDirReturn(res.getResultValue());
}
}
public int FSA_Close(int handle) throws IOException{
int[] buffer = new int[1];
buffer[0] = handle;
@ -344,56 +206,11 @@ public class WUPClient {
return result;
}
public int FSA_CloseDir(int handle, int dirhandle) throws IOException{
byte[] inbuffer = new byte[0x520];
System.arraycopy(intToByteArray(dirhandle), 0, inbuffer, 4, 4);
Result res = ioctl(handle, 0x0D, inbuffer, 0x293);
return res.getResultValue();
}
public List<ReadDirReturn> ls() throws IOException{
return ls(null,false);
}
public List<ReadDirReturn> ls(boolean return_data) throws IOException{
return ls(null,return_data);
}
public List<ReadDirReturn> ls(String targetPath) throws IOException{
return ls(targetPath,false);
}
public List<ReadDirReturn> ls(String targetPath,boolean return_data) throws IOException{
List<ReadDirReturn> results = new ArrayList<>();
int fsa_handle = get_fsa_handle();
String path = targetPath;
if(targetPath == null || targetPath.isEmpty()){
path = getCwd();
}
Result res = FSA_OpenDir(fsa_handle, path);
if(res.getResultValue() != 0x0){
System.out.println("opendir error : " + String.format("%08X",res.getResultValue()));
}
int dirhandle = res.getInt_value();
while(true){
ReadDirReturn result = FSA_ReadDir(fsa_handle, dirhandle);
if (result.getResult() != 0){
break;
}
if(!return_data){
if(result.isFile()){
System.out.println(result.getFilename());
}else{
System.out.println(result.getFilename() + "/");
}
}else{
results.add(result);
}
}
int result;
if((result = FSA_CloseDir(fsa_handle, dirhandle)) != 0){
System.err.println("CloseDirdone failed!" + result);
}
return results;
public byte[] intToByteArray(int number){
ByteBuffer destByteBuffer = ByteBuffer.allocate(4);
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
destByteBuffer.putInt(number);
return destByteBuffer.array();
}
private Socket createSocket(String ip) {
@ -430,10 +247,10 @@ public class WUPClient {
private int getFsaHandle() {
return fsaHandle;
}
private void setFsaHandle(int fsaHandle) {
public void setFsaHandle(int fsaHandle) {
this.fsaHandle = fsaHandle;
}
private String getCwd() {
public String getCwd() {
return cwd;
}
private void setCwd(String cwd) {

View File

@ -0,0 +1,91 @@
package de.mas.wupclient.client.operations;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
import de.mas.wupclient.client.WUPClient;
import de.mas.wupclient.client.utils.FEntry;
import de.mas.wupclient.client.utils.Result;
public class FSAOperations extends Operations {
private static Map<WUPClient,FSAOperations> instances = new HashMap<>();
public static FSAOperations FSAOperationsFactory(WUPClient client){
if(!instances.containsKey(client)){
instances.put(client, new FSAOperations(client));
}
return instances.get(client);
}
private IoctlOperations ioctl = null;
public FSAOperations(WUPClient client) {
super(client);
setIoctlOperations(IoctlOperations.IoctlOperationsFactory(client));
}
public IoctlOperations getIoctlOperations() {
return ioctl;
}
public void setIoctlOperations(IoctlOperations ioctl) {
this.ioctl = ioctl;
}
public Result<FEntry> FSA_ReadDir(int fsa_handle, int dirhandle) throws IOException {
byte[] inbuffer = new byte[0x520];
System.arraycopy(getClient().intToByteArray(dirhandle), 0, inbuffer, 4, 4);
Result<byte[]> res = ioctl.ioctl(fsa_handle, 0x0B, inbuffer, 0x293);
byte[] rawdata = res.getData();
byte[] data = new byte[rawdata.length-4];
System.arraycopy(rawdata, 4, data, 0, rawdata.length-4);
byte[] unknowndata = new byte[0x64];
System.arraycopy(data, 0, unknowndata, 0, 0x64);
int i = 0;
while(data[0x64 + i] != 0 && (i +0x64) < data.length){
i++;
}
byte[] stringData = new byte[i];
boolean isFile = false;
if((unknowndata[0] & 0x01) == 1){
isFile = true;
}
System.arraycopy(data, 0x64, stringData, 0, i);
//System.out.println(new String(stringData, "UTF-8") + ":" + Integer.toBinaryString(unknowndata[0] & 0x01));
if(res.getResultValue() == 0){
return new Result<FEntry>(res.getResultValue(),new FEntry(new String(stringData, "UTF-8"),isFile,unknowndata));
}else{
return new Result<FEntry>(res.getResultValue(),null);
}
}
public Result<Integer> FSA_OpenDir(int handle, String path) throws IOException{
byte[] inbuffer = new byte[0x520];
byte[] string = path.getBytes();
System.arraycopy(string , 0, inbuffer, 4, string.length);
inbuffer[string.length + 4] = 0;
Result<byte[]> res = ioctl.ioctl(handle, 0x0A, inbuffer, 0x293);
byte[] data = res.getData();
ByteBuffer destByteBuffer = ByteBuffer.allocate(data.length);
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
destByteBuffer.put(data);
return new Result<Integer>(res.getResultValue(),destByteBuffer.getInt(4));
}
public int FSA_CloseDir(int handle, int dirhandle) throws IOException{
byte[] inbuffer = new byte[0x520];
System.arraycopy(getClient().intToByteArray(dirhandle), 0, inbuffer, 4, 4);
Result<byte[]> res = ioctl.ioctl(handle, 0x0D, inbuffer, 0x293);
return res.getResultValue();
}
}

View File

@ -0,0 +1,113 @@
package de.mas.wupclient.client.operations;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.HashMap;
import java.util.Map;
import de.mas.wupclient.Utils;
import de.mas.wupclient.client.WUPClient;
import de.mas.wupclient.client.utils.Result;
public class IoctlOperations extends Operations {
private static Map<WUPClient,IoctlOperations> instances = new HashMap<>();
public static IoctlOperations IoctlOperationsFactory(WUPClient client){
if(!instances.containsKey(client)){
instances.put(client, new IoctlOperations(client));
}
return instances.get(client);
}
private IoctlOperations(WUPClient client) {
super(client);
}
public Result<byte[]> ioctl(int handle,int cmd, byte[] inbuf,int outbuf_size) throws IOException{
int in_address = getClient().load_buffer(inbuf);
byte[] out_data = new byte[0];
int ret;
if(outbuf_size > 0){
int out_address = getClient().alloc(outbuf_size);
int[] buffer = new int[6];
buffer[0] = handle;
buffer[1] = cmd;
buffer[2] = in_address;
buffer[3] = inbuf.length;
buffer[4] = out_address;
buffer[5] = outbuf_size;
ret = getClient().svc(0x38, buffer);
out_data = getClient().read(out_address, outbuf_size);
getClient().free(out_address);
}else{
int[] buffer = new int[6];
buffer[0] = handle;
buffer[1] = cmd;
buffer[2] = in_address;
buffer[3] = inbuf.length;
buffer[4] = 0;
buffer[5] = 0;
ret = getClient().svc(0x38, buffer);
}
getClient().free(in_address);
return new Result<byte[]>(ret,out_data);
}
public int iovec(int[][] inputData) throws IOException{
ByteBuffer destByteBuffer = ByteBuffer.allocate(inputData.length * (0x04*3));
destByteBuffer.order(ByteOrder.BIG_ENDIAN);
for (int[] foo : inputData){
destByteBuffer.putInt(foo[0]);
destByteBuffer.putInt(foo[1]);
destByteBuffer.putInt(0);
}
return getClient().load_buffer(destByteBuffer.array());
}
/**
* UNTESTED!
*/
public Result<byte[][]> ioctlv(int handle, int cmd,byte[][] inbufs,int[] outbuf_sizes,int[][] ínbufs_ptr,int[][] outbufs_ptr) throws IOException{
int new_inbufs[][] = new int[inbufs.length][2];
int i = 0;
for(byte[] data : inbufs){
new_inbufs[i][0] = getClient().load_buffer(data, 0x40);
new_inbufs[i][1] = data.length;
i++;
}
int new_outbufs[][] = new int[outbuf_sizes.length][2];
i = 0;
for(int cur_size : outbuf_sizes){
new_outbufs[i][0] = getClient().alloc(cur_size, 0x40);
new_outbufs[i][1] = cur_size;
i++;
}
int[][] iovecs_buffer = Utils.concatAll(new_inbufs,ínbufs_ptr,outbufs_ptr,new_outbufs);
int iovecs = iovec(iovecs_buffer);
int[] buffer = new int[5];
buffer[0] = handle;
buffer[1] = cmd;
buffer[2] = new_inbufs.length + ínbufs_ptr.length;
buffer[3] = new_outbufs.length + outbufs_ptr.length;
buffer[4] = iovecs;
int ret = getClient().svc(0x39, buffer);
byte[][] out_data = new byte[new_outbufs.length][];
i=0;
for (int[] foo : new_outbufs){
out_data[i] = getClient().read(foo[0],foo[1]);
i++;
}
i=0;
int[][] free_buffer = Utils.concatAll(new_inbufs,new_outbufs);
for (int[] foo : free_buffer){
getClient().free(foo[0]);
i++;
}
getClient().free(iovecs);
return new Result<byte[][]>(ret,out_data);
}
}

View File

@ -0,0 +1,16 @@
package de.mas.wupclient.client.operations;
import de.mas.wupclient.client.WUPClient;
public abstract class Operations {
private WUPClient client = null;
public Operations(WUPClient client){
setClient(client);
}
public WUPClient getClient() {
return client;
}
public void setClient(WUPClient client) {
this.client = client;
}
}

View File

@ -0,0 +1,82 @@
package de.mas.wupclient.client.operations;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.mas.wupclient.client.WUPClient;
import de.mas.wupclient.client.utils.FEntry;
import de.mas.wupclient.client.utils.Result;
public class UtilOperations extends Operations {
private static Map<WUPClient,UtilOperations> instances = new HashMap<>();
public static UtilOperations UtilOperationsFactory(WUPClient client){
if(!instances.containsKey(client)){
instances.put(client, new UtilOperations(client));
}
return instances.get(client);
}
private FSAOperations fsa = null;
public UtilOperations(WUPClient client) {
super(client);
setFSAOperations(FSAOperations.FSAOperationsFactory(client));
}
public List<FEntry> ls() throws IOException{
return ls(null,false);
}
public List<FEntry> ls(boolean return_data) throws IOException{
return ls(null,return_data);
}
public List<FEntry> ls(String targetPath) throws IOException{
return ls(targetPath,false);
}
public List<FEntry> ls(String targetPath,boolean return_data) throws IOException{
List<FEntry> results = new ArrayList<>();
int fsa_handle = getClient().get_fsa_handle();
String path = targetPath;
if(targetPath == null || targetPath.isEmpty()){
path = getClient().getCwd();
}
Result<Integer> res = fsa.FSA_OpenDir(fsa_handle, path);
if(res.getResultValue() != 0x0){
System.out.println("opendir error : " + String.format("%08X",res.getResultValue()));
}
int dirhandle = res.getData();
while(true){
Result<FEntry> result = fsa.FSA_ReadDir(fsa_handle, dirhandle);
if (result.getResultValue() != 0){
break;
}
FEntry entry = result.getData();
if(entry == null){
break;
}
if(!return_data){
if(entry.isFile()){
System.out.println(entry.getFilename());
}else{
System.out.println(entry.getFilename() + "/");
}
}else{
results.add(entry);
}
}
int result;
if((result = fsa.FSA_CloseDir(fsa_handle, dirhandle)) != 0){
System.err.println("CloseDirdone failed!" + result);
}
return results;
}
public FSAOperations getFSAOperations() {
return fsa;
}
public void setFSAOperations(FSAOperations fsa) {
this.fsa = fsa;
}
}

View File

@ -0,0 +1,17 @@
package de.mas.wupclient.client.utils;
public abstract class CommonResult {
private int resultValue;
public CommonResult(int result){
setResultValue(result);
}
public int getResultValue() {
return resultValue;
}
public void setResultValue(int resultValue) {
this.resultValue = resultValue;
}
}

View File

@ -1,22 +1,16 @@
package de.mas.wupclient;
package de.mas.wupclient.client.utils;
public class ReadDirReturn {
private int result;
public class FEntry {
private String filename;
private boolean isFile;
private byte[] unknowndata;
public ReadDirReturn(int result,String filename, boolean isFile, byte[] unknowndata) {
setResult(result);
public FEntry(String filename, boolean isFile, byte[] unknowndata) {
setFilename(filename);
setFile(isFile);
setUnknowndata(unknowndata);
}
public ReadDirReturn(int resultValue) {
setResult(resultValue);
}
public byte[] getUnknowndata() {
return unknowndata;
}
@ -40,13 +34,4 @@ public class ReadDirReturn {
public void setFilename(String filename) {
this.filename = filename;
}
public int getResult() {
return result;
}
public void setResult(int result) {
this.result = result;
}
}

View File

@ -0,0 +1,19 @@
package de.mas.wupclient.client.utils;
public class Result<T> extends CommonResult{
private T data;
public Result(int result,T data) {
super(result);
setData(data);
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}