Esempio n. 1
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_CompressedChunk();

            result.Chunk_header = Unmarshal <LZNT1_ChunkHeader>(buffer, ref offset);
            int  value      = result.Chunk_header.Value;
            bool compressed = (value & 0x8000) != 0;

            if (!compressed)
            {
                throw new XcaException("Unreachable code!");
            }
            int signature = (value & 0x7000) >> 12;

            if (signature != 3)
            {
                throw new XcaException("[Data error]: Wrong signature!");
            }
            int size      = (value & 0x0FFF) + 1;
            var subBuffer = buffer.SubCopy(offset, size);
            int subOffset = 0;

            result.Flag_group = Unmarshal <LZNT1_FlagGroup>(subBuffer, ref subOffset);
            if (subOffset != size)
            {
                throw new XcaException("[Data error]: Data length is inconsistent!");
            }
            offset += size;

            return(result);
        }
Esempio n. 2
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_FlagData();

            result.Flag_byte = (byte)buffer.ReadBytes(offset, 1);
            offset++;

            var data = new List <LZNT1_Data>();

            for (int i = 0; i < 8; i++)
            {
                if (offset < buffer.Count)
                {
                    bool compressed = (result.Flag_byte & (1 << i)) != 0;

                    if (compressed)
                    {
                        data.Add(Unmarshal <LZNT1_CompressedWord>(buffer, ref offset));
                    }
                    else
                    {
                        data.Add(Unmarshal <LZNT1_Literal>(buffer, ref offset));
                    }
                }
            }

            result.Data = data.ToArray();

            return(result);
        }
Esempio n. 3
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     foreach (var chunk in Chunk)
     {
         chunk.Marshal(buffer, ref offset);
     }
 }
Esempio n. 4
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_UncompressedChunk();

            result.Chunk_header = Unmarshal <LZNT1_ChunkHeader>(buffer, ref offset);
            int  value      = result.Chunk_header.Value;
            bool compressed = (value & 0x8000) != 0;

            if (compressed)
            {
                throw new XcaException("Unreachable code!");
            }
            int signature = (value & 0x7000) >> 12;

            if (signature != 3)
            {
                throw new XcaException("[Data error]: Wrong signature!");
            }
            int size = (value & 0x0FFF) + 1;

            result.Uncompressed_data = new byte[size];
            for (int i = 0; i < size; i++)
            {
                result.Uncompressed_data[i] = (byte)buffer.ReadBytes(offset + i, 1);
            }
            offset += size;
            return(result);
        }
        public byte[] Compress(byte[] arg)
        {
            var result = new LZNT1_Buffer();

            var chunks = new List <LZNT1_Chunk>();
            int index  = 0;

            while (index < arg.Length)
            {
                int length = Math.Min(arg.Length - index, 4096);

                var chunk = CompressChunk(arg.Skip(index).Take(length).ToArray());

                chunks.Add(chunk);

                index += length;
            }


            result.Chunk = chunks.ToArray();

            var buffer = new LittleEndianByteBuffer();
            int offset = 0;

            result.Marshal(buffer, ref offset);

            return(buffer.GetBytes());
        }
Esempio n. 6
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     foreach (var data in Flag_data)
     {
         data.Marshal(buffer, ref offset);
     }
 }
Esempio n. 7
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_ChunkHeader();

            result.Value = buffer.ReadBytes(offset, 2);
            offset      += 2;
            return(result);
        }
Esempio n. 8
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     buffer.WriteBytes(offset, Flag_byte, 1);
     offset++;
     foreach (var data in Data)
     {
         data.Marshal(buffer, ref offset);
     }
 }
Esempio n. 9
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     Chunk_header.Marshal(buffer, ref offset);
     foreach (byte b in Uncompressed_data)
     {
         buffer.WriteBytes(offset, b, 1);
         offset++;
     }
 }
Esempio n. 10
0
 public override void Decode(LittleEndianByteBuffer buffer, ref int offset)
 {
     for (int i = 0; i < Length; i++)
     {
         int matchByte = buffer.ReadBytes(offset - Distance + i, 1);
         buffer.WriteBytes(offset + i, matchByte, 1);
     }
     offset += Length;
 }
Esempio n. 11
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_Literal();

            result.Literal = (byte)buffer.ReadBytes(offset, 1);
            offset++;

            return(result);
        }
