blob: cf23472ba95ae10dd8501e346d507805c07fb15a [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001#!/bin/sh -efu
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
Brad Bishop6e60e8b2018-02-01 10:27:11 -05003# This file comes from rpm 4.x distribution
Patrick Williamsc124f4f2015-09-15 14:41:29 -05004
Brad Bishop6e60e8b2018-02-01 10:27:11 -05005fatal() {
6 echo "$*" >&2
7 exit 1
8}
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
Brad Bishop6e60e8b2018-02-01 10:27:11 -050010pkg="$1"
11[ -n "$pkg" -a -e "$pkg" ] ||
12 fatal "No package supplied"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050013
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014_dd() {
15 local o="$1"; shift
16 dd if="$pkg" skip="$o" iflag=skip_bytes status=none $*
17}
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019calcsize() {
20 offset=$(($1 + 8))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
Brad Bishop6e60e8b2018-02-01 10:27:11 -050022 local i b b0 b1 b2 b3 b4 b5 b6 b7
Patrick Williamsc124f4f2015-09-15 14:41:29 -050023
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024 i=0
25 while [ $i -lt 8 ]; do
26 b="$(_dd $(($offset + $i)) bs=1 count=1)"
27 [ -z "$b" ] &&
28 b="0" ||
29 b="$(exec printf '%u\n' "'$b")"
30 eval "b$i=\$b"
31 i=$(($i + 1))
32 done
33
34 rsize=$((8 + ((($b0 << 24) + ($b1 << 16) + ($b2 << 8) + $b3) << 4) + ($b4 << 24) + ($b5 << 16) + ($b6 << 8) + $b7))
35 offset=$(($offset + $rsize))
36}
37
38case "$(_dd 0 bs=8 count=1)" in
39 "$(printf '\355\253\356\333')"*) ;; # '\xed\xab\xee\xdb'
40 *) fatal "File doesn't look like rpm: $pkg" ;;
41esac
42
43calcsize 96
44sigsize=$rsize
45
46calcsize $(($offset + (8 - ($sigsize % 8)) % 8))
47hdrsize=$rsize
48
49case "$(_dd $offset bs=3 count=1)" in
50 "$(printf '\102\132')"*) _dd $offset | bunzip2 ;; # '\x42\x5a'
51 "$(printf '\037\213')"*) _dd $offset | gunzip ;; # '\x1f\x8b'
52 "$(printf '\375\067')"*) _dd $offset | xzcat ;; # '\xfd\x37'
53 "$(printf '\135\000')"*) _dd $offset | unlzma ;; # '\x5d\x00'
54 *) fatal "Unrecognized rpm file: $pkg" ;;
55esac