Example #1
0
        void LzUnpack(IBinaryStream input, byte[] output)
        {
            int  dst  = 0;
            int  bits = 0;
            byte mask = 0;

            while (dst < output.Length)
            {
                mask >>= 1;
                if (0 == mask)
                {
                    bits = input.ReadByte();
                    if (-1 == bits)
                    {
                        break;
                    }
                    mask = 0x80;
                }
                if ((mask & bits) != 0)
                {
                    int offset = input.ReadUInt16();
                    int count  = (offset >> 12) + 1;
                    offset &= 0xFFF;
                    Binary.CopyOverlapped(output, dst - offset, dst, count);
                    dst += count;
                }
                else
                {
                    output[dst++] = input.ReadUInt8();
                }
            }
        }
 protected override void ReadArgs(IBinaryStream reader)
 {
     if (IsOffset)
     {
         ReadString(reader);
     }
     else
     {
         var startOffset = reader.ReadInt32LE() - Offset;
         if (startOffset != 10)
         {
             throw new InvalidDataException($"code [55] start {startOffset} != 10");
         }
         var unknown = reader.ReadByte();
         if (unknown != 0x01)
         {
             throw new InvalidDataException("code [55] separator != 0x01");
         }
         var endOffset = reader.ReadInt32LE() - Offset;
         ReadString(reader);
         if (endOffset != 10 + reader.GetStringZByteCount(Content))
         {
             throw new InvalidDataException($"code [55] end {endOffset} != 10 + strlen {reader.GetStringZByteCount(Content)}");
         }
     }
 }
Example #3
0
 public bool Decode(byte[] output, int dst_pos, int output_size, IBinaryStream input, out int output_chunk_size)
 {
     for (int i = 0; i < Length; ++i)
     {
         if (0 != (m_ctl_bits[i >> 3] & 1))
         {
             m_data[i] = 0;
         }
         else
         {
             int next = input.ReadByte();
             if (-1 == next)
             {
                 break;
             }
             m_data[i] = (byte)next;
         }
         m_ctl_bits[i >> 3] >>= 1;
     }
     m_data[0] ^= m_last_byte;
     for (int i = 0; i < Length; ++i)
     {
         m_data[i + 1] ^= m_data[i];
     }
     m_last_byte       = m_data[Length - 1];
     output_chunk_size = Math.Min(Length, output_size);
     Buffer.BlockCopy(m_data, 0, output, dst_pos, output_chunk_size);
     return(Length > output_size);
 }
Example #4
0
        public byte[] Unpack()
        {
            m_input.Position = 12;
            m_dst            = 0;
            for (int i = 0; i < m_chunk_count; ++i)
            {
                int ctl = m_input.ReadByte();
                if (-1 == ctl)
                {
                    break;
                }
                int count = (ctl & 0xF) + 1;
                switch (ctl & 0xF0)
                {
                case 0x00:  Chunk00(count); break;

                case 0x10:  Chunk10(count); break;

                case 0x40:  Chunk40(count); break;

                case 0x80:  Chunk80(count); break;
                }
            }
            return(m_output);
        }
Example #5
0
        ushort LoadBits(int count)
        {
            word_471D60 <<= count;
            if (count > m_bits_avail)
            {
                do
                {
                    count       -= m_bits_avail;
                    word_471D60 |= (ushort)(m_bits << count);
                    int bits = m_input.ReadByte();
                    if (-1 == bits)
                    {
                        bits  = 0;
                        m_eof = true;
                    }
                    m_bits       = bits;
                    m_bits_avail = 8;
                }while (count > 8);
            }
            int    v      = m_bits_avail - count;
            ushort result = (ushort)(m_bits >> v);

            m_bits_avail = v;
            word_471D60 |= result;
            return(result);
        }
