Search in sources :

Example 16 with NulTerminatedCharsetDecoder

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

the class SegmentCommandUtilsTest method testUpdatingSegmentCommandInByteBuffer32Bit.

@Test
public void testUpdatingSegmentCommandInByteBuffer32Bit() throws Exception {
    byte[] bytes = SegmentCommandTestData.getBigEndian32Bits();
    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)

Example 17 with NulTerminatedCharsetDecoder

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

the class SegmentCommandUtilsTest method testEnumeratingSections32Bit.

@Test
public void testEnumeratingSections32Bit() throws Exception {
    final int sectionSize = SectionTestData.getBigEndian32Bit().length;
    byte[] sectionData1 = SectionTestData.getBigEndian32Bit();
    // offset = 1
    sectionData1[43] = (byte) 0x01;
    byte[] sectionData2 = SectionTestData.getBigEndian32Bit();
    // sectname = "DECTNAME"
    sectionData2[0] = (byte) 0x44;
    // segname = "DEGNAME"
    sectionData2[16] = (byte) 0x44;
    // offset = 2
    sectionData2[43] = (byte) 0x02;
    byte[] sectionData3 = SectionTestData.getBigEndian32Bit();
    // sectname = "LECTNAME"
    sectionData3[0] = (byte) 0x4C;
    // segname = "LEGNAME"
    sectionData3[16] = (byte) 0x4C;
    // offset = 3
    sectionData3[43] = (byte) 0x03;
    byte[] segmentBytes = SegmentCommandTestData.getBigEndian32Bits();
    // nsects = 3
    segmentBytes[51] = (byte) 0x03;
    SegmentCommand command = SegmentCommandUtils.createFromBuffer(ByteBuffer.wrap(segmentBytes).order(ByteOrder.BIG_ENDIAN), new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
    ByteBuffer buffer = ByteBuffer.allocate(command.getLoadCommandCommonFields().getCmdsize().intValue() + 3 * sectionSize);
    buffer.order(ByteOrder.BIG_ENDIAN);
    SegmentCommandUtils.writeCommandToBuffer(command, buffer, false);
    buffer.put(sectionData1);
    buffer.put(sectionData2);
    buffer.put(sectionData3);
    final List<Section> enumeratedSections = new ArrayList<>();
    SegmentCommandUtils.enumerateSectionsInSegmentLoadCommand(buffer, new MachoMagicInfo(UnsignedInteger.fromIntBits(0xFEEDFACE)), command, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()), input -> {
        enumeratedSections.add(input);
        return Boolean.TRUE;
    });
    assertThat(enumeratedSections.size(), equalTo(3));
    assertThat(enumeratedSections.get(0).getSectname(), equalToObject("SECTNAME"));
    assertThat(enumeratedSections.get(0).getSegname(), equalToObject("SEGNAME"));
    assertThat(enumeratedSections.get(0).getOffset(), equalToObject(UnsignedInteger.fromIntBits(0x01)));
    assertThat(enumeratedSections.get(1).getSectname(), equalToObject("DECTNAME"));
    assertThat(enumeratedSections.get(1).getSegname(), equalToObject("DEGNAME"));
    assertThat(enumeratedSections.get(1).getOffset(), equalToObject(UnsignedInteger.fromIntBits(0x02)));
    assertThat(enumeratedSections.get(2).getSectname(), equalToObject("LECTNAME"));
    assertThat(enumeratedSections.get(2).getSegname(), equalToObject("LEGNAME"));
    assertThat(enumeratedSections.get(2).getOffset(), equalToObject(UnsignedInteger.fromIntBits(0x03)));
}
Also used : ArrayList(java.util.ArrayList) NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 18 with NulTerminatedCharsetDecoder

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

the class MachOAbsolutifyObjectPathsCommand method invokeWithParams.

@Override
protected int invokeWithParams(CommandRunnerParams params) throws IOException {
    try (RandomAccessFile file = new RandomAccessFile(getOutput().toFile(), "rw")) {
        NulTerminatedCharsetDecoder decoder = new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder());
        ObjectPathsAbsolutifier updater = new ObjectPathsAbsolutifier(file, getOldCompDir(), getUpdatedCompDir(), params.getCell().getFilesystem(), params.getCell().getKnownRoots(), decoder);
        updater.updatePaths();
    }
    return 0;
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ObjectPathsAbsolutifier(com.facebook.buck.macho.ObjectPathsAbsolutifier) NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder)

Example 19 with NulTerminatedCharsetDecoder

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

the class SectionTest method testCreatingFromBytesBigEndian32Bit.

@Test
public void testCreatingFromBytesBigEndian32Bit() throws Exception {
    Section section = SectionUtils.createFromBuffer(createBuffer(SectionTestData.getBigEndian32Bit(), 30, ByteOrder.BIG_ENDIAN), false, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
    assertThat(section.getOffsetInBinary(), equalTo(30));
    checkValues(section);
}
Also used : NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) Test(org.junit.Test)

Example 20 with NulTerminatedCharsetDecoder

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

the class SectionUtilsTest method testCreatingFromBytes64Bits.

@Test
public void testCreatingFromBytes64Bits() throws CharacterCodingException {
    ByteBuffer buffer = ByteBuffer.allocate(10 + SectionTestData.getBigEndian64Bit().length);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.position(10);
    buffer.put(SectionTestData.getBigEndian64Bit());
    buffer.position(10);
    Section section = SectionUtils.createFromBuffer(buffer, true, new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
    assertThat(section.getOffsetInBinary(), equalTo(10));
    SectionTestData.checkValues(section, true);
}
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