예제 #1
0
        void Read(IO.BitStream bs)
        {
            int  uncompressed_size;
            bool is_compressed;

            bs.Read(out uncompressed_size, mOwner.kInfo.BufferSizeBitLength); int size = uncompressed_size;
            if (uncompressed_size > mOwner.kInfo.BufferMaxSize)
            {
                throw new System.IO.InvalidDataException("Input string table buffer size too large by (bytes): " +
                                                         (uncompressed_size - mOwner.kInfo.BufferMaxSize).ToString());
            }

            bs.Read(out is_compressed);
            if (is_compressed)
            {
                bs.Read(out size, mOwner.kInfo.BufferSizeBitLength);
            }

            Buffer = new byte[size];
            bs.Read(Buffer);

            if (is_compressed)
            {
                Buffer = IO.Compression.ZLib.LowLevelDecompress(Buffer, uncompressed_size);
            }
        }
예제 #2
0
        /// <remarks>Used for indexes which *are not* typically NONE (-1)</remarks>
        public static void StreamIndexPos(this IO.BitStream s, ref int value, int bitCount = Bits.kInt32BitCount)
        {
            Contract.Requires(bitCount <= Bits.kInt32BitCount);

            if (s.IsReading)
            {
                bool not_none = s.ReadBoolean();

                if (not_none)
                {
                    s.Read(out value, bitCount);
                }
                else
                {
                    value = TypeExtensions.kNone;
                }
            }
            else if (s.IsWriting)
            {
                bool not_none = value.IsNotNone();
                s.Write(not_none);

                if (not_none)
                {
                    s.Write(value, bitCount);
                }
            }
        }
예제 #3
0
        static uint Read(IO.BitStream s, out float real, float min, float max, int bitCount, bool isSigned, bool unknown)
        {
            uint raw_bits;

            s.Read(out raw_bits, bitCount);

            real = DecodeSingle(raw_bits, min, max, bitCount, isSigned, unknown);
            return(raw_bits);
        }
예제 #4
0
        void Read(IO.BitStream s)
        {
            int count; s.Read(out count, kInfo.CountBitLength);

            ReferencesRead(s, count);
            if (HasStrings)
            {
                var string_data = new LocaleStringTableBuffer(this);
                s.StreamObject(string_data);
                ReadStringsFromBuffer(string_data);
            }
        }
예제 #5
0
        void Read(IO.BitStream bs)
        {
            for (int x = 0, mask = 1; x < kInfo.kLength; x++, mask <<= 1)
            {
                bool has_value;
                bs.Read(out has_value);

                if (has_value)
                {
                    mValidFlags |= (uint)mask;
                    bs.Stream(ref mArray[x],
                              kInfo.kRangeMin, kInfo.kRangeMax, kInfo.kBitCount, kInfo.kSigned, kInfo.kUnknown);
                }
            }
        }
예제 #6
0
        /// <summary>Read a multi-byte CString from an endian stream</summary>
        /// <param name="s">Bitstream to read from</param>
        /// <param name="ms">Stream to write the character's bytes to</param>
        /// <param name="maxLength">Optional maximum length of this specific string</param>
        void ReadCStringMultiByte(IO.BitStream s, System.IO.MemoryStream ms, int maxLength)
        {
            byte[] characters;
            if (maxLength > 0)
            {
                int x = 0;
                characters = new byte[mNullCharacterSize];
                while (!ReadStringMultiByteIsNull(mStorage.ByteOrder, s.Read(characters), 0) && ++x <= maxLength)
                {
                    ms.Write(characters, 0, characters.Length);
                }
            }
            else if (!mStorage.IsFixedLength)
            {
                characters = new byte[mNullCharacterSize];
                while (!ReadStringMultiByteIsNull(mStorage.ByteOrder, s.Read(characters), 0))
                {
                    ms.Write(characters, 0, characters.Length);
                }
            }
            else
            {
                characters = s.ReadBytes(mFixedLengthByteLength);

                int x;
                for (x = 0; x < characters.Length - mNullCharacterSize; x += mNullCharacterSize)
                {
                    if (ReadStringMultiByteIsNull(mStorage.ByteOrder, characters, x))
                    {
                        break;
                    }
                }

                ms.Write(characters, 0, x);
            }
        }
예제 #7
0
        /// <summary>Streams an integer which is >= -1, but when streamed out the value is added by 1 (so it will be >= 0)</summary>
        /// <param name="s"></param>
        /// <param name="value"></param>
        /// <param name="bitCount"></param>
        public static void StreamNoneable(this IO.BitStream s, ref int value, int bitCount = Bits.kInt32BitCount)
        {
            Contract.Requires(bitCount <= Bits.kInt32BitCount);

            if (s.IsReading)
            {
                s.Read(out value, bitCount);
                value--;
            }
            else if (s.IsWriting)
            {
                Contract.Assert(value.IsNoneOrPositive());
                s.Write(value + 1, bitCount);
            }
        }
