-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitfield.js
173 lines (146 loc) · 6.17 KB
/
Bitfield.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
const Struct = require('./Struct.js');
class Bitfield {
value;
#fields;
#bitOffsets = new Object;
bitlen() {}
constructor(value = 0, fields = {}) {
this.value = value;
this.#fields = fields;
let length = Object.values(fields).reduce((sum, l) => sum + l);
if (typeof length !== 'number' || Number.isNaN(length))
throw new TypeError(`Invalid bitfield definition '${JSON.stringify(fields)}'.`);
if (length > this.bitlen())
throw new Error(`The sum of the fields' bit lengths should not exceed ${this.bitlen()}, got ${length} instead.`);
let currentBit = 0;
for (const [name, length] of Object.entries(this.#fields)) {
this.#bitOffsets[name] = currentBit;
Object.defineProperty(this, name, {
get: () => (this.value >> (this.bitlen() - this.#bitOffsets[name] - length)) & ((1 << length) - 1),
set: (bitValue) => {
const mask = ((1 << length) - 1) << (this.bitlen() - this.#bitOffsets[name] - length);
this.value = (this.value & ~mask) | ((bitValue << (this.bitlen() - this.#bitOffsets[name] - length)) & mask);
},
enumerable: !name.startsWith('#'),
configurable: false,
});
currentBit += length;
}
}
toJSON() {
let ret = new Object;
for (const [name, length] of Object.entries(this.#fields)) {
if (name.startsWith('#')) continue;
if (length === 1)
ret[name] = Boolean(this[name]);
else
ret[name] = '0b'+ this[name].toString(2).padStart(length, '0');
}
return ret;
}
}
class Bitfield8 extends Bitfield {
static get sizeof() { return 1; }
bitlen() { return 8; }
static get name() { return 'Bitfield8'; }
/**
* Reads a 8-bit bitfield from the data view and returns an instance of Bitfield8.
* @param {DataView} data - The DataView containing the binary data.
* @param {number} position - The position in the DataView where the bitfield starts.
* @param {boolean} [isLittleEndian=false] - Indicates if the data is in little-endian format.
* @param {object} [options] - Field mapping options for bit names and lengths.
* @returns {Bitfield8} - An instance of Bitfield8 with named fields.
*/
static read(data, position, isLittleEndian = false, options = {}) {
const rawValue = Struct.readValue(data, position, 'u8', isLittleEndian).value;
return new Bitfield8(rawValue, options);
}
/**
* Returns the raw bitfield value as a binary string.
*/
toString() {
return `0b${this.value.toString(2).padStart(8, '0')}`;
}
static {
Struct.registerFromClass(Bitfield8);
}
}
class Bitfield16 extends Bitfield {
static get sizeof() { return 2; }
bitlen() { return 16; }
static get name() { return 'Bitfield16'; }
/**
* Reads a 16-bit bitfield from the data view and returns an instance of Bitfield16.
* @param {DataView} data - The DataView containing the binary data.
* @param {number} position - The position in the DataView where the bitfield starts.
* @param {boolean} [isLittleEndian=false] - Indicates if the data is in little-endian format.
* @param {object} [options] - Field mapping options for bit names and lengths.
* @returns {Bitfield16} - An instance of Bitfield16 with named fields.
*/
static read(data, position, isLittleEndian = false, options = {}) {
const rawValue = Struct.readValue(data, position, 'u16', isLittleEndian).value;
return new Bitfield16(rawValue, options);
}
/**
* Returns the raw bitfield value as a binary string.
*/
toString() {
return `0b${this.value.toString(2).padStart(16, '0')}`;
}
static {
Struct.registerFromClass(Bitfield16);
}
}
class Bitfield32 extends Bitfield {
static get sizeof() { return 4; }
bitlen() { return 32; }
static get name() { return 'Bitfield32'; }
/**
* Reads a 32-bit bitfield from the data view and returns an instance of Bitfield32.
* @param {DataView} data - The DataView containing the binary data.
* @param {number} position - The position in the DataView where the bitfield starts.
* @param {boolean} [isLittleEndian=false] - Indicates if the data is in little-endian format.
* @param {object} [options] - Field mapping options for bit names and lengths.
* @returns {Bitfield32} - An instance of Bitfield32 with named fields.
*/
static read(data, position, isLittleEndian = false, options = {}) {
const rawValue = Struct.readValue(data, position, 'u32', isLittleEndian).value;
return new Bitfield32(rawValue, options);
}
/**
* Returns the raw bitfield value as a binary string.
*/
toString() {
return `0b${this.value.toString(2).padStart(32, '0')}`;
}
static {
Struct.registerFromClass(Bitfield32);
}
}
class Bitfield64 extends Bitfield {
static get sizeof() { return 8; }
bitlen() { return 64; }
static get name() { return 'Bitfield64'; }
/**
* Reads a 64-bit bitfield from the data view and returns an instance of Bitfield64.
* @param {DataView} data - The DataView containing the binary data.
* @param {number} position - The position in the DataView where the bitfield starts.
* @param {boolean} [isLittleEndian=false] - Indicates if the data is in little-endian format.
* @param {object} [options] - Field mapping options for bit names and lengths.
* @returns {Bitfield64} - An instance of Bitfield64 with named fields.
*/
static read(data, position, isLittleEndian = false, options = {}) {
const rawValue = Struct.readValue(data, position, 'u64', isLittleEndian).value;
return new Bitfield64(rawValue, options);
}
/**
* Returns the raw bitfield value as a binary string.
*/
toString() {
return `0b${this.value.toString(2).padStart(64, '0')}`;
}
static {
Struct.registerFromClass(Bitfield64);
}
}
module.exports = { Bitfield8, Bitfield16, Bitfield32, Bitfield64 };