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 | /* |

18 | * Author: Lasse Collin |

19 | * |

20 | * This file has been put into the public domain. |

21 | * You can do whatever you want with this file. |

22 | * |

23 | * See ../lzma.h for information about liblzma as a whole. |

24 | */ |

25 | |

26 | #ifndef LZMA_H_INTERNAL |

27 | # error Never include this file directly. Use <lzma.h> instead. |

28 | #endif |

29 | |

30 | |

31 | /** |

32 | * \brief Maximum supported value of a variable-length integer |

33 | */ |

34 | #define LZMA_VLI_MAX (UINT64_MAX / 2) |

35 | |

36 | /** |

37 | * \brief VLI value to denote that the value is unknown |

38 | */ |

39 | #define LZMA_VLI_UNKNOWN UINT64_MAX |

40 | |

41 | /** |

42 | * \brief Maximum supported encoded length of variable length integers |

43 | */ |

44 | #define LZMA_VLI_BYTES_MAX 9 |

45 | |

46 | /** |

47 | * \brief VLI constant suffix |

48 | */ |

49 | #define LZMA_VLI_C(n) UINT64_C(n) |

50 | |

51 | |

52 | /** |

53 | * \brief Variable-length integer type |

54 | * |

55 | * Valid VLI values are in the range [0, LZMA_VLI_MAX]. Unknown value is |

56 | * indicated with LZMA_VLI_UNKNOWN, which is the maximum value of the |

57 | * underlaying integer type. |

58 | * |

59 | * lzma_vli will be uint64_t for the foreseeable future. If a bigger size |

60 | * is needed in the future, it is guaranteed that 2 * LZMA_VLI_MAX will |

61 | * not overflow lzma_vli. This simplifies integer overflow detection. |

62 | */ |

63 | typedef uint64_t lzma_vli; |

64 | |

65 | |

66 | /** |

67 | * \brief Validate a variable-length integer |

68 | * |

69 | * This is useful to test that application has given acceptable values |

70 | * for example in the uncompressed_size and compressed_size variables. |

71 | * |

72 | * \return True if the integer is representable as VLI or if it |

73 | * indicates unknown value. |

74 | */ |

75 | #define lzma_vli_is_valid(vli) \ |

76 | ((vli) <= LZMA_VLI_MAX || (vli) == LZMA_VLI_UNKNOWN) |

77 | |

78 | |

79 | /** |

80 | * \brief Encode a variable-length integer |

81 | * |

82 | * This function has two modes: single-call and multi-call. Single-call mode |

83 | * encodes the whole integer at once; it is an error if the output buffer is |

84 | * too small. Multi-call mode saves the position in *vli_pos, and thus it is |

85 | * possible to continue encoding if the buffer becomes full before the whole |

86 | * integer has been encoded. |

87 | * |

88 | * \param vli Integer to be encoded |

89 | * \param vli_pos How many VLI-encoded bytes have already been written |

90 | * out. When starting to encode a new integer in |

91 | * multi-call mode, *vli_pos must be set to zero. |

92 | * To use single-call encoding, set vli_pos to NULL. |

93 | * \param out Beginning of the output buffer |

94 | * \param out_pos The next byte will be written to out[*out_pos]. |

95 | * \param out_size Size of the out buffer; the first byte into |

96 | * which no data is written to is out[out_size]. |

97 | * |

98 | * \return Slightly different return values are used in multi-call and |

99 | * single-call modes. |

100 | * |

101 | * Single-call (vli_pos == NULL): |

102 | * - LZMA_OK: Integer successfully encoded. |

103 | * - LZMA_PROG_ERROR: Arguments are not sane. This can be due |

104 | * to too little output space; single-call mode doesn't use |

105 | * LZMA_BUF_ERROR, since the application should have checked |

106 | * the encoded size with lzma_vli_size(). |

107 | * |

108 | * Multi-call (vli_pos != NULL): |

109 | * - LZMA_OK: So far all OK, but the integer is not |

110 | * completely written out yet. |

111 | * - LZMA_STREAM_END: Integer successfully encoded. |

112 | * - LZMA_BUF_ERROR: No output space was provided. |

113 | * - LZMA_PROG_ERROR: Arguments are not sane. |

114 | */ |

115 | extern LZMA_API(lzma_ret) lzma_vli_encode(lzma_vli vli, size_t *vli_pos, |

116 | uint8_t *out, size_t *out_pos, size_t out_size) lzma_nothrow; |

117 | |

118 | |

119 | /** |

120 | * \brief Decode a variable-length integer |

121 | * |

122 | * Like lzma_vli_encode(), this function has single-call and multi-call modes. |

123 | * |

124 | * \param vli Pointer to decoded integer. The decoder will |

125 | * initialize it to zero when *vli_pos == 0, so |

126 | * application isn't required to initialize *vli. |

127 | * \param vli_pos How many bytes have already been decoded. When |

128 | * starting to decode a new integer in multi-call |

129 | * mode, *vli_pos must be initialized to zero. To |

130 | * use single-call decoding, set vli_pos to NULL. |

131 | * \param in Beginning of the input buffer |

132 | * \param in_pos The next byte will be read from in[*in_pos]. |

133 | * \param in_size Size of the input buffer; the first byte that |

134 | * won't be read is in[in_size]. |

135 | * |

136 | * \return Slightly different return values are used in multi-call and |

137 | * single-call modes. |

138 | * |

139 | * Single-call (vli_pos == NULL): |

140 | * - LZMA_OK: Integer successfully decoded. |

141 | * - LZMA_DATA_ERROR: Integer is corrupt. This includes hitting |

142 | * the end of the input buffer before the whole integer was |

143 | * decoded; providing no input at all will use LZMA_DATA_ERROR. |

144 | * - LZMA_PROG_ERROR: Arguments are not sane. |

145 | * |

146 | * Multi-call (vli_pos != NULL): |

147 | * - LZMA_OK: So far all OK, but the integer is not |

148 | * completely decoded yet. |

149 | * - LZMA_STREAM_END: Integer successfully decoded. |

150 | * - LZMA_DATA_ERROR: Integer is corrupt. |

151 | * - LZMA_BUF_ERROR: No input was provided. |

152 | * - LZMA_PROG_ERROR: Arguments are not sane. |

153 | */ |

154 | extern LZMA_API(lzma_ret) lzma_vli_decode(lzma_vli *vli, size_t *vli_pos, |

155 | const uint8_t *in, size_t *in_pos, size_t in_size) |

156 | lzma_nothrow; |

157 | |

158 | |

159 | /** |

160 | * \brief Get the number of bytes required to encode a VLI |

161 | * |

162 | * \return Number of bytes on success (1-9). If vli isn't valid, |

163 | * zero is returned. |

164 | */ |

165 | extern LZMA_API(uint32_t) lzma_vli_size(lzma_vli vli) |

166 | lzma_nothrow lzma_attr_pure; |

167 |