Crc32 implementation.
Exemple #1
0
        // Read the gzip header from the base stream.
        private void ReadHeader()
        {
            Crc32 headerCrc = new Crc32();
            int   magic1, magic2, flags, temp;

            // Validate the magic number.
            magic1 = ReadByte(headerCrc);
            if (magic1 < 0)
            {
                // The stream is empty, so just report EOF and exit.
                endOfStream = true;
                return;
            }
            magic2 = ReadByte(headerCrc);
            if (magic2 < 0 ||
                ((magic1 << 8) + magic2) != GZipConstants.GZIP_MAGIC)
            {
                // The magic number does not match.
                throw new IOException(S._("IO_Decompress_GzipHeader"));
            }

            // Check the compression type, which must be 8.
            if (ReadByte(headerCrc) != 0x08)
            {
                throw new IOException(S._("IO_Decompress_GzipHeader"));
            }

            // Read the flags.
            flags = ReadByte(headerCrc);
            if ((flags & 0xE0) != 0)
            {
                throw new IOException(S._("IO_Decompress_GzipHeader"));
            }

            // Skip the modification time, extra flags, and OS type.
            for (temp = 0; temp < 6; ++temp)
            {
                ReadByteThrow(headerCrc);
            }

            // Skip the "extra" field.
            if ((flags & GZipConstants.FEXTRA) != 0)
            {
                // Get the length of the extra field.
                temp  = ReadByteThrow(headerCrc);
                temp += (ReadByteThrow(headerCrc) << 8);

                // Skip the extra field.
                while (temp > 0)
                {
                    ReadByteThrow(headerCrc);
                    --temp;
                }
            }

            // Skip the "filename" field.
            if ((flags & GZipConstants.FNAME) != 0)
            {
                do
                {
                    temp = ReadByteThrow(headerCrc);
                }while(temp != 0);
            }

            // Skip the "comment" field.
            if ((flags & GZipConstants.FCOMMENT) != 0)
            {
                do
                {
                    temp = ReadByteThrow(headerCrc);
                }while(temp != 0);
            }

            // Read the header checksum.
            if ((flags & GZipConstants.FHCRC) != 0)
            {
                magic1 = stream.ReadByte();
                if (magic1 < 0)
                {
                    throw new IOException(S._("IO_Decompress_GzipHeader"));
                }
                temp = stream.ReadByte();
                if (temp < 0)
                {
                    throw new IOException(S._("IO_Decompress_GzipHeader"));
                }
                temp = magic1 + (temp << 8);
                if (temp != (((int)(headerCrc.Value)) & 0xFFFF))
                {
                    throw new IOException(S._("IO_Decompress_GzipHeader"));
                }
            }

            // The header has been read, and we are ready to go.
            headerDone = true;
        }