示例#1
0
        private long WriteToDbFile(DbFileResource dbFile, byte[] data, byte[] datalen, byte[] hash, int stublen, long position)
        {
            bool isAppend = position < 0 || position >= dbFile.length;

            if (isAppend)
            {
                position = dbFile.stream.Seek(0, SeekOrigin.End);
            }
            else
            {
                dbFile.stream.Position = position;
            }
            dbFile.stream.Write(datalen, 0, 4);
            dbFile.stream.Write(data, 0, data.Length);
            dbFile.stream.Write(hash, 0, 16);
            int s;

            for (s = stublen; s > stub.Length; s -= stub.Length)
            {
                dbFile.stream.Write(stub, 0, stub.Length);
            }
            dbFile.stream.Write(stub, 0, s);
            if (isAppend)
            {
                dbFile.length += data.Length + 20 + stublen;
            }
            return(position);
        }
示例#2
0
        /// <summary> If position == -1 write to end of file, else dataitems will be writed to position </summary>
        /// <returns> Position in file where dataitems were saved. It equals to parameter position, if it was valid,
        ///  else if parameter position was equals -1 or other not valid position, method returns new position of dataitems. </returns>
        private long WriteToDbFile(DbFileResource dbFile, int capacity, byte[] data, long position)
        {
            var len  = BitConverter.GetBytes(data.Length);
            var hash = new MD5CryptoServiceProvider().ComputeHash(data);

            lock (dbFile)
                return(WriteToDbFile(dbFile, data, len, hash, capacity - data.Length, position));
        }
示例#3
0
        private byte[] ReadFromDbFile(DbFileResource dbFile, int capacity, long position)
        {
            byte[] data, hash;
            lock (dbFile)
                ReadFromDbFile(dbFile, capacity, position, out data, out hash);
            var control = new MD5CryptoServiceProvider().ComputeHash(data);

            if (!control.SequenceEqual(hash))
            {
                return(null);
            }
            return(data);
        }
示例#4
0
        /// <summary>
        /// Need locking dbFile before calling!
        /// </summary>
        private void ReadFromDbFile(DbFileResource dbFile, int capacity, long position, out byte[] data, out byte[] hash)
        {
            data = null;
            var buf = hash = new byte[16];

            dbFile.stream.Position = position;
            dbFile.stream.Read(buf, 0, 4);
            int len = BitConverter.ToInt32(buf, 0);

            if (len > capacity || len < 0)
            {
                return;
            }
            data = new byte[len];
            dbFile.stream.Read(data, 0, len);
            dbFile.stream.Read(buf, 0, 16);
        }
示例#5
0
 private long WriteToDbFile(DbFileResource dbFile, byte[] data, byte[] datalen, byte[] hash, int stublen, long position)
 {
     bool isAppend = position < 0 || position >= dbFile.length;
     if (isAppend)
         position = dbFile.stream.Seek(0, SeekOrigin.End);
     else
         dbFile.stream.Position = position;
     dbFile.stream.Write(datalen, 0, 4);
     dbFile.stream.Write(data, 0, data.Length);
     dbFile.stream.Write(hash, 0, 16);
     int s;
     for (s = stublen; s > stub.Length; s-=stub.Length)
         dbFile.stream.Write(stub, 0, stub.Length);
     dbFile.stream.Write(stub, 0, s);
     if (isAppend) dbFile.length += data.Length + 20 + stublen;
     return position;
 }
示例#6
0
 /// <summary> If position == -1 write to end of file, else dataitems will be writed to position </summary>
 /// <returns> Position in file where dataitems were saved. It equals to parameter position, if it was valid,
 ///  else if parameter position was equals -1 or other not valid position, method returns new position of dataitems. </returns>
 private long WriteToDbFile(DbFileResource dbFile, int capacity, byte[] data, long position)
 {
     var len = BitConverter.GetBytes(data.Length);
     var hash = new MD5CryptoServiceProvider().ComputeHash(data);
     lock (dbFile)
         return WriteToDbFile(dbFile, data, len, hash, capacity - data.Length, position);
 }
示例#7
0
 /// <summary>
 /// Need locking dbFile before calling!
 /// </summary>
 private void ReadFromDbFile(DbFileResource dbFile, int capacity, long position, out byte[] data, out byte[] hash)
 {
     data = null;
     var buf = hash = new byte[16];
     dbFile.stream.Position = position;
     dbFile.stream.Read(buf, 0, 4);
     int len = BitConverter.ToInt32(buf, 0);
     if (len > capacity || len < 0) return;
     data = new byte[len];
     dbFile.stream.Read(data, 0, len);
     dbFile.stream.Read(buf, 0, 16);
 }
示例#8
0
 private byte[] ReadFromDbFile(DbFileResource dbFile, int capacity, long position)
 {
     byte[] data, hash;
     lock (dbFile)
         ReadFromDbFile(dbFile, capacity, position, out data, out hash);
     var control = new MD5CryptoServiceProvider().ComputeHash(data);
     if (!control.SequenceEqual(hash)) return null;
     return data; 
 }