-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbits.js
197 lines (170 loc) · 4.7 KB
/
bits.js
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { BitOutputStream } from '@thi.ng/bitstream';
export const OPS = {
SUM: 0,
PRODUCT: 1,
MINIMUM: 2,
MAXIMUM: 3,
LITERAL: 4,
GREATER_THAN: 5,
LESS_THAN: 6,
EQUAL_TO: 7,
};
export const HEX_TO_DEC = '0123456789ABCDEF'
.split('')
.reduce((obj, char) => ((obj[char] = parseInt(char, 16)), obj), {});
export function parseHexAs4Bits(input_str) {
// Sizing the buffer is an optimizaiton, BitOutputStream will resize if it needs to
const out = new BitOutputStream(Math.ceil(input_str.length / 2));
for (let char of input_str) {
out.write(HEX_TO_DEC[char], 4);
}
return out;
}
export function parseOutPackets(stream, packets = [], condition = null, depth = 0) {
if (!condition) {
// Minimum amount to read is a LITERAL value of 11 bits, so check against that.
condition = () => stream.position + 3 + 3 + 5 < stream.length;
}
while (condition()) {
try {
/**
* The first three bits encode the packet version,
* and the next three bits encode the packet type ID.
*/
const version = stream.read(3);
const type = stream.read(3);
if (type === OPS.LITERAL) {
const value_stream = new BitOutputStream();
let should_continue;
do {
should_continue = stream.readBit();
value_stream.write(stream.read(4), 4);
} while (should_continue);
const value_bits = [...value_stream.reader()].join('');
const value = parseInt(value_bits, 2);
packets.push(new Literal(version, type, value));
} else {
/**
* Operator packet
*
* An operator packet can use one of two modes indicated by
* the bit immediately after the packet header; this is called the _length type ID_.
* - If the length type ID is 0, then the next 15 bits are a
* number that represents the total length in bits of the sub-packets
* contained by this packet.
* - If the length type ID is 1, then the next 11 bits are a
* number that represents the number of sub-packets immediately
* contained by this packet.
*/
const length_type = stream.readBit();
const will_read_bits = length_type === 0;
const length_value = will_read_bits ? stream.read(15) : stream.read(11);
const end_position = stream.position + length_value;
let packet = new Operator(version, type);
packets.push(packet);
// We have different conditions to continue reading subpackets depending on length type
const newCondition = will_read_bits
? () => stream.position < end_position
: () => packet.length < length_value;
// `depth` was just used for debugging, but it's not needed
parseOutPackets(stream, packet.subpackets, newCondition, depth + 1);
}
} catch (e) {
/**
* Reading beyond the length of the stream throws an error, typically
* due to `0` padding at the end of our binrary stream. When this happens,
* it can safely be ignored and our packets can be returned.
*/
console.warn('error');
return packets;
}
}
return packets;
}
export function* packetsIter(packets) {
for (let packet of packets) {
yield* packet;
}
}
export class Packet {
constructor(version, type) {
this.version = version;
this.type = type;
}
}
export class Literal extends Packet {
constructor(version, type, value) {
super(version, type);
this.value = value;
}
*[Symbol.iterator]() {
yield this;
}
}
export class Operator extends Packet {
constructor(version, type) {
super(version, type);
this.subpackets = [];
}
get length() {
return this.subpackets.length;
}
*[Symbol.iterator]() {
yield this;
for (let subpacket of this.subpackets) {
yield* subpacket;
}
}
// @todo cache these return values
get value() {
switch (this.type) {
case OPS.SUM: {
let sum = 0;
for (let p of this.subpackets) {
sum += p.value;
}
return sum;
}
case OPS.PRODUCT: {
let prod = 1;
for (let p of this.subpackets) {
prod *= p.value;
}
return prod;
}
case OPS.MINIMUM: {
let min = this.subpackets[0].value;
for (let i = 1; i < this.subpackets.length; i++) {
let p = this.subpackets[i];
let v = p.value;
if (v < min) {
min = v;
}
}
return min;
}
case OPS.MAXIMUM: {
let max = this.subpackets[0].value;
for (let i = 1; i < this.subpackets.length; i++) {
let p = this.subpackets[i];
let v = p.value;
if (v > max) {
max = v;
}
}
return max;
}
case OPS.GREATER_THAN: {
return this.subpackets[0].value > this.subpackets[1].value ? 1 : 0;
}
case OPS.LESS_THAN: {
return this.subpackets[0].value < this.subpackets[1].value ? 1 : 0;
}
case OPS.EQUAL_TO: {
return this.subpackets[0].value === this.subpackets[1].value ? 1 : 0;
}
default:
throw 'Invalid Op';
}
}
}