2019-10-29 13:05:48 +01:00
|
|
|
#!/usr/bin/python3
|
2014-08-27 11:18:43 +02:00
|
|
|
|
|
|
|
import struct
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2014-08-27 11:18:43 +02:00
|
|
|
class Cpio(object):
|
|
|
|
def __init__(self, buf):
|
|
|
|
self.buf = buf
|
|
|
|
self.off = 0
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
f = CpioFile(self.off, self.buf)
|
|
|
|
if f.fin():
|
|
|
|
raise StopIteration
|
2019-11-27 11:08:06 +01:00
|
|
|
self.off = self.off + f.length()
|
2014-08-27 11:18:43 +02:00
|
|
|
return f
|
|
|
|
|
2022-02-18 17:15:48 +01:00
|
|
|
|
2014-08-27 11:18:43 +02:00
|
|
|
class CpioFile(object):
|
|
|
|
def __init__(self, off, buf):
|
|
|
|
self.off = off
|
|
|
|
self.buf = buf
|
2017-10-17 09:10:17 +02:00
|
|
|
|
2019-11-27 11:08:06 +01:00
|
|
|
if (off & 3):
|
|
|
|
raise Exception("invalid offset %d" % off)
|
2014-08-27 11:18:43 +02:00
|
|
|
|
|
|
|
fmt = "6s8s8s8s8s8s8s8s8s8s8s8s8s8s"
|
|
|
|
off = self.off + struct.calcsize(fmt)
|
|
|
|
|
|
|
|
fields = struct.unpack(fmt, buf[self.off:off])
|
|
|
|
|
2017-10-17 09:10:17 +02:00
|
|
|
if fields[0] != "070701":
|
2024-05-07 17:55:17 +02:00
|
|
|
raise Exception(f"invalid cpio header {self.c_magic}")
|
2014-08-27 11:18:43 +02:00
|
|
|
|
|
|
|
names = ("c_ino", "c_mode", "c_uid", "c_gid",
|
2022-02-18 17:35:33 +01:00
|
|
|
"c_nlink", "c_mtime", "c_filesize",
|
|
|
|
"c_devmajor", "c_devminor", "c_rdevmajor",
|
|
|
|
"c_rdevminor", "c_namesize", "c_check")
|
2014-08-27 11:18:43 +02:00
|
|
|
for (n, v) in zip(names, fields[1:]):
|
|
|
|
setattr(self, n, int(v, 16))
|
|
|
|
|
|
|
|
nlen = self.c_namesize - 1
|
2019-11-27 11:08:06 +01:00
|
|
|
self.name = struct.unpack('%ds' % nlen, buf[off:off + nlen])[0]
|
2014-08-27 11:18:43 +02:00
|
|
|
off = off + nlen + 1
|
2019-11-27 11:08:06 +01:00
|
|
|
if (off & 3):
|
|
|
|
off = off + 4 - (off & 3) # padding
|
2014-08-27 11:18:43 +02:00
|
|
|
self.payloadstart = off
|
|
|
|
|
|
|
|
def fin(self):
|
|
|
|
return self.name == 'TRAILER!!!'
|
|
|
|
|
|
|
|
def __str__(self):
|
2019-11-27 11:08:06 +01:00
|
|
|
return "[%s %d]" % (self.name, self.c_filesize)
|
2014-08-27 11:18:43 +02:00
|
|
|
|
|
|
|
def header(self):
|
2019-11-27 11:08:06 +01:00
|
|
|
return self.buf[self.payloadstart:self.payloadstart + self.c_filesize]
|
2014-08-27 11:18:43 +02:00
|
|
|
|
|
|
|
def length(self):
|
2022-02-18 13:17:02 +01:00
|
|
|
len = self.payloadstart - self.off + self.c_filesize
|
2019-11-27 11:08:06 +01:00
|
|
|
if (self.c_filesize & 3):
|
2022-02-18 13:17:02 +01:00
|
|
|
len = len + 4 - (self.c_filesize & 3)
|
|
|
|
return len
|
2014-08-27 11:18:43 +02:00
|
|
|
|
2022-02-18 17:11:46 +01:00
|
|
|
|
2014-08-27 11:18:43 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
parser = OptionParser()
|
|
|
|
parser.add_option("--debug", action="store_true", help="debug output")
|
|
|
|
parser.add_option("--verbose", action="store_true", help="verbose")
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
for fn in args:
|
|
|
|
fh = open(fn, 'rb')
|
|
|
|
cpio = Cpio(fh.read())
|
|
|
|
for i in cpio:
|
2018-04-26 13:28:25 +02:00
|
|
|
print(i)
|
2014-08-27 11:18:43 +02:00
|
|
|
ofh = open(i.name, 'wb')
|
|
|
|
ofh.write(i.header())
|
|
|
|
ofh.close()
|