blob: 8199b43784d794d00f5d89866d3d1d66f7e8fa8f [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#!/bin/sh -efu
Brad Bishop6e60e8b2018-02-01 10:27:11 -05002# This file comes from rpm 4.x distribution
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003
Brad Bishop6e60e8b2018-02-01 10:27:11 -05004fatal() {
5 echo "$*" >&2
6 exit 1
7}
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
Brad Bishop6e60e8b2018-02-01 10:27:11 -05009pkg="$1"
Patrick Williams520786c2023-06-25 16:20:36 -050010[ -n "$pkg" ] && [ -e "$pkg" ] ||
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011 fatal "No package supplied"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013_dd() {
14 local o="$1"; shift
15 dd if="$pkg" skip="$o" iflag=skip_bytes status=none $*
16}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017
Brad Bishop6e60e8b2018-02-01 10:27:11 -050018calcsize() {
Patrick Williams520786c2023-06-25 16:20:36 -050019
20 case "$(_dd $1 bs=4 count=1 | tr -d '\0')" in
21 "$(printf '\216\255\350')"*) ;; # '\x8e\xad\xe8'
22 *) fatal "File doesn't look like rpm: $pkg" ;;
23 esac
24
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 offset=$(($1 + 8))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026
Brad Bishop6e60e8b2018-02-01 10:27:11 -050027 local i b b0 b1 b2 b3 b4 b5 b6 b7
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029 i=0
30 while [ $i -lt 8 ]; do
Patrick Williams520786c2023-06-25 16:20:36 -050031 # add . to not loose \n
32 # strip \0 as it gets dropped with warning otherwise
33 b="$(_dd $(($offset + $i)) bs=1 count=1 | tr -d '\0' ; echo .)"
34 b=${b%.} # strip . again
35
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 [ -z "$b" ] &&
37 b="0" ||
38 b="$(exec printf '%u\n' "'$b")"
39 eval "b$i=\$b"
40 i=$(($i + 1))
41 done
42
43 rsize=$((8 + ((($b0 << 24) + ($b1 << 16) + ($b2 << 8) + $b3) << 4) + ($b4 << 24) + ($b5 << 16) + ($b6 << 8) + $b7))
44 offset=$(($offset + $rsize))
45}
46
Patrick Williams520786c2023-06-25 16:20:36 -050047case "$(_dd 0 bs=4 count=1 | tr -d '\0')" in
Brad Bishop6e60e8b2018-02-01 10:27:11 -050048 "$(printf '\355\253\356\333')"*) ;; # '\xed\xab\xee\xdb'
49 *) fatal "File doesn't look like rpm: $pkg" ;;
50esac
51
52calcsize 96
53sigsize=$rsize
54
55calcsize $(($offset + (8 - ($sigsize % 8)) % 8))
56hdrsize=$rsize
57
Patrick Williams520786c2023-06-25 16:20:36 -050058case "$(_dd $offset bs=2 count=1 | tr -d '\0')" in
59 "$(printf '\102\132')") _dd $offset | bunzip2 ;; # '\x42\x5a'
60 "$(printf '\037\213')") _dd $offset | gunzip ;; # '\x1f\x8b'
61 "$(printf '\375\067')") _dd $offset | xzcat ;; # '\xfd\x37'
62 "$(printf '\135')") _dd $offset | unlzma ;; # '\x5d\x00'
63 "$(printf '\050\265')") _dd $offset | unzstd ;; # '\x28\xb5'
64 *) fatal "Unrecognized payload compression format in rpm file: $pkg" ;;
Brad Bishop6e60e8b2018-02-01 10:27:11 -050065esac