]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/huffman.h
5 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 /** Maximum length of a Huffman symbol (in bits) */
33 #define HUFFMAN_BITS 16
35 /** Raw huffman symbol */
36 typedef uint16_t huffman_raw_symbol_t
;
38 /** Quick lookup length for a Huffman symbol (in bits)
40 * This is a policy decision.
42 #define HUFFMAN_QL_BITS 7
44 /** Quick lookup shift */
45 #define HUFFMAN_QL_SHIFT ( HUFFMAN_BITS - HUFFMAN_QL_BITS )
47 /** A Huffman-coded set of symbols of a given length */
48 struct huffman_symbols
{
49 /** Length of Huffman-coded symbols (in bits) */
51 /** Shift to normalise symbols of this length to HUFFMAN_BITS bits */
53 /** Number of Huffman-coded symbols having this length */
55 /** First symbol of this length (normalised to HUFFMAN_BITS bits)
57 * Stored as a 32-bit value to allow the value
58 * (1<<HUFFMAN_BITS ) to be used for empty sets of symbols
59 * longer than the maximum utilised length.
62 /** Raw symbols having this length */
63 huffman_raw_symbol_t
*raw
;
66 /** A Huffman-coded alphabet */
67 struct huffman_alphabet
{
68 /** Huffman-coded symbol set for each length */
69 struct huffman_symbols huf
[HUFFMAN_BITS
];
70 /** Quick lookup table */
71 uint8_t lookup
[ 1 << HUFFMAN_QL_BITS
];
74 * Ordered by Huffman-coded symbol length, then by symbol
75 * value. This field has a variable length.
77 huffman_raw_symbol_t raw
[0];
81 * Get Huffman symbol length
83 * @v sym Huffman symbol set
84 * @ret len Length (in bits)
86 static inline __attribute__ (( always_inline
)) unsigned int
87 huffman_len ( struct huffman_symbols
*sym
) {
93 * Get Huffman symbol value
95 * @v sym Huffman symbol set
96 * @v huf Raw input value (normalised to HUFFMAN_BITS bits)
97 * @ret raw Raw symbol value
99 static inline __attribute__ (( always_inline
)) huffman_raw_symbol_t
100 huffman_raw ( struct huffman_symbols
*sym
, unsigned int huf
) {
102 return sym
->raw
[ huf
>> sym
->shift
];
105 extern int huffman_alphabet ( struct huffman_alphabet
*alphabet
,
106 uint8_t *lengths
, unsigned int count
);
107 extern struct huffman_symbols
*
108 huffman_sym ( struct huffman_alphabet
*alphabet
, unsigned int huf
);
110 #endif /* _HUFFMAN_H */