Exemple #1
0
 internal static void Reload(iText.IO.Codec.Brotli.Dec.BitReader br)
 {
     if (br.bitOffset == 64)
     {
         Prepare(br);
     }
 }
Exemple #2
0
        private static void DecodeBlockTypeAndLength(iText.IO.Codec.Brotli.Dec.State state, int treeType)
        {
            iText.IO.Codec.Brotli.Dec.BitReader br = state.br;
            int[] ringBuffers = state.blockTypeRb;
            int   offset      = treeType * 2;

            iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
            int blockType = ReadSymbol(state.blockTypeTrees, treeType * iText.IO.Codec.Brotli.Dec.Huffman.HuffmanMaxTableSize, br);

            state.blockLength[treeType] = ReadBlockLength(state.blockLenTrees, treeType * iText.IO.Codec.Brotli.Dec.Huffman.HuffmanMaxTableSize, br);
            if (blockType == 1)
            {
                blockType = ringBuffers[offset + 1] + 1;
            }
            else if (blockType == 0)
            {
                blockType = ringBuffers[offset];
            }
            else
            {
                blockType -= 2;
            }
            if (blockType >= state.numBlockTypes[treeType])
            {
                blockType -= state.numBlockTypes[treeType];
            }
            ringBuffers[offset]     = ringBuffers[offset + 1];
            ringBuffers[offset + 1] = blockType;
        }
Exemple #3
0
        private static void CopyUncompressedData(iText.IO.Codec.Brotli.Dec.State state)
        {
            iText.IO.Codec.Brotli.Dec.BitReader br = state.br;
            byte[] ringBuffer = state.ringBuffer;
            // Could happen if block ends at ring buffer end.
            if (state.metaBlockLength <= 0)
            {
                iText.IO.Codec.Brotli.Dec.BitReader.Reload(br);
                state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.BlockStart;
                return;
            }
            int chunkLength = System.Math.Min(state.ringBufferSize - state.pos, state.metaBlockLength);

            iText.IO.Codec.Brotli.Dec.BitReader.CopyBytes(br, ringBuffer, state.pos, chunkLength);
            state.metaBlockLength -= chunkLength;
            state.pos             += chunkLength;
            if (state.pos == state.ringBufferSize)
            {
                state.nextRunningState = iText.IO.Codec.Brotli.Dec.RunningState.CopyUncompressed;
                state.bytesToWrite     = state.ringBufferSize;
                state.bytesWritten     = 0;
                state.runningState     = iText.IO.Codec.Brotli.Dec.RunningState.Write;
                return;
            }
            iText.IO.Codec.Brotli.Dec.BitReader.Reload(br);
            state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.BlockStart;
        }
Exemple #4
0
 private static void Prepare(iText.IO.Codec.Brotli.Dec.BitReader br)
 {
     ReadMoreInput(br);
     CheckHealth(br, false);
     FillBitWindow(br);
     FillBitWindow(br);
 }
Exemple #5
0
 /// <summary>Advances the Read buffer by 5 bytes to make room for reading next 24 bits.</summary>
 internal static void FillBitWindow(iText.IO.Codec.Brotli.Dec.BitReader br)
 {
     if (br.bitOffset >= 32)
     {
         br.accumulator = ((long)br.intBuffer[br.intOffset++] << 32) | ((long)(((ulong)br.accumulator) >> 32));
         br.bitOffset  -= 32;
     }
 }
Exemple #6
0
        /// <summary>Reads the specified number of bits from Read Buffer.</summary>
        internal static int ReadBits(iText.IO.Codec.Brotli.Dec.BitReader br, int n)
        {
            FillBitWindow(br);
            int val = (int)((long)(((ulong)br.accumulator) >> br.bitOffset)) & ((1 << n) - 1);

            br.bitOffset += n;
            return(val);
        }
Exemple #7
0
        private static int ReadBlockLength(int[] table, int offset, iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
            int code = ReadSymbol(table, offset, br);
            int n    = iText.IO.Codec.Brotli.Dec.Prefix.BlockLengthNBits[code];

            return(iText.IO.Codec.Brotli.Dec.Prefix.BlockLengthOffset[code] + iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, n));
        }
Exemple #8
0
 /// <exception cref="System.IO.IOException"/>
 internal static void Close(iText.IO.Codec.Brotli.Dec.BitReader br)
 {
     System.IO.Stream @is = br.input;
     br.input = null;
     if (@is != null)
     {
         @is.Dispose();
     }
 }
