예제 #1
0
파일: BufRef.cs 프로젝트: kyapp69/i2p-cs
 int IComparable <BufLen> .CompareTo(BufLen other)
 {
     return(Compare(this, other));
 }
예제 #2
0
파일: BufRef.cs 프로젝트: kyapp69/i2p-cs
 public BufRefLen(BufLen src, int offset, int len) : base(src, offset)
 {
     LengthDef = Math.Min(len, src.Length - offset);
 }
예제 #3
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static ParametersWithIV ToParametersWithIV(this BufLen key, BufLen iv)
 {
     return(new ParametersWithIV(key.ToKeyParameter(), iv.BaseArray, iv.BaseArrayOffset, iv.Length));
 }
예제 #4
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static void Randomize(this BufLen buf)
 {
     Rnd.NextBytes(buf.BaseArray, buf.BaseArrayOffset, buf.Length);
 }
예제 #5
0
파일: BufRef.cs 프로젝트: glasgowdev/i2p-cs
 public void Poke(BufLen src, int offset, int maxlen)
 {
     Array.Copy(src.BaseArray, src.BaseArrayOffset, Data, Position + offset, Math.Min(maxlen, src.Length));
 }
예제 #6
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static KeyParameter ToKeyParameter(this BufLen key)
 {
     return(new KeyParameter(key.BaseArray, key.BaseArrayOffset, key.Length));
 }
예제 #7
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static void Decrypt(this BufferedBlockCipher cipher, BufLen key, BufLen iv, BufLen data)
 {
     cipher.Init(false, new ParametersWithIV(new KeyParameter(key.BaseArray, key.BaseArrayOffset, key.Length), iv.BaseArray, iv.BaseArrayOffset, iv.Length));
     cipher.ProcessBytes(data);
 }
예제 #8
0
파일: BufRef.cs 프로젝트: kyapp69/i2p-cs
 public BufLen(BufLen src) : base(src)
 {
     LengthDef = src.LengthDef;
 }
예제 #9
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static void AesEcbDecrypt(this BufLen buf, byte[] key)
 {
     AesEcbCipher.Init(false, new KeyParameter(key));
     AesEcbCipher.ProcessBytes(buf);
 }
예제 #10
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static void AesEcbDecrypt(this BufLen buf, BufLen key)
 {
     AesEcbCipher.Init(false, new KeyParameter(key.BaseArray, key.BaseArrayOffset, key.Length));
     AesEcbCipher.ProcessBytes(buf);
 }
예제 #11
0
        public static BufLen BCGZipDecompressNew(BufLen buf)
        {
            var reader = new BufRefLen(buf);

            // Skip gzip header
            var gzheader = reader.ReadBufLen(10);
            var flag     = gzheader.Peek8(3);

            if ((flag & 0x04) != 0)
            {
                reader.Seek(reader.Read16());                           // "Extra"
            }
            if ((flag & 0x08) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Name"
                }
            }
            if ((flag & 0x10) != 0)
            {
                while (reader.Read8() != 0)
                {
                    ;                                                   // "Comment"
                }
            }
            if ((flag & 0x02) != 0)
            {
                reader.Read16();                         // "CRC16"
            }
            var z = new ZStream();
            z.inflateInit(true);

            var dest   = new byte[buf.Length * 2];
            var destix = 0;

            z.next_in_index = reader.BaseArrayOffset;
            z.next_in       = reader.BaseArray;
            z.avail_in      = reader.Length - 8;

