Repository navigation
Expand file tree
/
Copy pathguanoHandler.js
More file actions
178 lines (101 loc) · 3.48 KB
/
Copy pathguanoHandler.js
File metadata and controls
178 lines (101 loc) · 3.48 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
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
/****************************************************************************
* guanoHandler.js
* openacousticdevices.info
* February 2024
*****************************************************************************/
'use strict';
/* RIFF constants */
const UINT32_LENGTH = 4;
const RIFF_ID_LENGTH = 4;
/* RIFF component read functions */
function readString (state, length) {
if (state.buffer.length - state.index < length) throw new Error('RIFF component exceeded buffer length.');
const buffer = state.buffer.subarray(state.index, state.index + length);
state.index += length;
return {
contents: buffer.toString('utf8'),
buffer: Buffer.from(buffer)
};
}
function readUInt32LE (state) {
if (state.buffer.length - state.index < UINT32_LENGTH) throw new Error('RIFF component exceeded buffer length.');
const result = state.buffer.readUInt32LE(state.index);
state.index += UINT32_LENGTH;
return result;
}
function readChunk (state, id) {
const response = readString(state, RIFF_ID_LENGTH);
if (response.contents !== id) throw new Error('Could not find ' + id.replace(' ', '') + ' chunk ID.');
return {
id: response.contents,
size: readUInt32LE(state)
};
}
/* RIFF component write functions */
function writeString (state, string, length) {
const maximumWriteLength = Math.min(string.length, length);
state.buffer.fill(0, state.index, state.index + length);
state.buffer.write(string, state.index, maximumWriteLength, 'utf8');
state.index += length;
}
function writeBuffer (state, buffer) {
buffer.copy(state.buffer, state.index, 0, buffer.length);
state.index += buffer.length;
}
function writeUInt32LE (state, value) {
state.buffer.writeUInt32LE(value, state.index);
state.index += UINT32_LENGTH;
}
function writeChunk (state, chunk) {
writeString(state, chunk.id, RIFF_ID_LENGTH);
writeUInt32LE(state, chunk.size);
}
/* GUANO read and write functions */
function readGuano (buffer, bufferSize) {
const guano = {};
const state = {buffer: buffer, index: 0};
try {
/* Read RIFF chunk */
guano.guan = readChunk(state, 'guan');
if (guano.guan.size + RIFF_ID_LENGTH + UINT32_LENGTH > bufferSize) {
return {
success: false,
error: 'GUANO size exceeds buffer size.'
};
}
/* Read the contents */
const response = readString(state, guano.guan.size);
guano.contents = response.contents;
guano.buffer = response.buffer;
guano.size = guano.guan.size + RIFF_ID_LENGTH + UINT32_LENGTH;
/* Success */
return {
guano: guano,
success: true,
error: null
};
} catch (e) {
/* GUANO has exceed buffer length */
return {
success: false,
error: e.message
};
}
}
function writeGuano (buffer, guano) {
const state = {buffer: buffer, index: 0};
writeChunk(state, guano.guan);
writeBuffer(state, guano.buffer);
return buffer;
}
/* Functions to update GUANO */
function updateContents (guano, contents) {
guano.buffer = Buffer.from(contents, 'utf8');
guano.contents = contents;
guano.guan.size = guano.buffer.length;
guano.size = RIFF_ID_LENGTH + UINT32_LENGTH + guano.guan.size;
}
/* Exports */
exports.writeGuano = writeGuano;
exports.readGuano = readGuano;
exports.updateContents = updateContents;