public static Locations[] Read_Locations(Stream s) { helper help = new helper(); Locations l = new Locations(); Locations[] result = new Locations[Header.LocationCount]; uint[] index = new uint[Header.LocationCount]; ulong[] offset = new ulong[Header.LocationCount]; uint pos = 0; byte[] block = ReturnBlock(s, l.SizeOf); for (uint i = 0; i < Header.LocationCount; i++) { offset[i] = help.ReadBits(block, pos, Header.LocationBitCount); pos += Header.LocationBitCount; index[i] = i; } Array.Sort(offset, index); for (uint i = 0; i < Header.LocationCount; i++) { result[i] = new Locations(); result[i].index = index[i]; result[i].offset = offset[i]; } return(result); }
public byte[] DecompressChunk(Stream fs, int offset, int rev) { //CPK_MAX_DECOMP_BUFFER_SIZE = 0x10000 helper help = new helper(); fs.Seek(offset, 0); uint DecompressedSize, Flag, CompressedSize; if (rev == 0) { DecompressedSize = help.ReadU16(fs); Flag = help.ReadU16(fs); CompressedSize = help.ReadU16(fs); } else { DecompressedSize = help.ReverseUInt16(help.ReadU16(fs)); Flag = help.ReverseUInt16(help.ReadU16(fs)); CompressedSize = help.ReverseUInt16(help.ReadU16(fs)); } byte[] buff = new byte[CompressedSize]; fs.Read(buff, 0, (int)CompressedSize); byte[] tmp = { }; try { tmp = DecompressZlib(buff); } catch { tmp = buff; } return(tmp); }
public static FileName[] Read_FileName(Stream s) { helper help = new helper(); FileName[] result = new FileName[Header.FileCount]; for (int i = 0; i < Header.FileCount; i++) { FileName fn = new FileName(); if (Header.MagicNumber.ToString("X8").Equals("A1B2C3D4")) { fn.offset = help.ReadU32(s); } if (Header.MagicNumber.ToString("X8").Equals("D4C3B2A1")) { fn.offset = help.ReverseUInt32(help.ReadU32(s)); } result[i] = fn; } long pos = s.Position; for (int i = 0; i < Header.FileCount; i++) { s.Seek(pos + result[i].offset, 0); result[i].filename = help.ReadString(s); result[i].fileHash = help.GetFileHash(result[i].filename); } return(result); }
public DecompressedSectorToCompressedSector() { helper help = new helper(); SizeOf = help.GetHighestBit(Header.CompSectorCount) * (((uint)Header.DecompressedFileSize + (uint)Header.CompSectorSize - 1) / (uint)Header.CompSectorSize); SizeOf += 7; SizeOf /= 8; }
public static CompressedSectorToDecompressedOffset[] Read_CompressedSectorToDecompressedOffset(Stream s) { helper help = new helper(); CompressedSectorToDecompressedOffset c = new CompressedSectorToDecompressedOffset(); uint pos = 0; byte[] block = ReturnBlock(s, c.SizeOf); CompressedSectorToDecompressedOffset[] result = new CompressedSectorToDecompressedOffset[Header.CompSectorCount]; for (uint i = 0; i < Header.CompSectorCount; i++) { result[i] = new CompressedSectorToDecompressedOffset(); result[i].SectorIndex = i; result[i].DecompressedOffset = help.ReadBits(block, pos, Header.LocationBitCount); pos += Header.LocationBitCount; } return(result); }
public static Dictionary <uint, CompressedSectorChunk> ReadSectors(Stream s) { helper help = new helper(); CompressedSectorChunk csc = new CompressedSectorChunk(); uint ChunkNumber = 0; ulong doffset = 0; s.Seek(Header.FirstSectorPosition, 0); Dictionary <uint, CompressedSectorChunk> result = new Dictionary <uint, CompressedSectorChunk>(); if (Header.MagicNumber.ToString("X8").Equals("A1B2C3D4")) { help.ReadU16(s); help.ReadU16(s); s.Seek(help.ReadU16(s), SeekOrigin.Current); } if (Header.MagicNumber.ToString("X8").Equals("D4C3B2A1")) { help.ReverseUInt16(help.ReadU16(s)); help.ReverseUInt16(help.ReadU16(s)); s.Seek(help.ReverseUInt16(help.ReadU16(s)), SeekOrigin.Current); } for (uint sector = 0; sector <= Header.CompSectorCount; sector++) { uint SectorStartPosition = Header.FirstSectorPosition + sector * Header.CompSectorSize; uint NextSectorPosition = SectorStartPosition + Header.CompSectorSize; s.Seek(SectorStartPosition, 0); while (s.Position + 0xf < NextSectorPosition) { csc = new CompressedSectorChunk(); csc.position = (uint)s.Position; ushort Size = help.ReadU16(s); ushort flag = help.ReadU16(s); ushort CompChunkSize = help.ReadU16(s); csc.nr = ChunkNumber; csc.CompChunkSize = CompChunkSize; csc.DecompChunkSize = Size; csc.flag = flag; csc.CompSector = sector; doffset += Size; csc.StartDecompOffset = doffset - Size; result.Add(ChunkNumber, csc); ChunkNumber++; s.Seek(CompChunkSize, SeekOrigin.Current); } } return(result); }
public static DecompressedSectorToCompressedSector[] Read_DecompressedSectorToCompressedSector(Stream s) { helper help = new helper(); uint temp = help.GetHighestBit(Header.CompSectorCount); DecompressedSectorToCompressedSector d = new DecompressedSectorToCompressedSector(); uint sector = (((uint)Header.DecompressedFileSize + (uint)Header.CompSectorSize - 1) / (uint)Header.CompSectorSize); uint pos = 0; byte[] block = ReturnBlock(s, d.SizeOf); DecompressedSectorToCompressedSector[] result = new DecompressedSectorToCompressedSector[sector]; for (uint i = 0; i < sector; i++) { result[i] = new DecompressedSectorToCompressedSector(); result[i].DecompressedSector = i; result[i].CompressedSector = help.ReadBits(block, pos, temp); pos += temp; } return(result); }
public HeaderStruct(Stream s, int rev) { s.Seek(0, SeekOrigin.End); FileSize = (uint)s.Position; helper help = new helper(); s.Seek(0, 0); MagicNumber = help.ReadU32(s); PackageVersion = help.RUInt32(s, rev); DecompressedFileSize = help.RUInt64(s, rev); Flags = help.RUInt32(s, rev); FileCount = help.RUInt32(s, rev); LocationCount = help.RUInt32(s, rev); HeaderSector = help.RUInt32(s, rev); FileSizeBitCount = help.RUInt32(s, rev); FileLocationCountBitCount = help.RUInt32(s, rev); FileLocationIndexBitCount = help.RUInt32(s, rev); LocationBitCount = help.RUInt32(s, rev); CompSectorToDecomOffsetBitCount = help.RUInt32(s, rev); DecompSectorToCompSectorBitCount = help.RUInt32(s, rev); CRC = help.RUInt32(s, rev); if (PackageVersion == 6) { Unknown = help.ReadU32(s); ReadSectorSize = 0x10000; CompSectorSize = 0x4000; HeaderSize = 64; } if (PackageVersion == 7) { ReadSectorSize = (uint)help.RInt32(s, rev); CompSectorSize = (uint)help.RInt32(s, rev); HeaderSize = 72; } CompSectorCount = ((uint)CompSectorSize + FileSize - 1 - (uint)ReadSectorSize * HeaderSector) / (uint)CompSectorSize; FirstSectorPosition = (uint)(ReadSectorSize * HeaderSector) & 0xFFFF0000; if ((FirstSectorPosition % ReadSectorSize) != 0) { FirstSectorPosition += ReadSectorSize; } }
static void Main(string[] args) { string path = ""; if (args.Length == 0) { path = "D:\\Common.cpk"; } else { path = args[0]; } helper help = new helper(); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.Seek(0, 0); rev = cpk.IsByteReverse(fs); HeaderStruct = cpk.HeaderStruct.ReadHeader(fs, rev); SortedFileInfo = cpk.SortedFileInfo.Read_SortedFileInfo(fs); Locations = cpk.Locations.Read_Locations(fs); CompressedSectorToDecompressedOffset = cpk.CompressedSectorToDecompressedOffset.Read_CompressedSectorToDecompressedOffset(fs); DecompressedSectorToCompressedSector = cpk.DecompressedSectorToCompressedSector.Read_DecompressedSectorToCompressedSector(fs); FileName = cpk.FileName.Read_FileName(fs); DictCompressedSectorChunk = cpk.CompressedSectorChunk.ReadSectors(fs); CompressedSectorChunk = cpk.CompressedSectorChunk.Read_CompressedSectorChunk(DictCompressedSectorChunk); WriteHeader(); for (int idx = 0; idx < Locations.Length; idx++) { int i = (int)Locations[idx].index; int a = GetLocationIndex(i); WriteFileInfo(a); WriteLocations((idx)); WriteFileName(a); } fs.Close(); Console.ReadKey(); }
public static SortedFileInfo[] Read_SortedFileInfo(Stream s) { SortedFileInfo[] result = new SortedFileInfo[Header.FileCount]; helper help = new helper(); SortedFileInfo sfi = new SortedFileInfo(); s.Position = HeaderSize; byte[] Htable = ReturnBlock(s, sfi.SizeOf); uint position = 0; for (uint i = 0; i < Header.FileCount; i++) { result[i] = new SortedFileInfo(); result[i].dwHash = help.ReadBits(Htable, position, 64); position += 64; result[i].nSize = (uint)help.ReadBits(Htable, position, Header.FileSizeBitCount); position += Header.FileSizeBitCount; result[i].nLocationCount = (uint)help.ReadBits(Htable, position, Header.FileLocationCountBitCount); position += Header.FileLocationCountBitCount; result[i].nLocationIndex = (uint)help.ReadBits(Htable, position, Header.FileLocationIndexBitCount); position += Header.FileLocationIndexBitCount; } return(result); }
public static byte[] GetChunck(FileStream s, int i, int rev) { helper help = new helper(); return(help.DecompressChunk(s, (int)CompressedSectorChunk[i].position, rev)); }