Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  * Copyright (c) 2016 embedded brains GmbH & Co. KG
0005  *
0006  * Redistribution and use in source and binary forms, with or without
0007  * modification, are permitted provided that the following conditions
0008  * are met:
0009  * 1. Redistributions of source code must retain the above copyright
0010  *    notice, this list of conditions and the following disclaimer.
0011  * 2. Redistributions in binary form must reproduce the above copyright
0012  *    notice, this list of conditions and the following disclaimer in the
0013  *    documentation and/or other materials provided with the distribution.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0025  * POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 
0028 #ifdef HAVE_CONFIG_H
0029 #include "config.h"
0030 #endif
0031 
0032 #include <string.h>
0033 
0034 #include <rtems/untar.h>
0035 
0036 int Untar_GzChunkContext_Init(
0037   Untar_GzChunkContext *ctx,
0038   void *inflateBuffer,
0039   size_t inflateBufferSize
0040 )
0041 {
0042   int ret;
0043   int status = UNTAR_SUCCESSFUL;
0044 
0045   Untar_ChunkContext_Init(&ctx->base);
0046   ctx->inflateBuffer = inflateBuffer;
0047   ctx->inflateBufferSize = inflateBufferSize;
0048   memset(&ctx->strm, 0, sizeof(ctx->strm));
0049   ret = inflateInit2(&ctx->strm, 32 + MAX_WBITS);
0050   if (ret != Z_OK){
0051     status = UNTAR_FAIL;
0052   }
0053   return status;
0054 }
0055 
0056 int Untar_FromGzChunk_Print(
0057   Untar_GzChunkContext *ctx,
0058   void *chunk,
0059   size_t chunk_size,
0060   const rtems_printer *printer
0061 )
0062 {
0063   int untar_succesful;
0064   int status;
0065 
0066   ctx->strm.next_in = (Bytef *)chunk;
0067   ctx->strm.avail_in = (size_t)chunk_size;
0068 
0069     /* Inflate until output buffer is not full */
0070   do {
0071     ctx->strm.next_out = (Bytef *) ctx->inflateBuffer;
0072     ctx->strm.avail_out = ctx->inflateBufferSize;
0073 
0074     status = inflate(&ctx->strm, Z_NO_FLUSH);
0075     if (status == Z_OK || status == Z_STREAM_END) {
0076       size_t inflated_size =
0077         ctx->inflateBufferSize - ctx->strm.avail_out;
0078       untar_succesful = Untar_FromChunk_Print(&ctx->base,
0079         ctx->inflateBuffer, inflated_size, NULL);
0080       if (untar_succesful != UNTAR_SUCCESSFUL){
0081         return untar_succesful;
0082       }
0083     } else {
0084       untar_succesful = UNTAR_GZ_INFLATE_FAILED;
0085     }
0086   } while (ctx->strm.avail_out == 0 && ctx->strm.avail_in > 0
0087       && status == Z_OK);
0088 
0089   if (status != Z_OK) {
0090     if (untar_succesful != UNTAR_SUCCESSFUL){
0091       rtems_printf(printer, "Untar not successful\n");
0092     }
0093 
0094     if (status != Z_STREAM_END) {
0095       rtems_printf(printer, "Zlib inflate failed\n");
0096     }
0097 
0098     status = inflateEnd(&ctx->strm);
0099     if (status != Z_OK) {
0100       rtems_printf(printer, "Zlib inflate end failed\n");
0101     }
0102   }
0103   return untar_succesful;
0104 }
0105 
0106