busybox/archival/libarchive/unxz/xz.h
<<
>>
Prefs
   1/*
   2 * XZ decompressor
   3 *
   4 * Authors: Lasse Collin <lasse.collin@tukaani.org>
   5 *          Igor Pavlov <http://7-zip.org/>
   6 *
   7 * This file has been put into the public domain.
   8 * You can do whatever you want with this file.
   9 */
  10
  11#ifndef XZ_H
  12#define XZ_H
  13
  14#ifdef __KERNEL__
  15#       include <linux/stddef.h>
  16#       include <linux/types.h>
  17#else
  18#       include <stddef.h>
  19#       include <stdint.h>
  20#endif
  21
  22/* In Linux, this is used to make extern functions static when needed. */
  23#ifndef XZ_EXTERN
  24#       define XZ_EXTERN extern
  25#endif
  26
  27/* In Linux, this is used to mark the functions with __init when needed. */
  28#ifndef XZ_FUNC
  29#       define XZ_FUNC
  30#endif
  31
  32/**
  33 * enum xz_mode - Operation mode
  34 *
  35 * @XZ_SINGLE:              Single-call mode. This uses less RAM than
  36 *                          than multi-call modes, because the LZMA2
  37 *                          dictionary doesn't need to be allocated as
  38 *                          part of the decoder state. All required data
  39 *                          structures are allocated at initialization,
  40 *                          so xz_dec_run() cannot return XZ_MEM_ERROR.
  41 * @XZ_PREALLOC:            Multi-call mode with preallocated LZMA2
  42 *                          dictionary buffer. All data structures are
  43 *                          allocated at initialization, so xz_dec_run()
  44 *                          cannot return XZ_MEM_ERROR.
  45 * @XZ_DYNALLOC:            Multi-call mode. The LZMA2 dictionary is
  46 *                          allocated once the required size has been
  47 *                          parsed from the stream headers. If the
  48 *                          allocation fails, xz_dec_run() will return
  49 *                          XZ_MEM_ERROR.
  50 *
  51 * It is possible to enable support only for a subset of the above
  52 * modes at compile time by defining XZ_DEC_SINGLE, XZ_DEC_PREALLOC,
  53 * or XZ_DEC_DYNALLOC. The xz_dec kernel module is always compiled
  54 * with support for all operation modes, but the preboot code may
  55 * be built with fewer features to minimize code size.
  56 */
  57enum xz_mode {
  58        XZ_SINGLE,
  59        XZ_PREALLOC,
  60        XZ_DYNALLOC
  61};
  62
  63/**
  64 * enum xz_ret - Return codes
  65 * @XZ_OK:                  Everything is OK so far. More input or more
  66 *                          output space is required to continue. This
  67 *                          return code is possible only in multi-call mode
  68 *                          (XZ_PREALLOC or XZ_DYNALLOC).
  69 * @XZ_STREAM_END:          Operation finished successfully.
  70 * @XZ_UNSUPPORTED_CHECK:   Integrity check type is not supported. Decoding
  71 *                          is still possible in multi-call mode by simply
  72 *                          calling xz_dec_run() again.
  73 *                          NOTE: This return value is used only if
  74 *                          XZ_DEC_ANY_CHECK was defined at build time,
  75 *                          which is not used in the kernel. Unsupported
  76 *                          check types return XZ_OPTIONS_ERROR if
  77 *                          XZ_DEC_ANY_CHECK was not defined at build time.
  78 * @XZ_MEM_ERROR:           Allocating memory failed. This return code is
  79 *                          possible only if the decoder was initialized
  80 *                          with XZ_DYNALLOC. The amount of memory that was
  81 *                          tried to be allocated was no more than the
  82 *                          dict_max argument given to xz_dec_init().
  83 * @XZ_MEMLIMIT_ERROR:      A bigger LZMA2 dictionary would be needed than
  84 *                          allowed by the dict_max argument given to
  85 *                          xz_dec_init(). This return value is possible
  86 *                          only in multi-call mode (XZ_PREALLOC or
  87 *                          XZ_DYNALLOC); the single-call mode (XZ_SINGLE)
  88 *                          ignores the dict_max argument.
  89 * @XZ_FORMAT_ERROR:        File format was not recognized (wrong magic
  90 *                          bytes).
  91 * @XZ_OPTIONS_ERROR:       This implementation doesn't support the requested
  92 *                          compression options. In the decoder this means
  93 *                          that the header CRC32 matches, but the header
  94 *                          itself specifies something that we don't support.
  95 * @XZ_DATA_ERROR:          Compressed data is corrupt.
  96 * @XZ_BUF_ERROR:           Cannot make any progress. Details are slightly
  97 *                          different between multi-call and single-call
  98 *                          mode; more information below.
  99 *
 100 * In multi-call mode, XZ_BUF_ERROR is returned when two consecutive calls
 101 * to XZ code cannot consume any input and cannot produce any new output.
 102 * This happens when there is no new input available, or the output buffer
 103 * is full while at least one output byte is still pending. Assuming your
 104 * code is not buggy, you can get this error only when decoding a compressed
 105 * stream that is truncated or otherwise corrupt.
 106 *
 107 * In single-call mode, XZ_BUF_ERROR is returned only when the output buffer
 108 * is too small, or the compressed input is corrupt in a way that makes the
 109 * decoder produce more output than the caller expected. When it is
 110 * (relatively) clear that the compressed input is truncated, XZ_DATA_ERROR
 111 * is used instead of XZ_BUF_ERROR.
 112 */
 113enum xz_ret {
 114        XZ_OK,
 115        XZ_STREAM_END,
 116        XZ_UNSUPPORTED_CHECK,
 117        XZ_MEM_ERROR,
 118        XZ_MEMLIMIT_ERROR,
 119        XZ_FORMAT_ERROR,
 120        XZ_OPTIONS_ERROR,
 121        XZ_DATA_ERROR,
 122        XZ_BUF_ERROR
 123};
 124
 125/**
 126 * struct xz_buf - Passing input and output buffers to XZ code
 127 * @in:         Beginning of the input buffer. This may be NULL if and only
 128 *              if in_pos is equal to in_size.
 129 * @in_pos:     Current position in the input buffer. This must not exceed
 130 *              in_size.
 131 * @in_size:    Size of the input buffer
 132 * @out:        Beginning of the output buffer. This may be NULL if and only
 133 *              if out_pos is equal to out_size.
 134 * @out_pos:    Current position in the output buffer. This must not exceed
 135 *              out_size.
 136 * @out_size:   Size of the output buffer
 137 *
 138 * Only the contents of the output buffer from out[out_pos] onward, and
 139 * the variables in_pos and out_pos are modified by the XZ code.
 140 */
 141struct xz_buf {
 142        const uint8_t *in;
 143        size_t in_pos;
 144        size_t in_size;
 145
 146        uint8_t *out;
 147        size_t out_pos;
 148        size_t out_size;
 149};
 150
 151/**
 152 * struct xz_dec - Opaque type to hold the XZ decoder state
 153 */
 154struct xz_dec;
 155
 156/**
 157 * xz_dec_init() - Allocate and initialize a XZ decoder state
 158 * @mode:       Operation mode
 159 * @dict_max:   Maximum size of the LZMA2 dictionary (history buffer) for
 160 *              multi-call decoding. This is ignored in single-call mode
 161 *              (mode == XZ_SINGLE). LZMA2 dictionary is always 2^n bytes
 162 *              or 2^n + 2^(n-1) bytes (the latter sizes are less common
 163 *              in practice), so other values for dict_max don't make sense.
 164 *              In the kernel, dictionary sizes of 64 KiB, 128 KiB, 256 KiB,
 165 *              512 KiB, and 1 MiB are probably the only reasonable values,
 166 *              except for kernel and initramfs images where a bigger
 167 *              dictionary can be fine and useful.
 168 *
 169 * Single-call mode (XZ_SINGLE): xz_dec_run() decodes the whole stream at
 170 * once. The caller must provide enough output space or the decoding will
 171 * fail. The output space is used as the dictionary buffer, which is why
 172 * there is no need to allocate the dictionary as part of the decoder's
 173 * internal state.
 174 *
 175 * Because the output buffer is used as the workspace, streams encoded using
 176 * a big dictionary are not a problem in single-call mode. It is enough that
 177 * the output buffer is big enough to hold the actual uncompressed data; it
 178 * can be smaller than the dictionary size stored in the stream headers.
 179 *
 180 * Multi-call mode with preallocated dictionary (XZ_PREALLOC): dict_max bytes
 181 * of memory is preallocated for the LZMA2 dictionary. This way there is no
 182 * risk that xz_dec_run() could run out of memory, since xz_dec_run() will
 183 * never allocate any memory. Instead, if the preallocated dictionary is too
 184 * small for decoding the given input stream, xz_dec_run() will return
 185 * XZ_MEMLIMIT_ERROR. Thus, it is important to know what kind of data will be
 186 * decoded to avoid allocating excessive amount of memory for the dictionary.
 187 *
 188 * Multi-call mode with dynamically allocated dictionary (XZ_DYNALLOC):
 189 * dict_max specifies the maximum allowed dictionary size that xz_dec_run()
 190 * may allocate once it has parsed the dictionary size from the stream
 191 * headers. This way excessive allocations can be avoided while still
 192 * limiting the maximum memory usage to a sane value to prevent running the
 193 * system out of memory when decompressing streams from untrusted sources.
 194 *
 195 * On success, xz_dec_init() returns a pointer to struct xz_dec, which is
 196 * ready to be used with xz_dec_run(). If memory allocation fails,
 197 * xz_dec_init() returns NULL.
 198 */
 199XZ_EXTERN struct xz_dec * XZ_FUNC xz_dec_init(
 200                enum xz_mode mode, uint32_t dict_max);
 201
 202/**
 203 * xz_dec_run() - Run the XZ decoder
 204 * @s:          Decoder state allocated using xz_dec_init()
 205 * @b:          Input and output buffers
 206 *
 207 * The possible return values depend on build options and operation mode.
 208 * See enum xz_ret for details.
 209 *
 210 * NOTE: If an error occurs in single-call mode (return value is not
 211 * XZ_STREAM_END), b->in_pos and b->out_pos are not modified, and the
 212 * contents of the output buffer from b->out[b->out_pos] onward are
 213 * undefined. This is true even after XZ_BUF_ERROR, because with some filter
 214 * chains, there may be a second pass over the output buffer, and this pass
 215 * cannot be properly done if the output buffer is truncated. Thus, you
 216 * cannot give the single-call decoder a too small buffer and then expect to
 217 * get that amount valid data from the beginning of the stream. You must use
 218 * the multi-call decoder if you don't want to uncompress the whole stream.
 219 */
 220XZ_EXTERN enum xz_ret XZ_FUNC xz_dec_run(struct xz_dec *s, struct xz_buf *b);
 221
 222/**
 223 * xz_dec_reset() - Reset an already allocated decoder state
 224 * @s:          Decoder state allocated using xz_dec_init()
 225 *
 226 * This function can be used to reset the multi-call decoder state without
 227 * freeing and reallocating memory with xz_dec_end() and xz_dec_init().
 228 *
 229 * In single-call mode, xz_dec_reset() is always called in the beginning of
 230 * xz_dec_run(). Thus, explicit call to xz_dec_reset() is useful only in
 231 * multi-call mode.
 232 */
 233XZ_EXTERN void XZ_FUNC xz_dec_reset(struct xz_dec *s);
 234
 235/**
 236 * xz_dec_end() - Free the memory allocated for the decoder state
 237 * @s:          Decoder state allocated using xz_dec_init(). If s is NULL,
 238 *              this function does nothing.
 239 */
 240XZ_EXTERN void XZ_FUNC xz_dec_end(struct xz_dec *s);
 241
 242/*
 243 * Standalone build (userspace build or in-kernel build for boot time use)
 244 * needs a CRC32 implementation. For normal in-kernel use, kernel's own
 245 * CRC32 module is used instead, and users of this module don't need to
 246 * care about the functions below.
 247 */
 248#ifndef XZ_INTERNAL_CRC32
 249#       ifdef __KERNEL__
 250#               define XZ_INTERNAL_CRC32 0
 251#       else
 252#               define XZ_INTERNAL_CRC32 1
 253#       endif
 254#endif
 255
 256#if XZ_INTERNAL_CRC32
 257/*
 258 * This must be called before any other xz_* function to initialize
 259 * the CRC32 lookup table.
 260 */
 261XZ_EXTERN void XZ_FUNC xz_crc32_init(void);
 262
 263/*
 264 * Update CRC32 value using the polynomial from IEEE-802.3. To start a new
 265 * calculation, the third argument must be zero. To continue the calculation,
 266 * the previously returned value is passed as the third argument.
 267 */
 268XZ_EXTERN uint32_t XZ_FUNC xz_crc32(
 269                const uint8_t *buf, size_t size, uint32_t crc);
 270#endif
 271#endif
 272