Beispiel #1
0
        /// <summary>
        /// Test POP3 connection possibility
        /// </summary>
        /// <returns>Test result message</returns>
        public string Pop3Client_Test(int NumOfTries)
        {
            string ReturnMessage = "";

            while (NumOfTries > 0)
            {
                using (CancellationTokenSource cancel = new CancellationTokenSource())
                {
                    try
                    {
                        int Msg = -1;
                        using (Pop3Client Pop3Client__ = Pop3Client_(cancel))
                        {
                            Msg = Pop3Client__.Count;
                            Pop3Client__.Disconnect(true, cancel.Token);
                        }
                        ReturnMessage = "OK (" + Msg + " messages)";
                        NumOfTries    = 0;
                    }
                    catch (Exception e)
                    {
                        ReturnMessage = "Error: " + MailSegment.ExcMsg(e);
                    }
                }
                NumOfTries--;
            }
            return(ReturnMessage);
        }
Beispiel #2
0
        /// <summary>
        /// Test SMTP connection possibility
        /// </summary>
        /// <returns>Test result message</returns>
        public string SmtpClient_Test(int NumOfTries)
        {
            string ReturnMessage = "";

            while (NumOfTries > 0)
            {
                using (CancellationTokenSource cancel = new CancellationTokenSource())
                {
                    try
                    {
                        using (SmtpClient SmtpClient__ = SmtpClient_(cancel))
                        {
                            SmtpClient__.Disconnect(true, cancel.Token);
                        }
                        ReturnMessage = "OK";
                        NumOfTries    = 0;
                    }
                    catch (Exception e)
                    {
                        ReturnMessage = "Error: " + MailSegment.ExcMsg(e);
                    }
                }
                NumOfTries--;
            }
            return(ReturnMessage);
        }
Beispiel #3
0
        public string Init(int CalcBits_, string HexPadL, string HexPadT)
        {
            PadL        = new byte[HexPadL.Length / 2];
            PadL_Length = PadL.Length;
            for (int i = 0; i < PadL.Length; i++)
            {
                PadL[i] = (byte)MailSegment.HexToInt(HexPadL.Substring(i * 2, 2));
            }
            PadT        = new byte[HexPadT.Length / 2];
            PadT_Length = PadT.Length;
            for (int i = 0; i < PadT.Length; i++)
            {
                PadT[i] = (byte)MailSegment.HexToInt(HexPadT.Substring(i * 2, 2));
            }
            PadTOffset = PadL_Length + DigestLength;

            CacheVals.Clear();
            DigestState = new byte[PadL_Length + PadT_Length];
            for (int i = 0; i < PadL.Length; i++)
            {
                DigestState[i] = PadL[i];
            }
            for (int i = 0; i < PadT_Length; i++)
            {
                DigestState[i + PadL_Length] = PadT[i];
            }
            CacheVals.Add(DigestState);
            DigestStateLength = PadL_Length + DigestLength + PadT_Length;
            return("");
        }
Beispiel #4
0
        byte[] GenValSeq()
        {
            if ((CachePos & CacheStepV) == 0)
            {
                if (CacheVals.Count == (CachePos >> CacheStepB))
                {
                    CacheItem = new byte[DigestStateLength];
                    for (int i = 0; i < DigestStateLength; i++)
                    {
                        CacheItem[i] = DigestState[i];
                    }
                    CacheVals.Add(CacheItem);
                }
            }

            byte[] Seq = MailSegment.DigestBin_(DigestState);
            DigestState = new byte[DigestStateLength];
            for (int i = 0; i < PadL_Length; i++)
            {
                DigestState[i] = PadL[i];
            }
            for (int i = 0; i < DigestLength; i++)
            {
                DigestState[i + PadL_Length] = Seq[i];
            }
            for (int i = 0; i < PadT_Length; i++)
            {
                DigestState[i + PadTOffset] = PadT[i];
            }
            CachePos++;
            return(Seq);
        }
Beispiel #5
0
        /// <summary>
        /// Create new POP3 connection
        /// </summary>
        /// <param name="cancel"></param>
        /// <returns></returns>
        public Pop3Client Pop3Client_(CancellationTokenSource cancel)
        {
            MailSegment.AllowCert();
            Pop3Client Pop3Client__ = new Pop3Client();

            Pop3Client__.Connect(Pop3Host, Pop3Port, Pop3Ssl, cancel.Token);
            Pop3Client__.Authenticate(Login, Password, cancel.Token);
            return(Pop3Client__);
        }
Beispiel #6
0
        /// <summary>
        /// Create new SMTP connection
        /// </summary>
        /// <param name="cancel"></param>
        /// <returns></returns>
        public SmtpClient SmtpClient_(CancellationTokenSource cancel)
        {
            MailSegment.AllowCert();
            SmtpClient SmtpClient__ = new SmtpClient();

            SmtpClient__.Connect(SmtpHost, SmtpPort, SmtpSsl, cancel.Token);

            // Note: since we don't have an OAuth2 token, disable
            // the XOAUTH2 authentication mechanism.
            SmtpClient__.AuthenticationMechanisms.Remove("XOAUTH2");

            SmtpClient__.Authenticate(Login, Password);
            return(SmtpClient__);
        }
Beispiel #7
0
        /// <summary>
        /// Create new IMAP connection
        /// </summary>
        /// <param name="cancel"></param>
        /// <param name="InboxReadWrite"></param>
        /// <returns></returns>
        public ImapClient ImapClient_(CancellationTokenSource cancel, bool InboxReadWrite)
        {
            MailSegment.AllowCert();
            ImapClient ImapClient__ = new ImapClient();

            ImapClient__.Connect(ImapHost, ImapPort, ImapSsl, cancel.Token);

            // If you want to disable an authentication mechanism,
            // you can do so by removing the mechanism like this:
            ImapClient__.AuthenticationMechanisms.Remove("XOAUTH");

            ImapClient__.Authenticate(Login, Password, cancel.Token);
            ImapClient__.Inbox.Open(InboxReadWrite ? FolderAccess.ReadWrite : FolderAccess.ReadOnly, cancel.Token);
            return(ImapClient__);
        }
