3038N/A#!/usr/bin/perl -w
307N/A
307N/A# Copyright (C) 1997,1998,1999, Roger Espel Llima
3038N/A# Copyright (C) 2000, Sergey Babkin
3038N/A# Copyright (C) 2009, Alex Kozlov
307N/A#
307N/A# Permission is hereby granted, free of charge, to any person obtaining a copy
307N/A# of this software and any associated documentation files (the "Software"), to
307N/A# deal in the Software without restriction, including without limitation the
307N/A# rights to use, copy, modify, merge, publish, distribute, sublicense,
307N/A# and/or sell copies of the Software, and to permit persons to whom the
307N/A# Software is furnished to do so, subject to the following conditions:
307N/A#
307N/A# The above copyright notice and this permission notice shall be included in
307N/A# all copies or substantial portions of the Software.
307N/A#
307N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
307N/A# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
307N/A# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
307N/A# SOFTWARE'S COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
307N/A# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
307N/A# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
307N/A# THE SOFTWARE
307N/A
307N/A# (whew, that's done!)
307N/A
307N/A# why does the world need another rpm2cpio? because the existing one
307N/A# won't build unless you have half a ton of things that aren't really
307N/A# required for it, since it uses the same library used to extract RPM's.
307N/A# in particular, it won't build on the HPsUX box i'm on.
307N/A
3038N/Ause strict;
307N/A
3038N/Amy ($f, $rpm, $filter) = ();
307N/A
307N/Aif ($#ARGV == -1) {
3038N/A $f = "STDIN";
307N/A} elsif ($#ARGV == 0) {
3038N/A open($f, "< $ARGV[0]") or die "Can't read file $ARGV[0]\n";
307N/A} else {
3038N/A print "rpm2cpio v1.3, perl version by orabidoo\n";
3038N/A print "use: rpm2cpio [file.rpm]\n";
3038N/A print "dumps the contents to stdout as a GNU cpio archive\n";
3038N/A exit 0;
307N/A}
307N/A
3038N/Aread $f, $rpm, 96;
307N/A
3038N/Amy ($magic, $major, undef) = unpack("NCC", $rpm);
307N/A
307N/Adie "Not an RPM\n" if $magic != 0xedabeedb;
3038N/Adie "Not a version 3 or 4 RPM\n" if $major != 3 and $major != 4;
307N/A
3038N/Aread $f, $rpm, 16 or die "No header\n";
3038N/Awhile(1) {
3038N/A ($magic, undef, my $sections, my $bytes) = unpack("N4", $rpm);
3038N/A my ($smagic, $smagic2) = unpack("nN", $rpm);
3038N/A
3038N/A #printf(STDERR "0x%x 0x%x 0x%x 0x%x 0x%x\n",
3038N/A # tell($f)-16, $magic, $sections, $bytes, $smagic);
307N/A
3038N/A if ($smagic == 0x1f8b) {
3038N/A $filter = "gzip -cd";
3038N/A last;
3038N/A }
3038N/A # BZh
3038N/A if ($smagic == 0x425a and ($smagic2 & 0xff000000) == 0x68000000) {
3038N/A $filter = "bzip2 -cd";
3038N/A last;
3038N/A }
3038N/A # 0xFD, '7zXZ', 0x0
3038N/A if ($smagic == 0xfd37 and $smagic2 == 0x7a585a00) {
3038N/A $filter = "xz -cd";
3038N/A last;
3038N/A }
3038N/A # assume lzma if there is no sig
3038N/A if ($magic != 0x8eade801) {
3038N/A $filter = "lzma -cd";
3038N/A last;
3038N/A }
3038N/A
3038N/A # skip the headers
3038N/A seek $f, 16 * $sections + $bytes, 1 or die "File is too small\n";
3038N/A do {
3038N/A read $f, $rpm, 1 or die "No header\n" ;
3038N/A } while(0 == unpack("C", $rpm));
3038N/A read $f, $rpm, 15, 1 or die "No header\n" ;
307N/A}
307N/A
3038N/Aopen(ZCAT, "| $filter") or die "can't pipe to $filter\n";
307N/A
3038N/Awhile($rpm ne '') {
3038N/A print ZCAT $rpm;
3038N/A read $f, $rpm, 10240; # read in blocks
3038N/A}
307N/A
307N/Aclose ZCAT;
3038N/Aclose $f;