ObjFW  Diff

Differences From Artifact [c8d56de598]:

To Artifact [f7a4f8d9de]:


14
15
16
17
18
19
20
21

22
23
24
25
26

27
28
29

30
31

32
33
34
35
36
37
38
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


59
60

61
62
63
64
65
66
67
68
69
70

71
72

73
74
75
76
77
78
79
14
15
16
17
18
19
20

21
22
23
24
25

26
27
28

29
30

31
32
33
34
35
36
37
38

39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


56
57
58

59
60
61
62
63
64
65
66
67
68

69
70

71
72
73
74
75
76
77
78







-
+




-
+


-
+

-
+







-
+
-
















-
-
+
+

-
+









-
+

-
+







 */

#include "config.h"

#include <stdint.h>
#include <stdlib.h>

#import "huffman_tree.h"
#import "OFHuffmanTree.h"

#import "OFInvalidFormatException.h"
#import "OFOutOfMemoryException.h"

static struct of_huffman_tree *
static OFHuffmanTree
newTree(void)
{
	struct of_huffman_tree *tree;
	OFHuffmanTree tree;

	tree = of_alloc(1, sizeof(*tree));
	tree = OFAllocMemory(1, sizeof(*tree));
	tree->leaves[0] = tree->leaves[1] = NULL;
	tree->value = 0xFFFF;

	return tree;
}

static void
insertTree(struct of_huffman_tree *tree, uint16_t code, uint8_t length,
treeInsert(OFHuffmanTree tree, uint16_t code, uint8_t length, uint16_t value)
    uint16_t value)
{
	while (length > 0) {
		uint8_t bit;

		length--;
		bit = (code & (1u << length)) >> length;

		if (tree->leaves[bit] == NULL)
			tree->leaves[bit] = newTree();

		tree = tree->leaves[bit];
	}

	tree->value = value;
}

struct of_huffman_tree *
of_huffman_tree_construct(uint8_t lengths[], uint16_t count)
OFHuffmanTree
OFHuffmanTreeNew(uint8_t lengths[], uint16_t count)
{
	struct of_huffman_tree *tree;
	OFHuffmanTree tree;
	uint16_t *lengthCount = NULL;
	uint16_t code, maxCode = 0, *nextCode = NULL;
	uint_fast8_t maxBit = 0;

	@try {
		for (uint16_t i = 0; i < count; i++) {
			uint_fast8_t length = lengths[i];

			if OF_UNLIKELY (length > maxBit) {
				lengthCount = of_realloc(lengthCount,
				lengthCount = OFResizeMemory(lengthCount,
				    length + 1, sizeof(uint16_t));
				nextCode = of_realloc(nextCode,
				nextCode = OFResizeMemory(nextCode,
				    length + 1, sizeof(uint16_t));

				for (uint_fast8_t j = maxBit + 1; j <= length;
				    j++) {
					lengthCount[j] = 0;
					nextCode[j] = 0;
				}
95
96
97
98
99
100
101
102

103
104
105
106


107
108
109
110
111
112
113


114
115

116
117
118
119
120
121
122
123

124
125
126
127

128
129

130
94
95
96
97
98
99
100

101
102
103


104
105
106
107
108
109
110


111
112
113

114
115
116
117
118
119
120
121

122
123
124
125

126
127

128
129







-
+


-
-
+
+





-
-
+
+

-
+







-
+



-
+

-
+


		tree = newTree();

		for (uint16_t i = 0; i <= maxCode; i++) {
			uint8_t length = lengths[i];

			if (length > 0)
				insertTree(tree, nextCode[length]++, length, i);
				treeInsert(tree, nextCode[length]++, length, i);
		}
	} @finally {
		free(lengthCount);
		free(nextCode);
		OFFreeMemory(lengthCount);
		OFFreeMemory(nextCode);
	}

	return tree;
}

struct of_huffman_tree *
of_huffman_tree_construct_single(uint16_t value)
OFHuffmanTree
OFHuffmanTreeNewSingle(uint16_t value)
{
	struct of_huffman_tree *tree = newTree();
	OFHuffmanTree tree = newTree();

	tree->value = value;

	return tree;
}

void
of_huffman_tree_release(struct of_huffman_tree *tree)
OFHuffmanTreeFree(OFHuffmanTree tree)
{
	for (uint_fast8_t i = 0; i < 2; i++)
		if OF_LIKELY (tree->leaves[i] != NULL)
			of_huffman_tree_release(tree->leaves[i]);
			OFHuffmanTreeFree(tree->leaves[i]);

	free(tree);
	OFFreeMemory(tree);
}