public void DeepScan() { const int bufferSize = 4096 * 128; byte[] buffer = new byte[bufferSize]; for (int index = 0; index < _localFiles.Count; index++) { if (_localFiles[index].isDirectory || _localFiles[index].UncompressedSize == 0) { _localFiles[index].md5 = new byte[] { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e }; _localFiles[index].sha1 = new byte[] { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90, 0xaf, 0xd8, 0x07, 0x09 }; _localFiles[index].FileStatus = ZipReturn.ZipGood; continue; } ulong sizetogo; Stream inStream; ZipReturn zr = ZipFileOpenReadStream(index, out inStream, out sizetogo); if (zr != ZipReturn.ZipGood) { continue; } if (inStream == null) { continue; } CRC crc32 = new CRC(); MD5 lmd5 = System.Security.Cryptography.MD5.Create(); SHA1 lsha1 = System.Security.Cryptography.SHA1.Create(); while (sizetogo > 0) { int sizenow = sizetogo > (ulong)bufferSize ? bufferSize : (int)sizetogo; inStream.Read(buffer, 0, sizenow); crc32.Update(buffer, 0, (uint)sizenow); lmd5.TransformBlock(buffer, 0, sizenow, null, 0); lsha1.TransformBlock(buffer, 0, sizenow, null, 0); sizetogo = sizetogo - (ulong)sizenow; } lmd5.TransformFinalBlock(buffer, 0, 0); lsha1.TransformFinalBlock(buffer, 0, 0); byte[] testcrc = Util.uinttobytes(crc32.GetDigest()); _localFiles[index].md5 = lmd5.Hash; _localFiles[index].sha1 = lsha1.Hash; _localFiles[index].FileStatus = Util.ByteArrCompare(_localFiles[index].crc, testcrc) ? ZipReturn.ZipGood : ZipReturn.ZipCRCDecodeError; } }
private bool IsRomVault7Z() { long length = _zipFs.Length; if (length < 32) { return(false); } _zipFs.Seek(length - 32, SeekOrigin.Begin); const string sig = "RomVault7Zip"; byte[] RV7Zid = Util.Enc.GetBytes(sig); byte[] header = new byte[12]; _zipFs.Read(header, 0, 12); for (int i = 0; i < 12; i++) { if (header[i] != RV7Zid[i]) { return(false); } } BinaryReader br = new BinaryReader(_zipFs); uint HeaderCRC = br.ReadUInt32(); ulong HeaderOffset = br.ReadUInt64(); ulong HeaderSize = br.ReadUInt64(); if ((ulong)length < HeaderOffset) { return(false); } _zipFs.Seek((long)HeaderOffset, SeekOrigin.Begin); byte[] mainHeader = new byte[HeaderSize]; int bytesread = _zipFs.Read(mainHeader, 0, (int)HeaderSize); return((ulong)bytesread == HeaderSize && CRC.VerifyDigest(HeaderCRC, mainHeader, 0, (uint)HeaderSize)); }
private void CloseWriting7Zip(ICodeProgress p = null) { if (_compressed) { _lzmaStream.Close(); } _packStreamSize = (UInt64)_zipFs.Position - _packStreamStart; Create7ZStructure(); byte[] newHeaderByte; using (Stream headerMem = new MemoryStream()) { using (BinaryWriter headerBw = new BinaryWriter(headerMem)) { _header.WriteHeader(headerBw); newHeaderByte = new byte[headerMem.Length]; headerMem.Position = 0; headerMem.Read(newHeaderByte, 0, newHeaderByte.Length); } } CRC mainHeadercrc = new CRC(); mainHeadercrc.Update(newHeaderByte, 0, (uint)newHeaderByte.Length); UInt32 mainHeaderCRC = mainHeadercrc.GetDigest(); UInt64 headerpos = (UInt64)_zipFs.Position; BinaryWriter bw = new BinaryWriter(_zipFs); bw.Write(newHeaderByte); _signatureHeader.WriteFinal(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC); WriteRomVault7Zip(bw, headerpos, (ulong)newHeaderByte.Length, mainHeaderCRC); _zipFs.Flush(); _zipFs.Close(); _zipFs.Dispose(); }
private bool Istorrent7Z() { const int crcsz = 128; const int t7ZsigSize = 16 + 1 + 9 + 4 + 4; byte[] kSignature = { (byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C }; int kSignatureSize = kSignature.Length; const string sig = "\xa9\x9f\xd1\x57\x08\xa9\xd7\xea\x29\x64\xb2\x36\x1b\x83\x52\x33\x01torrent7z_0.9beta"; byte[] t7Zid = Util.Enc.GetBytes(sig); int t7ZidSize = t7Zid.Length; const int tmpbufsize = 256 + t7ZsigSize + 8 + 4; byte[] buffer = new byte[tmpbufsize]; // read fist 128 bytes, pad with zeros if less bytes int bufferPos = 0; _zipFs.Seek(0, SeekOrigin.Begin); int ar = _zipFs.Read(buffer, bufferPos, crcsz); if (ar < crcsz) { Util.memset(buffer, bufferPos + ar, 0, crcsz - ar); } bufferPos = crcsz; long foffs = _zipFs.Length; int endReadLength = crcsz + t7ZsigSize + 4; foffs = foffs < endReadLength ? 0 : foffs - endReadLength; _zipFs.Seek(foffs, SeekOrigin.Begin); ar = _zipFs.Read(buffer, bufferPos, endReadLength); if (ar < endReadLength) { if (ar >= t7ZsigSize + 4) { ar -= t7ZsigSize + 4; } if (ar < kSignatureSize) { ar = kSignatureSize; } Util.memset(buffer, bufferPos + ar, 0, crcsz - ar); Util.memcpyr(buffer, crcsz * 2 + 8, buffer, bufferPos + ar, t7ZsigSize + 4); } else { Util.memcpyr(buffer, crcsz * 2 + 8, buffer, crcsz * 2, t7ZsigSize + 4); } foffs = _zipFs.Length; foffs -= t7ZsigSize + 4; //memcpy(buffer, crcsz * 2, &foffs, 8); buffer[crcsz * 2 + 0] = (byte)((foffs >> 0) & 0xff); buffer[crcsz * 2 + 1] = (byte)((foffs >> 8) & 0xff); buffer[crcsz * 2 + 2] = (byte)((foffs >> 16) & 0xff); buffer[crcsz * 2 + 3] = (byte)((foffs >> 24) & 0xff); buffer[crcsz * 2 + 4] = 0; buffer[crcsz * 2 + 5] = 0; buffer[crcsz * 2 + 6] = 0; buffer[crcsz * 2 + 7] = 0; if (Util.memcmp(buffer, 0, kSignature, kSignatureSize)) { t7Zid[16] = buffer[crcsz * 2 + 4 + 8 + 16]; if (Util.memcmp(buffer, crcsz * 2 + 4 + 8, t7Zid, t7ZidSize)) { UInt32 inCrc32 = (UInt32)((buffer[crcsz * 2 + 8 + 0]) + (buffer[crcsz * 2 + 8 + 1] << 8) + (buffer[crcsz * 2 + 8 + 2] << 16) + (buffer[crcsz * 2 + 8 + 3] << 24)); buffer[crcsz * 2 + 8 + 0] = 0xff; buffer[crcsz * 2 + 8 + 1] = 0xff; buffer[crcsz * 2 + 8 + 2] = 0xff; buffer[crcsz * 2 + 8 + 3] = 0xff; uint calcCrc32 = CRC.CalculateDigest(buffer, 0, crcsz * 2 + 8 + t7ZsigSize + 4); if (inCrc32 == calcCrc32) { return(true); } } } return(false); }