Sign in to follow this  
Followers 0
crashdemons

Alternative Script Compressor

5 posts in this topic

#1 ·  Posted (edited)

Bleh, I missed the fact that the Obfuscator could already do this.

What, another stupid script from me? Yes, sigh. :x

If you've ever seen Javascript where all variables and functions are only one or two letters, you know what this is about. However, this works on AutoIt scripts.

I don't know if this already exists, but this script will rename variables and functions so that they take up the least room. This script will also remove indention,comments, and blank lines.

This isn't really meant as a obfuscator, so; just a shortener.

Do we need this script? probably not, but I was bored.

Does something like this already exist? No idea, if it does please smack me with a keyboard.

You will need to edit the input and output strings to select your own script.

Global $VarnameGen=StringSplit("abcdefghjiklmnopqrstuvwxyz0124375689",""); don't edit this

Global $input_file='test.au3'     ;<------ EDIT THIS
Global $output_file='test_out.au3';<------ EDIT THIS

FileWrite($output_file,Compress(FileRead($input_file)))


Func Compress($s)
    $s=StringStripCR (StringStripWS($s,1+2))
    Local $a=StringSplit($s,@LF)
    Local $o=""

    Local $funcs=FindFuncs($a)

    Local $v[1][2]
    Local $vi=0
    Local $f[1][2]
    Local $fi=0

    Local $commentblock=False
    For $i=1 To UBound($a)-1
        ConsoleWrite(@CRLF&'>'&$i&' | '&$a[$i]&@CRLF)
        LineProcess($a[$i],$commentblock, $v,$vi ,$f,$fi, $funcs)
        ConsoleWrite(@CRLF&'!'&$i&' | '&$a[$i]&@CRLF)
    Next

    $s=""
    For $i=1 To UBound($a)-1
        If $a[$i]<>'' Then $s&=$a[$i]&@CRLF
    Next
    Return $s
EndFunc
Func FindFuncs(ByRef $a)
    Local $sf=""
    For $i=1 To UBound($a)-1
        $a[$i]=StringStripWS($a[$i],1)
        If StringLeft($a[$i],4)='Func' Then
            Switch StringMid($a[$i],5,1)
                Case ' ','  '
                    Local $p=StringInStr($a[$i],'(')
                    If $p>6 Then
                        $sf&=StringStripWS(StringMid($a[$i],6,$p-6),8)&','
                    EndIf
            EndSwitch
        EndIf
    Next
    Return StringSplit($sf,',')
EndFunc
Func LineProcess(ByRef $l, Byref $commentblock, ByRef $v, ByRef $vi, Byref $f, ByRef $fi, ByRef $funcs)
    Local $variable=False
    Local $variable_s,$variable_e
    Local $variable_str=""

    Local $function_maybe=False
    Local $function_spacepassed=False
    Local $function_s,$function_e
    Local $function_str=""


    Local $string=False
    Local $string_chr=''

    ;$l=StringStripWS($l,1); done by findfuncs... only here in case we add options to disable Func compression
    If $commentblock Then
        If StringInStr($l,'#ce')=1 Then; BTW this line is a comment regardless
            ConsoleWrite(@CRLF&'-}ce'&@CRLF)
            $commentblock=False
        EndIf
        $l=""
        Return ;ret what? -
    EndIf
    For $p=1 To StringLen($l)+1; necessary for variable ending match at end of line
        Local $c=StringMid($l,$p,1)
        ConsoleWrite($c)
        If $string Then; inside a quoted block of text
            Switch $c
                Case '"',"'"
                    If $c==$string_chr Then
                        ConsoleWrite(@CRLF&'-}str'&@CRLF)
                        $string=false; ends a quoted block of text
                    EndIf
                EndSwitch
        Else;not inside a quoted block of text
            If $variable Then
                If StringRegExp($c,'^[a-zA-Z0-9_]$') Then
                    $variable_str&=$c
                Else;invalid variable character implies end of variable name
                    $variable=False
                    $variable_e=$p-1
                    ConsoleWrite(@CRLF&'-}var: '&$variable_str&@CRLF)
                    If StringLen($variable_str) Then
                        Local $sub=VarNameCache($v,$vi, $variable_str)
                        ConsoleWrite(':  '&$sub&@CRLF)
                        $variable_e=StringReplaceSub($l, $sub, $variable_s, $variable_e)
                        $p=$variable_e+1
                    EndIf
                EndIf
            Else
                ;not a variable and not in a string - either a keyword or a function!
                If StringRegExp($c,'^[a-zA-Z0-9_]$') Then
                    If $function_maybe And (Not $function_spacepassed) Then
                        $function_str&=$c
                    Else
                        $function_maybe=True
                        $function_spacepassed=False
                        $function_str=$c
                        $function_s=$p
                        ConsoleWrite(@CRLF&'+{func ?'&@CRLF)
                    EndIf
                Else
                    If $function_maybe Then
                        Switch $c
                            Case ' ','  '
                                $function_spacepassed=True
                                ;do nothing, doesn't append to nor ends the function text
                            Case '('

                                $function_maybe=False
                                $function_e=$p-1
                                Local $function_def=false
                                If StringLen($function_str) Then
                                    For $i=1 To UBound($funcs)-1
                                        If $funcs[$i]=$function_str Then
                                            $function_def=True
                                            ExitLoop
                                        EndIf
                                    Next
                                    If $function_def Then
                                        ConsoleWrite(@CRLF&'-}func: '&$function_str&@CRLF)
                                        Local $sub=VarNameCache($f,$fi,$function_str,true)
                                        ConsoleWrite(':  '&$sub&@CRLF)
                                        $function_e=StringReplaceSub($l, $sub, $function_s, $function_e)
                                        $p=$function_e+1
                                    Else
                                        ConsoleWrite(@CRLF&'-}undeffunc: '&$function_str&@CRLF)
                                    EndIf
                                EndIf
                            Case Else
                                ConsoleWrite(@CRLF&'}nofunc '&@CRLF)
                                $function_maybe=False
                        EndSwitch
                    EndIf
                EndIf
            EndIf
            Switch $c
                Case '$'
                    ConsoleWrite(@CRLF&'+{var'&@CRLF&@TAB)
                    $variable=True
                    $variable_s=$p+1
                    $variable_str=""
                Case '"',"'";begins a quoted block of text
                    ConsoleWrite(@CRLF&'+{str'&@CRLF&@TAB)
                    $string=True
                    $string_chr=$c
                Case '#'
                    If StringMid($l,$p,3)=='#cs' Then
                        ConsoleWrite(@CRLF&'+{cs'&@CRLF&@TAB)
                        $commentblock=true
                        ContinueCase
                    EndIf
                Case ';'
                    If $p=1 Then
                        $l=""
                    Else
                        $l=StringMid($l,1,$p-1)
                    EndIf
                    ExitLoop; comment follows - nothing left on this line.
            EndSwitch
        EndIf
    Next
