[826f3afa] | 1 | /* |
---|
| 2 | * Copyright (c) 2016 Chris Johns <chrisj.rtems.org>. All rights reserved. |
---|
| 3 | * |
---|
| 4 | * The license and distribution terms for this file may be |
---|
| 5 | * found in the file LICENSE in this distribution or at |
---|
| 6 | * http://www.rtems.org/license/LICENSE. |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | #ifdef HAVE_CONFIG_H |
---|
| 10 | #include "config.h" |
---|
| 11 | #endif |
---|
| 12 | |
---|
| 13 | #include <rtems/untar.h> |
---|
| 14 | |
---|
| 15 | int Untar_XzChunkContext_Init( |
---|
| 16 | Untar_XzChunkContext *ctx, |
---|
| 17 | enum xz_mode mode, |
---|
| 18 | uint32_t dict_max, |
---|
| 19 | void *inflateBuffer, |
---|
| 20 | size_t inflateBufferSize |
---|
| 21 | ) |
---|
| 22 | { |
---|
| 23 | int status = UNTAR_SUCCESSFUL; |
---|
| 24 | |
---|
| 25 | xz_crc32_init(); |
---|
| 26 | |
---|
| 27 | Untar_ChunkContext_Init(&ctx->base); |
---|
| 28 | ctx->inflateBuffer = inflateBuffer; |
---|
| 29 | ctx->inflateBufferSize = inflateBufferSize; |
---|
| 30 | ctx->strm = xz_dec_init(mode, dict_max); |
---|
| 31 | if (ctx->strm == NULL) { |
---|
| 32 | status = UNTAR_FAIL; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | return status; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | int Untar_FromXzChunk_Print( |
---|
| 39 | Untar_XzChunkContext *ctx, |
---|
[7e82962] | 40 | const void *chunk, |
---|
[826f3afa] | 41 | size_t chunk_size, |
---|
| 42 | const rtems_printer *printer |
---|
| 43 | ) |
---|
| 44 | { |
---|
| 45 | int untar_status = UNTAR_SUCCESSFUL; |
---|
| 46 | enum xz_ret status = XZ_OK; |
---|
| 47 | |
---|
| 48 | if (ctx->strm == NULL) |
---|
| 49 | return UNTAR_FAIL; |
---|
| 50 | |
---|
| 51 | ctx->buf.in = (const uint8_t*) chunk; |
---|
| 52 | ctx->buf.in_pos = 0; |
---|
| 53 | ctx->buf.in_size = chunk_size; |
---|
| 54 | ctx->buf.out = (uint8_t *) ctx->inflateBuffer; |
---|
| 55 | |
---|
| 56 | /* Inflate until output buffer is not full */ |
---|
| 57 | do { |
---|
| 58 | ctx->buf.out_pos = 0; |
---|
| 59 | ctx->buf.out_size = ctx->inflateBufferSize; |
---|
| 60 | status = xz_dec_run(ctx->strm, &ctx->buf); |
---|
| 61 | if (status == XZ_OPTIONS_ERROR) |
---|
| 62 | status = XZ_OK; |
---|
| 63 | if (status == XZ_OK && ctx->buf.out_pos != 0) { |
---|
| 64 | untar_status = Untar_FromChunk_Print(&ctx->base, |
---|
| 65 | ctx->inflateBuffer, |
---|
| 66 | ctx->buf.out_pos, |
---|
| 67 | NULL); |
---|
| 68 | if (untar_status != UNTAR_SUCCESSFUL) { |
---|
| 69 | break; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | } while (ctx->buf.in_pos != ctx->buf.in_size && status == XZ_OK); |
---|
| 73 | |
---|
| 74 | if (status != XZ_OK) { |
---|
| 75 | xz_dec_end(ctx->strm); |
---|
| 76 | ctx->strm = NULL; |
---|
| 77 | if (untar_status == UNTAR_SUCCESSFUL) { |
---|
| 78 | switch (status) { |
---|
| 79 | case XZ_OK: |
---|
| 80 | case XZ_STREAM_END: |
---|
| 81 | break; |
---|
| 82 | case XZ_UNSUPPORTED_CHECK: |
---|
| 83 | rtems_printf(printer, "XZ unsupported check\n"); |
---|
| 84 | break; |
---|
| 85 | case XZ_MEM_ERROR: |
---|
| 86 | rtems_printf(printer, "XZ memory allocation error\n"); |
---|
| 87 | break; |
---|
| 88 | case XZ_MEMLIMIT_ERROR: |
---|
| 89 | rtems_printf(printer, "XZ memory usage limit reached\n"); |
---|
| 90 | break; |
---|
| 91 | case XZ_FORMAT_ERROR: |
---|
| 92 | rtems_printf(printer, "Not a XZ file\n"); |
---|
| 93 | break; |
---|
| 94 | case XZ_OPTIONS_ERROR: |
---|
| 95 | rtems_printf(printer, "Unsupported options in XZ header\n"); |
---|
| 96 | break; |
---|
| 97 | case XZ_DATA_ERROR: |
---|
| 98 | rtems_printf(printer, "XZ file is corrupt (data)\n"); |
---|
| 99 | break; |
---|
| 100 | case XZ_BUF_ERROR: |
---|
| 101 | rtems_printf(printer, "XZ file is corrupt (buffer)\n"); |
---|
| 102 | break; |
---|
| 103 | } |
---|
| 104 | untar_status = UNTAR_FAIL; |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | return untar_status; |
---|
| 109 | } |
---|