File: compress.c

package info (click to toggle)
apfsprogs 0.2.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,112 kB
  • sloc: ansic: 16,034; makefile: 175; sh: 57
file content (353 lines) | stat: -rw-r--r-- 11,748 bytes parent folder | download
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
/*
 * Copyright (C) 2020 Corellium LLC
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include "apfs/raw.h"
#include "apfs/libzbitmap.h"
#include "apfs/zlib_inflate/zlib.h"
#include "apfsck.h"
#include "btree.h"
#include "compress.h"
#include "extents.h"
#include "super.h"

/* maximum size of compressed data currently supported */
#define MAX_FBUF_SIZE        (1024 * 1024 * 1024)

void read_whole_dstream(u64 oid, void *buf, loff_t size)
{
    void *block = NULL;
    u64 curr_copylen;
    u64 bno;
    int ret;
    u64 i;

    i = 0;
    while(size) {
        curr_copylen = MIN(size, sb->s_blocksize);

        if(apfs_volume_is_sealed())
            ret = fext_tree_lookup(oid, i << sb->s_blocksize_bits, &bno);
        else
            ret = file_extent_lookup(oid, i << sb->s_blocksize_bits, &bno);

        if(ret)
            report("Compressed file", "dstream read failed.");

        if(bno == 0)
            report("Compressed file", "has a hole.");

        block = apfs_mmap(NULL, sb->s_blocksize, PROT_READ, MAP_PRIVATE, bno * sb->s_blocksize);
        if(block == MAP_FAILED)
            system_error();
        memcpy(buf, block, curr_copylen);
        munmap(block, sb->s_blocksize);
        block = NULL;

        size -= curr_copylen;
        buf += curr_copylen;
        ++i;
    }
}

static inline bool apfs_compressed_in_dstream(struct compress *compress)
{
    /* For sealed volumes we may use a fake dstream that only holds the hash */
    return compress->rsrc_dstream && !compress->rsrc_dstream->d_inline;
}

