Exemplo n.º 1
0
        private void _ParseDataType(byte[] buffer)
        {
            int offset = 0;

            // header
            FileHeader fileHeader = FileTools.ByteArrayToStructure <FileHeader>(buffer, ref offset);

            if (fileHeader.FileToken != Token.Head)
            {
                throw new Exceptions.UnexpectedTokenException("Expected Head token but got 0x" + fileHeader.FileToken.ToString("X8"));
            }
            if (fileHeader.FileVersion != RequiredVersion)
            {
                throw new Exceptions.NotSupportedFileVersionException(RequiredVersion, fileHeader.FileVersion);
            }

            // Read the strings section
            StringsHeader stringsHeader = FileTools.ByteArrayToStructure <StringsHeader>(buffer, ref offset);

            if (stringsHeader.StringsToken != Token.Sect)
            {
                throw new Exceptions.UnexpectedTokenException(Token.Sect, stringsHeader.StringsToken);
            }
            List <String> strings = new List <String>();

            for (int i = 0; i < stringsHeader.StringsCount; i++)
            {
                strings.Add(FileTools.ByteArrayToStringASCII(buffer, offset));
                offset += strings[i].Length + 1; // +1 for \0
            }

            // String Details
            UInt32 stringsDetailsToken = FileTools.ByteArrayToUInt32(buffer, ref offset);

            if (stringsDetailsToken != Token.Sect)
            {
                throw new Exceptions.UnexpectedTokenException(Token.Sect, stringsDetailsToken);
            }
            // Skip over the details struct because we don't need it.
            offset += stringsHeader.StringsCount * Marshal.SizeOf(typeof(StringDetailsStruct));

            // Files Structure details
            UInt32 filesToken = FileTools.ByteArrayToUInt32(buffer, ref offset);

            if (filesToken != Token.Sect)
            {
                throw new Exceptions.UnexpectedTokenException(Token.Sect, stringsDetailsToken);
            }
            FileDetails.AddRange(FileTools.ByteArrayToArray <FileEntryStruct>(buffer, ref offset, fileHeader.FileCount));

            // The Files list is the public interface
            for (int i = 0; i < fileHeader.FileCount; i++)
            {
                PackFileEntry fileEntry = new FileEntry(FileDetails[i])
                {
                    Pack = this,
                    Path = System.IO.Path.Combine(strings[FileDetails[i].DirectoryIndex], strings[FileDetails[i].NameIndex]),
                };

                Files.Add(fileEntry);
            }

            HasIntegrity = true;
        }