aboutsummaryrefslogtreecommitdiff
path: root/lib/util/file_cmp.c
blob: 2aa0cc278969361db154e5a21a34e6c8df1cd44e (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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * file_cmp.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

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

#include <string.h>

int check_file_range_equal(sqfs_file_t *file, void *scratch, size_t scratch_sz,
			   sqfs_u64 loc_a, sqfs_u64 loc_b, sqfs_u64 size)
{
	sqfs_u8 *ptr_a = scratch, *ptr_b = ptr_a + scratch_sz / 2;
	int ret;

	while (size > 0) {
		size_t diff = scratch_sz / 2;
		diff = (sqfs_u64)diff > size ? size : diff;

		ret = file->read_at(file, loc_a, ptr_a, diff);
		if (ret != 0)
			return ret;

		ret = file->read_at(file, loc_b, ptr_b, diff);
		if (ret != 0)
			return ret;

		if (memcmp(ptr_a, ptr_b, diff) != 0)
			return 1;

		size -= diff;
		loc_a += diff;
		loc_b += diff;
	}

	return 0;
}