Beispiel #1
0
        private static MemoryStream ReadFileData(
            HogFile hog, Hog.FileEntry entry, Stream input)
        {
            var data = new MemoryStream();

            ReadFileData(hog, entry, input, data);
            data.Position = 0;
            return(data);
        }
        private static void ReadFileData(
            HogFile hog,
            Hog.FileEntry entry,
            Stream input,
            Stream output)
        {
            if (hog.Files.Contains(entry) == false)
            {
                throw new ArgumentException("bad entry", "entry");
            }

            if (entry.Size == -1)
            {
                throw new ArgumentException("cannot read file with size of -1", "entry");
            }

            if (entry.Unknown5 != -2 || entry.AttributeId == -1)
            {
                throw new ArgumentException("strange entry");
            }

            if (entry.AttributeId < 0 || entry.AttributeId >= hog.Attributes.Count)
            {
                throw new ArgumentException("entry pointing to invalid metadata", "entry");
            }

            if ((hog.Attributes[entry.AttributeId].Flags & 1) == 1) // entry is unused
            {
                throw new ArgumentException("entry referencing unused attribute", "entry");
            }

            input.Seek(entry.Offset, SeekOrigin.Begin);
            var attribute = hog.Attributes[entry.AttributeId];

            if (attribute.UncompressedSize != 0)
            {
                using (var temp = input.ReadToMemoryStream(entry.Size))
                {
                    var zlib = new InflaterInputStream(temp);
                    output.WriteFromStream(zlib, attribute.UncompressedSize);
                }
            }
            else
            {
                output.WriteFromStream(input, entry.Size);
            }
        }
Beispiel #3
0
        public void Deserialize(Stream input)
        {
            var magic = input.ReadValueU32(Endian.Little);

            if (magic != Signature &&
                magic.Swap() != Signature)
            {
                throw new FormatException("bad hog magic");
            }
            var endian = magic == Signature ? Endian.Little : Endian.Big;

            var version = input.ReadValueU16(endian);

            if (version < 10 || version > 11)
            {
                throw new FormatException("unsupported hog version");
            }

            var operationJournalSize = input.ReadValueU16(endian);
            var fileListSize         = input.ReadValueU32(endian);
            var attributeListSize    = input.ReadValueU32(endian);
            var dataListFileNumber   = input.ReadValueU32(endian);
            var dataListJournalSize  = input.ReadValueU32(endian);

            if (operationJournalSize > 1024)
            {
                throw new FormatException("bad hog operation journal size");
            }

            this.OperationJournal = input.ReadBytes(operationJournalSize);
            this.DataListJournal  = input.ReadBytes((int)dataListJournalSize);

            this.Files.Clear();
            using (var data = input.ReadToMemoryStream((int)fileListSize))
            {
                while (data.Position < data.Length)
                {
                    // ReSharper disable UseObjectOrCollectionInitializer
                    var entry = new Hog.FileEntry();
                    // ReSharper restore UseObjectOrCollectionInitializer
                    entry.Offset      = data.ReadValueS64(endian);
                    entry.Size        = data.ReadValueS32(endian);
                    entry.Timestamp   = data.ReadValueU32(endian);
                    entry.Checksum    = data.ReadValueU32(endian);
                    entry.Unknown4    = data.ReadValueU32(endian);
                    entry.Unknown5    = data.ReadValueS16(endian);
                    entry.Unknown6    = data.ReadValueS16(endian);
                    entry.AttributeId = data.ReadValueS32(endian);
                    this.Files.Add(entry);
                }
            }

            this.Attributes.Clear();
            using (var data = input.ReadToMemoryStream((int)attributeListSize))
            {
                while (data.Position < data.Length)
                {
                    // ReSharper disable UseObjectOrCollectionInitializer
                    var entry = new Hog.AttributeEntry();
                    // ReSharper restore UseObjectOrCollectionInitializer
                    entry.NameId           = data.ReadValueS32(endian);
                    entry.HeaderDataId     = data.ReadValueS32(endian);
                    entry.UncompressedSize = data.ReadValueU32(endian);
                    entry.Flags            = data.ReadValueU32(endian);
                    this.Attributes.Add(entry);
                }
            }

            this.Endian  = endian;
            this.Version = version;
        }