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); }
public static LZNT1_Chunk Compress(byte[] arg, List <LZNT1_Data> data) { int compressedLength = CalculateCompressedLength(data); if (compressedLength < arg.Length) { var compressedChunk = new LZNT1_CompressedChunk(); compressedChunk.Chunk_header = new LZNT1_ChunkHeader(); compressedChunk.Chunk_header.Value = 0xB000; compressedChunk.Chunk_header.Value |= compressedLength - 3; compressedChunk.Flag_group = new LZNT1_FlagGroup(); var flagData = new List <LZNT1_FlagData>(); int i = 0; while (i < data.Count) { int groupLength = Math.Min(8, data.Count - i); var group = data.Skip(i).Take(groupLength); var flagDataItem = new LZNT1_FlagData(group.ToArray()); flagData.Add(flagDataItem); i += groupLength; } compressedChunk.Flag_group.Flag_data = flagData.ToArray(); return(compressedChunk); } else { var uncompressedChunk = new LZNT1_UncompressedChunk(); uncompressedChunk.Chunk_header = new LZNT1_ChunkHeader(); uncompressedChunk.Chunk_header.Value = 0x3000; uncompressedChunk.Chunk_header.Value |= arg.Length - 1; uncompressedChunk.Uncompressed_data = arg.ToArray(); return(uncompressedChunk); } }