Esempio n. 1
0
            internal void MatchFinderMt_CreateVTable(out IMatchFinder vTable)
            {
                // Careful: don't use this.mNumHashBytes - it hasn't been initialized yet!
                TR("MatchFinderMt_CreateVTable", base.mNumHashBytes);
                switch (base.mNumHashBytes)
                {
                case 2:
                    vTable = mInterface = new MatchFinderMt2();
                    break;

                case 3:
                    vTable = mInterface = new MatchFinderMt3();
                    break;

                default:
#if PROTOTYPE
                    vTable = mInterface = new MatchFinderMt5();
                    break;

                case 4:
#endif
                    if (base.mBigHash)
                    {
                        vTable = mInterface = new MatchFinderMt4b();
                    }
                    else
                    {
                        vTable = mInterface = new MatchFinderMt4a();
                    }
                    break;
                }
            }
        private Match[] FindMatches(Stream input, int compressionMode, int huffmanMode)
        {
            IMatchFinder[] matchFinders;
            switch (compressionMode)
            {
            case 1:
                matchFinders = new[]
                {
                    new HybridSuffixTreeMatchFinder(
                        new FindLimitations(3, 18, 1, 0xFFFF),
                        new FindOptions(false, 0, 0, UnitSize.Byte, 8))
                };
                break;

            case 2:
                matchFinders = new[]
                {
                    new HybridSuffixTreeMatchFinder(
                        new FindLimitations(3, -1, 1, 0xFFFF),
                        new FindOptions(false, 0, 0, UnitSize.Byte, 8))
                };
                break;

            case 3:
                //var newLength = input.Length >> 1 << 1;
                matchFinders = new[]
                {
                    new HybridSuffixTreeMatchFinder(
                        new FindLimitations(4, -1, 2, 0xFFFF),
                        new FindOptions(false, 0, 0, UnitSize.Short, 8))
                };
                break;

            case 4:
                return(Array.Empty <Match>());

            case 5:
                matchFinders = new IMatchFinder[]
                {
                    new HybridSuffixTreeMatchFinder(new FindLimitations(3, 0x42, 1, 0xFFFF),
                                                    new FindOptions(false, 0, 0, UnitSize.Byte, 8)),
                    new RleMatchFinder(new FindLimitations(1, 0x40),
                                       new FindOptions(false, 0, 0, UnitSize.Byte, 8))
                };
                break;

            default:
                throw new InvalidOperationException($"Unknown compression mode {compressionMode}.");
            }

            // Optimal parse all LZ matches
            var parser = new ForwardBackwardOptimalParser(
                new FindOptions(false, 0, 0, compressionMode == 3 ? UnitSize.Short : UnitSize.Byte, 8),
                new SlimePriceCalculator(compressionMode, huffmanMode),
                matchFinders);

            return(parser.ParseMatches(input).ToArray());
        }
Esempio n. 3
0
        // =============================================================
        //  Code
        // =============================================================

        public byte[] Compress(byte[] input)
        {
            Output  = new BitEncoder();
            Matcher = new HashChain8(input, 26, 32);
            for (int i = 0; i < SymbolProbabilities.Length; i++)
            {
                SymbolProbabilities[i].Init();
            }
            for (int i = 0; i < DistanceScaleProbabilities.Length; i++)
            {
                DistanceScaleProbabilities[i].Init();
            }

            int inputSize   = input.Length;
            int inputOffset = 1;

            EncodeSymbol(input[0], 0); // Emit the first byte as a literal with no context.

            while (inputOffset <= inputSize)
            {
                var  match          = FindBestMatch(inputOffset);
                bool matchAvailable = match.Benefit > 0;

                if (matchAvailable)
                {
                    var nextMatch = PeekNextMatch(inputOffset + 1);
                    if (nextMatch.Benefit > match.Benefit)
                    {
                        matchAvailable = false;
                    }
                }

                if (matchAvailable)
                {
                    if (matchAvailable)
                    {
                        if (match.Length > 255)
                        {
                            match.Length = 255;
                        }
                        EncodeSymbol(256 + match.Length, input[inputOffset - 1]);
                        EncodeDistance(match.Distance);
                        inputOffset += match.Length;
                    }
                }
                else
                {
                    EncodeSymbol(input[inputOffset], input[inputOffset - 1]);
                    inputOffset++;
                }
                if (inputOffset == inputSize)
                {
                    break;
                }
            }
            return(Output.GetBytes());
        }
