コード例 #1
0
        internal int SetDictionary(byte[] byte_2)
        {
            int num         = byte_2.Length;
            int sourceIndex = 0;

            if (byte_2 != null)
            {
                if (this.int_19 == Class0.int_3)
                {
                    this.zlibCodec_0.uint_0 = Adler.Adler32(this.zlibCodec_0.uint_0, byte_2, 0, byte_2.Length);
                    if (num < Class0.int_14)
                    {
                        return(0);
                    }
                    if (num > this.int_23 - Class0.int_16)
                    {
                        num         = this.int_23 - Class0.int_16;
                        sourceIndex = byte_2.Length - num;
                    }
                    Array.Copy(byte_2, sourceIndex, this.byte_1, 0, num);
                    this.int_36 = num;
                    this.int_32 = num;
                    this.int_27 = (int)(this.byte_1[0] & 255);
                    this.int_27 = ((this.int_27 << this.int_31 ^ (int)(this.byte_1[1] & 255)) & this.int_30);
                    for (int i = 0; i <= num - Class0.int_14; i++)
                    {
                        this.int_27 = ((this.int_27 << this.int_31 ^ (int)(this.byte_1[i + (Class0.int_14 - 1)] & 255)) & this.int_30);
                        this.short_0[i & this.int_25] = this.short_1[this.int_27];
                        this.short_1[this.int_27]     = (short)i;
                    }
                    return(0);
                }
            }
            throw new ZlibException("Stream error.");
        }
コード例 #2
0
        /// <summary>
        /// An object to hold the Volume section data.
        /// </summary>
        /// <param name="bytes">The bytes from which to make the object.</param>
        public Volume(byte[] bytes)
        {
            MediaType = (MEDIA_TYPE)bytes[0];

            ChunkCount      = BitConverter.ToInt32(bytes, 4);
            SectorsPerChunk = BitConverter.ToInt32(bytes, 8);
            BytesPerSector  = BitConverter.ToInt32(bytes, 12);
            SectorCount     = BitConverter.ToInt32(bytes, 16);

            Cylinders = BitConverter.ToInt32(bytes, 24);
            Heads     = BitConverter.ToInt32(bytes, 28);
            Sectors   = BitConverter.ToInt32(bytes, 32);

            MediaFlag = (MEDIA_FLAG)bytes[36];

            PALMVolumeStart      = BitConverter.ToInt32(bytes, 40);
            SMARTLogsStartSector = BitConverter.ToInt32(bytes, 48);

            Compression = (COMPRESSION)bytes[52];

            ErrorBlockSize = BitConverter.ToInt32(bytes, 56);

            byte[] guidBytes = new byte[16];
            Array.Copy(bytes, 64, guidBytes, 0, 16);
            SetGUID = new Guid(guidBytes);

            uint adler32 = Adler.Adler32(1, bytes, 0, 1048);

            if (adler32 != BitConverter.ToUInt32(bytes, 1048))
            {
                throw new ArgumentException("bad Adler32 checksum");
            }
        }
コード例 #3
0
        private void SendTile(Tile tile)
        {
            message.Reset();
            message.WriteByte(0x01);

            message.WriteLocation(tile.Location);
            message.WriteByte((byte)tile.ThingCount);

            for (int i = 0; i < tile.ThingCount; i++)
            {
                var thing = tile.GetThing(i);

                if (thing is Creature)
                {
                    var cr = thing as Creature;
                    message.WriteByte(0x01);
                    message.WriteUInt(cr.Id);
                    message.WriteString(cr.Name);
                    message.WriteByte((byte)cr.Type);
                }
                else
                {
                    var item = thing as Item;
                    message.WriteByte(0x02);
                    message.WriteUShort((ushort)item.Id);
                    message.WriteByte(item.SubType);
                }
            }

            message.WriteInternalHead();
            Adler.Generate(message, true);
            message.WriteHead();

            connection.Send(message);
        }
コード例 #4
0
        public static uint GetDataKey(uint HeaderKey, RhoPackedFileInfo info)
        {
            byte[] strData = Encoding.GetEncoding("UTF-16").GetBytes(info.FileName);
            uint   key     = Adler.Adler32(0, strData, 0, strData.Length);

            key += info.Ext;
            key += (HeaderKey - 0x756DE654);
            return(key);
        }
コード例 #5
0
        /// <summary>
        /// Decompressed (inflates) a compressed byte array using the Inflate algorithm.
        /// </summary>
        /// <param name="compressedData">The deflate-compressed data</param>
        /// <param name="dictionary">The dictionary originally used to compress the data, or null if no dictionary was used.</param>
        /// <returns>The uncompressed data</returns>
        internal static byte[] ZlibDecompressWithDictionary(byte[] compressedData, byte[] dictionary)
        {
            using (var ms = new MemoryStream())
            {
                const int bufferSize = 256;
                var       buffer     = new byte[bufferSize];

                var codec = new ZlibCodec
                {
                    InputBuffer      = compressedData,
                    NextIn           = 0,
                    AvailableBytesIn = compressedData.Length
                };

                codec.AssertOk("InitializeInflate", codec.InitializeInflate(false));
                if (dictionary != null)
                {
                    codec.AssertOk("SetDictionary", codec.SetDictionary(dictionary));
                }

                codec.OutputBuffer = buffer;

                while (true)
                {
                    codec.NextOut           = 0;
                    codec.AvailableBytesOut = bufferSize;
                    var inflateReturnCode = codec.Inflate(FlushType.None);
                    var bytesToWrite      = bufferSize - codec.AvailableBytesOut;
                    ms.Write(buffer, 0, bytesToWrite);

                    if (inflateReturnCode == ZlibConstants.Z_STREAM_END)
                    {
                        break;
                    }
                    else if (inflateReturnCode == ZlibConstants.Z_NEED_DICT && dictionary != null)
                    {
                        //implies bytesToWrite was 0
                        var dictionaryAdler32 = ((int)Adler.Adler32(1u, dictionary, 0, dictionary.Length));
                        if (codec.Adler32 != dictionaryAdler32)
                        {
                            throw new InvalidOperationException("Compressed data is requesting a dictionary with adler32 " + codec.Adler32 + ", but the dictionary is actually " + dictionaryAdler32);
                        }

                        codec.AssertOk("SetDictionary", codec.SetDictionary(dictionary));
                    }
                    else
                    {
                        codec.AssertOk("Inflate", inflateReturnCode);
                    }
                }

                codec.AssertOk("EndInflate", codec.EndInflate());
                return(ms.ToArray());
            }
        }
