예제 #1
0
        public void TestUInt32Type()
        {
            FileStream file;
            UInt32Type data;

            /*
             * Verify size is 4 bytes
             */
            data = new UInt32Type(0);
            Assert.AreEqual(4, data.Size);

            /*
             * 0x0 offset
             */
            file = new FileStream(TestHelper.DataTypeTestsFile, FileMode.Open);
            data = new UInt32Type(0x0);
            data.Read(file, 0);
            Assert.AreEqual((UInt32)0x05060708, data.Value);
            file.Close();

            /*
             * Negative offset
             */
            file = new FileStream(TestHelper.DataTypeTestsFile, FileMode.Open);
            data = new UInt32Type(-8);
            data.Read(file, 8);
            Assert.AreEqual((UInt32)0x05060708, data.Value);
            file.Close();

            /*
             * Positive offset
             */
            file = new FileStream(TestHelper.DataTypeTestsFile, FileMode.Open);
            data = new UInt32Type(8);
            data.Read(file, 0);
            Assert.AreEqual((UInt32)0x15161718, data.Value);
            file.Close();

            /* Test ToString() */
            Assert.AreEqual("15161718", data.ToString());
        }
예제 #2
0
        /// <summary>
        /// Loads a NeFS item with the specified id from the archive.
        /// </summary>
        /// <param name="file">The file stream to load from.</param>
        /// <param name="archive">The NeFS archive this item is in.</param>
        /// <param name="id">The id of the item in the archive to load.</param>
        public NefsItem(FileStream file, NefsArchive archive, UInt32 id)
        {
            /* Validate inputs */
            if (file == null)
            {
                throw new ArgumentNullException("File stream required to load NeFS item.");
            }

            if (archive == null)
            {
                throw new ArgumentNullException("NeFS archive object required to load an item.");
            }

            _archive = archive;
            _id      = id;

            /* Get header entries related to this item */
            _pt1Entry = archive.Header.Part1.GetEntry(id);
            _pt2Entry = archive.Header.Part2.GetEntry(_pt1Entry);
            _pt5Entry = archive.Header.Part5.GetEntry(id);
            try
            {
                _pt6Entry = archive.Header.Part6.GetEntry(id);
            }
            catch (Exception ex)
            {
                try
                {
                    /* Some items share a part 2 entry, so try to find the corresponding part 6 entry */
                    _pt6Entry = archive.Header.Part6.GetEntry(_pt2Entry.Id);
                }
                catch (Exception ex2)
                {
                    // TODO : Handle when an item doesn't have a part 6 entry
                    _pt6Entry = archive.Header.Part6.GetEntry(0);
                }
            }

            /* Determine item type */
            _type = (_pt1Entry.OffsetToData == 0)
                ? NefsItemType.Directory
                : NefsItemType.File;

            /* Get the filename */
            _fileName = archive.Header.Part3.GetFilename(_pt2Entry.FilenameOffset);

            /* Hash the filename */
            _fileNameHash = FilePathHelper.HashStringMD5(_fileName);

            /* Get offsets */
            _dataOffset       = _pt1Entry.OffsetToData;
            _offsetIntoPt2Raw = _pt1Entry.OffsetIntoPt2Raw;
            _offsetIntoPt4Raw = _pt1Entry.OffsetIntoPt4Raw;

            /* Get extracted size */
            _extractedSize = _pt2Entry.ExtractedSize;

            /*
             * Build the file path inside this archive
             * for example: "rootDir/childDir/file.xml".
             */
            _filePathInArchive = _fileName;
            var currentItem = _pt2Entry;

            /* The root directory's id is equal to its parent directory id */
            while (currentItem.Id != currentItem.DirectoryId)
            {
                var pt1Entry = archive.Header.Part1.GetEntry(currentItem.DirectoryId);
                var dir      = archive.Header.Part2.GetEntry(pt1Entry);
                var dirName  = archive.Header.Part3.GetFilename(dir.FilenameOffset);
                _filePathInArchive = Path.Combine(dirName, _filePathInArchive);

                currentItem = dir;
            }

            /* Hash the file path in archive */
            _filePathInArchiveHash = FilePathHelper.HashStringMD5(_filePathInArchive);

            //
            // Get the compressed file chunk offsets
            //
            if (_pt1Entry.OffsetIntoPt4Raw == 0xFFFFFFFF)
            {
                // TODO : Not sure exactly what this value means yet
                // For now, just set compressed size as extracted size with not compressed chunk sizes
                _compressedSize = ExtractedSize;
            }
            else
            {
                var numChunks = (UInt32)Math.Ceiling(ExtractedSize / (double)CHUNK_SIZE);
                if (numChunks > 0)
                {
                    var        firstChunkSizeEntry = _archive.Header.Part4.Offset + _pt1Entry.OffsetIntoPt4;
                    UInt32Type chunkOffset;

                    for (int i = 0; i < numChunks; i++)
                    {
                        chunkOffset = new UInt32Type(i * 4);
                        chunkOffset.Read(file, firstChunkSizeEntry);
                        _chunkSizes.Add(chunkOffset.Value);
                    }

                    _compressedSize = _chunkSizes.Last();
                }
            }
        }