ObjFW
Loading...
Searching...
No Matches
OFHuffmanTree.h
1/*
2 * Copyright (c) 2008-2024 Jonathan Schleifer <js@nil.im>
3 *
4 * All rights reserved.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License version 3.0 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * version 3.0 for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * version 3.0 along with this program. If not, see
17 * <https://www.gnu.org/licenses/>.
18 */
19
20#include <stdbool.h>
21#include <stdint.h>
22
23#import "macros.h"
24
25#import "OFInvalidFormatException.h"
26
27OF_ASSUME_NONNULL_BEGIN
28
29typedef struct _OFHuffmanTree {
30 struct _OFHuffmanTree *_Nullable leaves[2];
31 uint16_t value;
32} *OFHuffmanTree;
33
34/* Inlined for performance. */
35static OF_INLINE bool
36OFHuffmanTreeWalk(id _Nullable stream,
37 bool (*bitReader)(id _Nullable, uint16_t *_Nonnull, uint8_t),
38 OFHuffmanTree _Nonnull *_Nonnull tree, uint16_t *_Nonnull value)
39{
40 OFHuffmanTree iter = *tree;
41 uint16_t bits;
42
43 while (iter->value == 0xFFFF) {
44 if OF_UNLIKELY (!bitReader(stream, &bits, 1)) {
45 *tree = iter;
46 return false;
47 }
48
49 if OF_UNLIKELY (iter->leaves[bits] == NULL)
50 @throw [OFInvalidFormatException exception];
51
52 iter = iter->leaves[bits];
53 }
54
55 *value = iter->value;
56 return true;
57}
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62extern OFHuffmanTree _Nonnull OFHuffmanTreeNew(uint8_t lengths[_Nonnull],
63 uint16_t count);
64extern OFHuffmanTree _Nonnull OFHuffmanTreeNewSingle(uint16_t value);
65extern void OFHuffmanTreeFree(OFHuffmanTree _Nonnull tree);
66#ifdef __cplusplus
67}
68#endif
69
70OF_ASSUME_NONNULL_END
An exception indicating that the format is invalid.
Definition OFInvalidFormatException.h:31