Exemplo n.º 1
0
            private void LoadFile(BinaryReader inputFileStream, bool boolInflate = true, bool UnpackWhenNeed = true)
            {
                long prevPosition = inputFileStream.BaseStream.Position;
                inputFileStream.BaseStream.Position = 0;

                bool useTempFiles = false;
                if(OperationMode == Mode.FileSystem)                
                    useTempFiles = true;
                else if (OperationMode == Mode.Optimal)
                {
                    // В оптимальном режиме, если обрабатываемый файл больше 200 МБ,
                    // то автоматически включается режим использования файловой системы
                    if (inputFileStream.BaseStream.Length > MAX_FILE_SIZE)
                        OperationMode = Mode.FileSystem;
                }

                MemoryTributary InflateBufferStream;
                UInt32 InflateSize = 0;

                this.FileHeader = new stFileHeader(inputFileStream.ReadBytes((int)stFileHeader.Size()), 0);

                stBlockHeader pBlockHeader = new stBlockHeader(inputFileStream.ReadBytes((int)stBlockHeader.Size()), 0);

                UInt32 ElemsAddrsSize;
                MemoryTributary pElemsAddrsStream;
                ReadBlockData(inputFileStream, pBlockHeader, stFileHeader.Size(), out pElemsAddrsStream, out ElemsAddrsSize);

                UInt32 ElemsNum = ElemsAddrsSize / stElemAddr.Size();

                Elems.Clear();
                ElemsAddrs.Clear();

                for (UInt32 i = 0; i < ElemsNum; i++)
                {

                    stElemAddr pElemsAddrs = new stElemAddr(pElemsAddrsStream, (int)(i * stElemAddr.Size()));
                    ElemsAddrs.Add(pElemsAddrs);

                    if (pElemsAddrs.fffffff != V8_FF_SIGNATURE)
                    {
                        ElemsNum = i;
                        break;
                    }

                    inputFileStream.BaseStream.Position = pElemsAddrs.elem_header_addr;
                    pBlockHeader = new stBlockHeader(inputFileStream.ReadBytes((int)stBlockHeader.Size()), 0);

                    if (pBlockHeader.EOL_0D != 0x0d ||
                        pBlockHeader.EOL_0A != 0x0a ||
                        pBlockHeader.space1 != 0x20 ||
                        pBlockHeader.space2 != 0x20 ||
                        pBlockHeader.space3 != 0x20 ||
                        pBlockHeader.EOL2_0D != 0x0d ||
                        pBlockHeader.EOL2_0A != 0x0a)
                    {
                        throw new Exception("Header is not correct!");
                    }

                    UInt32 ElemsAddrsSizeHeader = 0;
                    MemoryTributary pHeaderStream;
                    UInt32 DataSize = 0;
                    MemoryTributary pDataStream;
                    ReadBlockData(inputFileStream, pBlockHeader, pElemsAddrs.elem_header_addr, out pHeaderStream, out ElemsAddrsSizeHeader);

                    //080228 Блока данных может не быть, тогда адрес блока данных равен 0x7fffffff
                    if (pElemsAddrs.elem_data_addr != V8_FF_SIGNATURE)
                    {
                        inputFileStream.BaseStream.Position = pElemsAddrs.elem_data_addr;
                        pBlockHeader = new stBlockHeader(inputFileStream.ReadBytes((int)stBlockHeader.Size()), 0);
                        ReadBlockData(inputFileStream, pBlockHeader, pElemsAddrs.elem_data_addr, out pDataStream, out DataSize);
                    }
                    else
                    {
                        throw new Exception("Incorrect data block!");
                    }

                    CV8Elem elem = new CV8Elem(pHeaderStream, ElemsAddrsSizeHeader, pDataStream, (UInt32)pDataStream.Length, this, false, false, useTempFiles);

                    if (boolInflate && IsDataPacked)
                    {
                        bool success = Inflate(elem.GetDataLikeMemStream(), out InflateBufferStream);

                        if (!success)
                        {
                            IsDataPacked = false;
                            elem.SetDataFromMemStream(InflateBufferStream);
                            elem.DataSize = (UInt32)InflateBufferStream.Length;
                            elem.IsV8File = false;
                        }
                        else
                        {
                            elem.NeedUnpack = false; // отложенная распаковка не нужна
                            elem.pData = null; //нераспакованные данные больше не нужны
                            if (IsV8File(InflateBufferStream))
                            {
                                elem.UnpackedData = new V8File(this, InflateBufferStream, (int)InflateSize, boolInflate, this.OperationMode);
                                elem.pData = null;
                                elem.IsV8File = true;
                            }
                            else
                            {
                                elem.SetDataFromMemStream(InflateBufferStream);
                                elem.DataSize = InflateSize;
                                elem.IsV8File = false;
                            }
                        }
                    }

                    elem.InitElemName(inputFileStream, pElemsAddrs);
                    Elems.Add(elem);

                }

                inputFileStream.BaseStream.Position = prevPosition;
            }