Exemple #9
0
        private static int DecodeContextMap(int contextMapSize, byte[] contextMap, iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
            int numTrees = DecodeVarLenUnsignedByte(br) + 1;

            if (numTrees == 1)
            {
                iText.IO.Codec.Brotli.Dec.Utils.FillWithZeroes(contextMap, 0, contextMapSize);
                return(numTrees);
            }
            bool useRleForZeros     = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) == 1;
            int  maxRunLengthPrefix = 0;

            if (useRleForZeros)
            {
                maxRunLengthPrefix = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 4) + 1;
            }
            int[] table = new int[iText.IO.Codec.Brotli.Dec.Huffman.HuffmanMaxTableSize];
            ReadHuffmanCode(numTrees + maxRunLengthPrefix, table, 0, br);
            for (int i = 0; i < contextMapSize;)
            {
                iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
                iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
                int code = ReadSymbol(table, 0, br);
                if (code == 0)
                {
                    contextMap[i] = 0;
                    i++;
                }
                else if (code <= maxRunLengthPrefix)
                {
                    int reps = (1 << code) + iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, code);
                    while (reps != 0)
                    {
                        if (i >= contextMapSize)
                        {
                            throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Corrupted context map");
                        }
                        // COV_NF_LINE
                        contextMap[i] = 0;
                        i++;
                        reps--;
                    }
                }
                else
                {
                    contextMap[i] = unchecked ((byte)(code - maxRunLengthPrefix));
                    i++;
                }
            }
            if (iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) == 1)
            {
                InverseMoveToFrontTransform(contextMap, contextMapSize);
            }
            return(numTrees);
        }
Exemple #10
0
        internal static int IntAvailable(iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            int limit = Capacity;

            if (br.endOfStreamReached)
            {
                limit = (br.tailBytes + 3) >> 2;
            }
            return(limit - br.intOffset);
        }
Exemple #11
0
        private static void DecodeMetaBlockLength(iText.IO.Codec.Brotli.Dec.BitReader br, iText.IO.Codec.Brotli.Dec.State state)
        {
            state.inputEnd        = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) == 1;
            state.metaBlockLength = 0;
            state.isUncompressed  = false;
            state.isMetadata      = false;
            if (state.inputEnd && iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) != 0)
            {
                return;
            }
            int sizeNibbles = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 2) + 4;

            if (sizeNibbles == 7)
            {
                state.isMetadata = true;
                if (iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) != 0)
                {
                    throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Corrupted reserved bit");
                }
                int sizeBytes = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 2);
                if (sizeBytes == 0)
                {
                    return;
                }
                for (int i = 0; i < sizeBytes; i++)
                {
                    int bits = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 8);
                    if (bits == 0 && i + 1 == sizeBytes && sizeBytes > 1)
                    {
                        throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Exuberant nibble");
                    }
                    state.metaBlockLength |= bits << (i * 8);
                }
            }
            else
            {
                for (int i = 0; i < sizeNibbles; i++)
                {
                    int bits = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 4);
                    if (bits == 0 && i + 1 == sizeNibbles && sizeNibbles > 4)
                    {
                        throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Exuberant nibble");
                    }
                    state.metaBlockLength |= bits << (i * 4);
                }
            }
            state.metaBlockLength++;
            if (!state.inputEnd)
            {
                state.isUncompressed = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) == 1;
            }
        }
Exemple #12
0
        internal static void JumpToByteBoundary(iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            int padding = (64 - br.bitOffset) & 7;

            if (padding != 0)
            {
                int paddingBits = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, padding);
                if (paddingBits != 0)
                {
                    throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Corrupted padding bits");
                }
            }
        }
Exemple #13
0
 /// <summary>Initialize bit reader.</summary>
 /// <remarks>
 /// Initialize bit reader.
 /// <p> Initialisation turns bit reader to a ready state. Also a number of bytes is prefetched to
 /// accumulator. Because of that this method may block until enough data could be read from input.
 /// </remarks>
 /// <param name="br">BitReader POJO</param>
 /// <param name="input">data source</param>
 internal static void Init(iText.IO.Codec.Brotli.Dec.BitReader br, System.IO.Stream input)
 {
     if (br.input != null)
     {
         throw new System.InvalidOperationException("Bit reader already has associated input stream");
     }
     iText.IO.Codec.Brotli.Dec.IntReader.Init(br.intReader, br.byteBuffer, br.intBuffer);
     br.input              = input;
     br.accumulator        = 0;
     br.bitOffset          = 64;
     br.intOffset          = Capacity;
     br.endOfStreamReached = false;
     Prepare(br);
 }
Exemple #14
0
 public virtual void TestReadAfterEos()
 {
     iText.IO.Codec.Brotli.Dec.BitReader reader = new iText.IO.Codec.Brotli.Dec.BitReader();
     iText.IO.Codec.Brotli.Dec.BitReader.Init(reader, new System.IO.MemoryStream(new byte[1]));
     iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(reader, 9);
     try
     {
         iText.IO.Codec.Brotli.Dec.BitReader.CheckHealth(reader, false);
     }
     catch (iText.IO.Codec.Brotli.Dec.BrotliRuntimeException)
     {
         // This exception is expected.
         return;
     }
     NUnit.Framework.Assert.Fail("BrotliRuntimeException should have been thrown by BitReader.checkHealth");
 }
