Skip to content

Commit 826f3af

Browse files
committed
libmisc/xz: Add xz decompression.
Add support to untar XZ compressed files.
1 parent be57318 commit 826f3af

File tree

20 files changed

+3292
-5
lines changed

20 files changed

+3292
-5
lines changed

cpukit/Makefile.am

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ include_uuid_HEADERS = libmisc/uuid/uuid.h
4848
include_utf8procdir = $(includedir)/utf8proc
4949
include_utf8proc_HEADERS = libmisc/utf8proc/utf8proc.h
5050

51+
include_xzdir = $(includedir)
52+
include_xz_HEADERS = libmisc/xz/xz.h
53+
5154
include_sysdir = $(includedir)/sys
5255
include_sys_HEADERS =
5356

cpukit/libmisc/Makefile.am

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ EXTRA_DIST += stackchk/README
155155

156156
## libuntar
157157
noinst_LIBRARIES += libuntar.a
158-
libuntar_a_SOURCES = untar/untar.c untar/untar_tgz.c untar/untar.h
158+
libuntar_a_SOURCES = untar/untar.c untar/untar_txz.c untar/untar_tgz.c \
159+
untar/untar.h
159160

160161
EXTRA_DIST += untar/README
161162

@@ -192,6 +193,13 @@ libuuid_a_SOURCES = uuid/clear.c uuid/compare.c uuid/copy.c uuid/gen_uuid.c \
192193
uuid/isnull.c uuid/pack.c uuid/parse.c uuid/unpack.c uuid/unparse.c \
193194
uuid/uuid_time.c uuid/uuidd.h uuid/uuidP.h
194195

196+
## xz-embedded
197+
noinst_LIBRARIES += libxz.a
198+
libxz_a_SOURCES = xz/xz/h xz/xz_crc32.c xz/xz_crc64.c \
199+
xz/xz_dec_lzma2.c xz/xz_dec_stream.c
200+
201+
EXTRA_DIST += xz/README xz/COPING
202+
195203
## ---
196204
include $(srcdir)/preinstall.am
197205
include $(top_srcdir)/automake/local.am

cpukit/libmisc/untar/untar.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <stddef.h>
2222
#include <tar.h>
2323
#include <zlib.h>
24+
#include <xz.h>
2425

2526
#include <rtems/print.h>
2627

@@ -117,6 +118,34 @@ typedef struct {
117118

118119
} Untar_GzChunkContext;
119120

121+
typedef struct {
122+
/**
123+
* @brief Instance of Chunk Context needed for tar decompression.
124+
*/
125+
Untar_ChunkContext base;
126+
127+
/**
128+
* @brief Xz context.
129+
*/
130+
struct xz_dec* strm;
131+
132+
/**
133+
* @brief Xz buffer.
134+
*/
135+
struct xz_buf buf;
136+
137+
/**
138+
* @brief Buffer that contains the inflated data.
139+
*/
140+
void *inflateBuffer;
141+
142+
/**
143+
* @brief Size of buffer that contains the inflated data.
144+
*/
145+
size_t inflateBufferSize;
146+
147+
} Untar_XzChunkContext;
148+
120149
/**
121150
* @brief Initializes the Untar_ChunkContext files out of a part of a block of
122151
* memory.
@@ -175,6 +204,40 @@ int Untar_FromGzChunk_Print(
175204
const rtems_printer* printer
176205
);
177206

207+
/**
208+
* @brief Initializes the Untar_ChunkXzContext.
209+
*
210+
* @param Untar_ChunkXzContext *context [in] Pointer to a context structure.
211+
* @param enum xz_mode mode [in] Dictionary mode.
212+
* @param uint32_t dict_max [in] Maximum size of dictionary.
213+
* @param void *inflateBuffer [in] Pointer to a context structure.
214+
* @param size_t inflateBufferSize [in] Size of inflateBuffer.
215+
*/
216+
int Untar_XzChunkContext_Init(
217+
Untar_XzChunkContext *ctx,
218+
enum xz_mode mode,
219+
uint32_t dict_max,
220+
void *inflateBuffer,
221+
size_t inflateBufferSize
222+
);
223+
224+
/*
225+
* @brief Untars a XZ compressed POSIX TAR file.
226+
*
227+
* This is a subroutine used to rip links, directories, and
228+
* files out of a tar.gz/tgz file.
229+
*
230+
* @param Untar_ChunkContext *context [in] Pointer to a context structure.
231+
* @param ssize buflen [in] Size of valid bytes in input buffer.
232+
* @param z_stream *strm [in] Pointer to the current zlib context.
233+
*/
234+
int Untar_FromXzChunk_Print(
235+
Untar_XzChunkContext *ctx,
236+
void *chunk,
237+
size_t chunk_size,
238+
const rtems_printer* printer
239+
);
240+
178241
/**************************************************************************
179242
* This converts octal ASCII number representations into an
180243
* unsigned long. Only support 32-bit numbers for now.

cpukit/libmisc/untar/untar_txz.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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,
40+
void *chunk,
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+
}

cpukit/libmisc/xz/COPYING

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
Licensing of XZ Embedded
3+
========================
4+
5+
All the files in this package have been written by Lasse Collin
6+
and/or Igor Pavlov. All these files have been put into the
7+
public domain. You can do whatever you want with these files.
8+
9+
As usual, this software is provided "as is", without any warranty.
10+

cpukit/libmisc/xz/README

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
XZ Emebdded
2+
3+
Chris Johns <[email protected]> 2016
4+
5+
The code is take from the git repo:
6+
7+
http://git.tukaani.org/xz-embedded.git
8+
9+
The source has been pulled out of the Linux structure held in the repo.

0 commit comments

Comments
 (0)