Пример #1
0
        public bool FindExactMatch(RvFile file)
        {
            if (!Equal(file.Size, Size))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.CRC, CRC))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.SHA1, SHA1))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.MD5, MD5))
            {
                return(false);
            }

            // should check header file type also.

            if (!Equal(file.AltSize, AltSize))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.AltCRC, AltCRC))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.AltSHA1, AltSHA1))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.AltMD5, AltMD5))
            {
                return(false);
            }

            return(true);
        }
Пример #2
0
        private bool FindAltExactMatch(RvFile file)
        {
            // should check header file type also.

            if (!Equal(file.AltSize, Size))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.AltCRC, CRC))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.AltSHA1, SHA1))
            {
                return(false);
            }
            if (!ArrByte.ECompare(file.AltMD5, MD5))
            {
                return(false);
            }

            return(true);
        }
Пример #3
0
        private byte[] ReceivedData(string ip, byte[] buffer)
        {
            FileShareUiComm fs;

            if (buffer == null)
            {
                return(null);
            }
            _thWrk.Report($"Received {ip} data -> {BitConverter.ToString(buffer)}");

            if (buffer.Length < 2)
            {
                // return an error command reply
                return(new byte[] { 0, 0 });
            }

            int bOffset = 0;
            int command = (buffer[bOffset++] << 8) + buffer[bOffset++];

            byte[] name    = buffer.Copy(bOffset, 10);  bOffset += 10;
            byte[] session = buffer.Copy(bOffset, 16); bOffset += 16;

            switch (command)
            {
            //  00 -  2 Command 1 (0,1)
            //  02 - 10 Name
            //  12 - 16 Session Guid
            case 1:
                //  00 -  2 Command 1 (0,1)
                //  02 -  1 01

                fs = new FileShareUiComm
                {
                    command  = 1,
                    username = name,
                    session  = session,
                    message  = "Hello"
                };
                _thWrk?.Report(fs);

                return(new byte[] { 0, 1, 1 });



            //  00 -  2 Command 2 (0, 2)
            //  02 - 10 Name
            //  12 - 16 Session Guid
            //  28 -  8 Size

            case 2:
                //  00 - 2 Command 2 (0, 2)
                //  02 - 1 File Not Found = 0

                //  00 - 2 Command 2 (0, 2)
                //  02 - 1 File Found = 1
                //   3 - 8 File Compressed Size
                //  11 - 4 File CRC
            {
                RvFile testFile = new RvFile(FileType.ZipFile);
                testFile.Size = BitConverter.ToUInt64(buffer, bOffset); bOffset += 8;
                comBits cb = (comBits)buffer[bOffset++];
                if ((cb & comBits.cCRC) != 0)
                {
                    testFile.CRC = buffer.Copy(bOffset, 4); bOffset += 4;
                }
                if ((cb & comBits.cSHA1) != 0)
                {
                    testFile.SHA1 = buffer.Copy(bOffset, 20); bOffset += 20;
                }
                if ((cb & comBits.cMD5) != 0)
                {
                    testFile.MD5 = buffer.Copy(bOffset, 16); bOffset += 16;
                }

                bool found = FindFixes.FindMatch(fileGroupsSHA1Sorted, testFile, CompareSHA1, FileGroup.FindExactMatch, out List <int> listIndex);

                if (!found)
                {
                    fs = new FileShareUiComm
                    {
                        command  = 2,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1 not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 2, 0 });
                }

                FileGroup fgMatch = fileGroupsSHA1Sorted[listIndex[0]];
                if (testFile.Size != fgMatch.Size)
                {
                    fs = new FileShareUiComm
                    {
                        command  = 2,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1,Size not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 2, 0 });
                }
                if (!ArrByte.ECompare(testFile.CRC, fgMatch.CRC))
                {
                    fs = new FileShareUiComm
                    {
                        command  = 2,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1,CRC not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 2, 0 });
                }
                if (!ArrByte.ECompare(testFile.MD5, fgMatch.MD5))
                {
                    fs = new FileShareUiComm
                    {
                        command  = 2,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1,MD5 not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 2, 0 });
                }

                foreach (RvFile fMatch in fgMatch.Files)
                {
                    Zip zFile = new Zip();
                    zFile.ZipFileOpen(fMatch.Parent.FullNameCase, fMatch.Parent.FileModTimeStamp, false);
                    zFile.ZipFileOpenReadStreamQuick((ulong)fMatch.ZipFileHeaderPosition, true, out Stream stream, out ulong streamSize, out ushort compressionMethod);

                    if (compressionMethod != 8)
                    {
                        zFile.ZipFileCloseReadStream();
                        zFile.ZipFileClose();
                        continue;
                    }

                    List <byte> ret = new List <byte>();
                    ret.AddRange(new byte[] { 0, 2, 1 });
                    ret.AddRange(BitConverter.GetBytes((ulong)streamSize));
                    ret.AddRange(fMatch.CRC);

                    fs = new FileShareUiComm
                    {
                        command        = 2,
                        username       = name,
                        session        = session,
                        sha1           = testFile.SHA1,
                        message        = "Found File",
                        filename       = fMatch.Parent.FullName,
                        zippedFilename = fMatch.Name,
                        compressedSize = streamSize
                    };
                    _thWrk?.Report(fs);

                    zFile.ZipFileCloseReadStream();
                    zFile.ZipFileClose();

                    return(ret.ToArray());
                }

                fs = new FileShareUiComm
                {
                    command  = 2,
                    username = name,
                    session  = session,
                    sha1     = testFile.SHA1,
                    message  = "No Zipped version found."
                };
                _thWrk?.Report(fs);

                return(new byte[] { 0, 2, 0 });
            }

            //  00 -  2 Command 3 (0, 3)
            //  02 - 10 Name
            //  12 - 16 Session Guid
            //  28 - 20 SHA1
            //  48 -  8 Size
            //  56 -  8 long file offset block
            //  64 -  4 block length to read

            case 3:
                //  00 - 2 Command 2 (0, 3)
                //  02 - 1 File Not Found = 0

                //  00 - 2 Command 2 (0, 3)
                //  02 - 1 File Found = 1
                //  03 - 4: Block read length
                //  07 - X: Data
            {
                RvFile testFile = new RvFile(FileType.ZipFile);
                testFile.Size = BitConverter.ToUInt64(buffer, bOffset); bOffset += 8;
                comBits cb = (comBits)buffer[bOffset++];
                if ((cb & comBits.cCRC) != 0)
                {
                    testFile.CRC = buffer.Copy(bOffset, 4); bOffset += 4;
                }
                if ((cb & comBits.cSHA1) != 0)
                {
                    testFile.SHA1 = buffer.Copy(bOffset, 20); bOffset += 20;
                }
                if ((cb & comBits.cMD5) != 0)
                {
                    testFile.MD5 = buffer.Copy(bOffset, 16); bOffset += 16;
                }

                ulong offset = BitConverter.ToUInt64(buffer, bOffset); bOffset += 8;
                uint  length = BitConverter.ToUInt32(buffer, bOffset); bOffset += 4;

                bool found = FindFixes.FindMatch(fileGroupsSHA1Sorted, testFile, CompareSHA1, FileGroup.FindExactMatch, out List <int> listIndex);

                if (!found)
                {
                    fs = new FileShareUiComm
                    {
                        command  = 3,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1 not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 3, 0 });
                }

                FileGroup fgMatch = fileGroupsSHA1Sorted[listIndex[0]];
                if (testFile.Size != fgMatch.Size)
                {
                    fs = new FileShareUiComm
                    {
                        command  = 3,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1,Size not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 3, 0 });
                }
                if (!ArrByte.ECompare(testFile.CRC, fgMatch.CRC))
                {
                    fs = new FileShareUiComm
                    {
                        command  = 3,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1,CRC not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 3, 0 });
                }
                if (!ArrByte.ECompare(testFile.MD5, fgMatch.MD5))
                {
                    fs = new FileShareUiComm
                    {
                        command  = 3,
                        username = name,
                        session  = session,
                        sha1     = testFile.SHA1,
                        message  = "SHA1,MD5 not found"
                    };
                    _thWrk?.Report(fs);
                    return(new byte[] { 0, 3, 0 });
                }

                foreach (RvFile fMatch in fgMatch.Files)
                {
                    Zip zFile = new Zip();
                    zFile.ZipFileOpen(fMatch.Parent.FullNameCase, fMatch.Parent.FileModTimeStamp, false);
                    zFile.ZipFileOpenReadStreamQuick((ulong)fMatch.ZipFileHeaderPosition, true, out Stream stream, out ulong streamSize, out ushort compressionMethod);

                    if (compressionMethod != 8)
                    {
                        zFile.ZipFileCloseReadStream();
                        zFile.ZipFileClose();
                        continue;
                    }

                    List <byte> ret = new List <byte>();
                    ret.AddRange(new byte[] { 0, 3, 1 });

                    stream.Seek((long)offset, SeekOrigin.Current);
                    byte[] data       = new byte[length];
                    int    readLength = stream.Read(data, 0, (int)length);
                    ret.AddRange(BitConverter.GetBytes(readLength));
                    ret.AddRange(data);

                    fs = new FileShareUiComm
                    {
                        command  = 2,
                        username = name,
                        session  = session,
                        filename = fMatch.Name,
                        sha1     = testFile.SHA1,

                        message = "Sending",

                        zippedFilename = fMatch.Parent.FullName,
                        compressedSize = streamSize,
                        offset         = offset,
                        blockLength    = length
                    };

                    _thWrk?.Report(fs);
                    zFile.ZipFileCloseReadStream();
                    zFile.ZipFileClose();
                    return(ret.ToArray());
                }

                return(new byte[] { 0, 2, 0 });
            }

            default:
                return(new byte[] { 0, 0 });
            }
        }
