public static bool Insert(Buffer dest, Buffer source) { if (dest.BitsLeft() < source.BitsLeft()) return false; Bitstream.Buffer tmp = new Bitstream.Buffer(); Copy(tmp, source); while (tmp.BitsLeft() > 0) { if (tmp.bitpos > 0) { uint bits = (uint)(8 - tmp.bitpos); if (bits > tmp.BitsLeft()) { bits = tmp.BitsLeft(); } Bitstream.PutBits(dest, bits, Bitstream.ReadBits(tmp, bits)); } if (tmp.BitsLeft() > 32) Bitstream.PutBits(dest, 32, Bitstream.ReadBits(tmp, 32)); uint left = tmp.BitsLeft(); if (left >= 8) { Bitstream.PutBits(dest, 8, Bitstream.ReadBits(tmp, 8)); } else if (left > 0) { Bitstream.PutBits(dest, left, Bitstream.ReadBits(tmp, left)); } } return true; }
public static bool PutBits(Buffer buf, int bits, UInt32 value) { if (buf.BitsLeft() < bits) { buf.error = 1; return false; } if (bits > 8) { PutBits(buf, 8, value & 0xff); PutBits(buf, bits - 8, value >> 8); return true; } if (buf.bitpos == 0) { buf.buf[buf.bytepos] = (byte)(value & s_bitmask[bits]); if (bits == 8) buf.bytepos++; else buf.bitpos = bits; } else { int room = 8 - buf.bitpos; if (bits <= room) { byte mixin = (byte)((value & s_bitmask[bits]) << buf.bitpos); buf.buf[buf.bytepos] = (byte)(buf.buf[buf.bytepos] | mixin); buf.bitpos += bits; if (buf.bitpos == 8) { buf.bitpos = 0; buf.bytepos++; } } else { PutBits(buf, room, value); return PutBits(buf, bits - room, value >> room); } } return true; }
public static UInt32 ReadBits(Buffer buf, uint bits) { if (buf.BitsLeft() < bits) { buf.error = 1; return 0; } CheatEntry ce = CheatTable[bits * 8 + buf.bitpos]; uint dpos = buf.bytepos; byte[] data = buf.buf; buf.bitpos = ce.bitpos; buf.bytepos = dpos + ce.byteofs; uint value = ((uint)data[dpos] & ce.mfirst) >> ce.s0; if (ce.count == 2) { return value | ((uint)(data[dpos+1] & ce.mlast) << ce.s1); } else if (ce.count == 3) { return value | ((uint)(data[dpos+1]) << ce.s1) | ((uint)(data[dpos+2] & ce.mlast) << ce.s2); } else if (ce.count == 4) { return value | ((uint)(data[dpos+1]) << ce.s1) | ((uint)(data[dpos+2]) << ce.s2) | ((uint)(data[dpos+3] & ce.mlast) << ce.s3); } else if (ce.count == 5) { return value | ((uint)(data[dpos+1]) << ce.s1) | ((uint)(data[dpos+2]) << ce.s2) | ((uint)(data[dpos+3]) << ce.s3) | ((uint)(data[dpos+4] & ce.mlast) << ce.s4); } return value; }
public static float ReadFloat(Buffer buf) { if (buf.BitsLeft () < 32) { return 0; } byte[] tmp = new byte[4]; for (int i = 0; i != 4; i++) { tmp[i] = (byte) ReadBits(buf, 8); } float[] f = new float[1]; System.Buffer.BlockCopy(tmp, 0, f, 0, 4); return f[0]; }
public static bool PutBits(Buffer buf, uint bits, uint value) { if (buf.BitsLeft() < bits) { buf.error = 1; return false; } PutBitsU(buf, bits, value); return true; }
public static float ReadFloat(Buffer buf) { SyncByte(buf); if (buf.BitsLeft() < 32) return 0; float f = BitConverter.ToSingle(buf.buf, buf.bytepos); buf.bytepos += 4; return f; }
public static UInt32 ReadBits(Buffer buf, int bits) { if (buf.BitsLeft() < bits) { buf.error = 1; return 0; } int max = 8 - buf.bitpos; if (bits <= max) { UInt32 val = ((UInt32)(buf.buf[buf.bytepos] >> buf.bitpos)) & s_bitmask[bits]; buf.bitpos += bits; if (buf.bitpos == 8) { buf.bitpos = 0; buf.bytepos++; } return val; } return ReadBits(buf, max) | (ReadBits(buf, bits - max) << max); }