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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
* data_reader.c
*
* Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
*/
#include "config.h"
#include "sqfs/block_processor.h"
#include "sqfs/error.h"
#include "data_reader.h"
#include "highlevel.h"
#include "util.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
struct data_reader_t {
sqfs_fragment_t *frag;
sqfs_compressor_t *cmp;
sqfs_block_t *data_block;
sqfs_block_t *frag_block;
uint64_t current_block;
sqfs_file_t *file;
uint32_t num_fragments;
uint32_t current_frag_index;
uint32_t block_size;
uint8_t scratch[];
};
static int get_block(data_reader_t *data, uint64_t off, uint32_t size,
size_t unpacked_size, sqfs_block_t **out)
{
sqfs_block_t *blk = alloc_flex(sizeof(*blk), 1, unpacked_size);
size_t on_disk_size;
ssize_t ret;
int err;
if (blk == NULL)
return SQFS_ERROR_ALLOC;
blk->size = unpacked_size;
if (SQFS_IS_SPARSE_BLOCK(size)) {
*out = blk;
return 0;
}
on_disk_size = SQFS_ON_DISK_BLOCK_SIZE(size);
if (on_disk_size > unpacked_size)
return SQFS_ERROR_OVERFLOW;
if (SQFS_IS_BLOCK_COMPRESSED(size)) {
err = data->file->read_at(data->file, off,
data->scratch, on_disk_size);
if (err) {
free(blk);
return err;
}
ret = data->cmp->do_block(data->cmp, data->scratch,
on_disk_size, blk->data, blk->size);
if (ret <= 0)
err = ret < 0 ? ret : SQFS_ERROR_OVERFLOW;
} else {
err = data->file->read_at(data->file, off,
blk->data, on_disk_size);
}
if (err) {
free(blk);
return err;
}
*out = blk;
return 0;
}
static int precache_data_block(data_reader_t *data, uint64_t location,
uint32_t size)
{
int ret;
if (data->data_block != NULL && data->current_block == location)
return 0;
free(data->data_block);
ret = get_block(data, location, size, data->block_size,
&data->data_block);
if (ret < 0) {
data->data_block = NULL;
return -1;
}
data->current_block = location;
return 0;
}
static int precache_fragment_block(data_reader_t *data, size_t idx)
{
int ret;
if (data->frag_block != NULL && idx == data->current_frag_index)
return 0;
if (idx >= data->num_fragments) {
fputs("fragment index out of bounds\n", stderr);
return -1;
}
free(data->frag_block);
ret = get_block(data, data->frag[idx].start_offset,
data->frag[idx].size, data->block_size,
&data->frag_block);
if (ret < 0)
return -1;
data->current_frag_index = idx;
return 0;
}
data_reader_t *data_reader_create(sqfs_file_t *file, sqfs_super_t *super,
sqfs_compressor_t *cmp)
{
data_reader_t *data = alloc_flex(sizeof(*data), 1, super->block_size);
size_t i, size;
void *raw_frag;
int ret;
if (data == NULL) {
perror("creating data reader");
return data;
}
data->num_fragments = super->fragment_entry_count;
data->current_frag_index = super->fragment_entry_count;
data->file = file;
data->block_size = super->block_size;
data->cmp = cmp;
if (super->fragment_entry_count == 0 ||
(super->flags & SQFS_FLAG_NO_FRAGMENTS) != 0) {
return data;
}
if (super->fragment_table_start >= super->bytes_used) {
fputs("Fragment table start is past end of file\n", stderr);
free(data);
return NULL;
}
if (SZ_MUL_OV(sizeof(data->frag[0]), data->num_fragments, &size)) {
fputs("Too many fragments: overflow\n", stderr);
free(data);
return NULL;
}
ret = sqfs_read_table(file, cmp, size, super->fragment_table_start,
super->directory_table_start,
super->fragment_table_start, &raw_frag);
if (ret) {
free(data);
return NULL;
}
data->frag = raw_frag;
for (i = 0; i < data->num_fragments; ++i) {
data->frag[i].size = le32toh(data->frag[i].size);
data->frag[i].start_offset =
le64toh(data->frag[i].start_offset);
}
return data;
}
void data_reader_destroy(data_reader_t *data)
{
free(data->data_block);
free(data->frag_block);
free(data->frag);
free(data);
}
int data_reader_get_block(data_reader_t *data,
const sqfs_inode_generic_t *inode,
size_t index, sqfs_block_t **out)
{
size_t i, unpacked_size;
uint64_t off, filesz;
if (inode->base.type == SQFS_INODE_FILE) {
off = inode->data.file.blocks_start;
filesz = inode->data.file.file_size;
} else if (inode->base.type == SQFS_INODE_EXT_FILE) {
off = inode->data.file_ext.blocks_start;
filesz = inode->data.file_ext.file_size;
} else {
return SQFS_ERROR_NOT_FILE;
}
if (index >= inode->num_file_blocks)
return SQFS_ERROR_OUT_OF_BOUNDS;
for (i = 0; i < index; ++i) {
off += SQFS_ON_DISK_BLOCK_SIZE(inode->block_sizes[i]);
filesz -= data->block_size;
}
unpacked_size = filesz < data->block_size ? filesz : data->block_size;
return get_block(data, off, inode->block_sizes[index],
unpacked_size, out);
}
int data_reader_get_fragment(data_reader_t *data,
const sqfs_inode_generic_t *inode,
sqfs_block_t **out)
{
uint32_t frag_idx, frag_off, frag_sz;
sqfs_block_t *blk;
uint64_t filesz;
if (inode->base.type == SQFS_INODE_EXT_FILE) {
filesz = inode->data.file_ext.file_size;
frag_idx = inode->data.file_ext.fragment_idx;
frag_off = inode->data.file_ext.fragment_offset;
} else if (inode->base.type == SQFS_INODE_FILE) {
filesz = inode->data.file.file_size;
frag_idx = inode->data.file.fragment_index;
frag_off = inode->data.file.fragment_offset;
} else {
return -1;
}
if (inode->num_file_blocks * data->block_size >= filesz) {
*out = NULL;
return 0;
}
frag_sz = filesz % data->block_size;
if (precache_fragment_block(data, frag_idx))
return -1;
if (frag_off + frag_sz > data->block_size)
return -1;
blk = alloc_flex(sizeof(*blk), 1, frag_sz);
if (blk == NULL)
return -1;
blk->size = frag_sz;
memcpy(blk->data, (char *)data->frag_block->data + frag_off, frag_sz);
*out = blk;
return 0;
}
ssize_t data_reader_read(data_reader_t *data,
const sqfs_inode_generic_t *inode,
uint64_t offset, void *buffer, size_t size)
{
uint32_t frag_idx, frag_off;
size_t i, diff, total = 0;
uint64_t off, filesz;
char *ptr;
/* work out file location and size */
if (inode->base.type == SQFS_INODE_EXT_FILE) {
off = inode->data.file_ext.blocks_start;
filesz = inode->data.file_ext.file_size;
frag_idx = inode->data.file_ext.fragment_idx;
frag_off = inode->data.file_ext.fragment_offset;
} else {
off = inode->data.file.blocks_start;
filesz = inode->data.file.file_size;
frag_idx = inode->data.file.fragment_index;
frag_off = inode->data.file.fragment_offset;
}
/* find location of the first block */
i = 0;
while (offset > data->block_size && i < inode->num_file_blocks) {
off += SQFS_ON_DISK_BLOCK_SIZE(inode->block_sizes[i++]);
offset -= data->block_size;
if (filesz >= data->block_size) {
filesz -= data->block_size;
} else {
filesz = 0;
}
}
/* copy data from blocks */
while (i < inode->num_file_blocks && size > 0 && filesz > 0) {
diff = data->block_size - offset;
if (size < diff)
diff = size;
if (SQFS_IS_SPARSE_BLOCK(inode->block_sizes[i])) {
memset(buffer, 0, diff);
} else {
if (precache_data_block(data, off,
inode->block_sizes[i])) {
return -1;
}
memcpy(buffer, (char *)data->data_block->data + offset,
diff);
off += SQFS_ON_DISK_BLOCK_SIZE(inode->block_sizes[i]);
}
if (filesz >= data->block_size) {
filesz -= data->block_size;
} else {
filesz = 0;
}
++i;
offset = 0;
size -= diff;
total += diff;
buffer = (char *)buffer + diff;
}
/* copy from fragment */
if (i == inode->num_file_blocks && size > 0 && filesz > 0) {
if (precache_fragment_block(data, frag_idx))
return -1;
if (frag_off + filesz > data->block_size)
goto fail_range;
if (offset >= filesz)
return total;
if (offset + size > filesz)
size = filesz - offset;
if (size == 0)
return total;
ptr = (char *)data->frag_block->data + frag_off + offset;
memcpy(buffer, ptr, size);
total += size;
}
return total;
fail_range:
fputs("attempted to read past fragment block limits\n", stderr);
return -1;
}
|