-
Notifications
You must be signed in to change notification settings - Fork 1
/
zle.hh
160 lines (119 loc) · 3.96 KB
/
zle.hh
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
// Copyright Stephan T. Lavavej, http://nuwen.net .
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://boost.org/LICENSE_1_0.txt .
#ifndef PHAM_ZLE_HH
#define PHAM_ZLE_HH
#include "compiler.hh"
#ifdef NUWEN_PLATFORM_MSVC
#pragma once
#endif
#include "bwt.hh"
#include "typedef.hh"
#include "external_begin.hh"
#include <stdexcept>
#include "external_end.hh"
namespace nuwen {
inline vuc_t zle(const vuc_t& v);
inline vuc_t unzle(const vuc_t& v);
}
namespace pham {
inline void encode_zero_run(nuwen::vuc_t& v, nuwen::ul_t& n) {
if (n != 0) {
++n;
while (n != 1) {
v.push_back(static_cast<nuwen::uc_t>(n & 1));
n >>= 1;
}
n = 0;
}
}
inline void decode_zero_run(nuwen::vuc_t& v, nuwen::ul_t& len, nuwen::ul_t& nextbit) {
if (nextbit != 1) {
const nuwen::ull_t total_size = static_cast<nuwen::ull_t>(v.size()) + (len | nextbit) - 1;
if (total_size > 9 + ukk::MAX_ALLOWED_SIZE) {
throw std::runtime_error("RUNTIME ERROR: pham::decode_zero_run() - Too many bytes produced.");
}
v.resize(static_cast<nuwen::vuc_s_t>(total_size), 0x00);
len = 0;
nextbit = 1;
}
}
}
inline nuwen::vuc_t nuwen::zle(const vuc_t& v) {
if (v.size() > 9 + pham::ukk::MAX_ALLOWED_SIZE) {
throw std::logic_error("LOGIC ERROR: nuwen::zle() - v is too big.");
}
vuc_t ret;
ul_t len = 0;
for (vuc_ci_t i = v.begin(); i != v.end(); ++i) {
const uc_t byte = *i;
switch (byte) {
case 0x00:
++len;
break;
case 0xFE:
pham::encode_zero_run(ret, len);
ret.push_back(0xFF);
ret.push_back(0x00);
break;
case 0xFF:
pham::encode_zero_run(ret, len);
ret.push_back(0xFF);
ret.push_back(0x01);
break;
default:
pham::encode_zero_run(ret, len);
ret.push_back(static_cast<uc_t>(byte + 1));
break;
}
}
pham::encode_zero_run(ret, len);
return ret;
}
inline nuwen::vuc_t nuwen::unzle(const vuc_t& v) {
vuc_t ret;
ul_t len = 0;
ul_t nextbit = 1;
for (vuc_ci_t i = v.begin(); i != v.end(); ++i) {
const uc_t byte = *i;
switch (byte) {
case 0x00:
nextbit <<= 1;
if (nextbit == 0) {
throw std::runtime_error("RUNTIME ERROR: nuwen::unzle() - A 0x00 byte overflowed nextbit.");
}
break;
case 0x01:
len |= nextbit;
nextbit <<= 1;
if (nextbit == 0) {
throw std::runtime_error("RUNTIME ERROR: nuwen::unzle() - A 0x01 byte overflowed nextbit.");
}
break;
case 0xFF:
pham::decode_zero_run(ret, len, nextbit);
if (++i == v.end()) {
throw std::runtime_error("RUNTIME ERROR: nuwen::unzle() - A 0xFF byte was followed by nothing.");
}
switch (*i) {
case 0x00:
ret.push_back(0xFE);
break;
case 0x01:
ret.push_back(0xFF);
break;
default:
throw std::runtime_error("RUNTIME ERROR: nuwen::unzle() - A 0xFF byte was followed by an invalid byte.");
}
break;
default:
pham::decode_zero_run(ret, len, nextbit);
ret.push_back(static_cast<uc_t>(byte - 1));
break;
}
}
pham::decode_zero_run(ret, len, nextbit);
return ret;
}
#endif // Idempotency