EndFunc
Func StringReplaceSub(ByRef $s, $sub, $start, $end)
    Local $pfx="",$sfx=""
    If $start>1 Then $pfx=StringLeft($s,$start-1)
                     $sfx=StringMid ($s,$end  +1)
    $s=$pfx&$sub&$sfx
    Return $end + (StringLen($sub)- (($end-$start) +1))
EndFunc
Func VarNameCache(ByRef $v,ByRef $vi, ByRef $name,$function=false)
    For $i=0 To $vi-1
        If $v[$i][0]==$name Then Return SetError(0,$i,$v[$i][1])
    Next
    $v[$vi][0]=$name
    Local $m=36
    If $function Then $m=26
    $v[$vi][1]=VarNameFromIdx($vi,$m)
    $vi+=1
    ReDim $v[$vi+1][2]
    Return SetError(0,$vi-1,$v[$vi-1][1])
EndFunc
Func VarNameFromIdx($i,$m=36)
    ; abcdefghjiklmnopqrstuvwxyz0124375689
    Local $s=''
    While $i>=0
        Local $cp=Mod($i,$m);first character 0-35
        $s&=$VarnameGen[$cp+1];stringsplit is 1-based
        $i=Int($i/$m)-1;0-35 second character
    WEnd
    Return $s
EndFunc

Obligatory output when the compressor compresses itself:

