-
Notifications
You must be signed in to change notification settings - Fork 2
/
BdfReader.cpp
172 lines (137 loc) · 3.71 KB
/
BdfReader.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
93
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "Bdf.hpp"
#include "BdfHelpers.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <codecvt>
#include <locale>
using namespace Bdf;
using namespace BdfHelpers;
void BdfReader::initEmpty()
{
lookupTable = new BdfLookupTable(this);
bdf = new BdfObject(lookupTable);
}
BdfReader::BdfReader() {
initEmpty();
}
BdfReader::BdfReader(const char* data, int size)
{
if(size == 0) {
initEmpty();
return;
}
// Get the size of the bdf size tag and the lookup table size tag
char lookupTable_size_tag;
char lookupTable_size_bytes = 0;
char bdf_size_tag;
char bdf_size_bytes = 0;
BdfObject::getFlagData(data, NULL, &bdf_size_tag, &lookupTable_size_tag);
lookupTable_size_bytes = BdfObject::getSizeBytes(lookupTable_size_tag);
bdf_size_bytes = BdfObject::getSizeBytes(bdf_size_tag);
// Check if there is enough space
if(1 + lookupTable_size_bytes + bdf_size_bytes > size) {
initEmpty();
return;
}
// Get the rest of the data
int bdf_size = BdfObject::getSize(data);
// Check if there is enough space in the buffer
if(bdf_size <= 0 || bdf_size + lookupTable_size_bytes > size) {
initEmpty();
return;
}
const char* data_bdf = data;
data += bdf_size;
// Get the size of the lookup table
int lookupTable_size = 0;
switch(lookupTable_size_tag)
{
case 0:
lookupTable_size = get_netsi(data);
break;
case 1:
lookupTable_size = get_netus(data);
break;
case 2:
lookupTable_size = data[0] & 255;
break;
}
// Check if there is enough space in the buffer
if(bdf_size + lookupTable_size_bytes + lookupTable_size > size) {
initEmpty();
return;
}
// Load the lookup table and the objects from the buffer
lookupTable = new BdfLookupTable(this, data + lookupTable_size_bytes, lookupTable_size);
bdf = new BdfObject(lookupTable, data_bdf, bdf_size);
}
BdfReader::~BdfReader() {
delete lookupTable;
delete bdf;
}
void BdfReader::serialize(char** pData, int* pSize)
{
int locations_size = lookupTable->size();
int* locations = new int[locations_size];
lookupTable->serializeGetLocations(locations);
int bdf_size = bdf->serializeSeeker(locations);
int lookupTable_size = lookupTable->serializeSeeker(locations, locations_size);
int lookupTable_size_bytes = 0;
char lookupTable_size_tag = 0;
if(lookupTable_size > 65535) {
lookupTable_size_tag = 0;
lookupTable_size_bytes = 4;
} else if(lookupTable_size > 255) {
lookupTable_size_tag = 1;
lookupTable_size_bytes = 2;
} else {
lookupTable_size_tag = 2;
lookupTable_size_bytes = 1;
}
int data_size = bdf_size + lookupTable_size + lookupTable_size_bytes;
char* data = new char[data_size];
*pData = data;
*pSize = data_size;
bdf->serialize(data, locations, lookupTable_size_tag);
data += bdf_size;
switch(lookupTable_size_bytes)
{
case 4:
put_netsi(data, lookupTable_size);
break;
case 2:
put_netus(data, lookupTable_size);
break;
default:
data[0] = lookupTable_size & 255;
}
lookupTable->serialize(data + lookupTable_size_bytes, locations, locations_size);
delete[] locations;
}
BdfObject* BdfReader::getObject() {
return bdf;
}
BdfObject* BdfReader::resetObject()
{
delete bdf;
bdf = new BdfObject(lookupTable);
return bdf;
}
std::string BdfReader::serializeHumanReadable(BdfIndent indent)
{
std::stringstream stream;
bdf->serializeHumanReadable(stream, indent, 0);
return stream.str();
}
std::string BdfReader::serializeHumanReadable() {
return serializeHumanReadable(BdfIndent("", ""));
}
void BdfReader::serializeHumanReadable(std::ostream &stream) {
bdf->serializeHumanReadable(stream, BdfIndent("", ""), 0);
stream << "\n";
}
void BdfReader::serializeHumanReadable(std::ostream &stream, BdfIndent indent) {
bdf->serializeHumanReadable(stream, indent, 0);
stream << "\n";
}