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

int store_block_location(sqfs_block_processor_t *proc, uint64_t offset,
			 uint32_t size, uint32_t chksum)
{
	size_t new_sz;
	void *new;

	if (proc->num_blocks == proc->max_blocks) {
		new_sz = proc->max_blocks * 2;
		new = realloc(proc->blocks, sizeof(proc->blocks[0]) * new_sz);

		if (new == NULL)
			return SQFS_ERROR_ALLOC;

		proc->blocks = new;
		proc->max_blocks = new_sz;
	}

	proc->blocks[proc->num_blocks].offset = offset;
	proc->blocks[proc->num_blocks].hash = MK_BLK_HASH(chksum, size);
	proc->num_blocks += 1;
	return 0;
}

size_t deduplicate_blocks(sqfs_block_processor_t *proc, size_t count)
{
	size_t i, j;

	for (i = 0; i < proc->file_start; ++i) {
		for (j = 0; j < count; ++j) {
			if (proc->blocks[i + j].hash !=
			    proc->blocks[proc->file_start + j].hash)
				break;
		}

		if (j == count)
			break;
	}

	return i;
}