void apfs_compress_open(struct compress *compress)
{
    struct dstream *rsrc = compress->rsrc_dstream;
    struct apfs_compress_file_data *fd;
    ssize_t res;
    u8 *cdata;
    ssize_t csize;

    fd = calloc(1, sizeof(*fd));
    if(!fd)
        system_error();

    if(!compress->decmpfs)
        report("Compressed file", "missing decmpfs xattr.");
    memcpy(&fd->hdr, compress->decmpfs, sizeof(fd->hdr));

    if(apfs_compressed_in_dstream(compress) && le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZBITMAP_RSRC) {
        struct apfs_compress_rsrc_hdr *rsrc_hdr = NULL;
        struct apfs_compress_rsrc_data *rsrc_data = NULL;
    	u32 data_end;

        fd->buf = malloc(APFS_COMPRESS_BLOCK);
        if(!fd->buf)
            system_error();
        fd->bufblk = -1;

        if(rsrc->d_size > MAX_FBUF_SIZE)
            report_unknown("Large compressed file");

        fd->size = rsrc->d_size;
        fd->data = malloc(fd->size);
        if(!fd->data)
            system_error();
        read_whole_dstream(rsrc->d_id, fd->data, fd->size);

        if(fd->size < sizeof(*rsrc_hdr))
            report("Resource compressed file", "header won't fit.");
        rsrc_hdr = fd->data;

        /* TODO: check for overlaps, figure out 'mgmt' */
        compress->data_offs = be32_to_cpu(rsrc_hdr->data_offs);
        compress->data_size = be32_to_cpu(rsrc_hdr->data_size);
        data_end = compress->data_offs + compress->data_size;
        if(data_end < compress->data_offs || data_end > fd->size)
            report("Resource compressed file", "block metadata is too big.");

        if(compress->data_size < sizeof(*rsrc_data))
            report("Resource compressed file", "block metadata header won't fit.");
        rsrc_data = fd->data + compress->data_offs;

        compress->block_num = le32_to_cpu(rsrc_data->num);
        if(compress->block_num > MAX_FBUF_SIZE) /* Rough bound to avoid overflow */
            report_unknown("Large compressed file");
        if(compress->data_size < compress->block_num * sizeof(rsrc_data->block[0]) + sizeof(*rsrc_data))
            report("Resource compressed file", "block metadata won't fit.");
        /* TODO: figure out the 'unknown' field */
    } else if(apfs_compressed_in_dstream(compress)) {
        __le32 *block_offs;
        int i;

        fd->buf = malloc(APFS_COMPRESS_BLOCK);
        if(!fd->buf)
            system_error();
        fd->bufblk = -1;

        if(rsrc->d_size > MAX_FBUF_SIZE)
            report_unknown("Large compressed file");

        fd->size = rsrc->d_size;
        fd->data = malloc(fd->size);
        if(!fd->data)
            system_error();
        read_whole_dstream(rsrc->d_id, fd->data, fd->size);
        block_offs = fd->data;

        compress->data_offs = 0;
        compress->data_size = fd->size;
        /* Put a rough bound on block count to avoid overflow */
        for(i = 0; i < MAX_FBUF_SIZE; ++i) {
            if((i + 1) * sizeof(*block_offs) >= fd->size)
                report("LZBITMAP-compressed file", "block offsets won't fit.");
            if(le32_to_cpu(block_offs[i]) == fd->size)
                break;
        }
        compress->block_num = i;
        if(compress->block_num == MAX_FBUF_SIZE)
            report("LZBITMAP-compressed file", "missing final block offset.");
    } else {
        if(le64_to_cpu(fd->hdr.size) > MAX_FBUF_SIZE)
            report("Inline compressed file", "size is too big.");

        fd->size = le64_to_cpu(fd->hdr.size);
        fd->data = malloc(le64_to_cpu(fd->hdr.size));
        if(!fd->data)
            system_error();

        cdata = compress->decmpfs + sizeof(fd->hdr);
        csize = compress->decmpfs_len - sizeof(fd->hdr);

        compress->block_num = 1;

        switch(le32_to_cpu(fd->hdr.algo)) {
        case APFS_COMPRESS_ZLIB_ATTR:
            if(cdata[0] == 0x78 && csize >= 2) {
                res = zlib_inflate_blob(fd->data, fd->size, cdata + 2, csize - 2);
                if(res != fd->size)
                    report("Inline compressed file", "wrong reported length.");
            } else if((cdata[0] & 0x0F) == 0x0F) {
                if(csize - 1 != fd->size)
                    report("Inline compressed file", "wrong reported length.");
                memcpy(fd->data, cdata + 1, csize - 1);
            } else {
                report("Inline compressed file", "invalid header for zlib.");
            }
            break;
        case APFS_COMPRESS_PLAIN_ATTR:
            if(csize - 1 != fd->size)
                report("Inline uncompressed file", "wron reported length.");
            memcpy(fd->data, cdata + 1, csize - 1);
            break;
        default:
            report_unknown("Compression algorithm");
        }
    }
    compress->compress_data = fd;
}