Example #6
0
        DelphiObject DeserializeNode()
        {
            int type_len = m_input.ReadByte();

            if (type_len <= 0)
            {
                return(null);
            }
            var node = new DelphiObject();

            node.Type = ReadString(type_len);
            node.Name = ReadString();
            int key_length;

            while ((key_length = m_input.ReadUInt8()) > 0)
            {
                var key = ReadString(key_length);
                node.Props[key] = ReadValue();
            }
            DelphiObject child;

            while ((child = DeserializeNode()) != null)
            {
                node.Contents.Add(child);
            }
            return(node);
        }
Example #7
0
        void Unpack24()
        {
            int dst  = 0;
            int bits = 0;
            int mask = 0;

            while (dst < m_output.Length)
            {
                mask >>= 1;
                if (0 == mask)
                {
                    bits = m_input.ReadByte();
                    mask = 0x80;
                }
                if (0 != (bits & mask))
                {
                    m_input.Read(m_output, dst, 3);
                    dst += 4;
                }
                else
                {
                    int offset = m_input.ReadUInt16();
                    int count  = (1 + (offset & 0xF)) * 4;
                    offset = (1 + (offset >> 4)) * 4;
                    Binary.CopyOverlapped(m_output, dst - offset, dst, count);
                    dst += count;
                }
            }
        }
Example #8
0
        void UnpackChannel(IBinaryStream input, byte[] output)
        {
            int dst = 0;

            while (dst < output.Length)
            {
                int count = input.ReadByte();
                if (count < 0)
                {
                    break;
                }
                if (count <= 0x32)
                {
                    input.Read(output, dst, count);
                    dst += count;
                }
                else
                {
                    count -= 0x32;
                    byte v = input.ReadUInt8();
                    while (count-- > 0)
                    {
                        output[dst++] = v;
                    }
                }
            }
        }
Example #9
0
        bool SkipFrameTable(IBinaryStream file)
        {
            file.Position = 4;
            int table_count = file.ReadInt16();

            file.Position = 8;
            for (int i = 0; i < table_count; ++i)
            {
                switch (file.ReadByte())
                {
                case 0: break;

                case 1: file.Seek(8, SeekOrigin.Current); break;

                case 2:
                case 3:
                case 4:
                case 5: file.Seek(4, SeekOrigin.Current); break;

                default: return(false);
                }
            }
            int count = file.ReadUInt16();

            file.Seek(count * 8, SeekOrigin.Current);
            return(true);
        }
Example #10
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            stream.Position = 4;
            var pixels = new byte[3 * info.Width * info.Height];
            int dst    = 0;

            while (dst < pixels.Length)
            {
                int count = stream.ReadByte();
                if (-1 == count)
                {
                    throw new EndOfStreamException();
                }
                if (0 != (count & 0x80))
                {
                    count = 3 * (count & 0x7F);
                    stream.Read(pixels, dst, count);
                    dst += count;
                }
                else
                {
                    count *= 3;
                    stream.Read(pixels, dst, 3);
                    Binary.CopyOverlapped(pixels, dst, dst + 3, count - 3);
                    dst += count;
                }
            }
            return(ImageData.Create(info, PixelFormats.Bgr24, null, pixels));
        }
Example #11
0
        void UnpackRle()
        {
            int dst = 0;

            while (dst < m_output.Length)
            {
                int count = m_input.ReadByte();
                if (-1 == count)
                {
                    break;
                }
                if (0 != (count & 0x80))
                {
                    byte v = m_input.ReadUInt8();
                    count = (count & 0x7F) + 1;
                    while (count-- > 0)
                    {
                        m_output[dst++] = v;
                    }
                }
                else
                {
                    count++;
                    m_input.Read(m_output, dst, count);
                    dst += count;
                }
            }
        }
Example #12
0
            public void Unpack(byte[] output)
            {
                ReadDict();
                var  root = BuildTree();
                int  dst  = 0;
                int  bits = 0;
                byte mask = 0;

                while (dst < output.Length)
                {
                    var token = root;
                    while (token > 0x100)
                    {
                        if (0 == mask)
                        {
                            bits = m_input.ReadByte();
                            if (-1 == bits)
                            {
                                return;
                            }
                            mask = 0x80;
                        }
                        if ((bits & mask) != 0)
                        {
                            token = m_tree[token].RNode;
                        }
                        else
                        {
                            token = m_tree[token].LNode;
                        }
                        mask >>= 1;
                    }
                    output[dst++] = (byte)token;
                }
            }
