-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRIFFparser.cpp
More file actions
432 lines (344 loc) · 9.76 KB
/
RIFFparser.cpp
File metadata and controls
432 lines (344 loc) · 9.76 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "RIFFparser.h"
// ====================================================================================================================
RIFF_chunk_t::~RIFF_chunk_t() {}
void RIFF_chunk_t::set_identifier(const char *new_id)
{
if (strlen(new_id) != 4)
throw std::invalid_argument("RIFF chunk identifier must be exactly four characters.");
m_identifier[0] = new_id[0];
m_identifier[1] = new_id[1];
m_identifier[2] = new_id[2];
m_identifier[3] = new_id[3];
}
const char *RIFF_chunk_t::get_identifier()
{
return reinterpret_cast<const char *>(m_identifier);
}
// ====================================================================================================================
RIFF_chunk_data_t::RIFF_chunk_data_t(std::ifstream &f, const char *id)
{
read(f, id);
}
RIFF_chunk_data_t::RIFF_chunk_data_t()
{
}
RIFF_chunk_data_t::RIFF_chunk_data_t(const char *id, const std::vector<uint8_t> &data)
{
set_identifier(id);
set_data(data);
}
RIFF_chunk_data_t::~RIFF_chunk_data_t()
{
}
void RIFF_chunk_data_t::read(std::ifstream &f, const char *id)
{
// if no id is supplied, read it from the filestream
if (id)
{
set_identifier(id);
}
else
{
char identifier[5]{0};
f.read(identifier, 4);
set_identifier(identifier);
}
f.read(reinterpret_cast<char *>(&m_size), sizeof(m_size));
// grab data
m_data.reserve(m_size);
for (int i = 0; i < m_size; i++)
m_data.push_back(f.get());
if(m_data.size() > 64)
{
putchar('\n');
return;
}
}
int RIFF_chunk_data_t::write(std::ofstream &f)
{
f.write(reinterpret_cast<const char *>(m_identifier), 4);
m_size = m_data.size();
f.write(reinterpret_cast<const char *>(&m_size), 4);
for (auto i : m_data)
f.write(reinterpret_cast<const char *>(&i), 1);
int bytes = 8 + m_data.size();
// padding byte if data is odd sized
if (bytes % 2 != 0)
{
const char pad{'\0'};
f.write(&pad, 1);
bytes++;
}
return bytes;
}
void RIFF_chunk_data_t::set_data(const std::vector<uint8_t> &new_data)
{
m_data = new_data;
}
std::vector<uint8_t> &RIFF_chunk_data_t::get_data()
{
return m_data;
}
int RIFF_chunk_data_t::size()
{
return m_data.size();
}
int RIFF_chunk_data_t::total_size()
{
int bytes = m_data.size() + 8;
// padding byte
if (bytes % 2 != 0)
bytes += 1;
return bytes;
}
void RIFF_chunk_data_t::print()
{
printf("RIFF_chunk_data: (length %lu) id: %s\n", m_data.size(), get_identifier());
for (int i = 0; i < m_data.size() && i < 8; i++)
{
printf(" %02x", m_data[i]);
}
if (m_data.size() > 8)
printf(" ... ");
putchar('\n');
}
void RIFF_chunk_data_t::print_full()
{
printf("RIFF_chunk_data: (length %lu) id: %s\n", m_data.size(), get_identifier());
for (int i = 0; i < m_data.size(); i++)
{
if (i % 8 == 0 && i != 0)
putchar('\n');
printf(" %02x", m_data[i]);
}
putchar('\n');
}
// ====================================================================================================================
RIFF_chunk_list_t::RIFF_chunk_list_t()
{
}
RIFF_chunk_list_t::RIFF_chunk_list_t(std::ifstream &f, const char *id)
{
read(f, id);
}
RIFF_chunk_list_t::RIFF_chunk_list_t(const char *form_type)
{
set_identifier("LIST");
set_form_type(form_type);
}
RIFF_chunk_list_t::~RIFF_chunk_list_t()
{
}
const char *RIFF_chunk_list_t::get_form_type()
{
return reinterpret_cast<const char *>(m_form_type);
}
void RIFF_chunk_list_t::set_form_type(const char *new_form_type)
{
if (strlen(new_form_type) != 4)
throw std::runtime_error("RIFF chunk form type must be exactly four characters.");
m_form_type[0] = new_form_type[0];
m_form_type[1] = new_form_type[1];
m_form_type[2] = new_form_type[2];
m_form_type[3] = new_form_type[3];
}
void RIFF_chunk_list_t::read(std::ifstream &f, const char *id)
{
if (id)
set_identifier(id);
else
{
char identifier[5]{0};
f.read(identifier, 4);
set_identifier(identifier);
}
f.read(reinterpret_cast<char *>(&m_size), sizeof(m_size));
f.read(reinterpret_cast<char *>(&m_form_type), 4);
// size == 4 means the list contains only the form type
// form type has already been read, return
if(m_size <= 4)
return;
// while bytes from original position until position + chunk size have been read, or file has been read completely
std::streampos fpos = f.tellg();
while (!f.eof() && f.tellg() <= fpos + static_cast<std::streampos>(m_size))
{
char identifier[5]{0};
// skip padding bytes, eof sometimes doesn't work in the initial while test? check it here also.
if(f.peek() == 0 || f.eof())
{
f.read(identifier, 1);
continue;
}
f.read(identifier, 4);
// determine which type of chunk to add based on its identifier
if (strcmp(identifier, "LIST") == 0)
m_subchunks.push_back(std::make_unique<RIFF_chunk_list_t>(f, identifier));
else
m_subchunks.push_back(std::make_unique<RIFF_chunk_data_t>(f, identifier));
}
}
int RIFF_chunk_list_t::write(std::ofstream &f)
{
int bytes{0};
f.write(reinterpret_cast<const char *>(m_identifier), 4);
// size of contained data minus size of header bytes
uint32_t size = total_size() - 8;
// in case a padding byte is needed at the end
if (size % 2 != 0)
size += 1;
f.write(reinterpret_cast<const char *>(&size), 4);
f.write(reinterpret_cast<const char *>(&m_form_type), 4);
bytes += 12;
for (auto &i : m_subchunks)
bytes += i.get()->write(f);
// if the calculated size of the bytes does not match - write pad bytes until it does
for (int i = 0; i + bytes < size + 8; i++)
{
const char pad{'\0'};
f.write(&pad, 1);
bytes++;
}
return bytes;
}
std::vector<std::unique_ptr<RIFF_chunk_t>> &RIFF_chunk_list_t::get_subchunks()
{
return m_subchunks;
}
int RIFF_chunk_list_t::size()
{
int bytes{0};
for (auto &i : m_subchunks)
bytes += i.get()->size();
return bytes;
}
int RIFF_chunk_list_t::total_size()
{
int bytes{0};
for (auto &i : m_subchunks)
bytes += i.get()->total_size();
// compensate for padding bytes if the number of bytes is odd
if (bytes % 2 != 0)
bytes += 1;
// include size of id, size, form type
return bytes + 12;
}
void RIFF_chunk_list_t::print()
{
printf("\nRIFF_chunk_list: (length %lu chunks) id: %s, form type: %s\n", m_subchunks.size(), get_identifier(), get_form_type());
for (auto &i : m_subchunks)
{
i.get()->print();
}
}
void RIFF_chunk_list_t::print_full()
{
printf("\nRIFF_chunk_list: (length %lu chunks) id: %s, form type: %s\n", m_subchunks.size(), get_identifier(), get_form_type());
for (auto &i : m_subchunks)
{
i.get()->print_full();
}
}
bool RIFF_chunk_list_t::exists_chunk_with_id(const char *id)
{
return get_chunk_with_id(id) ? true : false;
}
RIFF_chunk_t *RIFF_chunk_list_t::get_chunk_with_id(const char *id)
{
if (strlen(id) != 4)
return nullptr;
for (auto &i : m_subchunks)
{
// compare identifiers
RIFF_chunk_t *current = i.get();
if (strcmp(current->get_identifier(), id) == 0)
return current;
// if the current chunk is a list chunk, recurse into it
RIFF_chunk_list_t *dyn_next = dynamic_cast<RIFF_chunk_list_t *>(current);
if (dyn_next)
{
RIFF_chunk_t *next = dyn_next->get_chunk_with_id(id);
if (next)
return next;
}
}
return nullptr;
}
// ====================================================================================================================
RIFF_t::RIFF_t()
{
m_riff.set_identifier("RIFF");
m_riff.set_form_type("NULL");
}
RIFF_t::RIFF_t(std::string filename)
{
m_filepath = filename;
std::ifstream f(filename, std::ios::binary);
if (!f.is_open())
throw std::runtime_error("An error occurred opening the specified RIFF file.");
// read file identifier, verify that this is a valid RIFF file
char identifier[5]{0};
f.read(identifier, 4);
if (strcmp(identifier, "RIFF") != 0)
throw std::runtime_error("The specified file is not a valid RIFF file.");
// this is a valid riff file, read the rest of it
m_riff.read(f, identifier);
f.close();
}
int RIFF_t::size()
{
return m_riff.size();
}
int RIFF_t::total_size()
{
int size = m_riff.total_size();
// compensate for padding bytes if the number of bytes is odd
if (size % 2)
size += 1;
return size;
}
void RIFF_t::print()
{
m_riff.print();
}
void RIFF_t::print_full()
{
m_riff.print_full();
}
int RIFF_t::write()
{
if (m_filepath.empty())
throw std::runtime_error("No file path specified.");
int bytes{0};
std::ofstream f(m_filepath, std::ios::binary | std::ios::trunc);
if (!f.is_open())
throw std::runtime_error("Unable to open specified file for writing.");
bytes += m_riff.write(f);
return bytes;
}
const std::string &RIFF_t::get_filepath()
{
return m_filepath;
}
void RIFF_t::set_filepath(const std::string &new_file_path)
{
m_filepath = new_file_path;
}
bool RIFF_t::exists_chunk_with_id(const char *id)
{
if (strlen(id) != 4)
return false;
return get_chunk_with_id(id) ? true : false;
}
RIFF_chunk_t *RIFF_t::get_chunk_with_id(const char *id)
{
if (strlen(id) != 4)
return nullptr;
// if the root RIFF chunk is requested
if (strcmp(id, "RIFF") == 0)
return &m_riff;
return m_riff.get_chunk_with_id(id);
}
RIFF_chunk_list_t &RIFF_t::get_root_chunk()
{
return m_riff;
}