Skip to content

Instantly share code, notes, and snippets.

@meeuw
Forked from pudquick/parse_pbzx.py
Last active July 6, 2017 10:25
Show Gist options
  • Save meeuw/3e37381992f2a212a97f to your computer and use it in GitHub Desktop.
Save meeuw/3e37381992f2a212a97f to your computer and use it in GitHub Desktop.
Pure python reimplementation of .cpio.xz content extraction from pbzx file payload for OS X packages.without any third party tools
#!/usr/bin/python
# tar -Oxf /Volumes/install_app/Packages/Essentials.pkg Payload|python pbzx.py|tar t
import subprocess
import struct
import sys
def pbzx():
if sys.stdin.read(4) != "pbzx":
print "Not a pbzx stream"
sys.exit(1)
flags = struct.unpack(">Q", sys.stdin.read(8))[0]
print >> sys.stderr, hex(flags)
while (flags & 1 << 24):
flags, length = struct.unpack(">QQ", sys.stdin.read(16))
xz = None
while length:
xbuf = sys.stdin.read(min(4*1024, length))
if xz == None:
xz = xbuf[:8] == "\xfd7zXZ\x00\x00\x00"
if xz:
p = subprocess.Popen("zcat", stdin=subprocess.PIPE)
length -= len(xbuf)
if xz:
p.stdin.write(xbuf)
else:
sys.stdout.write(xbuf)
if xz:
p.stdin.close()
p.wait()
# This is required because Popen seems to flush stdout
sys.stdout.flush()
if __name__ == "__main__":
pbzx()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment