コード例 #1
0
        public void fetch(searchFiles f)
        {
            FileShareUiComm fs = new FileShareUiComm
            {
                username       = name,
                session        = session,
                filename       = f.name,
                size           = f.Size,
                crc            = f.CRC,
                sha1           = f.SHA1,
                md5            = f.MD5,
                compressedSize = f.compressedSize,
                offset         = 0,
                blockLength    = 0
            };

            fs.command = 3;

            comBits cb = 0;

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


            ulong fileSize = f.compressedSize;

            // now try and get the file
            string filenameTmp  = "ToSort\\__FCDownload.tmp";
            string filename     = "ToSort\\" + BitConverter.ToString(f.SHA1).Replace("-", "") + ".zip";
            string filenameBin  = BitConverter.ToString(f.SHA1).Replace("-", "") + ".bin";
            uint   blockMaxSize = 4096 * 4096;

            Zip zFile = new Zip();

            zFile.ZipFileCreate(filenameTmp);
            zFile.ZipFileOpenWriteStream(true, false, filenameBin, f.Size, 8, out Stream stream);

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

            byte[] bReply = null;

            ulong offset = 0;

            while (fileSize > 0)
            {
                uint partNow = fileSize > blockMaxSize ? blockMaxSize : (uint)fileSize;
                Debug.WriteLine($"Index {offset} : Block {partNow} : Total {f.compressedSize} / {f.Size}");

                fs.command     = 3;
                fs.offset      = offset;
                fs.blockLength = (ulong)partNow;

                int  FetchCount = 0;
                bool res        = false;
                while (!res && FetchCount < 4)
                {
                    list.Clear();
                    list.Add(0); list.Add(3);
                    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);
                    }
                    list.AddRange(BitConverter.GetBytes(offset));
                    list.AddRange(BitConverter.GetBytes(partNow));

                    fs.message = "Receiving";
                    _thWrk?.Report(fs);

                    res = NetClient.SendToServer(list.ToArray(), ip, port, out bReply);
                    FetchCount++;
                }
                if (FetchCount >= 4)
                {
                    fs.message = "Server Lost";
                    _thWrk?.Report(fs);
                    zFile.ZipFileCloseFailed();
                    zFile.ZipFileClose();
                    File.Delete(filenameTmp);
                    return;
                }

                if (bReply[0] != 0 || bReply[1] != 3)
                {
                    fs.message = "Error Back command";
                    _thWrk?.Report(fs);
                    zFile.ZipFileCloseFailed();
                    zFile.ZipFileClose();
                    File.Delete(filenameTmp);
                    return;
                }
                if (bReply[2] != 1)
                {
                    fs.message = "Find now not found";
                    _thWrk?.Report(fs);
                    zFile.ZipFileCloseFailed();
                    zFile.ZipFileClose();
                    File.Delete(filenameTmp);
                    return;
                }
                int partSize = BitConverter.ToInt32(bReply, 3);
                if (partSize != partNow)
                {
                    fs.message = "Receive Size Error";

                    _thWrk?.Report(fs);
                    zFile.ZipFileCloseFailed();
                    zFile.ZipFileClose();
                    File.Delete(filenameTmp);
                    return;
                }

                stream.Write(bReply, 7, partSize);

                offset   += partNow;
                fileSize -= partNow;
            }
            zFile.ZipFileCloseWriteStream(f.CRC);
            zFile.ZipFileClose();
            File.Move(filenameTmp, filename);
        }
コード例 #2
0
ファイル: FileServer.cs プロジェクト: artifexor/RVWorld
        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 });
            }
        }
コード例 #3
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);
        }