1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Untar an Image |
---|
5 | * |
---|
6 | * This file defines the interface to methods which can untar an image. |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Written by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu> |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef _RTEMS_UNTAR_H |
---|
18 | #define _RTEMS_UNTAR_H |
---|
19 | |
---|
20 | #include <stdbool.h> |
---|
21 | #include <stddef.h> |
---|
22 | #include <tar.h> |
---|
23 | #include <zlib.h> |
---|
24 | #include <xz.h> |
---|
25 | |
---|
26 | #include <rtems/print.h> |
---|
27 | |
---|
28 | /** |
---|
29 | * @defgroup libmisc_untar_img Untar Image |
---|
30 | * |
---|
31 | * @ingroup libmisc |
---|
32 | */ |
---|
33 | /**@{*/ |
---|
34 | #ifdef __cplusplus |
---|
35 | extern "C" { |
---|
36 | #endif |
---|
37 | |
---|
38 | #define UNTAR_SUCCESSFUL 0 |
---|
39 | #define UNTAR_FAIL 1 |
---|
40 | #define UNTAR_INVALID_CHECKSUM 2 |
---|
41 | #define UNTAR_INVALID_HEADER 3 |
---|
42 | |
---|
43 | #define UNTAR_GZ_INFLATE_FAILED 4 |
---|
44 | #define UNTAR_GZ_INFLATE_END_FAILED 5 |
---|
45 | |
---|
46 | int Untar_FromMemory(void *tar_buf, size_t size); |
---|
47 | int Untar_FromMemory_Print(void *tar_buf, size_t size, const rtems_printer* printer); |
---|
48 | int Untar_FromFile(const char *tar_name); |
---|
49 | int Untar_FromFile_Print(const char *tar_name, const rtems_printer* printer); |
---|
50 | |
---|
51 | typedef struct { |
---|
52 | /** |
---|
53 | * @brief Current context state. |
---|
54 | */ |
---|
55 | enum { |
---|
56 | UNTAR_CHUNK_HEADER, |
---|
57 | UNTAR_CHUNK_SKIP, |
---|
58 | UNTAR_CHUNK_WRITE, |
---|
59 | UNTAR_CHUNK_ERROR |
---|
60 | } state; |
---|
61 | |
---|
62 | /** |
---|
63 | * @brief Header buffer. |
---|
64 | */ |
---|
65 | char header[512]; |
---|
66 | |
---|
67 | /** |
---|
68 | * @brief Name buffer. |
---|
69 | */ |
---|
70 | char fname[100]; |
---|
71 | |
---|
72 | /** |
---|
73 | * @brief Number of bytes of overall length are already processed. |
---|
74 | */ |
---|
75 | size_t done_bytes; |
---|
76 | |
---|
77 | /** |
---|
78 | * @brief Mode of the file. |
---|
79 | */ |
---|
80 | unsigned long mode; |
---|
81 | |
---|
82 | /** |
---|
83 | * @brief Overall amount of bytes to be processed. |
---|
84 | */ |
---|
85 | unsigned long todo_bytes; |
---|
86 | |
---|
87 | /** |
---|
88 | * @brief Overall amount of blocks to be processed. |
---|
89 | */ |
---|
90 | unsigned long todo_blocks; |
---|
91 | |
---|
92 | /** |
---|
93 | * @brief File descriptor of output file. |
---|
94 | */ |
---|
95 | int out_fd; |
---|
96 | } Untar_ChunkContext; |
---|
97 | |
---|
98 | typedef struct { |
---|
99 | /** |
---|
100 | * @brief Instance of Chunk Context needed for tar decompression. |
---|
101 | */ |
---|
102 | Untar_ChunkContext base; |
---|
103 | |
---|
104 | /** |
---|
105 | * @brief Current zlib context. |
---|
106 | */ |
---|
107 | z_stream strm; |
---|
108 | |
---|
109 | /** |
---|
110 | * @brief Buffer that contains the inflated data. |
---|
111 | */ |
---|
112 | void *inflateBuffer; |
---|
113 | |
---|
114 | /** |
---|
115 | * @brief Size of buffer that contains the inflated data. |
---|
116 | */ |
---|
117 | size_t inflateBufferSize; |
---|
118 | |
---|
119 | } Untar_GzChunkContext; |
---|
120 | |
---|
121 | typedef struct { |
---|
122 | /** |
---|
123 | * @brief Instance of Chunk Context needed for tar decompression. |
---|
124 | */ |
---|
125 | Untar_ChunkContext base; |
---|
126 | |
---|
127 | /** |
---|
128 | * @brief Xz context. |
---|
129 | */ |
---|
130 | struct xz_dec* strm; |
---|
131 | |
---|
132 | /** |
---|
133 | * @brief Xz buffer. |
---|
134 | */ |
---|
135 | struct xz_buf buf; |
---|
136 | |
---|
137 | /** |
---|
138 | * @brief Buffer that contains the inflated data. |
---|
139 | */ |
---|
140 | void *inflateBuffer; |
---|
141 | |
---|
142 | /** |
---|
143 | * @brief Size of buffer that contains the inflated data. |
---|
144 | */ |
---|
145 | size_t inflateBufferSize; |
---|
146 | |
---|
147 | } Untar_XzChunkContext; |
---|
148 | |
---|
149 | /** |
---|
150 | * @brief Initializes the Untar_ChunkContext files out of a part of a block of |
---|
151 | * memory. |
---|
152 | * |
---|
153 | * @param Untar_ChunkContext *context [in] Pointer to a context structure. |
---|
154 | */ |
---|
155 | void Untar_ChunkContext_Init(Untar_ChunkContext *context); |
---|
156 | |
---|
157 | /* |
---|
158 | * @brief Rips links, directories and files out of a part of a block of memory. |
---|
159 | * |
---|
160 | * @param Untar_ChunkContext *context [in] Pointer to a context structure. |
---|
161 | * @param void *chunk [in] Pointer to a chunk of a TAR buffer. |
---|
162 | * @param size_t chunk_size [in] Length of the chunk of a TAR buffer. |
---|
163 | * |
---|
164 | * @retval UNTAR_SUCCESSFUL (0) on successful completion. |
---|
165 | * @retval UNTAR_FAIL for a faulty step within the process. |
---|
166 | * @retval UNTAR_INVALID_CHECKSUM for an invalid header checksum. |
---|
167 | * @retval UNTAR_INVALID_HEADER for an invalid header. |
---|
168 | */ |
---|
169 | |
---|
170 | int Untar_FromChunk_Print( |
---|
171 | Untar_ChunkContext *context, |
---|
172 | void *chunk, |
---|
173 | size_t chunk_size, |
---|
174 | const rtems_printer* printer |
---|
175 | ); |
---|
176 | |
---|
177 | /** |
---|
178 | * @brief Initializes the Untar_ChunkGzContext. |
---|
179 | * |
---|
180 | * @param Untar_ChunkGzContext *context [in] Pointer to a context structure. |
---|
181 | * @param void *inflateBuffer [in] Pointer to a context structure. |
---|
182 | * @param size_t inflateBufferSize [in] Size of inflateBuffer. |
---|
183 | */ |
---|
184 | int Untar_GzChunkContext_Init( |
---|
185 | Untar_GzChunkContext *ctx, |
---|
186 | void *inflateBuffer, |
---|
187 | size_t inflateBufferSize |
---|
188 | ); |
---|
189 | |
---|
190 | /* |
---|
191 | * @brief Untars a GZ compressed POSIX TAR file. |
---|
192 | * |
---|
193 | * This is a subroutine used to rip links, directories, and |
---|
194 | * files out of a tar.gz/tgz file. |
---|
195 | * |
---|
196 | * @param Untar_ChunkContext *context [in] Pointer to a context structure. |
---|
197 | * @param ssize buflen [in] Size of valid bytes in input buffer. |
---|
198 | * @param z_stream *strm [in] Pointer to the current zlib context. |
---|
199 | */ |
---|
200 | int Untar_FromGzChunk_Print( |
---|
201 | Untar_GzChunkContext *ctx, |
---|
202 | void *chunk, |
---|
203 | size_t chunk_size, |
---|
204 | const rtems_printer* printer |
---|
205 | ); |
---|
206 | |
---|
207 | /** |
---|
208 | * @brief Initializes the Untar_ChunkXzContext. |
---|
209 | * |
---|
210 | * @param Untar_ChunkXzContext *context [in] Pointer to a context structure. |
---|
211 | * @param enum xz_mode mode [in] Dictionary mode. |
---|
212 | * @param uint32_t dict_max [in] Maximum size of dictionary. |
---|
213 | * @param void *inflateBuffer [in] Pointer to a context structure. |
---|
214 | * @param size_t inflateBufferSize [in] Size of inflateBuffer. |
---|
215 | */ |
---|
216 | int Untar_XzChunkContext_Init( |
---|
217 | Untar_XzChunkContext *ctx, |
---|
218 | enum xz_mode mode, |
---|
219 | uint32_t dict_max, |
---|
220 | void *inflateBuffer, |
---|
221 | size_t inflateBufferSize |
---|
222 | ); |
---|
223 | |
---|
224 | /* |
---|
225 | * @brief Untars a XZ compressed POSIX TAR file. |
---|
226 | * |
---|
227 | * This is a subroutine used to rip links, directories, and |
---|
228 | * files out of a tar.gz/tgz file. |
---|
229 | * |
---|
230 | * @param Untar_ChunkContext *context [in] Pointer to a context structure. |
---|
231 | * @param ssize buflen [in] Size of valid bytes in input buffer. |
---|
232 | * @param z_stream *strm [in] Pointer to the current zlib context. |
---|
233 | */ |
---|
234 | int Untar_FromXzChunk_Print( |
---|
235 | Untar_XzChunkContext *ctx, |
---|
236 | void *chunk, |
---|
237 | size_t chunk_size, |
---|
238 | const rtems_printer* printer |
---|
239 | ); |
---|
240 | |
---|
241 | /************************************************************************** |
---|
242 | * This converts octal ASCII number representations into an |
---|
243 | * unsigned long. Only support 32-bit numbers for now. |
---|
244 | *************************************************************************/ |
---|
245 | extern unsigned long |
---|
246 | _rtems_octal2ulong(const char *octascii, size_t len); |
---|
247 | |
---|
248 | /************************************************************************ |
---|
249 | * Compute the TAR checksum and check with the value in |
---|
250 | * the archive. The checksum is computed over the entire |
---|
251 | * header, but the checksum field is substituted with blanks. |
---|
252 | ************************************************************************/ |
---|
253 | extern int |
---|
254 | _rtems_tar_header_checksum(const char *bufr); |
---|
255 | |
---|
256 | #ifdef __cplusplus |
---|
257 | } |
---|
258 | #endif |
---|
259 | /**@}*/ |
---|
260 | #endif /* _RTEMS_UNTAR_H */ |
---|