Beispiel #8
0
        /// <summary>
        /// Calculate number of segments based on data file length and segment size
        /// </summary>
        /// <returns></returns>
        public int CalcSegmentCount()
        {
            if (SegmentSizeL == 0)
            {
                SegmentCount = 0;
                return(0);
            }
            Monitor.Enter(DataF_);
            long FileSize;

            if (IsDummyFile)
            {
                FileSize = DummyFileSize;
            }
            else
            {
                FileStream DataS = DataOpen();
                if (ParamDigestMode)
                {
                    byte[] Buf = new byte[16];
                    DataS.Read(Buf, 0, 16);
                    FileSize = long.Parse(MailSegment.BinToStr(Buf), NumberStyles.HexNumber);
                }
                else
                {
                    FileSize = DataS.Length;
                }
                DataS.Close();
            }
            FileSize__   = FileSize;
            SegmentCount = (int)(FileSize / SegmentSizeL);
            if ((FileSize % SegmentSizeL) > 0)
            {
                SegmentCount++;
            }
            Monitor.Exit(DataF_);
            return(SegmentCount);
        }
        /// <summary>
        /// Create or check the digest file
        /// </summary>
        /// <param name="DigestMode">Digest mode</param>
        /// <param name="DataFile_">Data file name</param>
        /// <param name="MapFile_">Map file name</param>
        /// <param name="DigestFile_">Digest file name</param>
        /// <param name="SegmentSize_">Segment size</param>
        public void Proc(int DigestMode, string DataFile_, string MapFile_, string DigestFile_, int SegmentSize_, bool PromptConfirm)
        {
            long DigestFileSize    = 0;
            int  DigestSegmentSize = 0;

            int SegmentSize = SegmentSize_;

            DigestSegmentSize = SegmentSize;

            MailFile MF = new MailFile();

            if (MF.Open(false, true, DataFile_, MapFile_))
            {
                MF.SetSegmentSize(SegmentSize);
                MF.CalcSegmentCount();

                long FileSize = MF.GetDataSize();
                DigestFileSize = FileSize;
                int SegmentCount__ = MF.GetSegmentCount();
                MailSegment.ConsoleLineToLog    = true;
                MailSegment.ConsoleLineToLogSum = true;
                MailSegment.Console_WriteLine("");
                MailSegment.Console_WriteLine("Data file size: " + FileSize);
                MailSegment.Console_WriteLine("Data segment count: " + SegmentCount__);
                MailSegment.ConsoleLineToLogSum = false;
                MailSegment.ConsoleLineToLog    = false;
                MailSegment.Console_WriteLine("");

                if (DigestMode != 0)
                {
                    MailFile MD = new MailFile();
                    if (MD.Open(true, DigestMode != 0, DigestFile_, null))
                    {
                        MD.SetSegmentSize(SegmentSize);
                        if (DigestFile_ != null)
                        {
                            MD.CalcSegmentCount();
                            DigestSegmentSize = MD.DigestSegmentSize;
                            DigestFileSize    = MD.DigestFileSize;
                        }
                        else
                        {
                            DigestSegmentSize = 0;
                            DigestFileSize    = 0;
                        }
                        MailSegment.Console_WriteLine("Data file size from digest file: " + DigestFileSize);
                        MailSegment.Console_WriteLine("Segment size from digest file: " + DigestSegmentSize);
                        MailSegment.Console_WriteLine("");
                        MD.Close();
                    }
                    else
                    {
                        MailSegment.Console_WriteLine("Digest file open error");
                    }
                }

                if (Program.PromptConfirm(PromptConfirm))
                {
                    if ((DigestMode == 1) && (DigestMode == 3))
                    {
                        MF.MapChange(1, 0);
                        MF.MapChange(2, 0);
                    }

                    if (DigestMode == 0)
                    {
                        try
                        {
                            if (File.Exists(DigestFile_))
                            {
                                File.Delete(DigestFile_);
                            }
                        }
                        catch
                        {
                            MailSegment.ConsoleLineToLog    = true;
                            MailSegment.ConsoleLineToLogSum = true;
                            MailSegment.Console_WriteLine("Digest file creation error");
                            MailSegment.ConsoleLineToLogSum = false;
                            MailSegment.ConsoleLineToLog    = false;
                            return;
                        }
                    }

                    MailFile MD = new MailFile();
                    if (MD.Open(true, DigestMode != 0, DigestFile_, null))
                    {
                        Stopwatch_ TSW = new Stopwatch_();
                        MailSegment.Log();
                        MailSegment.LogReset();
                        MailSegment.Log("Time stamp", "Processed segments since previous entry", "Totally processed segments", "All segments", "Processed bytes since previous entry", "Totally processed bytes", "All bytes");

                        MD.SetSegmentSize(SegmentSize);
                        if (DigestMode != 0)
                        {
                            if (DigestFile_ != null)
                            {
                                MD.CalcSegmentCount();
                                DigestSegmentSize = MD.DigestSegmentSize;
                                DigestFileSize    = MD.DigestFileSize;
                            }
                            else
                            {
                                DigestSegmentSize = 0;
                                DigestFileSize    = 0;
                            }
                        }
                        if ((DigestMode == 2) || (DigestMode == 3))
                        {
                            if (DigestSegmentSize == SegmentSize)
                            {
                                if (FileSize != DigestFileSize)
                                {
                                    MailSegment.Console_WriteLine("Data file size correction started");
                                    MF.ResizeData(DigestFileSize);
                                    MF.CalcSegmentCount();
                                    FileSize = MF.GetDataSize();
                                    MailSegment.Console_WriteLine("Data file size correction finished");
                                    MailSegment.Console_WriteLine("");
                                }
                                else
                                {
                                    MailSegment.Console_WriteLine("Data file size is correct");
                                    MailSegment.Console_WriteLine("");
                                }
                            }
                        }
                        if ((DigestSegmentSize == SegmentSize) && (FileSize == DigestFileSize))
                        {
                            if (DigestMode != 2)
                            {
                                Stopwatch_ SWProgress = new Stopwatch_();
                                long       SWWorkTime = 0;
                                SWProgress.Reset();

                                int DigestG = 0;
                                int DigestB = 0;

                                long ToLogSize         = 0;
                                bool PrintLastProgress = true;

                                for (int i = 0; i < SegmentCount__; i++)
                                {
                                    ToLogSize += MF.DataGetSize(i);
                                    if (DigestMode == 0)
                                    {
                                        if (DigestFile_ != null)
                                        {
                                            byte[] Temp = MF.DataGet(i);
                                            MD.DataSet(i, Temp, Temp.Length);
                                        }
                                        MF.MapSet(i, 1);
                                        DigestG++;
                                    }
                                    else
                                    {
                                        if ((DigestFile_ != null) && (MailSegment.BinToStr(MD.DataGetDigest(i)) == MailSegment.BinToStr(MF.DataGetDigest(i))))
                                        {
                                            MF.MapSet(i, 1);
                                            DigestG++;
                                        }
                                        else
                                        {
                                            MF.MapSet(i, 0);
                                            DigestB++;
                                        }
                                    }

                                    if (SWWorkTime < SWProgress.Elapsed())
                                    {
                                        while (SWWorkTime < SWProgress.Elapsed())
                                        {
                                            SWWorkTime += 1000L;
                                        }
                                        MailSegment.Console_WriteLine("Segment " + (i + 1) + "/" + SegmentCount__ + " (" + ((i + 1) * 100 / SegmentCount__) + "%)");
                                        MailSegment.Log(TSW.Elapsed().ToString(), MailSegment.LogDiffS(i + 1).ToString(), (i + 1).ToString(), SegmentCount__.ToString(), MailSegment.LogDiffB(ToLogSize).ToString(), ToLogSize.ToString(), FileSize.ToString());
                                        if ((i + 1) == SegmentCount__)
                                        {
                                            PrintLastProgress = false;
                                        }
                                    }
                                }
                                MF.ResizeMap();

                                if (PrintLastProgress)
                                {
                                    MailSegment.Console_WriteLine("Segment " + SegmentCount__ + "/" + SegmentCount__ + " (100%)");
                                    MailSegment.Log(TSW.Elapsed().ToString(), MailSegment.LogDiffS(SegmentCount__).ToString(), SegmentCount__.ToString(), SegmentCount__.ToString(), MailSegment.LogDiffB(ToLogSize).ToString(), ToLogSize.ToString(), FileSize.ToString());
                                }

                                MailSegment.ConsoleLineToLog    = true;
                                MailSegment.ConsoleLineToLogSum = true;
                                MailSegment.Console_WriteLine("");
                                MailSegment.Console_WriteLine("Total segments: " + (DigestG + DigestB));
                                if (DigestMode != 0)
                                {
                                    MailSegment.Console_WriteLine("Good segments: " + DigestG);
                                    MailSegment.Console_WriteLine("Bad segments: " + DigestB);
                                }
                                MailSegment.Console_WriteLine("Total time: " + MailSegment.TimeHMSM(TSW.Elapsed()));
                                MailSegment.ConsoleLineToLogSum = false;
                                MailSegment.ConsoleLineToLog    = false;
                            }
                            else
                            {
                                MailSegment.ConsoleLineToLog    = true;
                                MailSegment.ConsoleLineToLogSum = true;
                                MailSegment.Console_WriteLine("Data file contents are not checked");
                                MailSegment.Console_WriteLine("Total time: " + MailSegment.TimeHMSM(TSW.Elapsed()));
                                MailSegment.ConsoleLineToLogSum = false;
                                MailSegment.ConsoleLineToLog    = false;
                            }
                        }
                        else
                        {
                            MailSegment.ConsoleLineToLog    = true;
                            MailSegment.ConsoleLineToLogSum = true;
                            if (FileSize != DigestFileSize)
                            {
                                MailSegment.Console_WriteLine("Data file size mismatch");
                            }
                            if (DigestSegmentSize != SegmentSize)
                            {
                                MailSegment.Console_WriteLine("Segment size mismatch");
                            }
                            MailSegment.Console_WriteLine("Total time: " + MailSegment.TimeHMSM(TSW.Elapsed()));
                            MailSegment.ConsoleLineToLogSum = false;
                            MailSegment.ConsoleLineToLog    = false;
                        }

                        MD.Close();
                    }
                    else
                    {
                        MailSegment.ConsoleLineToLog    = true;
                        MailSegment.ConsoleLineToLogSum = true;
                        if (DigestMode == 0)
                        {
                            MailSegment.Console_WriteLine("Digest file creation error");
                        }
                        else
                        {
                            MailSegment.Console_WriteLine("Digest file open error");
                        }
                        MailSegment.ConsoleLineToLogSum = false;
                        MailSegment.ConsoleLineToLog    = false;
                    }
                }

                MF.Close();
            }
            else
            {
                MailSegment.ConsoleLineToLog    = true;
                MailSegment.ConsoleLineToLogSum = true;
                MailSegment.Console_WriteLine("Data file open error");
                MailSegment.ConsoleLineToLogSum = false;
                MailSegment.ConsoleLineToLog    = false;
            }
        }
        public void CreateFile(string FileNameSrc, string FileNameDst, int SegmentSize, int CreateStats, int CreatePeriod)
        {
            string FileNameSta = FileNameDst;

            MailFile FileSrc = new MailFile();
            MailFile FileDst = new MailFile();

            if (FileSrc.Open(false, true, FileNameSrc, null))
            {
                Stopwatch_ TSW = new Stopwatch_();

                FileSrc.SetSegmentSize(SegmentSize);
                FileSrc.CalcSegmentCount();
                long FileSize    = FileSrc.GetDataSize();
                bool DstFileGood = false;
                if (FileNameDst != null)
                {
                    if (FileDst.Open(false, false, FileNameDst, null))
                    {
                        DstFileGood = true;
                    }
                    else
                    {
                        FileNameSta = FileNameSrc;
                        MailSegment.ConsoleLineToLog    = true;
                        MailSegment.ConsoleLineToLogSum = true;
                        MailSegment.Console_WriteLine("");
                        MailSegment.Console_WriteLine("Destination file create error: " + FileDst.OpenError);
                        MailSegment.ConsoleLineToLogSum = false;
                        MailSegment.ConsoleLineToLog    = false;
                    }
                }


                if (DstFileGood || (CreateStats > 0))
                {
                    ulong[] Stats = new ulong[256];
                    for (int i = 0; i < 256; i++)
                    {
                        Stats[i] = 0;
                    }

                    MailSegment.Console_WriteLine("");
                    Stopwatch_ SW = new Stopwatch_();
                    MailSegment.Log();
                    MailSegment.LogReset();
                    MailSegment.Log("Time stamp", "Processed segments since previous entry", "Totally processed segments", "All segments", "Processed bytes since previous entry", "Totally processed bytes", "All bytes");

                    long SWWorkTime = 0;
                    if (DstFileGood)
                    {
                        FileDst.SetSegmentSize(SegmentSize);
                        FileDst.CalcSegmentCount();
                    }
                    long ToLogSize = 0;


                    int  SegmentCount__    = FileSrc.GetSegmentCount();
                    bool PrintLastProgress = true;
                    for (int i = 0; i < SegmentCount__; i++)
                    {
                        byte[] Temp = FileSrc.DataGet(i);
                        if (DstFileGood)
                        {
                            FileDst.DataSet(i, Temp, Temp.Length);
                        }
                        for (int ii = Temp.Length - 1; ii >= 0; ii--)
                        {
                            Stats[Temp[ii]]++;
                        }
                        ToLogSize += Temp.LongLength;

                        if (SWWorkTime < SW.Elapsed())
                        {
                            while (SWWorkTime < SW.Elapsed())
                            {
                                SWWorkTime += 1000L;
                            }
                            MailSegment.Console_WriteLine("Segment " + (i + 1) + "/" + SegmentCount__ + " (" + ((i + 1) * 100 / SegmentCount__) + "%)");
                            MailSegment.Log(SW.Elapsed().ToString(), MailSegment.LogDiffS(i + 1).ToString(), (i + 1).ToString(), SegmentCount__.ToString(), MailSegment.LogDiffB(ToLogSize).ToString(), ToLogSize.ToString(), FileSize.ToString());
                            if ((i + 1) == SegmentCount__)
                            {
                                PrintLastProgress = false;
                            }
                        }
                    }
                    if (PrintLastProgress)
                    {
                        MailSegment.Console_WriteLine("Segment " + SegmentCount__ + "/" + SegmentCount__ + " (100%)");
                        MailSegment.Log(SW.Elapsed().ToString(), MailSegment.LogDiffS(SegmentCount__).ToString(), SegmentCount__.ToString(), SegmentCount__.ToString(), MailSegment.LogDiffB(ToLogSize).ToString(), ToLogSize.ToString(), FileSize.ToString());
                    }

                    if (DstFileGood)
                    {
                        FileDst.Close();
                    }

                    MailSegment.ConsoleLineToLog    = true;
                    MailSegment.ConsoleLineToLogSum = true;
                    MailSegment.Console_WriteLine("");
                    MailSegment.Console_WriteLine("File created in time: " + MailSegment.TimeHMSM(SW.Elapsed()));
                    if (CreateStats > 0)
                    {
                        MailSegment.Console_WriteLine("");
                        DisplayStats("File distribution", Stats, CreateStats);
                    }
                    MailSegment.ConsoleLineToLogSum = false;
                    MailSegment.ConsoleLineToLog    = false;
                }

                FileSrc.Close();

                if (CreatePeriod > 0)
                {
                    MailSegment.Console_WriteLine("");
                    MailSegment.Console_WriteLine("Searching for sequence period");

                    MailFile FileSta = new MailFile();
                    if (FileSta.Open(false, true, FileNameSta, null))
                    {
                        FileSta.DataValueFileOpen();

                        long   PeriodBufferSize = SegmentSize;
                        byte[] PeriodArray0     = new byte[PeriodBufferSize];
                        byte[] PeriodArray1     = new byte[PeriodBufferSize];

                        if (PeriodBufferSize > FileSize)
                        {
                            PeriodBufferSize = (int)FileSize;
                        }

                        long PeriodSize = 0;
                        int  PeriodChunks;
                        int  PeriodChunkOffset;
                        long PeriodChunkSize;
                        long PeriodChunkSize0;
                        long PeriodFilePos;

                        Stopwatch_ SW__ = new Stopwatch_();
                        SW__.Reset();

                        ulong[] PeriodStats = new ulong[256];
                        for (int i = 0; i < 256; i++)
                        {
                            PeriodStats[i] = 0;
                        }

                        long WorkTime = 0;

                        MailSegment.Log();
                        MailSegment.LogReset();
                        MailSegment.Log("Time stamp", "Current period length", "File length", "Current period occurence", "All period occurences");

                        for (long i = 1; i < FileSize; i++)
                        {
                            FileSta.DataGetBytes(i - 1, 1, PeriodArray0);
                            PeriodStats[PeriodArray0[0]]++;

                            bool IsPeriodical    = true;
                            bool PeriodicalPrint = true;

                            PeriodChunkSize  = PeriodBufferSize;
                            PeriodChunkSize0 = i;
                            PeriodChunks     = (int)(i / PeriodBufferSize);
                            if ((i % PeriodBufferSize) > 0)
                            {
                                PeriodChunks++;
                            }

                            long PeriodCount = (FileSize / i);
                            if ((FileSize % i) > 0)
                            {
                                PeriodCount++;
                            }

                            PeriodChunkOffset = 0;
                            for (int ii = 0; ii < PeriodChunks; ii++)
                            {
                                PeriodFilePos = PeriodChunkOffset;

                                if (PeriodChunkSize > PeriodChunkSize0)
                                {
                                    PeriodChunkSize = PeriodChunkSize0;
                                }

                                // Read the first period occurence and treat as pattern
                                FileSta.DataGetBytes(PeriodFilePos, PeriodChunkSize, PeriodArray0);

                                int PeriodChunkSize__ = 0;
                                for (long iii = (PeriodCount - 2); iii >= 0; iii--)
                                {
                                    PeriodFilePos    += i;
                                    PeriodChunkSize__ = (int)PeriodChunkSize;
                                    if (iii == 0)
                                    {
                                        int FileRemain = (int)(FileSize - PeriodFilePos);
                                        if (PeriodChunkSize__ > FileRemain)
                                        {
                                            PeriodChunkSize__ = FileRemain;
                                        }
                                    }

                                    // Read the period occurence other than first and compare with pattern,
                                    // if doest match the pattern, the reading and comparing will be broken
                                    if (PeriodChunkSize__ > 0)
                                    {
                                        FileSta.DataGetBytes(PeriodFilePos, PeriodChunkSize, PeriodArray1);

                                        for (int iiii = (PeriodChunkSize__ - 1); iiii >= 0; iiii--)
                                        {
                                            if (PeriodArray0[iiii] != PeriodArray1[iiii])
                                            {
                                                IsPeriodical = false;

                                                // Break all check iteration if data has no period by length given by i
                                                ii  = PeriodChunks;
                                                iii = (-1);
                                                break;
                                            }
                                        }
                                    }

                                    if (WorkTime < SW__.Elapsed())
                                    {
                                        while (WorkTime < SW__.Elapsed())
                                        {
                                            WorkTime += 1000L;
                                        }
                                        MailSegment.Console_WriteLine("Period " + i + "/" + FileSize + " (" + (FileSize > 0 ? (i * 100 / FileSize) : 0) + "%); occurence " + (PeriodCount - iii - 1) + "/" + PeriodCount + " (" + (PeriodCount > 0 ? (((PeriodCount - iii - 1) * 100) / PeriodCount) : 0) + "%)");
                                        MailSegment.Log(SW__.Elapsed().ToString(), i.ToString(), FileSize.ToString(), (PeriodCount - iii - 1).ToString(), PeriodCount.ToString());
                                        if (((PeriodCount - iii - 1) == PeriodCount))
                                        {
                                            PeriodicalPrint = false;
                                        }
                                    }
                                }
                                PeriodChunkOffset += (int)PeriodChunkSize;

                                PeriodChunkSize0 -= PeriodChunkSize;
                            }

                            if (IsPeriodical && PeriodicalPrint)
                            {
                                MailSegment.Console_WriteLine("Period " + i + "/" + FileSize + " (" + (FileSize > 0 ? (i * 100 / FileSize) : 0) + "%); occurence " + PeriodCount + "/" + PeriodCount + " (100%)");
                                MailSegment.Log(SW__.Elapsed().ToString(), i.ToString(), FileSize.ToString(), PeriodCount.ToString(), PeriodCount.ToString());
                                PeriodSize = i;
                                break;
                            }
                        }
                        if (PeriodSize == 0)
                        {
                            MailSegment.Console_WriteLine("Period " + FileSize + "/" + FileSize + " (100%); occurence " + "1/1" + " (100%)");
                            MailSegment.Log(SW__.Elapsed().ToString(), FileSize.ToString(), FileSize.ToString(), "1", "1");
                        }

                        FileSta.DataValueFileClose();
                        FileSta.Close();

                        MailSegment.ConsoleLineToLog    = true;
                        MailSegment.ConsoleLineToLogSum = true;
                        MailSegment.Console_WriteLine("");
                        if (PeriodSize > 0)
                        {
                            MailSegment.Console_WriteLine("Sequence period length: " + PeriodSize);
                            MailSegment.Console_WriteLine("Period search time: " + MailSegment.TimeHMSM(SW__.Elapsed()));
                            MailSegment.Console_WriteLine("");
                            DisplayStats("Sequence period distribution", PeriodStats, CreatePeriod);
                        }
                        else
                        {
                            MailSegment.Console_WriteLine("Sequence has no period");
                            MailSegment.Console_WriteLine("Period search time: " + MailSegment.TimeHMSM(SW__.Elapsed()));
                        }
                        MailSegment.ConsoleLineToLogSum = false;
                        MailSegment.ConsoleLineToLog    = false;
                    }
                    else
                    {
                        MailSegment.ConsoleLineToLog    = true;
                        MailSegment.ConsoleLineToLogSum = true;
                        MailSegment.Console_WriteLine("Period search error: " + FileSta.OpenError);
                        MailSegment.ConsoleLineToLogSum = false;
                        MailSegment.ConsoleLineToLog    = false;
                    }
                }

                MailSegment.ConsoleLineToLog    = true;
                MailSegment.ConsoleLineToLogSum = true;
                MailSegment.Console_WriteLine("");
                MailSegment.Console_WriteLine("Total time: " + MailSegment.TimeHMSM(TSW.Elapsed()));
                MailSegment.ConsoleLineToLogSum = false;
                MailSegment.ConsoleLineToLog    = false;
            }
            else
            {
                MailSegment.ConsoleLineToLog    = true;
                MailSegment.ConsoleLineToLogSum = true;
                MailSegment.Console_WriteLine("");
                MailSegment.Console_WriteLine("Source file open error: " + FileSrc.OpenError);
                MailSegment.ConsoleLineToLogSum = false;
                MailSegment.ConsoleLineToLog    = false;
            }
        }
        void DisplayStats(string Msg, ulong[] StatData, int Mode)
        {
            ulong ValMax    = 0;
            ulong ValMin    = ulong.MaxValue;
            ulong ValMin0   = ulong.MaxValue;
            ulong ValAvg    = 0;
            ulong ValAvg0   = 0;
            ulong ValDev    = 0;
            ulong ValDev0   = 0;
            ulong ValSum    = 0;
            ulong ValCount0 = 0;

            for (int i = 0; i < 256; i++)
            {
                if (ValMin > StatData[i])
                {
                    ValMin = StatData[i];
                }
                ValAvg = ValAvg + StatData[i];
                if (StatData[i] > 0)
                {
                    ValAvg0 = ValAvg0 + StatData[i];
                    ValCount0++;
                    if (ValMin0 > StatData[i])
                    {
                        ValMin0 = StatData[i];
                    }
                }
                if (ValMax < StatData[i])
                {
                    ValMax = StatData[i];
                }
                ValSum += StatData[i];
            }
            ValAvg = ValAvg / 256;
            if (ValCount0 > 0)
            {
                ValAvg0 = ValAvg0 / ValCount0;
            }
            for (int i = 0; i < 256; i++)
            {
                ulong DevDiff  = 0;
                ulong DevDiff0 = 0;
                if (StatData[i] >= ValAvg)
                {
                    DevDiff = StatData[i] - ValAvg;
                }
                else
                {
                    DevDiff = ValAvg - StatData[i];
                }
                ValDev += DevDiff;
                if (StatData[i] > 0)
                {
                    if (StatData[i] >= ValAvg0)
                    {
                        DevDiff0 = StatData[i] - ValAvg0;
                    }
                    else
                    {
                        DevDiff0 = ValAvg0 - StatData[i];
                    }
                    ValDev0 += DevDiff0;
                }
            }

            if (Mode == 1)
            {
                ulong GreatVal = ValMax / 10000UL;
                if ((ValMax % 10000UL) > 0)
                {
                    GreatVal++;
                }
                uint[] Stat_ = new uint[256];
                ulong  ValD  = 1;
                if (GreatVal == 0)
                {
                    GreatVal = 1;
                }
                while (ValD < GreatVal)
                {
                    ValD = ValD * 10;
                }
                ulong ValDx = ValD / 10;
                if (ValDx < 1)
                {
                    ValDx = 1;
                }

                MailSegment.Console_WriteLine(Msg + " n/" + ValD + ":");
                string ConBuf = "";
                for (int i = 0; i < 256; i++)
                {
                    Stat_[i] = (uint)(StatData[i] / ValD);
                    if ((ValD > 1) && ((StatData[i] / ValDx) % 10) > 0)
                    {
                        if (Stat_[i] < 9999)
                        {
                            Stat_[i]++;
                        }
                    }

                    ConBuf += (Stat_[i].ToString().PadLeft(4, ' '));
                    if (((i + 1) % 16) == 0)
                    {
                        MailSegment.Console_WriteLine(ConBuf);
                        ConBuf = "";
                    }
                    else
                    {
                        ConBuf += " ";
                    }
                }
            }
            if ((Mode == 2) || (Mode == 3))
            {
                MailSegment.Console_WriteLine(Msg + ":");

                int PadI = (int)(Math.Floor(Math.Log10(ValMax)) + 1);
                for (int i = 0; i < 256; i++)
                {
                    if ((Mode == 2) || (StatData[i] > 0))
                    {
                        MailSegment.Console_WriteLine(i.ToString().PadLeft(3, ' ') + ": " + StatData[i].ToString().PadLeft(PadI, ' '));
                    }
                }
            }

            MailSegment.Console_WriteLine("Minimum including 0: " + ValMin);
            if (ValCount0 > 0)
            {
                MailSegment.Console_WriteLine("Minimum excluding 0: " + ValMin0);
            }
            MailSegment.Console_WriteLine("Maximum: " + ValMax);
            MailSegment.Console_WriteLine("Average including 0: " + ValAvg);
            if (ValCount0 > 0)
            {
                MailSegment.Console_WriteLine("Average excluding 0: " + ValAvg0);
            }

            ValDev = (ulong)(Math.Sqrt((double)ValDev / 256.0) * 1000.0);
            MailSegment.Console_WriteLine("Standard deviation x1000 including 0: " + ValDev);
            if (ValCount0 > 0)
            {
                ValDev0 = (ulong)(Math.Sqrt((double)ValDev0 / (double)ValCount0) * 1000.0);
                MailSegment.Console_WriteLine("Standard deviation x1000 excluding 0: " + ValDev0);
            }

            MailSegment.Console_WriteLine("Sum: " + ValSum);
            MailSegment.Console_WriteLine("Number of non-zeros: " + ValCount0);
            MailSegment.Console_WriteLine("Number of zeros: " + (256UL - ValCount0));
        }