Example #13
0
        public override SoundInput TryOpen(IBinaryStream file)
        {
            var  header      = file.ReadHeader(0x10);
            int  samples     = header.ToInt32(4);
            uint sample_rate = header.ToUInt32(8);

            if (sample_rate < 8000 || sample_rate > 96000)
            {
                return(null);
            }
            ushort channels = header.ToUInt16(12);

            if (channels != 1 && channels != 2)
            {
                return(null);
            }
            samples *= channels;

            var output = new byte[2 * samples];
            var first  = new AdpDecoder();
            var second = first;

            if (channels > 1)
            {
                second = new AdpDecoder();
            }
            int dst = 0;

            while (samples > 0)
            {
                int v = file.ReadByte();
                if (-1 == v)
                {
                    break;
                }
                LittleEndian.Pack(first.DecodeSample(v), output, dst);
                if (0 == --samples)
                {
                    break;
                }
                dst += 2;
                LittleEndian.Pack(second.DecodeSample(v >> 4), output, dst);
                dst += 2;
                --samples;
            }

            var format = new WaveFormat {
                FormatTag             = 1,
                Channels              = channels,
                SamplesPerSecond      = sample_rate,
                AverageBytesPerSecond = 2u * channels * sample_rate,
                BlockAlign            = (ushort)(2 * channels),
                BitsPerSample         = 16,
            };
            var pcm   = new MemoryStream(output);
            var sound = new RawPcmInput(pcm, format);

            file.Dispose();
            return(sound);
        }
Example #14
0
        int ReadPacketSize(IBinaryStream input)
        {
            int lo = input.ReadByte();

            if (-1 == lo)
            {
                return(0);
            }
            int hi = input.ReadByte();

            if (-1 == hi)
            {
                return(0);
            }
            return(hi << 8 | lo);
        }
Example #15
0
        internal byte[] LzDecompress(IBinaryStream input)
        {
            int unpacked_size = input.ReadInt32();
            var data          = new byte[unpacked_size];
            int dst           = 0;

            while (dst < unpacked_size)
            {
                int ctl = input.ReadByte();
                if (-1 == ctl)
                {
                    throw new EndOfStreamException();
                }
                if (0 != (ctl & 0x80))
                {
                    byte lo     = input.ReadUInt8();
                    int  offset = ((ctl << 3 | lo >> 5) & 0x3FF) + 1;
                    int  count  = (lo & 0x1F) + 1;
                    Binary.CopyOverlapped(data, dst - offset, dst, count);
                    dst += count;
                }
                else
                {
                    int count = ctl + 1;
                    if (input.Read(data, dst, count) != count)
                    {
                        throw new EndOfStreamException();
                    }
                    dst += count;
                }
            }
            return(data);
        }
Example #16
0
        public override ImageData Read(IBinaryStream file, ImageMetaData info)
        {
            file.Position = 0xC;
            var palette = ReadColorMap(file.AsStream, 256, PaletteFormat.Bgr);
            int stride  = (int)info.Width * 3;
            var pixels  = new byte[stride * (int)info.Height];
            int dst     = 0;

            while (dst < pixels.Length)
            {
                int ctl = file.PeekByte();
                if (-1 == ctl)
                {
                    break;
                }
                if (1 == ctl)
                {
                    file.ReadByte();
                    int idx   = file.ReadByte();
                    var color = palette[idx];
                    pixels[dst]     = color.B;
                    pixels[dst + 1] = color.G;
                    pixels[dst + 2] = color.R;
                    dst            += 3;
                }
                else if (2 == ctl)
                {
                    file.ReadByte();
                    int idx   = file.ReadByte();
                    int count = file.ReadByte();
                    var color = palette[idx];
                    for (int i = 0; i < count; ++i)
                    {
                        pixels[dst]     = color.B;
                        pixels[dst + 1] = color.G;
                        pixels[dst + 2] = color.R;
                        dst            += 3;
                    }
                }
                else
                {
                    file.Read(pixels, dst, 3);
                    dst += 3;
                }
            }
            return(ImageData.CreateFlipped(info, PixelFormats.Bgr24, null, pixels, stride));
        }
