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 | |
---|
25 | #include <rtems/print.h> |
---|
26 | |
---|
27 | /** |
---|
28 | * @defgroup libmisc_untar_img Untar Image |
---|
29 | * |
---|
30 | * @ingroup libmisc |
---|
31 | */ |
---|
32 | /**@{*/ |
---|
33 | #ifdef __cplusplus |
---|
34 | extern "C" { |
---|
35 | #endif |
---|
36 | |
---|
37 | #define UNTAR_SUCCESSFUL 0 |
---|
38 | #define UNTAR_FAIL 1 |
---|
39 | #define UNTAR_INVALID_CHECKSUM 2 |
---|
40 | #define UNTAR_INVALID_HEADER 3 |
---|
41 | |
---|
42 | #define UNTAR_GZ_INFLATE_FAILED 4 |
---|
43 | #define UNTAR_GZ_INFLATE_END_FAILED 5 |
---|
44 | |
---|
45 | int Untar_FromMemory(void *tar_buf, size_t size); |
---|
46 | int Untar_FromMemory_Print(void *tar_buf, size_t size, const rtems_printer* printer); |
---|
47 | int Untar_FromFile(const char *tar_name); |
---|
48 | int Untar_FromFile_Print(const char *tar_name, const rtems_printer* printer); |
---|
49 | |
---|
50 | typedef struct { |
---|
51 | /** |
---|
52 | * @brief Current context state. |
---|
53 | */ |
---|
54 | enum { |
---|
55 | UNTAR_CHUNK_HEADER, |
---|
56 | UNTAR_CHUNK_SKIP, |
---|
57 | UNTAR_CHUNK_WRITE, |
---|
58 | UNTAR_CHUNK_ERROR |
---|
59 | } state; |
---|
60 | |
---|
61 | /** |
---|
62 | * @brief Header buffer. |
---|
63 | */ |
---|
64 | char header[512]; |
---|
65 | |
---|
66 | /** |
---|
67 | * @brief Name buffer. |
---|
68 | */ |
---|
69 | char fname[100]; |
---|
70 | |
---|
71 | /** |
---|
72 | * @brief Number of bytes of overall length are already processed. |
---|
73 | */ |
---|
74 | size_t done_bytes; |
---|
75 | |
---|
76 | /** |
---|
77 | * @brief Overall amount of bytes to be processed. |
---|
78 | */ |
---|
79 | long unsigned todo_bytes; |
---|
80 | |
---|
81 | /** |
---|
82 | * @brief Overall amount of blocks to be processed. |
---|
83 | */ |
---|
84 | unsigned long todo_blocks; |
---|
85 | |
---|
86 | /** |
---|
87 | * @brief File descriptor of output file. |
---|
88 | */ |
---|
89 | int out_fd; |
---|
90 | } Untar_ChunkContext; |
---|
91 | |
---|
92 | typedef struct { |
---|
93 | /** |
---|
94 | * @brief Instance of Chunk Context needed for tar decompression. |
---|
95 | */ |
---|
96 | Untar_ChunkContext base; |
---|
97 | |
---|
98 | /** |
---|
99 | * @brief Current zlib context. |
---|
100 | */ |
---|
101 | z_stream strm; |
---|
102 | |
---|
103 | /** |
---|
104 | * @brief Buffer that contains the inflated data. |
---|
105 | */ |
---|
106 | void *inflateBuffer; |
---|
107 | |
---|
108 | /** |
---|
109 | * @brief Size of buffer that contains the inflated data. |
---|
110 | */ |
---|
111 | size_t inflateBufferSize; |
---|
112 | |
---|
113 | } Untar_GzChunkContext; |
---|
114 | |
---|
115 | /** |
---|
116 | * @brief Initializes the Untar_ChunkContext files out of a part of a block of |
---|
117 | * memory. |
---|
118 | * |
---|
119 | * @param Untar_ChunkContext *context [in] Pointer to a context structure. |
---|
120 | */ |
---|
121 | void Untar_ChunkContext_Init(Untar_ChunkContext *context); |
---|
122 | |
---|
123 | /* |
---|
124 | * @brief Rips links, directories and files out of a part of a block of memory. |
---|
125 | * |
---|
126 | * @param Untar_ChunkContext *context [in] Pointer to a context structure. |
---|
127 | * @param void *chunk [in] Pointer to a chunk of a TAR buffer. |
---|
128 | * @param size_t chunk_size [in] Length of the chunk of a TAR buffer. |
---|
129 | * |
---|
130 | * @retval UNTAR_SUCCESSFUL (0) on successful completion. |
---|
131 | * @retval UNTAR_FAIL for a faulty step within the process. |
---|
132 | * @retval UNTAR_INVALID_CHECKSUM for an invalid header checksum. |
---|
133 | * @retval UNTAR_INVALID_HEADER for an invalid header. |
---|
134 | */ |
---|
135 | |
---|
136 | int Untar_FromChunk_Print( |
---|
137 | Untar_ChunkContext *context, |
---|
138 | void *chunk, |
---|
139 | size_t chunk_size, |
---|
140 | const rtems_printer* printer |
---|
141 | ); |
---|
142 | |
---|
143 | /** |
---|
144 | * @brief Initializes the Untar_ChunkGzContext. |
---|
145 | * |
---|
146 | * @param Untar_ChunkGzContext *context [in] Pointer to a context structure. |
---|
147 | * @param void *inflateBuffer [in] Pointer to a context structure. |
---|
148 | * @param size_t inflateBufferSize [in] Size of inflateBuffer. |
---|
149 | */ |
---|
150 | int Untar_GzChunkContext_Init( |
---|
151 | Untar_GzChunkContext *ctx, |
---|
152 | void *inflateBuffer, |
---|
153 | size_t inflateBufferSize |
---|
154 | ); |
---|
155 | |
---|
156 | /* |
---|
157 | * @brief Untars a GZ compressed POSIX TAR file. |
---|
158 | * |
---|
159 | * This is a subroutine used to rip links, directories, and |
---|
160 | * files out of a tar.gz/tgz file. |
---|
161 | * |
---|
162 | * @param Untar_ChunkContext *context [in] Pointer to a context structure. |
---|
163 | * @param ssize buflen [in] Size of valid bytes in input buffer. |
---|
164 | * @param z_stream *strm [in] Pointer to the current zlib context. |
---|
165 | */ |
---|
166 | int Untar_FromGzChunk_Print( |
---|
167 | Untar_GzChunkContext *ctx, |
---|
168 | void *chunk, |
---|
169 | size_t chunk_size, |
---|
170 | const rtems_printer* printer |
---|
171 | ); |
---|
172 | |
---|
173 | /************************************************************************** |
---|
174 | * This converts octal ASCII number representations into an |
---|
175 | * unsigned long. Only support 32-bit numbers for now. |
---|
176 | *************************************************************************/ |
---|
177 | extern unsigned long |
---|
178 | _rtems_octal2ulong(const char *octascii, size_t len); |
---|
179 | |
---|
180 | /************************************************************************ |
---|
181 | * Compute the TAR checksum and check with the value in |
---|
182 | * the archive. The checksum is computed over the entire |
---|
183 | * header, but the checksum field is substituted with blanks. |
---|
184 | ************************************************************************/ |
---|
185 | extern int |
---|
186 | _rtems_tar_header_checksum(const char *bufr); |
---|
187 | |
---|
188 | #ifdef __cplusplus |
---|
189 | } |
---|
190 | #endif |
---|
191 | /**@}*/ |
---|
192 | #endif /* _RTEMS_UNTAR_H */ |
---|