-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecoder.cpp
More file actions
82 lines (60 loc) · 2.19 KB
/
decoder.cpp
File metadata and controls
82 lines (60 loc) · 2.19 KB
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
//
// decoder.cpp
// p4
//
// Created by Davey Jay Belliss on 5/7/15.
// Copyright (c) 2015 Davey Jay Belliss. All rights reserved.
//
#include "decoder.h"
#include "hNode.h"
#include <fstream>
//#include "CPUTimer.h"
void Decoder::decode(const unsigned char* encodedMessage, const int encodedSize,
unsigned char* decodedMessage, int *decodedSize)
{
hNode* root = new hNode();
int pointer = 0;
int *pos = &pointer; // POSITION IN ENCODED ARRAY
short extra = encodedMessage[0];
makeTree(encodedMessage, encodedSize, decodedMessage, decodedSize, root, *pos, false);
(*pos) = *pos + 5;
int decode = 0;
int *decoded = &decode; // NUMBER OF ELEMENTS DECODED
short bits = 0;
short *bit = &bits;
int bytes = 0;
int *byte = &bytes; // numbers oc chars decoded
unsigned char* binary = new unsigned char; // CHAR TO READ BINARY CHARACTERS OUT OF
int size = encodedSize - *pos; // HOW MANY BITS TO READ
binary[0] = encodedMessage[(*pos)++];
root->treeTraverse(decodedMessage, encodedMessage, *pos, *decoded, binary, *bit, *byte, size, extra);
*decodedSize = *decoded;
return;
} // decode()
Decoder::Decoder()
{
} // Decoder()
Decoder::~Decoder()
{
} // ~Decoder()
void Decoder::makeTree(const unsigned char *encodedMessage, const int encodedSize, unsigned char *decodedMessage, int *decodedSize, hNode* parent, int& i, bool isLeft)
{
i++;
if (encodedMessage[i] == 'D')
if (encodedMessage[i+1] == 'O')
if (encodedMessage[i+2] == 'N')
if (encodedMessage[i+3] == 'E')
return;
if (encodedMessage[i] == '0')//current node is NOT a leafnode
{
hNode* newNodeL = new hNode();
newNodeL->setParentL(parent);
makeTree(encodedMessage, encodedSize, decodedMessage, decodedSize, newNodeL, i, true);
hNode* newNodeR = new hNode();
newNodeR->setParentR(parent);
makeTree(encodedMessage, encodedSize, decodedMessage, decodedSize, newNodeR, i, false);
return;
}
else //(encodedMessage[i] == '1')
parent->setASCII(encodedMessage[++i]);
} // Rebuilds Tree