Esempio n. 4
0
 internal static void MatchFinder_CreateVTable(CMatchFinder p, out IMatchFinder vTable)
 {
     TR("MatchFinder_CreateVTable", p.mNumHashBytes);
     if (!p.mBtMode)
         vTable = new MatchFinderHc4();
     else if (p.mNumHashBytes == 2)
         vTable = new MatchFinderBt2();
     else if (p.mNumHashBytes == 3)
         vTable = new MatchFinderBt3();
     else
         vTable = new MatchFinderBt4();
 }
Esempio n. 5
0
        public byte[] Compress(byte[] input, LZBPerformance mode = LZBPerformance.NORMAL)
        {
            switch (mode)
            {
            case LZBPerformance.L1_VeryFast: Matcher = new HashTable3(input, 21);        LazyParse = false; break;

            case LZBPerformance.L2_VeryFast: Matcher = new HashTable3(input, 21);        LazyParse = true;  break;

            case LZBPerformance.NORMAL:      Matcher = new MetaHT3HC4HC8(input, 21, 32); LazyParse = true;  break;
            }

            Writer         = new BitWriterFast();
            CachedPosition = -1;
            int inputSize   = input.Length;
            int inputOffset = 0;

            while (inputOffset < inputSize)
            {
                var  match          = FindBestMatch(inputOffset);
                bool matchAvailable = match.Benefit > 0;

                if (LazyParse && matchAvailable)
                {
                    var nextMatch = PeekNextMatch(inputOffset + 1);
                    if (nextMatch.Benefit > match.Benefit + 9)
                    {
                        matchAvailable = false;
                    }
                }

                if (matchAvailable)
                {
                    if (match.Length > MAX_MATCH)
                    {
                        match.Length = MAX_MATCH;
                    }
                    Writer.WriteBits(1, 1);
                    EncodeMatchLength(match.Length);
                    EncodeMatchOffset(match.Distance);
                    inputOffset += match.Length;
                }
                else
                {
                    Writer.WriteBits(input[inputOffset++] << 1, 9);
                }
            }
            return(Writer.GetBuffer());
        }
Esempio n. 6
0
        private IMatchFinder[] BuildMatchFinders(FindOptions options)
        {
            if (_matchFinderFactories.Count != _limitFactories.Count)
            {
                throw new InvalidOperationException("Not all match finders have limitations to search patterns in.");
            }

            var matchFinders = new IMatchFinder[_limitFactories.Count];

            for (var i = 0; i < _limitFactories.Count; i++)
            {
                var limits      = _limitFactories[i]();
                var matchFinder = _matchFinderFactories[i](limits, options);

                matchFinders[i] = matchFinder;
            }

            return(matchFinders);
        }
Esempio n. 7
0
        private IMatchFinder[] BuildMatchFinders(FindOptions options)
        {
            if (_matchFinderFactories.Count != _limitFactories.Count)
            {
                throw new InvalidOperationException("One match finder has no limitations.");
            }

            var matchFinders = new IMatchFinder[_limitFactories.Count];

            for (var i = 0; i < _limitFactories.Count; i++)
            {
                var limit       = _limitFactories[i]();
                var matchFinder = _matchFinderFactories[i](options, limit);

                matchFinders[i] = matchFinder;
            }

            return(matchFinders);
        }
Esempio n. 8
0
            TTransaction2[] unmatchedTransactions2) GetMatches(
            IMatchFinder <TTransaction1, TTransaction2> matcher,
            IList <TTransaction1> transactions1,
            IList <TTransaction2> transactions2)
        {
            var matches = matcher.GetMatches(transactions1, transactions2);

            var unmatchedTransactions1 = transactions1
                                         .Except(
                matches.Select(x => x.Item1))
                                         .ToArray();

            var unmatchedTransactions2 = transactions2
                                         .Except(
                matches.Select(x => x.Item2))
                                         .ToArray();

            return(matches, unmatchedTransactions1, unmatchedTransactions2);
        }