Esempio n. 12
0
        public LittleEndianByteBuffer SubCopy(int offset, int length)
        {
            if (offset + length > buffer.Count)
            {
                throw new ArgumentOutOfRangeException("Either offset or length is out of range!");
            }
            var result = new LittleEndianByteBuffer(buffer.GetRange(offset, length).ToArray());

            return(result);
        }
Esempio n. 13
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_EndOfBuffer();

            result.Chunk_header = Unmarshal <LZNT1_ChunkHeader>(buffer, ref offset);
            int value = result.Chunk_header.Value;

            if (value != 0)
            {
                throw new XcaException("Unreachable code!");
            }
            return(result);
        }
Esempio n. 14
0
        /// <summary>
        /// Decode LZ77 symbols to data.
        /// </summary>
        /// <param name="symbols">LZ77 symbols to be decoded.</param>
        /// <returns>The data after decoded.</returns>
        public byte[] Decode(List <LZ77Symbol> symbols)
        {
            var buffer = new LittleEndianByteBuffer();
            int offset = 0;

            Verify(symbols);

            foreach (var symbol in symbols)
            {
                symbol.Decode(buffer, ref offset);
            }

            return(buffer.GetBytes());
        }
Esempio n. 15
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_FlagGroup();
            var data   = new List <LZNT1_FlagData>();

            while (offset < buffer.Count)
            {
                var flag = Unmarshal <LZNT1_FlagData>(buffer, ref offset);
                data.Add(flag);
            }

            result.Flag_data = data.ToArray();

            return(result);
        }
Esempio n. 16
0
        /// <summary>
        /// Decompress data.
        /// </summary>
        /// <param name="data">Data to be decompressed.</param>
        /// <returns>Decompressed Data.</returns>
        public byte[] Decompress(byte[] data)
        {
            var buffer      = new LittleEndianByteBuffer(data);
            int offset      = 0;
            var lznt1Buffer = LZNT1_Object.Unmarshal <LZNT1_Buffer>(buffer, ref offset);
            var result      = new List <byte>();

            foreach (var chunk in lznt1Buffer.Chunk)
            {
                var symbols = chunk.ParseToLZ77Symbols();

                result.AddRange(lz77Code.Decode(symbols));
            }

            return(result.ToArray());
        }
Esempio n. 17
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            var result = new LZNT1_Buffer();

            var chunks = new List <LZNT1_Chunk>();

            while (offset < buffer.Count)
            {
                var chunk = Unmarshal <LZNT1_Chunk>(buffer, ref offset);
                chunks.Add(chunk);
            }

            result.Chunk = chunks.ToArray();

            return(result);
        }
Esempio n. 18
0
        public override object Unmarshal(LittleEndianByteBuffer buffer, ref int offset)
        {
            int offset1 = offset;
            var header  = Unmarshal <LZNT1_ChunkHeader>(buffer, ref offset1);
            int value   = header.Value;

            if (value == 0)
            {
                var result = Unmarshal <LZNT1_EndOfBuffer>(buffer, ref offset);
                return(result);
            }
            bool compressed = (value & 0x8000) != 0;

            if (compressed)
            {
                var result = Unmarshal <LZNT1_CompressedChunk>(buffer, ref offset);
                return(result);
            }
            else
            {
                var result = Unmarshal <LZNT1_UncompressedChunk>(buffer, ref offset);
                return(result);
            }
        }
Esempio n. 19
0
 public override void Decode(LittleEndianByteBuffer buffer, ref int offset)
 {
 }
Esempio n. 20
0
 public override void Decode(LittleEndianByteBuffer buffer, ref int offset)
 {
     buffer.WriteBytes(offset, Literal, 1);
     offset++;
 }
Esempio n. 21
0
 public abstract void Marshal(LittleEndianByteBuffer buffer, ref int offset);
Esempio n. 22
0
 public abstract void Decode(LittleEndianByteBuffer buffer, ref int offset);
