Path: blob/main/sys/contrib/xz-embedded/linux/lib/xz/xz_dec_stream.c
48521 views
/*1* .xz Stream decoder2*3* Author: Lasse Collin <[email protected]>4*5* This file has been put into the public domain.6* You can do whatever you want with this file.7*/89#include "xz_private.h"10#include "xz_stream.h"1112#ifdef XZ_USE_CRC6413# define IS_CRC64(check_type) ((check_type) == XZ_CHECK_CRC64)14#else15# define IS_CRC64(check_type) false16#endif1718/* Hash used to validate the Index field */19struct xz_dec_hash {20vli_type unpadded;21vli_type uncompressed;22uint32_t crc32;23};2425struct xz_dec {26/* Position in dec_main() */27enum {28SEQ_STREAM_HEADER,29SEQ_BLOCK_START,30SEQ_BLOCK_HEADER,31SEQ_BLOCK_UNCOMPRESS,32SEQ_BLOCK_PADDING,33SEQ_BLOCK_CHECK,34SEQ_INDEX,35SEQ_INDEX_PADDING,36SEQ_INDEX_CRC32,37SEQ_STREAM_FOOTER,38SEQ_STREAM_PADDING39} sequence;4041/* Position in variable-length integers and Check fields */42uint32_t pos;4344/* Variable-length integer decoded by dec_vli() */45vli_type vli;4647/* Saved in_pos and out_pos */48size_t in_start;49size_t out_start;5051#ifdef XZ_USE_CRC6452/* CRC32 or CRC64 value in Block or CRC32 value in Index */53uint64_t crc;54#else55/* CRC32 value in Block or Index */56uint32_t crc;57#endif5859/* Type of the integrity check calculated from uncompressed data */60enum xz_check check_type;6162/* Operation mode */63enum xz_mode mode;6465/*66* True if the next call to xz_dec_run() is allowed to return67* XZ_BUF_ERROR.68*/69bool allow_buf_error;7071/* Information stored in Block Header */72struct {73/*74* Value stored in the Compressed Size field, or75* VLI_UNKNOWN if Compressed Size is not present.76*/77vli_type compressed;7879/*80* Value stored in the Uncompressed Size field, or81* VLI_UNKNOWN if Uncompressed Size is not present.82*/83vli_type uncompressed;8485/* Size of the Block Header field */86uint32_t size;87} block_header;8889/* Information collected when decoding Blocks */90struct {91/* Observed compressed size of the current Block */92vli_type compressed;9394/* Observed uncompressed size of the current Block */95vli_type uncompressed;9697/* Number of Blocks decoded so far */98vli_type count;99100/*101* Hash calculated from the Block sizes. This is used to102* validate the Index field.103*/104struct xz_dec_hash hash;105} block;106107/* Variables needed when verifying the Index field */108struct {109/* Position in dec_index() */110enum {111SEQ_INDEX_COUNT,112SEQ_INDEX_UNPADDED,113SEQ_INDEX_UNCOMPRESSED114} sequence;115116/* Size of the Index in bytes */117vli_type size;118119/* Number of Records (matches block.count in valid files) */120vli_type count;121122/*123* Hash calculated from the Records (matches block.hash in124* valid files).125*/126struct xz_dec_hash hash;127} index;128129/*130* Temporary buffer needed to hold Stream Header, Block Header,131* and Stream Footer. The Block Header is the biggest (1 KiB)132* so we reserve space according to that. buf[] has to be aligned133* to a multiple of four bytes; the size_t variables before it134* should guarantee this.135*/136struct {137size_t pos;138size_t size;139uint8_t buf[1024];140} temp;141142struct xz_dec_lzma2 *lzma2;143144#ifdef XZ_DEC_BCJ145struct xz_dec_bcj *bcj;146bool bcj_active;147#endif148};149150#ifdef XZ_DEC_ANY_CHECK151/* Sizes of the Check field with different Check IDs */152static const uint8_t check_sizes[16] = {1530,1544, 4, 4,1558, 8, 8,15616, 16, 16,15732, 32, 32,15864, 64, 64159};160#endif161162/*163* Fill s->temp by copying data starting from b->in[b->in_pos]. Caller164* must have set s->temp.pos to indicate how much data we are supposed165* to copy into s->temp.buf. Return true once s->temp.pos has reached166* s->temp.size.167*/168static bool fill_temp(struct xz_dec *s, struct xz_buf *b)169{170size_t copy_size = min_t(size_t,171b->in_size - b->in_pos, s->temp.size - s->temp.pos);172173memcpy(s->temp.buf + s->temp.pos, b->in + b->in_pos, copy_size);174b->in_pos += copy_size;175s->temp.pos += copy_size;176177if (s->temp.pos == s->temp.size) {178s->temp.pos = 0;179return true;180}181182return false;183}184185/* Decode a variable-length integer (little-endian base-128 encoding) */186static enum xz_ret dec_vli(struct xz_dec *s, const uint8_t *in,187size_t *in_pos, size_t in_size)188{189uint8_t byte;190191if (s->pos == 0)192s->vli = 0;193194while (*in_pos < in_size) {195byte = in[*in_pos];196++*in_pos;197198s->vli |= (vli_type)(byte & 0x7F) << s->pos;199200if ((byte & 0x80) == 0) {201/* Don't allow non-minimal encodings. */202if (byte == 0 && s->pos != 0)203return XZ_DATA_ERROR;204205s->pos = 0;206return XZ_STREAM_END;207}208209s->pos += 7;210if (s->pos == 7 * VLI_BYTES_MAX)211return XZ_DATA_ERROR;212}213214return XZ_OK;215}216217/*218* Decode the Compressed Data field from a Block. Update and validate219* the observed compressed and uncompressed sizes of the Block so that220* they don't exceed the values possibly stored in the Block Header221* (validation assumes that no integer overflow occurs, since vli_type222* is normally uint64_t). Update the CRC32 or CRC64 value if presence of223* the CRC32 or CRC64 field was indicated in Stream Header.224*225* Once the decoding is finished, validate that the observed sizes match226* the sizes possibly stored in the Block Header. Update the hash and227* Block count, which are later used to validate the Index field.228*/229static enum xz_ret dec_block(struct xz_dec *s, struct xz_buf *b)230{231enum xz_ret ret;232233s->in_start = b->in_pos;234s->out_start = b->out_pos;235236#ifdef XZ_DEC_BCJ237if (s->bcj_active)238ret = xz_dec_bcj_run(s->bcj, s->lzma2, b);239else240#endif241ret = xz_dec_lzma2_run(s->lzma2, b);242243s->block.compressed += b->in_pos - s->in_start;244s->block.uncompressed += b->out_pos - s->out_start;245246/*247* There is no need to separately check for VLI_UNKNOWN, since248* the observed sizes are always smaller than VLI_UNKNOWN.249*/250if (s->block.compressed > s->block_header.compressed251|| s->block.uncompressed252> s->block_header.uncompressed)253return XZ_DATA_ERROR;254255if (s->check_type == XZ_CHECK_CRC32)256s->crc = xz_crc32(b->out + s->out_start,257b->out_pos - s->out_start, s->crc);258#ifdef XZ_USE_CRC64259else if (s->check_type == XZ_CHECK_CRC64)260s->crc = xz_crc64(b->out + s->out_start,261b->out_pos - s->out_start, s->crc);262#endif263264if (ret == XZ_STREAM_END) {265if (s->block_header.compressed != VLI_UNKNOWN266&& s->block_header.compressed267!= s->block.compressed)268return XZ_DATA_ERROR;269270if (s->block_header.uncompressed != VLI_UNKNOWN271&& s->block_header.uncompressed272!= s->block.uncompressed)273return XZ_DATA_ERROR;274275s->block.hash.unpadded += s->block_header.size276+ s->block.compressed;277278#ifdef XZ_DEC_ANY_CHECK279s->block.hash.unpadded += check_sizes[s->check_type];280#else281if (s->check_type == XZ_CHECK_CRC32)282s->block.hash.unpadded += 4;283else if (IS_CRC64(s->check_type))284s->block.hash.unpadded += 8;285#endif286287s->block.hash.uncompressed += s->block.uncompressed;288s->block.hash.crc32 = xz_crc32(289(const uint8_t *)&s->block.hash,290sizeof(s->block.hash), s->block.hash.crc32);291292++s->block.count;293}294295return ret;296}297298/* Update the Index size and the CRC32 value. */299static void index_update(struct xz_dec *s, const struct xz_buf *b)300{301size_t in_used = b->in_pos - s->in_start;302s->index.size += in_used;303s->crc = xz_crc32(b->in + s->in_start, in_used, s->crc);304}305306/*307* Decode the Number of Records, Unpadded Size, and Uncompressed Size308* fields from the Index field. That is, Index Padding and CRC32 are not309* decoded by this function.310*311* This can return XZ_OK (more input needed), XZ_STREAM_END (everything312* successfully decoded), or XZ_DATA_ERROR (input is corrupt).313*/314static enum xz_ret dec_index(struct xz_dec *s, struct xz_buf *b)315{316enum xz_ret ret;317318do {319ret = dec_vli(s, b->in, &b->in_pos, b->in_size);320if (ret != XZ_STREAM_END) {321index_update(s, b);322return ret;323}324325switch (s->index.sequence) {326case SEQ_INDEX_COUNT:327s->index.count = s->vli;328329/*330* Validate that the Number of Records field331* indicates the same number of Records as332* there were Blocks in the Stream.333*/334if (s->index.count != s->block.count)335return XZ_DATA_ERROR;336337s->index.sequence = SEQ_INDEX_UNPADDED;338break;339340case SEQ_INDEX_UNPADDED:341s->index.hash.unpadded += s->vli;342s->index.sequence = SEQ_INDEX_UNCOMPRESSED;343break;344345case SEQ_INDEX_UNCOMPRESSED:346s->index.hash.uncompressed += s->vli;347s->index.hash.crc32 = xz_crc32(348(const uint8_t *)&s->index.hash,349sizeof(s->index.hash),350s->index.hash.crc32);351--s->index.count;352s->index.sequence = SEQ_INDEX_UNPADDED;353break;354}355} while (s->index.count > 0);356357return XZ_STREAM_END;358}359360/*361* Validate that the next four or eight input bytes match the value362* of s->crc. s->pos must be zero when starting to validate the first byte.363* The "bits" argument allows using the same code for both CRC32 and CRC64.364*/365static enum xz_ret crc_validate(struct xz_dec *s, struct xz_buf *b,366uint32_t bits)367{368do {369if (b->in_pos == b->in_size)370return XZ_OK;371372if (((s->crc >> s->pos) & 0xFF) != b->in[b->in_pos++])373return XZ_DATA_ERROR;374375s->pos += 8;376377} while (s->pos < bits);378379s->crc = 0;380s->pos = 0;381382return XZ_STREAM_END;383}384385#ifdef XZ_DEC_ANY_CHECK386/*387* Skip over the Check field when the Check ID is not supported.388* Returns true once the whole Check field has been skipped over.389*/390static bool check_skip(struct xz_dec *s, struct xz_buf *b)391{392while (s->pos < check_sizes[s->check_type]) {393if (b->in_pos == b->in_size)394return false;395396++b->in_pos;397++s->pos;398}399400s->pos = 0;401402return true;403}404#endif405406/* Decode the Stream Header field (the first 12 bytes of the .xz Stream). */407static enum xz_ret dec_stream_header(struct xz_dec *s)408{409if (!memeq(s->temp.buf, HEADER_MAGIC, HEADER_MAGIC_SIZE))410return XZ_FORMAT_ERROR;411412if (xz_crc32(s->temp.buf + HEADER_MAGIC_SIZE, 2, 0)413!= get_le32(s->temp.buf + HEADER_MAGIC_SIZE + 2))414return XZ_DATA_ERROR;415416if (s->temp.buf[HEADER_MAGIC_SIZE] != 0)417return XZ_OPTIONS_ERROR;418419/*420* Of integrity checks, we support none (Check ID = 0),421* CRC32 (Check ID = 1), and optionally CRC64 (Check ID = 4).422* However, if XZ_DEC_ANY_CHECK is defined, we will accept other423* check types too, but then the check won't be verified and424* a warning (XZ_UNSUPPORTED_CHECK) will be given.425*/426if (s->temp.buf[HEADER_MAGIC_SIZE + 1] > XZ_CHECK_MAX)427return XZ_OPTIONS_ERROR;428429s->check_type = s->temp.buf[HEADER_MAGIC_SIZE + 1];430431#ifdef XZ_DEC_ANY_CHECK432if (s->check_type > XZ_CHECK_CRC32 && !IS_CRC64(s->check_type))433return XZ_UNSUPPORTED_CHECK;434#else435if (s->check_type > XZ_CHECK_CRC32 && !IS_CRC64(s->check_type))436return XZ_OPTIONS_ERROR;437#endif438439return XZ_OK;440}441442/* Decode the Stream Footer field (the last 12 bytes of the .xz Stream) */443static enum xz_ret dec_stream_footer(struct xz_dec *s)444{445if (!memeq(s->temp.buf + 10, FOOTER_MAGIC, FOOTER_MAGIC_SIZE))446return XZ_DATA_ERROR;447448if (xz_crc32(s->temp.buf + 4, 6, 0) != get_le32(s->temp.buf))449return XZ_DATA_ERROR;450451/*452* Validate Backward Size. Note that we never added the size of the453* Index CRC32 field to s->index.size, thus we use s->index.size / 4454* instead of s->index.size / 4 - 1.455*/456if ((s->index.size >> 2) != get_le32(s->temp.buf + 4))457return XZ_DATA_ERROR;458459if (s->temp.buf[8] != 0 || s->temp.buf[9] != s->check_type)460return XZ_DATA_ERROR;461462/*463* Use XZ_STREAM_END instead of XZ_OK to be more convenient464* for the caller.465*/466return XZ_STREAM_END;467}468469/* Decode the Block Header and initialize the filter chain. */470static enum xz_ret dec_block_header(struct xz_dec *s)471{472enum xz_ret ret;473474/*475* Validate the CRC32. We know that the temp buffer is at least476* eight bytes so this is safe.477*/478s->temp.size -= 4;479if (xz_crc32(s->temp.buf, s->temp.size, 0)480!= get_le32(s->temp.buf + s->temp.size))481return XZ_DATA_ERROR;482483s->temp.pos = 2;484485/*486* Catch unsupported Block Flags. We support only one or two filters487* in the chain, so we catch that with the same test.488*/489#ifdef XZ_DEC_BCJ490if (s->temp.buf[1] & 0x3E)491#else492if (s->temp.buf[1] & 0x3F)493#endif494return XZ_OPTIONS_ERROR;495496/* Compressed Size */497if (s->temp.buf[1] & 0x40) {498if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)499!= XZ_STREAM_END)500return XZ_DATA_ERROR;501502s->block_header.compressed = s->vli;503} else {504s->block_header.compressed = VLI_UNKNOWN;505}506507/* Uncompressed Size */508if (s->temp.buf[1] & 0x80) {509if (dec_vli(s, s->temp.buf, &s->temp.pos, s->temp.size)510!= XZ_STREAM_END)511return XZ_DATA_ERROR;512513s->block_header.uncompressed = s->vli;514} else {515s->block_header.uncompressed = VLI_UNKNOWN;516}517518#ifdef XZ_DEC_BCJ519/* If there are two filters, the first one must be a BCJ filter. */520s->bcj_active = s->temp.buf[1] & 0x01;521if (s->bcj_active) {522if (s->temp.size - s->temp.pos < 2)523return XZ_OPTIONS_ERROR;524525ret = xz_dec_bcj_reset(s->bcj, s->temp.buf[s->temp.pos++]);526if (ret != XZ_OK)527return ret;528529/*530* We don't support custom start offset,531* so Size of Properties must be zero.532*/533if (s->temp.buf[s->temp.pos++] != 0x00)534return XZ_OPTIONS_ERROR;535}536#endif537538/* Valid Filter Flags always take at least two bytes. */539if (s->temp.size - s->temp.pos < 2)540return XZ_DATA_ERROR;541542/* Filter ID = LZMA2 */543if (s->temp.buf[s->temp.pos++] != 0x21)544return XZ_OPTIONS_ERROR;545546/* Size of Properties = 1-byte Filter Properties */547if (s->temp.buf[s->temp.pos++] != 0x01)548return XZ_OPTIONS_ERROR;549550/* Filter Properties contains LZMA2 dictionary size. */551if (s->temp.size - s->temp.pos < 1)552return XZ_DATA_ERROR;553554ret = xz_dec_lzma2_reset(s->lzma2, s->temp.buf[s->temp.pos++]);555if (ret != XZ_OK)556return ret;557558/* The rest must be Header Padding. */559while (s->temp.pos < s->temp.size)560if (s->temp.buf[s->temp.pos++] != 0x00)561return XZ_OPTIONS_ERROR;562563s->temp.pos = 0;564s->block.compressed = 0;565s->block.uncompressed = 0;566567return XZ_OK;568}569570static enum xz_ret dec_main(struct xz_dec *s, struct xz_buf *b)571{572enum xz_ret ret;573574/*575* Store the start position for the case when we are in the middle576* of the Index field.577*/578s->in_start = b->in_pos;579580while (true) {581switch (s->sequence) {582case SEQ_STREAM_HEADER:583/*584* Stream Header is copied to s->temp, and then585* decoded from there. This way if the caller586* gives us only little input at a time, we can587* still keep the Stream Header decoding code588* simple. Similar approach is used in many places589* in this file.590*/591if (!fill_temp(s, b))592return XZ_OK;593594/*595* If dec_stream_header() returns596* XZ_UNSUPPORTED_CHECK, it is still possible597* to continue decoding if working in multi-call598* mode. Thus, update s->sequence before calling599* dec_stream_header().600*/601s->sequence = SEQ_BLOCK_START;602603ret = dec_stream_header(s);604if (ret != XZ_OK)605return ret;606607/* Fall through */608609case SEQ_BLOCK_START:610/* We need one byte of input to continue. */611if (b->in_pos == b->in_size)612return XZ_OK;613614/* See if this is the beginning of the Index field. */615if (b->in[b->in_pos] == 0) {616s->in_start = b->in_pos++;617s->sequence = SEQ_INDEX;618break;619}620621/*622* Calculate the size of the Block Header and623* prepare to decode it.624*/625s->block_header.size626= ((uint32_t)b->in[b->in_pos] + 1) * 4;627628s->temp.size = s->block_header.size;629s->temp.pos = 0;630s->sequence = SEQ_BLOCK_HEADER;631632/* Fall through */633634case SEQ_BLOCK_HEADER:635if (!fill_temp(s, b))636return XZ_OK;637638ret = dec_block_header(s);639if (ret != XZ_OK)640return ret;641642s->sequence = SEQ_BLOCK_UNCOMPRESS;643644/* Fall through */645646case SEQ_BLOCK_UNCOMPRESS:647ret = dec_block(s, b);648if (ret != XZ_STREAM_END)649return ret;650651s->sequence = SEQ_BLOCK_PADDING;652653/* Fall through */654655case SEQ_BLOCK_PADDING:656/*657* Size of Compressed Data + Block Padding658* must be a multiple of four. We don't need659* s->block.compressed for anything else660* anymore, so we use it here to test the size661* of the Block Padding field.662*/663while (s->block.compressed & 3) {664if (b->in_pos == b->in_size)665return XZ_OK;666667if (b->in[b->in_pos++] != 0)668return XZ_DATA_ERROR;669670++s->block.compressed;671}672673s->sequence = SEQ_BLOCK_CHECK;674675/* Fall through */676677case SEQ_BLOCK_CHECK:678if (s->check_type == XZ_CHECK_CRC32) {679ret = crc_validate(s, b, 32);680if (ret != XZ_STREAM_END)681return ret;682}683else if (IS_CRC64(s->check_type)) {684ret = crc_validate(s, b, 64);685if (ret != XZ_STREAM_END)686return ret;687}688#ifdef XZ_DEC_ANY_CHECK689else if (!check_skip(s, b)) {690return XZ_OK;691}692#endif693694s->sequence = SEQ_BLOCK_START;695break;696697case SEQ_INDEX:698ret = dec_index(s, b);699if (ret != XZ_STREAM_END)700return ret;701702s->sequence = SEQ_INDEX_PADDING;703704/* Fall through */705706case SEQ_INDEX_PADDING:707while ((s->index.size + (b->in_pos - s->in_start))708& 3) {709if (b->in_pos == b->in_size) {710index_update(s, b);711return XZ_OK;712}713714if (b->in[b->in_pos++] != 0)715return XZ_DATA_ERROR;716}717718/* Finish the CRC32 value and Index size. */719index_update(s, b);720721/* Compare the hashes to validate the Index field. */722if (!memeq(&s->block.hash, &s->index.hash,723sizeof(s->block.hash)))724return XZ_DATA_ERROR;725726s->sequence = SEQ_INDEX_CRC32;727728/* Fall through */729730case SEQ_INDEX_CRC32:731ret = crc_validate(s, b, 32);732if (ret != XZ_STREAM_END)733return ret;734735s->temp.size = STREAM_HEADER_SIZE;736s->sequence = SEQ_STREAM_FOOTER;737738/* Fall through */739740case SEQ_STREAM_FOOTER:741if (!fill_temp(s, b))742return XZ_OK;743744return dec_stream_footer(s);745746case SEQ_STREAM_PADDING:747/* Never reached, only silencing a warning */748break;749}750}751752/* Never reached */753}754755/*756* xz_dec_run() is a wrapper for dec_main() to handle some special cases in757* multi-call and single-call decoding.758*759* In multi-call mode, we must return XZ_BUF_ERROR when it seems clear that we760* are not going to make any progress anymore. This is to prevent the caller761* from calling us infinitely when the input file is truncated or otherwise762* corrupt. Since zlib-style API allows that the caller fills the input buffer763* only when the decoder doesn't produce any new output, we have to be careful764* to avoid returning XZ_BUF_ERROR too easily: XZ_BUF_ERROR is returned only765* after the second consecutive call to xz_dec_run() that makes no progress.766*767* In single-call mode, if we couldn't decode everything and no error768* occurred, either the input is truncated or the output buffer is too small.769* Since we know that the last input byte never produces any output, we know770* that if all the input was consumed and decoding wasn't finished, the file771* must be corrupt. Otherwise the output buffer has to be too small or the772* file is corrupt in a way that decoding it produces too big output.773*774* If single-call decoding fails, we reset b->in_pos and b->out_pos back to775* their original values. This is because with some filter chains there won't776* be any valid uncompressed data in the output buffer unless the decoding777* actually succeeds (that's the price to pay of using the output buffer as778* the workspace).779*/780XZ_EXTERN enum xz_ret xz_dec_run(struct xz_dec *s, struct xz_buf *b)781{782size_t in_start;783size_t out_start;784enum xz_ret ret;785786if (DEC_IS_SINGLE(s->mode))787xz_dec_reset(s);788789in_start = b->in_pos;790out_start = b->out_pos;791ret = dec_main(s, b);792793if (DEC_IS_SINGLE(s->mode)) {794if (ret == XZ_OK)795ret = b->in_pos == b->in_size796? XZ_DATA_ERROR : XZ_BUF_ERROR;797798if (ret != XZ_STREAM_END) {799b->in_pos = in_start;800b->out_pos = out_start;801}802803} else if (ret == XZ_OK && in_start == b->in_pos804&& out_start == b->out_pos) {805if (s->allow_buf_error)806ret = XZ_BUF_ERROR;807808s->allow_buf_error = true;809} else {810s->allow_buf_error = false;811}812813return ret;814}815816#ifdef XZ_DEC_CONCATENATED817XZ_EXTERN enum xz_ret xz_dec_catrun(struct xz_dec *s, struct xz_buf *b,818int finish)819{820enum xz_ret ret;821822if (DEC_IS_SINGLE(s->mode)) {823xz_dec_reset(s);824finish = true;825}826827while (true) {828if (s->sequence == SEQ_STREAM_PADDING) {829/*830* Skip Stream Padding. Its size must be a multiple831* of four bytes which is tracked with s->pos.832*/833while (true) {834if (b->in_pos == b->in_size) {835/*836* Note that if we are repeatedly837* given no input and finish is false,838* we will keep returning XZ_OK even839* though no progress is being made.840* The lack of XZ_BUF_ERROR support841* isn't a problem here because a842* reasonable caller will eventually843* provide more input or set finish844* to true.845*/846if (!finish)847return XZ_OK;848849if (s->pos != 0)850return XZ_DATA_ERROR;851852return XZ_STREAM_END;853}854855if (b->in[b->in_pos] != 0x00) {856if (s->pos != 0)857return XZ_DATA_ERROR;858859break;860}861862++b->in_pos;863s->pos = (s->pos + 1) & 3;864}865866/*867* More input remains. It should be a new Stream.868*869* In single-call mode xz_dec_run() will always call870* xz_dec_reset(). Thus, we need to do it here only871* in multi-call mode.872*/873if (DEC_IS_MULTI(s->mode))874xz_dec_reset(s);875}876877ret = xz_dec_run(s, b);878879if (ret != XZ_STREAM_END)880break;881882s->sequence = SEQ_STREAM_PADDING;883}884885return ret;886}887#endif888889XZ_EXTERN struct xz_dec *xz_dec_init(enum xz_mode mode, uint32_t dict_max)890{891struct xz_dec *s = kmalloc(sizeof(*s), GFP_KERNEL);892if (s == NULL)893return NULL;894895s->mode = mode;896897#ifdef XZ_DEC_BCJ898s->bcj = xz_dec_bcj_create(DEC_IS_SINGLE(mode));899if (s->bcj == NULL)900goto error_bcj;901#endif902903s->lzma2 = xz_dec_lzma2_create(mode, dict_max);904if (s->lzma2 == NULL)905goto error_lzma2;906907xz_dec_reset(s);908return s;909910error_lzma2:911#ifdef XZ_DEC_BCJ912xz_dec_bcj_end(s->bcj);913error_bcj:914#endif915kfree(s);916return NULL;917}918919XZ_EXTERN void xz_dec_reset(struct xz_dec *s)920{921s->sequence = SEQ_STREAM_HEADER;922s->allow_buf_error = false;923s->pos = 0;924s->crc = 0;925memzero(&s->block, sizeof(s->block));926memzero(&s->index, sizeof(s->index));927s->temp.pos = 0;928s->temp.size = STREAM_HEADER_SIZE;929}930931XZ_EXTERN void xz_dec_end(struct xz_dec *s)932{933if (s != NULL) {934xz_dec_lzma2_end(s->lzma2);935#ifdef XZ_DEC_BCJ936xz_dec_bcj_end(s->bcj);937#endif938kfree(s);939}940}941942943