aboutsummaryrefslogtreecommitdiff
path: root/src/tar.h
blob: 4b3fb199e5d3fb642c9cd4b7b5435c33be5ed338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef PW_TAR_H
#define PW_TAR_H

#include <SDL3/SDL_iostream.h>
#include <SDL3/SDL_stdinc.h>

#define PW_TAR_BLOCK_SIZE 512
#define PW_TAR_PATH_MAX 257
#define PW_TAR_SIZE_MAX ((SIZE_MAX / PW_TAR_BLOCK_SIZE) * PW_TAR_BLOCK_SIZE)

/**
 * A UStar archive stream.
 */
struct pw_tar {
  SDL_IOStream *stream;
  uint8_t block[PW_TAR_BLOCK_SIZE];
};

/**
 * Specify Tar archive stream open mode.
 */
enum pw_tar_open_flag {
  PW_TAR_OPEN_READ,
  PW_TAR_OPEN_WRITE,
};

/**
 * A read Tar entry.
 */
struct pw_tar_entry {
  char path[PW_TAR_PATH_MAX];
  uint8_t *data;
  size_t size;
  size_t capacity;
  size_t blocks;
};

/**
 * Opens a new Tar reader.
 */
struct pw_tar *pw_tar_open(char const *path, enum pw_tar_open_flag flag);

/**
 * Closes and frees the Tar reader.
 */
void pw_tar_free(struct pw_tar *tar);

/**
 * Reads the next entry in a Tar file, returning the its file name.
 *
 * Sets `data` to the file contents with `size`, `data` is allocated with
 * `blocks * PW_TAR_BLOCK_SIZE` bytes, and must be freed by the caller.
 */
bool pw_tar_read(struct pw_tar *tar, struct pw_tar_entry *entry);

/**
 * Writes an entry to the Tar file. Write NULL data to indicate the end of
 * the archive.
 */
bool pw_tar_write(struct pw_tar *tar, char const *path, uint8_t const *data,
                  size_t size);

#endif