Exemplo n.º 1
0
        private void backgroundWorkerJoin_DoWork(object sender, DoWorkEventArgs e)
        {
            string file = ((string)e.Argument);string directoryName = file.Substring(0, file.LastIndexOf('\\') + 1);
            string startingFile = file; saveDirectory = directoryName;
            long origSize = 0;

            byte[] bufferStorage; long bytesCopied = 0, timesToRun = 0, bytesRead = 0, singleFileSize = 0; FileStream fOpenSplit;
            string fileName = startingFile.Substring(startingFile.LastIndexOf('\\') + 1, startingFile.LastIndexOf('.') - startingFile.LastIndexOf('\\') - 1);
            FileMetaData fileData = FileUtilities.ReadBackMetaFile(saveDirectory, fileName); origSize = fileData.OrigFileSize;
            FileStream fOpenOrig = File.Create(saveDirectory + fileData.OriginalFileName); file="join";

            int i = 0; origSize = fileData.OrigFileSize;
            for (i = 0; i < fileData.NoOfSplitFiles; i++)
            {
                if (backgroundWorkerJoin.CancellationPending) { joinStatus = 1; goto checksum; }
                fOpenSplit = File.Open(saveDirectory + fileName + ".vip" + i.ToString(), FileMode.Open, FileAccess.Read);
                bytesRead = 0; singleFileSize = fOpenSplit.Length;
                if (fOpenSplit.Length > buffer)
                {
                    bufferStorage = new byte[buffer];
                    timesToRun = (Int64)(fOpenSplit.Length / buffer) + 1;
                    while (timesToRun > 1)
                    {
                        if (backgroundWorkerJoin.CancellationPending) { joinStatus = 1; break; }
                        bytesRead += fOpenSplit.Read(bufferStorage, 0, bufferStorage.Length);
                        fOpenOrig.Write(bufferStorage, 0, bufferStorage.Length);
                        bytesCopied += buffer;
                        backgroundWorkerJoin.ReportProgress((int)(((float)(bytesCopied) / (float)(origSize)) * 100),file);
                        timesToRun--;
                    }
                    if (joinStatus == 1) { fOpenSplit.Close(); goto checksum; }
                    bufferStorage = new byte[fOpenSplit.Length - bytesRead];
                    bytesRead += fOpenSplit.Read(bufferStorage, 0, bufferStorage.Length);
                    fOpenOrig.Write(bufferStorage, 0, bufferStorage.Length); bytesCopied += bufferStorage.Length;
                    backgroundWorkerJoin.ReportProgress((int)(((float)(bytesCopied) / (float)(origSize)) * 100),file);

                    fOpenSplit.Close();
                }
                else
                {
                    if (backgroundWorkerJoin.CancellationPending) { fOpenSplit.Close(); joinStatus = 1; goto checksum; }
                    bufferStorage = new byte[fOpenSplit.Length];
                    bytesRead += fOpenSplit.Read(bufferStorage, 0, bufferStorage.Length);
                    fOpenOrig.Write(bufferStorage, 0, bufferStorage.Length); bytesCopied += bufferStorage.Length;
                    backgroundWorkerJoin.ReportProgress((int)(((float)(bytesCopied) / (float)(origSize)) * 100),file);
                    fOpenSplit.Close();
                }
            }

            checksum:
            fOpenOrig.Close();

            string hash = "";
            if (fileData.FileCheckSum == "NC")
            { if(joinStatus != 1) joinStatus = 4; }
            else if (joinStatus != 1)
            {
                if (MessageBox.Show("Joining of Files Done and Original File Generated.\n\nDo you want to check the generated file for integrity?\n( Whether it has been correctly generated or not )", "File Integrity Check", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    file = "check";
                    float currentHashProg = 0; int check = (int)fileData.CheckSumType;
                    CRC32 crchash = new CRC32(); FileStream fOpen; byte[] buffer = new byte[8192];
                    HashAlgorithm crc32 = (HashAlgorithm)(crchash); long bytesHashed = 0;
                    Adler32 adler32 = new Adler32(); MurmurHash3 murmurhash3 = new MurmurHash3();

                    //************************************* Start Hashing *****************************************************

                    if (check == 2)
                    {
                        //------------------------------------ CRC32 Calculation For a file ---------------------------------------
                        crc32.Initialize();
                        using (fOpen = File.Open(saveDirectory + fileData.OriginalFileName, FileMode.Open, FileAccess.ReadWrite))
                        {
                            while (bytesHashed != fOpen.Length)
                            {
                                if (backgroundWorkerJoin.CancellationPending) { joinStatus = 2; break; }
                                if (fOpen.Length - bytesHashed < 8192)
                                { buffer = new byte[fOpen.Length - bytesHashed]; bytesHashed += fOpen.Read(buffer, 0, buffer.Length); }
                                else
                                    bytesHashed += fOpen.Read(buffer, 0, buffer.Length);

                                ((CRC32)crc32).CalcHashBuffer(buffer, 0, buffer.Length);

                                currentHashProg = ((float)bytesHashed / (float)fOpen.Length) * 100;
                                backgroundWorkerJoin.ReportProgress((int)currentHashProg, file);
                            }
                        }

                        if (joinStatus == 0)
                            hash = ((CRC32)crc32).CalcHashFinal();
                        else
                            hash = "";
                        //------------------------------------------------------------------------------------------------------
                    }
                    else if (check == 1)
                    {
                        //========================= Adler32 Calculation for a File =============================================

                        using (fOpen = File.Open(saveDirectory + fileData.OriginalFileName, FileMode.Open, FileAccess.ReadWrite))
                        {
                            adler32.m_unChecksumValue = Adler32.AdlerStart;
                            byte[] bytesBuff = new byte[Adler32.AdlerBuff];
                            while (bytesHashed != fOpen.Length)
                            {
                                if (backgroundWorkerJoin.CancellationPending) { joinStatus = 2; break; }

                                if (fOpen.Length - bytesHashed < 0x400)
                                    bytesBuff = new byte[fOpen.Length - bytesHashed];

                                if (!adler32.MakeForBuff(bytesBuff, adler32.m_unChecksumValue))
                                {
                                    adler32.m_unChecksumValue = 0;
                                }
                                else
                                {
                                    bytesHashed += bytesBuff.Length;
                                    currentHashProg = ((float)bytesHashed / (float)fOpen.Length) * 100;
                                    backgroundWorkerJoin.ReportProgress((int)currentHashProg);
                                }
                            }
                        }

                        if (joinStatus == 0) hash = BitConverter.ToString(BitConverter.GetBytes(adler32.ChecksumValue), 0).Replace("-", "");
                        else hash = "";

                        //======================================================================================================
                    }
                    else if (check == 3)
                    {
                        //+++++++++++++++++++++++++++ MurmurHash3 Calculation of a File ++++++++++++++++++++++++++++++++++++++++

                        buffer = new byte[4]; int prevProg = -1;
                        using (fOpen = File.Open(saveDirectory + fileData.OriginalFileName, FileMode.Open, FileAccess.ReadWrite))
                        {
                            while (bytesHashed != fOpen.Length)
                            {
                                if (backgroundWorkerJoin.CancellationPending) { joinStatus = 2; break; }
                                if (fOpen.Length - bytesHashed < 4)
                                { buffer = new byte[fOpen.Length - bytesHashed]; bytesHashed += fOpen.Read(buffer, 0, buffer.Length); }
                                else
                                    bytesHashed += fOpen.Read(buffer, 0, buffer.Length);

                                murmurhash3.CalcHashBuffer(buffer);
                                currentHashProg = ((float)bytesHashed / (float)fOpen.Length) * 100;

                                if (prevProg != (int)currentHashProg)
                                    backgroundWorkerJoin.ReportProgress((int)currentHashProg);

                                prevProg = (int)currentHashProg;
                            }
                        }
                        murmurhash3.Hash ^= murmurhash3.StreamLength;
                        murmurhash3.Hash = MurmurHash3.fmix(murmurhash3.Hash);

                        if (joinStatus == 0)
                            hash = murmurhash3.GetHashString();
                        else
                            hash = "";

                        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    }
                }
                else joinStatus = 2;

                if (hash != fileData.FileCheckSum && hash != null && hash != ""){ joinStatus = 3; }
            }
        }
Exemplo n.º 2
0
        private void backgroundWorkerHash_DoWork(object sender, DoWorkEventArgs e)
        {
            FileUtilities fSplit = ((FileUtilities)e.Argument); float currentHashProg=0; int success = 0;
            fSplit.CheckSumType = (CheckSumMethod)checkCode;
            CRC32 crchash = new CRC32(); FileStream fOpen; byte[] buffer = new byte[8192];
            HashAlgorithm crc32 = (HashAlgorithm)(crchash); long bytesHashed = 0;
            Adler32 adler32 = new Adler32(); MurmurHash3 murmurhash3 = new MurmurHash3();

            //************************************* Start Hashing *****************************************************

            if (checkCode == 2)
            {
                //------------------------------------ CRC32 Calculation For a file ---------------------------------------
                crc32.Initialize();
                using (fOpen = File.Open(fSplit.FilePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    while (bytesHashed != fOpen.Length)
                    {
                        if (backgroundWorkerHash.CancellationPending) { success = 1; break; }
                        if (fOpen.Length - bytesHashed < 8192)
                        { buffer = new byte[fOpen.Length - bytesHashed]; bytesHashed += fOpen.Read(buffer, 0, buffer.Length); }
                        else
                            bytesHashed += fOpen.Read(buffer, 0, buffer.Length);

                        ((CRC32)crc32).CalcHashBuffer(buffer, 0, buffer.Length);

                        currentHashProg = ((float)bytesHashed / (float)fOpen.Length) * 100;
                        backgroundWorkerHash.ReportProgress((int)currentHashProg);
                    }
                }

                if (success == 0)
                    fSplit.MD5Sum = ((CRC32)crc32).CalcHashFinal();
                else
                    fSplit.MD5Sum = "";
                //------------------------------------------------------------------------------------------------------
            }
            else if (checkCode == 1)
            {
                //========================= Adler32 Calculation for a File =============================================

                using (fOpen = File.Open(fSplit.FilePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    adler32.m_unChecksumValue = Adler32.AdlerStart;
                    byte[] bytesBuff = new byte[Adler32.AdlerBuff];
                    while (bytesHashed != fOpen.Length)
                    {
                        if (backgroundWorkerHash.CancellationPending) { success = 1; break; }

                        if (fOpen.Length - bytesHashed < 0x400)
                            bytesBuff = new byte[fOpen.Length - bytesHashed];

                        if (!adler32.MakeForBuff(bytesBuff, adler32.m_unChecksumValue))
                        {
                            adler32.m_unChecksumValue = 0;
                        }
                        else
                        {
                            bytesHashed += bytesBuff.Length;
                            currentHashProg = ((float)bytesHashed / (float)fOpen.Length) * 100;
                            backgroundWorkerHash.ReportProgress((int)currentHashProg);
                        }
                    }
                }

                if (success == 0) fSplit.MD5Sum = BitConverter.ToString(BitConverter.GetBytes(adler32.ChecksumValue), 0).Replace("-", "");
                else fSplit.MD5Sum = "";

                //======================================================================================================
            }
            else if (checkCode == 3)
            {
                //+++++++++++++++++++++++++++ MurmurHash3 Calculation of a File ++++++++++++++++++++++++++++++++++++++++

                buffer = new byte[4]; int prevProg = -1;
                using (fOpen = File.Open(fSplit.FilePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    while (bytesHashed != fOpen.Length)
                    {
                        if (backgroundWorkerHash.CancellationPending) { success = 1; break; }
                        if (fOpen.Length - bytesHashed < 4)
                        { buffer = new byte[fOpen.Length - bytesHashed]; bytesHashed += fOpen.Read(buffer, 0, buffer.Length); }
                        else
                            bytesHashed += fOpen.Read(buffer, 0, buffer.Length);

                        murmurhash3.CalcHashBuffer(buffer);
                        currentHashProg = ((float)bytesHashed / (float)fOpen.Length) * 100;

                        if (prevProg != (int)currentHashProg)
                            backgroundWorkerHash.ReportProgress((int)currentHashProg);

                        prevProg = (int)currentHashProg;
                    }
                }
                murmurhash3.Hash ^= murmurhash3.StreamLength;
                murmurhash3.Hash = MurmurHash3.fmix(murmurhash3.Hash);

                if (success == 0)
                    fSplit.MD5Sum = murmurhash3.GetHashString();
                else
                    fSplit.MD5Sum = "";

                //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            }
            else fSplit.MD5Sum = "";
        }