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" |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 10 | [ -n "$pkg" ] && [ -e "$pkg" ] || |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 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() { |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 19 | |
| 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | offset=$(($1 + 8)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 27 | local i b b0 b1 b2 b3 b4 b5 b6 b7 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 29 | i=0 |
| 30 | while [ $i -lt 8 ]; do |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 31 | # 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 36 | [ -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 Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 47 | case "$(_dd 0 bs=4 count=1 | tr -d '\0')" in |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 48 | "$(printf '\355\253\356\333')"*) ;; # '\xed\xab\xee\xdb' |
| 49 | *) fatal "File doesn't look like rpm: $pkg" ;; |
| 50 | esac |
| 51 | |
| 52 | calcsize 96 |
| 53 | sigsize=$rsize |
| 54 | |
| 55 | calcsize $(($offset + (8 - ($sigsize % 8)) % 8)) |
| 56 | hdrsize=$rsize |
| 57 | |
Patrick Williams | 520786c | 2023-06-25 16:20:36 -0500 | [diff] [blame] | 58 | case "$(_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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 65 | esac |