vli.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * \file lzma/vli.h
  3. * \brief Variable-length integer handling
  4. *
  5. * In the .xz format, most integers are encoded in a variable-length
  6. * representation, which is sometimes called little endian base-128 encoding.
  7. * This saves space when smaller values are more likely than bigger values.
  8. *
  9. * The encoding scheme encodes seven bits to every byte, using minimum
  10. * number of bytes required to represent the given value. Encodings that use
  11. * non-minimum number of bytes are invalid, thus every integer has exactly
  12. * one encoded representation. The maximum number of bits in a VLI is 63,
  13. * thus the vli argument must be less than or equal to UINT64_MAX / 2. You
  14. * should use LZMA_VLI_MAX for clarity.
  15. */
  16. /*
  17. * Author: Lasse Collin
  18. *
  19. * This file has been put into the public domain.
  20. * You can do whatever you want with this file.
  21. *
  22. * See ../lzma.h for information about liblzma as a whole.
  23. */
  24. #ifndef LZMA_H_INTERNAL
  25. # error Never include this file directly. Use <lzma.h> instead.
  26. #endif
  27. /**
  28. * \brief Maximum supported value of a variable-length integer
  29. */
  30. #define LZMA_VLI_MAX (UINT64_MAX / 2)
  31. /**
  32. * \brief VLI value to denote that the value is unknown
  33. */
  34. #define LZMA_VLI_UNKNOWN UINT64_MAX
  35. /**
  36. * \brief Maximum supported encoded length of variable length integers
  37. */
  38. #define LZMA_VLI_BYTES_MAX 9
  39. /**
  40. * \brief VLI constant suffix
  41. */
  42. #define LZMA_VLI_C(n) UINT64_C(n)
  43. /**
  44. * \brief Variable-length integer type
  45. *
  46. * Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is
  47. * indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the
  48. * underlaying integer type.
  49. *
  50. * lzma_vli will be uint64_t for the foreseeable future. If a bigger size
  51. * is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will
  52. * not overflow lzma_vli. This simplifies integer overflow detection.
  53. */
  54. typedef uint64_t lzma_vli;
  55. /**
  56. * \brief Validate a variable-length integer
  57. *
  58. * This is useful to test that application has given acceptable values
  59. * for example in the uncompressed_size and compressed_size variables.
  60. *
  61. * \return True if the integer is representable as VLI or if it
  62. * indicates unknown value.
  63. */
  64. #define lzma_vli_is_valid(vli) \
  65. ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN)
  66. /**
  67. * \brief Encode a variable-length integer
  68. *
  69. * This function has two modes: single-call and multi-call. Single-call mode
  70. * encodes the whole integer at once; it is an error if the output buffer is
  71. * too small. Multi-call mode saves the position in *vli_pos, and thus it is
  72. * possible to continue encoding if the buffer becomes full before the whole
  73. * integer has been encoded.
  74. *
  75. * \param vli Integer to be encoded
  76. * \param vli_pos How many VLI-encoded bytes have already been written
  77. * out. When starting to encode a new integer in
  78. * multi-call mode, *vli_pos must be set to zero.
  79. * To use single-call encoding, set vli_pos to NULL.
  80. * \param out Beginning of the output buffer
  81. * \param out_pos The next byte will be written to out[*out_pos].
  82. * \param out_size Size of the out buffer; the first byte into
  83. * which no data is written to is out[out_size].
  84. *
  85. * \return Slightly different return values are used in multi-call and
  86. * single-call modes.
  87. *
  88. * Single-call (vli_pos == NULL):
  89. * - LZMA_OK: Integer successfully encoded.
  90. * - LZMA_PROG_ERROR: Arguments are not sane. This can be due
  91. * to too little output space; single-call mode doesn't use
  92. * LZMA_BUF_ERROR, since the application should have checked
  93. * the encoded size with lzma_vli_size().
  94. *
  95. * Multi-call (vli_pos != NULL):
  96. * - LZMA_OK: So far all OK, but the integer is not
  97. * completely written out yet.
  98. * - LZMA_STREAM_END: Integer successfully encoded.
  99. * - LZMA_BUF_ERROR: No output space was provided.
  100. * - LZMA_PROG_ERROR: Arguments are not sane.
  101. */
  102. extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos,
  103. uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow;
  104. /**
  105. * \brief Decode a variable-length integer
  106. *
  107. * Like lzma_vli_encode(), this function has single-call and multi-call modes.
  108. *
  109. * \param vli Pointer to decoded integer. The decoder will
  110. * initialize it to zero when *vli_pos == 0, so
  111. * application isn't required to initialize *vli.
  112. * \param vli_pos How many bytes have already been decoded. When
  113. * starting to decode a new integer in multi-call
  114. * mode, *vli_pos must be initialized to zero. To
  115. * use single-call decoding, set vli_pos to NULL.
  116. * \param in Beginning of the input buffer
  117. * \param in_pos The next byte will be read from in[*in_pos].
  118. * \param in_size Size of the input buffer; the first byte that
  119. * won't be read is in[in_size].
  120. *
  121. * \return Slightly different return values are used in multi-call and
  122. * single-call modes.
  123. *
  124. * Single-call (vli_pos == NULL):
  125. * - LZMA_OK: Integer successfully decoded.
  126. * - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting
  127. * the end of the input buffer before the whole integer was
  128. * decoded; providing no input at all will use LZMA_DATA_ERROR.
  129. * - LZMA_PROG_ERROR: Arguments are not sane.
  130. *
  131. * Multi-call (vli_pos != NULL):
  132. * - LZMA_OK: So far all OK, but the integer is not
  133. * completely decoded yet.
  134. * - LZMA_STREAM_END: Integer successfully decoded.
  135. * - LZMA_DATA_ERROR: Integer is corrupt.
  136. * - LZMA_BUF_ERROR: No input was provided.
  137. * - LZMA_PROG_ERROR: Arguments are not sane.
  138. */
  139. extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos,
  140. const uint8_t *in, size_t *in_pos, size_t in_size)
  141. lzma_nothrow;
  142. /**
  143. * \brief Get the number of bytes required to encode a VLI
  144. *
  145. * \return Number of bytes on success (1-9). If vli isn't valid,
  146. * zero is returned.
  147. */
  148. extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli)
  149. lzma_nothrow lzma_attr_pure;