jQuery RSS/ATOM feed parser plugin
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.0 KiB

  1. #!perl
  2. #jsPacker (July 2005)
  3. #
  4. use strict;
  5. use Pack;
  6. use vars qw($PROGNAME $VERSION
  7. $opt_h $opt_q $opt_v $opt_i $opt_o $opt_e $opt_f $opt_s);
  8. use Getopt::Std;
  9. $PROGNAME = $0;
  10. $VERSION = '1.00b';
  11. my $Description = 'A JavaScript Compressor/Obfuscator';
  12. my $Version = "v$VERSION\[p$Pack::VERSION-pm$Pack::PM_VERSION\]";
  13. # "English" versions of settings
  14. my %ENCODINGS = (0=>'None', 10=>'Decimal', 36=>'Normal', 62=>'Normal', 95=>'High-ascii');
  15. my %SETTINGS = (0=>'No', 1=>'Yes');
  16. exit(0) if &main();
  17. exit(1);
  18. ################
  19. # Sub-routines #
  20. ################
  21. # Main program
  22. sub main {
  23. # Get command line options
  24. &getopts('hqvfsi:o:e:');
  25. $opt_h ||= 0; # $opt_h shows usage and exits
  26. $opt_q ||= 0; # $opt_q sets quiet mode (no stdout output)
  27. $opt_v ||= 0; # $opt_v shows version and exits
  28. $opt_i ||= ''; # $opt_i is input file. Required!
  29. $opt_o ||= ''; # $opt_o is output file. If not set, use standard output
  30. $opt_e ||= 0; # $opt_e encoding level (0,10,36,62,95)
  31. $opt_f ||= 0; # $opt_f use fast decoding
  32. $opt_s ||= 0; # $opt_x use special characters
  33. # Display help or version if requested
  34. if ($opt_h) {&usage("help")}
  35. if ($opt_v) {&usage("version")}
  36. # Constrain encoding level, fastdecoding and specialcharacters to allowed limits
  37. $opt_e = ($opt_e > 0) ? ($opt_e > 10) ? ($opt_e > 36) ? ($opt_e > 62) ? 95 : 62 : 36 : 10 : 0;
  38. $opt_f = ($opt_f) ? 1 : 0;
  39. $opt_s = ($opt_s) ? 1 : 0;
  40. # Do the job if an input file is specified
  41. if ($opt_i) {
  42. # Read the source script
  43. my $script = &readInputFile($opt_i);
  44. # Pack the source script
  45. my $packedscript = &Pack::pack($script,$opt_e, $opt_f, $opt_s);
  46. # Show what happened (if not in quiet mode)
  47. if (!$opt_q) {showJobDetails($opt_i, $opt_o, $opt_e, $opt_f,$opt_s,\$script,\$packedscript)}
  48. # Output the packed script
  49. if ($opt_o) {&writeOutputFile($opt_o,\$packedscript)} # to output file if specifed
  50. else {print "$packedscript"} # otherwise to STDOUT
  51. }
  52. else { # If no input file is specified, display help
  53. &usage();
  54. }
  55. return(1);
  56. }
  57. ######################
  58. sub showJobDetails { #
  59. ######################
  60. # Show details of input/output files, settings and compression ratio
  61. my ($inputfile, $outputfile,
  62. $encoding, $fastdecode, $specialchars,
  63. $instringref, $outstringref) = @_;
  64. print "$PROGNAME $Version\n";
  65. print "\tSource file : ";
  66. print "\"$inputfile\"\n";
  67. print (($outputfile) ? ("\tOutput file : \"$outputfile\"\n") : ''); # Print only if output is going to a file
  68. print "\tSettings : encoding=$ENCODINGS{$encoding} fastdecode=$SETTINGS{$fastdecode} specialchars=$SETTINGS{$specialchars}\n";
  69. print "\tCompression : " . &compressionRatio($instringref, $outstringref). "\n\n";
  70. }
  71. #####################
  72. sub readInputFile { #
  73. #####################
  74. # Read content (source script) from input file
  75. my $filename = shift;
  76. open(FH, $filename) || die "Error!!! Problem opening input file \"$filename\"!\n";
  77. my @content = <FH>;
  78. close(FH);
  79. return join('',@content);
  80. }
  81. #######################
  82. sub writeOutputFile { #
  83. #######################
  84. # Write content (packed script) to output file
  85. my ($filename,$refcontent) = @_;
  86. open(FH, ">$filename") || die "Error!!! Problem opening output file \"$filename\"\n";
  87. print(FH $$refcontent);
  88. close(FH);
  89. }
  90. ########################
  91. sub compressionRatio { #
  92. ########################
  93. # Calculate the ratio of output string to input string
  94. my ($sref1,$sref2) = @_;
  95. my $ratio = (length($$sref2) / (length($$sref1)||1));
  96. $ratio = sprintf "%.2f", $ratio;
  97. return $ratio;
  98. }
  99. #############
  100. sub usage { #
  101. #############
  102. # Inform user about usage, version and exit
  103. my $showusage = 0;
  104. my $showversion = 0;
  105. my $params = shift;
  106. if (defined $params) {
  107. if ($params eq "help") {$showusage = 1;}
  108. elsif ($params eq "version") {$showversion = 1;}
  109. else {$showusage = 1;}
  110. }
  111. else {$showversion = 1;}
  112. if ($showversion) {
  113. print<<EOT;
  114. $PROGNAME $Version
  115. $Description
  116. \tBased on "Packer.js" by Dean Edwards <http://dean.edwards.name/>
  117. \tPorted to Perl by Rob Seiler, ELR Software Pty Ltd <http://www.elr.com.au>
  118. \tCopyright 2005. License <http://creativecommons.org/licenses/LGPL/2.1/>
  119. Use "$PROGNAME -h" for options
  120. EOT
  121. exit(1);
  122. }
  123. if ($showusage) {
  124. print<<EOT;
  125. $PROGNAME $Version
  126. $Description
  127. Usage:
  128. \t$PROGNAME -i inputfile [-o outputfile] [-eX] [-f] [-s] [-qvh]\n
  129. \t-i <inputfile> (eg -i myscript.js)
  130. \t-o <outputfile> (eg -o myscript-p.js)
  131. \t-eN <encoding> [0=None 10=Numeric 62=Normal(alphanumeric) 95=High-ascii]
  132. \t-f <fast decode>
  133. \t-s <special characters>
  134. \t-q quiet mode
  135. \t-v version
  136. \t-h help
  137. Examples:
  138. \t$PROGNAME -i myscript.js
  139. \t$PROGNAME -i myscript.js -o packed.js
  140. \t$PROGNAME -i myscript.js -o packed.js -e10 -f -s
  141. \t$PROGNAME -i myscript.js -e95 -fsq > packed.js
  142. EOT
  143. exit(1);
  144. }
  145. }