Beispiel #12
0
        /// <summary>
        /// Write segment to data file
        /// </summary>
        /// <param name="SegmentNo"></param>
        /// <param name="SegmentData"></param>
        /// <param name="SegmentDataLength"></param>
        public void DataSet(int SegmentNo, byte[] SegmentData, int SegmentDataLength)
        {
            Monitor.Enter(DataF_);
            if ((!IsDummyFile) && (ParamDataFile != null))
            {
                FileStream DataS         = DataOpen();
                long       SegmentOffset = SegmentNo;
                if (ParamDigestMode)
                {
                    SegmentOffset = (SegmentOffset + 1) * DigestSize;
                    long FileSize___ = DataS.Length;
                    if (DataS.Length < SegmentOffset)
                    {
                        DataS.Seek(0, SeekOrigin.End);
                        while (DataS.Length < SegmentOffset)
                        {
                            DataS.Write(Dummy, 0, DigestSize);
                        }
                    }
                    DataS.Seek(SegmentOffset, SeekOrigin.Begin);
                    if (SegmentData != null)
                    {
                        DataS.Write(MailSegment.DigestBin(SegmentData, SegmentDataLength), 0, DigestSize);
                    }
                    else
                    {
                        DataS.Write(Dummy, 0, DigestSize);
                    }

                    if ((FileSize___ != DataS.Length) || (SegmentCount == (SegmentNo + 1)))
                    {
                        SegmentOffset = SegmentNo;
                        FileSize__    = (SegmentOffset * SegmentSizeL) + (long)SegmentDataLength;
                        DataS.Seek(0, SeekOrigin.Begin);
                        DigestFileSize = FileSize__;
                        SegmentData    = MailSegment.StrToBin(FileSize__.ToString("X").PadLeft(16, '0'));
                        DataS.Write(SegmentData, 0, 16);
                        DigestSegmentSize = SegmentSize;
                        SegmentData       = MailSegment.StrToBin(SegmentSize.ToString("X").PadLeft(16, '0'));
                        DataS.Write(SegmentData, 0, 16);
                    }
                }
                else
                {
                    SegmentOffset = SegmentOffset * SegmentSize;
                    if (DataS.Length < SegmentOffset)
                    {
                        DataS.Seek(0, SeekOrigin.End);
                        while (DataS.Length < SegmentOffset)
                        {
                            DataS.Write(Dummy, 0, SegmentSize);
                        }
                    }
                    if (SegmentCount <= SegmentNo)
                    {
                        SegmentCount = SegmentNo + 1;
                    }
                    DataS.Seek(SegmentOffset, SeekOrigin.Begin);
                    if (SegmentData != null)
                    {
                        DataS.Write(SegmentData, 0, SegmentDataLength);
                    }
                    else
                    {
                        if (DataS.Length > SegmentOffset)
                        {
                            if ((SegmentOffset + SegmentDataLength) > DataS.Length)
                            {
                                DataS.Write(Dummy, 0, (int)(DataS.Length - SegmentOffset));
                            }
                            else
                            {
                                DataS.Write(Dummy, 0, SegmentDataLength);
                            }
                        }
                    }
                    FileSize__ = DataS.Length;
                }
                DataS.Close();
            }
            Monitor.Exit(DataF_);
        }
