/// <summary>
        /// Zero encoding - just removal of whitespace and comments
        /// </summary>
        /// <param name="script">Script to compress</param>
        /// <returns>Compressed script</returns>
        private string BasicCompression(string script)
        {
            ParseMaster parser = new ParseMaster();

            // make safe
            parser.EscapeChar = '\\';
            // protect strings
            parser.Add("'[^'\\n\\r]*'", IGNORE);
            parser.Add("\"[^\"\\n\\r]*\"", IGNORE);
            // remove comments
            parser.Add("\\/\\/[^\\n\\r]*[\\n\\r]");
            parser.Add("\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/");
            // protect regular expressions
            parser.Add("\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?)", "$2");
            parser.Add("[^\\w\\$\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/g?i?", IGNORE);
            // remove: ;;; doSomething();
            if (_specialChars)
            {
                parser.Add(";;[^\\n\\r]+[\\n\\r]");
            }
            // remove redundant semi-colons
            parser.Add(";+\\s*([};])", "$2");
            // remove white-space
            parser.Add("(\\b|\\$)\\s+(\\b|\\$)", "$2 $3");
            parser.Add("([+\\-])\\s+([+\\-])", "$2 $3");
            parser.Add("\\s+");
            // done
            return(parser.Exec(script));
        }
Example #2
0
            private string encodeKeywords(string script)
            {
                // escape high-ascii values already in the script (i.e. in strings)
                if (Encoding == PackerEncoding.HighAscii)
                {
                    script = escape95(script);
                }
                // create the parser
                ParseMaster  parser = new ParseMaster();
                EncodeMethod encode = getEncoder(Encoding);

                // for high-ascii, don't encode single character low-ascii
                Regex regex = new Regex(
                    (Encoding == PackerEncoding.HighAscii) ? "\\w\\w+" : "\\w+"
                    );

                // build the word list
                encodingLookup = analyze(script, regex, encode);

                // encode
                parser.Add((Encoding == PackerEncoding.HighAscii) ? "\\w\\w+" : "\\w+",
                           new ParseMaster.MatchGroupEvaluator(encodeWithLookup));

                // if encoded, wrap the script in a decoding function
                return((script == string.Empty) ? "" : bootStrap(parser.Exec(script), encodingLookup));
            }
        /// <summary>
        /// Encodes special characters
        /// </summary>
        /// <param name="script">Script to encode</param>
        /// <returns>Encoded script</returns>
        private string EncodeSpecialChars(string script)
        {
            ParseMaster parser = new ParseMaster();

            // replace: $name -> n, $$name -> na
            parser.Add("((\\$+)([a-zA-Z\\$_]+))(\\d*)", new ParseMaster.MatchGroupEvaluator(EncodeLocalVars));

            // replace: _name -> _0, double-underscore (__name) is ignored
            Regex regex = new Regex("\\b_[A-Za-z\\d]\\w*");

            // build the word list
            _encodingLookup = Analyze(script, regex, EncodePrivate);

            parser.Add("\\b_[A-Za-z\\d]\\w*", new ParseMaster.MatchGroupEvaluator(EncodeWithLookup));

            script = parser.Exec(script);
            return(script);
        }