Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active September 5, 2021 06:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruario/9674124 to your computer and use it in GitHub Desktop.
Save ruario/9674124 to your computer and use it in GitHub Desktop.
Minimal rpm2cpio using only bash, grep, tail and a decompressor
#!/bin/bash
RPMHDRLGTH=$(LC_CTYPE=C grep -abom1 '.7zXZ\|]'$'\000\000''....'$'\377\377\377\377\377\377''\|BZh9\|'$'\037\213\b' "$1")
case "$RPMHDRLGTH" in
*7zXZ) COMPRESSOR=xz ;;
*]*) COMPRESSOR=lzma ;;
*BZh9) COMPRESSOR=bzip2 ;;
*) COMPRESSOR=gzip ;;
esac
tail -c+$[${RPMHDRLGTH%:*}+1] "$1" | $COMPRESSOR -d
@ruario
Copy link
Author

ruario commented Nov 4, 2017

For history buffs, I first wrote this back in 2012

https://web.archive.org/web/20140303100327/http://my.opera.com/ruario/blog/2012/01/15/rpm2cpio

The one in this gist is only slightly changed

@ruario
Copy link
Author

ruario commented Nov 25, 2018

These days, packages are almost exclusively XZ compressed on recent versions of RPM-based distributions. If you only need to handle files from modern versions of Fedora, SUSE, etc. then you can get away with a single line of POSIX shell:

#!/bin/sh
tail -c+`grep -Fabom1 7zXZ "$1" | cut -d: -f1` "$1" | xz -d

@ruario
Copy link
Author

ruario commented Sep 4, 2021

The above still assumes a GNU toolset. This should work on both GNU and BSD systems and even BusyBox.

#!/bin/sh
o=`strings -o "$1" | grep -m1 7zXZ | sed 's/ *\([0-9][0-9]*\) .*/\1/'`
[ `printf popular\\\novel | strings -o | sed -n '/v/s/ *\([0-9][0-9]*\) .*/\1/p'` -eq 10 ] && o=`printf %d 0$o`
tail -c+$o "$1" | xz -d

I shall leave it as an exercise as to what I am doing here and why this looks unnecessarily complex. (spoiler: it isn't) 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment