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

#include "sqfs/block_processor.h"
#include "util.h"

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

int process_block(block_t *block, compressor_t *cmp,
		  uint8_t *scratch, size_t scratch_size)
{
	ssize_t ret;

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

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

		if (ret < 0)
			return -1;

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

	return 0;
}