Example #17
0
 protected override IEnumerator <int> Unpack()
 {
     for (int i = 0; i < Chunks; ++i)
     {
         int ctl = m_input.ReadByte();
         if (-1 == ctl)
         {
             yield break;
         }
         int count;
         if (ctl <= 1)
         {
             if (0 == ctl)
             {
                 count = m_input.ReadUInt8();
             }
             else
             {
                 count = 0x100;
             }
             while (count > 0)
             {
                 int avail = Math.Min(count, m_length);
                 int read  = m_input.Read(m_buffer, m_pos, avail);
                 if (0 == read)
                 {
                     yield break;
                 }
                 count    -= read;
                 m_pos    += read;
                 m_length -= read;
                 if (0 == m_length)
                 {
                     yield return(m_pos);
                 }
             }
         }
         else
         {
             if (3 == ctl)
             {
                 count = m_input.ReadUInt16();
             }
             else
             {
                 count = ctl;
             }
             byte v = m_input.ReadUInt8();
             while (count-- > 0)
             {
                 m_buffer[m_pos++] = v;
                 if (0 == --m_length)
                 {
                     yield return(m_pos);
                 }
             }
         }
     }
 }
Example #18
0
        byte[] UnpackCfp0(IBinaryStream input)
        {
            input.Position = 8;
            int unpacked_size = input.ReadInt32();
            var output        = new byte[unpacked_size];
            int dst           = 0;

            while (dst < output.Length)
            {
                int cmd   = input.ReadByte();
                int count = 0;
                switch (cmd)
                {
                case 0:
                    count = input.ReadUInt8();
                    input.Read(output, dst, count);
                    break;

                case 1:
                    count = input.ReadInt32();
                    input.Read(output, dst, count);
                    break;

                case 2:
                {
                    count = input.ReadUInt8();
                    byte v = input.ReadUInt8();
                    for (int i = 0; i < count; ++i)
                    {
                        output[dst + i] = v;
                    }
                    break;
                }

                case 3:
                {
                    count = input.ReadInt32();
                    byte v = input.ReadUInt8();
                    for (int i = 0; i < count; ++i)
                    {
                        output[dst + i] = v;
                    }
                    break;
                }

                case 6:
                    int offset = input.ReadUInt16();
                    count = input.ReadUInt16();
                    Binary.CopyOverlapped(output, dst - offset, dst, count);
                    break;

                case 15:
                case -1:
                    return(output);
                }
                dst += count;
            }
            return(output);
        }
Example #19
0
            public void Unpack()
            {
                int pixels_remaining = m_data.Length;
                int data_pos         = 0;
                int eax = 0;

                while (pixels_remaining > 0)
                {
                    int count = eax * 3 + 3;
                    if (count > pixels_remaining)
                    {
                        throw new InvalidFormatException();
                    }
                    pixels_remaining -= count;
                    if (count != m_input.Read(m_data, data_pos, count))
                    {
                        throw new InvalidFormatException();
                    }
                    data_pos += count;

                    while (pixels_remaining > 0)
                    {
                        eax = m_input.ReadByte();
                        if (0 == (eax & 0x80))
                        {
                            if (0x7f == eax)
                            {
                                eax += m_input.ReadUInt16();
                            }
                            break;
                        }
                        int shift_index = eax >> 2;
                        eax &= 3;
                        if (3 == eax)
                        {
                            eax += m_input.ReadUInt16();
                        }

                        count = eax * 3 + 3;
                        if (pixels_remaining < count)
                        {
                            throw new InvalidFormatException();
                        }
                        pixels_remaining -= count;
                        int shift     = ShiftTable[shift_index & 0x1f];
                        int shift_row = shift & 0x0f;
                        shift    >>= 4;
                        shift_row *= (int)m_width;
                        shift     -= shift_row;
                        shift     *= 3;
                        if (shift >= 0 || data_pos + shift < 0)
                        {
                            throw new InvalidFormatException();
                        }
                        Binary.CopyOverlapped(m_data, data_pos + shift, data_pos, count);
                        data_pos += count;
                    }
                }
            }
