summaryrefslogtreecommitdiff
path: root/include/meta_reader.h
blob: 78c0db367eb874eb4b7ce1ef62da37900f8ca362 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* SPDX-License-Identifier: GPL-3.0-or-later */
#ifndef META_READER_H
#define META_READER_H

#include "compress.h"
#include "squashfs.h"

/**
 * @struct meta_reader_t
 *
 * @brief Abstracts away I/O on SquashFS meta data
 */
typedef struct {
	/**
	 * @brief The location of the current block in the file
	 */
	uint64_t block_offset;

	/**
	 * @brief The location of the next block after the current one
	 */
	uint64_t next_block;

	/**
	 * @brief A byte offset into the uncompressed data of the current block
	 */
	size_t offset;

	/**
	 * @brief The underlying file descriptor to read from
	 */
	int fd;

	/**
	 * @brief A pointer to the compressor to use for extracting data
	 */
	compressor_t *cmp;

	/**
	 * @brief The raw data read from the input file
	 */
	uint8_t data[SQFS_META_BLOCK_SIZE];

	/**
	 * @brief The uncompressed data read from the input file
	 */
	uint8_t scratch[SQFS_META_BLOCK_SIZE];
} meta_reader_t;

/**
 * @brief Create a meta data reader
 *
 * @memberof meta_reader_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * @param fd  The underlying file descriptor to read from
 * @param cmp A pointer to a compressor to use for extracting the data
 *
 * @return A pointer to a meta data reader, NULL on failure
 */
meta_reader_t *meta_reader_create(int fd, compressor_t *cmp);

/**
 * @brief Destroy a meta data reader and free all memory used by it
 *
 * @memberof meta_reader_t
 *
 * @param m A pointer to a meta data reader
 */
void meta_reader_destroy(meta_reader_t *m);

/**
 * @brief Seek to a specific meta data block and offset
 *
 * @memberof meta_reader_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * @param m           A pointer to a meta data reader
 * @param block_start The absolute position of the 16 bit header right before
 *                    the compressed data block
 * @param offset      A byte offset into the data block
 *
 * @return Zero on success, -1 on failure
 */
int meta_reader_seek(meta_reader_t *m, uint64_t block_start,
		     size_t offset);

/**
 * @brief Read a chunk of data from a meta data block
 *
 * @memberof meta_reader_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * If the end of the block is reached, this function transparently tries to
 * interpret the data after the current block as a further meta data block,
 * i.e. it can transparently read across multiple meta data blocks.
 *
 * @param m    A pointer to a meta data reader
 * @param data A pointer to a memory block to write the data to
 * @param size The number of bytes to read
 *
 * @return Zero on success, -1 on failure
 */
int meta_reader_read(meta_reader_t *m, void *data, size_t size);

/**
 * @brief Read an inode from a meta data block
 *
 * @memberof meta_reader_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * This function is a conveniance wrapper around @ref meta_reader_read that
 * reads and decodes an entire SquashFS inode. It first reads the common inode
 * header, interprets it and reads the additional, type dependend data.
 *
 * @param ir          A pointer to a meta data reader
 * @param super       A pointer to the SquashFS super block
 * @param block_start A byte offset relative to the inode table start where
 *                    the meta data containing the inode starts
 * @param offset      A byte offset into the uncompressed meta data block
 *                    where the inode is stored
 *
 * @return A pointer to the decoded inode or NULL on failure. Can be freed
 *         with a single free call.
 */
sqfs_inode_generic_t *meta_reader_read_inode(meta_reader_t *ir,
					     sqfs_super_t *super,
					     uint64_t block_start,
					     size_t offset);

/**
 * @brief Read a directory header from a meta data block
 *
 * @memberof meta_reader_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * This function is a conveniance wrapper around @ref meta_reader_read that
 * reads and decodes a SquashFS directory header.
 *
 * @param m   A pointer to a meta data reader
 * @param hdr A pointer to a directory header structure to write the decoded
 *            data to
 *
 * @return Zero on success, -1 on failure
 */
int meta_reader_read_dir_header(meta_reader_t *m, sqfs_dir_header_t *hdr);

/**
 * @brief Read a directory entry from a meta data block
 *
 * @memberof meta_reader_t
 *
 * @note This function internally prints error message to stderr on failure
 *
 * This function is a conveniance wrapper around @ref meta_reader_read that
 * reads and decodes a SquashFS directory entry.
 *
 * @param m A pointer to a meta data reader
 *
 * @return A pointer to a directory entry or NULL on failure. Can be freed
 *         with a single free call.
 */
sqfs_dir_entry_t *meta_reader_read_dir_ent(meta_reader_t *m);

#endif /* META_READER_H */