Пример #4
0
        public bool FindSHA1(searchFiles f)
        {
            f.found = false;

            FileShareUiComm fs = new FileShareUiComm
            {
                username    = name,
                session     = session,
                filename    = f.name,
                size        = f.Size,
                crc         = f.CRC,
                sha1        = f.SHA1,
                md5         = f.MD5,
                offset      = 0,
                blockLength = 0
            };

            fs.command = 2;

            comBits cb = 0;

            if (f.CRC != null)
            {
                cb |= comBits.cCRC;
            }
            if (f.SHA1 != null)
            {
                cb |= comBits.cSHA1;
            }
            if (f.MD5 != null)
            {
                cb |= comBits.cMD5;
            }


            List <byte> list = new List <byte>();

            list.Add(0); list.Add(2);
            list.AddRange(name);
            list.AddRange(session);
            list.AddRange(BitConverter.GetBytes(f.Size));
            list.Add((byte)cb);
            if (f.CRC != null)
            {
                list.AddRange(f.CRC);
            }
            if (f.SHA1 != null)
            {
                list.AddRange(f.SHA1);
            }
            if (f.MD5 != null)
            {
                list.AddRange(f.MD5);
            }



            NetClient.SendToServer(list.ToArray(), ip, port, out byte[] bReply);

            if (bReply == null)
            {
                fs.message = "Server Lost";
                _thWrk?.Report(fs);
                return(false);
            }

            if (bReply[0] != 0 || bReply[1] != 2)
            {
                //                textBox2.Text = "Incorrect Command reply";
                return(false);
            }

            if (bReply[2] != 1)
            {
                fs.message = "Not Found";
                _thWrk?.Report(fs);
                return(false);
            }

            f.compressedSize = BitConverter.ToUInt64(bReply, 3);
            byte[] fCRC = bReply.Copy(11, 4);

            if (!ArrByte.ECompare(f.CRC, fCRC))
            {
                return(false);
            }

            if (f.CRC == null)
            {
                f.CRC = fCRC;
            }

            f.found = true;
            return(true);
        }