summaryrefslogtreecommitdiff
path: root/lib/sqfs/blk_proc/process_block.c
blob: 1b6ee29a43b803d5131660dd143daf9da4bfd92d (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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * process_block.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "internal.h"

#include <string.h>
#include <zlib.h>

int sqfs_block_process(sqfs_block_t *block, sqfs_compressor_t *cmp,
		       uint8_t *scratch, size_t scratch_size)
{
	ssize_t ret;

	if (!(block->flags & SQFS_BLK_DONT_CHECKSUM))
		block->checksum = crc32(0, block->data, block->size);

	if (!(block->flags & SQFS_BLK_DONT_COMPRESS)) {
		ret = cmp->do_block(cmp, block->data, block->size,
				    scratch, scratch_size);

		if (ret < 0) {
			block->flags |= SQFS_BLK_COMPRESS_ERROR;
			return ret;
		}

		if (ret > 0) {
			memcpy(block->data, scratch, ret);
			block->size = ret;
			block->flags |= SQFS_BLK_IS_COMPRESSED;
		}
	}

	return 0;
}

int process_completed_blocks(sqfs_block_processor_t *proc, sqfs_block_t *queue)
{
	sqfs_block_t *it;

	while (queue != NULL) {
		it = queue;
		queue = queue->next;

		if (it->flags & SQFS_BLK_COMPRESS_ERROR) {
			proc->status = SQFS_ERROR_COMRPESSOR;
		} else if (proc->status == 0) {
			proc->status = proc->cb(proc->user, it);
		}

		free(it);
	}

	return proc->status;
}