libsqsh v1.5.2
Loading...
Searching...
No Matches
custom_mapper.c
Go to the documentation of this file.
1
10#include <assert.h>
11#include <errno.h>
12#include <sqsh.h>
13#include <stdio.h>
14#include <stdlib.h>
15
16// This function is called while initialization and gets its input parameter
17// from the `input` parameter of sqsh_archive_open(). The `size` parameter is
18// initialized from SqshConfig::source_size and expected to be set to actual
19// size of the input file.
20static int
21mapper_init(struct SqshMapper *mapper, const void *input, uint64_t *size) {
22 (void)size;
23 int rv = 0;
24 FILE *file;
25 long pos;
26
27 file = fopen(input, "r");
28 if (file == NULL) {
29 rv = -errno;
30 goto out;
31 }
32
33 pos = fseek(file, 0, SEEK_END);
34 if (pos < 0) {
35 rv = -errno;
36 goto out;
37 }
38 *size = (size_t)pos;
39
40 sqsh_mapper_set_user_data(mapper, file);
41
42out:
43 return rv;
44}
45
46// This function is called when libsqsh needs a chunk of the archive in memory.
47// The `offset` parameter is the offset in the archive starting from the
48// beginning of the archive. That means that the mapper does not need to handle
49// SqshConfig::archive_offset. The `size` parameter is the size of the chunk
50// that should be read. The `data` parameter is a double pointer. It is expected
51// to be set to a buffer of `size` bytes.
52static int
53mapper_map(
54 const struct SqshMapper *mapper, uint64_t offset, size_t size,
55 uint8_t **data) {
56 int rv = 0;
57 FILE *file = sqsh_mapper_user_data(mapper);
58
59 *data = calloc(size, sizeof(uint8_t));
60 if (*data == NULL) {
62 goto out;
63 }
64 rv = fseek(file, offset, SEEK_SET);
65 if (rv < 0) {
66 rv = -errno;
67 goto out;
68 }
69
70 if (fread(*data, sizeof(uint8_t), size, file) != size) {
71 rv = -1;
72 goto out;
73 }
74
75out:
76 if (rv < 0) {
77 free(*data);
78 }
79 return rv;
80}
81
82// This function is called when libsqsh cleans up a chunk of the archive in
83// memory. The `data` parameter is the buffer that was allocated in mapper_map.
84// The `size` parameter is the size of the buffer.
85static int
86mapper_unmap(const struct SqshMapper *mapper, uint8_t *data, size_t size) {
87 (void)mapper;
88 (void)size;
89 free(data);
90 return 0;
91}
92
93// This function is called when the archive is closed. It is expected to clean
94// up any resources that were allocated in mapper_init.
95static int
96mapper_cleanup(struct SqshMapper *mapper) {
97 FILE *file = sqsh_mapper_user_data(mapper);
98 fclose(file);
99 return 0;
100}
101
102static const struct SqshMemoryMapperImpl custom_mapper = {
103 // The block size is a hint to libsqsh how much data it should request
104 // from the mapper at once. This field must be set as it is used as
105 // default value. Setting it to 0 is undefined behavior.
106 .block_size_hint = 1024 * 1024,
107 .init2 = mapper_init,
108 .map2 = mapper_map,
109 .unmap = mapper_unmap,
110 .cleanup = mapper_cleanup,
111};
112
113int
114main(int argc, char *argv[]) {
115 if (argc != 3) {
116 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
117 return 1;
118 }
119
120 struct SqshConfig config = {
121 .source_mapper = &custom_mapper,
122 };
123 struct SqshArchive *archive = sqsh_archive_open(argv[1], &config, NULL);
124 assert(archive != NULL);
125
126 sqsh_archive_close(archive);
127 return 0;
128}
int main(int argc, char *argv[])
@ SQSH_ERROR_MALLOC_FAILED
Definition sqsh_error.h:70
SQSH_NO_UNUSED struct SqshArchive * sqsh_archive_open(const void *source, const struct SqshConfig *config, int *err)
initializes a archive context in heap.
int sqsh_archive_close(struct SqshArchive *archive)
Frees the resources used by a Sqsh instance.
The SqshConfig struct contains all the configuration options for a sqsh session.
const struct SqshMemoryMapperImpl * source_mapper
source_mapper is the memory mapper implementation that will be used to map the archive.
The mapper that is used to map chunks of the archive into memory.
The implementation of a memory mapper.
Definition sqsh_mapper.h:53
size_t block_size_hint
A hint to libsqsh to use this block size if the user did not specify one.
Definition sqsh_mapper.h:58