Skip to content

Instantly share code, notes, and snippets.

@afb
Created June 11, 2012 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afb/2911662 to your computer and use it in GitHub Desktop.
Save afb/2911662 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Copyright (C) 1997,1998,1999, Roger Espel Llima
# Copyright (C) 2000, Sergey Babkin
# Copyright (C) 2009, Alex Kozlov
# Copyright (C) 2012, Anders F Bjorklund
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and any associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# SOFTWARE'S COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE
if ARGV.length == 0 then
f = STDIN
elsif ARGV.length == 1 then
f = open(ARGV[0], 'r')
else
puts "rpm2cpio v1.3, ruby version by afb"
puts "use: rpm2cpio [file.rpm]"
puts "dumps the contents to stdout as a GNU cpio archive"
exit 0
end
rpm = f.read(96)
magic, major, minor = rpm.unpack('NCC')
abort "Not an RPM" if magic != 0xedabeedb
abort "Not a version 3 or 4 RPM" if major != 3 and major != 4
rpm = f.read(16) or abort "No header"
while true
magic, ignore, sections, bytes = rpm.unpack('N4')
smagic, smagic2 = rpm.unpack('nN')
#STDERR.printf("0x%x 0x%x 0x%x 0x%x 0x%x\n",
# f.tell-16, magic, sections, bytes, smagic)
if smagic == 0x1f8b then
filter = "gzip -cd"
break
end
# BZh
if smagic == 0x425a and (smagic2 & 0xff000000) == 0x68000000 then
filter = "bzip2 -cd"
break
end
# 0xFD, '7zXZ', 0x0
if smagic == 0xfd37 and smagic2 == 0x7a585a00 then
filter = "xz -cd"
break
end
# assume lzma if there is no sig
if magic != 0x8eade801 then
filter = "lzma -cd"
break
end
# skip the headers
f.seek(16*sections+bytes, IO::SEEK_CUR) or abort "File is too small"
begin
rpm = f.read(1) or abort "No header"
end while 0 == rpm.unpack('C').at(0)
rpm += f.read(15) or abort "No header"
end
zcat = open("| #{filter}", 'w') or abort "can't pipe to #{filter}"
while rpm
zcat.syswrite(rpm) rescue Errno::EPIPE
rpm = f.read(10240) # read in blocks
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment