gzip: fix bug in unpack EOB check
[debian/gzip] / gzexe.in
1 #!/bin/sh
2 # gzexe: compressor for Unix executables.
3 # Use this only for binaries that you do not use frequently.
4 #
5 # The compressed version is a shell script which decompresses itself after
6 # skipping $skip lines of shell commands.  We try invoking the compressed
7 # executable with the original name (for programs looking at their name).
8 # We also try to retain the original file permissions on the compressed file.
9 # For safety reasons, gzexe will not create setuid or setgid shell scripts.
10
11 # WARNING: the first line of this file must be either : or #!/bin/sh
12 # The : is required for some old versions of csh.
13 # On Ultrix, /bin/sh is too buggy, change the first line to: #!/bin/sh5
14
15
16 # Copyright (C) 1998, 2002, 2004, 2006-2007, 2010-2017 Free Software
17 # Foundation, Inc.
18 # Copyright (C) 1993 Jean-loup Gailly
19
20 # This program is free software; you can redistribute it and/or modify
21 # it under the terms of the GNU General Public License as published by
22 # the Free Software Foundation; either version 3 of the License, or
23 # (at your option) any later version.
24
25 # This program is distributed in the hope that it will be useful,
26 # but WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 # GNU General Public License for more details.
29
30 # You should have received a copy of the GNU General Public License along
31 # with this program; if not, write to the Free Software Foundation, Inc.,
32 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33
34 tab='   '
35 nl='
36 '
37 IFS=" $tab$nl"
38
39 version='gzexe (gzip) @VERSION@
40 Copyright (C) 2007, 2011-2017 Free Software Foundation, Inc.
41 This is free software.  You may redistribute copies of it under the terms of
42 the GNU General Public License <https://www.gnu.org/licenses/gpl.html>.
43 There is NO WARRANTY, to the extent permitted by law.
44
45 Written by Jean-loup Gailly.'
46
47 usage="Usage: $0 [OPTION] FILE...
48 Replace each executable FILE with a compressed version of itself.
49 Make a backup FILE~ of the old version of FILE.
50
51   -d             Decompress each FILE instead of compressing it.
52       --help     display this help and exit
53       --version  output version information and exit
54
55 Report bugs to <bug-gzip@gnu.org>."
56
57 decomp=0
58 res=0
59 while :; do
60   case $1 in
61   -d) decomp=1; shift;;
62   --h*) exec echo "$usage";;
63   --v*) exec echo "$version";;
64   --) shift; break;;
65   *) break;;
66   esac
67 done
68
69 if test $# -eq 0; then
70   echo >&2 "$0: missing operand
71 Try \`$0 --help' for more information."
72   exit 1
73 fi
74
75 tmp=
76 trap 'res=$?
77   test -n "$tmp" && rm -f "$tmp"
78   (exit $res); exit $res
79 ' 0 1 2 3 5 10 13 15
80
81 mktemp_status=
82
83 for i do
84   case $i in
85   -*) file=./$i;;
86   *)  file=$i;;
87   esac
88   if test ! -f "$file" || test ! -r "$file"; then
89     res=$?
90     echo >&2 "$0: $i is not a readable regular file"
91     continue
92   fi
93   if test $decomp -eq 0; then
94     if sed -e 1d -e 2q "$file" | grep "^skip=[0-9][0-9]*$" >/dev/null; then
95       echo >&2 "$0: $i is already gzexe'd"
96       continue
97     fi
98   fi
99   if test -u "$file"; then
100     echo >&2 "$0: $i has setuid permission, unchanged"
101     continue
102   fi
103   if test -g "$file"; then
104     echo >&2 "$0: $i has setgid permission, unchanged"
105     continue
106   fi
107   case /$file in
108   */basename | */bash | */cat | */chmod | */cp | \
109   */dirname | */echo | */expr | */gzip | \
110   */ln | */mkdir | */mktemp | */mv | */rm | \
111   */sed | */sh | */sleep | */test | */tail)
112     echo >&2 "$0: $i might depend on itself"; continue;;
113   esac
114
115   dir=`dirname "$file"` || dir=$TMPDIR
116   test -d "$dir" && test -w "$dir" && test -x "$dir" || dir=/tmp
117   test -n "$tmp" && rm -f "$tmp"
118   if test -z "$mktemp_status"; then
119     type mktemp >/dev/null 2>&1
120     mktemp_status=$?
121   fi
122   if test $mktemp_status -eq 0; then
123     tmp=`TMPDIR=$dir mktemp`
124   else
125     tmp=$dir/gzexe$$
126   fi && { cp -p "$file" "$tmp" 2>/dev/null || cp "$file" "$tmp"; } || {
127     res=$?
128     echo >&2 "$0: cannot copy $file"
129     continue
130   }
131   if test -w "$tmp"; then
132     writable=1
133   else
134     writable=0
135     chmod u+w "$tmp" || {
136       res=$?
137       echo >&2 "$0: cannot chmod $tmp"
138       continue
139     }
140   fi
141   if test $decomp -eq 0; then
142     (cat <<'EOF' &&
143 #!/bin/sh
144 skip=44
145
146 tab='   '
147 nl='
148 '
149 IFS=" $tab$nl"
150
151 umask=`umask`
152 umask 77
153
154 gztmpdir=
155 trap 'res=$?
156   test -n "$gztmpdir" && rm -fr "$gztmpdir"
157   (exit $res); exit $res
158 ' 0 1 2 3 5 10 13 15
159
160 if type mktemp >/dev/null 2>&1; then
161   gztmpdir=`mktemp -d`
162 else
163   gztmpdir=/tmp/gztmp$$; mkdir $gztmpdir
164 fi || { (exit 127); exit 127; }
165
166 gztmp=$gztmpdir/$0
167 case $0 in
168 -* | */*'
169 ') mkdir -p "$gztmp" && rm -r "$gztmp";;
170 */*) gztmp=$gztmpdir/`basename "$0"`;;
171 esac || { (exit 127); exit 127; }
172
173 case `echo X | tail -n +1 2>/dev/null` in
174 X) tail_n=-n;;
175 *) tail_n=;;
176 esac
177 if tail $tail_n +$skip <"$0" | gzip -cd > "$gztmp"; then
178   umask $umask
179   chmod 700 "$gztmp"
180   (sleep 5; rm -fr "$gztmpdir") 2>/dev/null &
181   "$gztmp" ${1+"$@"}; res=$?
182 else
183   echo >&2 "Cannot decompress $0"
184   (exit 127); res=127
185 fi; exit $res
186 EOF
187     gzip -cv9 "$file") > "$tmp" || {
188       res=$?
189       echo >&2 "$0: compression not possible for $i, file unchanged."
190       continue
191     }
192
193   else
194     # decompression
195     skip=44
196     skip_line=`sed -e 1d -e 2q "$file"`
197     case $skip_line in
198     skip=[0-9] | skip=[0-9][0-9] | skip=[0-9][0-9][0-9])
199       eval "$skip_line";;
200     esac
201     case `echo X | tail -n +1 2>/dev/null` in
202     X) tail_n=-n;;
203     *) tail_n=;;
204     esac
205     tail $tail_n +$skip "$file" | gzip -cd > "$tmp" || {
206       res=$?
207       echo >&2 "$0: $i probably not in gzexe format, file unchanged."
208       continue
209     }
210   fi
211   test $writable -eq 1 || chmod u-w "$tmp" || {
212     res=$?
213     echo >&2 "$0: $tmp: cannot chmod"
214     continue
215   }
216   ln -f "$file" "$file~" || {
217     res=$?
218     echo >&2 "$0: cannot backup $i as $i~"
219     continue
220   }
221   mv -f "$tmp" "$file" || {
222     res=$?
223     echo >&2 "$0: cannot rename $tmp to $i"
224     continue
225   }
226   tmp=
227 done
228 (exit $res); exit $res