Esempio n. 23
0
 public abstract object Unmarshal(LittleEndianByteBuffer buffer, ref int offset);
        /// <summary>
        /// Encode LZ77 symbols.
        /// </summary>
        /// <param name="symbols">LZ77 symbols to be encoded.</param>
        /// <returns>Byte array containing encoded result.</returns>
        public byte[] Encode(List <LZ77Symbol> symbols)
        {
            long Flags              = 0;
            int  FlagCount          = 0;
            int  FlagOutputPosition = 0;
            int  OutputPosition     = 4;
            int  LastLengthHalfByte = 0;

            var buffer = new LittleEndianByteBuffer();

            foreach (var symbol in symbols)
            {
                if (symbol is LZ77Literal)
                {
                    var literal = symbol as LZ77Literal;

                    buffer.WriteBytes(OutputPosition, literal.Literal, 1);

                    OutputPosition += 1;

                    Flags <<= 1;

                    FlagCount += 1;
                }
                else if (symbol is LZ77Match)
                {
                    var match       = symbol as LZ77Match;
                    int MatchLength = match.Length;
                    int MatchOffset = match.Distance;

                    MatchLength -= 3;

                    MatchOffset -= 1;

                    MatchOffset <<= 3;

                    if (MatchLength < 7)
                    {
                        MatchOffset += (int)MatchLength;

                        buffer.WriteBytes(OutputPosition, MatchOffset, 2);

                        OutputPosition += 2;
                    }
                    else
                    {
                        MatchOffset |= 7;

                        buffer.WriteBytes(OutputPosition, MatchOffset, 2);

                        OutputPosition += 2;

                        MatchLength -= 7;

                        bool EncodeExtraLen;

                        if (LastLengthHalfByte == 0)
                        {
                            LastLengthHalfByte = OutputPosition;

                            if (MatchLength < 15)
                            {
                                buffer.WriteBytes(OutputPosition, MatchLength, 1);

                                OutputPosition += 1;

                                EncodeExtraLen = false;
                            }
                            else
                            {
                                buffer.WriteBytes(OutputPosition, 15, 1);

                                OutputPosition++;

                                EncodeExtraLen = true;
                            }
                        }
                        else
                        {
                            if (MatchLength < 15)
                            {
                                byte LastLength = buffer[LastLengthHalfByte];

                                LastLength |= (byte)(MatchLength << 4);

                                buffer.WriteBytes(LastLengthHalfByte, LastLength, 1);

                                LastLengthHalfByte = 0;

                                EncodeExtraLen = false;
                            }
                            else
                            {
                                byte LastLength = buffer[LastLengthHalfByte];

                                LastLength |= 15 << 4;

                                buffer.WriteBytes(LastLengthHalfByte, LastLength, 1);

                                LastLengthHalfByte = 0;

                                EncodeExtraLen = true;
                            }
                        }

                        if (EncodeExtraLen)
                        {
                            MatchLength -= 15;

                            if (MatchLength < 255)
                            {
                                buffer.WriteBytes(OutputPosition, MatchLength, 1);

                                OutputPosition += 1;
                            }
                            else
                            {
                                buffer.WriteBytes(OutputPosition, 255, 1);

                                OutputPosition += 1;

                                MatchLength += 7 + 15;

                                if (MatchLength < (1 << 16))
                                {
                                    buffer.WriteBytes(OutputPosition, MatchLength, 2);

                                    OutputPosition += 2;
                                }
                                else
                                {
                                    buffer.WriteBytes(OutputPosition, 0, 2);

                                    OutputPosition += 2;

                                    buffer.WriteBytes(OutputPosition, MatchLength, 4);

                                    OutputPosition += 4;
                                }
                            }
                        }
                    }

                    Flags = (Flags << 1) | 1;

                    FlagCount += 1;
                }
                else if (symbol is LZ77EOF)
                {
                    // Do nothing to EOF.
                }
                else
                {
                    throw new XcaException("Unreachable code!");
                }

                if (FlagCount == 32)
                {
                    buffer.WriteBytes(FlagOutputPosition, (int)Flags, 4);

                    FlagCount = 0;

                    FlagOutputPosition = OutputPosition;

                    OutputPosition += 4;
                }
            }

            Flags <<= (32 - FlagCount);
            Flags  |= (((long)1 << (32 - FlagCount)) - 1);

            buffer.WriteBytes(FlagOutputPosition, (int)Flags, 4);

            return(buffer.GetBytes());
        }
Esempio n. 25
0
        public static T Unmarshal <T>(LittleEndianByteBuffer buffer, ref int offset) where T : LZNT1_Object, new()
        {
            var result = new T().Unmarshal(buffer, ref offset);

            return(result as T);
        }
Esempio n. 26
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     Chunk_header.Marshal(buffer, ref offset);
     Flag_group.Marshal(buffer, ref offset);
 }
Esempio n. 27
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     throw new XcaException("Unreachable code!");
 }
Esempio n. 28
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     // Do not marshal since its optional.
 }
Esempio n. 29
0
 public override void Marshal(LittleEndianByteBuffer buffer, ref int offset)
 {
     buffer.WriteBytes(offset, Value, 2);
     offset += 2;
 }