Search in sources :

Example 1 with NulTerminatedCharsetDecoder

use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.

the class MachOFixCompDirCommand method invokeWithParams.

@Override
protected int invokeWithParams(CommandRunnerParams params) throws IOException {
    NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
    CompDirReplacer.replaceCompDirInFile(getOutput(), getOldCompDir(), getUpdatedCompDir(), decoder);
    return 0;
}
Also used : NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder)

Example 2 with NulTerminatedCharsetDecoder

use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.

the class LoadCommandUtilsTest method testEnumeratingLoadCommands.

@Test
public void testEnumeratingLoadCommands() throws Exception {
    byte[] header = MachoHeaderTestData.getBigEndian64Bit();
    // ncmds
    header[19] = 2;
    // sizeofcmds
    header[23] = 16;
    byte[] commandBytes = BaseEncoding.base16().decode(// command 1 is AA, size 8
    "000000AA00000008" + // command 2 is BB, size 8
    "000000BB00000008");
    ByteBuffer buffer = ByteBuffer.allocate(MachoHeaderTestData.getBigEndian64Bit().length + commandBytes.length).put(header).put(commandBytes).order(ByteOrder.BIG_ENDIAN);
    final List<LoadCommand> result = new ArrayList<>();
    buffer.position(0);
    LoadCommandUtils.enumerateLoadCommandsInFile(buffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()), input -> {
        result.add(input);
        return Boolean.TRUE;
    });
    assertThat(result.size(), equalTo(2));
    assertThat(result.get(0).getLoadCommandCommonFields().getCmd(), equalTo(UnsignedInteger.fromIntBits(0xAA)));
    assertThat(result.get(0).getLoadCommandCommonFields().getCmdsize(), equalTo(UnsignedInteger.fromIntBits(0x08)));
    assertThat(result.get(1).getLoadCommandCommonFields().getCmd(), equalTo(UnsignedInteger.fromIntBits(0xBB)));
    assertThat(result.get(1).getLoadCommandCommonFields().getCmdsize(), equalTo(UnsignedInteger.fromIntBits(0x08)));
}
Also used : ArrayList(java.util.ArrayList) NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 3 with NulTerminatedCharsetDecoder

use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.

the class LoadCommandUtilsTest method testFindingLoadCommands.

@Test
public void testFindingLoadCommands() throws Exception {
    byte[] header = MachoHeaderTestData.getBigEndian64Bit();
    // ncmds
    header[19] = 3;
    // sizeofcmds
    header[23] = 16;
    byte[] symtabBytes = SymTabCommandTestData.getBigEndian();
    byte[] uuid1Bytes = UUIDCommandTestData.getBigEndian();
    uuid1Bytes[8] = (byte) 0x11;
    byte[] uuid2Bytes = UUIDCommandTestData.getBigEndian();
    uuid2Bytes[8] = (byte) 0x22;
    ByteBuffer buffer = ByteBuffer.allocate(MachoHeaderTestData.getBigEndian64Bit().length + symtabBytes.length + uuid1Bytes.length + uuid2Bytes.length).order(ByteOrder.BIG_ENDIAN).put(header).put(uuid1Bytes).put(symtabBytes).put(uuid2Bytes);
    buffer.position(0);
    ImmutableList<UUIDCommand> uuidCommands = LoadCommandUtils.findLoadCommandsWithClass(buffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()), UUIDCommand.class);
    assertThat(uuidCommands.size(), equalTo(2));
    assertThat(uuidCommands.get(0).getUuid().toString(), startsWith("11"));
    assertThat(uuidCommands.get(0).getLoadCommandCommonFields().getOffsetInBinary(), equalTo(header.length + 0));
    assertThat(uuidCommands.get(1).getUuid().toString(), startsWith("22"));
    assertThat(uuidCommands.get(1).getLoadCommandCommonFields().getOffsetInBinary(), equalTo(header.length + 48));
    buffer.position(0);
    ImmutableList<SymTabCommand> symTabCommands = LoadCommandUtils.findLoadCommandsWithClass(buffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()), SymTabCommand.class);
    assertThat(symTabCommands.size(), equalTo(1));
    assertThat(symTabCommands.get(0).getLoadCommandCommonFields().getOffsetInBinary(), equalTo(header.length + 24));
}
Also used : NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with NulTerminatedCharsetDecoder