bigger_dest:

            z.next_out       = dest;
            z.next_out_index = destix;
            z.avail_out      = dest.Length - destix;
            var err = z.inflate(JZlib.Z_FINISH);
            if (err != JZlib.Z_BUF_ERROR && err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
            {
                throw new IOException("inflating: " + z.msg);
            }

            if (z.avail_out == 0)
            {
                var newdest = new byte[dest.Length * 2];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            var result = new BufLen(dest, 0, dest.Length - z.avail_out);
            z.inflateEnd();

            return(result);
        }
예제 #12
0
        public static UInt32 Adler32(UInt32 adler, BufLen data)
        {
            const Int32 BASE = 65521;      /* largest prime smaller than 65536 */
            const Int32 NMAX = 5552;

            UInt32 sum2;
            UInt32 n;

            int len = data.Length;

            /* split Adler-32 into component sums */
            sum2   = (adler >> 16) & 0xffff;
            adler &= 0xffff;

            /* in case user likes doing a byte at a time, keep it fast */
            if (data.Length == 1)
            {
                adler += data[0];
                if (adler >= BASE)
                {
                    adler -= BASE;
                }
                sum2 += adler;
                if (sum2 >= BASE)
                {
                    sum2 -= BASE;
                }
                return(adler | (sum2 << 16));
            }

            /* initial Adler-32 value (deferred check for len == 1 speed) */
            if (data == null)
            {
                return(1);
            }

            /* in case short lengths are provided, keep it somewhat fast */
            if (data.Length < 16)
            {
                int ix = 0;
                while (len-- > 0)
                {
                    adler += data[ix++];
                    sum2  += adler;
                }
                if (adler >= BASE)
                {
                    adler -= BASE;
                }
                sum2 %= BASE;            /* only added so many BASE's */
                return(adler | (sum2 << 16));
            }

            Int32 i = 0;

            /* do length NMAX blocks -- requires just one modulo operation */
            while (len >= NMAX)
            {
                len -= NMAX;
                n    = NMAX / 16;       /* NMAX is divisible by 16 */
                do
                {
                    //DO16(data);          /* 16 sums unrolled */
                    adler += data[i]; sum2 += adler;
                    adler += data[i + 1]; sum2 += adler;
                    adler += data[i + 2]; sum2 += adler;
                    adler += data[i + 3]; sum2 += adler;
                    adler += data[i + 4]; sum2 += adler;
                    adler += data[i + 5]; sum2 += adler;
                    adler += data[i + 6]; sum2 += adler;
                    adler += data[i + 7]; sum2 += adler;
                    adler += data[i + 8]; sum2 += adler;
                    adler += data[i + 9]; sum2 += adler;
                    adler += data[i + 10]; sum2 += adler;
                    adler += data[i + 11]; sum2 += adler;
                    adler += data[i + 12]; sum2 += adler;
                    adler += data[i + 13]; sum2 += adler;
                    adler += data[i + 14]; sum2 += adler;
                    adler += data[i + 15]; sum2 += adler;
                    i     += 16;
                } while (--n > 0);
                adler %= BASE;
                sum2  %= BASE;
            }

            /* do remaining bytes (less than NMAX, still just one modulo) */
            if (len > 0)
            {                  /* avoid modulos if none remaining */
                while (len >= 16)
                {
                    len   -= 16;
                    adler += data[i]; sum2 += adler;
                    adler += data[i + 1]; sum2 += adler;
                    adler += data[i + 2]; sum2 += adler;
                    adler += data[i + 3]; sum2 += adler;
                    adler += data[i + 4]; sum2 += adler;
                    adler += data[i + 5]; sum2 += adler;
                    adler += data[i + 6]; sum2 += adler;
                    adler += data[i + 7]; sum2 += adler;
                    adler += data[i + 8]; sum2 += adler;
                    adler += data[i + 9]; sum2 += adler;
                    adler += data[i + 10]; sum2 += adler;
                    adler += data[i + 11]; sum2 += adler;
                    adler += data[i + 12]; sum2 += adler;
                    adler += data[i + 13]; sum2 += adler;
                    adler += data[i + 14]; sum2 += adler;
                    adler += data[i + 15]; sum2 += adler;
                    i     += 16;
                }
                while (len-- > 0)
                {
                    adler += data[i++];
                    sum2  += adler;
                }
                adler %= BASE;
                sum2  %= BASE;
            }

            /* return recombined sums */
            return(adler | (sum2 << 16));
        }
예제 #13
0
        public static BufLen BCGZipCompressNew(BufLen buf)
        {
            var crc = LZUtils.CRC32(buf);

            var dest   = new byte[buf.Length + 4096];
            var destix = 0;

            dest[destix++] = 0x1f;
            dest[destix++] = 0x8b;
            dest[destix++] = 0x08;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x00;
            dest[destix++] = 0x02;
            dest[destix++] = 0xFF;

            var z = new ZStream();

            z.deflateInit(6, true);

            z.next_in_index = buf.BaseArrayOffset;
            z.next_in       = buf.BaseArray;
            z.avail_in      = buf.Length;

bigger_dest:

            z.next_out       = dest;
            z.next_out_index = destix;
            z.avail_out      = dest.Length - destix;
            var err = z.deflate(JZlib.Z_FINISH);

            if (err != JZlib.Z_OK && err != JZlib.Z_STREAM_END)
            {
                throw new IOException("deflating: " + z.msg);
            }

            if (z.avail_out == 0)
            {
                var newdest = new byte[dest.Length * 2];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            if (z.avail_out < 8)
            {
                var newdest = new byte[dest.Length + 8];
                Array.Copy(dest, newdest, dest.Length);
                destix = dest.Length;
                dest   = newdest;
                goto bigger_dest;
            }

            var result = new BufLen(dest, 0, 10 + dest.Length - z.avail_out + 8);

            result.Poke32(crc, result.Length - 8);
            result.Poke32((uint)buf.Length, result.Length - 4);

            z.deflateEnd();

            /*
             * using ( var foos = new FileStream( "test.gz", FileMode.Create, FileAccess.Write ) )
             * {
             * foos.Write( msb );
             * }
             */
            return(result);
        }
예제 #14
0
파일: BufRef.cs 프로젝트: kyapp69/i2p-cs
        public void Poke(BufLen src)
        {
            PreWriteCheck(src.Length);

            Array.Copy(src.BaseArray, src.BaseArrayOffset, Data, Position, src.Length);
        }
예제 #15
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static void Decrypt(this CbcBlockCipher cipher, BufLen key, BufLen iv, BufLen data)
 {
     cipher.Init(false, key.ToParametersWithIV(iv));
     cipher.ProcessBytes(data);
 }
예제 #16
0
파일: BufRef.cs 프로젝트: kyapp69/i2p-cs
        public void Poke(BufLen src, int offset)
        {
            PreWriteCheck(offset + src.Length);

            Array.Copy(src.BaseArray, src.BaseArrayOffset, Data, Position + offset, src.Length);
        }
예제 #17
0
파일: BufUtils.cs 프로젝트: itfenom/i2p-cs
 public static void ProcessBytes(this BufferedBlockCipher cipher, BufLen data)
 {
     cipher.ProcessBytes(data.BaseArray, data.BaseArrayOffset, data.Length, data.BaseArray, data.BaseArrayOffset);
 }
예제 #18
0
파일: BufRef.cs 프로젝트: kyapp69/i2p-cs
 public BufLen(BufLen src, int offset)
     : base(src, offset)
 {
     LengthDef = src.LengthDef - offset;
 }
예제 #19
0
파일: BufRef.cs 프로젝트: glasgowdev/i2p-cs
 public void Poke(BufLen src, int offset)
 {
     Array.Copy(src.BaseArray, src.BaseArrayOffset, Data, Position + offset, src.Length);
 }