Ejemplo n.º 1
0
            /// <summary>
            /// Adapted from https://github.com/google/brotli/blob/master/c/enc/encode.c (EncodeData).
            /// </summary>
            public (MetaBlock MetaBlock, BrotliEncodeInfo Next) Encode(BrotliEncodeInfo info)
            {
                var builder = info.NewBuilder();

                do
                {
                    position = lastProcessedPos;
                    // TODO extend last command somehow???

                    int chunkLength = Math.Min(input.Length - position - lastInsertLen, InputBlockSize(lgBlock));

                    if (chunkLength == 0)
                    {
                        break;
                    }

                    hasher.StitchToPreviousBlock(chunkLength, position);

                    lastProcessedPos = position + chunkLength;
                    CreateBackwardReferences(builder, chunkLength);
                }while(lastProcessedPos + lastInsertLen < input.Length && ShouldContinueThisBlock(builder));

                if (lastInsertLen > 0)
                {
                    builder.AddInsert(Literal.FromBytes(info.Bytes.Slice(builder.OutputSize, lastInsertLen)));
                    lastInsertLen = 0;
                }

                return(builder.Build(info));
            }
        public (MetaBlock, BrotliEncodeInfo) Encode(BrotliEncodeInfo info)
        {
            var bytes = CollectionHelper.SliceAtMost(info.Bytes, DataLength.MaxUncompressedBytes);
            var mb    = new MetaBlock.Uncompressed(bytes);

            return(mb, info.WithOutputtedMetaBock(mb));
        }
Ejemplo n.º 3
0
        public (MetaBlock, BrotliEncodeInfo) Encode(BrotliEncodeInfo info)
        {
            var bytes = CollectionHelper.SliceAtMost(info.Bytes, DataLength.MaxUncompressedBytes).ToArray();

            return(info.NewBuilder()
                   .AddInsert(Literal.FromBytes(bytes))
                   .Build(info));
        }
Ejemplo n.º 4
0
        public MetaBlock?NextMetaBlock()
        {
            if (encodeInfo.IsFinished)
            {
                return(null);
            }

            var(metaBlock, newEncodeInfo) = encoder.Encode(encodeInfo);

            encodeInfo = newEncodeInfo;
            return(metaBlock);
        }
Ejemplo n.º 5
0
            public (MetaBlock, BrotliEncodeInfo) Encode(BrotliEncodeInfo info)
            {
                (literalTree, literalRatio) = Huffman.EstimateLiteralRatio(new ArraySegment <byte>(input, ip, blockSize));

                var builder  = info.NewBuilder();
                var nextStep = NextStep.EmitCommands;

                while (true)
                {
                    switch (nextStep)
                    {
                    case NextStep.EmitCommands:
                        nextStep = EmitCommands();
                        break;

                    case NextStep.EmitCommandsNextHash:
                        nextStep = EmitCommandsNextHash();
                        break;

                    case NextStep.Trawl:
                        nextStep = Trawl(builder);
                        break;

                    case NextStep.EmitRemainder:
                        nextStep = EmitRemainder(builder);
                        break;

                    case NextStep.OutputCompressed:
                        OnNextBlock();

                        return(builder.Build(info, new BrotliCompressionParameters.Builder(info.CompressionParameters)
                        {
                            GenerateLiteralTree = _ => literalTree
                        }.Build()));

                    case NextStep.OutputUncompressed:
                        var metaBlock = new MetaBlock.Uncompressed(input, metaBlockStart, outputTo - metaBlockStart);

                        inputSize  -= metaBlock.DataLength.UncompressedBytes;
                        nextIpStart = outputTo;
                        nextEmit    = nextIpStart;
                        OnNextBlock();

                        return(metaBlock, info.WithOutputtedMetaBock(metaBlock));
                    }
                }
            }
Ejemplo n.º 6
0
 public BrotliFileStreamEncoder(BrotliFileParameters fileParameters, BrotliCompressionParameters compressionParameters, byte[] bytes, IBrotliEncoder encoder)
 {
     this.encoder    = encoder;
     this.encodeInfo = new BrotliEncodeInfo(fileParameters, compressionParameters, bytes);
 }