示例#1
0
        public static string ReadString(this BitsReader bitsReader, BinarySize size, Encoding encoding)
        {
            Debug.WriteLine("Reading String...");
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            var buffer = bitsReader.ReadBits(size);
            var result = encoding.GetString(buffer);

            Debug.WriteLine("String = {0}.", result);
            return(result);
        }
示例#2
0
        public static object ReadStruct(this BitsReader bitsReader, Type structType, BinarySize size, Endianness endianness)
        {
            if (structType == null)
            {
                throw new ArgumentNullException("structType");
            }
            byte[] readBuffer = bitsReader.ReadBits(size, endianness);
            if (readBuffer.Length > 1 && !endianness.MatchesMachineEndianness())
            {
                Debug.WriteLine("Endianness mismatch. Reversing bytes...");
                Array.Reverse(readBuffer);
                Debug.WriteLine(string.Concat("Bytes: ", readBuffer.ToCsvString()));
            }
            GCHandle handle    = GCHandle.Alloc(readBuffer, GCHandleType.Pinned);
            var      structure = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), structType);

            handle.Free();
            return(structure);
        }
示例#3
0
        public byte[] ReadBits(BinarySize size, Endianness?endianness = null)
        {
            if (size.Bytes == 0 && size.Bits == 0)
            {
                return(new byte[0]);
            }

/*
 *          byte[] result;
 *          Debug.WriteLine("Reading {0} bits...", size.TotalBits);
 *          var position = Position;
 *          var hasBitsInCurrentByte = (byte) (Constants.BitsInByte - _bitPosition);
 *          Debug.Assert(hasBitsInCurrentByte <= 8);
 *          var bitsInCurrentByte = (byte) Math.Min(hasBitsInCurrentByte, size.TotalBits);
 *          Debug.Assert(bitsInCurrentByte <= 8 && bitsInCurrentByte <= hasBitsInCurrentByte);
 *          var bitsAfterCurrentByte = size.TotalBits - bitsInCurrentByte;
 *          Debug.Assert(bitsAfterCurrentByte == 0 ||
 *                       (bitsAfterCurrentByte > 0 && hasBitsInCurrentByte == bitsInCurrentByte));
 *          var bitsInLastByte = (byte) (bitsAfterCurrentByte%Constants.BitsInByte);
 *          Debug.Assert(bitsInLastByte < 8);
 *          int bytesCount = 1 + (int) (bitsAfterCurrentByte/Constants.BitsInByte);
 *          if (bitsInLastByte > 0)
 *              bytesCount++;
 *          var bytes = _br.ReadBytes(bytesCount);
 *          var bits = bytes.SelectMany(bs =>
 *              {
 *                  var bts = new[] {bs}.ToBits();
 *                  Array.Reverse(bts);
 *                  return bts;
 *              }).ToArray();
 *          bits = bits.Skip(_bitPosition).Take((int) size.TotalBits).ToArray();
 *          result = ConvertBitsToBytes(bits, endianness);
 *          Position = position.Add(size);
 *          return result;
 */
            Debug.WriteLine("Reading {0} bits...", size.TotalBits);
            var position    = Position;
            var newPosition = position + size;
            var bytesCount  = (newPosition - position).Bytes + (newPosition.Bits > 0 ? 1 : 0);

            Debug.WriteLine("Reading {0} bytes...", bytesCount);
            var bytes = _br.ReadBytes((int)bytesCount);

            Debug.WriteLine(string.Concat("Bytes: ", bytes.ToCsvString()));
            if (position.Bits > 0)
            {
                Debug.WriteLine("Truncating first byte {0:X}...", bytes[0]);
                bytes[0] = (Byte)(bytes[0] & (Byte)(Byte.MaxValue >> position.Bits));
                Debug.WriteLine("First byte truncated: {0:X}.", bytes[0]);
            }
            if (newPosition.Bits > 0)
            {
                Debug.WriteLine("Truncating last byte {0:X}...", bytes[bytes.Length - 1]);
                bytes[bytes.Length - 1] = (Byte)(bytes[bytes.Length - 1] & (Byte)(Byte.MaxValue << (Constants.BitsInByte - newPosition.Bits)));
                Debug.WriteLine("Last byte truncated: {0:X}.", bytes[bytes.Length - 1]);
            }
            int bitsInLastByte = newPosition.Bits;

            if (endianness == Endianness.BigEndian)
            {
                if (bytes.Length > 1 && newPosition.Bits > 0)
                {
                    Debug.WriteLine("Left shifting bytes by {0} bits...", newPosition.Bits);
                    bytes = LshBytes(bytes, newPosition.Bits);
                    Debug.WriteLine(string.Concat("Bytes: ", bytes.ToCsvString()));
                    bitsInLastByte = 0;
                }
            }
            else
            {
                if (bytes.Length > 1 && position.Bits > 0 && endianness == Endianness.LittleEndian)
                {
                    Debug.WriteLine("Right shifting bytes by {0} bits...", Constants.BitsInByte - position.Bits);
                    bytes = RshBytes(bytes, Constants.BitsInByte - position.Bits);
                    Debug.WriteLine(string.Concat("Bytes: ", bytes.ToCsvString()));
                    bitsInLastByte += Constants.BitsInByte - position.Bits;
                }
            }
            bitsInLastByte %= Constants.BitsInByte;
            if (bitsInLastByte > 0)
            {
                Debug.WriteLine("Shifting last byte {0:X}...", bytes[bytes.Length - 1]);
                bytes[bytes.Length - 1] = (byte)(bytes[bytes.Length - 1] >> (Constants.BitsInByte - bitsInLastByte));
                Debug.WriteLine("Last byte shifted: {0:X}.", bytes[bytes.Length - 1]);
            }
            Position = newPosition;
            return(bytes);
        }
示例#4
0
 public static T ReadStruct <T>(this BitsReader bitsReader, BinarySize size, Endianness endianness)
 {
     return((T)bitsReader.ReadStruct(typeof(T), size, endianness));
 }
示例#5
0
 public BinaryOffset Substract(BinarySize other)
 {
     return(Substract((BinaryOffset)other));
 }
示例#6
0
 public BinaryOffset Add(BinarySize other)
 {
     return(Add((BinaryOffset)other));
 }