summaryrefslogtreecommitdiff
path: root/lib/tar/checksum.c
blob: 925f942141683ff4c1e34cadd21efdbae3738b12 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "internal.h"

void update_checksum(tar_header_t *hdr)
{
	unsigned int chksum = 0;
	size_t i;

	memset(hdr->chksum, ' ', sizeof(hdr->chksum));

	for (i = 0; i < sizeof(*hdr); ++i)
		chksum += ((unsigned char *)hdr)[i];

	write_octal(hdr->chksum, chksum, 6);
	hdr->chksum[6] = '\0';
	hdr->chksum[7] = ' ';
}

bool is_checksum_valid(const tar_header_t *hdr)
{
	tar_header_t copy;

	memcpy(&copy, hdr, sizeof(*hdr));
	update_checksum(&copy);
	return memcmp(hdr, &copy, sizeof(*hdr)) == 0;
}