-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhuffman_decompressor_stub.cpp
92 lines (62 loc) · 1.88 KB
/
huffman_decompressor_stub.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
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
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "includes.hpp"
CODE_SEG(".stub_f") CHARS_HUFFMAN_TREE* __stdcall ReadTree_ForStub(STUB_FUNCTION_TABLE* f, BYTE** start_addr)
{
char node_type = **start_addr;
++(*start_addr);
if (node_type == '1')
{
char ch = **start_addr;
++(*start_addr);
auto* head = _HeapAlloc<CHARS_HUFFMAN_TREE>(f, 1);
head->ch = ch;
return head;
}
auto* head = _HeapAlloc<CHARS_HUFFMAN_TREE>(f, 1);
head->ch = '%';
head->left = ReadTree_ForStub(f, start_addr);
head->right = ReadTree_ForStub(f, start_addr);
return head;
}
CODE_SEG(".stub_f") void __stdcall TraverseTree_ForStub(STUB_FUNCTION_TABLE* f, CHARS_CODES_LIST** list_head, CHARS_HUFFMAN_TREE* head, char* code)
{
if (head->left == nullptr && head->right == nullptr)
{
auto* ch_codes_tail = _HeapAlloc<CHARS_CODES_LIST>(f, 1);
ch_codes_tail->ch = head->ch;
__strcpy(ch_codes_tail->code, code);
if ((*list_head) == nullptr)
{
(*list_head) = ch_codes_tail;
return;
}
auto* tmp = _HeapAlloc<CHARS_CODES_LIST>(f, 1);
tmp->next = ch_codes_tail;
tmp->prev = (*list_head)->prev;
tmp->ch = (*list_head)->ch;
__strcpy(tmp->code, (*list_head)->code);
ch_codes_tail->prev = tmp;
(*list_head) = ch_codes_tail;
return;
}
char left_code[CHAR_CODE_LEN];
f->RtlZeroMemory(left_code, CHAR_CODE_LEN);
__strcpy(left_code, code);
__strcat(left_code, '0');
char right_code[CHAR_CODE_LEN];
f->RtlZeroMemory(right_code, CHAR_CODE_LEN);
__strcpy(right_code, code);
__strcat(right_code, '1');
TraverseTree_ForStub(f, &(*list_head), head->left, left_code);
TraverseTree_ForStub(f, &(*list_head), head->right, right_code);
}
CODE_SEG(".stub_f") void DeleteTree_ForStub(STUB_FUNCTION_TABLE* f, CHARS_HUFFMAN_TREE* head)
{
if (head->left == nullptr && head->right == nullptr)
{
_FreeHeap(f, head);
return;
}
DeleteTree_ForStub(f, head->left);
DeleteTree_ForStub(f, head->right);
_FreeHeap(f, head);
}