1 | /* |
---|
2 | * Copyright (c) 2016 embedded brains GmbH. 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 <string.h> |
---|
14 | |
---|
15 | #include <rtems/untar.h> |
---|
16 | |
---|
17 | int Untar_GzChunkContext_Init( |
---|
18 | Untar_GzChunkContext *ctx, |
---|
19 | void *inflateBuffer, |
---|
20 | size_t inflateBufferSize |
---|
21 | ) |
---|
22 | { |
---|
23 | int ret; |
---|
24 | int status = UNTAR_SUCCESSFUL; |
---|
25 | |
---|
26 | Untar_ChunkContext_Init(&ctx->base); |
---|
27 | ctx->inflateBuffer = inflateBuffer; |
---|
28 | ctx->inflateBufferSize = inflateBufferSize; |
---|
29 | memset(&ctx->strm, 0, sizeof(ctx->strm)); |
---|
30 | ret = inflateInit2(&ctx->strm, 32 + MAX_WBITS); |
---|
31 | if (ret != Z_OK){ |
---|
32 | status = UNTAR_FAIL; |
---|
33 | } |
---|
34 | return status; |
---|
35 | } |
---|
36 | |
---|
37 | int Untar_FromGzChunk_Print( |
---|
38 | Untar_GzChunkContext *ctx, |
---|
39 | void *chunk, |
---|
40 | size_t chunk_size, |
---|
41 | const rtems_printer *printer |
---|
42 | ) |
---|
43 | { |
---|
44 | int untar_succesful; |
---|
45 | int status; |
---|
46 | |
---|
47 | ctx->strm.next_in = (Bytef *)chunk; |
---|
48 | ctx->strm.avail_in = (size_t)chunk_size; |
---|
49 | |
---|
50 | /* Inflate until output buffer is not full */ |
---|
51 | do { |
---|
52 | ctx->strm.next_out = (Bytef *) ctx->inflateBuffer; |
---|
53 | ctx->strm.avail_out = ctx->inflateBufferSize; |
---|
54 | |
---|
55 | status = inflate(&ctx->strm, Z_NO_FLUSH); |
---|
56 | if (status == Z_OK || status == Z_STREAM_END) { |
---|
57 | size_t inflated_size = |
---|
58 | ctx->inflateBufferSize - ctx->strm.avail_out; |
---|
59 | untar_succesful = Untar_FromChunk_Print(&ctx->base, |
---|
60 | ctx->inflateBuffer, inflated_size, NULL); |
---|
61 | if (untar_succesful != UNTAR_SUCCESSFUL){ |
---|
62 | return untar_succesful; |
---|
63 | } |
---|
64 | } else { |
---|
65 | untar_succesful = UNTAR_GZ_INFLATE_FAILED; |
---|
66 | } |
---|
67 | } while (ctx->strm.avail_out == 0 && ctx->strm.avail_in > 0 |
---|
68 | && status == Z_OK); |
---|
69 | |
---|
70 | if (status != Z_OK) { |
---|
71 | if (untar_succesful != UNTAR_SUCCESSFUL){ |
---|
72 | rtems_printf(printer, "Untar not successful\n"); |
---|
73 | } |
---|
74 | |
---|
75 | if (status != Z_STREAM_END) { |
---|
76 | rtems_printf(printer, "Zlib inflate failed\n"); |
---|
77 | } |
---|
78 | |
---|
79 | status = inflateEnd(&ctx->strm); |
---|
80 | if (status != Z_OK) { |
---|
81 | rtems_printf(printer, "Zlib inflate end failed\n"); |
---|
82 | } |
---|
83 | } |
---|
84 | return untar_succesful; |
---|
85 | } |
---|
86 | |
---|
87 | |
---|