Exemple #15
0
 /// <summary>Decodes a number in the range [0..255], by reading 1 - 11 bits.</summary>
 private static int DecodeVarLenUnsignedByte(iText.IO.Codec.Brotli.Dec.BitReader br)
 {
     if (iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) != 0)
     {
         int n = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 3);
         if (n == 0)
         {
             return(1);
         }
         else
         {
             return(iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, n) + (1 << n));
         }
     }
     return(0);
 }
Exemple #16
0
        internal static void CheckHealth(iText.IO.Codec.Brotli.Dec.BitReader br, bool endOfStream)
        {
            if (!br.endOfStreamReached)
            {
                return;
            }
            int byteOffset = (br.intOffset << 2) + ((br.bitOffset + 7) >> 3) - 8;

            if (byteOffset > br.tailBytes)
            {
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Read after end");
            }
            if (endOfStream && (byteOffset != br.tailBytes))
            {
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Unused bytes after end");
            }
        }
Exemple #17
0
 /// <summary>Reads next metablock header.</summary>
 /// <param name="state">decoding state</param>
 private static void ReadMetablockInfo(iText.IO.Codec.Brotli.Dec.State state)
 {
     iText.IO.Codec.Brotli.Dec.BitReader br = state.br;
     if (state.inputEnd)
     {
         state.nextRunningState = iText.IO.Codec.Brotli.Dec.RunningState.Finished;
         state.bytesToWrite     = state.pos;
         state.bytesWritten     = 0;
         state.runningState     = iText.IO.Codec.Brotli.Dec.RunningState.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;
     iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
     DecodeMetaBlockLength(br, state);
     if (state.metaBlockLength == 0 && !state.isMetadata)
     {
         return;
     }
     if (state.isUncompressed || state.isMetadata)
     {
         iText.IO.Codec.Brotli.Dec.BitReader.JumpToByteBoundary(br);
         state.runningState = state.isMetadata ? iText.IO.Codec.Brotli.Dec.RunningState.ReadMetadata : iText.IO.Codec.Brotli.Dec.RunningState.CopyUncompressed;
     }
     else
     {
         state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.CompressedBlockStart;
     }
     if (state.isMetadata)
     {
         return;
     }
     state.expectedTotalSize += state.metaBlockLength;
     if (state.ringBufferSize < state.maxRingBufferSize)
     {
         MaybeReallocateRingBuffer(state);
     }
 }
Exemple #18
0
        // Current meta-block header information.
        // TODO: Update to current spec.
        private static int DecodeWindowBits(iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            if (iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) == 0)
            {
                return(16);
            }
            int n = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 3);

            if (n != 0)
            {
                return(17 + n);
            }
            n = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 3);
            if (n != 0)
            {
                return(8 + n);
            }
            return(17);
        }
Exemple #19
0
        /* Number of bytes in unfinished "int" item. */
        /// <summary>Fills up the input buffer.</summary>
        /// <remarks>
        /// Fills up the input buffer.
        /// <p> No-op if there are at least 36 bytes present after current position.
        /// <p> After encountering the end of the input stream, 64 additional zero bytes are copied to the
        /// buffer.
        /// </remarks>
        internal static void ReadMoreInput(iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            // TODO: Split to check and read; move read outside of decoding loop.
            if (br.intOffset <= Capacity - 9)
            {
                return;
            }
            if (br.endOfStreamReached)
            {
                if (IntAvailable(br) >= -2)
                {
                    return;
                }
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("No more input");
            }
            int readOffset = br.intOffset << 2;
            int bytesRead  = ByteReadSize - readOffset;

            System.Array.Copy(br.byteBuffer, readOffset, br.byteBuffer, 0, bytesRead);
            br.intOffset = 0;
            try
            {
                while (bytesRead < ByteReadSize)
                {
                    int len = br.input.Read(br.byteBuffer, bytesRead, ByteReadSize - bytesRead);
                    // EOF is -1 in Java, but 0 in C#.
                    if (len <= 0)
                    {
                        br.endOfStreamReached = true;
                        br.tailBytes          = bytesRead;
                        bytesRead            += 3;
                        break;
                    }
                    bytesRead += len;
                }
            }
            catch (System.IO.IOException e)
            {
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Failed to read input", e);
            }
            iText.IO.Codec.Brotli.Dec.IntReader.Convert(br.intReader, bytesRead >> 2);
        }
Exemple #20
0
        /// <summary>Decodes the next Huffman code from bit-stream.</summary>
        private static int ReadSymbol(int[] table, int offset, iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            int val = (int)((long)(((ulong)br.accumulator) >> br.bitOffset));

            offset += val & HuffmanTableMask;
            int bits = table[offset] >> 16;
            int sym  = table[offset] & unchecked ((int)(0xFFFF));

            if (bits <= HuffmanTableBits)
            {
                br.bitOffset += bits;
                return(sym);
            }
            offset += sym;
            int mask = (1 << bits) - 1;

            offset       += (int)(((uint)(val & mask)) >> HuffmanTableBits);
            br.bitOffset += ((table[offset] >> 16) + HuffmanTableBits);
            return(table[offset] & unchecked ((int)(0xFFFF)));
        }