Example #20
0
        void Unpack(IBinaryStream input, int input_size, byte[] output)
        {
            int dst = 0;

            while (input_size > 0)
            {
                int count;
                int ctl = input.ReadByte();
                --input_size;
                if (0 == (ctl & 0xC0))
                {
                    count = ((ctl & 0x3F) + 1) * 4;
                    input.Read(output, dst, count);
                    input_size -= count;
                }
                else
                {
                    int offset;
                    if (0x40 == (ctl & 0xC0))
                    {
                        count  = (ctl & 0x3F) + 1;
                        offset = 1;
                    }
                    else if (0x80 == (ctl & 0xE0))
                    {
                        count  = (ctl & 0x1F) + 1;
                        offset = input.ReadByte();
                        --input_size;
                    }
                    else if (0xA0 == (ctl & 0xE0))
                    {
                        count       = (ctl & 0x1F) + 1;
                        offset      = input.ReadUInt16();
                        input_size -= 2;
                    }
                    else
                    {
                        continue;
                    }
                    count  *= 4;
                    offset *= 4;
                    Binary.CopyOverlapped(output, dst - offset, dst, count);
                }
                dst += count;
            }
        }
Example #21
0
 internal static uint ReadUInt(IBinaryStream input)
 {
     if (input.ReadByte() != 4)
     {
         throw new InvalidFormatException();
     }
     return(Binary.BigEndian(input.ReadUInt32()));
 }
Example #22
0
        }                                                        // 'OggS'

        public override SoundInput TryOpen(IBinaryStream file)
        {
            file.Position = 0x1E;
            int offset = file.ReadByte();

            if (offset <= 0)
            {
                return(null);
            }
            file.Position = 0x20 + offset;
            if (!(file.ReadByte() == 'O' && file.ReadByte() == 'g' &&
                  file.ReadByte() == 'g' && file.ReadByte() == 'S'))
            {
                return(null);
            }
            return(new OggInput(new StreamRegion(file.AsStream, 0x20 + offset)));
        }
Example #23
0
 protected void ReadHeader(int start)
 {
     m_input.Position = start;
     SkipChunk();
     m_title = ReadString();
     if (m_version.Major < 3)
     {
         m_input.ReadCString();
     }
     SkipString();
     SkipString();
     m_input.ReadByte();
     SkipString();
     SkipString();
     SkipDict();
     m_input.ReadByte();
 }
Example #24
0
        public override ImageMetaData ReadMetaData(IBinaryStream file)
        {
            file.Position = 4;
            int type = file.ReadByte();
            int bpp  = file.ReadByte();

            if (24 != bpp && 32 != bpp)
            {
                throw new NotSupportedException("Not supported CPB image format");
            }
            int version = file.ReadInt16();

            if (1 != version && 0 != version)
            {
                throw new NotSupportedException("Not supported CPB image version");
            }
            var info = new CpbMetaData {
                Type    = type,
                Version = version,
                BPP     = bpp,
            };

            if (1 == version)
            {
                file.ReadUInt32();
                info.Width      = file.ReadUInt16();
                info.Height     = file.ReadUInt16();
                info.Channel[0] = file.ReadUInt32();
                info.Channel[1] = file.ReadUInt32();
                info.Channel[2] = file.ReadUInt32();
                info.Channel[3] = file.ReadUInt32();
            }
            else
            {
                info.Width  = file.ReadUInt16();
                info.Height = file.ReadUInt16();
                file.ReadUInt32();
                info.Channel[0] = file.ReadUInt32();
                info.Channel[1] = file.ReadUInt32();
                info.Channel[2] = file.ReadUInt32();
                info.Channel[3] = file.ReadUInt32();
            }
            info.DataOffset = (uint)file.Position;
            return(info);
        }
