public void Parse(IBinaryStream index) { index.Position = 16; int segment_count = Binary.BigEndian(index.ReadInt32()); m_segments = new List <BundleSegment> (segment_count); long packed_offset = m_data_offset; long unpacked_offset = 0; for (int i = 0; i < segment_count; ++i) { var segment = new BundleSegment(); segment.Offset = packed_offset; segment.UnpackedOffset = unpacked_offset; segment.UnpackedSize = Binary.BigEndian(index.ReadUInt32()); segment.PackedSize = Binary.BigEndian(index.ReadUInt32()); segment.Compression = Binary.BigEndian(index.ReadUInt16()); m_segments.Add(segment); packed_offset += segment.PackedSize; unpacked_offset += segment.UnpackedSize; } int count = Binary.BigEndian(index.ReadInt32()); m_bundles = new List <BundleEntry> (count); for (int i = 0; i < count; ++i) { var entry = new BundleEntry(); entry.Offset = Binary.BigEndian(index.ReadInt64()); entry.Size = (uint)Binary.BigEndian(index.ReadInt64()); entry.Flags = Binary.BigEndian(index.ReadUInt32()); entry.Name = index.ReadCString(Encoding.UTF8); m_bundles.Add(entry); } }
/// <summary> /// Setup reader endianness accordingly. /// </summary> public void SetupReaders(int format, bool is_little_endian) { m_format = format; if (is_little_endian) { ReadUInt16 = () => m_input.ReadUInt16(); ReadUInt32 = () => m_input.ReadUInt32(); ReadInt16 = () => m_input.ReadInt16(); ReadInt32 = () => m_input.ReadInt32(); ReadInt64 = () => m_input.ReadInt64(); } else { ReadUInt16 = () => Binary.BigEndian(m_input.ReadUInt16()); ReadUInt32 = () => Binary.BigEndian(m_input.ReadUInt32()); ReadInt16 = () => Binary.BigEndian(m_input.ReadInt16()); ReadInt32 = () => Binary.BigEndian(m_input.ReadInt32()); ReadInt64 = () => Binary.BigEndian(m_input.ReadInt64()); } if (m_format >= 14 || m_format == 9) { Align = () => { long pos = m_input.Position; if (0 != (pos & 3)) { m_input.Position = (pos + 3) & ~3L; } }; } else { Align = () => {}; } if (m_format >= 14) { ReadId = ReadInt64; } else { ReadId = () => ReadInt32(); } if (m_format >= 22) { ReadOffset = ReadInt64; } else { ReadOffset = () => ReadUInt32(); } }
public override ImageMetaData ReadMetaData(IBinaryStream stream) { int width = stream.ReadInt16(); int height = stream.ReadInt16(); if (width <= 0 || height <= 0) { return(null); } int bpp = stream.ReadInt32(); if (24 != bpp && 32 != bpp && 8 != bpp) { return(null); } if (0 != stream.ReadInt64()) { return(null); } return(new ImageMetaData { Width = (uint)width, Height = (uint)height, BPP = bpp, }); }
public List <Entry> Read(IEncryption enc, byte[] key_file, bool use_pack_keyfile) { m_dir.Clear(); m_index.Position = 0; bool read_pack_keyfile = 3 == m_pack_version.Major && use_pack_keyfile; for (int i = 0; i < m_count; ++i) { int name_length = m_index.ReadUInt16(); if (enc.IsUnicode) { name_length *= 2; } if (name_length > m_name_buffer.Length) { m_name_buffer = new byte[name_length]; } if (name_length != m_index.Read(m_name_buffer, 0, name_length)) { return(null); } var name = enc.DecryptName(m_name_buffer, name_length); var entry = m_fmt.Create <QlieEntry> (name); if (use_pack_keyfile) { entry.RawName = m_name_buffer.Take(name_length).ToArray(); } entry.Offset = m_index.ReadInt64(); // [+00] entry.Size = m_index.ReadUInt32(); // [+08] if (!entry.CheckPlacement(m_file.MaxOffset)) { return(null); } entry.UnpackedSize = m_index.ReadUInt32(); // [+0C] entry.IsPacked = 0 != m_index.ReadInt32(); // [+10] entry.EncryptionMethod = m_index.ReadInt32(); // [+14] if (m_pack_version.Major > 1) { entry.Hash = m_index.ReadUInt32(); // [+18] } entry.KeyFile = key_file; if (read_pack_keyfile && entry.Name.Contains("pack_keyfile")) { // note that 'pack_keyfile' itself is encrypted using 'key.fkey' file contents. key_file = m_fmt.ReadEntryBytes(m_file, entry, enc); read_pack_keyfile = false; } m_dir.Add(entry); } return(m_dir); }
internal static object ReadField(this IBinaryStream input, FieldInfo field) { var field_type = field.FieldType; var type_code = Type.GetTypeCode(field_type); switch (type_code) { case TypeCode.SByte: return(input.ReadInt8()); case TypeCode.Byte: return(input.ReadUInt8()); case TypeCode.Int16: return(input.ReadInt16()); case TypeCode.UInt16: return(input.ReadUInt16()); case TypeCode.Int32: return(input.ReadInt32()); case TypeCode.UInt32: return(input.ReadUInt32()); case TypeCode.Int64: return(input.ReadInt64()); case TypeCode.UInt64: return(input.ReadUInt64()); case TypeCode.Char: return((char)input.ReadInt16()); case TypeCode.String: var cstring_attr = field.GetCustomAttribute <CStringAttribute>(); if (cstring_attr != null) { var encoding = cstring_attr.Encoding ?? Encodings.cp932; if (cstring_attr.IsLengthDefined) { return(input.ReadCString(cstring_attr.Length, encoding)); } else { return(input.ReadCString(encoding)); } } throw new FormatException("Serialization method for string field is not defined."); case TypeCode.Object: object val = Activator.CreateInstance(field_type); ReadStruct(input, field_type, ref val); // FIXME check object graph for cycles return(val); default: throw new NotSupportedException("Not supported serialization type."); } }
} // '256G' public override ImageMetaData ReadMetaData(IBinaryStream file) { file.Position = 4; var info = new GgdMetaData { BPP = 8 }; info.HeaderSize = file.ReadUInt32(); info.Width = file.ReadUInt32(); int height = file.ReadInt32(); if (height < 0) { height = -height; info.Flipped = true; } info.Height = (uint)height; file.ReadInt64(); info.BitmapSize = file.ReadUInt32(); return(info); }
public override ImageMetaData ReadMetaData(IBinaryStream input) { int sig = input.ReadInt32(); if (sig != 5 && sig != 10) { return(null); } uint header_end = (uint)sig * 4; uint index_offset = input.ReadUInt32(); input.Position = header_end - 4; uint data_offset = input.ReadUInt32() + 8; if (index_offset >= data_offset || index_offset < header_end) { return(null); } input.Position = index_offset; int tile_count = input.ReadInt16(); int flag = input.ReadInt16(); if (tile_count <= 0 || 0 != flag) { return(null); } input.ReadInt32(); uint w = input.ReadUInt16(); uint h = input.ReadUInt16(); if (0 == w || 0 == h) { return(null); } var meta = new BipMetaData { Width = w, Height = h, BPP = 32 }; meta.Tiles.Capacity = tile_count; for (int i = 0; i < tile_count; ++i) { input.ReadInt64(); var tile = new BipTile(); tile.Left = input.ReadUInt16(); tile.Top = input.ReadUInt16(); tile.Width = input.ReadUInt16(); tile.Height = input.ReadUInt16(); if (tile.Left + tile.Width > meta.Width) { meta.Width = (uint)(tile.Left + tile.Width); } if (tile.Top + tile.Height > meta.Height) { meta.Height = (uint)(tile.Top + tile.Height); } input.ReadInt64(); tile.Offset = input.ReadUInt32() + data_offset; meta.Tiles.Add(tile); } return(meta); }
public long ReadInt64() { return(m_input.ReadInt64()); }
List <Sample> ReadSamples() { var header = m_input.ReadHeader(0x3C); int version = header.ToInt32(4); int sample_count = header.ToInt32(8); m_sample_header_size = header.ToInt32(0xC); m_name_table_size = header.ToInt32(0x10); m_data_size = header.ToInt32(0x14); m_format = (SoundFormat)header.ToInt32(0x18); if (!Supported.Contains(m_format)) { throw new NotSupportedException(); } if (0 == version) { m_input.ReadInt32(); } var samples = new List <Sample> (sample_count); m_header_size = (int)m_input.Position; for (int i = 0; i < sample_count; ++i) { long raw = m_input.ReadInt64(); bool next_chunk = 0 != (raw & 1); int sample_rate = (int)((raw >> 1) & 0xF); ushort channels = (ushort)(((raw >> 5) & 1) + 1); long data_offset = ((raw >> 6) & 0xFFFFFFF) * 0x10; int count = (int)((raw >> 34) & 0x3FFFFFFF); var chunks = new Dictionary <ChunkType, object>(); while (next_chunk) { int d = m_input.ReadInt32(); next_chunk = 0 != (d & 1); int chunk_size = (d >> 1) & 0xFFFFFF; var chunk_type = (ChunkType)((d >> 25) & 0x7F); object chunk; switch (chunk_type) { case ChunkType.Channels: chunk = m_input.ReadUInt8(); break; case ChunkType.SampleRate: chunk = m_input.ReadInt32(); break; case ChunkType.Loop: int v1 = m_input.ReadInt32(); int v2 = m_input.ReadInt32(); chunk = Tuple.Create(v1, v2); break; case ChunkType.VorbisData: chunk = new VorbisData { Crc32 = m_input.ReadUInt32(), Data = m_input.ReadBytes(chunk_size - 4) // XXX unused }; break; default: chunk = m_input.ReadBytes(chunk_size); break; } chunks[chunk_type] = chunk; } if (chunks.ContainsKey(ChunkType.SampleRate)) { sample_rate = (int)chunks[ChunkType.SampleRate]; } else if (SampleRates.ContainsKey(sample_rate)) { sample_rate = SampleRates[sample_rate]; } else { throw new InvalidFormatException("Invalid FSB5 sample rate."); } var sample = new Sample { SampleRate = sample_rate, Channels = channels, DataOffset = data_offset, SampleCount = count, MetaData = chunks, Data = null }; samples.Add(sample); } return(samples); }