Beispiel #1
0
        /// <summary>
        /// Reads next metablock header.
        /// </summary>
        /// <param name="state">decoding state</param>
        public static void ReadMeablockInfo(State state)
        {
            BitReader br = state.br;

            if (state.inputEnd)
            {
                state.nextRunningState = RunningStage.FINISHED;
                state.bytesToWrite     = state.pos & (state.ringBufferSize - 1);
                state.bytesWritten     = 0;
                state.runningState     = RunningStage.WRITE;
                return;
            }
            // TODO: Reset? Do we need this?
            state.hGroup0.codes = null;
            state.hGroup0.trees = null;
            state.hGroup1.codes = null;
            state.hGroup1.trees = null;
            state.hGroup2.codes = null;
            state.hGroup2.trees = null;

            BitReader.ReadMoreInput(br);
            DecodeMetaBlockLength(br, state);
            if (state.metaBlockLength == 0 && !state.isMetadata)
            {
                return;
            }
            if (state.isUncompressed || state.isMetadata)
            {
                BitReader.JumpToByteBoundry(br);
                state.runningState = state.isMetadata ? RunningStage.READ_METADATA : RunningStage.COPY_UNCOMPRESSED;
            }
            else
            {
                state.runningState = RunningStage.COMPRESSED_BLOCK_START;
            }

            if (state.isMetadata)
            {
                return;
            }
            state.expectedTotalSize += state.metaBlockLength;
            if (state.ringBufferSize < state.maxRingBufferSize)
            {
                MaybeReallocateRingBuffer(state);
            }
        }