Example #25
0
        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            if ('A' != stream.ReadByte() || 'M' != stream.ReadByte())
            {
                return(null);
            }
            var header = new byte[0x30];

            if (0x2e != stream.Read(header, 2, 0x2e))
            {
                return(null);
            }
            uint size = LittleEndian.ToUInt32(header, 2);

            if (size != stream.Length)
            {
                return(null);
            }
            int am_type = header[0x16];

            if (am_type != 2 && am_type != 1 || header[0x18] != 1)
            {
                return(null);
            }
            var info = new AmMetaData();

            info.Width            = LittleEndian.ToUInt16(header, 6);
            info.Height           = LittleEndian.ToUInt16(header, 8);
            info.MaskWidth        = LittleEndian.ToUInt16(header, 0x0a);
            info.MaskHeight       = LittleEndian.ToUInt16(header, 0x0c);
            info.Colors           = LittleEndian.ToUInt16(header, 0x12);
            info.BPP              = header[0x14];
            info.IsCompressed     = 0 != header[0x15];
            info.DataOffset       = LittleEndian.ToUInt32(header, 0x1a);
            info.DataLength       = LittleEndian.ToUInt32(header, 0x1e);
            info.MaskOffset       = LittleEndian.ToUInt32(header, 0x22);
            info.MaskLength       = LittleEndian.ToUInt32(header, 0x26);
            info.IsMaskCompressed = 0 != header[0x2a];
            if (checked (info.DataLength + info.MaskLength) > size)
            {
                return(null);
            }
            return(info);
        }
Example #26
0
        void LzUnpack(IBinaryStream input, byte[] output)
        {
            var frame     = new byte[0x1000];
            int dst       = 0;
            int bits      = 0;
            int mask      = 0;
            int frame_pos = 0xFEE;

            while (dst < output.Length)
            {
                mask >>= 1;
                if (0 == mask)
                {
                    bits = input.ReadByte();
                    if (-1 == bits)
                    {
                        break;
                    }
                    mask = 0x80;
                }
                if (0 == (bits & mask))
                {
                    int b = input.ReadByte();
                    if (-1 == b)
                    {
                        break;
                    }
                    output[dst++] = (byte)b;
                    frame[frame_pos++ & 0xFFF] = (byte)b;
                }
                else
                {
                    int offset = input.ReadUInt16();
                    int count  = (offset & 0xF) + 3;
                    offset >>= 4;
                    while (count-- > 0 && dst < output.Length)
                    {
                        byte v = frame[offset++ & 0xFFF];
                        frame[frame_pos++ & 0xFFF] = v;
                        output[dst++] = v;
                    }
                }
            }
        }
Example #27
0
        public override ImageMetaData ReadMetaData(IBinaryStream file)
        {
            var header = file.ReadHeader(0x18);

            if (!header.AsciiEqual("CLS_TEXFILE"))
            {
                return(null);
            }

            file.Position = header.ToUInt32(0x14);
            int frame_offset = file.ReadInt32();

            file.Position = frame_offset + 4;
            if (file.ReadUInt16() != 1)
            {
                return(null);
            }

            file.Position = frame_offset + 0x1C;
            uint width  = file.ReadUInt32();
            uint height = file.ReadUInt32();
            int  x      = file.ReadInt32();
            int  y      = file.ReadInt32();

            file.Position = frame_offset + 0x30;
            if (file.ReadByte() != 1)
            {
                return(null);
            }
            int format = file.ReadByte();

            if (format != 4 && format != 5 && format != 2)
            {
                return(null);
            }
            return(new ClsMetaData {
                Width = width,
                Height = height,
                OffsetX = x,
                OffsetY = y,
                BPP = 8 * (format - 1),
                FrameOffset = frame_offset,
            });
        }