Esempio n. 9
0
 internal static void MatchFinder_CreateVTable(CMatchFinder p, out IMatchFinder vTable)
 {
     //TR("MatchFinder_CreateVTable", p.mNumHashBytes);
     if (!p.mBtMode)
     {
         vTable = new MatchFinderHc4();
     }
     else if (p.mNumHashBytes == 2)
     {
         vTable = new MatchFinderBt2();
     }
     else if (p.mNumHashBytes == 3)
     {
         vTable = new MatchFinderBt3();
     }
     else
     {
         vTable = new MatchFinderBt4();
     }
 }
Esempio n. 10
0
 internal static void Prepare(Func <Arena, IArenaFactory> factory_factory, Func <Arena, int, IMatchFinder> match_finder_factory, ref List <IMatchFinder> match_finders_sink, ref List <IArenaFactory> arena_sink) //LOL, don't care.
 {
     for (var i = 0; i < ArenaTypesCount; ++i)
     {
         var arena = arena_enum_values[i + 1];
         for (var j = 0; j < MaxTeamSizeValue; ++j)
         {
             var           team_size    = team_size_enum_values[j];
             IArenaFactory factory      = null;
             IMatchFinder  match_finder = null;
             if (ArenaOps.IsMatchSizeSupported(arena, team_size))
             {
                 factory      = factory_factory(arena);
                 match_finder = match_finder_factory(arena, j + 1);
             }
             match_finders_sink.Add(match_finder);
             arena_sink.Add(factory); //The team size as human number
         }
     }
 }
Esempio n. 11
0
        void Create()
        {
            if (_matchFinder == null)
            {
                BinTree bt = new BinTree();
                int numHashBytes = 4;
                if (_matchFinderType == EMatchFinderType.BT2)
                    numHashBytes = 2;
                bt.SetType(numHashBytes);
                _matchFinder = bt;
            }
            _literalEncoder.Create(_numLiteralPosStateBits, _numLiteralContextBits);

            if (_dictionarySize == _dictionarySizePrev && _numFastBytesPrev == _numFastBytes)
                return;
            _matchFinder.Create(_dictionarySize, kNumOpts, _numFastBytes, Base.kMatchMaxLen + 1);
            _dictionarySizePrev = _dictionarySize;
            _numFastBytesPrev = _numFastBytes;
        }
