|
| 1 | +package moe.shizuku.manager.adb; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.ByteOrder; |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +public class AdbMessage { |
| 8 | + |
| 9 | + public final int command; |
| 10 | + public final int arg0; |
| 11 | + public final int arg1; |
| 12 | + public final int data_length; |
| 13 | + public final int data_crc32; |
| 14 | + public final int magic; |
| 15 | + public final byte[] data; |
| 16 | + |
| 17 | + public AdbMessage(int command, int arg0, int arg1, int data_length, int data_crc32, int magic, byte[] data) { |
| 18 | + this.command = command; |
| 19 | + this.arg0 = arg0; |
| 20 | + this.arg1 = arg1; |
| 21 | + this.data_length = data_length; |
| 22 | + this.data_crc32 = data_crc32; |
| 23 | + this.magic = magic; |
| 24 | + this.data = data; |
| 25 | + } |
| 26 | + |
| 27 | + public AdbMessage(int command, int arg0, int arg1, String data) { |
| 28 | + this(command, arg0, arg1, ("" + data + "\u0000").getBytes()); |
| 29 | + } |
| 30 | + |
| 31 | + public AdbMessage(int command, int arg0, int arg1, byte[] data) { |
| 32 | + this(command, |
| 33 | + arg0, |
| 34 | + arg1, |
| 35 | + data != null ? data.length : 0, |
| 36 | + crc32(data), |
| 37 | + (int) ( (long)command ^ 0xFFFFFFFFL ), |
| 38 | + data); |
| 39 | + } |
| 40 | + |
| 41 | + public boolean validate() { |
| 42 | + if (command != (magic ^ -0x1)) return false; |
| 43 | + if (data_length != 0 && crc32(data) != data_crc32) return false; |
| 44 | + return true; |
| 45 | + } |
| 46 | + |
| 47 | + public void validateOrThrow() { |
| 48 | + if (!validate()) throw new IllegalArgumentException("bad message " + toStringShort()); |
| 49 | + } |
| 50 | + |
| 51 | + public byte[] toByteArray() { |
| 52 | + int length = HEADER_LENGTH + (data != null ? data.length : 0); |
| 53 | + ByteBuffer buffer = ByteBuffer.allocate(length); |
| 54 | + buffer.order(ByteOrder.LITTLE_ENDIAN); |
| 55 | + buffer.putInt(command); |
| 56 | + buffer.putInt(arg0); |
| 57 | + buffer.putInt(arg1); |
| 58 | + buffer.putInt(data_length); |
| 59 | + buffer.putInt(data_crc32); |
| 60 | + buffer.putInt(magic); |
| 61 | + if (data != null) buffer.put(data); |
| 62 | + return buffer.array(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean equals(Object other) { |
| 67 | + if (this == other) return true; |
| 68 | + if (other == null || getClass() != other.getClass()) return false; |
| 69 | + AdbMessage o = (AdbMessage) other; |
| 70 | + if (command != o.command) return false; |
| 71 | + if (arg0 != o.arg0) return false; |
| 72 | + if (arg1 != o.arg1) return false; |
| 73 | + if (data_length != o.data_length) return false; |
| 74 | + if (data_crc32 != o.data_crc32) return false; |
| 75 | + if (magic != o.magic) return false; |
| 76 | + if (!Arrays.equals(data, o.data)) return false; |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int hashCode() { |
| 82 | + int result = command; |
| 83 | + result = 31 * result + arg0; |
| 84 | + result = 31 * result + arg1; |
| 85 | + result = 31 * result + data_length; |
| 86 | + result = 31 * result + data_crc32; |
| 87 | + result = 31 * result + magic; |
| 88 | + result = 31 * result + (data != null ? Arrays.hashCode(data) : 0); |
| 89 | + return result; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString() { |
| 94 | + return "AdbMessage(" + toStringShort() + ")"; |
| 95 | + } |
| 96 | + |
| 97 | + public String toStringShort() { |
| 98 | + String commandString; |
| 99 | + switch (command) { |
| 100 | + case AdbProtocol.A_SYNC: commandString = "A_SYNC"; break; |
| 101 | + case AdbProtocol.A_CNXN: commandString = "A_CNXN"; break; |
| 102 | + case AdbProtocol.A_AUTH: commandString = "A_AUTH"; break; |
| 103 | + case AdbProtocol.A_OPEN: commandString = "A_OPEN"; break; |
| 104 | + case AdbProtocol.A_OKAY: commandString = "A_OKAY"; break; |
| 105 | + case AdbProtocol.A_CLSE: commandString = "A_CLSE"; break; |
| 106 | + case AdbProtocol.A_WRTE: commandString = "A_WRTE"; break; |
| 107 | + case AdbProtocol.A_STLS: commandString = "A_STLS"; break; |
| 108 | + default: commandString = Integer.toString(command); break; |
| 109 | + } |
| 110 | + return "command=" + commandString + ", arg0=" + arg0 + ", arg1=" + arg1 + ", data_length=" + data_length + ", data_crc32=" + data_crc32 + ", magic=" + magic + ", data=" + (data != null ? Arrays.toString(data) : "null"); |
| 111 | + } |
| 112 | + |
| 113 | + public static final int HEADER_LENGTH = 24; |
| 114 | + |
| 115 | + private static int crc32(byte[] data) { |
| 116 | + if (data == null) return 0; |
| 117 | + int res = 0; |
| 118 | + for (byte b : data) { |
| 119 | + if (b >= 0) res += b; |
| 120 | + else res += b + 256; |
| 121 | + } |
| 122 | + return res; |
| 123 | + } |
| 124 | +} |
0 commit comments