Beispiel #13
0
 /// <summary>
 /// Read segment from data file and compute the digest
 /// </summary>
 /// <param name="SegmentNo"></param>
 /// <returns></returns>
 public byte[] DataGetDigest(int SegmentNo)
 {
     Monitor.Enter(DataF_);
     byte[] SegmentData;
     if (ParamDataFile != null)
     {
         if (IsDummyFile)
         {
             long SegmentOffset = SegmentNo;
             SegmentOffset = SegmentOffset * SegmentSize;
             long SegmentSize_ = DummyFileSize - SegmentOffset;
             if (SegmentSize_ > SegmentSize)
             {
                 SegmentSize_ = SegmentSize;
             }
             SegmentData = RandomSequence_.GenSeq(SegmentOffset, SegmentSize_);
             SegmentData = MailSegment.StrToBin(MailSegment.Digest(SegmentData));
         }
         else
         {
             FileStream DataS         = DataOpen();
             long       SegmentOffset = SegmentNo;
             if (ParamDigestMode)
             {
                 SegmentOffset = (SegmentOffset + 1) * DigestSize;
                 SegmentData   = new byte[DigestSize];
                 for (int i = 0; i < DigestSize; i++)
                 {
                     SegmentData[i] = (byte)'_';
                 }
                 if ((SegmentOffset + DigestSize) <= DataS.Length)
                 {
                     DataS.Seek(SegmentOffset, SeekOrigin.Begin);
                     DataS.Read(SegmentData, 0, DigestSize);
                 }
             }
             else
             {
                 SegmentOffset = SegmentOffset * SegmentSize;
                 long SegmentSize_ = DataS.Length - SegmentOffset;
                 if (SegmentSize_ > SegmentSize)
                 {
                     SegmentSize_ = SegmentSize;
                 }
                 SegmentData = new byte[SegmentSize_];
                 DataS.Seek(SegmentOffset, SeekOrigin.Begin);
                 DataS.Read(SegmentData, 0, (int)SegmentSize_);
                 SegmentData = MailSegment.StrToBin(MailSegment.Digest(SegmentData));
             }
             DataS.Close();
         }
     }
     else
     {
         SegmentData = new byte[SegmentSize];
         for (int i = 0; i < SegmentSize; i++)
         {
             SegmentData[i] = 0;
         }
         SegmentData = MailSegment.StrToBin(MailSegment.Digest(SegmentData));
     }
     Monitor.Exit(DataF_);
     return(SegmentData);
 }