예제 #8
0
        /// <summary>Streams an integer which is >= -1, but when streamed out the value is added by 1 (so it will be >= 0)</summary>
        /// <param name="s"></param>
        /// <param name="value"></param>
        /// <param name="bitCount"></param>
        public static void StreamNoneable(this IO.BitStream s, ref short value, int bitCount = Bits.kInt16BitCount)
        {
            Contract.Requires(bitCount <= Bits.kInt16BitCount);

            if (s.IsReading)
            {
                s.Read(out value, bitCount);
                value--;
            }
            else if (s.IsWriting)
            {
                Contract.Assert(value >= TypeExtensions.kNone);
                s.Write(value + 1, bitCount);
            }
        }
예제 #9
0
        public void IO_BitStreamLogicTest()
        {
            var values = new KeyValuePair <uint, int>[] {
                new KeyValuePair <uint, int>(10, 7),
                new KeyValuePair <uint, int>(0xBEEFBEEF, 32),
                new KeyValuePair <uint, int>(12, 7),
                new KeyValuePair <uint, int>(0x13371337, 32),
                new KeyValuePair <uint, int>(123, 7),
                new KeyValuePair <uint, int>(0xDEADC0DE, 32),
                new KeyValuePair <uint, int>(0, 7),
                new KeyValuePair <uint, int>(111, 7),

                new KeyValuePair <uint, int>(1, 1),
                new KeyValuePair <uint, int>(2, 2),
                new KeyValuePair <uint, int>(7, 3),
                new KeyValuePair <uint, int>(14, 4),
                new KeyValuePair <uint, int>(21, 5),
                new KeyValuePair <uint, int>(42, 6),
                new KeyValuePair <uint, int>(14406, 15),
            };

            using (var ms = new MemoryStream())
            {
                using (var bs = new IO.BitStream(ms, FileAccess.Write))
                {
                    int expected_bit_position = 0;

                    bs.StreamMode = FileAccess.Write;
                    foreach (var kv in values)
                    {
                        bs.WriteWord(kv.Key, kv.Value);
                        expected_bit_position += kv.Value;
                        Assert.AreEqual(expected_bit_position, bs.BitPosition, "Value=" + kv.Key);
                    }
                }
                Text.Util.ByteArrayToStream(ms.ToArray(), System.Console.Out);
                System.Console.WriteLine();

                ms.Position = 0;
                using (var bs_old = new BKSystem.IO.BitStream(ms))
                {
                    foreach (var kv in values)
                    {
                        bs_old.Read(out uint word, 0, kv.Value);
                        Assert.AreEqual(kv.Key, word);
                    }
                }
            }

            using (var ms = new MemoryStream())
            {
                using (var bs_old = new BKSystem.IO.BitStream())
                {
                    foreach (var kv in values)
                    {
                        bs_old.Write(kv.Key, 0, kv.Value);
                    }
                    bs_old.WriteTo(ms);
                }
                Text.Util.ByteArrayToStream(ms.ToArray(), System.Console.Out);
                System.Console.WriteLine();

                ms.Position = 0;
                using (var bs = new IO.BitStream(ms, FileAccess.Read))
                {
                    bs.StreamMode = FileAccess.Read;
                    foreach (var kv in values)
                    {
                        bs.ReadWord(out uint word, kv.Value);
                        Assert.AreEqual(kv.Key, word);
                    }
                }
            }

            using (var ms = new MemoryStream())
            {
                using (var bs = new IO.BitStream(ms, FileAccess.Write))
                {
                    bs.StreamMode = FileAccess.Write;
                    bs.Write((int)1337, 15);
                    bs.Write(-21474836480L, 60);
                    bs.Write(-1, 30);
                    bs.Write(false);
                    bs.Write((int)0xDEDEAD, 27);
                }
                Text.Util.ByteArrayToStream(ms.ToArray(), System.Console.Out);
                System.Console.WriteLine();

                ms.Position = 0;
//				using (var bs_old = new BKSystem.IO.BitStream(ms))
                using (var bs_old = new IO.BitStream(ms, FileAccess.Read))
                {
                    bs_old.StreamMode = FileAccess.Read;

                    //bs_old.Read(out _int, 0, 15);
                    bs_old.Read(out int _int, 15);
                    Assert.AreEqual(1337, _int);

                    bs_old.Read(out long _long, 60, signExtend: true);
                    Assert.AreEqual(-21474836480L, _long);
                    //bs_old.Read(out _int, 0, 30);
                    bs_old.Read(out _int, 30, signExtend: true);
                    Assert.AreEqual(-1, _int);

                    bs_old.Read(out bool _bool);
                    Assert.AreEqual(false, _bool);

                    //bs_old.Read(out _int, 0, 27);
                    bs_old.Read(out _int, 27);
                    Assert.AreEqual((int)0xDEDEAD, _int);
                }
            }
        }