コード例 #6
0
 internal int Flush(int int_15)
 {
     for (int i = 0; i < 2; i++)
     {
         int num;
         if (i == 0)
         {
             num = ((this.int_13 <= this.int_14) ? this.int_14 : this.int_12) - this.int_13;
         }
         else
         {
             num = this.int_14 - this.int_13;
         }
         if (num == 0)
         {
             if (int_15 == -5)
             {
                 int_15 = 0;
             }
             return(int_15);
         }
         if (num > this.zlibCodec_0.AvailableBytesOut)
         {
             num = this.zlibCodec_0.AvailableBytesOut;
         }
         if (num != 0 && int_15 == -5)
         {
             int_15 = 0;
         }
         this.zlibCodec_0.AvailableBytesOut -= num;
         this.zlibCodec_0.TotalBytesOut     += (long)num;
         if (this.object_0 != null)
         {
             this.zlibCodec_0.uint_0 = (this.uint_0 = Adler.Adler32(this.uint_0, this.byte_0, this.int_13, num));
         }
         Array.Copy(this.byte_0, this.int_13, this.zlibCodec_0.OutputBuffer, this.zlibCodec_0.NextOut, num);
         this.zlibCodec_0.NextOut += num;
         this.int_13 += num;
         if (this.int_13 == this.int_12 && i == 0)
         {
             this.int_13 = 0;
             if (this.int_14 == this.int_12)
             {
                 this.int_14 = 0;
             }
         }
         else
         {
             i++;
         }
     }
     return(int_15);
 }
コード例 #7
0
        internal uint Reset()
        {
            uint result = this.uint_0;

            this.enum2_0 = Class2.Enum2.const_0;
            this.int_9   = 0;
            this.int_10  = 0;
            this.int_14  = 0;
            this.int_13  = 0;
            if (this.object_0 != null)
            {
                this.zlibCodec_0.uint_0 = (this.uint_0 = Adler.Adler32(0u, null, 0, 0));
            }
            return(result);
        }
コード例 #8
0
        internal void Reset()
        {
            ZlibCodec arg_1C_0 = this.zlibCodec_0;

            this.zlibCodec_0.TotalBytesOut = 0L;
            arg_1C_0.TotalBytesIn          = 0L;
            this.zlibCodec_0.Message       = null;
            this.int_21             = 0;
            this.int_20             = 0;
            this.bool_0             = false;
            this.int_19             = (this.WantRfc1950HeaderBytes ? Class0.int_3 : Class0.int_4);
            this.zlibCodec_0.uint_0 = Adler.Adler32(0u, null, 0, 0);
            this.int_22             = 0;
            this.method_1();
            this.method_0();
        }
コード例 #9
0
        public RhoHeader(byte[] data, uint HeaderKey)
        {
            byte[] newData    = Crypt.RhoCrypt.DecryptHeader(data, HeaderKey);
            byte[] _hash_data = new byte[0x7C];
            Array.Copy(newData, 0x04, _hash_data, 0, 0x7C);
            uint Hash = Adler.Adler32(0, _hash_data, 0, _hash_data.Length);

            using (MemoryStream ms = new MemoryStream(newData))
            {
                BinaryReader br = new BinaryReader(ms);
                this.Hash = br.ReadUInt32();
                if (Hash != this.Hash)
                {
                    throw new Exception("*** RhoDecoderError!!! Reason: The hash is not match.");
                }
                Check = br.ReadUInt32();
                this.StreamInfoCount = br.ReadUInt32();
                b = br.ReadUInt32();
            }
        }
コード例 #10
0
		internal int SetDictionary(byte[] byte_1)
		{
			int int_ = 0;
			int num = byte_1.Length;
			if (this.enum3_0 != Class5.Enum3.const_6)
			{
				throw new ZlibException("Stream error.");
			}
			if (Adler.Adler32(1u, byte_1, 0, byte_1.Length) != this.zlibCodec_0.uint_0)
			{
				return -3;
			}
			this.zlibCodec_0.uint_0 = Adler.Adler32(0u, null, 0, 0);
			if (num >= 1 << this.int_4)
			{
				num = (1 << this.int_4) - 1;
				int_ = byte_1.Length - num;
			}
			this.class2_0.SetDictionary(byte_1, int_, num);
			this.enum3_0 = Class5.Enum3.const_7;
			return 0;
		}
