Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @brief Untar an Image
0007  *
0008  * This file defines the interface to methods which can untar an image.
0009  */
0010 
0011 /*
0012  *  Written by: Jake Janovetz <janovetz@tempest.ece.uiuc.edu>
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifndef _RTEMS_UNTAR_H
0037 #define _RTEMS_UNTAR_H
0038 
0039 #include <stdbool.h>
0040 #include <stddef.h>
0041 #include <tar.h>
0042 #include <zlib.h>
0043 #include <xz.h>
0044 
0045 #include <rtems/print.h>
0046 
0047 /**
0048  *  @defgroup libmisc_untar_img Untar Image
0049  *
0050  *  @ingroup LibIO
0051  */
0052 /**@{*/
0053 #ifdef __cplusplus
0054 extern "C" {
0055 #endif
0056 
0057 #define UNTAR_SUCCESSFUL         0
0058 #define UNTAR_FAIL               1
0059 #define UNTAR_INVALID_CHECKSUM   2
0060 #define UNTAR_INVALID_HEADER     3
0061 
0062 #define UNTAR_GZ_INFLATE_FAILED 4
0063 #define UNTAR_GZ_INFLATE_END_FAILED 5
0064 
0065 int Untar_FromMemory(void *tar_buf, size_t size);
0066 int Untar_FromMemory_Print(void *tar_buf, size_t size, const rtems_printer* printer);
0067 int Untar_FromFile(const char *tar_name);
0068 int Untar_FromFile_Print(const char *tar_name, const rtems_printer* printer);
0069 
0070 #define UNTAR_FILE_NAME_SIZE 100
0071 
0072 typedef struct {
0073   char *file_path;
0074   char *file_name;
0075   char link_name[UNTAR_FILE_NAME_SIZE];
0076   unsigned long mode;
0077   unsigned long file_size;
0078   unsigned long nblocks;
0079   unsigned char linkflag;
0080   const rtems_printer *printer;
0081 } Untar_HeaderContext;
0082 
0083 typedef struct {
0084   Untar_HeaderContext base;
0085 
0086   /**
0087    * @brief File path buffer.
0088    */
0089   char buf[UNTAR_FILE_NAME_SIZE];
0090 
0091   /**
0092    * @brief Current context state.
0093    */
0094   enum {
0095     UNTAR_CHUNK_HEADER,
0096     UNTAR_CHUNK_SKIP,
0097     UNTAR_CHUNK_WRITE,
0098     UNTAR_CHUNK_ERROR
0099   } state;
0100 
0101   /**
0102    * @brief Header buffer.
0103    */
0104   char header[512];
0105 
0106   /**
0107    * @brief Number of bytes of overall length are already processed.
0108    */
0109   size_t done_bytes;
0110 
0111   /**
0112    * @brief File descriptor of output file.
0113    */
0114   int out_fd;
0115 } Untar_ChunkContext;
0116 
0117 typedef struct {
0118   /**
0119    * @brief Instance of Chunk Context needed for tar decompression.
0120    */
0121   Untar_ChunkContext base;
0122 
0123   /**
0124    * @brief Current zlib context.
0125    */
0126   z_stream strm;
0127 
0128   /**
0129    * @brief Buffer that contains the inflated data.
0130    */
0131   void *inflateBuffer;
0132 
0133   /**
0134    * @brief Size of buffer that contains the inflated data.
0135    */
0136   size_t inflateBufferSize;
0137 
0138 } Untar_GzChunkContext;
0139 
0140 typedef struct {
0141   /**
0142    * @brief Instance of Chunk Context needed for tar decompression.
0143    */
0144   Untar_ChunkContext base;
0145 
0146   /**
0147    * @brief Xz context.
0148    */
0149   struct xz_dec* strm;
0150 
0151   /**
0152    * @brief Xz buffer.
0153    */
0154   struct xz_buf buf;
0155 
0156   /**
0157    * @brief Buffer that contains the inflated data.
0158    */
0159   void *inflateBuffer;
0160 
0161   /**
0162    * @brief Size of buffer that contains the inflated data.
0163    */
0164   size_t inflateBufferSize;
0165 
0166 } Untar_XzChunkContext;
0167 
0168 /**
0169  * @brief Initializes the Untar_ChunkContext files out of a part of a block of
0170  * memory.
0171  *
0172  * @param Untar_ChunkContext *context [in] Pointer to a context structure.
0173  */
0174 void Untar_ChunkContext_Init(Untar_ChunkContext *context);
0175 
0176 /*
0177  * @brief Rips links, directories and files out of a part of a block of memory.
0178  *
0179  * @param Untar_ChunkContext *context [in] Pointer to a context structure.
0180  * @param void *chunk [in] Pointer to a chunk of a TAR buffer.
0181  * @param size_t chunk_size [in] Length of the chunk of a TAR buffer.
0182  *
0183  * @retval UNTAR_SUCCESSFUL (0)    on successful completion.
0184  * @retval UNTAR_FAIL              for a faulty step within the process.
0185  * @retval UNTAR_INVALID_CHECKSUM  for an invalid header checksum.
0186  * @retval UNTAR_INVALID_HEADER    for an invalid header.
0187  */
0188 
0189 int Untar_FromChunk_Print(
0190   Untar_ChunkContext *context,
0191   void *chunk,
0192   size_t chunk_size,
0193   const rtems_printer* printer
0194 );
0195 
0196 /**
0197  * @brief Initializes the Untar_ChunkGzContext.
0198  *
0199  * @param Untar_ChunkGzContext *context [in] Pointer to a context structure.
0200  * @param void *inflateBuffer [in] Pointer to a context structure.
0201  * @param size_t inflateBufferSize [in] Size of inflateBuffer.
0202  */
0203 int Untar_GzChunkContext_Init(
0204   Untar_GzChunkContext *ctx,
0205   void *inflateBuffer,
0206   size_t inflateBufferSize
0207 );
0208 
0209 /*
0210  * @brief Untars a GZ compressed POSIX TAR file.
0211  *
0212  * This is a subroutine used to rip links, directories, and
0213  * files out of a tar.gz/tgz file.
0214  *
0215  * @param Untar_ChunkContext *context [in] Pointer to a context structure.
0216  * @param ssize buflen [in] Size of valid bytes in input buffer.
0217  * @param z_stream *strm [in] Pointer to the current zlib context.
0218  */
0219 int Untar_FromGzChunk_Print(
0220   Untar_GzChunkContext *ctx,
0221   void *chunk,
0222   size_t chunk_size,
0223   const rtems_printer* printer
0224 );
0225 
0226 /**
0227  * @brief Initializes the Untar_ChunkXzContext.
0228  *
0229  * @param Untar_ChunkXzContext *context [in] Pointer to a context structure.
0230  * @param enum xz_mode mode [in] Dictionary mode.
0231  * @param uint32_t dict_max [in] Maximum size of dictionary.
0232  * @param void *inflateBuffer [in] Pointer to a context structure.
0233  * @param size_t inflateBufferSize [in] Size of inflateBuffer.
0234  */
0235 int Untar_XzChunkContext_Init(
0236   Untar_XzChunkContext *ctx,
0237   enum xz_mode mode,
0238   uint32_t dict_max,
0239   void *inflateBuffer,
0240   size_t inflateBufferSize
0241 );
0242 
0243 /*
0244  * @brief Untars a XZ compressed POSIX TAR file.
0245  *
0246  * This is a subroutine used to rip links, directories, and
0247  * files out of a tar.gz/tgz file.
0248  *
0249  * @param Untar_ChunkContext *context [in] Pointer to a context structure.
0250  * @param ssize buflen [in] Size of valid bytes in input buffer.
0251  * @param z_stream *strm [in] Pointer to the current zlib context.
0252  */
0253 int Untar_FromXzChunk_Print(
0254   Untar_XzChunkContext *ctx,
0255   const void *chunk,
0256   size_t chunk_size,
0257   const rtems_printer* printer
0258 );
0259 
0260 int Untar_ProcessHeader(Untar_HeaderContext *ctx, const char *bufr);
0261 
0262 #ifdef __cplusplus
0263 }
0264 #endif
0265 /**@}*/
0266 #endif  /* _RTEMS_UNTAR_H */