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 Chris Johns <chrisj.rtems.org>.  All rights reserved.
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 <rtems/untar.h>
0033 
0034 int Untar_XzChunkContext_Init(
0035   Untar_XzChunkContext *ctx,
0036   enum xz_mode mode,
0037   uint32_t dict_max,
0038   void *inflateBuffer,
0039   size_t inflateBufferSize
0040 )
0041 {
0042   int status = UNTAR_SUCCESSFUL;
0043 
0044   xz_crc32_init();
0045 
0046   Untar_ChunkContext_Init(&ctx->base);
0047   ctx->inflateBuffer = inflateBuffer;
0048   ctx->inflateBufferSize = inflateBufferSize;
0049   ctx->strm = xz_dec_init(mode, dict_max);
0050   if (ctx->strm == NULL) {
0051     status = UNTAR_FAIL;
0052   }
0053 
0054   return status;
0055 }
0056 
0057 int Untar_FromXzChunk_Print(
0058   Untar_XzChunkContext *ctx,
0059   const void *chunk,
0060   size_t chunk_size,
0061   const rtems_printer *printer
0062 )
0063 {
0064   int untar_status = UNTAR_SUCCESSFUL;
0065   enum xz_ret status = XZ_OK;
0066 
0067   if (ctx->strm == NULL)
0068     return UNTAR_FAIL;
0069 
0070   ctx->buf.in = (const uint8_t*) chunk;
0071   ctx->buf.in_pos = 0;
0072   ctx->buf.in_size = chunk_size;
0073   ctx->buf.out = (uint8_t *) ctx->inflateBuffer;
0074 
0075   /* Inflate until output buffer is not full */
0076   do {
0077     ctx->buf.out_pos = 0;
0078     ctx->buf.out_size = ctx->inflateBufferSize;
0079     status = xz_dec_run(ctx->strm, &ctx->buf);
0080     if (status == XZ_OPTIONS_ERROR)
0081       status = XZ_OK;
0082     if (status == XZ_OK && ctx->buf.out_pos != 0) {
0083       untar_status = Untar_FromChunk_Print(&ctx->base,
0084                                            ctx->inflateBuffer,
0085                                            ctx->buf.out_pos,
0086                                            NULL);
0087       if (untar_status != UNTAR_SUCCESSFUL) {
0088         break;
0089       }
0090     }
0091   } while (ctx->buf.in_pos != ctx->buf.in_size && status == XZ_OK);
0092 
0093   if (status != XZ_OK) {
0094     xz_dec_end(ctx->strm);
0095     ctx->strm = NULL;
0096     if (untar_status == UNTAR_SUCCESSFUL) {
0097       switch (status) {
0098       case XZ_OK:
0099       case XZ_STREAM_END:
0100         break;
0101       case XZ_UNSUPPORTED_CHECK:
0102         rtems_printf(printer, "XZ unsupported check\n");
0103         break;
0104       case XZ_MEM_ERROR:
0105         rtems_printf(printer, "XZ memory allocation error\n");
0106         break;
0107       case XZ_MEMLIMIT_ERROR:
0108         rtems_printf(printer, "XZ memory usage limit reached\n");
0109         break;
0110       case XZ_FORMAT_ERROR:
0111         rtems_printf(printer, "Not a XZ file\n");
0112         break;
0113       case XZ_OPTIONS_ERROR:
0114         rtems_printf(printer, "Unsupported options in XZ header\n");
0115         break;
0116       case XZ_DATA_ERROR:
0117         rtems_printf(printer, "XZ file is corrupt (data)\n");
0118         break;
0119       case XZ_BUF_ERROR:
0120         rtems_printf(printer, "XZ file is corrupt (buffer)\n");
0121         break;
0122       }
0123       untar_status = UNTAR_FAIL;
0124     }
0125   }
0126 
0127   return untar_status;
0128 }