Exemple #21
0
        private static void ReadHuffmanCodeLengths(int[] codeLengthCodeLengths, int numSymbols, int[] codeLengths, iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            int symbol        = 0;
            int prevCodeLen   = DefaultCodeLength;
            int repeat        = 0;
            int repeatCodeLen = 0;
            int space         = 32768;

            int[] table = new int[32];
            iText.IO.Codec.Brotli.Dec.Huffman.BuildHuffmanTable(table, 0, 5, codeLengthCodeLengths, CodeLengthCodes);
            while (symbol < numSymbols && space > 0)
            {
                iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
                iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
                int p = (int)(((long)(((ulong)br.accumulator) >> br.bitOffset))) & 31;
                br.bitOffset += table[p] >> 16;
                int codeLen = table[p] & unchecked ((int)(0xFFFF));
                if (codeLen < CodeLengthRepeatCode)
                {
                    repeat = 0;
                    codeLengths[symbol++] = codeLen;
                    if (codeLen != 0)
                    {
                        prevCodeLen = codeLen;
                        space      -= 32768 >> codeLen;
                    }
                }
                else
                {
                    int extraBits = codeLen - 14;
                    int newLen    = 0;
                    if (codeLen == CodeLengthRepeatCode)
                    {
                        newLen = prevCodeLen;
                    }
                    if (repeatCodeLen != newLen)
                    {
                        repeat        = 0;
                        repeatCodeLen = newLen;
                    }
                    int oldRepeat = repeat;
                    if (repeat > 0)
                    {
                        repeat  -= 2;
                        repeat <<= extraBits;
                    }
                    repeat += iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, extraBits) + 3;
                    int repeatDelta = repeat - oldRepeat;
                    if (symbol + repeatDelta > numSymbols)
                    {
                        throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("symbol + repeatDelta > numSymbols");
                    }
                    // COV_NF_LINE
                    for (int i = 0; i < repeatDelta; i++)
                    {
                        codeLengths[symbol++] = repeatCodeLen;
                    }
                    if (repeatCodeLen != 0)
                    {
                        space -= repeatDelta << (15 - repeatCodeLen);
                    }
                }
            }
            if (space != 0)
            {
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Unused space");
            }
            // COV_NF_LINE
            // TODO: Pass max_symbol to Huffman table builder instead?
            iText.IO.Codec.Brotli.Dec.Utils.FillWithZeroes(codeLengths, symbol, numSymbols - symbol);
        }
Exemple #22
0
        // TODO: Use specialized versions for smaller tables.
        internal static void ReadHuffmanCode(int alphabetSize, int[] table, int offset, iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            bool ok = true;
            int  simpleCodeOrSkip;

            iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
            // TODO: Avoid allocation.
            int[] codeLengths = new int[alphabetSize];
            simpleCodeOrSkip = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 2);
            if (simpleCodeOrSkip == 1)
            {
                // Read symbols, codes & code lengths directly.
                int   maxBitsCounter = alphabetSize - 1;
                int   maxBits        = 0;
                int[] symbols        = new int[4];
                int   numSymbols     = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 2) + 1;
                while (maxBitsCounter != 0)
                {
                    maxBitsCounter >>= 1;
                    maxBits++;
                }
                // TODO: uncomment when codeLengths is reused.
                // Utils.fillWithZeroes(codeLengths, 0, alphabetSize);
                for (int i = 0; i < numSymbols; i++)
                {
                    symbols[i] = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, maxBits) % alphabetSize;
                    codeLengths[symbols[i]] = 2;
                }
                codeLengths[symbols[0]] = 1;
                switch (numSymbols)
                {
                case 1:
                {
                    break;
                }

                case 2:
                {
                    ok = symbols[0] != symbols[1];
                    codeLengths[symbols[1]] = 1;
                    break;
                }

                case 3:
                {
                    ok = symbols[0] != symbols[1] && symbols[0] != symbols[2] && symbols[1] != symbols[2];
                    break;
                }

                case 4:
                default:
                {
                    ok = symbols[0] != symbols[1] && symbols[0] != symbols[2] && symbols[0] != symbols[3] && symbols[1] != symbols[2] && symbols[1] != symbols[3] && symbols[2] != symbols[3];
                    if (iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 1) == 1)
                    {
                        codeLengths[symbols[2]] = 3;
                        codeLengths[symbols[3]] = 3;
                    }
                    else
                    {
                        codeLengths[symbols[0]] = 2;
                    }
                    break;
                }
                }
            }
            else
            {
                // Decode Huffman-coded code lengths.
                int[] codeLengthCodeLengths = new int[CodeLengthCodes];
                int   space    = 32;
                int   numCodes = 0;
                for (int i = simpleCodeOrSkip; i < CodeLengthCodes && space > 0; i++)
                {
                    int codeLenIdx = CodeLengthCodeOrder[i];
                    iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
                    int p = (int)((long)(((ulong)br.accumulator) >> br.bitOffset)) & 15;
                    // TODO: Demultiplex FIXED_TABLE.
                    br.bitOffset += FixedTable[p] >> 16;
                    int v = FixedTable[p] & unchecked ((int)(0xFFFF));
                    codeLengthCodeLengths[codeLenIdx] = v;
                    if (v != 0)
                    {
                        space -= (32 >> v);
                        numCodes++;
                    }
                }
                ok = (numCodes == 1 || space == 0);
                ReadHuffmanCodeLengths(codeLengthCodeLengths, alphabetSize, codeLengths, br);
            }
            if (!ok)
            {
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Can't readHuffmanCode");
            }
            // COV_NF_LINE
            iText.IO.Codec.Brotli.Dec.Huffman.BuildHuffmanTable(table, offset, HuffmanTableBits, codeLengths, alphabetSize);
        }
