private void GetFileNumber() { this.archiveStream.Seek(24L, SeekOrigin.Begin); byte[] numArray = new byte[4]; this.archiveStream.Read(numArray, 0, numArray.Length); this.totalNumberOfFiles = (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 0L); }
/// <summary> /// Set the alleged number of files in the archive /// </summary> void GetFileNumber() { archiveStream.Seek(24, SeekOrigin.Begin); byte[] buffer = new byte[4]; archiveStream.Read(buffer, 0, buffer.Length); totalNumberOfFiles = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 0); }
public MYPHandler(string filename, del_FileTableEventHandler eventHandler_FileTable, del_FileEventHandler eventHandler_Extraction, HashDictionary hashDic) { // Try to set ramCounter to the performance counter but if it fails set it to null to default getUsedRAM() to 0 try { ramCounter = new PerformanceCounter("Process", "Private Bytes", Process.GetCurrentProcess().ProcessName); } catch { ramCounter = null; } this.hashDictionary = hashDic; if (eventHandler_Extraction != null) { this.event_Extraction += eventHandler_Extraction; } if (eventHandler_FileTable != null) { this.event_FileTable += eventHandler_FileTable; } this.currentMypFileName = filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - filename.LastIndexOf('\\') - 1); this.currentMypFileName = this.currentMypFileName.Split('.')[0]; this.fullMypFileName = filename; this.mypPath = filename.LastIndexOf('\\') < 0 ? "" : filename.Substring(0, filename.LastIndexOf('\\')); this.pattern = "*"; this.unCompressedSize = 0L; this.numberOfFileNamesFound = 0L; this.totalNumberOfFiles = 0L; this.numberOfFilesFound = 0L; this.error_FileEntryNumber = 0L; this.error_ExtractionNumber = 0L; this.archiveStream = new FileStream(filename, FileMode.Open, FileAccess.Read); this.archiveStream.Seek(12L, SeekOrigin.Begin); byte[] numArray = new byte[8]; this.archiveStream.Read(numArray, 0, numArray.Length); this.tableStart = (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 0L); this.tableStart += (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 4L) << 32; this.GetFileNumber(); this.oSearcher = new ManagementObjectSearcher(this.oMs, this.oQuery); this.oReturnCollection = this.oSearcher.Get(); foreach (ManagementBaseObject oReturn in this.oReturnCollection) { this.totalMemory += Convert.ToDouble(oReturn["Capacity"]); } if (this.totalMemory > this.programMemory) { return; } this.programMemory = this.totalMemory / 2.0; }
/// <summary> /// Creates a new myp worker /// </summary> /// <param name="filename">the name of the file to work with</param> /// <param name="event_FileTable">method to treat events return when reading the file table</param> /// <param name="event_Extraction">method to treat events return when extracting files</param> /// <param name="hasher">the dictionnary</param> public MYPHandler(string filename , del_FileTableEventHandler eventHandler_FileTable , del_FileEventHandler eventHandler_Extraction , HashDictionary hashDic) { this.hashDictionary = hashDic; if (eventHandler_Extraction != null) { this.event_Extraction += eventHandler_Extraction; } if (eventHandler_FileTable != null) { this.event_FileTable += eventHandler_FileTable; } //parse the filename to get the path this.currentMypFileName = filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - filename.LastIndexOf('\\') - 1); this.currentMypFileName = currentMypFileName.Split('.')[0]; this.fullMypFileName = filename; if (filename.LastIndexOf('\\') >= 0) { this.mypPath = filename.Substring(0, filename.LastIndexOf('\\')); } else { this.mypPath = ""; } //Initialize some data pattern = "*"; unCompressedSize = 0; numberOfFileNamesFound = 0; totalNumberOfFiles = 0; numberOfFilesFound = 0; error_FileEntryNumber = 0; error_ExtractionNumber = 0; //open the archive file archiveStream = new FileStream(filename, FileMode.Open, FileAccess.Read); //read the position of the starting file table archiveStream.Seek(0x0C, SeekOrigin.Begin); byte[] buffer = new byte[8]; archiveStream.Read(buffer, 0, buffer.Length); tableStart = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 0); tableStart += ((long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 4)) << 32; GetFileNumber(); }
public FileInArchiveDescriptor(byte[] buffer) { this.startingPosition = (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 0L); this.startingPosition += (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 4L) << 32; this.fileHeaderSize = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 8L); this.compressedSize = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 12L); this.uncompressedSize = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 16L); Array.Copy((Array)buffer, 20, (Array)this.file_hash, 0, 8); this.sh = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 20L); this.ph = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 24L); this.crc = BitConverter.ToInt32(buffer, 28); this.filename += string.Format("{0:X8}", (object)this.crc); this.filename += "_"; this.filename += string.Format("{0:X16}", (object)BitConverter.ToInt64(this.file_hash, 0)); this.compressionMethod = buffer[32]; this.isCompressed = (int)this.compressionMethod != 0; }
/// <summary> /// Parse the source file and reads the file table entries /// Raises according event depending on result /// </summary> public void GetFileTable() { error_FileEntryNumber = 0; error_ExtractionNumber = 0; unCompressedSize = 0; numberOfFileNamesFound = 0; numberOfFilesFound = 0; //Init long currentReadingPosition; uint numberOfFileInTable = 0; long endOfTableAddress; byte[] bufferTableHeader = new byte[12]; byte[] bufferFileDesc = new byte[FileInArchiveDescriptor.fileDescriptorSize]; FileInArchive myArchFile; while (tableStart != 0) { archiveStream.Seek(tableStart, SeekOrigin.Begin); archiveStream.Read(bufferTableHeader, 0, bufferTableHeader.Length); numberOfFileInTable = FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 0); //get number of files currentReadingPosition = tableStart + 12; endOfTableAddress = tableStart + 12 + (long)FileInArchiveDescriptor.fileDescriptorSize * (long)numberOfFileInTable; // calculates the end address tableStart = FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 4); //find the next filetable tableStart += (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(bufferTableHeader, 8) << 32; //mostly 0 #region File Table Read while (currentReadingPosition < endOfTableAddress) { archiveStream.Seek(currentReadingPosition, SeekOrigin.Begin); archiveStream.Read(bufferFileDesc, 0, bufferFileDesc.Length); myArchFile = new FileInArchive(); myArchFile.descriptor = new FileInArchiveDescriptor(bufferFileDesc); myArchFile.descriptor.fileTableEntryPosition = currentReadingPosition; if (myArchFile.descriptor.startingPosition > 0 && myArchFile.descriptor.compressedSize > 0 && myArchFile.descriptor.uncompressedSize > 0 //If the compressed size is 0, then there is no file ) { //Search for the filename HashData ffn = null; if (hashDictionary != null) { ffn = hashDictionary.SearchHashList(myArchFile.descriptor.ph, myArchFile.descriptor.sh); if (ffn != null && ffn.filename != "") { myArchFile.descriptor.foundFileName = true; myArchFile.descriptor.filename = ffn.filename; if (myArchFile.descriptor.crc != ffn.crc) { myArchFile.State = FileInArchiveState.MODIFIED; hashDictionary.UpdateCRC(myArchFile.descriptor.ph, myArchFile.descriptor.sh, myArchFile.descriptor.crc); archiveModifiedFileList.Add(myArchFile); } numberOfFileNamesFound++; int dotpos = myArchFile.descriptor.filename.LastIndexOf('.'); myArchFile.descriptor.extension = myArchFile.descriptor.filename.Substring(dotpos + 1); } else { if (ffn == null) { myArchFile.State = FileInArchiveState.NEW; hashDictionary.AddHash(myArchFile.descriptor.ph, myArchFile.descriptor.sh, "", myArchFile.descriptor.crc); archiveNewFileList.Add(myArchFile); } else if (myArchFile.descriptor.crc != ffn.crc) { myArchFile.State = FileInArchiveState.MODIFIED; hashDictionary.UpdateCRC(myArchFile.descriptor.ph, myArchFile.descriptor.sh, myArchFile.descriptor.crc); archiveModifiedFileList.Add(myArchFile); } //Retrieve header myArchFile.metadata = new byte[myArchFile.descriptor.fileHeaderSize]; archiveStream.Seek(myArchFile.descriptor.startingPosition, SeekOrigin.Begin); archiveStream.Read(myArchFile.metadata, 0, myArchFile.metadata.Length); //Retrieve head of the data for extension definition purpose if (!myArchFile.descriptor.foundFileName) { if (myArchFile.descriptor.compressedSize < myArchFile.data_start_200.Length) { myArchFile.data_start_200 = new byte[myArchFile.descriptor.compressedSize]; } archiveStream.Seek(myArchFile.descriptor.startingPosition + myArchFile.descriptor.fileHeaderSize, SeekOrigin.Begin); archiveStream.Read(myArchFile.data_start_200, 0, myArchFile.data_start_200.Length); try { TreatHeader(myArchFile); } catch { Error_FileTableEntry(myArchFile); } } } //weird Wildcard match. //Updates the table try { numberOfFilesFound++; if (WildcardMatch(pattern, myArchFile.Filename)) { archiveFileList.Add(myArchFile); unCompressedSize += myArchFile.descriptor.uncompressedSize; /// \todo Speed: we should do something here to avoid passing an event for each and every file.... TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.NewFile, myArchFile)); } else { TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.UpdateFile, null)); } } catch { Error_FileTableEntry(myArchFile); } } } else { //FileTableEntryError(myArchFile); } currentReadingPosition += FileInArchiveDescriptor.fileDescriptorSize; } // currentReadingPosition < endOfTableAddress #endregion } //tableStart != 0 TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.Finished, null)); }
/// <summary> /// Creates a new myp worker /// </summary> /// <param name="filename">the name of the file to work with</param> /// <param name="event_FileTable">method to treat events return when reading the file table</param> /// <param name="event_Extraction">method to treat events return when extracting files</param> /// <param name="hasher">the dictionnary</param> public MYPHandler(string filename , del_FileTableEventHandler eventHandler_FileTable , del_FileEventHandler eventHandler_Extraction , HashDictionary hashDic) { this.hashDictionary = hashDic; if (eventHandler_Extraction != null) { this.event_Extraction += eventHandler_Extraction; } if (eventHandler_FileTable != null) { this.event_FileTable += eventHandler_FileTable; } //parse the filename to get the path this.currentMypFileName = filename.Substring(filename.LastIndexOf('\\') + 1, filename.Length - filename.LastIndexOf('\\') - 1); this.currentMypFileName = currentMypFileName.Split('.')[0]; this.fullMypFileName = filename; if (filename.LastIndexOf('\\') >= 0) { this.mypPath = filename.Substring(0, filename.LastIndexOf('\\')); } else { this.mypPath = ""; } //Initialize some data pattern = "*"; unCompressedSize = 0; numberOfFileNamesFound = 0; totalNumberOfFiles = 0; numberOfFilesFound = 0; error_FileEntryNumber = 0; error_ExtractionNumber = 0; //open the archive file archiveStream = new FileStream(filename, FileMode.Open, FileAccess.Read); //read the position of the starting file table archiveStream.Seek(0x0C, SeekOrigin.Begin); byte[] buffer = new byte[8]; archiveStream.Read(buffer, 0, buffer.Length); tableStart = FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 0); tableStart += ((long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(buffer, 4)) << 32; GetFileNumber(); //get the total memory available #region System Memory Management oSearcher = new ManagementObjectSearcher(oMs, oQuery); oReturnCollection = oSearcher.Get(); foreach (ManagementObject oReturn in oReturnCollection) { totalMemory += Convert.ToDouble(oReturn["Capacity"]); } if (totalMemory <= programMemory) { programMemory = totalMemory / 2; //Security } #endregion }
public void GetFileTable() { this.error_FileEntryNumber = 0L; this.error_ExtractionNumber = 0L; this.unCompressedSize = 0L; this.numberOfFileNamesFound = 0L; this.numberOfFilesFound = 0L; byte[] numArray = new byte[12]; byte[] buffer = new byte[FileInArchiveDescriptor.fileDescriptorSize]; while (this.tableStart != 0L) { this.archiveStream.Seek(this.tableStart, SeekOrigin.Begin); this.archiveStream.Read(numArray, 0, numArray.Length); uint num1 = FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 0L); long offset = this.tableStart + 12L; long num2 = this.tableStart + 12L + (long)FileInArchiveDescriptor.fileDescriptorSize * (long)num1; this.tableStart = (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 4L); this.tableStart += (long)FileInArchiveDescriptor.convertLittleEndianBufferToInt(numArray, 8L) << 32; while (offset < num2) { this.archiveStream.Seek(offset, SeekOrigin.Begin); this.archiveStream.Read(buffer, 0, buffer.Length); FileInArchive archFile = new FileInArchive(); archFile.descriptor = new FileInArchiveDescriptor(buffer); archFile.sourceFileName = this.fullMypFileName; archFile.descriptor.fileTableEntryPosition = offset; if (archFile.descriptor.startingPosition > 0L && archFile.descriptor.compressedSize > 0U && archFile.descriptor.uncompressedSize > 0U) { if (this.hashDictionary != null) { HashData hashData = this.hashDictionary.SearchHashList(archFile.descriptor.ph, archFile.descriptor.sh); if (hashData != null && (string)hashData.filename != "") { archFile.descriptor.foundFileName = true; archFile.descriptor.filename = (string)hashData.filename; if (archFile.descriptor.crc != hashData.crc) { archFile.State = FileInArchiveState.MODIFIED; this.hashDictionary.UpdateCRC(archFile.descriptor.ph, archFile.descriptor.sh, archFile.descriptor.crc); this.archiveModifiedFileList.Add(archFile); } ++this.numberOfFileNamesFound; int num3 = archFile.descriptor.filename.LastIndexOf('.'); archFile.descriptor.extension = archFile.descriptor.filename.Substring(num3 + 1); } else { if (hashData == null) { archFile.State = FileInArchiveState.NEW; this.hashDictionary.AddHash(archFile.descriptor.ph, archFile.descriptor.sh, "", archFile.descriptor.crc); this.archiveNewFileList.Add(archFile); } else if (archFile.descriptor.crc != hashData.crc) { archFile.State = FileInArchiveState.MODIFIED; this.hashDictionary.UpdateCRC(archFile.descriptor.ph, archFile.descriptor.sh, archFile.descriptor.crc); this.archiveModifiedFileList.Add(archFile); } archFile.metadata = new byte[archFile.descriptor.fileHeaderSize]; this.archiveStream.Seek(archFile.descriptor.startingPosition, SeekOrigin.Begin); this.archiveStream.Read(archFile.metadata, 0, archFile.metadata.Length); if (!archFile.descriptor.foundFileName) { if ((long)archFile.descriptor.compressedSize < (long)archFile.data_start_200.Length) { archFile.data_start_200 = new byte[archFile.descriptor.compressedSize]; } this.archiveStream.Seek(archFile.descriptor.startingPosition + (long)archFile.descriptor.fileHeaderSize, SeekOrigin.Begin); this.archiveStream.Read(archFile.data_start_200, 0, archFile.data_start_200.Length); try { this.TreatHeader(archFile); } catch (Exception ex) { this.Error_FileTableEntry(archFile); } } } try { ++this.numberOfFilesFound; if (this.WildcardMatch(this.pattern, archFile.Filename)) { this.archiveFileList.Add(archFile); this.unCompressedSize += (long)archFile.descriptor.uncompressedSize; this.TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.NewFile, archFile)); } else { this.TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.UpdateFile, (FileInArchive)null)); } } catch (Exception ex) { this.Error_FileTableEntry(archFile); } } } offset += (long)FileInArchiveDescriptor.fileDescriptorSize; } } this.TriggerFileTableEvent(new MYPFileTableEventArgs(Event_FileTableType.Finished, (FileInArchive)null)); }