This repository has been archived on 2023-07-17. You can view files and clone it, but cannot push or open issues or pull requests.
bl_mcu_sdk/components/xz/xz_decompress.c

61 lines
1.2 KiB
C
Raw Normal View History

2021-06-04 17:51:55 +08:00
#include "xz.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
static struct xz_dec *s;
int xz_uncompress_init(struct xz_buf *stream, uint8_t *sbuf, uint8_t *dbuf)
{
xz_crc32_init();
/*
* Support up to 32 KiB dictionary. The actually needed memory
* is allocated once the headers have been parsed.
*/
s = xz_dec_init(XZ_DYNALLOC, 1 << 15);
2021-06-20 12:25:46 +08:00
2021-06-04 17:51:55 +08:00
if (s == NULL) {
2021-06-20 12:25:46 +08:00
return 1;
2021-06-04 17:51:55 +08:00
}
stream->in = sbuf;
stream->in_pos = 0;
stream->in_size = 0;
stream->out = dbuf;
stream->out_pos = 0;
stream->out_size = 0;
2021-06-20 12:25:46 +08:00
return 0;
2021-06-04 17:51:55 +08:00
}
int xz_uncompress_stream(struct xz_buf *stream, uint8_t *sbuf, uint32_t slen,
2021-06-20 12:25:46 +08:00
uint8_t *dbuf, uint32_t dlen, uint32_t *decomp_len)
2021-06-04 17:51:55 +08:00
{
2021-06-20 12:25:46 +08:00
int status;
*decomp_len = 0;
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
if (stream->in_pos == stream->in_size) {
stream->in_size = slen;
stream->in_pos = 0;
}
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
if (stream->out_pos == stream->out_size) {
stream->out_size = dlen;
stream->out_pos = 0;
}
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
status = xz_dec_run(s, stream);
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
if ((status == XZ_STREAM_END) || (stream->out_pos == stream->out_size)) {
*decomp_len = stream->out_pos;
}
2021-06-04 17:51:55 +08:00
2021-06-20 12:25:46 +08:00
return status;
2021-06-04 17:51:55 +08:00
}
void xz_uncompress_end()
{
2021-06-20 12:25:46 +08:00
xz_dec_end(s);
2021-06-04 17:51:55 +08:00
}