decompress_common.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * decompress_common.h - Code shared by the XPRESS and LZX decompressors
  4. *
  5. * Copyright (C) 2015 Eric Biggers
  6. */
  7. #ifndef _LINUX_NTFS3_LIB_DECOMPRESS_COMMON_H
  8. #define _LINUX_NTFS3_LIB_DECOMPRESS_COMMON_H
  9. #include <linux/string.h>
  10. #include <linux/compiler.h>
  11. #include <linux/types.h>
  12. #include <linux/slab.h>
  13. #include <linux/unaligned.h>
  14. /* "Force inline" macro (not required, but helpful for performance) */
  15. #define forceinline __always_inline
  16. /* Enable whole-word match copying on selected architectures */
  17. #if defined(__i386__) || defined(__x86_64__) || defined(__ARM_FEATURE_UNALIGNED)
  18. # define FAST_UNALIGNED_ACCESS
  19. #endif
  20. /* Size of a machine word */
  21. #define WORDBYTES (sizeof(size_t))
  22. static forceinline void
  23. copy_unaligned_word(const void *src, void *dst)
  24. {
  25. put_unaligned(get_unaligned((const size_t *)src), (size_t *)dst);
  26. }
  27. /* Generate a "word" with platform-dependent size whose bytes all contain the
  28. * value 'b'.
  29. */
  30. static forceinline size_t repeat_byte(u8 b)
  31. {
  32. size_t v;
  33. v = b;
  34. v |= v << 8;
  35. v |= v << 16;
  36. v |= v << ((WORDBYTES == 8) ? 32 : 0);
  37. return v;
  38. }
  39. /* Structure that encapsulates a block of in-memory data being interpreted as a
  40. * stream of bits, optionally with interwoven literal bytes. Bits are assumed
  41. * to be stored in little endian 16-bit coding units, with the bits ordered high
  42. * to low.
  43. */
  44. struct input_bitstream {
  45. /* Bits that have been read from the input buffer. The bits are
  46. * left-justified; the next bit is always bit 31.
  47. */
  48. u32 bitbuf;
  49. /* Number of bits currently held in @bitbuf. */
  50. u32 bitsleft;
  51. /* Pointer to the next byte to be retrieved from the input buffer. */
  52. const u8 *next;
  53. /* Pointer to just past the end of the input buffer. */
  54. const u8 *end;
  55. };
  56. /* Initialize a bitstream to read from the specified input buffer. */
  57. static forceinline void init_input_bitstream(struct input_bitstream *is,
  58. const void *buffer, u32 size)
  59. {
  60. is->bitbuf = 0;
  61. is->bitsleft = 0;
  62. is->next = buffer;
  63. is->end = is->next + size;
  64. }
  65. /* Ensure the bit buffer variable for the bitstream contains at least @num_bits
  66. * bits. Following this, bitstream_peek_bits() and/or bitstream_remove_bits()
  67. * may be called on the bitstream to peek or remove up to @num_bits bits. Note
  68. * that @num_bits must be <= 16.
  69. */
  70. static forceinline void bitstream_ensure_bits(struct input_bitstream *is,
  71. u32 num_bits)
  72. {
  73. if (is->bitsleft < num_bits) {
  74. if (is->end - is->next >= 2) {
  75. is->bitbuf |= (u32)get_unaligned_le16(is->next)
  76. << (16 - is->bitsleft);
  77. is->next += 2;
  78. }
  79. is->bitsleft += 16;
  80. }
  81. }
  82. /* Return the next @num_bits bits from the bitstream, without removing them.
  83. * There must be at least @num_bits remaining in the buffer variable, from a
  84. * previous call to bitstream_ensure_bits().
  85. */
  86. static forceinline u32
  87. bitstream_peek_bits(const struct input_bitstream *is, const u32 num_bits)
  88. {
  89. return (is->bitbuf >> 1) >> (sizeof(is->bitbuf) * 8 - num_bits - 1);
  90. }
  91. /* Remove @num_bits from the bitstream. There must be at least @num_bits
  92. * remaining in the buffer variable, from a previous call to
  93. * bitstream_ensure_bits().
  94. */
  95. static forceinline void
  96. bitstream_remove_bits(struct input_bitstream *is, u32 num_bits)
  97. {
  98. is->bitbuf <<= num_bits;
  99. is->bitsleft -= num_bits;
  100. }
  101. /* Remove and return @num_bits bits from the bitstream. There must be at least
  102. * @num_bits remaining in the buffer variable, from a previous call to
  103. * bitstream_ensure_bits().
  104. */
  105. static forceinline u32
  106. bitstream_pop_bits(struct input_bitstream *is, u32 num_bits)
  107. {
  108. u32 bits = bitstream_peek_bits(is, num_bits);
  109. bitstream_remove_bits(is, num_bits);
  110. return bits;
  111. }
  112. /* Read and return the next @num_bits bits from the bitstream. */
  113. static forceinline u32
  114. bitstream_read_bits(struct input_bitstream *is, u32 num_bits)
  115. {
  116. bitstream_ensure_bits(is, num_bits);
  117. return bitstream_pop_bits(is, num_bits);
  118. }
  119. /* Read and return the next literal byte embedded in the bitstream. */
  120. static forceinline u8
  121. bitstream_read_byte(struct input_bitstream *is)
  122. {
  123. if (unlikely(is->end == is->next))
  124. return 0;
  125. return *is->next++;
  126. }
  127. /* Read and return the next 16-bit integer embedded in the bitstream. */
  128. static forceinline u16
  129. bitstream_read_u16(struct input_bitstream *is)
  130. {
  131. u16 v;
  132. if (unlikely(is->end - is->next < 2))
  133. return 0;
  134. v = get_unaligned_le16(is->next);
  135. is->next += 2;
  136. return v;
  137. }
  138. /* Read and return the next 32-bit integer embedded in the bitstream. */
  139. static forceinline u32
  140. bitstream_read_u32(struct input_bitstream *is)
  141. {
  142. u32 v;
  143. if (unlikely(is->end - is->next < 4))
  144. return 0;
  145. v = get_unaligned_le32(is->next);
  146. is->next += 4;
  147. return v;
  148. }
  149. /* Read into @dst_buffer an array of literal bytes embedded in the bitstream.
  150. * Return either a pointer to the byte past the last written, or NULL if the
  151. * read overflows the input buffer.
  152. */
  153. static forceinline void *bitstream_read_bytes(struct input_bitstream *is,
  154. void *dst_buffer, size_t count)
  155. {
  156. if ((size_t)(is->end - is->next) < count)
  157. return NULL;
  158. memcpy(dst_buffer, is->next, count);
  159. is->next += count;
  160. return (u8 *)dst_buffer + count;
  161. }
  162. /* Align the input bitstream on a coding-unit boundary. */
  163. static forceinline void bitstream_align(struct input_bitstream *is)
  164. {
  165. is->bitsleft = 0;
  166. is->bitbuf = 0;
  167. }
  168. extern int make_huffman_decode_table(u16 decode_table[], const u32 num_syms,
  169. const u32 num_bits, const u8 lens[],
  170. const u32 max_codeword_len,
  171. u16 working_space[]);
  172. /* Reads and returns the next Huffman-encoded symbol from a bitstream. If the
  173. * input data is exhausted, the Huffman symbol is decoded as if the missing bits
  174. * are all zeroes.
  175. */
  176. static forceinline u32 read_huffsym(struct input_bitstream *istream,
  177. const u16 decode_table[],
  178. u32 table_bits,
  179. u32 max_codeword_len)
  180. {
  181. u32 entry;
  182. u32 key_bits;
  183. bitstream_ensure_bits(istream, max_codeword_len);
  184. /* Index the decode table by the next table_bits bits of the input. */
  185. key_bits = bitstream_peek_bits(istream, table_bits);
  186. entry = decode_table[key_bits];
  187. if (entry < 0xC000) {
  188. /* Fast case: The decode table directly provided the
  189. * symbol and codeword length. The low 11 bits are the
  190. * symbol, and the high 5 bits are the codeword length.
  191. */
  192. bitstream_remove_bits(istream, entry >> 11);
  193. return entry & 0x7FF;
  194. }
  195. /* Slow case: The codeword for the symbol is longer than
  196. * table_bits, so the symbol does not have an entry
  197. * directly in the first (1 << table_bits) entries of the
  198. * decode table. Traverse the appropriate binary tree
  199. * bit-by-bit to decode the symbol.
  200. */
  201. bitstream_remove_bits(istream, table_bits);
  202. do {
  203. key_bits = (entry & 0x3FFF) + bitstream_pop_bits(istream, 1);
  204. } while ((entry = decode_table[key_bits]) >= 0xC000);
  205. return entry;
  206. }
  207. /*
  208. * Copy an LZ77 match at (dst - offset) to dst.
  209. *
  210. * The length and offset must be already validated --- that is, (dst - offset)
  211. * can't underrun the output buffer, and (dst + length) can't overrun the output
  212. * buffer. Also, the length cannot be 0.
  213. *
  214. * @bufend points to the byte past the end of the output buffer. This function
  215. * won't write any data beyond this position.
  216. *
  217. * Returns dst + length.
  218. */
  219. static forceinline u8 *lz_copy(u8 *dst, u32 length, u32 offset, const u8 *bufend,
  220. u32 min_length)
  221. {
  222. const u8 *src = dst - offset;
  223. /*
  224. * Try to copy one machine word at a time. On i386 and x86_64 this is
  225. * faster than copying one byte at a time, unless the data is
  226. * near-random and all the matches have very short lengths. Note that
  227. * since this requires unaligned memory accesses, it won't necessarily
  228. * be faster on every architecture.
  229. *
  230. * Also note that we might copy more than the length of the match. For
  231. * example, if a word is 8 bytes and the match is of length 5, then
  232. * we'll simply copy 8 bytes. This is okay as long as we don't write
  233. * beyond the end of the output buffer, hence the check for (bufend -
  234. * end >= WORDBYTES - 1).
  235. */
  236. #ifdef FAST_UNALIGNED_ACCESS
  237. u8 * const end = dst + length;
  238. if (bufend - end >= (ptrdiff_t)(WORDBYTES - 1)) {
  239. if (offset >= WORDBYTES) {
  240. /* The source and destination words don't overlap. */
  241. /* To improve branch prediction, one iteration of this
  242. * loop is unrolled. Most matches are short and will
  243. * fail the first check. But if that check passes, then
  244. * it becomes increasing likely that the match is long
  245. * and we'll need to continue copying.
  246. */
  247. copy_unaligned_word(src, dst);
  248. src += WORDBYTES;
  249. dst += WORDBYTES;
  250. if (dst < end) {
  251. do {
  252. copy_unaligned_word(src, dst);
  253. src += WORDBYTES;
  254. dst += WORDBYTES;
  255. } while (dst < end);
  256. }
  257. return end;
  258. } else if (offset == 1) {
  259. /* Offset 1 matches are equivalent to run-length
  260. * encoding of the previous byte. This case is common
  261. * if the data contains many repeated bytes.
  262. */
  263. size_t v = repeat_byte(*(dst - 1));
  264. do {
  265. put_unaligned(v, (size_t *)dst);
  266. src += WORDBYTES;
  267. dst += WORDBYTES;
  268. } while (dst < end);
  269. return end;
  270. }
  271. /*
  272. * We don't bother with special cases for other 'offset <
  273. * WORDBYTES', which are usually rarer than 'offset == 1'. Extra
  274. * checks will just slow things down. Actually, it's possible
  275. * to handle all the 'offset < WORDBYTES' cases using the same
  276. * code, but it still becomes more complicated doesn't seem any
  277. * faster overall; it definitely slows down the more common
  278. * 'offset == 1' case.
  279. */
  280. }
  281. #endif /* FAST_UNALIGNED_ACCESS */
  282. /* Fall back to a bytewise copy. */
  283. if (min_length >= 2) {
  284. *dst++ = *src++;
  285. length--;
  286. }
  287. if (min_length >= 3) {
  288. *dst++ = *src++;
  289. length--;
  290. }
  291. do {
  292. *dst++ = *src++;
  293. } while (--length);
  294. return dst;
  295. }
  296. #endif /* _LINUX_NTFS3_LIB_DECOMPRESS_COMMON_H */