Beispiel #14
0
 /// <summary>
 /// Open data/map file object with accessibility test before action
 /// </summary>
 /// <param name="DigestMode"></param>
 /// <param name="DataRead"></param>
 /// <param name="DataFile"></param>
 /// <param name="MapFile"></param>
 /// <returns></returns>
 public bool Open(bool DigestMode, bool DataRead, string DataFile, string MapFile)
 {
     MapDummy.Clear();
     OpenError         = "";
     ParamDigestMode   = DigestMode;
     ParamDataRead     = DataRead;
     ParamDataFile     = DataFile;
     ParamMapFile      = MapFile;
     SegmentSizeL      = 0;
     SegmentSize       = 0;
     SegmentCount      = 0;
     DigestFileSize    = 0;
     DigestSegmentSize = 0;
     try
     {
         if (DataFile != null)
         {
             IsDummyFile = DataFile.StartsWith(DummyFileSign, StringComparison.InvariantCulture);
             if (IsDummyFile)
             {
                 RandomSequence_ = RandomSequence.CreateRS(DataFile.Substring(1), MailSegment.RandomCacheStep);
                 if (RandomSequence_ == null)
                 {
                     throw new Exception(RandomSequence.ErrorMsg);
                 }
                 DummyFileSize = RandomSequence.DummyFileSize;
             }
             else
             {
                 FileStream Temp = DataOpen();
                 if (ParamDigestMode)
                 {
                     if (ParamDataRead)
                     {
                         if (Temp.Length < 32)
                         {
                             if (ParamDataRead)
                             {
                                 Temp.Close();
                                 OpenError = "Incorrect digest file";
                                 return(false);
                             }
                             else
                             {
                                 for (int i = 0; i < DigestSize; i++)
                                 {
                                     Temp.WriteByte((byte)'_');
                                 }
                                 Temp.Seek(0, SeekOrigin.Begin);
                             }
                         }
                         else
                         {
                             byte[] Buf1 = new byte[16];
                             byte[] Buf2 = new byte[16];
                             Temp.Seek(0, SeekOrigin.Begin);
                             Temp.Read(Buf1, 0, 16);
                             Temp.Read(Buf2, 0, 16);
                             Temp.Seek(0, SeekOrigin.Begin);
                             try
                             {
                                 DigestFileSize    = long.Parse(MailSegment.BinToStr(Buf1), NumberStyles.HexNumber);
                                 DigestSegmentSize = int.Parse(MailSegment.BinToStr(Buf2), NumberStyles.HexNumber);
                             }
                             catch
                             {
                                 Temp.Close();
                                 OpenError = "Incorrect digest file";
                                 return(false);
                             }
                         }
                     }
                 }
                 Temp.Close();
             }
         }
     }
     catch (Exception e)
     {
         OpenError = e.Message;
         return(false);
     }
     try
     {
         if (MapFile != null)
         {
             FileStream Temp = MapOpen();
             Temp.Close();
         }
     }
     catch (Exception e)
     {
         OpenError = e.Message;
         return(false);
     }
     return(true);
 }
