]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/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
30 /** Maximum length of a Huffman symbol (in bits) */
31 #define HUFFMAN_BITS 16
33 /** Raw huffman symbol */
34 typedef uint16_t huffman_raw_symbol_t
;
36 /** Quick lookup length for a Huffman symbol (in bits)
38 * This is a policy decision.
40 #define HUFFMAN_QL_BITS 7
42 /** Quick lookup shift */
43 #define HUFFMAN_QL_SHIFT ( HUFFMAN_BITS - HUFFMAN_QL_BITS )
45 /** A Huffman-coded set of symbols of a given length */
46 struct huffman_symbols
{
47 /** Length of Huffman-coded symbols (in bits) */
49 /** Shift to normalise symbols of this length to HUFFMAN_BITS bits */
51 /** Number of Huffman-coded symbols having this length */
53 /** First symbol of this length (normalised to HUFFMAN_BITS bits)
55 * Stored as a 32-bit value to allow the value
56 * (1<<HUFFMAN_BITS ) to be used for empty sets of symbols
57 * longer than the maximum utilised length.
60 /** Raw symbols having this length */
61 huffman_raw_symbol_t
*raw
;
64 /** A Huffman-coded alphabet */
65 struct huffman_alphabet
{
66 /** Huffman-coded symbol set for each length */
67 struct huffman_symbols huf
[HUFFMAN_BITS
];
68 /** Quick lookup table */
69 uint8_t lookup
[ 1 << HUFFMAN_QL_BITS
];
72 * Ordered by Huffman-coded symbol length, then by symbol
73 * value. This field has a variable length.
75 huffman_raw_symbol_t raw
[0];
79 * Get Huffman symbol length
81 * @v sym Huffman symbol set
82 * @ret len Length (in bits)
84 static inline __attribute__ (( always_inline
)) unsigned int
85 huffman_len ( struct huffman_symbols
*sym
) {
91 * Get Huffman symbol value
93 * @v sym Huffman symbol set
94 * @v huf Raw input value (normalised to HUFFMAN_BITS bits)
95 * @ret raw Raw symbol value
97 static inline __attribute__ (( always_inline
)) huffman_raw_symbol_t
98 huffman_raw ( struct huffman_symbols
*sym
, unsigned int huf
) {
100 return sym
->raw
[ huf
>> sym
->shift
];
103 extern int huffman_alphabet ( struct huffman_alphabet
*alphabet
,
104 uint8_t *lengths
, unsigned int count
);
105 extern struct huffman_symbols
*
106 huffman_sym ( struct huffman_alphabet
*alphabet
, unsigned int huf
);
108 #endif /* _HUFFMAN_H */