Skip to content

Instantly share code, notes, and snippets.

@bruienne
Last active August 7, 2022 19:50
Show Gist options
  • Save bruienne/029494bbcfb358098b41 to your computer and use it in GitHub Desktop.
Save bruienne/029494bbcfb358098b41 to your computer and use it in GitHub Desktop.
PBZX handling of Yosemite-style Payload archives
//
// main.c
// pbzx
//
// Created by PHPdev32 on 6/20/14.
// Licensed under GPLv3, full text at http://www.gnu.org/licenses/gpl-3.0.txt
//
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define err(m,e) { fprintf(stderr, m"\n"); return e; }
#define fswap64(f,s) fread(&s, 8, 1, f); s = __builtin_bswap64(s)
#define BSIZE 8 * 1024
int main(int argc, const char * argv[])
{
// insert code here...
char buffer[BSIZE];
fread(buffer, 4, 1, stdin);
if (strncmp(buffer, "pbzx", 4))
err("Not a pbzx stream", 1);
uint64_t length = 0, flags = 0, last = 0;
fswap64(stdin, flags);
while (flags & 1 << 24) {
fswap64(stdin, flags);
fswap64(stdin, length);
fread(buffer, 1, 6, stdin);
if (strncmp(buffer, "\xfd""7zXZ\0", 6))
err("Header is not <FD>7zXZ<00>", 2);
length -= fwrite(buffer, 1, 6, stdout);
while (length)
length -= last = fwrite(buffer, 1, fread(buffer, 1, BSIZE < length ? BSIZE : length, stdin), stdout);
if (strncmp(buffer + last - 2, "YZ", 2))
err("Footer is not YZ", 3);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment