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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
|
#include "frame.h"
#include "util.h"
#ifdef _WIN32
#include "zlib/zlib.h"
#else
#include "zlib/zlib.h"
#endif
#include "frames.h"
#include <string.h>
#include <stdlib.h>
#include "nu/ByteReader.h"
#include "nu/ByteWriter.h"
#include "foundation/error.h"
#include "nsid3v2.h"
/* === ID3v2 common === */
ID3v2::Frame::Frame()
{
data = 0;
data_size = 0;
}
ID3v2::Frame::~Frame()
{
free(data);
}
int ID3v2::Frame::GetData(const void **_data, size_t *data_len) const
{
if (data)
{
*_data = data;
*data_len = data_size;
return NErr_Success;
}
else
return NErr_NullPointer;
}
size_t ID3v2::Frame::GetDataSize() const
{
return data_size;
}
int ID3v2::Frame::NewData(size_t new_len, void **_data, size_t *_data_len)
{
// we DO NOT update the header, as its meant to hold the original data
void *new_data = realloc(data, new_len);
if (new_data)
{
data = new_data;
data_size = new_len;
*_data = data;
*_data_len = data_size;
return NErr_Success;
}
else
return NErr_OutOfMemory;
}
bool ID3v2::Frame::Encrypted() const
{
return false;
}
bool ID3v2::Frame::Compressed() const
{
return false;
}
bool ID3v2::Frame::Grouped() const
{
return false;
}
bool ID3v2::Frame::ReadOnly() const
{
return false;
}
bool ID3v2::Frame::FrameUnsynchronised() const
{
return false;
}
bool ID3v2::Frame::DataLengthIndicated() const
{
return false;
}
bool ID3v2::Frame::TagAlterPreservation() const
{
return false;
}
bool ID3v2::Frame::FileAlterPreservation() const
{
return false;
}
static inline void Advance(const void *&data, size_t &len, size_t amount)
{
data = (const uint8_t *)data + amount;
len -= amount;
}
static inline void AdvanceBoth(const void *&data, size_t &len, size_t &len2, size_t amount)
{
data = (const uint8_t *)data + amount;
len -= amount;
len2 -= amount;
}
/* === ID3v2.2 === */
ID3v2_2::Frame::Frame(const ID3v2::Header &_header, const int8_t *id, int flags) : header(_header, id, flags)
{
}
ID3v2_2::Frame::Frame(const FrameHeader &_header) : header(_header)
{
}
int ID3v2_2::Frame::Parse(const void *_data, size_t len, size_t *read)
{
*read = 0;
data_size = header.FrameSize(); // size of frame AFTER re-synchronization
/* check to make sure that we have enough input data to read the data */
if (header.Unsynchronised())
{
/* this is tricky, because the stored size reflects after re-synchronization,
but the incoming data is unsynchronized */
if (ID3v2::Util::UnsynchronisedInputSize(_data, data_size) > len)
return 1;
}
else if (data_size > len)
return 1;
/* allocate memory (real data_size) */
data = malloc(data_size);
if (!data)
return 1;
/* === Read the data === */
if (header.Unsynchronised())
{
*read += ID3v2::Util::UnsynchroniseTo(data, _data, data_size);
}
else // normal data
{
memcpy(data, _data, data_size);
*read += data_size;
}
return NErr_Success;
}
int ID3v2_2::Frame::SerializedSize(uint32_t *length, const ID3v2::Header &tag_header, int flags) const
{
ID3v2_2::FrameHeader new_header(header, tag_header);
// TODO: for now, we're not going to deal with compression
new_header.SetSize(data_size);
uint32_t current_length=0;
new_header.SerializedSize(¤t_length);
if (new_header.Unsynchronised())
{
current_length += ID3v2::Util::SynchronisedSize(data, data_size);
}
else
{
current_length += new_header.FrameSize();
}
*length = current_length;
return NErr_Success;
}
int ID3v2_2::Frame::Serialize(void *output, uint32_t *written, const ID3v2::Header &tag_header, int flags) const
{
size_t current_length = FrameHeader::SIZE;
uint8_t *data_ptr = (uint8_t *)output;
ID3v2_2::FrameHeader new_header(header, tag_header);
new_header.SetSize(data_size);
// write frame header
new_header.Serialize(data_ptr);
data_ptr += FrameHeader::SIZE;
if (new_header.Unsynchronised())
{
current_length += ID3v2::Util::SynchroniseTo(data_ptr, data, data_size);
}
else
{
memcpy(data_ptr, data, data_size);
current_length += data_size;
}
*written = current_length;
return NErr_Success;
}
const int8_t *ID3v2_2::Frame::GetIdentifier() const
{
return header.GetIdentifier();
}
/* === ID3v2.3 === */
ID3v2_3::Frame::Frame(const ID3v2::Header &_header, const int8_t *id, int flags) : header(_header, id, flags)
{
}
ID3v2_3::Frame::Frame(const FrameHeader &_header) : header(_header)
{
}
/* helper function
reads num_bytes from input into output, dealing with re-synchronization and length checking
increments input pointer
increments bytes_read value by number of input bytes read (different from num_bytes when data is unsynchronized
decrements input_len by bytes read
decrements output_len by bytes written
*/
bool ID3v2_3::Frame::ReadData(void *output, const void *&input, size_t &input_len, size_t &frame_len, size_t num_bytes, size_t *bytes_read) const
{
/* verify that we have enough data in the frame */
if (num_bytes > frame_len)
return false;
/* verify that we have enough data in the buffer */
size_t bytes_to_read;
if (header.Unsynchronised())
bytes_to_read = ID3v2::Util::UnsynchronisedInputSize(input, num_bytes);
else
bytes_to_read = num_bytes;
if (bytes_to_read > input_len)
return false;
/* read data */
if (header.Unsynchronised())
{
*bytes_read += ID3v2::Util::SynchroniseTo(&output, input, num_bytes);
}
else
{
*bytes_read += num_bytes;
memcpy(output, input, num_bytes);
}
/* increment input pointer */
input = (const uint8_t *)input + bytes_to_read;
/* decrement sizes */
frame_len -= num_bytes;
input_len -= bytes_to_read;
return true;
}
/* benski> this function is a bit complex
we have two things to worry about, and can have any combination of the two
1) Is the data 'unsynchronized'
2) Is the data compressed (zlib)
we keep track of three sizes:
len - number of bytes in input buffer
data_size - number of bytes of output data buffer
frame_size - number of bytes of data in frame AFTER re-synchronization
frame_size==data_size when compression is OFF
*/
int ID3v2_3::Frame::Parse(const void *_data, size_t len, size_t *read)
{
*read = 0;
size_t frame_size = header.FrameSize(); // size of frame AFTER re-synchronization
if (header.Compressed())
{
// read 4 bytes of decompressed size
uint8_t raw_size[4];
if (ReadData(raw_size, _data, len, frame_size, 4, read) == false)
return 1;
bytereader_value_t byte_reader;
bytereader_init(&byte_reader, raw_size, 4);
data_size = bytereader_read_u32_be(&byte_reader);
}
/* Check for group identity. If this exists, we'll store it separate from the raw data */
if (header.Grouped())
{
// read 1 byte for group identity
if (ReadData(&group_identity, _data, len, frame_size, 1, read) == false)
return 1;
}
if (!header.Compressed())
{
data_size = frame_size;
}
/* check to make sure that we have enough input data to read the data */
if (!header.Compressed() && header.Unsynchronised())
{
/* this is tricky, because the stored size reflects after re-synchronization,
but the incoming data is unsynchronized */
if (ID3v2::Util::UnsynchronisedInputSize(_data, data_size) > len)
return 1;
}
else if (frame_size > len)
return 1;
/* allocate memory (real data_size) */
data = malloc(data_size);
if (!data)
return NErr_OutOfMemory;
/* === Read the data === */
if (header.Compressed())
{
if (header.Unsynchronised()) // compressed AND unsynchronized.. what a pain!!
{
// TODO: combined re-synchronization + inflation
void *temp = malloc(frame_size);
if (!temp)
return NErr_OutOfMemory;
*read += ID3v2::Util::UnsynchroniseTo(temp, _data, frame_size);
uLongf uncompressedSize = data_size;
int ret = uncompress((Bytef *)data, &uncompressedSize, (const Bytef *)temp, frame_size);
free(temp);
if (ret != Z_OK)
return 1;
}
else
{
uLongf uncompressedSize = data_size;
if (uncompress((Bytef *)data, &uncompressedSize, (const Bytef *)_data, frame_size) != Z_OK)
return 1;
*read += frame_size;
}
}
else if (header.Unsynchronised())
{
*read += ID3v2::Util::UnsynchroniseTo(data, _data, data_size);
}
else // normal data
{
memcpy(data, _data, data_size);
*read += data_size;
}
return NErr_Success;
}
int ID3v2_3::Frame::SerializedSize(uint32_t *length, const ID3v2::Header &tag_header, int flags) const
{
ID3v2_3::FrameHeader new_header(header, tag_header);
// TODO: for now, we're not going to deal with compression
new_header.ClearCompressed();
new_header.SetSize(data_size);
uint32_t current_length=0;
new_header.SerializedSize(¤t_length);
if (new_header.Unsynchronised())
{
if (new_header.Compressed())
{
uint8_t data_length[4];
bytewriter_s byte_writer;
bytewriter_init(&byte_writer, data_length, 4);
bytewriter_write_u32_be(&byte_writer, data_size);
current_length += ID3v2::Util::SynchronisedSize(&data_length, 4);
}
if (new_header.Grouped())
current_length += ID3v2::Util::SynchronisedSize(&group_identity, 1);
current_length += ID3v2::Util::SynchronisedSize(data, data_size);
}
else
{
current_length += new_header.FrameSize();
}
*length = current_length;
return NErr_Success;
}
int ID3v2_3::Frame::Serialize(void *output, uint32_t *written, const ID3v2::Header &tag_header, int flags) const
{
size_t current_length = FrameHeaderBase::SIZE;
uint8_t *data_ptr = (uint8_t *)output;
ID3v2_3::FrameHeader new_header(header, tag_header);
// TODO: for now, we're not going to deal with compression
new_header.ClearCompressed();
new_header.SetSize(data_size);
// write frame header
uint32_t header_size;
new_header.Serialize(data_ptr, &header_size);
data_ptr += header_size;
if (new_header.Unsynchronised())
{
if (new_header.Compressed())
{
uint8_t data_length[4];
bytewriter_s byte_writer;
bytewriter_init(&byte_writer, data_length, 4);
bytewriter_write_u32_be(&byte_writer, data_size);
current_length += ID3v2::Util::SynchroniseTo(data_ptr, &data_length, 4);
data_ptr+=4;
}
if (new_header.Grouped())
current_length += ID3v2::Util::SynchroniseTo(data_ptr++, &group_identity, 1);
current_length += ID3v2::Util::SynchroniseTo(data_ptr, data, data_size);
}
else
{
if (new_header.Compressed())
{
bytewriter_s byte_writer;
bytewriter_init(&byte_writer, data_ptr, 4);
bytewriter_write_u32_be(&byte_writer, data_size);
data_ptr+=4;
}
if (new_header.Grouped())
{
*data_ptr++ = group_identity;
current_length++;
}
memcpy(data_ptr, data, data_size);
current_length += data_size;
}
*written = current_length;
return NErr_Success;
}
const int8_t *ID3v2_3::Frame::GetIdentifier() const
{
return header.GetIdentifier();
}
bool ID3v2_3::Frame::Encrypted() const
{
return header.Encrypted();
}
bool ID3v2_3::Frame::Compressed() const
{
return header.Compressed();
}
bool ID3v2_3::Frame::Grouped() const
{
return header.Grouped();
}
bool ID3v2_3::Frame::ReadOnly() const
{
return header.ReadOnly();
}
bool ID3v2_3::Frame::TagAlterPreservation() const
{
return header.TagAlterPreservation();
}
bool ID3v2_3::Frame::FileAlterPreservation() const
{
return header.FileAlterPreservation();
}
/* === ID3v2.4 === */
ID3v2_4::Frame::Frame(const ID3v2::Header &_header, const int8_t *id, int flags) : header(_header, id, flags)
{
}
ID3v2_4::Frame::Frame(const FrameHeader &_header) : header(_header)
{
}
/* helper function
reads num_bytes from input into output, dealing with re-synchronization and length checking
increments input pointer
increments bytes_read value by number of input bytes read (different from num_bytes when data is unsynchronized
decrements input_len by bytes read
decrements output_len by bytes written
*/
bool ID3v2_4::Frame::ReadData(void *output, const void *&input, size_t &input_len, size_t &frame_len, size_t num_bytes, size_t *bytes_read) const
{
/* verify that we have enough data in the frame */
if (num_bytes > frame_len)
return false;
/* verify that we have enough data in the buffer */
size_t bytes_to_read = num_bytes;
if (bytes_to_read > input_len)
return false;
/* read data */
*bytes_read += num_bytes;
memcpy(output, input, num_bytes);
/* increment input pointer */
input = (const uint8_t *)input + bytes_to_read;
/* decrement sizes */
frame_len -= num_bytes;
input_len -= bytes_to_read;
return true;
}
/* benski> this function is a bit complex
we have two things to worry about, and can have any combination of the two
1) Is the data 'unsynchronized'
2) Is the data compressed (zlib)
we keep track of three sizes:
len - number of bytes in input buffer
data_size - number of bytes of output data buffer
frame_size - number of bytes of data in frame AFTER re-synchronization
frame_size==data_size when compression is OFF
*/
int ID3v2_4::Frame::Parse(const void *_data, size_t len, size_t *read)
{
*read = 0;
size_t frame_size = header.FrameSize();
// TODO: if frame_size >= 128, verify size. iTunes v2.4 parser bug ...
/* Check for group identity. If this exists, we'll store it separate from the raw data */
/* Note: ID3v2.4 puts group identity BEFORE data length indicator, where as v2.3 has it the other way */
if (header.Grouped())
{
// read 1 byte for group identity
if (ReadData(&group_identity, _data, len, frame_size, 1, read) == false)
return 1;
}
if (header.Compressed() || header.DataLengthIndicated())
{
// read 4 bytes of decompressed size
uint8_t raw_size[4];
if (ReadData(raw_size, _data, len, frame_size, 4, read) == false)
return 1;
bytereader_value_t byte_reader;
bytereader_init(&byte_reader, raw_size, 4);
data_size = bytereader_read_u32_be(&byte_reader);
}
if (!(header.Compressed() || header.DataLengthIndicated()))
{
data_size = frame_size;
}
/* check to make sure that we have enough input data to read the data */
if (frame_size > len)
return 1;
if (!header.Compressed() && header.Unsynchronised())
{
data_size = ID3v2::Util::UnsynchronisedOutputSize(_data, frame_size);
}
/* allocate memory (real data_size) */
data = malloc(data_size);
if (!data)
return NErr_OutOfMemory;
/* === Read the data === */
if (header.Compressed())
{
if (header.Unsynchronised()) // compressed AND unsynchronized.. what a pain!!
{
// TODO: combined re-synchronization + inflation
size_t sync_size = ID3v2::Util::UnsynchronisedOutputSize(_data, frame_size);
void *temp = malloc(sync_size);
if (!temp)
return NErr_OutOfMemory;
*read += ID3v2::Util::UnsynchroniseTo(temp, _data, sync_size);
uLongf uncompressedSize = data_size;
int ret = uncompress((Bytef *)data, &uncompressedSize, (const Bytef *)temp, sync_size);
/* TODO: realloc and set data_size to uncompressedSize if uncompressedSize was actually lower */
free(temp);
if (ret != Z_OK)
return 1;
}
else
{
uLongf uncompressedSize = data_size;
if (uncompress((Bytef *)data, &uncompressedSize, (const Bytef *)_data, frame_size) != Z_OK)
return 1;
/* TODO: realloc and set data_size to uncompressedSize if uncompressedSize was actually lower */
*read += frame_size;
}
}
else if (header.Unsynchronised())
{
*read += ID3v2::Util::UnsynchroniseTo(data, _data, data_size);
}
else // normal data
{
memcpy(data, _data, data_size);
*read += data_size;
}
return 0;
}
int ID3v2_4::Frame::SerializedSize(uint32_t *length, const ID3v2::Header &tag_header, int flags) const
{
ID3v2_4::FrameHeader new_header(header, tag_header);
// TODO: for now, we're not going to deal with compression
new_header.ClearCompressed();
switch(flags & Serialize_UnsynchronizeMask)
{
case Serialize_Unsynchronize:
// TODO:
break;
case Serialize_NoUnsynchronize:
new_header.ClearUnsynchronized();
break;
}
// TODO: this doesn't handle compression
if (new_header.Unsynchronised())
{
size_t unsynchronized_data_size = ID3v2::Util::SynchronisedSize(data, data_size);
new_header.SetSize(unsynchronized_data_size);
}
else
{
new_header.SetSize(data_size);
}
size_t current_length = ID3v2_4::FrameHeader::SIZE;
if (new_header.Unsynchronised())
{
if (new_header.DataLengthIndicated() || new_header.Compressed())
{
current_length += 4;
}
if (new_header.Grouped())
current_length += ID3v2::Util::SynchronisedSize(&group_identity, 1);
current_length += ID3v2::Util::SynchronisedSize(data, data_size);
}
else
{
current_length += new_header.FrameSize();
}
*length = current_length;
return NErr_Success;
}
int ID3v2_4::Frame::Serialize(void *output, uint32_t *written, const ID3v2::Header &tag_header, int flags) const
{
size_t current_length = ID3v2_4::FrameHeader::SIZE;
uint8_t *data_ptr = (uint8_t *)output;
ID3v2_4::FrameHeader new_header(header, tag_header);
// TODO: for now, we're not going to deal with compression
new_header.ClearCompressed();
switch(flags & Serialize_UnsynchronizeMask)
{
case Serialize_Unsynchronize:
// TODO:
break;
case Serialize_NoUnsynchronize:
new_header.ClearUnsynchronized();
break;
}
// TODO: this doesn't handle compression
if (new_header.Unsynchronised())
{
size_t unsynchronized_data_size = ID3v2::Util::SynchronisedSize(data, data_size);
new_header.SetSize(unsynchronized_data_size);
}
else
{
new_header.SetSize(data_size);
}
// write frame header
uint32_t header_size;
new_header.Serialize(data_ptr, &header_size);
data_ptr += header_size;
if (new_header.Compressed() || new_header.DataLengthIndicated())
{
bytewriter_s byte_writer;
bytewriter_init(&byte_writer, data_ptr, 4);
bytewriter_write_u32_be(&byte_writer, ID3v2::Util::Int32To28(data_size));
data_ptr+=4;
current_length+=4;
}
if (new_header.Unsynchronised())
{
if (Grouped())
current_length += ID3v2::Util::SynchroniseTo(data_ptr++, &group_identity, 1);
current_length += ID3v2::Util::SynchroniseTo(data_ptr, data, data_size);
}
else
{
if (new_header.Grouped())
{
*data_ptr++ = group_identity;
current_length++;
}
memcpy(data_ptr, data, data_size);
current_length += data_size;
}
*written = current_length;
return NErr_Success;
}
const int8_t *ID3v2_4::Frame::GetIdentifier() const
{
return header.GetIdentifier();
}
bool ID3v2_4::Frame::Encrypted() const
{
return header.Encrypted();
}
bool ID3v2_4::Frame::Compressed() const
{
return header.Compressed();
}
bool ID3v2_4::Frame::Grouped() const
{
return header.Grouped();
}
bool ID3v2_4::Frame::ReadOnly() const
{
return header.ReadOnly();
}
bool ID3v2_4::Frame::FrameUnsynchronised() const
{
return header.FrameUnsynchronised();
}
bool ID3v2_4::Frame::DataLengthIndicated() const
{
return header.DataLengthIndicated();
}
bool ID3v2_4::Frame::TagAlterPreservation() const
{
return header.TagAlterPreservation();
}
bool ID3v2_4::Frame::FileAlterPreservation() const
{
return header.FileAlterPreservation();
}
|