Example #28
0
        void UnpackRle(IBinaryStream input, byte[] output)
        {
            var  header      = input.ReadBytes(0x18);
            byte count_limit = header[5];
            int  dst         = 0;

            while (dst < output.Length)
            {
                int v = input.ReadByte();
                if (-1 == v)
                {
                    break;
                }
                int count = 1;
                while (count < count_limit)
                {
                    if (input.PeekByte() != v)
                    {
                        break;
                    }
                    ++count;
                    input.ReadByte();
                }
                if (count == count_limit)
                {
                    byte ctl = input.ReadUInt8();
                    if (ctl > 0x7F)
                    {
                        byte lo = input.ReadUInt8();
                        count += lo + ((ctl & 0x7F) << 8) + 128;
                    }
                    else
                    {
                        count += ctl;
                    }
                }
                for (int i = 0; i < count; ++i)
                {
                    output[dst++] = (byte)v;
                }
            }
        }
Example #29
0
        public override ImageMetaData ReadMetaData(IBinaryStream stream)
        {
            int A = stream.ReadByte();
            int P = stream.ReadByte();

            if ('A' != A || 'P' != P)
            {
                return(null);
            }
            var info = new ImageMetaData();

            info.Width  = stream.ReadUInt32();
            info.Height = stream.ReadUInt32();
            info.BPP    = stream.ReadInt16();
            if (info.Width > 0x8000 || info.Height > 0x8000 || !(32 == info.BPP || 24 == info.BPP))
            {
                return(null);
            }
            return(info);
        }
Example #30
0
        protected override IImageDecoder DecryptImage(IBinaryStream input, AImageScheme scheme)
        {
            int id = input.ReadByte();

            if (id == scheme.Value2)
            {
                return(new AImageReader(input, scheme));
            }
            input.Position = 0;
            return(new ImageFormatDecoder(input));
        }
Example #31
0
        /// <summary>
        /// Tests the stream.
        /// </summary>
        private static unsafe void TestStream(IBinaryStream stream, bool sameArr, Action flush)
        {
            Action seek = () => Assert.AreEqual(0, stream.Seek(0, SeekOrigin.Begin));

            Action<Action, Func<object>, object> check = (write, read, expectedResult) =>
            {
                seek();
                write();
                flush();
                seek();
                Assert.AreEqual(expectedResult, read());
            };

            // Arrays.
            Assert.AreEqual(sameArr, stream.IsSameArray(stream.GetArray()));
            Assert.IsFalse(stream.IsSameArray(new byte[1]));
            Assert.IsFalse(stream.IsSameArray(stream.GetArrayCopy()));

            // byte*
            byte* bytes = stackalloc byte[10];
            *bytes = 1;
            *(bytes + 1) = 2;

            stream.Write(bytes, 2);
            Assert.AreEqual(2, stream.Position);
            flush();

            seek();
            Assert.AreEqual(sameArr ? 256 : 2, stream.Remaining);
            byte* bytes2 = stackalloc byte[2];
            stream.Read(bytes2, 2);
            Assert.AreEqual(1, *bytes2);
            Assert.AreEqual(2, *(bytes2 + 1));

            // char*
            seek();
            char* chars = stackalloc char[10];
            *chars = 'a';
            *(chars + 1) = 'b';

            Assert.AreEqual(2, stream.WriteString(chars, 2, 2, Encoding.ASCII));
            flush();

            seek();
            stream.Read(bytes2, 2);
            Assert.AreEqual('a', *bytes2);
            Assert.AreEqual('b', *(bytes2 + 1));

            // Others.
            check(() => stream.Write(new byte[] {3, 4, 5}, 1, 2), () => stream.ReadByteArray(2), new byte[] {4, 5});

            check(() => stream.WriteBool(true), () => stream.ReadBool(), true);
            check(() => stream.WriteBoolArray(new[] {true, false}), () => stream.ReadBoolArray(2), 
                new[] {true, false});

            check(() => stream.WriteByte(4), () => stream.ReadByte(), 4);
            check(() => stream.WriteByteArray(new byte[] {4, 5, 6}), () => stream.ReadByteArray(3), 
                new byte[] {4, 5, 6});

            check(() => stream.WriteChar('x'), () => stream.ReadChar(), 'x');
            check(() => stream.WriteCharArray(new[] {'a', 'b'}), () => stream.ReadCharArray(2), new[] {'a', 'b'});

            check(() => stream.WriteDouble(4), () => stream.ReadDouble(), 4d);
            check(() => stream.WriteDoubleArray(new[] {4d}), () => stream.ReadDoubleArray(1), new[] {4d});

            check(() => stream.WriteFloat(4), () => stream.ReadFloat(), 4f);
            check(() => stream.WriteFloatArray(new[] {4f}), () => stream.ReadFloatArray(1), new[] {4f});

            check(() => stream.WriteInt(4), () => stream.ReadInt(), 4);
            check(() => stream.WriteInt(0, 4), () => stream.ReadInt(), 4);
            check(() => stream.WriteIntArray(new[] {4}), () => stream.ReadIntArray(1), new[] {4});

            check(() => stream.WriteLong(4), () => stream.ReadLong(), 4L);
            check(() => stream.WriteLongArray(new[] {4L}), () => stream.ReadLongArray(1), new[] {4L});

            check(() => stream.WriteShort(4), () => stream.ReadShort(), (short)4);
            check(() => stream.WriteShortArray(new short[] {4}), () => stream.ReadShortArray(1), new short[] {4});
        }
        /// <summary>
        /// Reads the schema according to this header data.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="position">The position.</param>
        /// <param name="hdr">The header.</param>
        /// <param name="fieldIdsFunc">The field ids function.</param>
        /// <returns>
        /// Schema.
        /// </returns>
        public static BinaryObjectSchemaField[] ReadSchema(IBinaryStream stream, int position, BinaryObjectHeader hdr, 
            Func<int[]> fieldIdsFunc)
        {
            Debug.Assert(stream != null);
            Debug.Assert(fieldIdsFunc != null);

            var schemaSize = hdr.SchemaFieldCount;

            if (schemaSize == 0)
                return null;

            stream.Seek(position + hdr.SchemaOffset, SeekOrigin.Begin);

            var res = new BinaryObjectSchemaField[schemaSize];

            var offsetSize = hdr.SchemaFieldOffsetSize;

            if (hdr.IsCompactFooter)
            {
                var fieldIds = fieldIdsFunc();

                Debug.Assert(fieldIds.Length == schemaSize);

                if (offsetSize == 1)
                {
                    for (var i = 0; i < schemaSize; i++)
                        res[i] = new BinaryObjectSchemaField(fieldIds[i], stream.ReadByte());

                }
                else if (offsetSize == 2)
                {
                    for (var i = 0; i < schemaSize; i++)
                        res[i] = new BinaryObjectSchemaField(fieldIds[i], stream.ReadShort());
                }
                else
                {
                    for (var i = 0; i < schemaSize; i++)
                        res[i] = new BinaryObjectSchemaField(fieldIds[i], stream.ReadInt());
                }
            }
            else
            {
                if (offsetSize == 1)
                {
                    for (var i = 0; i < schemaSize; i++)
                        res[i] = new BinaryObjectSchemaField(stream.ReadInt(), stream.ReadByte());
                }
                else if (offsetSize == 2)
                {
                    for (var i = 0; i < schemaSize; i++)
                        res[i] = new BinaryObjectSchemaField(stream.ReadInt(), stream.ReadShort());
                }
                else
                {
                    for (var i = 0; i < schemaSize; i++)
                        res[i] = new BinaryObjectSchemaField(stream.ReadInt(), stream.ReadInt());
                }
            }

            return res;
        }