/* If @buf is NULL, just return the size */
static ssize_t apfs_compress_read_block(struct compress *compress, char *buf, size_t size, loff_t off)
{
    struct apfs_compress_file_data *fd = compress->compress_data;
    u32 doffs, coffs;
    loff_t block;
    u8 *cdata, *tmp = fd->buf;
    size_t csize, bsize;
    ssize_t res;

    if(off >= (loff_t)le64_to_cpu(fd->hdr.size))
        return 0;
    if(size > le64_to_cpu(fd->hdr.size) - (size_t)off)
        size = le64_to_cpu(fd->hdr.size) - off;

    block = off / APFS_COMPRESS_BLOCK;
    off -= block * APFS_COMPRESS_BLOCK;
    if(block != fd->bufblk) {
        doffs = compress->data_offs;

        if(block >= compress->block_num)
            return 0;

        if(le32_to_cpu(fd->hdr.algo) != APFS_COMPRESS_LZBITMAP_RSRC) {
            struct apfs_compress_rsrc_data *cd = fd->data + doffs;

            bsize = le64_to_cpu(fd->hdr.size) - block * APFS_COMPRESS_BLOCK;
            if(bsize > APFS_COMPRESS_BLOCK)
                bsize = APFS_COMPRESS_BLOCK;

            csize = le32_to_cpu(cd->block[block].size);
            coffs = le32_to_cpu(cd->block[block].offs) + 4;
        } else {
            __le32 *block_offs = fd->data + doffs;

            bsize = le64_to_cpu(fd->hdr.size) - block * APFS_COMPRESS_BLOCK;
            if(bsize > APFS_COMPRESS_BLOCK)
                bsize = APFS_COMPRESS_BLOCK;

            coffs = le32_to_cpu(block_offs[block]);
            csize = le32_to_cpu(block_offs[block + 1]) - coffs;
        }
        if(coffs >= fd->size - doffs || fd->size - doffs - coffs < (loff_t)csize || csize > APFS_COMPRESS_BLOCK + 1)
            report("Resource compressed file", "invalid block size or position.");
        cdata = fd->data + doffs + coffs;

        switch(le32_to_cpu(fd->hdr.algo)) {
        case APFS_COMPRESS_ZLIB_RSRC:
            if(cdata[0] == 0x78 && csize >= 2) {
                res = zlib_inflate_blob(tmp, bsize, cdata + 2, csize - 2);
                if(res < 0)
                    report("Resource compressed file", "invalid compression.");
                bsize = res;
            } else if((cdata[0] & 0x0F) == 0x0F) {
                memcpy(tmp, &cdata[1], csize - 1);
                bsize = csize - 1;
            } else {
                report("Resource compressed file", "invalid header for zlib.");
            }
            break;
        case APFS_COMPRESS_LZBITMAP_RSRC:
            if(cdata[0] == 0x5a) {
                res = zbm_decompress(tmp, bsize, cdata, csize, &bsize);
                if(res < 0)
                    report("LZBITMAP compressed file", "invalid compression.");
            } else if((cdata[0] & 0x0F) == 0x0F) {
                memcpy(tmp, &cdata[1], csize - 1);
                bsize = csize - 1;
            } else {
                report("LZBITMAP compressed file", "invalid header.");
            }
            break;
        case APFS_COMPRESS_PLAIN_RSRC:
            memcpy(tmp, &cdata[1], csize - 1);
            bsize = csize - 1;
            break;
        default:
            return -EINVAL;
        }
        fd->bufblk = block;
        fd->bufsize = bsize;
    } else
        bsize = fd->bufsize;

    if (block != compress->block_num - 1 && bsize != APFS_COMPRESS_BLOCK)
        report("Resource compressed file", "wrong size for uncompressed block.");

    if(bsize < (size_t)off)
        return 0;
    bsize -= off;
    if(size > bsize)
        size = bsize;
    if(buf)
        memcpy(buf, tmp + off, size);
    return size;
}

ssize_t apfs_compress_read(struct compress *compress, char *buf, size_t size, loff_t *off)
{
    struct apfs_compress_file_data *fd = compress->compress_data;
    loff_t step;
    ssize_t block, res;

    if(apfs_compressed_in_dstream(compress)) {
        step = 0;
        while(!buf || step < (int64_t)size) {
            block = APFS_COMPRESS_BLOCK - ((*off + step) & (APFS_COMPRESS_BLOCK - 1));
            if(buf && block > (int64_t)size - step)
                block = size - step;
            res = apfs_compress_read_block(compress, buf ? buf + step : NULL, block, *off + step);
            if(res < block) {
                step += res > 0 ? res : 0;
                break;
            }
            step += block;
        }
        *off += step;
        return step;
    } else {
        if(!buf)
            return fd->size;
        if(*off >= fd->size)
            return 0;
        if((int64_t)size > fd->size - *off)
            size = fd->size - *off;
        memcpy(buf, fd->data + *off, size);
        *off += size;
        return size;
    }
}

void apfs_compress_check(struct compress *compress)
{
    ssize_t size;
    loff_t off = 0;
    u64 block_num;

    /* Inline compression was already checked on open */
    if (!compress->rsrc_dstream)
        return;

    size = apfs_compress_read(compress, NULL, 0, &off);
    if(compress->size != size)
        report("Resource compressed file", "wrong reported length.");

    block_num = DIV_ROUND_UP(compress->size, APFS_COMPRESS_BLOCK);
    if(block_num != compress->block_num)
        report("Resource compressed file", "inconsistent block count.");
}

void apfs_compress_close(struct compress *compress)
{
    struct apfs_compress_file_data *fd = compress->compress_data;

    if (!fd)
        return;

    if(fd->data)
        free(fd->data);
    if(fd->buf)
        free(fd->buf);
    free(fd);
    compress->compress_data = NULL;
}