File: sparse.c

package info (click to toggle)
qdl 2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: ansic: 3,910; makefile: 75; xml: 75; sh: 70
file content (116 lines) | stat: -rw-r--r-- 3,057 bytes parent folder | download | duplicates (2)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// SPDX-License-Identifier: BSD-3-Clause
/*
 * Copyright (c) 2025, Maksim Paimushkin <maxim.paymushkin.development@gmail.com>
 * All rights reserved.
 */
#define _FILE_OFFSET_BITS 64
#ifdef _WIN32
#include <winsock2.h>
#else
#include <arpa/inet.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "sparse.h"
#include "qdl.h"

int sparse_header_parse(int fd, sparse_header_t *sparse_header)
{
	lseek(fd, 0, SEEK_SET);

	if (read(fd, sparse_header, sizeof(sparse_header_t)) != sizeof(sparse_header_t)) {
		ux_err("[SPARSE] Unable to read sparse header\n");
		return -EINVAL;
	}

	if (sparse_header->magic != SPARSE_HEADER_MAGIC) {
		ux_err("[SPARSE] Invalid magic in sparse header\n");
		return -EINVAL;
	}

	if (sparse_header->major_version != SPARSE_HEADER_MAJOR_VER) {
		ux_err("[SPARSE] Invalid major version in sparse header\n");
		return -EINVAL;
	}

	if (sparse_header->minor_version != SPARSE_HEADER_MINOR_VER) {
		ux_err("[SPARSE] Invalid minor version in sparse header\n");
		return -EINVAL;
	}

	if (sparse_header->file_hdr_sz > sizeof(sparse_header_t))
		lseek(fd, sparse_header->file_hdr_sz - sizeof(sparse_header_t), SEEK_CUR);

	return 0;
}

int sparse_chunk_header_parse(int fd, sparse_header_t *sparse_header,
			      uint64_t *chunk_size,
			      uint32_t *value,
			      off_t *offset)
{
	chunk_header_t chunk_header;
	uint32_t fill_value = 0;
	unsigned int type;

	*chunk_size = 0;
	*value = 0;

	if (read(fd, &chunk_header, sizeof(chunk_header_t)) != sizeof(chunk_header_t)) {
		ux_err("[SPARSE] Unable to read sparse chunk header\n");
		return -EINVAL;
	}

	if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
		lseek(fd, sparse_header->chunk_hdr_sz - sizeof(chunk_header_t), SEEK_CUR);

	type = chunk_header.chunk_type;
	*chunk_size = (uint64_t)chunk_header.chunk_sz * sparse_header->blk_sz;

	switch (type) {
	case CHUNK_TYPE_RAW:
		if (chunk_header.total_sz != (sparse_header->chunk_hdr_sz + *chunk_size)) {
			ux_err("[SPARSE] Bogus chunk size, type Raw\n");
			return -EINVAL;
		}

		/* Save the current file offset in the 'value' variable */
		*offset = lseek(fd, 0, SEEK_CUR);

		/* Move the file cursor forward by the size of the chunk */
		lseek(fd, *chunk_size, SEEK_CUR);
		break;
	case CHUNK_TYPE_DONT_CARE:
		if (chunk_header.total_sz != sparse_header->chunk_hdr_sz) {
			ux_err("[SPARSE] Bogus chunk size, type Don't Care\n");
			return -EINVAL;
		}
		break;
	case CHUNK_TYPE_FILL:
		if (chunk_header.total_sz != (sparse_header->chunk_hdr_sz + sizeof(fill_value))) {
			ux_err("[SPARSE] Bogus chunk size, type Fill\n");
			return -EINVAL;
		}

		if (read(fd, &fill_value, sizeof(fill_value)) != sizeof(fill_value)) {
			ux_err("[SPARSE] Unable to read fill value\n");
			return -EINVAL;
		}

		/* Save the current fill value in the 'value' variable */
		*value = fill_value;
		break;
	default:
		ux_err("[SPARSE] Unknown chunk type: %#x\n", type);
		return -EINVAL;
	}

	return type;
}