1#************************************************************** 2# 3# Licensed to the Apache Software Foundation (ASF) under one 4# or more contributor license agreements. See the NOTICE file 5# distributed with this work for additional information 6# regarding copyright ownership. The ASF licenses this file 7# to you under the Apache License, Version 2.0 (the 8# "License"); you may not use this file except in compliance 9# with the License. You may obtain a copy of the License at 10# 11# http://www.apache.org/licenses/LICENSE-2.0 12# 13# Unless required by applicable law or agreed to in writing, 14# software distributed under the License is distributed on an 15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16# KIND, either express or implied. See the License for the 17# specific language governing permissions and limitations 18# under the License. 19# 20#************************************************************** 21 22 23 24package installer::upx; 25 26use installer::converter; 27use installer::existence; 28use installer::globals; 29use installer::logger; 30use installer::pathanalyzer; 31use installer::scriptitems; 32use installer::systemactions; 33 34##################################################################### 35# Checking whether a file has to be stripped 36##################################################################### 37 38sub is_upx_candidate 39{ 40 my ( $filename, $onefile ) = @_; 41 42 my $useupx = 0; 43 44 if (( $filename =~ /\.so\s*$/ ) || 45 ( $filename =~ /\.dll\s*$/ ) || 46 ( $filename =~ /\.exe\s*$/ ) || 47 ( $filename =~ /\.bin\s*$/ )) 48 { 49 my $styles = ""; 50 if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; } 51 if ( ! ( $styles =~ /\bDONT_UPX\b/ )) { $useupx = 1; } 52 } 53 54 return $useupx; 55} 56 57##################################################################### 58# Checking whether a file has to be stripped 59##################################################################### 60 61sub do_upx 62{ 63 my ( $filename ) = @_; 64 65 my $compression = "9"; 66 my $systemcall = $installer::globals::upxfile . " -" . $compression . " " . $filename; 67 68 my $returnvalue = system($systemcall); 69 70 my $infoline = "Systemcall: $systemcall\n"; 71 push( @installer::globals::logfileinfo, $infoline); 72 73 if ($returnvalue) 74 { 75 $infoline = "WARNING: Could not successfully upx $filename! Using original file.\n"; 76 push( @installer::globals::logfileinfo, $infoline); 77 } 78 else 79 { 80 $infoline = "SUCCESS: upx $filename!\n"; 81 push( @installer::globals::logfileinfo, $infoline); 82 } 83 84 return $returnvalue; 85} 86 87##################################################################### 88# Using upx to decrease file size 89##################################################################### 90 91sub upx_on_libraries 92{ 93 my ( $filelist, $languagestringref) = @_; 94 95 installer::logger::include_header_into_logfile("UPX'ing files:"); 96 my $infoline = ""; 97 98 if ( ! $installer::globals::upx_in_path ) 99 { 100 $infoline = "\n\nWarning: This is an UPX product, but upx was not found in PATH!\n\n"; 101 push( @installer::globals::logfileinfo, $infoline); 102 } 103 else 104 { 105 $infoline = "Using upx: $installer::globals::upxfile\n"; 106 push( @installer::globals::logfileinfo, $infoline); 107 108 my $upxdirbase = installer::systemactions::create_directories("upx", $languagestringref); 109 110 if (! installer::existence::exists_in_array($upxdirbase, \@installer::globals::removedirs)) 111 { 112 push(@installer::globals::removedirs, $upxdirbase); 113 } 114 115 for ( my $i = 0; $i <= $#{$filelist}; $i++ ) 116 { 117 my $sourcefilename = ${$filelist}[$i]->{'sourcepath'}; 118 119 if ( is_upx_candidate($sourcefilename, ${$filelist}[$i]) ) 120 { 121 my $shortfilename = $sourcefilename; 122 installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$shortfilename); 123 124 $infoline = "\nUpx: $shortfilename"; 125 push( @installer::globals::logfileinfo, $infoline); 126 127 # copy file into directory for stripped libraries 128 my $onelanguage = ${$filelist}[$i]->{'specificlanguage'}; 129 130 # files without language into directory "00" 131 if ($onelanguage eq "") { $onelanguage = "00"; } 132 133 my $upxdir = $upxdirbase . $installer::globals::separator . $onelanguage; 134 installer::systemactions::create_directory($upxdir); # creating language specific subdirectories 135 136 my $destfilename = $upxdir . $installer::globals::separator . $shortfilename; 137 installer::systemactions::copy_one_file($sourcefilename, $destfilename); 138 139 # change sourcepath in files collector 140 ${$filelist}[$i]->{'sourcepath'} = $destfilename; 141 142 # do upx on file 143 my $return = do_upx($destfilename); 144 145 # Using original file, if upx was not successful (no reason for error) 146 if ( $return ) { ${$filelist}[$i]->{'sourcepath'} = $sourcefilename; } 147 } 148 } 149 } 150} 151 1521; 153