Пример #1
0
        /// <summary>
        /// Sets the coder properties
        /// </summary>
        /// <param name="propIDs">The property identificators</param>
        /// <param name="properties">The array of properties</param>
        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();
                        var 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(CultureInfo.CurrentCulture));
                        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();
                        ;
                        var 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();
                        var v = (Int32) prop;
                        if (v < 0 || v > (UInt32) Base.kNumPosStatesBitsEncodingMax)
                            throw new InvalidParamException();
                        _posStateBits = v;
                        _posStateMask = (((UInt32) 1) << _posStateBits) - 1;
                        break;
                    }
                    case CoderPropId.LitPosBits:
                    {
                        if (!(prop is Int32))
                            throw new InvalidParamException();
                        var v = (Int32) prop;
                        if (v < 0 || v > Base.kNumLitPosStatesBitsEncodingMax)
                            throw new InvalidParamException();
                        _numLiteralPosStateBits = v;
                        break;
                    }
                    case CoderPropId.LitContextBits:
                    {
                        if (!(prop is Int32))
                            throw new InvalidParamException();
                        var v = (Int32) prop;
                        if (v < 0 || v > Base.kNumLitContextBitsMax)
                            throw new InvalidParamException();
                        ;
                        _numLiteralContextBits = v;
                        break;
                    }
                    case CoderPropId.EndMarker:
                    {
                        if (!(prop is Boolean))
                            throw new InvalidParamException();
                        SetWriteEndMarkerMode((Boolean) prop);
                        break;
                    }
                    default:
                        throw new InvalidParamException();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Compresses this instance.
        /// </summary>
        internal void Process()
        {
            Console.WriteLine("[*] Processing " + this.Input.Name + "..");

            if (this.Input.Extension == ".csv" || this.Input.Extension == ".sc")
            {
                if (this.Input.Extension == ".sc")
                {
                    using (MemoryStream Stream = new MemoryStream(this.Buffer))
                    {
                        using (BinaryReader Reader = new BinaryReader(Stream))
                        {
                            char[] Prefix = Reader.ReadChars(2);

                            if (string.Join("", Prefix) == "SC")
                            {
                                Console.WriteLine("[*] Skipping compression of " + this.Output.Name + this.Output.Extension);
                                this.Output = this.Input.CopyTo(this.Output.FullName, true);
                                return;
                            }
                        }
                    }
                }

                Encoder Compresser = new Encoder();

                using (MemoryStream IStream = new MemoryStream(this.Buffer))
                {
                    using (FileStream OStream = this.Output.Create())
                    {
                        CoderPropId[] CoderPropIDs = new CoderPropId[8]
                        {
                            CoderPropId.DictionarySize, CoderPropId.PosStateBits, CoderPropId.LitContextBits, CoderPropId.LitPosBits,
                            CoderPropId.Algorithm, CoderPropId.NumFastBytes, CoderPropId.MatchFinder, CoderPropId.EndMarker
                        };

                        object[] Properties = new object[8]
                        {
                            262144, 2, 3, 0, 2, 32, "bt4", false
                        };

                        Compresser.SetCoderProperties(CoderPropIDs, Properties);
                        Compresser.WriteCoderProperties(OStream);

                        OStream.Write(BitConverter.GetBytes(IStream.Length), 0, 4);

                        Compresser.Code(IStream, OStream, IStream.Length, -1L, null);
                    }

                    if (this.Input.Extension == ".sc")
                    {
                        int    Version = 1;
                        byte[] Header  = new byte[26];

                        using (BinaryWriter Writer = new BinaryWriter(new MemoryStream(Header)))
                        {
                            Writer.Write("SC".ToCharArray());
                            Writer.Write(BitConverter.GetBytes(Version).Reverse().ToArray());

                            using (var MD5Hash = MD5.Create())
                            {
                                byte[] MD5 = MD5Hash.ComputeHash(this.Buffer);

                                Writer.Write(BitConverter.GetBytes(MD5.Length).Reverse().ToArray());
                                Writer.Write(MD5);
                            }
                        }

                        byte[] OldData = File.ReadAllBytes(this.Output.FullName);
                        byte[] NewData = Header.Concat(OldData).ToArray();

                        this.Output.Delete();

                        using (FileStream OStream = this.Output.Create())
                        {
                            OStream.Write(NewData, 0, NewData.Length);
                        }
                    }
                }
            }
            else
            {
                try
                {
                    this.Output = this.Input.CopyTo(this.Output.FullName, true);
                }
                catch (Exception)
                {
                    Console.WriteLine("[*] Couldn't copy " + this.Output.Name + " to the patch folder, please do it manualy.");
                }
            }
        }