public static StringField DecodingString(Stream stream) { int lengthNeedRead = 0; while (true) { var b = stream.ReadByte(); if (b == _stringSpliter) { break; } lengthNeedRead++; } stream.Position = stream.Position - lengthNeedRead - 1; byte[] lengthBuf = new byte[lengthNeedRead]; stream.EnsureRead(lengthBuf, 0, lengthNeedRead); var lengthStr = Encoding.UTF8.GetString(lengthBuf); if (!int.TryParse(lengthStr, out int length)) { throw new Exception("字符串的长度格式不对"); } stream.ReadByte(); var buf = new byte[length]; stream.EnsureRead(buf, 0, length); var val = Encoding.UTF8.GetString(buf); return(StringField.Create(val, length, buf)); }
public unsafe void ReadFromStream(Stream input) { if (input.Length - input.Position < 16) return; byte[] buff = input.EnsureRead(16); fixed (byte* b = &buff[0]) { Magic = Endian.ToBigInt32(b + 0); Count = Endian.ToBigInt32(b + 4); } Entries = new WpdEntry[Count]; if (Count < 1) return; buff = input.EnsureRead(Count * 32); fixed (byte* b = &buff[0]) { for (int i = 0; i < Count; i++) { int offset = i * 32; Entries[i] = new WpdEntry( i, new string((sbyte*)b + offset), Endian.ToBigInt32(b + offset + 16), Endian.ToBigInt32(b + offset + 20), new string((sbyte*)b + offset + 24)); } } }
private void Decompress() { PacHeader header = _input.EnsureRead <PacHeader>(); header.Check(); PacEntry[] entries = _input.EnsureRead <PacEntry>(header.FileCount); Int64 beginPosition = _output.Position; Int32 headerSize = PacHeader.StructSize + PacEntry.StructSize * entries.Length; Int64 decompressedArchiveSize = headerSize + entries.Sum(e => (Int64)e.UncompressedSize); Int64 totalSize = beginPosition + decompressedArchiveSize; if (totalSize > UInt32.MaxValue) { throw new NotSupportedException("The game cannot address more than 4 GB of files. You can unpack this file, but you cannot decompress it."); } _output.SetLength(totalSize); _output.Seek(headerSize, SeekOrigin.Current); Int64 dataPosition = _output.Position; for (Int32 index = 0; index < entries.Length; index++) { ref var entry = ref entries[index]; Boolean isCompressed = IsCompressed(entry.CompressionType); Span <Byte> data = _reader.Read(_input, entry.CompressedSize, entry.UncompressedSize, isCompressed); entry.Offset = checked ((UInt32)(_output.Position - dataPosition)); entry.CompressedSize = entry.UncompressedSize; entry.CompressionType = PakCompression.None; _output.Write(data); }
public async Task Decrypt(Stream input, Stream output) { _aes.IV = input.EnsureRead(BitConverter.ToInt32(input.EnsureRead(4), 0)); Progress.NullSafeInvoke(4); using (ICryptoTransform decryptor = _aes.CreateDecryptor()) using (CryptoStream encryptionStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read)) { if (_cancelEvent.IsSet()) return; await PatcherService.CopyAsync(encryptionStream, output, _cancelEvent, Progress); } }
public async Task Decrypt(Stream input, Stream output) { _aes.IV = input.EnsureRead(BitConverter.ToInt32(input.EnsureRead(4), 0)); Progress.NullSafeInvoke(4); using (ICryptoTransform decryptor = _aes.CreateDecryptor()) using (CryptoStream encryptionStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read)) { if (_cancelEvent.IsSet()) { return; } await PatcherService.CopyAsync(encryptionStream, output, _cancelEvent, Progress); } }
public static T[] ReadStructs <T>(this Stream input, int count) where T : new() { if (input == null) { throw new ArgumentNullException("input"); } T[] result = new T[count]; if ((TypeCache <T> .Flags & TypeCacheFlags.ReadableFromStream) == TypeCacheFlags.ReadableFromStream) { for (int i = 0; i < result.Length; i++) { T value = new T(); IReadableFromStream readable = (IReadableFromStream)value; readable.ReadFromStream(input); result[i] = value; } } else { int size = Marshal.SizeOf(TypeCache <T> .Type); byte[] buff = new byte[size]; using (SafeGCHandle handle = new SafeGCHandle(buff, GCHandleType.Pinned)) { for (int i = 0; i < result.Length; i++) { input.EnsureRead(buff, 0, size); result[i] = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), TypeCache <T> .Type); } } } return(result); }
public static NumberField DecodingLong(Stream stream) { var begin = stream.ReadByte(); if (begin != _startInt) { throw new Exception("整数开始字节错误"); } int lengthNeedRead = 0; while (true) { var b = stream.ReadByte(); if (b == _end) { break; } lengthNeedRead++; } stream.Position = stream.Position - lengthNeedRead - 1; byte[] intBuf = new byte[lengthNeedRead]; stream.EnsureRead(intBuf, 0, lengthNeedRead); stream.ReadByte(); var longStr = Encoding.UTF8.GetString(intBuf); if (!long.TryParse(longStr, out long intVal)) { throw new Exception("整数内容的格式错误"); } return(NumberField.Create(intVal)); }
private static void Write(Stream paletteStream, Stream output) { Exceptions.CheckArgumentNull(paletteStream, "palette"); Exceptions.CheckArgumentNull(output, "output"); byte[] buff = new byte[4]; paletteStream.EnsureRead(buff, 0, 4); buff.Swap(0, 2); output.Write(buff, 0, 3); bool hasTransperentColor = buff[3] == 0; for (int i = 0; i < 255; i++) { paletteStream.Read(buff, 0, 4); buff.Swap(0, 2); output.Write(buff, 0, 3); } if (hasTransperentColor) { unsafe { fixed(byte *ptr = buff) * (int *)ptr = 1; } } output.Write(buff, 0, 4); }
public static DdsHeader FromFileStream(Stream input) { byte[] buff = new byte[128]; using (SafeGCHandle handle = new SafeGCHandle(buff, GCHandleType.Pinned)) { input.EnsureRead(buff, 0, buff.Length); return((DdsHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + 4, TypeCache <DdsHeader> .Type)); } }
public static DdsHeader FromFileStream(Stream input) { byte[] buff = new byte[128]; using (SafeGCHandle handle = new SafeGCHandle(buff, GCHandleType.Pinned)) { input.EnsureRead(buff, 0, buff.Length); return (DdsHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject() + 4, TypeCache<DdsHeader>.Type); } }
public void ReadFromStream(Stream stream) { BinaryReader br = new BinaryReader(stream); stream.EnsureRead(KeyData, 0, KeyDataSize); RawBlockOffset = br.ReadInt32(); RawInfoOffset = br.ReadInt32(); EntriesCount = br.ReadInt32(); }
public unsafe void ReadFromStream(Stream input) { byte[] buff = input.EnsureRead(0x10); fixed(byte *b = &buff[0]) { Version = Endian.ToBigInt32(b + 0); Count = Endian.ToBigInt32(b + 4); KeysUnpackedSize = Endian.ToBigInt32(b + 8); TextBlocksCount = Endian.ToBigInt32(b + 12); } if (Version != 1) { throw new NotImplementedException(); } TextBlockTable = new int[TextBlocksCount]; if (TextBlocksCount > 0) { buff = input.EnsureRead(TextBlocksCount * 4); fixed(byte *b = &buff[0]) { for (int i = 0; i < TextBlocksCount; i++) { TextBlockTable[i] = Endian.ToBigInt32(b + i * 4); } } } TextLinesTable = new ZtrFileHeaderLineInfo[Count]; if (Count > 0) { buff = input.EnsureRead(Count * 4); fixed(byte *b = &buff[0]) { for (int i = 0; i < Count; i++) { TextLinesTable[i].Block = *(b + i * 4); TextLinesTable[i].BlockOffset = *(b + i * 4 + 1); TextLinesTable[i].PackedOffset = Endian.ToBigUInt16(b + i * 4 + 2); } } } }
public static string ReadFixedSizeString(this Stream input, int size, Encoding encoding) { unsafe { byte[] name = input.EnsureRead(size); fixed(byte *namePtr = &name[0]) return(new string((sbyte *)namePtr, 0, size, encoding).TrimEnd('\0')); } }
public WaveStream[] Read() { SectionHeader sectionHeader = _input.ReadContent <SectionHeader>(); SscfHeader sscfHeader = _input.ReadContent <SscfHeader>(); BinaryReader br = new BinaryReader(_input); _input.SetPosition(sscfHeader.WavesOffset); int[] offsets = new int[sscfHeader.NumWaves]; for (int i = 0; i < offsets.Length; i++) { offsets[i] = br.ReadInt32(); } WaveStream[] result = new WaveStream[sscfHeader.NumWaves]; for (int i = 0; i < offsets.Length; i++) { _input.SetPosition(offsets[i]); SscfWaveHeader waveHeader = _input.ReadContent <SscfWaveHeader>(); if (waveHeader.Format == SscfWaveFormat.Vorbis) { _input.SetPosition(waveHeader.DataOffset); byte[] vorbisData = _input.EnsureRead(waveHeader.DataLength); MemoryStream vorbisMs = new MemoryStream(vorbisData, 0, vorbisData.Length, false); result[i] = new VorbisWaveReader(vorbisMs); continue; } WaveFormat format = ReadWaveFormat(waveHeader); if (format == null) { continue; } _input.SetPosition(waveHeader.DataOffset); byte[] data = _input.EnsureRead(waveHeader.DataLength); MemoryStream ms = new MemoryStream(data, 0, data.Length, false); result[i] = new RawSourceWaveStream(ms, format); } return(result); }
private static Color ReadA1B5G5R5Color(Stream input, byte[] buff) { input.EnsureRead(buff, 0, 2); ushort color = BitConverter.ToUInt16(buff, 0); return Color.FromArgb( (byte)(((color >> 15) & 1) * 255), (byte)Math.Round((color & 31) * ColorRate), (byte)Math.Round((color >> 5 & 31) * ColorRate), (byte)Math.Round((color >> 10 & 31) * ColorRate)); }
private static Color ReadA1B5G5R5Color(Stream input, byte[] buff) { input.EnsureRead(buff, 0, 2); ushort color = BitConverter.ToUInt16(buff, 0); return(Color.FromArgb( (byte)(((color >> 15) & 1) * 255), (byte)Math.Round((color & 31) * ColorRate), (byte)Math.Round((color >> 5 & 31) * ColorRate), (byte)Math.Round((color >> 10 & 31) * ColorRate))); }
public unsafe void ReadFromStream(Stream stream) { byte[] data = stream.EnsureRead(StructSize); fixed (byte* b = data) { PackageNameOffset = Endian.ToBigInt32(b + 0 * 4); Length = Endian.ToBigInt32(b + 1 * 4); Dummy = Endian.ToBigInt32(b + 2 * 4); Offset = Endian.ToBigUInt32(b + 3 * 4); } }
public unsafe void ReadFromStream(Stream stream) { byte[] data = stream.EnsureRead(StructSize); fixed(byte *b = data) { PackageNameOffset = Endian.ToBigInt32(b + 0 * 4); Length = Endian.ToBigInt32(b + 1 * 4); Dummy = Endian.ToBigInt32(b + 2 * 4); Offset = Endian.ToBigUInt32(b + 3 * 4); } }
public unsafe void ReadFromStream(Stream stream) { byte[] name = stream.EnsureRead(NameSize); fixed (byte* namePtr = &name[0]) Name = new string((sbyte*)namePtr, 0, NameSize, YkdFile.NamesEncoding).TrimEnd('\0'); Offsets = stream.ReadContent<YkdOffsets>(); Frames = new YkdFrames[Offsets.Count]; for (int i = 0; i < Frames.Length; i++) Frames[i] = stream.ReadContent<YkdFrames>(); }
private void Decompress() { CpkHeader header = _input.EnsureRead <CpkHeader>(); header.Check(); Boolean isCompressed = IsCompressed(header.CompressionType); Span <Byte> data = _reader.Read(_input, header.CompressedSize, header.UncompressedSize, isCompressed); _output.Write(data); }
private void SafeReadAdditionalEntryInfo(IsoTableEntryInfo info) { try { if (info.CompressedSize < 4) { return; } byte[] signature; using (Stream input = OpenStream(info.Offset, info.CompressedSize)) using (MemoryStream output = new MemoryStream(4)) { if (!info.IsCompressed) { info.IsCompressed = (input.ReadByte() == 0x01); input.Seek(-1, SeekOrigin.Current); } if (info.IsCompressed) { input.Seek(1, SeekOrigin.Current); int uncompressedSize = input.ReadStruct <int>(); if (uncompressedSize < 4) { return; } LZSStream decompressStream = new LZSStream(input, output); decompressStream.Decompress(4); output.Flush(); signature = output.ToArray(); if (signature.Length == 0) { return; } } else { signature = new byte[4]; input.EnsureRead(signature, 0, 4); } } info.Signature = (FFXFileSignatures)BitConverter.ToInt32(signature, 0); } catch (Exception ex) { Log.Error(ex, "Failed to read additional info of entry '{0}'", info.GetFileName()); } finally { ProgressIncrement.NullSafeInvoke(1); } }
public static unsafe ZtrFileEncoding ReadFromStream(Stream input) { int blockSize; byte[] buff = input.EnsureRead(4); fixed(byte *b = &buff[0]) blockSize = Endian.ToBigInt32(b); byte[] values = new byte[blockSize / 3]; byte[,] encoding = new byte[256, 2]; if (blockSize > 0) { buff = input.EnsureRead(blockSize); fixed(byte *b = &buff[0]) { for (int i = 0; i < values.Length; i++) { byte value = *(b + i * 3); encoding[value, 0] = *(b + i * 3 + 1); encoding[value, 1] = *(b + i * 3 + 2); values[i] = value; } } } List <byte>[] lists = new List <byte> [256]; for (int i = 0; i < 256; i++) { List <byte> list = lists[i] = new List <byte>(16); DecodeByte(values, encoding, (byte)i, list); } byte[][] result = new byte[256][]; for (int i = 0; i < 256; i++) { result[i] = lists[i].ToArray(); } return(new ZtrFileEncoding(blockSize, result)); }
public unsafe void ReadFromStream(Stream input) { byte[] buff = input.EnsureRead(0x10); fixed (byte* b = &buff[0]) { Version = Endian.ToBigInt32(b + 0); Count = Endian.ToBigInt32(b + 4); KeysUnpackedSize = Endian.ToBigInt32(b + 8); TextBlocksCount = Endian.ToBigInt32(b + 12); } if (Version != 1) throw new NotImplementedException(); TextBlockTable = new int[TextBlocksCount]; if (TextBlocksCount > 0) { buff = input.EnsureRead(TextBlocksCount * 4); fixed (byte* b = &buff[0]) { for (int i = 0; i < TextBlocksCount; i++) TextBlockTable[i] = Endian.ToBigInt32(b + i * 4); } } TextLinesTable = new ZtrFileHeaderLineInfo[Count]; if (Count > 0) { buff = input.EnsureRead(Count * 4); fixed (byte* b = &buff[0]) { for (int i = 0; i < Count; i++) { TextLinesTable[i].Block = *(b + i * 4); TextLinesTable[i].BlockOffset = *(b + i * 4 + 1); TextLinesTable[i].PackedOffset = Endian.ToBigUInt16(b + i * 4 + 2); } } } }
public void ReadFromStream(Stream stream) { BinaryReader br = new BinaryReader(stream); Magic = br.ReadInt32(); if (Magic != MagicNumber) throw new Exception("Неверная сигнатура файла: " + Magic); Type = (SectionType)br.ReadInt32(); Version = br.ReadBigInt32(); Unknown2 = br.ReadBigInt32(); SectionLength = br.ReadBigInt32(); Junk = stream.EnsureRead(28); }
public void ReadFromStream(Stream stream) { BinaryReader br = new BinaryReader(stream); FormatTag = (WaveFormatEncoding)br.ReadInt16(); Channels = br.ReadInt16(); SamplesPerSec = br.ReadInt32(); AverageBytesPerSecond = br.ReadInt32(); BlockAlign = br.ReadInt16(); BitsPerSample = br.ReadInt16(); ExtraDataSize = br.ReadInt16(); ExtraData = stream.EnsureRead(ExtraDataSize); }
public void Unpack() { PacHeader header = _input.EnsureRead <PacHeader>(); header.Check(); PacEntry[] entries = _input.EnsureRead <PacEntry>(header.FileCount); Int64 dataPosition = _input.Position; for (Int32 index = 0; index < entries.Length; index++) { ref var entry = ref entries[index]; Boolean isCompressed = PacDecompressor.IsCompressed(entry.CompressionType); _input.SetPosition(dataPosition + entry.Offset); Span <Byte> data = _reader.Read(_input, entry.CompressedSize, entry.UncompressedSize, isCompressed); String outputPath = _pathBuilder.GetOutputPath(entry.RelativePath, entry.FileId); FileSystem.CreateFileDirectory(outputPath); using (var output = File.Create(outputPath)) output.Write(data); }
public unsafe void ReadFromStream(Stream stream) { byte[] name = stream.EnsureRead(NameSize); fixed(byte *namePtr = &name[0]) Name = new string((sbyte *)namePtr, 0, NameSize, YkdFile.NamesEncoding).TrimEnd('\0'); Offsets = stream.ReadContent <YkdOffsets>(); Frames = new YkdFrames[Offsets.Count]; for (int i = 0; i < Frames.Length; i++) { Frames[i] = stream.ReadContent <YkdFrames>(); } }
public static T[] ReadStructs <T>(this Stream input, Int32 count) where T : unmanaged { if (count < 1) { return(new T[0]); } Array result = new T[count]; Int32 entrySize = UnsafeTypeCache <T> .UnsafeSize; using (UnsafeTypeCache <Byte> .ChangeArrayTypes(result, entrySize)) input.EnsureRead((Byte[])result, 0, result.Length); return((T[])result); }
public override void ReadFromStream(Stream stream) { BinaryReader br = new BinaryReader(stream); UnknownData = stream.EnsureRead(16); ViewportWidth = br.ReadInt32(); ViewportHeight = br.ReadInt32(); UpperLeftColor = br.ReadInt32(); BottomLeftColor = br.ReadInt32(); UpperRightColor = br.ReadInt32(); BottomRightColor = br.ReadInt32(); Unknown1 = br.ReadInt32(); Unknown2 = br.ReadInt32(); }
public void ComputeSha1(Stream stream) { var oldPos = stream.Position; var sha1 = SHA1.Create(); stream.Position = OffsetStart; var buf = new byte[OffsetEnd - OffsetStart + 1]; stream.EnsureRead(buf, 0, buf.Length); Sha1Val = sha1.ComputeHash(buf); sha1.Dispose(); stream.Position = oldPos; }
public static unsafe ZtrFileEncoding ReadFromStream(Stream input) { int blockSize; byte[] buff = input.EnsureRead(4); fixed (byte* b = &buff[0]) blockSize = Endian.ToBigInt32(b); byte[] values = new byte[blockSize / 3]; byte[,] encoding = new byte[256, 2]; if (blockSize > 0) { buff = input.EnsureRead(blockSize); fixed (byte* b = &buff[0]) { for (int i = 0; i < values.Length; i++) { byte value = *(b + i * 3); encoding[value, 0] = *(b + i * 3 + 1); encoding[value, 1] = *(b + i * 3 + 2); values[i] = value; } } } List<byte>[] lists = new List<byte>[256]; for (int i = 0; i < 256; i++) { List<byte> list = lists[i] = new List<byte>(16); DecodeByte(values, encoding, (byte)i, list); } byte[][] result = new byte[256][]; for (int i = 0; i < 256; i++) result[i] = lists[i].ToArray(); return new ZtrFileEncoding(blockSize, result); }
public virtual unsafe void ReadFromStream(Stream input) { if (input.Length - input.Position < 16) { return; } byte[] buff = input.EnsureRead(16); fixed(byte *b = &buff[0]) { Magic = Endian.ToBigInt32(b + 0); Count = Endian.ToBigInt32(b + 4); } Entries = new WpdEntry[Count]; if (Count < 1) { return; } buff = input.EnsureRead(Count * 32); fixed(byte *b = &buff[0]) { for (int i = 0; i < Count; i++) { int offset = i * 32; Entries[i] = new WpdEntry( i, new string((sbyte *)b + offset), Endian.ToBigInt32(b + offset + 16), Endian.ToBigInt32(b + offset + 20), new string((sbyte *)b + offset + 24)); } } }
public static T[] DungerousReadStructs <T>(this Stream input, int count) where T : struct { if (count < 1) { return(new T[0]); } Array result = new T[count]; Int32 entrySize = UnsafeTypeCache <T> .UnsafeSize; using (UnsafeTypeCache <byte> .ChangeArrayType(result, entrySize)) input.EnsureRead((byte[])result, 0, result.Length); return((T[])result); }
public void ReadFromStream(Stream stream) { BinaryReader br = new BinaryReader(stream); Magic = br.ReadInt32(); if (Magic != MagicNumber) { throw new Exception("Неверная сигнатура файла: " + Magic); } Type = (SectionType)br.ReadInt32(); Version = br.ReadBigInt32(); Unknown2 = br.ReadBigInt32(); SectionLength = br.ReadBigInt32(); Junk = stream.EnsureRead(28); }
public override void ReadFromStream(Stream stream) { BinaryReader br = new BinaryReader(stream); SourceX = br.ReadInt32(); SourceY = br.ReadInt32(); SourceWidth = br.ReadInt32(); SourceHeight = br.ReadInt32(); ViewportWidth = br.ReadInt32(); ViewportHeight = br.ReadInt32(); Flags = (YkdResourceFlags)br.ReadInt32(); Unknown5 = br.ReadInt32(); UpperLeftColor = br.ReadInt32(); BottomLeftColor = br.ReadInt32(); UpperRightColor = br.ReadInt32(); BottomRightColor = br.ReadInt32(); Tail = stream.EnsureRead(16); }
public static void DangerousReadStructs <T>(this Stream input, T[] output, Int32 count) where T : struct { if (count < 1) { return; } if (count < output.Length) { throw new ArgumentOutOfRangeException(nameof(count)); } Int32 entrySize = UnsafeTypeCache <T> .UnsafeSize; Int32 sizeToRead = entrySize * count; using (UnsafeTypeCache <Byte> .ChangeArrayTypes(output, entrySize)) input.EnsureRead((Byte[])(Array)output, 0, sizeToRead); }
public Span <Byte> Read(Stream input, Int32 compressedSize, Int32 uncompressedSize, Boolean isCompressed) { Span <Byte> data = _data.GetSpan(compressedSize); input.EnsureRead(data); if (!isCompressed) { if (compressedSize != uncompressedSize) { throw new NotSupportedException("if (compressedSize != uncompressedSize)"); } return(data); } Span <Byte> decompressed = _decompressed.GetSpan(uncompressedSize); HuffmanDecompressor.Decompress(data, decompressed); return(decompressed); }
public static T[] ReadStructs <T>(this Stream input, int count) where T : new() { if (input == null) { throw new ArgumentNullException(nameof(input)); } T[] result = new T[count]; int size = Marshal.SizeOf(TypeCache <T> .Type); byte[] buff = new byte[size]; using (SafeGCHandle handle = new SafeGCHandle(buff, GCHandleType.Pinned)) { for (int i = 0; i < result.Length; i++) { input.EnsureRead(buff, 0, size); result[i] = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), TypeCache <T> .Type); } } return(result); }
private static Color ReadBGRAColor(Stream input, byte[] buff) { input.EnsureRead(buff, 0, 4); return Color.FromArgb(buff[3], buff[2], buff[1], buff[0]); }
private static Color ReadB8G8R8Color(Stream input, byte[] buff) { input.EnsureRead(buff, 0, 3); return Color.FromArgb(255, buff[2], buff[1], buff[0]); }
private static Color ReadBGRAColor(Stream input, byte[] buff) { input.EnsureRead(buff, 0, 4); return(Color.FromArgb(buff[3], buff[2], buff[1], buff[0])); }