Beispiel #2
0
        public static void Decompress(State state)
        {
            if (state.runningState == RunningStage.UNINITIALIZED)
            {
                throw new InvalidOperationException("Can't decompress until initialized");
            }
            if (state.runningState == RunningStage.CLOSED)
            {
                throw new InvalidOperationException("Can't decompress after close");
            }
            BitReader br             = state.br;
            int       ringBufferMask = state.ringBufferSize - 1;

            byte[] ringBuffer = state.ringBuffer;

            while (state.runningState != RunningStage.FINISHED)
            {
                // TODO: extract cases to methods for the better readability.
                switch (state.runningState)
                {
                case RunningStage.BLOCK_START:
                    if (state.metaBlockLength < 0)
                    {
                        throw new BrotliRuntimeException("Invalid metablock length");
                    }
                    ReadMeablockInfo(state);
                    /* Ring-buffer would be reallocated here. */
                    ringBufferMask = state.ringBufferSize - 1;
                    ringBuffer     = state.ringBuffer;
                    continue;

                case RunningStage.COMPRESSED_BLOCK_START:
                    ReadMetablockHuffmanCodesAndContextMaps(state);
                    state.runningState = RunningStage.MAIN_LOOP;
                    goto case RunningStage.MAIN_LOOP;
                // Fall through

                case RunningStage.MAIN_LOOP:
                    if (state.metaBlockLength <= 0)
                    {
                        // Protect pos from overflow, wrap it around at every GB of input data.
                        state.pos         &= 0x3fffffff;
                        state.runningState = RunningStage.BLOCK_START;
                        continue;
                    }
                    BitReader.ReadMoreInput(br);
                    if (state.blockLength[1] == 0)
                    {
                        DecodeCommandBlockSwitch(state);
                    }
                    state.blockLength[1]--;
                    int cmdCode  = ReadSymbol(state.hGroup1.codes, state.treeCommandOffset, br);
                    int rangeIdx = (int)((uint)cmdCode >> 6);
                    state.distanceCode = 0;
                    if (rangeIdx >= 2)
                    {
                        rangeIdx          -= 2;
                        state.distanceCode = -1;
                    }
                    int insertCode = Prefix.INSERT_RANGE_LUT[rangeIdx] + ((int)((uint)cmdCode >> 3) & 7);
                    int copyCode   = Prefix.COPY_RANGE_LUT[rangeIdx] + (cmdCode & 7);
                    state.insertLength = Prefix.INSERT_LENGTH_OFFSET[insertCode] + BitReader
                                         .ReadBits(br, Prefix.INSERT_LENGTH_N_BITS[insertCode]);
                    state.copyLength = Prefix.COPY_LENGTH_OFFSET[copyCode] + BitReader
                                       .ReadBits(br, Prefix.COPY_LENGTH_N_BITS[copyCode]);

                    state.j            = 0;
                    state.runningState = RunningStage.INSERT_LOOP;
                    goto case RunningStage.INSERT_LOOP;

                // Fall through
                case RunningStage.INSERT_LOOP:
                    if (state.trivialLiteralContext)
                    {
                        while (state.j < state.insertLength)
                        {
                            BitReader.ReadMoreInput(br);
                            if (state.blockLength[0] == 0)
                            {
                                DecodeLiteralBlockSwitch(state);
                            }
                            state.blockLength[0]--;
                            ringBuffer[state.pos & ringBufferMask] = (byte)ReadSymbol(
                                state.hGroup0.codes, state.literalTree, br);
                            state.j++;
                            if ((state.pos++ & ringBufferMask) == ringBufferMask)
                            {
                                state.nextRunningState = RunningStage.INSERT_LOOP;
                                state.bytesToWrite     = state.ringBufferSize;
                                state.bytesWritten     = 0;
                                state.runningState     = RunningStage.WRITE;
                                break;
                            }
                        }
                    }
                    else
                    {
                        int prevByte1 = ringBuffer[(state.pos - 1) & ringBufferMask] & 0xFF;
                        int prevByte2 = ringBuffer[(state.pos - 2) & ringBufferMask] & 0xFF;
                        while (state.j < state.insertLength)
                        {
                            BitReader.ReadMoreInput(br);
                            if (state.blockLength[0] == 0)
                            {
                                DecodeLiteralBlockSwitch(state);
                            }
                            int literalTreeIndex = state.contextMap[state.contextMapSlice
                                                                    + (Context.LOOKUP[state.contextLookupOffset1 + prevByte1]
                                                                       | Context.LOOKUP[state.contextLookupOffset2 + prevByte2])] & 0xFF;
                            state.blockLength[0]--;
                            prevByte2 = prevByte1;
                            prevByte1 = ReadSymbol(
                                state.hGroup0.codes, state.hGroup0.trees[literalTreeIndex], br);
                            ringBuffer[state.pos & ringBufferMask] = (byte)prevByte1;
                            state.j++;
                            if ((state.pos++ & ringBufferMask) == ringBufferMask)
                            {
                                state.nextRunningState = RunningStage.INSERT_LOOP;
                                state.bytesToWrite     = state.ringBufferSize;
                                state.bytesWritten     = 0;
                                state.runningState     = RunningStage.WRITE;
                                break;
                            }
                        }
                    }
                    if (state.runningState != RunningStage.INSERT_LOOP)
                    {
                        continue;
                    }
                    state.metaBlockLength -= state.insertLength;
                    if (state.metaBlockLength <= 0)
                    {
                        state.runningState = RunningStage.MAIN_LOOP;
                        continue;
                    }
                    if (state.distanceCode < 0)
                    {
                        BitReader.ReadMoreInput(br);
                        if (state.blockLength[2] == 0)
                        {
                            DecodeDistanceBlockSwitch(state);
                        }
                        state.blockLength[2]--;
                        state.distanceCode = ReadSymbol(state.hGroup2.codes, state.hGroup2.trees[
                                                            state.distContextMap[state.distContextMapSlice
                                                                                 + (state.copyLength > 4 ? 3 : state.copyLength - 2)] & 0xFF], br);
                        if (state.distanceCode >= state.numDirectDistanceCodes)
                        {
                            state.distanceCode -= state.numDirectDistanceCodes;
                            int postfix = state.distanceCode & state.distancePostfixMask;
                            //state.distanceCode >>>= state.distancePostfixBits;
                            state.distanceCode = (int)((uint)state.distanceCode >> state.distancePostfixBits);
                            int n      = (int)((uint)state.distanceCode >> 1) + 1;
                            int offset = ((2 + (state.distanceCode & 1)) << n) - 4;
                            state.distanceCode = state.numDirectDistanceCodes + postfix
                                                 + ((offset + BitReader.ReadBits(br, n)) << state.distancePostfixBits);
                        }
                    }

                    // Convert the distance code to the actual distance by possibly looking up past distances
                    // from the ringBuffer.
                    state.distance = TranslateShortCodes(state.distanceCode, state.distRb, state.distRbIdx);
                    if (state.distance < 0)
                    {
                        throw new BrotliRuntimeException("Negative distance");     // COV_NF_LINE
                    }

                    if (state.pos < state.maxBackwardDistance &&
                        state.maxDistance != state.maxBackwardDistance)
                    {
                        state.maxDistance = state.pos;
                    }
                    else
                    {
                        state.maxDistance = state.maxBackwardDistance;
                    }

                    state.copyDst = state.pos & ringBufferMask;
                    if (state.distance > state.maxDistance)
                    {
                        state.runningState = RunningStage.TRANSFORM;
                        continue;
                    }

                    if (state.distanceCode > 0)
                    {
                        state.distRb[state.distRbIdx & 3] = state.distance;
                        state.distRbIdx++;
                    }

                    if (state.copyLength > state.metaBlockLength)
                    {
                        throw new BrotliRuntimeException("Invalid backward reference");     // COV_NF_LINE
                    }
                    state.j            = 0;
                    state.runningState = RunningStage.COPY_LOOP;
                    goto case RunningStage.COPY_LOOP;

                // fall through
                case RunningStage.COPY_LOOP:
                    for (; state.j < state.copyLength;)
                    {
                        ringBuffer[state.pos & ringBufferMask] =
                            ringBuffer[(state.pos - state.distance) & ringBufferMask];
                        // TODO: condense
                        state.metaBlockLength--;
                        state.j++;
                        if ((state.pos++ & ringBufferMask) == ringBufferMask)
                        {
                            state.nextRunningState = RunningStage.COPY_LOOP;
                            state.bytesToWrite     = state.ringBufferSize;
                            state.bytesWritten     = 0;
                            state.runningState     = RunningStage.WRITE;
                            break;
                        }
                    }
                    if (state.runningState == RunningStage.COPY_LOOP)
                    {
                        state.runningState = RunningStage.MAIN_LOOP;
                    }
                    continue;

                case RunningStage.TRANSFORM:
                    if (state.copyLength >= Dictionary.MIN_WORD_LENGTH &&
                        state.copyLength <= Dictionary.MAX_WORD_LENGTH)
                    {
                        int offset       = Dictionary.OFFSETS_BY_LENGTH[state.copyLength];
                        int wordId       = state.distance - state.maxDistance - 1;
                        int shift        = Dictionary.SIZE_BITS_BY_LENGTH[state.copyLength];
                        int mask         = (1 << shift) - 1;
                        int wordIdx      = wordId & mask;
                        int transformIdx = (int)((uint)wordId >> shift);
                        offset += wordIdx * state.copyLength;
                        if (transformIdx < Transform.TRANSFORMS.Length)
                        {
                            int len = Transform.TransformDictionaryWord(ringBuffer, state.copyDst,
                                                                        Dictionary.GetData(), offset, state.copyLength,
                                                                        Transform.TRANSFORMS[transformIdx]);
                            state.copyDst         += len;
                            state.pos             += len;
                            state.metaBlockLength -= len;
                            if (state.copyDst >= state.ringBufferSize)
                            {
                                state.nextRunningState = RunningStage.COPY_WRAP_BUFFER;
                                state.bytesToWrite     = state.ringBufferSize;
                                state.bytesWritten     = 0;
                                state.runningState     = RunningStage.WRITE;
                                continue;
                            }
                        }
                        else
                        {
                            throw new BrotliRuntimeException("Invalid backward reference");     // COV_NF_LINE
                        }
                    }
                    else
                    {
                        throw new BrotliRuntimeException("Invalid backward reference");     // COV_NF_LINE
                    }
                    state.runningState = RunningStage.MAIN_LOOP;
                    continue;

                case RunningStage.COPY_WRAP_BUFFER:
                    Array.Copy(ringBuffer, state.ringBufferSize, ringBuffer, 0,
                               state.copyDst - state.ringBufferSize);
                    state.runningState = RunningStage.MAIN_LOOP;
                    continue;

                case RunningStage.READ_METADATA:
                    while (state.metaBlockLength > 0)
                    {
                        BitReader.ReadMoreInput(br);
                        // Optimize
                        BitReader.ReadBits(br, 8);
                        state.metaBlockLength--;
                    }
                    state.runningState = RunningStage.BLOCK_START;
                    continue;


                case RunningStage.COPY_UNCOMPRESSED:
                    CopyUncompressedData(state);
                    continue;

                case RunningStage.WRITE:
                    if (!WriteRingBuffer(state))
                    {
                        // Output buffer is full.
                        return;
                    }
                    state.runningState = state.nextRunningState;
                    continue;

                default:
                    throw new BrotliRuntimeException("Unexpected state " + state.runningState);
                }
            }
            if (state.runningState == RunningStage.FINISHED)
            {
                if (state.metaBlockLength < 0)
                {
                    throw new BrotliRuntimeException("Invalid metablock length");
                }
                BitReader.JumpToByteBoundry(br);
                BitReader.CheckHealth(state.br);
            }
        }