Exemplo n.º 2
0
            int LoadFile(byte[] pFileData, int FileDataSize, bool boolInflate = true, bool UnpackWhenNeed = true)
            {
                int ret = 0;

                if (pFileData.Length == 0) {
                    return V8UNPACK_ERROR;
                }

                bool isV8File = IsV8File(pFileData, FileDataSize);
                if (!isV8File)
                {
                    return V8UNPACK_NOT_V8_FILE;
                }

                byte[] InflateBuffer = new byte[0];
                UInt32 InflateSize = 0;

                this.FileHeader = new stFileHeader(pFileData, 0);

                stBlockHeader pBlockHeader = new stBlockHeader(pFileData, stFileHeader.Size());

                UInt32 ElemsAddrsSize;                
                byte[] pElemsAddrsBytes;
                ReadBlockData(pFileData, pBlockHeader, stFileHeader.Size(), out pElemsAddrsBytes, out ElemsAddrsSize);

                UInt32 ElemsNum = ElemsAddrsSize / stElemAddr.Size();

                Elems.Clear();
                ElemsAddrs.Clear();
                                
                for (UInt32 i = 0; i < ElemsNum; i++) {

                    stElemAddr pElemsAddrs = new stElemAddr(pElemsAddrsBytes, (int)(i * stElemAddr.Size()));
                    ElemsAddrs.Add(pElemsAddrs);

                    if (pElemsAddrs.fffffff != V8_FF_SIGNATURE)
                    {
                        ElemsNum = i;
                        break;
                    }

                    pBlockHeader = new stBlockHeader(pFileData, pElemsAddrs.elem_header_addr);

                    if (pBlockHeader.EOL_0D != 0x0d ||
                            pBlockHeader.EOL_0A != 0x0a ||
                            pBlockHeader.space1 != 0x20 ||
                            pBlockHeader.space2 != 0x20 ||
                            pBlockHeader.space3 != 0x20 ||
                            pBlockHeader.EOL2_0D != 0x0d ||
                            pBlockHeader.EOL2_0A != 0x0a)
                    {

                        ret = V8UNPACK_HEADER_ELEM_NOT_CORRECT;
                        break;
                    }

                    UInt32 ElemsAddrsSizeHeader = 0;
                    byte[] pElemsAddrsBytesHeader = new byte[0];
                    UInt32 DataSize = 0;
                    byte[] pData = new byte[0];
                    ReadBlockData(pFileData, pBlockHeader, pElemsAddrs.elem_header_addr, out pElemsAddrsBytesHeader, out ElemsAddrsSizeHeader);                    

                    //080228 Блока данных может не быть, тогда адрес блока данных равен 0x7fffffff
                    if (pElemsAddrs.elem_data_addr != V8_FF_SIGNATURE)
                    {
                        pBlockHeader = new stBlockHeader(pFileData, pElemsAddrs.elem_data_addr);
                        ReadBlockData(pFileData, pBlockHeader, pElemsAddrs.elem_data_addr, out pData, out DataSize);
                    }
                    else
                    {
                        throw new Exception("Ебать копать!!!");
                        //ReadBlockData(pFileData, null, out pData, out DataSize);
                    }

                    CV8Elem elem = new CV8Elem(pElemsAddrsBytesHeader, ElemsAddrsSizeHeader, pData, (UInt32)pData.Length, new V8Formats.V8File(), false, false);

                    if (boolInflate && IsDataPacked) {
                        ret = Inflate(pData, out InflateBuffer, DataSize, out InflateSize);

                        if (ret != 0)
                        {
                            IsDataPacked = false;
                            elem.pData = new byte[InflateSize];
                            elem.DataSize = InflateSize;
                            InflateBuffer.CopyTo(elem.pData, 0);
                            elem.IsV8File = false;
                            }
                        else {
                            elem.NeedUnpack = false; // отложенная распаковка не нужна
                            elem.pData = null; //нераспакованные данные больше не нужны
                            if (IsV8File(InflateBuffer, (int)InflateSize)) {
                                elem.UnpackedData = new V8File(InflateBuffer, (int)InflateSize, boolInflate);
                                elem.pData = null;
                                elem.IsV8File = true;
                            } else {
                                elem.pData = new byte[InflateSize];
                                elem.DataSize = InflateSize;
                                InflateBuffer.CopyTo(elem.pData, 0);
                                elem.IsV8File = false;
                            }
                            ret = 0;
                        }
                    }

                    elem.InitElemName(pFileData, pElemsAddrs);
                    Elems.Add(elem);

                }


                if (InflateBuffer.Length != 0)
                    InflateBuffer = new byte[0];

                return ret;
            }