Global $a=StringSplit("abcdefghjiklmnopqrstuvwxyz0124375689","")
Global $b='test.au3'
Global $c='test_out.au3'
FileWrite($c,a(FileRead($b)))
Func a($d)
$d=StringStripCR (StringStripWS($d,1+2))
Local $e=StringSplit($d,@LF)
Local $f=""
Local $g=b($e)
Local $h[1][2]
Local $j=0
Local $i[1][2]
Local $k=0
Local $l=False
For $m=1 To UBound($e)-1
ConsoleWrite(@CRLF&'>'&$m&' | '&$e[$m]&@CRLF)
c($e[$m],$l, $h,$j ,$i,$k, $g)
ConsoleWrite(@CRLF&'!'&$m&' | '&$e[$m]&@CRLF)
Next
$d=""
For $m=1 To UBound($e)-1
If $e[$m]<>'' Then $d&=$e[$m]&@CRLF
Next
Return $d
EndFunc
Func b(ByRef $e)
Local $n=""
For $m=1 To UBound($e)-1
$e[$m]=StringStripWS($e[$m],1)
If StringLeft($e[$m],4)='Func' Then
Switch StringMid($e[$m],5,1)
Case ' ','  '
Local $o=StringInStr($e[$m],'(')
If $o>6 Then
$n&=StringStripWS(StringMid($e[$m],6,$o-6),8)&','
EndIf
EndSwitch
EndIf
Next
Return StringSplit($n,',')
EndFunc
Func c(ByRef $p, Byref $l, ByRef $h, ByRef $j, Byref $i, ByRef $k, ByRef $g)
Local $q=False
Local $r,$s
Local $t=""
Local $u=False
Local $v=False
Local $w,$x
Local $y=""
Local $z=False
Local $0=''
If $l Then
If StringInStr($p,'#ce')=1 Then
ConsoleWrite(@CRLF&'-}ce'&@CRLF)
$l=False
EndIf
$p=""
Return
EndIf
For $o=1 To StringLen($p)+1
Local $1=StringMid($p,$o,1)
ConsoleWrite($1)
If $z Then
Switch $1
Case '"',"'"
If $1==$0 Then
ConsoleWrite(@CRLF&'-}str'&@CRLF)
$z=false
EndIf
EndSwitch
Else
If $q Then
If StringRegExp($1,'^[a-zA-Z0-9_]$') Then
$t&=$1
Else
$q=False
$s=$o-1
ConsoleWrite(@CRLF&'-}var: '&$t&@CRLF)
If StringLen($t) Then
Local $2=d($h,$j, $t)
ConsoleWrite(':  '&$2&@CRLF)
$s=e($p, $2, $r, $s)
$o=$s+1
EndIf
EndIf
Else
If StringRegExp($1,'^[a-zA-Z0-9_]$') Then
If $u And (Not $v) Then
$y&=$1
Else
$u=True
$v=False
$y=$1
$w=$o
ConsoleWrite(@CRLF&'+{func ?'&@CRLF)
EndIf
Else
If $u Then
Switch $1
Case ' ','  '
$v=True
Case '('
$u=False
$x=$o-1
Local $4=false
If StringLen($y) Then
For $m=1 To UBound($g)-1
If $g[$m]=$y Then
$4=True
ExitLoop
EndIf
Next
If $4 Then
ConsoleWrite(@CRLF&'-}func: '&$y&@CRLF)
Local $2=d($i,$k,$y,true)
ConsoleWrite(':  '&$2&@CRLF)
$x=e($p, $2, $w, $x)
$o=$x+1
Else
ConsoleWrite(@CRLF&'-}undeffunc: '&$y&@CRLF)
EndIf
EndIf
Case Else
ConsoleWrite(@CRLF&'}nofunc '&@CRLF)
$u=False
EndSwitch
EndIf
EndIf
EndIf
Switch $1
Case '$'
ConsoleWrite(@CRLF&'+{var'&@CRLF&@TAB)
$q=True
$r=$o+1
$t=""
Case '"',"'"
ConsoleWrite(@CRLF&'+{str'&@CRLF&@TAB)
$z=True
$0=$1
Case '#'
If StringMid($p,$o,3)=='#cs' Then
ConsoleWrite(@CRLF&'+{cs'&@CRLF&@TAB)
$l=true
ContinueCase
Else
Return
EndIf
Case ';'
If $o=1 Then
$p=""
Else
$p=StringMid($p,1,$o-1)
EndIf
ExitLoop
EndSwitch
EndIf
Next
EndFunc
Func e(ByRef $d, $2, $3, $7)
Local $5="",$6=""
If $3>1 Then $5=StringLeft($d,$3-1)
$6=StringMid ($d,$7  +1)
$d=$5&$2&$6
Return $7 + (StringLen($2)- (($7-$3) +1))
EndFunc
Func d(ByRef $h,ByRef $j, ByRef $8,$9=false)
For $m=0 To $j-1
If $h[$m][0]==$8 Then Return SetError(0,$m,$h[$m][1])
Next
$h[$j][0]=$8
Local $aa=36
If $9 Then $aa=26
$h[$j][1]=f($j,$aa)
$j+=1
ReDim $h[$j+1][2]
Return SetError(0,$j-1,$h[$j-1][1])
EndFunc
Func f($m,$aa=36)
Local $d=''
While $m>=0
Local $ba=Mod($m,$aa)
$d&=$a[$ba+1]
$m=Int($m/$aa)-1
WEnd
Return $d
EndFunc

However, this script is very rough (and the code is horrendous and it's slow) and assumes that your input script is formatted validly, and it has some limitations:

  • will compress all variable names, declared or not.
  • will compress only function names defined in the input document.
  • no support for Eval/Assign/Execute or any string mention of an identifier.
  • will rename external variables (read: constants) that you are using in the current script.
  • assumes that the input script has no syntax errors etc (passing Au3Check is a good standard)
Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites



Obfuscator has also such option:

#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0

Br,

UEZ :x


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Obfuscator has also such option:

#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/sf /sv /om /cs=0 /cn=0

Br,

UEZ :x

Dang, I missed that even looking right at the options earlier! Bleh, I'll edit the first post so the passerby's don't bother.

Everything always seems like it's already been done these days...

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Start a parallel one! Why not!

There are very good alternative flying around! Belive me!

E.g. Another AutoIt PreProcessor by eukalyptus (German site)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Okay well, title and post have been edited - hopefully that softens the blow.

Yeah... I guess it couldn't hurt to have a different take on the same thing. (although I put more faith in Obfuscator than this.)

Also, unless I missed something, Obfuscator uses minimum 2-character variable names and 3-character function names (because of the underscores); so if I'm going to split hairs, this script has an advantage.


My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0