コード例 #11
0
ファイル: Program.cs プロジェクト: Daniel-McCarthy/Net-Hash
        static void testCustomInput(string input)
        {
            Console.WriteLine("Testing FNV1-Hash with string \"" + input + "\"");
            Console.WriteLine(FNV.fnv1_32Hash(stringToByteArray(input)));

            Console.WriteLine("Testing FNV1A-Hash with string \"" + input + "\"");
            Console.WriteLine(FNV.fnv1A_32Hash(stringToByteArray(input)));

            Console.WriteLine("Testing FNV0-Hash with string \"" + input + "\"");
            Console.WriteLine(FNV.fnv0_32Hash(stringToByteArray(input)) + '\n');

            Console.WriteLine("Testing CRC-Hash with string \"" + input + "\"");
            Console.WriteLine(CRC.crc_32Hash(stringToByteArray(input)));

            Console.WriteLine("Testing CRC-B-Hash with string \"" + input + "\"");
            Console.WriteLine(CRC.crcB_32Hash(stringToByteArray(input)) + '\n');

            Console.WriteLine("Testing Adler-Hash with string \"" + input + "\"");
            Console.WriteLine(Adler.adler_32Hash(stringToByteArray(input)) + '\n');

            Console.WriteLine("Testing MD2-Hash with string \"" + input + "\"");
            Console.WriteLine(MD.md2_128Hash(stringToByteArray(input)));

            Console.WriteLine("Testing MD4-Hash with string \"" + input + "\"");
            Console.WriteLine(MD.md4_128Hash(stringToByteArray(input)));

            Console.WriteLine("Testing MD5-Hash with string \"" + input + "\"");
            Console.WriteLine(MD.md5_128Hash(stringToByteArray(input)));

            Console.WriteLine("Testing SHA0-Hash with string \"" + input + "\"");
            Console.WriteLine(SHA.sha0_160Hash(stringToByteArray(input)) + '\n');

            Console.WriteLine("Testing SHA1-Hash with string \"" + input + "\"");
            Console.WriteLine(SHA.sha1_160Hash(stringToByteArray(input)) + '\n');

            Console.WriteLine("Testing SHA2-Hash with string \"" + input + "\"");
            Console.WriteLine(SHA.sha2_224Hash(stringToByteArray(input)));
        }
コード例 #12
0
        internal int SetDictionary(byte[] dictionary)
        {
            int start = 0;
            int n     = dictionary.Length;

            if (this.mode != InflateManager.InflateManagerMode.DICT0)
            {
                throw new ZlibException("Stream error.");
            }
            if ((int)Adler.Adler32(1U, dictionary, 0, dictionary.Length) != (int)this._codec._Adler32)
            {
                return(-3);
            }
            this._codec._Adler32 = Adler.Adler32(0U, (byte[])null, 0, 0);
            if (n >= 1 << this.wbits)
            {
                n     = (1 << this.wbits) - 1;
                start = dictionary.Length - n;
            }
            this.blocks.SetDictionary(dictionary, start, n);
            this.mode = InflateManager.InflateManagerMode.BLOCKS;
            return(0);
        }
コード例 #13
0
        private void ParseMessage()
        {
            message.ReadPosition = 2;
            message.Encrypted    = false;

            if (Adler.Generate(message) != message.ReadUInt())
            {
                return;           //discart the message
            }
            message.ReadUShort(); //internal head
            var cmd = message.ReadByte();

            switch (cmd)
            {
            case 0x01:
                ParseTile();
                break;

            default:
                Console.WriteLine("[Error] Unknown packet type " + cmd.ToString("X2"));
                break;
            }
        }
コード例 #14
0
        internal int SetDictionary(byte[] dictionary)
        {
            int start = 0;
            int n     = dictionary.Length;

            if (this.mode != 6)
            {
                throw new ZlibException("Stream error.");
            }
            if (Adler.Adler32(1L, dictionary, 0, dictionary.Length) != this._codec._Adler32)
            {
                return(-3);
            }
            this._codec._Adler32 = Adler.Adler32(0L, (byte[])null, 0, 0);
            if (n >= 1 << this.wbits)
            {
                n     = (1 << this.wbits) - 1;
                start = dictionary.Length - n;
            }
            this.blocks.SetDictionary(dictionary, start, n);
            this.mode = 7;
            return(0);
        }
コード例 #15
0
ファイル: InflateManager.cs プロジェクト: s-yi/StardewValley
        internal int SetDictionary(byte[] dictionary)
        {
            int index  = 0;
            int length = dictionary.Length;

            if (mode != InflateManagerMode.DICT0)
            {
                throw new ZlibException("Stream error.");
            }
            if (Adler.Adler32(1u, dictionary, 0, dictionary.Length) != _codec._Adler32)
            {
                return(-3);
            }
            _codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
            if (length >= 1 << wbits)
            {
                length = (1 << wbits) - 1;
                index  = dictionary.Length - length;
            }
            blocks.SetDictionary(dictionary, index, length);
            mode = InflateManagerMode.BLOCKS;
            return(0);
        }
コード例 #16
0
        internal int SetDictionary(byte[] dictionary)
        {
            int start = 0;
            int num   = dictionary.Length;

            if (mode != InflateManagerMode.DICT0)
            {
                throw new ZlibException("Stream error.");
            }
            if (Adler.Adler32(1u, dictionary, 0, dictionary.Length) != _codec._Adler32)
            {
                return(-3);
            }
            _codec._Adler32 = Adler.Adler32(0u, null, 0, 0);
            if (num >= 1 << wbits)
            {
                num   = (1 << wbits) - 1;
                start = dictionary.Length - num;
            }
            blocks.SetDictionary(dictionary, start, num);
            mode = InflateManagerMode.BLOCKS;
            return(0);
        }
コード例 #17
0
        public byte[] ToByteArray(uint HeaderKey)
        {
            byte[] data2;//0x7C
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryWriter bw = new BinaryWriter(ms);
                bw.Write(Check);
                bw.Write(this.StreamInfoCount);
                bw.Write(b);
                bw.Write(StreamInfosKey);
                bw.Write(c);
                bw.Write(d);
                bw.Write(new byte[75]);
                data2 = ms.ToArray();
            }
            uint Hash = Adler.Adler32(0, data2, 0, data2.Length);

            byte[] data1  = BitConverter.GetBytes(Hash);
            byte[] output = new byte[0x80];
            Array.Copy(data1, 0, output, 0, data1.Length);
            Array.Copy(data2, 0, output, data1.Length, data2.Length);
            output = Crypt.JMDCrypt.Decrypt(output, HeaderKey);
            return(output);
        }
