1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| package com.github.unidbg.linux.file;
import com.github.unidbg.Emulator; import com.github.unidbg.arm.backend.Backend; import com.github.unidbg.file.linux.IOConstants; import com.sun.jna.Pointer;
import java.util.Arrays; import java.util.concurrent.ThreadLocalRandom;
public class RandomFileIO extends DriverFileIO {
public RandomFileIO(Emulator<?> emulator, String path) { super(emulator, IOConstants.O_RDONLY, path); }
public static String toHex(byte[] bytes) { StringBuilder hexString = new StringBuilder(); for (byte b : bytes) { hexString.append(String.format("%02x ", b)); } return hexString.toString().trim();
}
@Override public int read(Backend backend, Pointer buffer, int count) { int total = 0; byte[] buf = new byte[Math.min(0x1000, count)]; randBytes(buf); System.out.println("固定随机数:" + toHex(buf)); Pointer pointer = buffer; while (total < count) { int read = Math.min(buf.length, count - total); pointer.write(0, buf, 0, read); total += read; pointer = pointer.share(read); } return total;
}
private static final byte[] FIXED_RANDOM_BYTES = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x9A, (byte) 0xBC, (byte) 0xDE, (byte) 0xF0 };
protected void randBytes(byte[] bytes) { if (bytes == null || bytes.length == 0) { return; }
for (int i = 0; i < bytes.length; i++) { bytes[i] = FIXED_RANDOM_BYTES[i % FIXED_RANDOM_BYTES.length]; } }
}
|