summaryrefslogtreecommitdiff
path: root/include/id_table.h
blob: 788519c2771b553b848147753597644a9b675d83 (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
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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#ifndef ID_TABLE_H
#define ID_TABLE_H

#include <stdint.h>
#include <stddef.h>

#include "compress.h"

/**
 * @struct id_table_t
 *
 * @brief Encapsulates the ID table used by SquashFS
 */
typedef struct {
	/**
	 * @brief Array of unique 32 bit IDs
	 */
	uint32_t *ids;

	/**
	 * @brief Number of 32 bit IDs stored in the array
	 */
	size_t num_ids;

	/**
	 * @brief Actual size of the array, i.e. maximum available
	 */
	size_t max_ids;
} id_table_t;

/**
 * @brief Initialize an ID table
 *
 * @memberof id_table_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * @param tbl A pointer to an uninitialized ID table
 *
 * @return Zero on success, -1 on failure
 */
int id_table_init(id_table_t *tbl);

/**
 * @brief Cleanup and free an ID table
 *
 * @memberof id_table_t
 *
 * @param tbl A pointer to an ID table
 */
void id_table_cleanup(id_table_t *tbl);

/**
 * @brief Resolve a 32 bit to a 16 bit table index
 *
 * @memberof id_table_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * @param tbl A pointer to an ID table
 * @param id  A 32 bit ID to resolve
 * @param out Returns the 16 bit table index
 *
 * @return Zero on success, -1 on failure
 */
int id_table_id_to_index(id_table_t *tbl, uint32_t id, uint16_t *out);

/**
 * @brief Write an ID table to a SquashFS image
 *
 * @memberof id_table_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * @param tbl   A pointer to an ID table
 * @param outfd A file descriptor to write the data to
 * @param super A pointer to the SquashFS super block to store the ID table
 *              offset in
 * @param cmp   A pointer to the compressor to use for compressing the ID
 *              table meta data blocks
 *
 * @return Zero on success, -1 on failure
 */
int id_table_write(id_table_t *tbl, int outfd, sqfs_super_t *super,
		   compressor_t *cmp);

/**
 * @brief Read an ID table from a SquashFS image
 *
 * @memberof id_table_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * @param tbl   A pointer to an ID table
 * @param fd    A file descriptor to read the data from
 * @param super A pointer to the SquashFS super block that tells us where
 *              the ID table is stored
 * @param cmp   A pointer to the compressor to use for extracting the ID
 *              table meta data blocks
 *
 * @return Zero on success, -1 on failure
 */
int id_table_read(id_table_t *tbl, int fd, sqfs_super_t *super,
		   compressor_t *cmp);

#endif /* ID_TABLE_H */