Esempio n. 12
0
 public void SetCoderProperties(CoderPropID[] propIDs, object[] properties)
 {
     for (UInt32 i = 0; i < properties.Length; i++)
     {
         object prop = properties[i];
         switch (propIDs[i])
         {
             case CoderPropID.NumFastBytes:
             {
                 if (!(prop is Int32))
                     throw new InvalidParamException();
                 Int32 numFastBytes = (Int32)prop;
                 if (numFastBytes < 5 || numFastBytes > Base.kMatchMaxLen)
                     throw new InvalidParamException();
                 _numFastBytes = (UInt32)numFastBytes;
                 break;
             }
             case CoderPropID.Algorithm:
             {
                 /*
                 if (!(prop is Int32))
                     throw new InvalidParamException();
                 Int32 maximize = (Int32)prop;
                 _fastMode = (maximize == 0);
                 _maxMode = (maximize >= 2);
                 */
                 break;
             }
             case CoderPropID.MatchFinder:
             {
                 if (!(prop is String))
                     throw new InvalidParamException();
                 EMatchFinderType matchFinderIndexPrev = _matchFinderType;
                 int m = FindMatchFinder(((string)prop).ToUpper());
                 if (m < 0)
                     throw new InvalidParamException();
                 _matchFinderType = (EMatchFinderType)m;
                 if (_matchFinder != null && matchFinderIndexPrev != _matchFinderType)
                     {
                     _dictionarySizePrev = 0xFFFFFFFF;
                     _matchFinder = null;
                     }
                 break;
             }
             case CoderPropID.DictionarySize:
             {
                 const int kDicLogSizeMaxCompress = 30;
                 if (!(prop is Int32))
                     throw new InvalidParamException(); ;
                 Int32 dictionarySize = (Int32)prop;
                 if (dictionarySize < (UInt32)(1 << Base.kDicLogSizeMin) ||
                     dictionarySize > (UInt32)(1 << kDicLogSizeMaxCompress))
                     throw new InvalidParamException();
                 _dictionarySize = (UInt32)dictionarySize;
                 int dicLogSize;
                 for (dicLogSize = 0; dicLogSize < (UInt32)kDicLogSizeMaxCompress; dicLogSize++)
                     if (dictionarySize <= ((UInt32)(1) << dicLogSize))
                         break;
                 _distTableSize = (UInt32)dicLogSize * 2;
                 break;
             }
             case CoderPropID.PosStateBits:
             {
                 if (!(prop is Int32))
                     throw new InvalidParamException();
                 Int32 v = (Int32)prop;
                 if (v < 0 || v > (UInt32)Base.kNumPosStatesBitsEncodingMax)
                     throw new InvalidParamException();
                 _posStateBits = (int)v;
                 _posStateMask = (((UInt32)1) << (int)_posStateBits) - 1;
                 break;
             }
             case CoderPropID.LitPosBits:
             {
                 if (!(prop is Int32))
                     throw new InvalidParamException();
                 Int32 v = (Int32)prop;
                 if (v < 0 || v > (UInt32)Base.kNumLitPosStatesBitsEncodingMax)
                     throw new InvalidParamException();
                 _numLiteralPosStateBits = (int)v;
                 break;
             }
             case CoderPropID.LitContextBits:
             {
                 if (!(prop is Int32))
                     throw new InvalidParamException();
                 Int32 v = (Int32)prop;
                 if (v < 0 || v > (UInt32)Base.kNumLitContextBitsMax)
                     throw new InvalidParamException(); ;
                 _numLiteralContextBits = (int)v;
                 break;
             }
             case CoderPropID.EndMarker:
             {
                 if (!(prop is Boolean))
                     throw new InvalidParamException();
                 SetWriteEndMarkerMode((Boolean)prop);
                 break;
             }
             default:
                 throw new InvalidParamException();
         }
     }
 }
Esempio n. 13
0
        public byte[] Compress(byte[] input, int windowBits = 26, NibLZPerformance mode = NibLZPerformance.NORMAL)
        {
            if (windowBits < 15 || windowBits > 28)
            {
                throw new ArgumentException("Invalid window size");
            }

            switch (mode)
            {
            case NibLZPerformance.VERY_FAST: Matcher = new HashTable3(input, windowBits);        LazyParse = false; break;

            case NibLZPerformance.FAST:      Matcher = new HashChain3(input, windowBits, 4);     LazyParse = false; break;

            case NibLZPerformance.NORMAL:    Matcher = new MetaHT3HC4(input, windowBits, 16);    LazyParse = true;  break;

            case NibLZPerformance.BETTER:    Matcher = new MetaHT3HC4HC8(input, windowBits, 16); LazyParse = true;  break;

            case NibLZPerformance.BEST:      Matcher = new MetaHT3HC4HC8(input, windowBits, 64); LazyParse = true;  break;
            }

            // Initialize state vars
            Output.Clear();
            int inputSize = input.Length;

            CachedPosition        = -1;
            SecondNibbleAvailable = false;
            SecondNibbleOffset    = 0;

            int inputOffset  = 0;
            int literalCount = 0;

            // Compression loop
            while (inputOffset <= inputSize)
            {
                var  match          = FindBestMatch(inputOffset);
                bool matchAvailable = match.Benefit > 0;

                if (matchAvailable && LazyParse)
                {
                    var nextMatch = PeekNextMatch(inputOffset + 1);
                    if (literalCount != 7 && nextMatch.Benefit > match.Benefit)
                    {
                        matchAvailable = false;
                    }
                }

                if (matchAvailable || (inputOffset == inputSize)) // Time to write a command
                {
                    if (literalCount > 0)                         // Emit literal packet if we've accumulated any literals
                    {
                        EmitLiteralCommand(literalCount);
                        for (int literalOffset = inputOffset - literalCount; literalOffset < inputOffset; literalOffset++)
                        {
                            Output.Add(input[literalOffset]);
                        }
                        literalCount = 0;
                    }

                    if (matchAvailable)   // Emit match if available
                    {
                        EmitMatchCommand(match.Length);
                        Output.EncodeMod7(match.Distance - 1);
                        inputOffset += match.Length;
                    }
                    if (inputOffset == inputSize)
                    {
                        break;
                    }
                }
                else
                {
                    inputOffset++;
                    literalCount++;
                }
            }
            return(Output.ToArray());
        }
