Ejemplo n.º 1
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);
 }
Ejemplo n.º 2
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_);
        }
Ejemplo n.º 3
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_);
        }