コード例 #18
0
        /// <summary>
        /// Writes the given record to the PQDIF file.
        /// </summary>
        /// <param name="record">The record to be written to the file.</param>
        /// <param name="lastRecord">Indicates whether this record is the last record in the file.</param>
        /// <exception cref="InvalidDataException">The PQDIF data is invalid.</exception>
        /// <exception cref="ObjectDisposedException">The writer was disposed.</exception>
        public async Task WriteRecordAsync(Record record, bool lastRecord = false)
        {
            if (m_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            byte[] bodyImage;
            uint   checksum;

            using (MemoryStream bodyStream = new MemoryStream())
                using (BinaryWriter bodyWriter = new BinaryWriter(bodyStream))
                {
                    // Write the record body to the memory stream
                    if (record.Body != null)
                    {
                        WriteCollection(bodyWriter, record.Body.Collection);
                    }

                    // Read and compress the body to a byte array
                    bodyImage = bodyStream.ToArray();

                    if (m_compressionAlgorithm == CompressionAlgorithm.Zlib && m_compressionStyle == CompressionStyle.RecordLevel)
                    {
                        bodyImage = ZlibStream.CompressBuffer(bodyImage);
                    }

                    // Create the checksum after compression
                    uint adler = Adler.Adler32(0u, null, 0, 0);
                    checksum = Adler.Adler32(adler, bodyImage, 0, bodyImage.Length);

                    // Save the checksum in the record body
                    if (record.Body != null)
                    {
                        record.Body.Checksum = checksum;
                    }
                }

            // Make sure the header points to the correct location based on the size of the body
            record.Header.HeaderSize         = 64;
            record.Header.BodySize           = bodyImage.Length;
            record.Header.NextRecordPosition = (int)m_stream.Length + record.Header.HeaderSize + record.Header.BodySize;
            record.Header.Checksum           = checksum;

            using (MemoryStream headerStream = new MemoryStream())
                using (BinaryWriter headerWriter = new BinaryWriter(headerStream))
                {
                    // Write up to the next record position
                    headerWriter.Write(record.Header.RecordSignature.ToByteArray());
                    headerWriter.Write(record.Header.RecordTypeTag.ToByteArray());
                    headerWriter.Write(record.Header.HeaderSize);
                    headerWriter.Write(record.Header.BodySize);

                    // The PQDIF standard defines the NextRecordPosition to be 0 for the last record in the file
                    if (!lastRecord)
                    {
                        headerWriter.Write(record.Header.NextRecordPosition);
                    }
                    else
                    {
                        headerWriter.Write(0);
                    }

                    // Write the rest of the header as well as the body
                    headerWriter.Write(record.Header.Checksum);
                    headerWriter.Write(record.Header.Reserved);

                    byte[] headerImage = headerStream.ToArray();
                    await m_stream.WriteAsync(headerImage, 0, headerImage.Length);
                }

            await m_stream.WriteAsync(bodyImage, 0, bodyImage.Length);

            // Dispose of the writer if this is the last record
            if (!m_stream.CanSeek && lastRecord)
            {
                await DisposeAsync();
            }
        }
コード例 #19
0
        internal int Deflate(FlushType flushType_0)
        {
            if (this.zlibCodec_0.OutputBuffer != null && (this.zlibCodec_0.InputBuffer != null || this.zlibCodec_0.AvailableBytesIn == 0))
            {
                if (this.int_19 != Class0.int_5 || flushType_0 == FlushType.Finish)
                {
                    if (this.zlibCodec_0.AvailableBytesOut == 0)
                    {
                        this.zlibCodec_0.Message = Class0.string_0[7];
                        throw new ZlibException("OutputBuffer is full (AvailableBytesOut == 0)");
                    }
                    int num = this.int_22;
                    this.int_22 = (int)flushType_0;
                    if (this.int_19 == Class0.int_3)
                    {
                        int num2 = Class0.int_6 + (this.int_24 - 8 << 4) << 8;
                        int num3 = (this.compressionLevel_0 - CompressionLevel.BestSpeed & 255) >> 1;
                        if (num3 > 3)
                        {
                            num3 = 3;
                        }
                        num2 |= num3 << 6;
                        if (this.int_36 != 0)
                        {
                            num2 |= Class0.int_2;
                        }
                        num2       += 31 - num2 % 31;
                        this.int_19 = Class0.int_4;
                        this.byte_0[this.int_21++] = (byte)(num2 >> 8);
                        this.byte_0[this.int_21++] = (byte)num2;
                        if (this.int_36 != 0)
                        {
                            this.byte_0[this.int_21++] = (byte)((this.zlibCodec_0.uint_0 & 4278190080u) >> 24);
                            this.byte_0[this.int_21++] = (byte)((this.zlibCodec_0.uint_0 & 16711680u) >> 16);
                            this.byte_0[this.int_21++] = (byte)((this.zlibCodec_0.uint_0 & 65280u) >> 8);
                            this.byte_0[this.int_21++] = (byte)(this.zlibCodec_0.uint_0 & 255u);
                        }
                        this.zlibCodec_0.uint_0 = Adler.Adler32(0u, null, 0, 0);
                    }
                    if (this.int_21 != 0)
                    {
                        this.zlibCodec_0.method_1();
                        if (this.zlibCodec_0.AvailableBytesOut == 0)
                        {
                            this.int_22 = -1;
                            return(0);
                        }
                    }
                    else if (this.zlibCodec_0.AvailableBytesIn == 0 && flushType_0 <= (FlushType)num && flushType_0 != FlushType.Finish)
                    {
                        return(0);
                    }
                    if (this.int_19 == Class0.int_5 && this.zlibCodec_0.AvailableBytesIn != 0)
                    {
                        this.zlibCodec_0.Message = Class0.string_0[7];
                        throw new ZlibException("status == FINISH_STATE && _codec.AvailableBytesIn != 0");
                    }
                    if (this.zlibCodec_0.AvailableBytesIn != 0 || this.int_38 != 0 || (flushType_0 != FlushType.None && this.int_19 != Class0.int_5))
                    {
                        Enum0 @enum = this.delegate0_0(flushType_0);
                        if (@enum == Enum0.const_2 || @enum == Enum0.const_3)
                        {
                            this.int_19 = Class0.int_5;
                        }
                        if (@enum != Enum0.const_0)
                        {
                            if (@enum != Enum0.const_2)
                            {
                                if (@enum != Enum0.const_1)
                                {
                                    goto IL_31F;
                                }
                                if (flushType_0 == FlushType.Partial)
                                {
                                    this.method_11();
                                }
                                else
                                {
                                    this.method_20(0, 0, false);
                                    if (flushType_0 == FlushType.Full)
                                    {
                                        for (int i = 0; i < this.int_28; i++)
                                        {
                                            this.short_1[i] = 0;
                                        }
                                    }
                                }
                                this.zlibCodec_0.method_1();
                                if (this.zlibCodec_0.AvailableBytesOut == 0)
                                {
                                    this.int_22 = -1;
                                    return(0);
                                }
                                goto IL_31F;
                            }
                        }
                        if (this.zlibCodec_0.AvailableBytesOut == 0)
                        {
                            this.int_22 = -1;
                        }
                        return(0);
                    }
IL_31F:
                    if (flushType_0 != FlushType.Finish)
                    {
                        return(0);
                    }
                    if (!this.WantRfc1950HeaderBytes || this.bool_0)
                    {
                        return(1);
                    }
                    this.byte_0[this.int_21++] = (byte)((this.zlibCodec_0.uint_0 & 4278190080u) >> 24);
                    this.byte_0[this.int_21++] = (byte)((this.zlibCodec_0.uint_0 & 16711680u) >> 16);
                    this.byte_0[this.int_21++] = (byte)((this.zlibCodec_0.uint_0 & 65280u) >> 8);
                    this.byte_0[this.int_21++] = (byte)(this.zlibCodec_0.uint_0 & 255u);
                    this.zlibCodec_0.method_1();
                    this.bool_0 = true;
                    if (this.int_21 == 0)
                    {
                        return(1);
                    }
                    return(0);
                }
            }
            this.zlibCodec_0.Message = Class0.string_0[4];
            throw new ZlibException(string.Format("Something is fishy. [{0}]", this.zlibCodec_0.Message));
        }
コード例 #20
0
 public void TestAlder32()
 {
     Assert.Equal("152D040A", Adler.adler_32Hash(stringToByteArray("HelloKitty")));
 }
コード例 #21
0
        // These two data types are supported in DotNetZip, but only if .NET Framework is targeted.
        //private SelfExtractorFlavor _selfExtractorFlavor;
        //private SelfExtractorSaveOptions _selfExtractorSaveOptions;

        public void CallAll()
        {
            // These two apis are supported in DotNetZip, but only if .NET Framework is targeted.
            //_zipFile.SaveSelfExtractor(_string, _selfExtractorFlavor);
            //_zipFile.SaveSelfExtractor(_string, _selfExtractorSaveOptions);

            //Project: Ionic.Zip
            _bZip2InputStream.Close();
            _bZip2InputStream.Flush();
            _int  = _bZip2InputStream.Read(_bytes, _int, _int);
            _int  = _bZip2InputStream.ReadByte();
            _long = _bZip2InputStream.Seek(_long, _seekOrigin);
            _bZip2InputStream.SetLength(_long);
            _bZip2InputStream.Write(_bytes, _int, _int);
            _bZip2OutputStream.Close();
            _bZip2OutputStream.Flush();
            _int  = _bZip2OutputStream.Read(_bytes, _int, _int);
            _long = _bZip2OutputStream.Seek(_long, _seekOrigin);
            _bZip2OutputStream.SetLength(_long);
            _bZip2OutputStream.Write(_bytes, _int, _int);
            _parallelBZip2OutputStream.Close();
            _parallelBZip2OutputStream.Flush();
            _int  = _parallelBZip2OutputStream.Read(_bytes, _int, _int);
            _long = _parallelBZip2OutputStream.Seek(_long, _seekOrigin);
            _parallelBZip2OutputStream.SetLength(_long);
            _parallelBZip2OutputStream.Write(_bytes, _int, _int);
            _crc32.Combine(_int, _int);
            _int = _crc32.ComputeCrc32(_int, _byte);
            _int = _crc32.GetCrc32(_stream);
            _int = _crc32.GetCrc32AndCopy(_stream, _stream);
            _crc32.Reset();
            _crc32.SlurpBlock(_bytes, _int, _int);
            _crc32.UpdateCRC(_byte);
            _crc32.UpdateCRC(_byte, _int);
            _crcCalculatorStream.Close();
            _crcCalculatorStream.Flush();
            _int  = _crcCalculatorStream.Read(_bytes, _int, _int);
            _long = _crcCalculatorStream.Seek(_long, _seekOrigin);
            _crcCalculatorStream.SetLength(_long);
            _crcCalculatorStream.Write(_bytes, _int, _int);
            _zipEntriesCollection = _fileSelector.SelectEntries(_zipFile);
            _zipEntriesCollection = _fileSelector.SelectEntries(_zipFile, _string);
            _stringsCollection    = _fileSelector.SelectFiles(_string);
            _stringsReadOnly      = _fileSelector.SelectFiles(_string, _bool);
            _string = _fileSelector.ToString();
            _bool   = _comHelper.CheckZip(_string);
            _bool   = _comHelper.CheckZipPassword(_string, _string);
            _comHelper.FixZipDirectory(_string);
            _string = _comHelper.GetZipLibraryVersion();
            _bool   = _comHelper.IsZipFile(_string);
            _bool   = _comHelper.IsZipFileWithExtract(_string);
            _countingStream.Adjust(_long);
            _countingStream.Flush();
            _int  = _countingStream.Read(_bytes, _int, _int);
            _long = _countingStream.Seek(_long, _seekOrigin);
            _countingStream.SetLength(_long);
            _countingStream.Write(_bytes, _int, _int);
            _zipEntry.Extract();
            _zipEntry.Extract(_extractExistingFileAction);
            _zipEntry.Extract(_string);
            _zipEntry.Extract(_string, _extractExistingFileAction);
            _zipEntry.Extract(_stream);
            _zipEntry.ExtractWithPassword(_extractExistingFileAction, _string);
            _zipEntry.ExtractWithPassword(_string);
            _zipEntry.ExtractWithPassword(_string, _extractExistingFileAction, _string);
            _zipEntry.ExtractWithPassword(_string, _string);
            _zipEntry.ExtractWithPassword(_stream, _string);
            _crcCalculatorStream = _zipEntry.OpenReader();
            _crcCalculatorStream = _zipEntry.OpenReader(_string);
            _zipEntry.SetEntryTimes(_datetime, _datetime, _datetime);
            _string   = _zipEntry.ToString();
            _zipEntry = _zipFile.AddDirectory(_string);
            _zipEntry = _zipFile.AddDirectory(_string, _string);
            _zipEntry = _zipFile.AddDirectoryByName(_string);
            _zipEntry = _zipFile.AddEntry(_string, _bytes);
            _zipEntry = _zipFile.AddEntry(_string, _openDelegate, _closeDelegate);
            _zipEntry = _zipFile.AddEntry(_string, _writeDelegate);
            _zipEntry = _zipFile.AddEntry(_string, _string);
            _zipEntry = _zipFile.AddEntry(_string, _string, _encoding);
            _zipEntry = _zipFile.AddEntry(_string, _stream);
            _zipEntry = _zipFile.AddFile(_string);
            _zipEntry = _zipFile.AddFile(_string, _string);
            _zipFile.AddFiles(_strings);
            _zipFile.AddFiles(_strings, _bool, _string);
            _zipFile.AddFiles(_strings, _string);
            _zipEntry = _zipFile.AddItem(_string);
            _zipEntry = _zipFile.AddItem(_string, _string);
            _zipFile.AddSelectedFiles(_string);
            _zipFile.AddSelectedFiles(_string, _bool);
            _zipFile.AddSelectedFiles(_string, _string);
            _zipFile.AddSelectedFiles(_string, _string, _bool);
            _zipFile.AddSelectedFiles(_string, _string, _string);
            _zipFile.AddSelectedFiles(_string, _string, _string, _bool);
            _bool = _zipFile.ContainsEntry(_string);
            _zipFile.Dispose();
            _zipFile.ExtractAll(_string);
            _zipFile.ExtractAll(_string, _extractExistingFileAction);
            _zipFile.ExtractSelectedEntries(_string);
            _zipFile.ExtractSelectedEntries(_string, _extractExistingFileAction);
            _zipFile.ExtractSelectedEntries(_string, _string);
            _zipFile.ExtractSelectedEntries(_string, _string, _string);
            _zipFile.ExtractSelectedEntries(_string, _string, _string, _extractExistingFileAction);
            _enumerator = _zipFile.GetNewEnum();
            _zipFile.Initialize(_string);
            _zipFile.RemoveEntries(_zipEntriesCollection);
            _zipFile.RemoveEntries(_stringsCollection);
            _zipFile.RemoveEntry(_zipEntry);
            _zipFile.RemoveEntry(_string);
            _int = _zipFile.RemoveSelectedEntries(_string);
            _int = _zipFile.RemoveSelectedEntries(_string, _string);
            _zipFile.Save();
            _zipFile.Save(_string);
            _zipFile.Save(_stream);
            _zipEntriesCollection = _zipFile.SelectEntries(_string);
            _zipEntriesCollection = _zipFile.SelectEntries(_string, _string);
            _string   = _zipFile.ToString();
            _zipEntry = _zipFile.UpdateDirectory(_string);
            _zipEntry = _zipFile.UpdateDirectory(_string, _string);
            _zipEntry = _zipFile.UpdateEntry(_string, _bytes);
            _zipEntry = _zipFile.UpdateEntry(_string, _openDelegate, _closeDelegate);
            _zipEntry = _zipFile.UpdateEntry(_string, _writeDelegate);
            _zipEntry = _zipFile.UpdateEntry(_string, _string);
            _zipEntry = _zipFile.UpdateEntry(_string, _string, _encoding);
            _zipEntry = _zipFile.UpdateEntry(_string, _stream);
            _zipEntry = _zipFile.UpdateFile(_string);
            _zipFile.UpdateFile(_string, _string);
            _zipFile.UpdateFiles(_strings);
            _zipFile.UpdateFiles(_strings, _string);
            _zipFile.UpdateItem(_string);
            _zipFile.UpdateItem(_string, _string);
            _zipFile.UpdateSelectedFiles(_string, _string, _string, _bool);
            _zipInputStream.Flush();
            _zipEntry = _zipInputStream.GetNextEntry();
            _int      = _zipInputStream.Read(_bytes, _int, _int);
            _long     = _zipInputStream.Seek(_long, _seekOrigin);
            _zipInputStream.SetLength(_long);
            _string = _zipInputStream.ToString();
            _zipInputStream.Write(_bytes, _int, _int);
            _bool = _zipOutputStream.ContainsEntry(_string);
            _zipOutputStream.Flush();
            _zipEntry = _zipOutputStream.PutNextEntry(_string);
            _int      = _zipOutputStream.Read(_bytes, _int, _int);
            _long     = _zipOutputStream.Seek(_long, _seekOrigin);
            _zipOutputStream.SetLength(_long);
            _string = _zipOutputStream.ToString();
            _zipOutputStream.Write(_bytes, _int, _int);
            _deflateStream.Flush();
            _int  = _deflateStream.Read(_bytes, _int, _int);
            _long = _deflateStream.Seek(_long, _seekOrigin);
            _deflateStream.SetLength(_long);
            _deflateStream.Write(_bytes, _int, _int);
            _gZipStream.Flush();
            _int  = _gZipStream.Read(_bytes, _int, _int);
            _long = _gZipStream.Seek(_long, _seekOrigin);
            _gZipStream.SetLength(_long);
            _gZipStream.Write(_bytes, _int, _int);
            _parallelDeflateOutputStream.Close();
            _parallelDeflateOutputStream.Flush();
            _int = _parallelDeflateOutputStream.Read(_bytes, _int, _int);
            _parallelDeflateOutputStream.Reset(_stream);
            _long = _parallelDeflateOutputStream.Seek(_long, _seekOrigin);
            _parallelDeflateOutputStream.SetLength(_long);
            _parallelDeflateOutputStream.Write(_bytes, _int, _int);

            // Static
            _bool = ZipFile.CheckZip(_string);
            _bool = ZipFile.CheckZip(_string, _bool, _textWriter);
            _bool = ZipFile.CheckZipPassword(_string, _string);
            ZipFile.FixZipDirectory(_string);
            _bool    = ZipFile.IsZipFile(_string);
            _bool    = ZipFile.IsZipFile(_string, _bool);
            _bool    = ZipFile.IsZipFile(_stream, _bool);
            _zipFile = ZipFile.Read(_string);
            _zipFile = ZipFile.Read(_string, _readOptions);
            _zipFile = ZipFile.Read(_stream);
            _zipFile = ZipFile.Read(_stream, _readOptions);
            _uint    = Adler.Adler32(_uint, _bytes, _int, _int);
            _bytes   = DeflateStream.CompressBuffer(_bytes);
            _bytes   = DeflateStream.CompressString(_string);
            _bytes   = DeflateStream.UncompressBuffer(_bytes);
            _string  = DeflateStream.UncompressString(_bytes);
            _bytes   = GZipStream.CompressBuffer(_bytes);
            _bytes   = GZipStream.CompressString(_string);
            _bytes   = GZipStream.UncompressBuffer(_bytes);
            _string  = GZipStream.UncompressString(_bytes);
            _bytes   = ZlibStream.CompressBuffer(_bytes);
            _bytes   = ZlibStream.CompressString(_string);
            _bytes   = ZlibStream.UncompressBuffer(_bytes);
            _string  = ZlibStream.UncompressString(_bytes);
        }
コード例 #22
0
 /// <summary>
 /// 用於解碼第二部分之金鑰
 /// </summary>
 /// <param name="FileName">不含副檔名的檔名</param>
 /// <returns></returns>
 public static uint GetHeaderKey(string FileName)
 {
     byte[] stringData = Encoding.GetEncoding("UTF-16").GetBytes(FileName);
     return(Adler.Adler32(0, stringData, 0, stringData.Length) - 0xa6ee7565);
 }
コード例 #23
0
ファイル: PhysicalWriter.cs プロジェクト: zx9506/pqdif
        /// <summary>
        /// Writes the given record to the PQDIF file.
        /// </summary>
        /// <param name="record">The record to be written to the file.</param>
        /// <param name="lastRecord">Indicates whether this record is the last record in the file.</param>
        /// <exception cref="InvalidDataException">The PQDIF data is invalid.</exception>
        /// <exception cref="ObjectDisposedException">The writer was disposed.</exception>
        public void WriteRecord(Record record, bool lastRecord = false)
        {
            byte[] bodyImage;
            uint   checksum;

            if (m_disposed)
            {
                throw new ObjectDisposedException(GetType().Name);
            }

            using (MemoryStream bodyStream = new MemoryStream())
                using (BinaryWriter bodyWriter = new BinaryWriter(bodyStream))
                {
                    // Write the record body to the memory stream
                    if (record.Body != null)
                    {
                        WriteCollection(bodyWriter, record.Body.Collection);
                    }

                    // Read and compress the body to a byte array
                    bodyImage = bodyStream.ToArray();

                    if (m_compressionAlgorithm == CompressionAlgorithm.Zlib && m_compressionStyle == CompressionStyle.RecordLevel)
                    {
                        bodyImage = ZlibStream.CompressBuffer(bodyImage);
                    }

                    // Create the checksum after compression
                    uint adler = Adler.Adler32(0u, null, 0, 0);
                    checksum = Adler.Adler32(adler, bodyImage, 0, bodyImage.Length);

                    // Write the record body to the memory stream
                    if (record.Body != null)
                    {
                        record.Body.Checksum = checksum;
                    }
                }

            // Fix the pointer to the next
            // record before writing this record
            if (m_stream.CanSeek && m_stream.Length > 0)
            {
                m_writer.Write((int)m_stream.Length);
                m_stream.Seek(0L, SeekOrigin.End);
            }

            // Make sure the header points to the correct location based on the size of the body
            record.Header.HeaderSize         = 64;
            record.Header.BodySize           = bodyImage.Length;
            record.Header.NextRecordPosition = (int)m_stream.Length + record.Header.HeaderSize + record.Header.BodySize;
            record.Header.Checksum           = checksum;

            // Write up to the next record position
            m_writer.Write(record.Header.RecordSignature.ToByteArray());
            m_writer.Write(record.Header.RecordTypeTag.ToByteArray());
            m_writer.Write(record.Header.HeaderSize);
            m_writer.Write(record.Header.BodySize);

            // The PQDIF standard defines the NextRecordPosition to be 0 for the last record in the file
            // We treat seekable streams differently because we can go back and fix the pointers later
            if (m_stream.CanSeek || lastRecord)
            {
                m_writer.Write(0);
            }
            else
            {
                m_writer.Write(record.Header.NextRecordPosition);
            }

            // Write the rest of the header as well as the body
            m_writer.Write(record.Header.Checksum);
            m_writer.Write(record.Header.Reserved);
            m_writer.Write(bodyImage);

            // If the stream is seekable, seek to the next record
            // position so we can fix the pointer if we end up
            // writing another record to the file
            if (m_stream.CanSeek)
            {
                m_stream.Seek(-(24 + record.Header.BodySize), SeekOrigin.Current);
            }

            // Dispose of the writer if this is the last record
            if (!m_stream.CanSeek && lastRecord)
            {
                Dispose();
            }
        }
コード例 #24
0
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("usage: packinfo <filename>");
                return;
            }
            string fn = args[0];

            data = File.ReadAllBytes(fn);

            string magic1 = "PACK";

            for (int i = 0; i < 4; i++)
            {
                if (data[offset + i] != magic1[i])
                {
                    Console.WriteLine("invalid file signature (PACK).");
                    return;
                }
            }
            Console.WriteLine("file signature read successfully.");
            offset += 4;

            Console.WriteLine("read version as {0}.", readInt());

            int objCount = readInt();

            Console.WriteLine("this pack contains {0} objects.", objCount);

            Console.WriteLine("next byte: {0}", data[offset]);

            for (int i = 0; i < objCount; i++)
            {
                Console.WriteLine();
                Console.WriteLine("-- object {0} --", i);
                long  len;
                types type;
                type = (types)((byte)(data[offset] << 1) >> 5);
                len  = (byte)(data[offset] << 4) >> 4;
                if (data[offset] < 128)
                {
                    offset++;
                }
                else
                {
                    int j = 0;
                    do
                    {
                        j++;
                        int lsh = 7;
                        if (j == 1)
                        {
                            lsh = 4;
                        }
                        len |= (data[offset + j] & 0x7f) << lsh;
                    } while (data[offset + j] >= 128);
                    offset += (j + 1);
                }
                Console.WriteLine("expected len {0}", len);

                Console.WriteLine("offset is now at {0}, begin reading object.", offset);

                MemoryStream s  = new MemoryStream(data, offset, data.Length - offset - 10);
                ZlibStream   zs = new ZlibStream(s, CompressionMode.Decompress);

                Array.Clear(buf, 0, 2000);
                try
                {
                    rd = zs.Read(buf, 0, 2000);
                } catch (Exception ex)
                {
                    Console.WriteLine("encountered an exception while inflating: {0}", ex.Message);
                    for (; rd < 2000 && buf[rd] != 0; rd++)
                    {
                    }
                }

                if (rd != len)
                {
                    Console.WriteLine("expected and inflated lengths do not match (expected: {0}, got: {1})", len, rd);
                }

                Console.WriteLine("deflated {0} bytes.", rd);
                Console.WriteLine("{0} {1}", type, rd);

                switch (type)
                {
                case types.blob:
                    for (int k = 0; k < rd; k++)
                    {
                        Console.Write("{0:X} ", buf[k]);
                    }
                    Console.WriteLine();
                    break;

                case types.commit:
                    string actual = Encoding.ASCII.GetString(buf, 0, rd);
                    Console.WriteLine(actual);
                    break;

                case types.tree:
                    int    ci = 0;
                    string mode, name, sha;
                    while (true)
                    {
                        if (ci >= rd)
                        {
                            break;
                        }
                        mode = ""; name = ""; sha = "";
                        while (buf[ci] == 0 || buf[ci] == 0x20)
                        {
                            ci++;
                        }
                        while (buf[ci] >= '0' && buf[ci] <= '9')
                        {
                            mode += (char)buf[ci];
                            ci++;
                        }
                        Console.Write("mode: {0}\t", mode);
                        if (buf[ci] != ' ')
                        {
                            Console.WriteLine("bad");
                        }
                        ci++;
                        while (buf[ci] != 0)
                        {
                            name += (char)buf[ci];
                            ci++;
                        }
                        Console.Write("name: {0}\t\t", name);
                        ci++;
                        sha = BitConverter.ToString(buf, ci, 20).Replace("-", "").ToLower();
                        Console.WriteLine("sha: {0}", sha);
                        ci += 20;
                    }
                    break;

                default:
                    Console.WriteLine(BitConverter.ToString(buf, 0, rd).Replace("-", "").ToLower());
                    break;
                }



                Console.WriteLine("searching for next object...");
                uint cs = Adler.Adler32(1, buf, 0, rd);
                Console.WriteLine("checksum of previous object = {0:X}", cs);


                for (; offset < data.Length - 4; offset++)
                {
                    if (data[offset - 4] == ((cs & 0xff000000) >> 24) && data[offset - 3] == ((cs & 0x00ff0000) >> 16) &&
                        data[offset - 2] == ((cs & 0x0000ff00) >> 8) && data[offset - 1] == ((cs & 0x000000ff)))
                    {
                        Console.WriteLine("found good checksum at offset {0}!", offset - 4);
                        break;
                    }
                }
                Console.WriteLine("next object header is most likely to be {0}.", offset);

                Console.WriteLine("-- end object {0} --", i);
            }

            Console.WriteLine("{0} bytes remain.", data.Length - offset);
            if (data.Length - offset != 20)
            {
                Console.WriteLine("this is not enough space for a sha1 - is your object corrupt?");
            }

            SHA1Managed hash = new SHA1Managed();

            byte[] hashed = hash.ComputeHash(data, 0, offset);
            Console.WriteLine("computed hash: {0}", BitConverter.ToString(hashed).Replace("-", "").ToLower());
            Console.WriteLine("found hash: {0}", BitConverter.ToString(data, offset, 20).Replace("-", "").ToLower());

            // check values
            bool ok = true;

            for (int i = 0; i < 20; i++)
            {
                ok &= (data[offset + i] == hashed[i]);
            }
            if (!ok)
            {
                Console.WriteLine("bad hash: hash does not match expected value.");
            }
            else
            {
                Console.WriteLine("hash checks out.");
            }

            Console.WriteLine("done, press enter to exit.");
            Console.ReadLine();
        }