コード例 #1
0
ファイル: Par2Library.cs プロジェクト: jhdscript/Par2NET
        private void UpdateOrAddRecoverySet(Par2RecoverySet recoverySet)
        {
            lock (syncObject)
            {
                string setid = ToolKit.ToHex(recoverySet.MainPacket.header.setid);

                if (!setids.Keys.Contains(setid))
                {
                    setids.Add(setid, recoverySet);
                }
                else
                {
                    UpdateRecoverySet(setids[setid], recoverySet);
                }
            }
        }
コード例 #2
0
ファイル: FileChecker.cs プロジェクト: jhdscript/Par2NET
        private static void CheckBuffer(byte[] buffer, DiskFile diskFile, string filename, int blocksize, Dictionary <uint, FileVerificationEntry> hashfull, ref MatchType matchType, int globalOffset)
        {
            uint partial_key = (uint)(Path.GetFileName(filename).GetHashCode());

            MD5 md5Hasher = MD5.Create();

            //FastCRC32.FastCRC32 crc32 = new FastCRC32.FastCRC32((ulong)blocksize);
            FastCRC32.FastCRC32 crc32 = FastCRC32.FastCRC32.GetCRC32Instance((ulong)blocksize);

            int offset = 0;

            byte inch  = 0;
            byte outch = 0;

            uint crc32Value = 0;

            crc32Value = crc32.CRCUpdateBlock(0xFFFFFFFF, (uint)blocksize, buffer, (uint)offset) ^ 0xFFFFFFFF;

            while (offset < (buffer.Length - blocksize))
            {
                uint key = crc32Value ^ partial_key;

                FileVerificationEntry entry = null;

                if (hashfull.ContainsKey(key))
                {
                    entry = hashfull[key];

                    byte[] blockhash = md5Hasher.ComputeHash(buffer, offset, blocksize);

                    if (ToolKit.ToHex(blockhash) == ToolKit.ToHex(entry.hash))
                    {
                        // We found a complete match, so go to next block !

                        //Console.WriteLine("block found at offset {0}, crc {1}", globalOffset + offset, entry.crc);
                        if (entry.datablock.diskfile == null)
                        {
                            lock (entry)
                            {
                                if (entry.datablock.diskfile == null)
                                {
                                    entry.SetBlock(diskFile, (int)(globalOffset + offset));
                                }
                            }
                        }

                        offset += blocksize;

                        crc32Value = crc32.CRCUpdateBlock(0xFFFFFFFF, (uint)(Math.Min(blocksize, buffer.Length - offset)), buffer, (uint)offset) ^ 0xFFFFFFFF;
                    }
                    else
                    {
                        if (offset + blocksize > buffer.Length)
                        {
                            return;
                        }

                        matchType = MatchType.PartialMatch;

                        inch  = buffer[offset + blocksize];
                        outch = buffer[offset];

                        crc32Value = crc32.windowMask ^ crc32.CRCSlideChar(crc32.windowMask ^ crc32Value, inch, outch);

                        ++offset;
                    }
                }
                else
                {
                    if (offset + blocksize > buffer.Length)
                    {
                        return;
                    }

                    matchType = MatchType.PartialMatch;

                    inch  = buffer[offset + blocksize];
                    outch = buffer[offset];

                    crc32Value = crc32.windowMask ^ crc32.CRCSlideChar(crc32.windowMask ^ crc32Value, inch, outch);

                    ++offset;
                }
            }
        }