Beispiel #15
0
        public void ResizeData()
        {
            if ((SegmentCount == 0) || (SegmentSize == 0))
            {
                return;
            }

            long FileSize__Min = (long)(SegmentCount - 1) * (long)(SegmentSize) + 1L;
            long FileSize__Max = (long)SegmentCount * (long)SegmentSize;

            Monitor.Enter(DataF_);
            if ((!IsDummyFile) && (ParamDataFile != null) && (!ParamDataRead))
            {
                FileStream DataS = ParamDigestMode ? DataOpenRW(false) : DataOpen();
                if (ParamDigestMode)
                {
                    if (DataS.Length >= 16L)
                    {
                        byte[] Buf1 = new byte[16];
                        //byte[] Buf2 = new byte[16];
                        DataS.Seek(0, SeekOrigin.Begin);
                        DataS.Read(Buf1, 0, 16);
                        //DataS.Read(Buf2, 0, 16);
                        DigestFileSize = 0;
                        try
                        {
                            DigestFileSize = long.Parse(MailSegment.BinToStr(Buf1), NumberStyles.HexNumber);
                            //DigestSegmentSize = int.Parse(MailSegment.BinToStr(Buf2), NumberStyles.HexNumber);
                        }
                        catch
                        {
                            DigestFileSize = 0;
                        }
                    }

                    long FileSizeDig = DigestFileSize;
                    if (FileSizeDig < FileSize__Min)
                    {
                        FileSizeDig = FileSize__Min;
                    }
                    if (FileSizeDig > FileSize__Max)
                    {
                        FileSizeDig = FileSize__Max;
                    }

                    DataS.Seek(0, SeekOrigin.Begin);
                    byte[] SegmentData = MailSegment.StrToBin(FileSizeDig.ToString("X").PadLeft(16, '0'));
                    DataS.Write(SegmentData, 0, 16);
                    SegmentData = MailSegment.StrToBin(SegmentSize.ToString("X").PadLeft(16, '0'));
                    DataS.Write(SegmentData, 0, 16);

                    long DigestDataSize__ = ((long)(SegmentCount + 1) * (long)DigestSize);
                    if (DataS.Length < DigestDataSize__)
                    {
                        DataS.Seek(0, SeekOrigin.End);
                        while (DataS.Length < DigestDataSize__)
                        {
                            DataS.Write(Dummy, 0, DigestSize);
                        }
                    }
                    DataS.SetLength(DigestDataSize__);
                }
                else
                {
                    if (DataS.Length < FileSize__Min)
                    {
                        DataS.SetLength(FileSize__Min);
                    }
                    if (DataS.Length > FileSize__Max)
                    {
                        DataS.SetLength(FileSize__Max);
                    }
                }
                DataS.Close();
            }
            Monitor.Exit(DataF_);
        }