1*f39cd3f7SHyunchul Lee /* SPDX-License-Identifier: MIT */ 2*f39cd3f7SHyunchul Lee /* 3*f39cd3f7SHyunchul Lee * decompress_common.h - Code shared by the XPRESS and LZX decompressors 4*f39cd3f7SHyunchul Lee * 5*f39cd3f7SHyunchul Lee * This is a port of the upstream wimlib "decompress_common.h" which uses a 6*f39cd3f7SHyunchul Lee * subtable-based Huffman decode table format, as opposed to the older 7*f39cd3f7SHyunchul Lee * binary-tree-based format previously used in this library. 8*f39cd3f7SHyunchul Lee * 9*f39cd3f7SHyunchul Lee * Copyright (C) 2022 Eric Biggers 10*f39cd3f7SHyunchul Lee */ 11*f39cd3f7SHyunchul Lee 12*f39cd3f7SHyunchul Lee #ifndef _LINUX_NTFS_LIB_DECOMPRESS_COMMON_H 13*f39cd3f7SHyunchul Lee #define _LINUX_NTFS_LIB_DECOMPRESS_COMMON_H 14*f39cd3f7SHyunchul Lee 15*f39cd3f7SHyunchul Lee #include <linux/compiler.h> 16*f39cd3f7SHyunchul Lee #include <linux/string.h> 17*f39cd3f7SHyunchul Lee #include <linux/types.h> 18*f39cd3f7SHyunchul Lee #include <linux/slab.h> 19*f39cd3f7SHyunchul Lee #include <linux/unaligned.h> 20*f39cd3f7SHyunchul Lee 21*f39cd3f7SHyunchul Lee /* "Force inline" macro (not required, but helpful for performance). */ 22*f39cd3f7SHyunchul Lee #define forceinline __always_inline 23*f39cd3f7SHyunchul Lee 24*f39cd3f7SHyunchul Lee /* Size of a machine word. */ 25*f39cd3f7SHyunchul Lee #define WORDBYTES sizeof(size_t) 26*f39cd3f7SHyunchul Lee #define WORDBITS (8 * WORDBYTES) 27*f39cd3f7SHyunchul Lee 28*f39cd3f7SHyunchul Lee /* UNALIGNED_ACCESS_IS_FAST should be 1 if unaligned memory accesses can be 29*f39cd3f7SHyunchul Lee * performed efficiently on the target platform. 30*f39cd3f7SHyunchul Lee */ 31*f39cd3f7SHyunchul Lee #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 32*f39cd3f7SHyunchul Lee # define UNALIGNED_ACCESS_IS_FAST 1 33*f39cd3f7SHyunchul Lee #else 34*f39cd3f7SHyunchul Lee # define UNALIGNED_ACCESS_IS_FAST 0 35*f39cd3f7SHyunchul Lee #endif 36*f39cd3f7SHyunchul Lee 37*f39cd3f7SHyunchul Lee /* Deprecated name kept for compatibility with the upstream source. */ 38*f39cd3f7SHyunchul Lee #define FAST_UNALIGNED_ACCESS UNALIGNED_ACCESS_IS_FAST 39*f39cd3f7SHyunchul Lee 40*f39cd3f7SHyunchul Lee /* likely()/unlikely() are provided by <linux/compiler.h>. */ 41*f39cd3f7SHyunchul Lee 42*f39cd3f7SHyunchul Lee /* STATIC_ASSERT() - verify the truth of an expression at compile time. */ 43*f39cd3f7SHyunchul Lee #define STATIC_ASSERT(expr) ((void)sizeof(char[1 - 2 * !(expr)])) 44*f39cd3f7SHyunchul Lee 45*f39cd3f7SHyunchul Lee /* STATIC_ASSERT_ZERO() - like STATIC_ASSERT() but evaluates to 0 so it can be 46*f39cd3f7SHyunchul Lee * used in constant expressions. 47*f39cd3f7SHyunchul Lee */ 48*f39cd3f7SHyunchul Lee #define STATIC_ASSERT_ZERO(expr) ((int)sizeof(char[-!(expr)])) 49*f39cd3f7SHyunchul Lee 50*f39cd3f7SHyunchul Lee /* Unaligned word load/store helpers. */ 51*f39cd3f7SHyunchul Lee static forceinline size_t load_word_unaligned(const void *p) 52*f39cd3f7SHyunchul Lee { 53*f39cd3f7SHyunchul Lee size_t v; 54*f39cd3f7SHyunchul Lee 55*f39cd3f7SHyunchul Lee memcpy(&v, p, sizeof(v)); 56*f39cd3f7SHyunchul Lee return v; 57*f39cd3f7SHyunchul Lee } 58*f39cd3f7SHyunchul Lee 59*f39cd3f7SHyunchul Lee static forceinline void store_word_unaligned(size_t v, void *p) 60*f39cd3f7SHyunchul Lee { 61*f39cd3f7SHyunchul Lee memcpy(p, &v, sizeof(v)); 62*f39cd3f7SHyunchul Lee } 63*f39cd3f7SHyunchul Lee 64*f39cd3f7SHyunchul Lee static forceinline void copy_word_unaligned(const void *src, void *dst) 65*f39cd3f7SHyunchul Lee { 66*f39cd3f7SHyunchul Lee store_word_unaligned(load_word_unaligned(src), dst); 67*f39cd3f7SHyunchul Lee } 68*f39cd3f7SHyunchul Lee 69*f39cd3f7SHyunchul Lee static forceinline size_t repeat_u16(u16 b) 70*f39cd3f7SHyunchul Lee { 71*f39cd3f7SHyunchul Lee size_t v = b; 72*f39cd3f7SHyunchul Lee 73*f39cd3f7SHyunchul Lee STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64); 74*f39cd3f7SHyunchul Lee v |= v << 16; 75*f39cd3f7SHyunchul Lee v |= v << ((WORDBITS == 64) ? 32 : 0); 76*f39cd3f7SHyunchul Lee return v; 77*f39cd3f7SHyunchul Lee } 78*f39cd3f7SHyunchul Lee 79*f39cd3f7SHyunchul Lee static forceinline size_t repeat_byte(u8 b) 80*f39cd3f7SHyunchul Lee { 81*f39cd3f7SHyunchul Lee return repeat_u16(((u16)b << 8) | b); 82*f39cd3f7SHyunchul Lee } 83*f39cd3f7SHyunchul Lee 84*f39cd3f7SHyunchul Lee /******************************************************************************/ 85*f39cd3f7SHyunchul Lee /* Input bitstream for XPRESS and LZX */ 86*f39cd3f7SHyunchul Lee /*----------------------------------------------------------------------------*/ 87*f39cd3f7SHyunchul Lee 88*f39cd3f7SHyunchul Lee /* Structure that encapsulates a block of in-memory data being interpreted as a 89*f39cd3f7SHyunchul Lee * stream of bits, optionally with interwoven literal bytes. Bits are assumed 90*f39cd3f7SHyunchul Lee * to be stored in little endian 16-bit coding units, with the bits ordered high 91*f39cd3f7SHyunchul Lee * to low. 92*f39cd3f7SHyunchul Lee */ 93*f39cd3f7SHyunchul Lee struct input_bitstream { 94*f39cd3f7SHyunchul Lee /* Bits that have been read from the input buffer. The bits are 95*f39cd3f7SHyunchul Lee * left-justified; the next bit is always bit 31. 96*f39cd3f7SHyunchul Lee */ 97*f39cd3f7SHyunchul Lee u32 bitbuf; 98*f39cd3f7SHyunchul Lee 99*f39cd3f7SHyunchul Lee /* Number of bits currently held in @bitbuf. */ 100*f39cd3f7SHyunchul Lee u32 bitsleft; 101*f39cd3f7SHyunchul Lee 102*f39cd3f7SHyunchul Lee /* Pointer to the next byte to be retrieved from the input buffer. */ 103*f39cd3f7SHyunchul Lee const u8 *next; 104*f39cd3f7SHyunchul Lee 105*f39cd3f7SHyunchul Lee /* Pointer past the end of the input buffer. */ 106*f39cd3f7SHyunchul Lee const u8 *end; 107*f39cd3f7SHyunchul Lee }; 108*f39cd3f7SHyunchul Lee 109*f39cd3f7SHyunchul Lee /* Initialize a bitstream to read from the specified input buffer. */ 110*f39cd3f7SHyunchul Lee static forceinline void init_input_bitstream(struct input_bitstream *is, 111*f39cd3f7SHyunchul Lee const void *buffer, u32 size) 112*f39cd3f7SHyunchul Lee { 113*f39cd3f7SHyunchul Lee is->bitbuf = 0; 114*f39cd3f7SHyunchul Lee is->bitsleft = 0; 115*f39cd3f7SHyunchul Lee is->next = buffer; 116*f39cd3f7SHyunchul Lee is->end = is->next + size; 117*f39cd3f7SHyunchul Lee } 118*f39cd3f7SHyunchul Lee 119*f39cd3f7SHyunchul Lee /* Note: for performance reasons, the following methods don't return error 120*f39cd3f7SHyunchul Lee * codes to the caller if the input buffer is overrun. Instead, they just 121*f39cd3f7SHyunchul Lee * assume that all overrun data is zeroes. 122*f39cd3f7SHyunchul Lee */ 123*f39cd3f7SHyunchul Lee 124*f39cd3f7SHyunchul Lee /* Ensure the bit buffer variable for the bitstream contains at least @num_bits 125*f39cd3f7SHyunchul Lee * bits. Following this, bitstream_peek_bits() and/or bitstream_remove_bits() 126*f39cd3f7SHyunchul Lee * may be called on the bitstream to peek or remove up to @num_bits bits. This 127*f39cd3f7SHyunchul Lee * works for at most 16 bits, which is sufficient for LZX (max codeword length 128*f39cd3f7SHyunchul Lee * 16) and XPRESS (max codeword length 15). 129*f39cd3f7SHyunchul Lee */ 130*f39cd3f7SHyunchul Lee static forceinline void bitstream_ensure_bits(struct input_bitstream *is, 131*f39cd3f7SHyunchul Lee unsigned int num_bits) 132*f39cd3f7SHyunchul Lee { 133*f39cd3f7SHyunchul Lee if (is->bitsleft >= num_bits) 134*f39cd3f7SHyunchul Lee return; 135*f39cd3f7SHyunchul Lee 136*f39cd3f7SHyunchul Lee if (unlikely(is->end - is->next < 2)) 137*f39cd3f7SHyunchul Lee goto overflow; 138*f39cd3f7SHyunchul Lee 139*f39cd3f7SHyunchul Lee is->bitbuf |= (u32)get_unaligned_le16(is->next) << (16 - is->bitsleft); 140*f39cd3f7SHyunchul Lee is->next += 2; 141*f39cd3f7SHyunchul Lee is->bitsleft += 16; 142*f39cd3f7SHyunchul Lee return; 143*f39cd3f7SHyunchul Lee 144*f39cd3f7SHyunchul Lee overflow: 145*f39cd3f7SHyunchul Lee is->bitsleft = 32; 146*f39cd3f7SHyunchul Lee } 147*f39cd3f7SHyunchul Lee 148*f39cd3f7SHyunchul Lee /* Return the next @num_bits bits from the bitstream, without removing them. 149*f39cd3f7SHyunchul Lee * There must be at least @num_bits remaining in the buffer variable. 150*f39cd3f7SHyunchul Lee */ 151*f39cd3f7SHyunchul Lee static forceinline u32 bitstream_peek_bits(const struct input_bitstream *is, 152*f39cd3f7SHyunchul Lee unsigned int num_bits) 153*f39cd3f7SHyunchul Lee { 154*f39cd3f7SHyunchul Lee return (is->bitbuf >> 1) >> (sizeof(is->bitbuf) * 8 - num_bits - 1); 155*f39cd3f7SHyunchul Lee } 156*f39cd3f7SHyunchul Lee 157*f39cd3f7SHyunchul Lee /* Remove @num_bits from the bitstream. */ 158*f39cd3f7SHyunchul Lee static forceinline void bitstream_remove_bits(struct input_bitstream *is, 159*f39cd3f7SHyunchul Lee unsigned int num_bits) 160*f39cd3f7SHyunchul Lee { 161*f39cd3f7SHyunchul Lee is->bitbuf <<= num_bits; 162*f39cd3f7SHyunchul Lee is->bitsleft -= num_bits; 163*f39cd3f7SHyunchul Lee } 164*f39cd3f7SHyunchul Lee 165*f39cd3f7SHyunchul Lee /* Remove and return @num_bits bits from the bitstream. */ 166*f39cd3f7SHyunchul Lee static forceinline u32 bitstream_pop_bits(struct input_bitstream *is, 167*f39cd3f7SHyunchul Lee unsigned int num_bits) 168*f39cd3f7SHyunchul Lee { 169*f39cd3f7SHyunchul Lee u32 bits = bitstream_peek_bits(is, num_bits); 170*f39cd3f7SHyunchul Lee 171*f39cd3f7SHyunchul Lee bitstream_remove_bits(is, num_bits); 172*f39cd3f7SHyunchul Lee return bits; 173*f39cd3f7SHyunchul Lee } 174*f39cd3f7SHyunchul Lee 175*f39cd3f7SHyunchul Lee /* Read and return the next @num_bits bits from the bitstream. */ 176*f39cd3f7SHyunchul Lee static forceinline u32 bitstream_read_bits(struct input_bitstream *is, 177*f39cd3f7SHyunchul Lee unsigned int num_bits) 178*f39cd3f7SHyunchul Lee { 179*f39cd3f7SHyunchul Lee bitstream_ensure_bits(is, num_bits); 180*f39cd3f7SHyunchul Lee return bitstream_pop_bits(is, num_bits); 181*f39cd3f7SHyunchul Lee } 182*f39cd3f7SHyunchul Lee 183*f39cd3f7SHyunchul Lee /* Read and return the next literal byte embedded in the bitstream. */ 184*f39cd3f7SHyunchul Lee static forceinline u8 bitstream_read_byte(struct input_bitstream *is) 185*f39cd3f7SHyunchul Lee { 186*f39cd3f7SHyunchul Lee if (unlikely(is->end == is->next)) 187*f39cd3f7SHyunchul Lee return 0; 188*f39cd3f7SHyunchul Lee return *is->next++; 189*f39cd3f7SHyunchul Lee } 190*f39cd3f7SHyunchul Lee 191*f39cd3f7SHyunchul Lee /* Read and return the next 16-bit integer embedded in the bitstream. */ 192*f39cd3f7SHyunchul Lee static forceinline u16 bitstream_read_u16(struct input_bitstream *is) 193*f39cd3f7SHyunchul Lee { 194*f39cd3f7SHyunchul Lee u16 v; 195*f39cd3f7SHyunchul Lee 196*f39cd3f7SHyunchul Lee if (unlikely(is->end - is->next < 2)) 197*f39cd3f7SHyunchul Lee return 0; 198*f39cd3f7SHyunchul Lee v = get_unaligned_le16(is->next); 199*f39cd3f7SHyunchul Lee is->next += 2; 200*f39cd3f7SHyunchul Lee return v; 201*f39cd3f7SHyunchul Lee } 202*f39cd3f7SHyunchul Lee 203*f39cd3f7SHyunchul Lee /* Read and return the next 32-bit integer embedded in the bitstream. */ 204*f39cd3f7SHyunchul Lee static forceinline u32 bitstream_read_u32(struct input_bitstream *is) 205*f39cd3f7SHyunchul Lee { 206*f39cd3f7SHyunchul Lee u32 v; 207*f39cd3f7SHyunchul Lee 208*f39cd3f7SHyunchul Lee if (unlikely(is->end - is->next < 4)) 209*f39cd3f7SHyunchul Lee return 0; 210*f39cd3f7SHyunchul Lee v = get_unaligned_le32(is->next); 211*f39cd3f7SHyunchul Lee is->next += 4; 212*f39cd3f7SHyunchul Lee return v; 213*f39cd3f7SHyunchul Lee } 214*f39cd3f7SHyunchul Lee 215*f39cd3f7SHyunchul Lee /* Read into @dst_buffer an array of literal bytes embedded in the bitstream. 216*f39cd3f7SHyunchul Lee * Return 0 if there were enough bytes remaining in the input, otherwise -1. 217*f39cd3f7SHyunchul Lee */ 218*f39cd3f7SHyunchul Lee static forceinline int bitstream_read_bytes(struct input_bitstream *is, 219*f39cd3f7SHyunchul Lee void *dst_buffer, size_t count) 220*f39cd3f7SHyunchul Lee { 221*f39cd3f7SHyunchul Lee if (unlikely((size_t)(is->end - is->next) < count)) 222*f39cd3f7SHyunchul Lee return -1; 223*f39cd3f7SHyunchul Lee memcpy(dst_buffer, is->next, count); 224*f39cd3f7SHyunchul Lee is->next += count; 225*f39cd3f7SHyunchul Lee return 0; 226*f39cd3f7SHyunchul Lee } 227*f39cd3f7SHyunchul Lee 228*f39cd3f7SHyunchul Lee /* Align the input bitstream on a coding-unit boundary. */ 229*f39cd3f7SHyunchul Lee static forceinline void bitstream_align(struct input_bitstream *is) 230*f39cd3f7SHyunchul Lee { 231*f39cd3f7SHyunchul Lee is->bitsleft = 0; 232*f39cd3f7SHyunchul Lee is->bitbuf = 0; 233*f39cd3f7SHyunchul Lee } 234*f39cd3f7SHyunchul Lee 235*f39cd3f7SHyunchul Lee /******************************************************************************/ 236*f39cd3f7SHyunchul Lee /* Huffman decoding */ 237*f39cd3f7SHyunchul Lee /*----------------------------------------------------------------------------*/ 238*f39cd3f7SHyunchul Lee 239*f39cd3f7SHyunchul Lee /* 240*f39cd3f7SHyunchul Lee * Required alignment for the Huffman decode tables. We require this alignment 241*f39cd3f7SHyunchul Lee * so that we can fill the entries with word instructions without having to deal 242*f39cd3f7SHyunchul Lee * with misaligned buffers. 243*f39cd3f7SHyunchul Lee */ 244*f39cd3f7SHyunchul Lee #define DECODE_TABLE_ALIGNMENT 16 245*f39cd3f7SHyunchul Lee 246*f39cd3f7SHyunchul Lee /* 247*f39cd3f7SHyunchul Lee * Each decode table entry is 16 bits divided into two fields: 'symbol' (high 12 248*f39cd3f7SHyunchul Lee * bits) and 'length' (low 4 bits). See the comments in decompress_common.c for 249*f39cd3f7SHyunchul Lee * the precise meaning of these fields depending on the entry type. 250*f39cd3f7SHyunchul Lee */ 251*f39cd3f7SHyunchul Lee #define DECODE_TABLE_SYMBOL_SHIFT 4 252*f39cd3f7SHyunchul Lee #define DECODE_TABLE_MAX_SYMBOL ((1 << (16 - DECODE_TABLE_SYMBOL_SHIFT)) - 1) 253*f39cd3f7SHyunchul Lee #define DECODE_TABLE_MAX_LENGTH ((1 << DECODE_TABLE_SYMBOL_SHIFT) - 1) 254*f39cd3f7SHyunchul Lee #define DECODE_TABLE_LENGTH_MASK DECODE_TABLE_MAX_LENGTH 255*f39cd3f7SHyunchul Lee #define MAKE_DECODE_TABLE_ENTRY(symbol, length) \ 256*f39cd3f7SHyunchul Lee (((symbol) << DECODE_TABLE_SYMBOL_SHIFT) | (length)) 257*f39cd3f7SHyunchul Lee 258*f39cd3f7SHyunchul Lee /* 259*f39cd3f7SHyunchul Lee * Read and return the next Huffman-encoded symbol from the given bitstream 260*f39cd3f7SHyunchul Lee * using the given decode table. If the input data is exhausted, then the 261*f39cd3f7SHyunchul Lee * Huffman symbol will be decoded as if the missing bits were all zeroes. 262*f39cd3f7SHyunchul Lee */ 263*f39cd3f7SHyunchul Lee static forceinline unsigned int read_huffsym(struct input_bitstream *is, 264*f39cd3f7SHyunchul Lee const u16 decode_table[], 265*f39cd3f7SHyunchul Lee unsigned int table_bits, 266*f39cd3f7SHyunchul Lee unsigned int max_codeword_len) 267*f39cd3f7SHyunchul Lee { 268*f39cd3f7SHyunchul Lee unsigned int entry; 269*f39cd3f7SHyunchul Lee unsigned int symbol; 270*f39cd3f7SHyunchul Lee unsigned int length; 271*f39cd3f7SHyunchul Lee 272*f39cd3f7SHyunchul Lee /* Preload the bitbuffer with 'max_codeword_len' bits. */ 273*f39cd3f7SHyunchul Lee bitstream_ensure_bits(is, max_codeword_len); 274*f39cd3f7SHyunchul Lee 275*f39cd3f7SHyunchul Lee /* Index the root table by the next 'table_bits' bits of input. */ 276*f39cd3f7SHyunchul Lee entry = decode_table[bitstream_peek_bits(is, table_bits)]; 277*f39cd3f7SHyunchul Lee 278*f39cd3f7SHyunchul Lee /* Extract the "symbol" and "length" from the entry. */ 279*f39cd3f7SHyunchul Lee symbol = entry >> DECODE_TABLE_SYMBOL_SHIFT; 280*f39cd3f7SHyunchul Lee length = entry & DECODE_TABLE_LENGTH_MASK; 281*f39cd3f7SHyunchul Lee 282*f39cd3f7SHyunchul Lee /* If the codeword is longer than 'table_bits', the root entry is a 283*f39cd3f7SHyunchul Lee * subtable pointer. Discard the bits used to index the root table and 284*f39cd3f7SHyunchul Lee * index the subtable by the next 'length' bits. 285*f39cd3f7SHyunchul Lee */ 286*f39cd3f7SHyunchul Lee if (max_codeword_len > table_bits && 287*f39cd3f7SHyunchul Lee entry >= (1U << (table_bits + DECODE_TABLE_SYMBOL_SHIFT))) { 288*f39cd3f7SHyunchul Lee bitstream_remove_bits(is, table_bits); 289*f39cd3f7SHyunchul Lee entry = decode_table[symbol + bitstream_peek_bits(is, length)]; 290*f39cd3f7SHyunchul Lee symbol = entry >> DECODE_TABLE_SYMBOL_SHIFT; 291*f39cd3f7SHyunchul Lee length = entry & DECODE_TABLE_LENGTH_MASK; 292*f39cd3f7SHyunchul Lee } 293*f39cd3f7SHyunchul Lee 294*f39cd3f7SHyunchul Lee /* Discard the (remaining) bits of the codeword. */ 295*f39cd3f7SHyunchul Lee bitstream_remove_bits(is, length); 296*f39cd3f7SHyunchul Lee 297*f39cd3f7SHyunchul Lee return symbol; 298*f39cd3f7SHyunchul Lee } 299*f39cd3f7SHyunchul Lee 300*f39cd3f7SHyunchul Lee /* 301*f39cd3f7SHyunchul Lee * DECODE_TABLE_ENOUGH() evaluates to the maximum number of decode table 302*f39cd3f7SHyunchul Lee * entries, including all subtable entries, that may be required for decoding a 303*f39cd3f7SHyunchul Lee * given Huffman code. It is a compile-time mapping computed by the zlib 304*f39cd3f7SHyunchul Lee * 'enough' utility. An unknown combination produces a build error. 305*f39cd3f7SHyunchul Lee */ 306*f39cd3f7SHyunchul Lee #define DECODE_TABLE_ENOUGH(num_syms, table_bits, max_codeword_len) ( \ 307*f39cd3f7SHyunchul Lee ((num_syms) == 8 && (table_bits) == 5 && (max_codeword_len) == 7) ? 36 : \ 308*f39cd3f7SHyunchul Lee ((num_syms) == 8 && (table_bits) == 6 && (max_codeword_len) == 7) ? 66 : \ 309*f39cd3f7SHyunchul Lee ((num_syms) == 8 && (table_bits) == 7 && (max_codeword_len) == 7) ? 128 : \ 310*f39cd3f7SHyunchul Lee ((num_syms) == 20 && (table_bits) == 5 && (max_codeword_len) == 15) ? 1062 : \ 311*f39cd3f7SHyunchul Lee ((num_syms) == 20 && (table_bits) == 6 && (max_codeword_len) == 15) ? 582 : \ 312*f39cd3f7SHyunchul Lee ((num_syms) == 20 && (table_bits) == 7 && (max_codeword_len) == 15) ? 390 : \ 313*f39cd3f7SHyunchul Lee ((num_syms) == 54 && (table_bits) == 9 && (max_codeword_len) == 15) ? 618 : \ 314*f39cd3f7SHyunchul Lee ((num_syms) == 54 && (table_bits) == 10 && (max_codeword_len) == 15) ? 1098 : \ 315*f39cd3f7SHyunchul Lee ((num_syms) == 249 && (table_bits) == 9 && (max_codeword_len) == 16) ? 878 : \ 316*f39cd3f7SHyunchul Lee ((num_syms) == 249 && (table_bits) == 10 && (max_codeword_len) == 16) ? 1326 : \ 317*f39cd3f7SHyunchul Lee ((num_syms) == 249 && (table_bits) == 11 && (max_codeword_len) == 16) ? 2318 : \ 318*f39cd3f7SHyunchul Lee ((num_syms) == 496 && (table_bits) == 11 && (max_codeword_len) == 16) ? 2566 : \ 319*f39cd3f7SHyunchul Lee ((num_syms) == 256 && (table_bits) == 9 && (max_codeword_len) == 15) ? 822 : \ 320*f39cd3f7SHyunchul Lee ((num_syms) == 256 && (table_bits) == 10 && (max_codeword_len) == 15) ? 1302 : \ 321*f39cd3f7SHyunchul Lee ((num_syms) == 256 && (table_bits) == 11 && (max_codeword_len) == 15) ? 2310 : \ 322*f39cd3f7SHyunchul Lee ((num_syms) == 512 && (table_bits) == 10 && (max_codeword_len) == 15) ? 1558 : \ 323*f39cd3f7SHyunchul Lee ((num_syms) == 512 && (table_bits) == 11 && (max_codeword_len) == 15) ? 2566 : \ 324*f39cd3f7SHyunchul Lee ((num_syms) == 512 && (table_bits) == 12 && (max_codeword_len) == 15) ? 4606 : \ 325*f39cd3f7SHyunchul Lee ((num_syms) == 656 && (table_bits) == 10 && (max_codeword_len) == 16) ? 1734 : \ 326*f39cd3f7SHyunchul Lee ((num_syms) == 656 && (table_bits) == 11 && (max_codeword_len) == 16) ? 2726 : \ 327*f39cd3f7SHyunchul Lee ((num_syms) == 656 && (table_bits) == 12 && (max_codeword_len) == 16) ? 4758 : \ 328*f39cd3f7SHyunchul Lee ((num_syms) == 799 && (table_bits) == 9 && (max_codeword_len) == 15) ? 1366 : \ 329*f39cd3f7SHyunchul Lee ((num_syms) == 799 && (table_bits) == 10 && (max_codeword_len) == 15) ? 1846 : \ 330*f39cd3f7SHyunchul Lee ((num_syms) == 799 && (table_bits) == 11 && (max_codeword_len) == 15) ? 2854 : \ 331*f39cd3f7SHyunchul Lee -1) 332*f39cd3f7SHyunchul Lee 333*f39cd3f7SHyunchul Lee /* Wrapper around DECODE_TABLE_ENOUGH() that does additional compile-time 334*f39cd3f7SHyunchul Lee * validation. 335*f39cd3f7SHyunchul Lee */ 336*f39cd3f7SHyunchul Lee #define DECODE_TABLE_SIZE(num_syms, table_bits, max_codeword_len) ( \ 337*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((num_syms) > 0) + \ 338*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((table_bits) > 0) + \ 339*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((max_codeword_len) > 0) + \ 340*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((num_syms) <= 1U << (max_codeword_len)) + \ 341*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((table_bits) <= (max_codeword_len)) + \ 342*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((num_syms) - 1 <= DECODE_TABLE_MAX_SYMBOL) + \ 343*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((table_bits) <= DECODE_TABLE_MAX_LENGTH) + \ 344*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((max_codeword_len) - (table_bits) <= \ 345*f39cd3f7SHyunchul Lee DECODE_TABLE_MAX_LENGTH) + \ 346*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO((1U << table_bits) > (num_syms) - 1) + \ 347*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO(DECODE_TABLE_ENOUGH( \ 348*f39cd3f7SHyunchul Lee (num_syms), (table_bits), \ 349*f39cd3f7SHyunchul Lee (max_codeword_len)) > 0) + \ 350*f39cd3f7SHyunchul Lee STATIC_ASSERT_ZERO(DECODE_TABLE_ENOUGH( \ 351*f39cd3f7SHyunchul Lee (num_syms), (table_bits), \ 352*f39cd3f7SHyunchul Lee (max_codeword_len)) - 1 <= \ 353*f39cd3f7SHyunchul Lee DECODE_TABLE_MAX_SYMBOL) + \ 354*f39cd3f7SHyunchul Lee DECODE_TABLE_ENOUGH((num_syms), (table_bits), \ 355*f39cd3f7SHyunchul Lee (max_codeword_len)) \ 356*f39cd3f7SHyunchul Lee ) 357*f39cd3f7SHyunchul Lee 358*f39cd3f7SHyunchul Lee /* Declare the decode table for a Huffman code. */ 359*f39cd3f7SHyunchul Lee #define DECODE_TABLE(name, num_syms, table_bits, max_codeword_len) \ 360*f39cd3f7SHyunchul Lee u16 name[DECODE_TABLE_SIZE((num_syms), (table_bits), \ 361*f39cd3f7SHyunchul Lee (max_codeword_len))] \ 362*f39cd3f7SHyunchul Lee __aligned(DECODE_TABLE_ALIGNMENT) 363*f39cd3f7SHyunchul Lee 364*f39cd3f7SHyunchul Lee /* Declare the temporary "working_space" array needed for building the decode 365*f39cd3f7SHyunchul Lee * table for a Huffman code. 366*f39cd3f7SHyunchul Lee */ 367*f39cd3f7SHyunchul Lee #define DECODE_TABLE_WORKING_SPACE(name, num_syms, max_codeword_len) \ 368*f39cd3f7SHyunchul Lee u16 name[2 * ((max_codeword_len) + 1) + (num_syms)] 369*f39cd3f7SHyunchul Lee 370*f39cd3f7SHyunchul Lee int make_huffman_decode_table(u16 decode_table[], u32 num_syms, 371*f39cd3f7SHyunchul Lee u32 table_bits, const u8 lens[], 372*f39cd3f7SHyunchul Lee u32 max_codeword_len, u16 working_space[], 373*f39cd3f7SHyunchul Lee u32 decode_table_size); 374*f39cd3f7SHyunchul Lee 375*f39cd3f7SHyunchul Lee /******************************************************************************/ 376*f39cd3f7SHyunchul Lee /* LZ match copying */ 377*f39cd3f7SHyunchul Lee /*----------------------------------------------------------------------------*/ 378*f39cd3f7SHyunchul Lee 379*f39cd3f7SHyunchul Lee /* 380*f39cd3f7SHyunchul Lee * Copy an LZ77 match of 'length' bytes from the match source at 'out_next - 381*f39cd3f7SHyunchul Lee * offset' to the match destination at 'out_next'. The source and destination 382*f39cd3f7SHyunchul Lee * may overlap. This handles validating the length and offset; it returns 0 if 383*f39cd3f7SHyunchul Lee * the match was valid (and was copied), otherwise -1. 384*f39cd3f7SHyunchul Lee */ 385*f39cd3f7SHyunchul Lee static forceinline int lz_copy(u32 length, u32 offset, u8 *out_begin, 386*f39cd3f7SHyunchul Lee u8 *out_next, u8 *out_end, u32 min_length) 387*f39cd3f7SHyunchul Lee { 388*f39cd3f7SHyunchul Lee const u8 *src; 389*f39cd3f7SHyunchul Lee u8 *end; 390*f39cd3f7SHyunchul Lee 391*f39cd3f7SHyunchul Lee /* Validate the offset. */ 392*f39cd3f7SHyunchul Lee if (unlikely(offset > (u32)(out_next - out_begin))) 393*f39cd3f7SHyunchul Lee return -1; 394*f39cd3f7SHyunchul Lee 395*f39cd3f7SHyunchul Lee src = out_next - offset; 396*f39cd3f7SHyunchul Lee 397*f39cd3f7SHyunchul Lee /* Fast path: copy a short, non-overlapping match whose end is not too 398*f39cd3f7SHyunchul Lee * close to the end of the buffer. 399*f39cd3f7SHyunchul Lee */ 400*f39cd3f7SHyunchul Lee if (UNALIGNED_ACCESS_IS_FAST && length <= 3 * WORDBYTES && 401*f39cd3f7SHyunchul Lee offset >= WORDBYTES && out_end - out_next >= 3 * WORDBYTES) { 402*f39cd3f7SHyunchul Lee copy_word_unaligned(src + WORDBYTES * 0, out_next + WORDBYTES * 0); 403*f39cd3f7SHyunchul Lee copy_word_unaligned(src + WORDBYTES * 1, out_next + WORDBYTES * 1); 404*f39cd3f7SHyunchul Lee copy_word_unaligned(src + WORDBYTES * 2, out_next + WORDBYTES * 2); 405*f39cd3f7SHyunchul Lee return 0; 406*f39cd3f7SHyunchul Lee } 407*f39cd3f7SHyunchul Lee 408*f39cd3f7SHyunchul Lee /* Validate the length. */ 409*f39cd3f7SHyunchul Lee if (unlikely(length > (u32)(out_end - out_next))) 410*f39cd3f7SHyunchul Lee return -1; 411*f39cd3f7SHyunchul Lee end = out_next + length; 412*f39cd3f7SHyunchul Lee 413*f39cd3f7SHyunchul Lee if (UNALIGNED_ACCESS_IS_FAST && likely(out_end - end >= WORDBYTES - 1)) { 414*f39cd3f7SHyunchul Lee if (offset >= WORDBYTES) { 415*f39cd3f7SHyunchul Lee do { 416*f39cd3f7SHyunchul Lee copy_word_unaligned(src, out_next); 417*f39cd3f7SHyunchul Lee src += WORDBYTES; 418*f39cd3f7SHyunchul Lee out_next += WORDBYTES; 419*f39cd3f7SHyunchul Lee } while (out_next < end); 420*f39cd3f7SHyunchul Lee return 0; 421*f39cd3f7SHyunchul Lee } else if (offset == 1) { 422*f39cd3f7SHyunchul Lee size_t v = repeat_byte(*(out_next - 1)); 423*f39cd3f7SHyunchul Lee 424*f39cd3f7SHyunchul Lee do { 425*f39cd3f7SHyunchul Lee store_word_unaligned(v, out_next); 426*f39cd3f7SHyunchul Lee src += WORDBYTES; 427*f39cd3f7SHyunchul Lee out_next += WORDBYTES; 428*f39cd3f7SHyunchul Lee } while (out_next < end); 429*f39cd3f7SHyunchul Lee return 0; 430*f39cd3f7SHyunchul Lee } 431*f39cd3f7SHyunchul Lee } 432*f39cd3f7SHyunchul Lee 433*f39cd3f7SHyunchul Lee /* Fall back to a bytewise copy. */ 434*f39cd3f7SHyunchul Lee if (min_length >= 2) 435*f39cd3f7SHyunchul Lee *out_next++ = *src++; 436*f39cd3f7SHyunchul Lee if (min_length >= 3) 437*f39cd3f7SHyunchul Lee *out_next++ = *src++; 438*f39cd3f7SHyunchul Lee do { 439*f39cd3f7SHyunchul Lee *out_next++ = *src++; 440*f39cd3f7SHyunchul Lee } while (out_next != end); 441*f39cd3f7SHyunchul Lee return 0; 442*f39cd3f7SHyunchul Lee } 443*f39cd3f7SHyunchul Lee 444*f39cd3f7SHyunchul Lee #endif /* _LINUX_NTFS_LIB_DECOMPRESS_COMMON_H */ 445