use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.

the class LoadCommandUtilsTest method testCreatingLoadCommandsFromBuffer.

@Test
public void testCreatingLoadCommandsFromBuffer() throws Exception {
    byte[] segmentBytes = SegmentCommandTestData.getBigEndian64Bits();
    byte[] symtabBytes = SymTabCommandTestData.getBigEndian();
    byte[] uuidBytes = UUIDCommandTestData.getBigEndian();
    byte[] linkEditBytes = LinkEditCommandTestData.getCodeSignBigEndian();
    byte[] unknownBytes = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x99, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x08 };
    ByteBuffer byteBuffer = ByteBuffer.allocate(segmentBytes.length + symtabBytes.length + uuidBytes.length + linkEditBytes.length + unknownBytes.length).order(ByteOrder.BIG_ENDIAN);
    byteBuffer.put(segmentBytes).put(symtabBytes).put(uuidBytes).put(linkEditBytes).put(unknownBytes);
    byteBuffer.position(0);
    assertThat(LoadCommandUtils.createLoadCommandFromBuffer(byteBuffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder())), instanceOf(SegmentCommand.class));
    assertThat(LoadCommandUtils.createLoadCommandFromBuffer(byteBuffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder())), instanceOf(SymTabCommand.class));
    assertThat(LoadCommandUtils.createLoadCommandFromBuffer(byteBuffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder())), instanceOf(UUIDCommand.class));
    assertThat(LoadCommandUtils.createLoadCommandFromBuffer(byteBuffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder())), instanceOf(LinkEditDataCommand.class));
    assertThat(LoadCommandUtils.createLoadCommandFromBuffer(byteBuffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder())), instanceOf(UnknownCommand.class));
}
Also used : NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 5 with NulTerminatedCharsetDecoder

use of com.facebook.buck.charset.NulTerminatedCharsetDecoder in project buck by facebook.

the class SegmentCommandUtilsTest method testUpdatingSegmentCommandInByteBuffer64Bit.

@Test
public void testUpdatingSegmentCommandInByteBuffer64Bit() throws Exception {
    byte[] bytes = SegmentCommandTestData.getBigEndian64Bits();
    final int commandSize = bytes.length;
    SegmentCommand command = SegmentCommandUtils.createFromBuffer(ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN), new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
    SegmentCommand updated = command.withFilesize(UnsignedLong.fromLongBits(1234)).withVmsize(UnsignedLong.fromLongBits(SegmentCommandUtils.alignValue(4321)));
    ByteBuffer buffer = ByteBuffer.allocate(commandSize);
    SegmentCommandUtils.updateSegmentCommand(buffer, command, updated);
    buffer.position(0);
    SegmentCommand commandInBuffer = SegmentCommandUtils.createFromBuffer(buffer, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
    assertThat(commandInBuffer.getFilesize(), equalToObject(updated.getFilesize()));
    assertThat(commandInBuffer.getVmsize(), equalToObject(updated.getVmsize()));
}
Also used : NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

NulTerminatedCharsetDecoder (com.facebook.buck.charset.NulTerminatedCharsetDecoder)25 Test (org.junit.Test)23 ByteBuffer (java.nio.ByteBuffer)15 ArrayList (java.util.ArrayList)4 MappedByteBuffer (java.nio.MappedByteBuffer)2 Path (java.nio.file.Path)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ObjectPathsAbsolutifier (com.facebook.buck.macho.ObjectPathsAbsolutifier)1 RandomAccessFile (java.io.RandomAccessFile)1