Exemple #23
0
        internal static void CopyBytes(iText.IO.Codec.Brotli.Dec.BitReader br, byte[] data, int offset, int length)
        {
            if ((br.bitOffset & 7) != 0)
            {
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Unaligned copyBytes");
            }
            // Drain accumulator.
            while ((br.bitOffset != 64) && (length != 0))
            {
                data[offset++] = unchecked ((byte)((long)(((ulong)br.accumulator) >> br.bitOffset)));
                br.bitOffset  += 8;
                length--;
            }
            if (length == 0)
            {
                return;
            }
            // Get data from shadow buffer with "sizeof(int)" granularity.
            int copyInts = System.Math.Min(IntAvailable(br), length >> 2);

            if (copyInts > 0)
            {
                int readOffset = br.intOffset << 2;
                System.Array.Copy(br.byteBuffer, readOffset, data, offset, copyInts << 2);
                offset       += copyInts << 2;
                length       -= copyInts << 2;
                br.intOffset += copyInts;
            }
            if (length == 0)
            {
                return;
            }
            // Read tail bytes.
            if (IntAvailable(br) > 0)
            {
                // length = 1..3
                FillBitWindow(br);
                while (length != 0)
                {
                    data[offset++] = unchecked ((byte)((long)(((ulong)br.accumulator) >> br.bitOffset)));
                    br.bitOffset  += 8;
                    length--;
                }
                CheckHealth(br, false);
                return;
            }
            // Now it is possible to copy bytes directly.
            try
            {
                while (length > 0)
                {
                    int len = br.input.Read(data, offset, length);
                    if (len == -1)
                    {
                        throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Unexpected end of input");
                    }
                    offset += len;
                    length -= len;
                }
            }
            catch (System.IO.IOException e)
            {
                throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Failed to read input", e);
            }
        }
Exemple #24
0
        private static void ReadMetablockHuffmanCodesAndContextMaps(iText.IO.Codec.Brotli.Dec.State state)
        {
            iText.IO.Codec.Brotli.Dec.BitReader br = state.br;
            for (int i = 0; i < 3; i++)
            {
                state.numBlockTypes[i] = DecodeVarLenUnsignedByte(br) + 1;
                state.blockLength[i]   = 1 << 28;
                if (state.numBlockTypes[i] > 1)
                {
                    ReadHuffmanCode(state.numBlockTypes[i] + 2, state.blockTypeTrees, i * iText.IO.Codec.Brotli.Dec.Huffman.HuffmanMaxTableSize, br);
                    ReadHuffmanCode(NumBlockLengthCodes, state.blockLenTrees, i * iText.IO.Codec.Brotli.Dec.Huffman.HuffmanMaxTableSize, br);
                    state.blockLength[i] = ReadBlockLength(state.blockLenTrees, i * iText.IO.Codec.Brotli.Dec.Huffman.HuffmanMaxTableSize, br);
                }
            }
            iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
            state.distancePostfixBits    = iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 2);
            state.numDirectDistanceCodes = NumDistanceShortCodes + (iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 4) << state.distancePostfixBits);
            state.distancePostfixMask    = (1 << state.distancePostfixBits) - 1;
            int numDistanceCodes = state.numDirectDistanceCodes + (48 << state.distancePostfixBits);

            // TODO: Reuse?
            state.contextModes = new byte[state.numBlockTypes[0]];
            for (int i = 0; i < state.numBlockTypes[0];)
            {
                /* Ensure that less than 256 bits read between readMoreInput. */
                int limit = System.Math.Min(i + 96, state.numBlockTypes[0]);
                for (; i < limit; ++i)
                {
                    state.contextModes[i] = unchecked ((byte)(iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 2) << 1));
                }
                iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
            }
            // TODO: Reuse?
            state.contextMap = new byte[state.numBlockTypes[0] << LiteralContextBits];
            int numLiteralTrees = DecodeContextMap(state.numBlockTypes[0] << LiteralContextBits, state.contextMap, br);

            state.trivialLiteralContext = true;
            for (int j = 0; j < state.numBlockTypes[0] << LiteralContextBits; j++)
            {
                if (state.contextMap[j] != j >> LiteralContextBits)
                {
                    state.trivialLiteralContext = false;
                    break;
                }
            }
            // TODO: Reuse?
            state.distContextMap = new byte[state.numBlockTypes[2] << DistanceContextBits];
            int numDistTrees = DecodeContextMap(state.numBlockTypes[2] << DistanceContextBits, state.distContextMap, br);

            iText.IO.Codec.Brotli.Dec.HuffmanTreeGroup.Init(state.hGroup0, NumLiteralCodes, numLiteralTrees);
            iText.IO.Codec.Brotli.Dec.HuffmanTreeGroup.Init(state.hGroup1, NumInsertAndCopyCodes, state.numBlockTypes[1]);
            iText.IO.Codec.Brotli.Dec.HuffmanTreeGroup.Init(state.hGroup2, numDistanceCodes, numDistTrees);
            iText.IO.Codec.Brotli.Dec.HuffmanTreeGroup.Decode(state.hGroup0, br);
            iText.IO.Codec.Brotli.Dec.HuffmanTreeGroup.Decode(state.hGroup1, br);
            iText.IO.Codec.Brotli.Dec.HuffmanTreeGroup.Decode(state.hGroup2, br);
            state.contextMapSlice      = 0;
            state.distContextMapSlice  = 0;
            state.contextLookupOffset1 = iText.IO.Codec.Brotli.Dec.Context.LookupOffsets[state.contextModes[0]];
            state.contextLookupOffset2 = iText.IO.Codec.Brotli.Dec.Context.LookupOffsets[state.contextModes[0] + 1];
            state.literalTreeIndex     = 0;
            state.literalTree          = state.hGroup0.trees[0];
            state.treeCommandOffset    = state.hGroup1.trees[0];
            // TODO: == 0?
            state.blockTypeRb[0] = state.blockTypeRb[2] = state.blockTypeRb[4] = 1;
            state.blockTypeRb[1] = state.blockTypeRb[3] = state.blockTypeRb[5] = 0;
        }
