#include #include #include "tar.h" static uint8_t empty_block[PW_TAR_BLOCK_SIZE] = {0}; static void init_write_block(uint8_t block[PW_TAR_BLOCK_SIZE]) { // Since we support a reduced set of Tar features, we can prepare our // write block up front and just write over the parts that change // per added entry. SDL_memset(block, '\0', PW_TAR_BLOCK_SIZE); SDL_memcpy(block + 100, "0000644", 8); SDL_memcpy(block + 108, "0000000", 8); SDL_memcpy(block + 116, "0000000", 8); SDL_memcpy(block + 136, "00000000000", 12); SDL_memcpy(block + 156, "0", 1); SDL_memcpy(block + 257, "ustar", 6); SDL_memcpy(block + 263, "00", 2); SDL_memcpy(block + 265, "root", 5); SDL_memcpy(block + 297, "root", 5); SDL_memcpy(block + 329, "0000000", 8); SDL_memcpy(block + 337, "0000000", 8); } struct pw_tar *pw_tar_open(char const *path, enum pw_tar_open_flag flag) { SDL_IOStream *stream; switch (flag) { case PW_TAR_OPEN_READ: stream = SDL_IOFromFile(path, "rb"); break; case PW_TAR_OPEN_WRITE: stream = SDL_IOFromFile(path, "wb"); break; default: SDL_InvalidParamError("flag"); return NULL; } if (!stream) { return NULL; } struct pw_tar *tar = SDL_malloc(sizeof(*tar)); if (!tar) { if (!SDL_CloseIO(stream)) { SDL_LogWarn(0, "unexpected error closing tar IO stream: %s", SDL_GetError()); } SDL_OutOfMemory(); return NULL; } if (flag == PW_TAR_OPEN_WRITE) { init_write_block(tar->block); } tar->stream = stream; return tar; } static bool close_archive(struct pw_tar *tar) { bool ok = !tar->stream || SDL_CloseIO(tar->stream); if (!ok) { SDL_LogWarn(0, "unexpected error closing tar IO stream: %s", SDL_GetError()); } tar->stream = NULL; return ok; } void pw_tar_free(struct pw_tar *tar) { if (!tar) { SDL_InvalidParamError("tar"); return; } (void)close_archive(tar); SDL_free(tar); } static bool read_exact(struct pw_tar *tar, uint8_t *buffer, size_t size) { while (size) { size_t n = SDL_ReadIO(tar->stream, buffer, size); if (n == 0) { if (SDL_GetIOStatus(tar->stream) == SDL_IO_STATUS_EOF) { return SDL_SetError("unexpected end of file"); } return false; } buffer += n; size -= n; } return true; } static bool skip_blocks(struct pw_tar *tar, size_t blocks) { while (blocks-- > 0) { if (!read_exact(tar, tar->block, sizeof(tar->block))) { return false; } } return true; } static bool is_eoa(struct pw_tar *tar) { for (size_t i = 0; i < sizeof(tar->block); ++i) { if (tar->block[i]) { return false; } } return true; } static uint64_t parse_octal(struct pw_tar *tar, size_t offset, size_t len) { uint8_t const *p = tar->block + offset; size_t n = len; // Extremely permissive parsing, inspired from `libarchive`'s `untar.c`. uint64_t i = 0; while (n && (*p < '0' || *p > '7')) { ++p; --n; } while (n && *p >= '0' && *p <= '7') { i *= 8; i += *p - '0'; ++p; --n; } return i; } static bool check_magic(struct pw_tar *tar) { static char const magic[] = "ustar\0" "00"; return !SDL_memcmp(tar->block + 257, magic, 8); } static uint64_t compute_checksum(struct pw_tar *tar) { uint64_t u = 0; for (size_t n = 0; n < sizeof(tar->block); ++n) { if (n < 148 || n > 155) { u += (uint64_t)tar->block[n]; } else { u += ' '; } } return u; } static bool verify_checksum(struct pw_tar *tar) { return compute_checksum(tar) == parse_octal(tar, 148, 8); } static void join_filename(struct pw_tar *tar, struct pw_tar_entry *e) { char const *b = (char const *)tar->block; size_t namelen = SDL_strnlen(b, 100); size_t prefixlen = SDL_strnlen(b + 345, 155); if (prefixlen) { SDL_memcpy(e->path, b + 345, prefixlen); e->path[prefixlen] = '/'; SDL_memcpy(e->path + prefixlen + 1, b, namelen); e->path[prefixlen + namelen + 1] = '\0'; } else { SDL_memcpy(e->path, b, namelen); e->path[namelen] = '\0'; } } static size_t block_count(size_t size) { return (size / PW_TAR_BLOCK_SIZE) + !!(size % PW_TAR_BLOCK_SIZE); } bool pw_tar_read(struct pw_tar *tar, struct pw_tar_entry *entry) { if (!tar) { return SDL_InvalidParamError("tar"); } if (!entry) { return SDL_InvalidParamError("entry"); } if (!tar->stream) { return SDL_SetError("read past end of archive"); } uint8_t *data = NULL; for (;;) { if (!read_exact(tar, tar->block, sizeof(tar->block))) { break; } if (is_eoa(tar)) { close_archive(tar); SDL_zerop(entry); return true; } if (!check_magic(tar)) { SDL_SetError("not a tar archive"); break; } if (!verify_checksum(tar)) { SDL_SetError("checksum verification failed"); break; } uint64_t size = parse_octal(tar, 124, 12); if (size > PW_TAR_SIZE_MAX) { SDL_SetError("file size too large %" SDL_PRIu64, size); break; } size_t blocks = (size_t)block_count(size); char ft = (char)tar->block[156]; if (ft && ft != '0') { SDL_LogDebug(0, "skipping unsupported tar file type %c", ft); if (!skip_blocks(tar, blocks)) { break; } continue; } size_t capacity = SDL_max(blocks, 1) * sizeof(tar->block); data = SDL_malloc(capacity); if (!data) { break; } if (!read_exact(tar, data, blocks * sizeof(tar->block))) { break; } join_filename(tar, entry); entry->data = data; entry->size = (size_t)size; entry->capacity = capacity; entry->blocks = blocks; return true; } close_archive(tar); if (data) { SDL_free(data); } return false; } static bool write_exact(struct pw_tar *tar, uint8_t const *buffer, size_t size) { // SDL will always write `size` bytes unless it encounters an error, // wo we don't need to loop for partial writes. size_t n = SDL_WriteIO(tar->stream, buffer, size); return n == size; } static bool encode_octal(struct pw_tar *tar, size_t offset, size_t len, uint64_t value) { uint8_t *p = tar->block + offset; size_t n = len; uint64_t i = value; p[--n] = '\0'; while (n) { p[--n] = (i & 7) + '0'; i /= 8; } return !i; } static bool encode_path(struct pw_tar *tar, char const *path) { size_t n = SDL_strlcpy((char *)tar->block, path, 100); if (n >= 100) { return false; } // Make sure to fill the rest of the path region with `0`s. size_t sz = n + 1; size_t dirty = 100 - sz; if (dirty) { SDL_memset(tar->block + sz, 0, dirty); } return true; } static void encode_checksum(struct pw_tar *tar) { // The checksum _always_ fits in its location, so we don't need to // check it. It can be at most `504 * 255 + 8 * 32 = 128776 = 0373410` // and fits into 8 octal characters. encode_octal(tar, 148, 8, compute_checksum(tar)); } bool pw_tar_write(struct pw_tar *tar, char const *path, uint8_t const *data, size_t size) { if (!tar) { return SDL_InvalidParamError("tar"); } if (!path) { return SDL_InvalidParamError("path"); } if (size > PW_TAR_SIZE_MAX) { return SDL_InvalidParamError("size"); } if (!tar->stream) { return SDL_SetError("write past end of archive"); } if (!data) { bool ok = write_exact(tar, empty_block, sizeof(empty_block)) && write_exact(tar, empty_block, sizeof(empty_block)); return close_archive(tar) && ok; } if (!encode_path(tar, path)) { return SDL_SetError("path too long '%s'", path); } if (!encode_octal(tar, 124, 12, size)) { return SDL_SetError("file too large %zu", size); } encode_checksum(tar); size_t padding = block_count(size) * PW_TAR_BLOCK_SIZE - size; if (!write_exact(tar, tar->block, sizeof(tar->block)) || !write_exact(tar, data, size) || !write_exact(tar, empty_block, padding)) { // We cannot rely on the archive being in a good state (we may have // partially written a block). close_archive(tar); return false; } return true; }