Пример #1
0
        private void ParseStream(bool resetPos)
        {
            if (resetPos)
            {
                _stream.Seek(0, SeekOrigin.Begin);
            }

            byte[] buf = new byte[0x180];
            if (_stream.Read(buf, 0, 0x180) != 0x180)
            {
                throw new FileFormatException("Stream too small to contain a Comp file.");
            }

            if (BitConverter.ToUInt32(buf, 0) != Magic)
            {
                throw new FileFormatException("Stream is not a Comp file.");
            }

            if (BitConverter.ToUInt32(buf, 4) != LzssSignature)
            {
                throw new FileFormatException("Unknown Comp compression type. Only LZSS is supported.");
            }

            // the lengths are stored in big endian; reverse them
            int decompLen = BitConverter.ToInt32(buf.Skip(0xC).Take(4).Reverse().ToArray(), 0);
            int compLen   = BitConverter.ToInt32(buf.Skip(0x10).Take(4).Reverse().ToArray(), 0);

            if (compLen <= 0 || decompLen <= 0)
            {
                throw new FileFormatException("Payload cannot have a zero or negative size.");
            }

            byte[] compPayload = new byte[compLen];
            _payload = new byte[decompLen];
            if (_stream.Read(compPayload, 0, compLen) != compLen)
            {
                throw new FileFormatException("Stream to small to contain indicated payload.");
            }

            int length = Lzss.Decompress(_payload, compPayload);

            if (length != decompLen)
            {
                throw new FileFormatException("Decompressed stream not expected size.");
            }
        }
Пример #2
0
        private void ParseStream(bool resetPos)
        {
            if (resetPos)
            {
                _stream.Seek(0, SeekOrigin.Begin);
            }

            byte[] buf = new byte[0x40];
            if (_stream.Read(buf, 0, 0x40) != 0x40)
            {
                throw new FileFormatException("Stream too small to contain an iBootImage file.");
            }

            if (BitConverter.ToUInt64(buf, 0) != Magic)
            {
                throw new FileFormatException("Stream is not an iBootImage file.");
            }

            if (BitConverter.ToUInt32(buf, 0xC) != LzssSignature)
            {
                throw new FileFormatException("Unknown iBootImage compression type. Only LZSS is supported.");
            }

            uint colorType = BitConverter.ToUInt32(buf, 0x10);

            if (colorType == ColorGray)
            {
                _colorType = IBootImageColorType.Gray;
            }
            else if (colorType == ColorArgb)
            {
                _colorType = IBootImageColorType.Argb;
            }
            else
            {
                throw new FileFormatException("Unknown iBootImage color type. Only ARGB and grey are supported");
            }

            _width  = BitConverter.ToInt16(buf, 0x14);
            _height = BitConverter.ToInt16(buf, 0x16);
            if (_width < 0 || _height < 0)
            {
                throw new FileFormatException("iBootImage cannot have a negative dimension.");
            }

            int compLen   = (int)_stream.Length - 0x40;
            int decompLen = 0;

            if (_colorType == IBootImageColorType.Gray)
            {
                decompLen = 2 * _width * _height;
            }
            else //if (_colorType == IBootImageColorType.Argb)
            {
                decompLen = 4 * _width * _height;
            }

            byte[] compPayload = new byte[compLen];
            _payload = new byte[decompLen];
            if (_stream.Read(compPayload, 0, compLen) != compLen)
            {
                throw new FileFormatException("Stream to small to contain indicated payload.");
            }

            int length = Lzss.Decompress(_payload, compPayload);

            System.Diagnostics.Debug.Assert(length == decompLen);
        }