Esempio n. 14
0
 internal void MatchFinderMt_CreateVTable(out IMatchFinder vTable)
 {
     // Careful: don't use this.mNumHashBytes - it hasn't been initialized yet!
     TR("MatchFinderMt_CreateVTable", base.mNumHashBytes);
     switch(base.mNumHashBytes)
     {
     case 2:
         vTable = mInterface = new MatchFinderMt2();
         break;
     case 3:
         vTable = mInterface = new MatchFinderMt3();
         break;
     default:
     #if PROTOTYPE
         vTable = mInterface = new MatchFinderMt5();
         break;
     case 4:
     #endif
         if(base.mBigHash)
             vTable = mInterface = new MatchFinderMt4b();
         else
             vTable = mInterface = new MatchFinderMt4a();
         break;
     }
 }
Esempio n. 15
0
        public byte[] Compress(byte[] input, int windowBits = 26, LZHPerformance mode = LZHPerformance.NORMAL)
        {
            switch (mode)
            {
            case LZHPerformance.L1_VeryFast: Matcher = new HashTable3(input, windowBits); LazyParse = false; break;

            case LZHPerformance.L2_VeryFast: Matcher = new HashTable3(input, windowBits); LazyParse = true;  break;

            case LZHPerformance.L3_Fast:     Matcher = new MetaHT3HC4(input, windowBits, 4); LazyParse = false; break;

            case LZHPerformance.L4_Fast:     Matcher = new MetaHT3HC4(input, windowBits, 4); LazyParse = true; break;

            case LZHPerformance.NORMAL:      Matcher = new MetaHT3HC4HC8(input, 26, 16); LazyParse = false; break;

            case LZHPerformance.BETTER:      Matcher = new MetaHT3HC4HC8(input, 26, 32); LazyParse = true; break;
            }

            int inputSize   = input.Length;
            int inputOffset = 0;

            Output         = new BitWriterFwd();
            CachedPosition = -1;
            CachedMatch    = default;
            SymbolFreqs    = new int[NUM_SYMBOLS];
            ChunkBuffer    = new ushort[Math.Min(inputSize, CHUNK_SIZE)];
            MatchEncodes   = new Queue <MatchData>();

            CalcChunkEnd(inputOffset, inputSize);
            while (true)
            {
                var  match          = FindBestMatch(inputOffset);
                bool matchAvailable = match.Benefit > 0;

                if (matchAvailable && LazyParse)
                {
                    var nextMatch = PeekNextMatch(inputOffset + 1);
                    if (nextMatch.Benefit > match.Benefit)
                    {
                        matchAvailable = false;
                    }
                }

                if (matchAvailable)
                {
                    if (match.Length > MAX_MATCH)
                    {
                        match.Length = MAX_MATCH;
                    }
                    EmitMatch(match.Length, match.Distance);
                    inputOffset += match.Length;
                }
                else     // No match available, emit literal
                {
                    EmitSymbol(input[inputOffset++]);
                }

                // Encode chunk if we've reached end of chunk
                if (inputOffset >= ChunkEnd)
                {
                    if (ChunkEnd == inputSize)
                    {
                        EmitSymbol(SYM_EOF);
                    }
                    else
                    {
                        EmitSymbol(SYM_NEWTREE);
                    }

                    EncodeChunk();
                    if (inputOffset >= inputSize)
                    {
                        break;
                    }

                    CalcChunkEnd(inputOffset, inputSize);
                }
            }
            return(Output.GetBuffer());
        }