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