Search in sources :

Example 1 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder in project suite by stupidsing.

the class Assembler method assemble.

private Bytes assemble(Generalizer generalizer, List<Pair<Reference, Node>> lnis) {
    int org = ((Int) generalizer.getVariable(Atom.of(".org")).finalNode()).number;
    BytesBuilder out = new BytesBuilder();
    for (boolean isPass2 : new boolean[] { false, true }) {
        AssemblePredicates.isPass2 = isPass2;
        out.clear();
        for (Pair<Reference, Node> lni : lnis) {
            int address = org + out.size();
            if (lni.t0 != null)
                if (!isPass2)
                    lni.t0.bound(Int.of(address));
                else if (((Int) lni.t0.finalNode()).number != address)
                    Fail.t("address varied between passes at " + Integer.toHexString(address));
            out.append(assemble(isPass2, address, lni.t1));
        }
        for (Pair<Reference, Node> lni : lnis) if (lni.t0 != null && isPass2)
            lni.t0.unbound();
    }
    return out.toBytes();
}
Also used : Reference(suite.node.Reference) Node(suite.node.Node) Int(suite.node.Int) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 2 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder in project suite by stupidsing.

the class Amd64Assemble method encode.

private Bytes encode(InsnCode insnCode, byte[] vexs) {
    if (insnCode.isValid()) {
        Modrm modrm = insnCode.modrm;
        BytesBuilder bb = new BytesBuilder();
        if (vexs != null)
            bb.append(vexs);
        else {
            if (insnCode.size == 2)
                bb.append((byte) 0x66);
            appendIf(bb, modrm != null ? rex(modrm) : rex(insnCode.size, 0, 0, 0));
        }
        bb.append(insnCode.bs);
        if (modrm != null) {
            bb.append(b(modrm.rm, modrm.num, modrm.mod));
            appendIf(bb, sib(modrm));
            appendImm(bb, modrm.dispSize, modrm.disp);
        }
        appendImm(bb, insnCode.immSize, insnCode.imm);
        return bb.toBytes();
    } else
        return Fail.t("bad instruction");
}
Also used : BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 3 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder in project suite by stupidsing.

the class FileSystemMutatorImpl method read.

public Bytes read(Bytes name) {
    KeyDataStore<Bytes> store = mutate.source();
    KeyValueMutator<Bytes, Integer> kvm = store.mutate();
    KeyDataMutator<Bytes> kdm = store.mutateData();
    Bytes hash = keyUtil.hash(name);
    Integer size = kvm.get(key(hash, SIZEID, 0));
    if (size != null) {
        int seq = 0;
        BytesBuilder bb = new BytesBuilder();
        for (int s = 0; s < size; s += pageSize) bb.append(kdm.getPayload(key(hash, DATAID, seq++)));
        return bb.toBytes().range(0, size);
    } else
        return null;
}
Also used : Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 4 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder in project suite by stupidsing.

the class As method utf8decode.

public static Outlet<Chars> utf8decode(Outlet<Bytes> bytesOutlet) {
    Source<Bytes> source = bytesOutlet.source();
    return Outlet.of(new Source<>() {

        private BytesBuilder bb = new BytesBuilder();

        public Chars source() {
            Chars chars;
            while ((chars = decode()).size() == 0) {
                Bytes bytes = source.source();
                if (bytes != null)
                    bb.append(bytes);
                else if (bb.size() == 0)
                    return null;
                else
                    return Fail.t();
            }
            return chars;
        }

        private Chars decode() {
            Bytes bytes = bb.toBytes();
            CharsBuilder cb = new CharsBuilder();
            int s = 0;
            while (s < bytes.size()) {
                int b0 = Byte.toUnsignedInt(bytes.get(s++));
                int ch, e;
                if (b0 < 0x80) {
                    ch = b0;
                    e = s;
                } else if (b0 < 0xE0) {
                    ch = b0 & 0x1F;
                    e = s + 1;
                } else if (b0 < 0xF0) {
                    ch = b0 & 0x0F;
                    e = s + 2;
                } else if (b0 < 0xF8) {
                    ch = b0 & 0x07;
                    e = s + 3;
                } else if (b0 < 0xFC) {
                    ch = b0 & 0x03;
                    e = s + 4;
                } else if (b0 < 0xFE) {
                    ch = b0 & 0x01;
                    e = s + 5;
                } else
                    throw new RuntimeException();
                if (e <= bytes.size()) {
                    while (s < e) {
                        int b = Byte.toUnsignedInt(bytes.get(s++));
                        if ((b & 0xC0) == 0x80)
                            ch = (ch << 6) + (b & 0x3F);
                        else
                            Fail.t();
                    }
                    cb.append((char) ch);
                } else
                    break;
            }
            bb = new BytesBuilder();
            bb.append(bytes.range(s));
            return cb.toChars();
        }
    });
}
Also used : Bytes(suite.primitive.Bytes) CharsBuilder(suite.primitive.Chars.CharsBuilder) Chars(suite.primitive.Chars) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Example 5 with BytesBuilder

use of suite.primitive.Bytes.BytesBuilder in project suite by stupidsing.

the class LempelZivWelchTest method doTest.

private String doTest(String s0) {
    byte[] bs = s0.getBytes(Constants.charset);
    Source<Byte> source0 = new Source<>() {

        private int index;

        public Byte source() {
            return index < bs.length ? bs[index++] : null;
        }
    };
    LempelZivWelch<Byte> lzw = new LempelZivWelch<>(allBytes());
    Source<Integer> source1 = lzw.encode(source0);
    Source<Byte> source2 = lzw.decode(source1);
    BytesBuilder bb = new BytesBuilder();
    Byte b;
    while ((b = source2.source()) != null) bb.append(b);
    return To.string(bb.toBytes());
}
Also used : Source(suite.util.FunUtil.Source) BytesBuilder(suite.primitive.Bytes.BytesBuilder)

Aggregations

BytesBuilder (suite.primitive.Bytes.BytesBuilder)12 Bytes (suite.primitive.Bytes)6 Int (suite.node.Int)2 Chars (suite.primitive.Chars)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Extent (suite.file.ExtentAllocator.Extent)1 ExtentFile (suite.file.ExtentFile)1 Node (suite.node.Node)1 Reference (suite.node.Reference)1 Tree (suite.node.Tree)1 CharsBuilder (suite.primitive.Chars.CharsBuilder)1 DataInput_ (suite.util.DataInput_)1 DataOutput_ (suite.util.DataOutput_)1 Source (suite.util.FunUtil.Source)1 Serialize (suite.util.Serialize)1