public static DataSet DecompressDataSet(byte[] bytDs, DataSet ds) { using (MemoryTributary inMs = new MemoryTributary(bytDs)) { inMs.Seek(0, 0); using (DeflateStream zipStream = new DeflateStream(inMs, CompressionMode.Decompress, true)) { System.IO.StreamReader sr = new StreamReader(zipStream); ds.ReadXml(sr); } } //string xml = (string)atLogic.BEManager.DecompressStream(bytDs); //using (System.IO.StringReader sw = new System.IO.StringReader(xml)) //{ // ds.ReadXml(sw); //} ds.AcceptChanges(); ds.RemotingFormat = SerializationFormat.Binary; AppManager.FixTZDSIssue(ds); return(ds); }
void ICommunicationService.UploadContent(UploadStreamMessage content) { Stream source = content.DataStream; MemoryTributary ms = new MemoryTributary(); const int bufferLen = 65536; byte[] buffer = new byte[bufferLen]; int count = 0; while ((count = source.Read(buffer, 0, bufferLen)) > 0) { ms.Write(buffer, 0, count); } ms.Seek(0, SeekOrigin.Begin); try { if (owner != null) owner.RouteData(content.Context, content.ContentType, ms); } catch { } //MessageBox.Show(content.ContentType + " " + content.Context + " " + ms.Length); ms.Close(); }
private void Read(Stream sourceStream) { var stream = new MemoryTributary(); var reader = new BinaryReader(stream); sourceStream.CopyTo(stream); stream.Seek(0, SeekOrigin.Begin); _header = Header.ReadStatic(reader); UseSingleByteIndices = _header.UseSingleByteIndices; Name = _header.SceneName; Textures = ReadArray <HashedName>(reader, _header.TextureOffset, _header.TextureCount); Materials = ReadArray <Material>(reader, _header.MaterialOffset, _header.MaterialCount); Meshes = ReadArray <Mesh>(reader, _header.MeshOffset, _header.MeshCount); Submeshes = ReadArray <Submesh>(reader, _header.SubmeshOffset, _header.SubmeshCount); BoneIndices = ReadArray <BoneIndex>(reader, _header.BoneIndexOffset, UseSingleByteIndices ? _header.BoneIndexBlockSize : _header.BoneIndexBlockSize / 2); BoneTransforms = ReadArray <BoneTransform>(reader, _header.BoneMatOffset, _header.BoneMatCount); BoneNames = ReadArray <HashedName>(reader, _header.BoneNameOffset, _header.BoneNameCount); Indices = ReadArray <Index>(reader, _header.IndicesOffset, _header.IndicesCount); Shaders = ReadArray <HashedName>(reader, _header.ShaderOffset, _header.ShaderCount); reader.BaseStream.Seek(_header.VertexBufferOffset, SeekOrigin.Begin); VertexBuffers = new byte[Meshes.Length][]; int i = 0; foreach (var mesh in Meshes) { VertexBuffers[i++] = reader.ReadBytes(mesh.Size, _header.VertexBufferOffset + mesh.Offset); } InverseBindPoses = ReadArray <Matrix64>(reader, _header.InverseBindPosesOffset, _header.InverseBindPosesCount); // This seems to be a list of transforms for attachment (?) bones that have no parent. The first 4 ints are [ID] [Bone No] [Bone No] [?] _submeshToBones = ReadArray <Unknown1Struct>(reader, _header.Offset01, _header.Count01); // _unknown1 = BoneTransforms.Where(bt => bt.SupplementaryBoneTableIndex >= 0).Take(6).Select((b, i) => // { // var matrix = new Unknown1Struct(); // matrix.Id = b.SupplementaryBoneTableIndex; // matrix.BoneNo = b.BoneNo; // matrix.BoneNo2 = b.BoneNo2; // matrix.Offset = i * 8; // return matrix; // }).ToArray(); // The value range correlates to the number of submeshes _submeshBonesMeta = ReadArray <Unknown12Struct>(reader, _header.Offset12, _header.Count12 / 8); // _unknown12[_unknown1[Submeshes[9].Unknown1Offset].Offset / 8].Unknown3 = 10; _unknown4 = ReadBuffer(reader, _header.Offset04, _header.Count04, 0x10); _unknown13 = ReadArray <Unknown13Struct>(reader, _header.Offset13, _header.Count13); _unknown14 = ReadBuffer(reader, _header.Offset14, _header.Count14, 0x40); _unknown15 = ReadBuffer(reader, _header.Offset15, _header.Count15, 0x40); }
private void ReadExports(MemoryTributary fs) { DebugOutput.PrintLn("Reading Exports..."); fs.Seek(ExportOffset, SeekOrigin.Begin); Exports = new List<ME2ExportEntry>(); for (int i = 0; i < ExportCount; i++) { long start = fs.Position; ME2ExportEntry exp = new ME2ExportEntry(); exp.pccRef = this; exp.infoOffset = (int)start; fs.Seek(40, SeekOrigin.Current); int count = fs.ReadValueS32(); fs.Seek(4 + count * 12, SeekOrigin.Current); count = fs.ReadValueS32(); fs.Seek(4 + count * 4, SeekOrigin.Current); fs.Seek(16, SeekOrigin.Current); long end = fs.Position; fs.Seek(start, SeekOrigin.Begin); exp.info = fs.ReadBytes((int)(end - start)); Exports.Add(exp); fs.Seek(end, SeekOrigin.Begin); if (LastExport == null || exp.DataOffset > LastExport.DataOffset) LastExport = exp; } }
private void ReadImports(MemoryTributary fs) { DebugOutput.PrintLn("Reading Imports..."); Imports = new List<ME2ImportEntry>(); fs.Seek(ImportOffset, SeekOrigin.Begin); for (int i = 0; i < ImportCount; i++) { ME2ImportEntry import = new ME2ImportEntry(); import.Package = Names[fs.ReadValueS32()]; fs.Seek(12, SeekOrigin.Current); import.link = fs.ReadValueS32(); import.Name = Names[fs.ReadValueS32()]; fs.Seek(-24, SeekOrigin.Current); import.raw = fs.ReadBytes(28); Imports.Add(import); } }
private void ReadNames(MemoryTributary fs) { DebugOutput.PrintLn("Reading Names..."); fs.Seek(NameOffset, SeekOrigin.Begin); Names = new List<string>(); for (int i = 0; i < NameCount; i++) { int len = fs.ReadValueS32(); string s = fs.ReadString((uint)(len - 1)); fs.Seek(5, SeekOrigin.Current); Names.Add(s); } }
private void LoadHelper(MemoryTributary tempStream) { tempStream.Seek(12, SeekOrigin.Begin); int tempNameSize = tempStream.ReadValueS32(); tempStream.Seek(64 + tempNameSize, SeekOrigin.Begin); int tempGenerator = tempStream.ReadValueS32(); tempStream.Seek(36 + tempGenerator * 12, SeekOrigin.Current); int tempPos = (int)tempStream.Position; NumChunks = tempStream.ReadValueS32(); tempStream.Seek(0, SeekOrigin.Begin); header = tempStream.ReadBytes(tempPos); tempStream.Seek(0, SeekOrigin.Begin); if (magic != ZBlock.magic && magic.Swap() != ZBlock.magic) { DebugOutput.PrintLn("Magic number incorrect: " + magic); throw new FormatException("This is not a pcc file. The magic number is incorrect."); } if (bCompressed) { DebugOutput.PrintLn("File is compressed"); { listsStream = lzo.DecompressPCC(tempStream, this); //Correct the header bCompressed = false; listsStream.Seek(0, SeekOrigin.Begin); listsStream.WriteBytes(header); //Set numblocks to zero listsStream.WriteValueS32(0); //Write the magic number listsStream.WriteValueS32(1026281201); //Write 8 bytes of 0 listsStream.WriteValueS32(0); listsStream.WriteValueS32(0); } } else { DebugOutput.PrintLn("File already decompressed. Reading decompressed data."); listsStream = tempStream; } ReadNames(listsStream); ReadImports(listsStream); ReadExports(listsStream); LoadExports(); }
public MemoryTributary DecompressPCC(Stream raw, PCCObject pcc) { raw.Seek(pcc.header.Length, SeekOrigin.Begin); int pos = 4; pcc.NumChunks = raw.ReadValueS32(); List<Chunk> Chunks = new List<Chunk>(); //DebugOutput.PrintLn("Reading chunk headers..."); for (int i = 0; i < pcc.NumChunks; i++) { Chunk c = new Chunk(); c.uncompressedOffset = raw.ReadValueS32(); c.uncompressedSize = raw.ReadValueS32(); c.compressedOffset = raw.ReadValueS32(); c.compressedSize = raw.ReadValueS32(); c.Compressed = new byte[c.compressedSize]; c.Uncompressed = new byte[c.uncompressedSize]; //DebugOutput.PrintLn("Chunk " + i + ", compressed size = " + c.compressedSize + ", uncompressed size = " + c.uncompressedSize); //DebugOutput.PrintLn("Compressed offset = " + c.compressedOffset + ", uncompressed offset = " + c.uncompressedOffset); Chunks.Add(c); } //DebugOutput.PrintLn("\tRead Chunks..."); int count = 0; for (int i = 0; i < Chunks.Count; i++) { Chunk c = Chunks[i]; raw.Seek(c.compressedOffset, SeekOrigin.Begin); c.Compressed = raw.ReadBytes(c.compressedSize); ChunkHeader h = new ChunkHeader(); h.magic = BitConverter.ToInt32(c.Compressed, 0); if (h.magic != -1641380927) throw new FormatException("Chunk magic number incorrect"); h.blocksize = BitConverter.ToInt32(c.Compressed, 4); h.compressedsize = BitConverter.ToInt32(c.Compressed, 8); h.uncompressedsize = BitConverter.ToInt32(c.Compressed, 12); //DebugOutput.PrintLn("Chunkheader read: Magic = " + h.magic + ", Blocksize = " + h.blocksize + ", Compressed Size = " + h.compressedsize + ", Uncompressed size = " + h.uncompressedsize); pos = 16; int blockCount = (h.uncompressedsize % h.blocksize == 0) ? h.uncompressedsize / h.blocksize : h.uncompressedsize / h.blocksize + 1; List<Block> BlockList = new List<Block>(); //DebugOutput.PrintLn("\t\t" + count + " Read Blockheaders..."); for (int j = 0; j < blockCount; j++) { Block b = new Block(); b.compressedsize = BitConverter.ToInt32(c.Compressed, pos); b.uncompressedsize = BitConverter.ToInt32(c.Compressed, pos + 4); //DebugOutput.PrintLn("Block " + j + ", compressed size = " + b.compressedsize + ", uncompressed size = " + b.uncompressedsize); pos += 8; BlockList.Add(b); } int outpos = 0; //DebugOutput.PrintLn("\t\t" + count + " Read and decompress Blocks..."); foreach (Block b in BlockList) { byte[] datain = new byte[b.compressedsize]; byte[] dataout = new byte[b.uncompressedsize]; for (int j = 0; j < b.compressedsize; j++) datain[j] = c.Compressed[pos + j]; pos += b.compressedsize; try { LZO1X.Decompress(datain, dataout); } catch { throw new Exception("LZO decompression failed!"); } for (int j = 0; j < b.uncompressedsize; j++) c.Uncompressed[outpos + j] = dataout[j]; outpos += b.uncompressedsize; } c.header = h; c.blocks = BlockList; count++; Chunks[i] = c; } MemoryTributary result = new MemoryTributary(); foreach (Chunk c in Chunks) { result.Seek(c.uncompressedOffset, SeekOrigin.Begin); result.WriteBytes(c.Uncompressed); } return result; }
private void ReadNames(MemoryTributary fs) { DebugOutput.PrintLn("Reading Names..."); fs.Seek(NameOffset, SeekOrigin.Begin); Names = new List<string>(); for (int i = 0; i < NameCount; i++) { int len = fs.ReadValueS32(); string s = ""; if (len > 0) { s = fs.ReadString((uint)(len - 1)); fs.Seek(9, SeekOrigin.Current); } else { len *= -1; for (int j = 0; j < len - 1; j++) { s += (char)fs.ReadByte(); fs.ReadByte(); } fs.Seek(10, SeekOrigin.Current); } Names.Add(s); } }
public PCCObject(String path) { lzo = new SaltLZOHelper(); fullname = path; BitConverter.IsLittleEndian = true; DebugOutput.PrintLn("Load file : " + path); pccFileName = Path.GetFullPath(path); MemoryTributary tempStream = new MemoryTributary(); if (!File.Exists(pccFileName)) throw new FileNotFoundException("PCC file not found"); using (FileStream fs = new FileStream(pccFileName, FileMode.Open, FileAccess.Read)) { FileInfo tempInfo = new FileInfo(pccFileName); tempStream.WriteFromStream(fs, tempInfo.Length); if (tempStream.Length != tempInfo.Length) { throw new FileLoadException("File not fully read in. Try again later"); } } tempStream.Seek(12, SeekOrigin.Begin); int tempNameSize = tempStream.ReadValueS32(); tempStream.Seek(64 + tempNameSize, SeekOrigin.Begin); int tempGenerator = tempStream.ReadValueS32(); tempStream.Seek(36 + tempGenerator * 12, SeekOrigin.Current); int tempPos = (int)tempStream.Position + 4; tempStream.Seek(0, SeekOrigin.Begin); header = tempStream.ReadBytes(tempPos); tempStream.Seek(0, SeekOrigin.Begin); if (magic != ZBlock.magic && magic.Swap() != ZBlock.magic) { DebugOutput.PrintLn("Magic number incorrect: " + magic); throw new FormatException("This is not a pcc file. The magic number is incorrect."); } if (bCompressed) { DebugOutput.PrintLn("File is compressed"); listsStream = lzo.DecompressPCC(tempStream, this); //Correct the header bCompressed = false; listsStream.Seek(0, SeekOrigin.Begin); listsStream.WriteBytes(header); // Set numblocks to zero listsStream.WriteValueS32(0); //Write the magic number listsStream.WriteBytes(new byte[] { 0xF2, 0x56, 0x1B, 0x4E }); // Write 4 bytes of 0 listsStream.WriteValueS32(0); } else { DebugOutput.PrintLn("File already decompressed. Reading decompressed data."); //listsStream = tempStream; listsStream = new MemoryTributary(); tempStream.WriteTo(listsStream); } tempStream.Dispose(); ReadNames(listsStream); ReadImports(listsStream); ReadExports(listsStream); LoadExports(); }
// KFreon: Decompresses specific blocks if compressed, or returns data specified by offset and length. public byte[] Decompressor(uint offset, int length) { using (MemoryTributary retval = new MemoryTributary()) { uint newoffset = 0; /*if (blockList.Count == 1) bCompressed = false;*/ /*if (bCompressed) {*/ // KFreon: Find datablocks to decompress int DataStart = 0; int DataEnd = 0; int got = 0; for (int m = 0; m < blockList.Count; m++) { if (got == 0 && blockList[m].uncOffset + blockList[m].uncSize > offset) { DataStart = m; got++; } if (got == 1 && blockList[m].uncOffset + blockList[m].uncSize > offset + length) { DataEnd = m; got++; } if (got == 2) break; } if (DataEnd == 0 && DataStart != 0) DataEnd = DataStart; /*// KFreon: Move end along so as able to read if (DataStart == DataEnd) DataEnd++;*/ // KFreon: Decompress blocks newoffset = offset - (uint)blockList[DataStart].uncOffset; for (int i = (int)DataStart; i <= DataEnd; i++) { DataStream.Seek(blockList[i].cprOffset, SeekOrigin.Begin); //retval.Seek(blockList[i].uncOffset, SeekOrigin.Begin); retval.WriteBytes(ZBlock.Decompress(DataStream, blockList[i].cprSize)); } /*} else { listsStream.Seek(offset, SeekOrigin.Begin); retval.ReadFrom(listsStream, length); newoffset = 0; }*/ //retval.Seek(offset, SeekOrigin.Begin); retval.Seek(newoffset, SeekOrigin.Begin); return retval.ReadBytes(length); } }
public void ME3PCCObjectHelper(MemoryTributary tempStream, string filePath, bool TablesOnly) { tempStream.Seek(0, SeekOrigin.Begin); DataStream = new MemoryTributary(); tempStream.WriteTo(DataStream); Names = new List<string>(); Imports = new List<ME3ImportEntry>(); Exports = new List<ME3ExportEntry>(); header = tempStream.ReadBytes(headerSize); if (magic != ZBlock.magic && magic.Swap() != ZBlock.magic) throw new FormatException(filePath + " is not a pcc file"); if (lowVers != 684 && highVers != 194) throw new FormatException("unsupported version"); if (bCompressed) { // seeks the blocks info position tempStream.Seek(idxOffsets + 60, SeekOrigin.Begin); int generator = tempStream.ReadValueS32(); tempStream.Seek((generator * 12) + 20, SeekOrigin.Current); int blockCount = tempStream.ReadValueS32(); blockList = new List<Block>(); // creating the Block list for (int i = 0; i < blockCount; i++) { Block temp = new Block(); temp.uncOffset = tempStream.ReadValueS32(); temp.uncSize = tempStream.ReadValueS32(); temp.cprOffset = tempStream.ReadValueS32(); temp.cprSize = tempStream.ReadValueS32(); blockList.Add(temp); } // correcting the header, in case there's need to be saved Buffer.BlockCopy(BitConverter.GetBytes((int)0), 0, header, header.Length - 12, sizeof(int)); tempStream.Read(header, header.Length - 8, 8); headerEnd = (int)tempStream.Position; // copying the extraNamesList int extraNamesLenght = blockList[0].cprOffset - headerEnd; if (extraNamesLenght > 0) { extraNamesList = new byte[extraNamesLenght]; tempStream.Read(extraNamesList, 0, extraNamesLenght); //FileStream fileStream = File.Create(Path.GetDirectoryName(pccFileName) + "\\temp.bin"); //fileStream.Write(extraNamesList, 0, extraNamesLenght); //MessageBox.Show("posizione: " + pccStream.Position.ToString("X8")); } int dataStart = 0; using (MemoryStream he = new MemoryStream(header)) { he.Seek(0, SeekOrigin.Begin); he.ReadValueS32(); he.ReadValueS32(); dataStart = he.ReadValueS32(); } if (TablesOnly) { int TableStart = 0; for (int m = 0; m < blockList.Count; m++) { if (blockList[m].uncOffset + blockList[m].uncSize > dataStart) { TableStart = m; break; } } listsStream = new MemoryTributary(); tempStream.Seek(blockList[TableStart].cprOffset, SeekOrigin.Begin); listsStream.Seek(blockList[TableStart].uncOffset, SeekOrigin.Begin); listsStream.WriteBytes(ZBlock.Decompress(tempStream, blockList[TableStart].cprSize)); DataStream = new MemoryTributary(); tempStream.WriteTo(DataStream); bCompressed = true; } else { //Decompress ALL blocks listsStream = new MemoryTributary(); for (int i = 0; i < blockCount; i++) { tempStream.Seek(blockList[i].cprOffset, SeekOrigin.Begin); listsStream.Seek(blockList[i].uncOffset, SeekOrigin.Begin); listsStream.WriteBytes(ZBlock.Decompress(tempStream, blockList[i].cprSize)); } } bCompressed = false; } else { listsStream = new MemoryTributary(); listsStream.WriteBytes(tempStream.ToArray()); } tempStream.Dispose(); //Fill name list listsStream.Seek(NameOffset, SeekOrigin.Begin); for (int i = 0; i < NameCount; i++) { int strLength = listsStream.ReadValueS32(); Names.Add(listsStream.ReadString(strLength * -2, true, Encoding.Unicode)); } // fill import list listsStream.Seek(ImportOffset, SeekOrigin.Begin); byte[] buffer = new byte[ME3ImportEntry.byteSize]; for (int i = 0; i < ImportCount; i++) { Imports.Add(new ME3ImportEntry(this, listsStream)); } //fill export list listsStream.Seek(ExportOffset, SeekOrigin.Begin); for (int i = 0; i < ExportCount; i++) { uint expInfoOffset = (uint)listsStream.Position; listsStream.Seek(44, SeekOrigin.Current); int count = listsStream.ReadValueS32(); listsStream.Seek(-48, SeekOrigin.Current); int expInfoSize = 68 + (count * 4); buffer = new byte[expInfoSize]; listsStream.Read(buffer, 0, buffer.Length); Exports.Add(new ME3ExportEntry(this, buffer, expInfoOffset)); } }