Exemple #25
0
        /// <summary>Actual decompress implementation.</summary>
        internal static void Decompress(iText.IO.Codec.Brotli.Dec.State state)
        {
            if (state.runningState == iText.IO.Codec.Brotli.Dec.RunningState.Uninitialized)
            {
                throw new System.InvalidOperationException("Can't decompress until initialized");
            }
            if (state.runningState == iText.IO.Codec.Brotli.Dec.RunningState.Closed)
            {
                throw new System.InvalidOperationException("Can't decompress after close");
            }
            iText.IO.Codec.Brotli.Dec.BitReader br = state.br;
            int ringBufferMask = state.ringBufferSize - 1;

            byte[] ringBuffer = state.ringBuffer;
            while (state.runningState != iText.IO.Codec.Brotli.Dec.RunningState.Finished)
            {
                switch (state.runningState)
                {
                case iText.IO.Codec.Brotli.Dec.RunningState.BlockStart:
                {
                    // TODO: extract cases to methods for the better readability.
                    if (state.metaBlockLength < 0)
                    {
                        throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Invalid metablock length");
                    }
                    ReadMetablockInfo(state);
                    /* Ring-buffer would be reallocated here. */
                    ringBufferMask = state.ringBufferSize - 1;
                    ringBuffer     = state.ringBuffer;
                    continue;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.CompressedBlockStart:
                {
                    ReadMetablockHuffmanCodesAndContextMaps(state);
                    state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.MainLoop;
                    goto case iText.IO.Codec.Brotli.Dec.RunningState.MainLoop;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.MainLoop:
                {
                    // Fall through
                    if (state.metaBlockLength <= 0)
                    {
                        state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.BlockStart;
                        continue;
                    }
                    iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
                    if (state.blockLength[1] == 0)
                    {
                        DecodeCommandBlockSwitch(state);
                    }
                    state.blockLength[1]--;
                    iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
                    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 = iText.IO.Codec.Brotli.Dec.Prefix.InsertRangeLut[rangeIdx] + (((int)(((uint)cmdCode) >> 3)) & 7);
                    int copyCode   = iText.IO.Codec.Brotli.Dec.Prefix.CopyRangeLut[rangeIdx] + (cmdCode & 7);
                    state.insertLength = iText.IO.Codec.Brotli.Dec.Prefix.InsertLengthOffset[insertCode] + iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, iText.IO.Codec.Brotli.Dec.Prefix.InsertLengthNBits[insertCode]);
                    state.copyLength   = iText.IO.Codec.Brotli.Dec.Prefix.CopyLengthOffset[copyCode] + iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, iText.IO.Codec.Brotli.Dec.Prefix.CopyLengthNBits[copyCode]);
                    state.j            = 0;
                    state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.InsertLoop;
                    goto case iText.IO.Codec.Brotli.Dec.RunningState.InsertLoop;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.InsertLoop:
                {
                    // Fall through
                    if (state.trivialLiteralContext)
                    {
                        while (state.j < state.insertLength)
                        {
                            iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
                            if (state.blockLength[0] == 0)
                            {
                                DecodeLiteralBlockSwitch(state);
                            }
                            state.blockLength[0]--;
                            iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
                            ringBuffer[state.pos] = unchecked ((byte)ReadSymbol(state.hGroup0.codes, state.literalTree, br));
                            state.j++;
                            if (state.pos++ == ringBufferMask)
                            {
                                state.nextRunningState = iText.IO.Codec.Brotli.Dec.RunningState.InsertLoop;
                                state.bytesToWrite     = state.ringBufferSize;
                                state.bytesWritten     = 0;
                                state.runningState     = iText.IO.Codec.Brotli.Dec.RunningState.Write;
                                break;
                            }
                        }
                    }
                    else
                    {
                        int prevByte1 = ringBuffer[(state.pos - 1) & ringBufferMask] & unchecked ((int)(0xFF));
                        int prevByte2 = ringBuffer[(state.pos - 2) & ringBufferMask] & unchecked ((int)(0xFF));
                        while (state.j < state.insertLength)
                        {
                            iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
                            if (state.blockLength[0] == 0)
                            {
                                DecodeLiteralBlockSwitch(state);
                            }
                            int literalTreeIndex = state.contextMap[state.contextMapSlice + (iText.IO.Codec.Brotli.Dec.Context.Lookup[state.contextLookupOffset1 + prevByte1] | iText.IO.Codec.Brotli.Dec.Context.Lookup[state.contextLookupOffset2 + prevByte2])] & unchecked ((int)(0xFF));
                            state.blockLength[0]--;
                            prevByte2 = prevByte1;
                            iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
                            prevByte1             = ReadSymbol(state.hGroup0.codes, state.hGroup0.trees[literalTreeIndex], br);
                            ringBuffer[state.pos] = unchecked ((byte)prevByte1);
                            state.j++;
                            if (state.pos++ == ringBufferMask)
                            {
                                state.nextRunningState = iText.IO.Codec.Brotli.Dec.RunningState.InsertLoop;
                                state.bytesToWrite     = state.ringBufferSize;
                                state.bytesWritten     = 0;
                                state.runningState     = iText.IO.Codec.Brotli.Dec.RunningState.Write;
                                break;
                            }
                        }
                    }
                    if (state.runningState != iText.IO.Codec.Brotli.Dec.RunningState.InsertLoop)
                    {
                        continue;
                    }
                    state.metaBlockLength -= state.insertLength;
                    if (state.metaBlockLength <= 0)
                    {
                        state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.MainLoop;
                        continue;
                    }
                    if (state.distanceCode < 0)
                    {
                        iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
                        if (state.blockLength[2] == 0)
                        {
                            DecodeDistanceBlockSwitch(state);
                        }
                        state.blockLength[2]--;
                        iText.IO.Codec.Brotli.Dec.BitReader.FillBitWindow(br);
                        state.distanceCode = ReadSymbol(state.hGroup2.codes, state.hGroup2.trees[state.distContextMap[state.distContextMapSlice + (state.copyLength > 4 ? 3 : state.copyLength - 2)] & unchecked ((int)(0xFF))], br);
                        if (state.distanceCode >= state.numDirectDistanceCodes)
                        {
                            state.distanceCode -= state.numDirectDistanceCodes;
                            int postfix = state.distanceCode & state.distancePostfixMask;
                            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 + iText.IO.Codec.Brotli.Dec.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 iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Negative distance");
                    }
                    // COV_NF_LINE
                    if (state.maxDistance != state.maxBackwardDistance && state.pos < state.maxBackwardDistance)
                    {
                        state.maxDistance = state.pos;
                    }
                    else
                    {
                        state.maxDistance = state.maxBackwardDistance;
                    }
                    state.copyDst = state.pos;
                    if (state.distance > state.maxDistance)
                    {
                        state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.Transform;
                        continue;
                    }
                    if (state.distanceCode > 0)
                    {
                        state.distRb[state.distRbIdx & 3] = state.distance;
                        state.distRbIdx++;
                    }
                    if (state.copyLength > state.metaBlockLength)
                    {
                        throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Invalid backward reference");
                    }
                    // COV_NF_LINE
                    state.j            = 0;
                    state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.CopyLoop;
                    goto case iText.IO.Codec.Brotli.Dec.RunningState.CopyLoop;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.CopyLoop:
                {
                    // fall through
                    int src        = (state.pos - state.distance) & ringBufferMask;
                    int dst        = state.pos;
                    int copyLength = state.copyLength - state.j;
                    if ((src + copyLength < ringBufferMask) && (dst + copyLength < ringBufferMask))
                    {
                        for (int k = 0; k < copyLength; ++k)
                        {
                            ringBuffer[dst++] = ringBuffer[src++];
                        }
                        state.j += copyLength;
                        state.metaBlockLength -= copyLength;
                        state.pos             += copyLength;
                    }
                    else
                    {
                        for (; state.j < state.copyLength;)
                        {
                            ringBuffer[state.pos] = ringBuffer[(state.pos - state.distance) & ringBufferMask];
                            state.metaBlockLength--;
                            state.j++;
                            if (state.pos++ == ringBufferMask)
                            {
                                state.nextRunningState = iText.IO.Codec.Brotli.Dec.RunningState.CopyLoop;
                                state.bytesToWrite     = state.ringBufferSize;
                                state.bytesWritten     = 0;
                                state.runningState     = iText.IO.Codec.Brotli.Dec.RunningState.Write;
                                break;
                            }
                        }
                    }
                    if (state.runningState == iText.IO.Codec.Brotli.Dec.RunningState.CopyLoop)
                    {
                        state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.MainLoop;
                    }
                    continue;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.Transform:
                {
                    if (state.copyLength >= iText.IO.Codec.Brotli.Dec.Dictionary.MinWordLength && state.copyLength <= iText.IO.Codec.Brotli.Dec.Dictionary.MaxWordLength)
                    {
                        int offset       = iText.IO.Codec.Brotli.Dec.Dictionary.OffsetsByLength[state.copyLength];
                        int wordId       = state.distance - state.maxDistance - 1;
                        int shift        = iText.IO.Codec.Brotli.Dec.Dictionary.SizeBitsByLength[state.copyLength];
                        int mask         = (1 << shift) - 1;
                        int wordIdx      = wordId & mask;
                        int transformIdx = (int)(((uint)wordId) >> shift);
                        offset += wordIdx * state.copyLength;
                        if (transformIdx < iText.IO.Codec.Brotli.Dec.Transform.Transforms.Length)
                        {
                            int len = iText.IO.Codec.Brotli.Dec.Transform.TransformDictionaryWord(ringBuffer, state.copyDst, iText.IO.Codec.Brotli.Dec.Dictionary.GetData(), offset, state.copyLength, iText.IO.Codec.Brotli.Dec.Transform.Transforms[transformIdx]);
                            state.copyDst         += len;
                            state.pos             += len;
                            state.metaBlockLength -= len;
                            if (state.copyDst >= state.ringBufferSize)
                            {
                                state.nextRunningState = iText.IO.Codec.Brotli.Dec.RunningState.CopyWrapBuffer;
                                state.bytesToWrite     = state.ringBufferSize;
                                state.bytesWritten     = 0;
                                state.runningState     = iText.IO.Codec.Brotli.Dec.RunningState.Write;
                                continue;
                            }
                        }
                        else
                        {
                            throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Invalid backward reference");
                        }
                    }
                    else
                    {
                        // COV_NF_LINE
                        throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Invalid backward reference");
                    }
                    // COV_NF_LINE
                    state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.MainLoop;
                    continue;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.CopyWrapBuffer:
                {
                    System.Array.Copy(ringBuffer, state.ringBufferSize, ringBuffer, 0, state.copyDst - state.ringBufferSize);
                    state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.MainLoop;
                    continue;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.ReadMetadata:
                {
                    while (state.metaBlockLength > 0)
                    {
                        iText.IO.Codec.Brotli.Dec.BitReader.ReadMoreInput(br);
                        // Optimize
                        iText.IO.Codec.Brotli.Dec.BitReader.ReadBits(br, 8);
                        state.metaBlockLength--;
                    }
                    state.runningState = iText.IO.Codec.Brotli.Dec.RunningState.BlockStart;
                    continue;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.CopyUncompressed:
                {
                    CopyUncompressedData(state);
                    continue;
                }

                case iText.IO.Codec.Brotli.Dec.RunningState.Write:
                {
                    if (!WriteRingBuffer(state))
                    {
                        // Output buffer is full.
                        return;
                    }
                    if (state.pos >= state.maxBackwardDistance)
                    {
                        state.maxDistance = state.maxBackwardDistance;
                    }
                    state.pos         &= ringBufferMask;
                    state.runningState = state.nextRunningState;
                    continue;
                }

                default:
                {
                    throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Unexpected state " + state.runningState);
                }
                }
            }
            if (state.runningState == iText.IO.Codec.Brotli.Dec.RunningState.Finished)
            {
                if (state.metaBlockLength < 0)
                {
                    throw new iText.IO.Codec.Brotli.Dec.BrotliRuntimeException("Invalid metablock length");
                }
                iText.IO.Codec.Brotli.Dec.BitReader.JumpToByteBoundary(br);
                iText.IO.Codec.Brotli.Dec.BitReader.CheckHealth(state.br, true);
            }
        }
        /// <summary>Decodes Huffman trees from input stream and constructs lookup tables.</summary>
        /// <param name="group">target POJO</param>
        /// <param name="br">data source</param>
        internal static void Decode(iText.IO.Codec.Brotli.Dec.HuffmanTreeGroup group, iText.IO.Codec.Brotli.Dec.BitReader br)
        {
            int next = 0;
            int n    = group.trees.Length;

            for (int i = 0; i < n; i++)
            {
                group.trees[i] = next;
                iText.IO.Codec.Brotli.Dec.Decode.ReadHuffmanCode(group.alphabetSize, group.codes, next, br);
                next += iText.IO.Codec.Brotli.Dec.Huffman.HuffmanMaxTableSize;
            }
        }