forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
id3tag.h
797 lines (660 loc) · 30.5 KB
/
id3tag.h
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
787
788
789
790
791
792
793
794
795
796
797
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
id3tag.h - v0.1 - Read/write ID3 tags from/to mp3 files in C/C++.
Do this:
#define ID3TAG_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef id3tag_h
#define id3tag_h
#ifndef ID3TAG_U32
#define ID3TAG_U32 unsigned int
#endif
#define ID3TAG_PIC_TYPE_COVER_FRONT 3
#define ID3TAG_PIC_TYPE_COVER_BACK 4
#define ID3TAG_PIC_TYPE_LEAFLET_PAGE 5
#define ID3TAG_PIC_TYPE_MEDIA 6
#define ID3TAG_PIC_TYPE_ARTIST 8
#define ID3TAG_PIC_TYPE_ARTIST_LOGO 13
typedef struct id3tag_pic_t
{
int pic_type;
char const* description;
char const* mime_type;
void const* data;
size_t size;
} id3tag_pic_t;
typedef struct id3tag_t
{
char const* title;
char const* artist;
char const* album_artist;
char const* album;
char const* sort_title;
char const* sort_artist;
char const* sort_album_artist;
char const* sort_album;
char const* genre;
char const* year;
char const* track;
char const* tracks;
char const* disc;
char const* discs;
char const* compilation;
int track_length;
int play_counter;
int pics_count;
id3tag_pic_t const* pics;
} id3tag_t;
#define ID3TAG_ALL_FIELDS ( 0xFFFFFFFF )
#define ID3TAG_FIELD_TITLE ( 1U )
#define ID3TAG_FIELD_ARTIST ( 1U << 1U )
#define ID3TAG_FIELD_ALBUM_ARTIST ( 1U << 2U )
#define ID3TAG_FIELD_ALBUM ( 1U << 3U )
#define ID3TAG_FIELD_SORT_TITLE ( 1U << 4U )
#define ID3TAG_FIELD_SORT_ARTIST ( 1U << 5U )
#define ID3TAG_FIELD_SORT_ALBUM_ARTIST ( 1U << 6U )
#define ID3TAG_FIELD_SORT_ALBUM ( 1U << 7U )
#define ID3TAG_FIELD_GENRE ( 1U << 8U )
#define ID3TAG_FIELD_YEAR ( 1U << 9U )
#define ID3TAG_FIELD_TRACK ( 1U << 10U )
#define ID3TAG_FIELD_TRACKS ( 1U << 11U )
#define ID3TAG_FIELD_DISC ( 1U << 12U )
#define ID3TAG_FIELD_DISCS ( 1U << 13U )
#define ID3TAG_FIELD_COMPILATION ( 1U << 14U )
#define ID3TAG_FIELD_PLAY_COUNTER ( 1U << 15U )
#define ID3TAG_FIELD_PICS ( 1U << 16U )
#define ID3TAG_FIELD_TRACK_LENGTH ( 1U << 17U )
size_t id3tag_size( void const* first_ten_bytes );
id3tag_t* id3tag_load( void const* data, size_t size, ID3TAG_U32 fields, void* memctx );
id3tag_t* id3tag_load_id3v1( void const* data, size_t size, void* memctx );
void id3tag_free( id3tag_t* id3tag );
size_t id3tag_save( id3tag_t const* id3tag, ID3TAG_U32 fields, void const* data, size_t capacity );
#endif /* id3tag_h */
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifdef ID3TAG_IMPLEMENTATION
#undef ID3TAG_IMPLEMENTATION
#ifndef ID3TAG_U8
#define ID3TAG_U8 unsigned char
#endif
#ifndef ID3TAG_U16
#define ID3TAG_U16 unsigned short
#endif
#ifndef ID3TAG_MALLOC
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#define ID3TAG_MALLOC( ctx, size ) ( malloc( size ) )
#define ID3TAG_FREE( ctx, ptr ) ( free( ptr ) )
#endif
typedef struct id3tag_internal_t
{
id3tag_t tag;
void* memctx;
int pics_capacity;
} id3tag_internal_t;
int id3tag_internal_utf16_to_utf8( ID3TAG_U16* str, int len, char* out, int capacity, int endian_swap )
{
ID3TAG_U16* in_ptr = str;
char* out_ptr = out;
char* out_end = out + capacity;
for( int i = 0; i < len; ++i )
{
ID3TAG_U32 cp = (ID3TAG_U32) *in_ptr++;
if( endian_swap ) cp = ( ( cp & 0xff00 ) >> 8 ) | ( ( cp & 0x00ff ) << 8 );
if( cp == 0x0000 ) break;
if( cp >= 0xd800u && cp <= 0xdbffu )
cp = ( cp << 10 ) + (*in_ptr++) + ( 0x10000u - ( 0xd800u << 10 ) - 0xdc00u );
if( cp < 0x80 )
{
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) cp ); else ++out_ptr;
}
else if( cp < 0x800 )
{
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp >> 6) | 0xc0 ) ); else ++out_ptr;
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp & 0x3f ) | 0x80 ) ); else ++out_ptr;
}
else if (cp < 0x10000)
{
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp >> 12 ) | 0xe0 ) ); else ++out_ptr;
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( ( cp >> 6 ) & 0x3f ) | 0x80 ) ); else ++out_ptr;
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp & 0x3f ) | 0x80 ) ); else ++out_ptr;
}
else
{
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp >> 18 ) | 0xf0 ) ); else ++out_ptr;
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( ( cp >> 12 ) & 0x3f ) | 0x80 ) ); else ++out_ptr;
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( ( cp >> 6 ) & 0x3f ) | 0x80 ) ); else ++out_ptr;
if( out && out_ptr < out_end ) *out_ptr++ = (char)( (ID3TAG_U8) ( ( cp & 0x3f ) | 0x80 ) ); else ++out_ptr;
}
}
if( out && out_ptr < out_end ) *out_ptr++ = '\0'; else ++out_ptr;
int out_len = out ? (int)( out_ptr - out ) : (int)(uintptr_t)( out_ptr );
return out_len;
}
// TODO: Proper unicode support with a font that can handle it, instead of doing this
char* utf8_to_latin1( char* s )
{
for( int readIndex = 0, writeIndex = 0 ; ; writeIndex++ )
{
if( s[ readIndex ] == 0 ) { s[ writeIndex ] = 0; break; }
int len;
if ( (ID3TAG_U8) s[ readIndex ] < 0x80 ) len = 1;
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x20 ) == 0 ) len = 2;
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x10 ) == 0 ) len = 3;
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x08 ) == 0 ) len = 4;
else if ( ( (ID3TAG_U8) s[ readIndex ] & 0x04 ) == 0 ) len = 5;
else len = 6;
char c = '\0';
if( len == 1 )
{
s[ writeIndex ] = s[ readIndex++ ];
}
else
{
int v = ( s[ readIndex++ ] & ( 0xff >> ( len + 1 ) ) ) << ( ( len - 1 ) * 6 );
for( len-- ; len > 0 ; len-- ) v |= ( (ID3TAG_U8)( s[ readIndex++ ] ) - 0x80 ) << ( ( len - 1 ) * 6 );
if( v == 8216 ) v = '\'';
if( v == 8217 ) v = '\'';
if( v == 9733 ) v = '*';
if( v == 8208 ) v = '-';
if( v == 8211 ) v = '-';
if( v == 8220 ) v = '"';
if( v == 8221 ) v = 'r';
if( v == 8482 )
v = ' '; // TM
if( v == 263 ) v = 'c';
if( v == 65533 ) v = '?';
if( v == 8230 ) { // '...'
s[ writeIndex++ ] = '.';
s[ writeIndex++ ] = '.';
v = '.';
}
if( v == 339 ) {
s[ writeIndex++ ] = 'o';
v = 'e';
}
if( v > 0xff ) {
v = ' ';
}
c = (char) v;
s[ writeIndex ] = c;
}
}
return s;
}
size_t id3tag_size( void const* first_ten_bytes )
{
ID3TAG_U8* ptr = (ID3TAG_U8*) first_ten_bytes;
// verify the header to see if this is a valid ID3 tag
if( *ptr++ != 0x49 || *ptr++ != 0x44 || *ptr++ != 0x33 || *ptr++ >= 0xff || *ptr++ >= 0xff ) return 0;
++ptr; // skip flags field
ID3TAG_U32 size = 0;
for( int i = 0; i < 4; ++i )
{
ID3TAG_U8 b = *ptr++;
if( b >= 0x80 ) return 0; // Not a valid ID3
size |= ( (ID3TAG_U32) b ) << ( 7 * ( 3 - i ) );
}
return (size_t) size + 10;
}
char* id3tag_internal_get_string( int encoding, ID3TAG_U8* ptr, int size, void* memctx, int* bytes_consumed )
{
(void) memctx;
if( encoding == 0) // Latin1
{
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t)( size + 1 ) );
memcpy( str, ptr, (size_t) size );
str[ size ] = '\0';
if( bytes_consumed ) *bytes_consumed = (int) strlen( str ) + 1;
return str;
}
else if( encoding == 1 ) // UTF16 with BOM
{
ID3TAG_U16 bom = *( (ID3TAG_U16*) ptr );
if( bom == 0xfffe || bom == 0xfeff ) { ptr += 2; size -= 2; }
int endian_swap = ( bom == 0xfffe );
int len = id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, 0, 0, endian_swap );
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t) len );
id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, str, len, endian_swap );
if( bytes_consumed )
{
*bytes_consumed = ( ( bom == 0xfffe || bom == 0xfeff ) ? 2 : 0 );
ID3TAG_U16* s = (ID3TAG_U16*) ptr;
while( *s++ ) *bytes_consumed = ( *bytes_consumed ) + 2;
*bytes_consumed = ( *bytes_consumed ) + 2;
}
utf8_to_latin1( str );
return str;
}
else if( encoding == 2 ) // UTF16BE without BOM
{
int endian_swap = 1;
int len = id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, 0, 0, endian_swap );
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t) len );
id3tag_internal_utf16_to_utf8( (ID3TAG_U16*) ptr, size / 2, str, len, endian_swap );
if( bytes_consumed )
{
*bytes_consumed = 0;
ID3TAG_U16* s = (ID3TAG_U16*) ptr;
while( *s++ ) *bytes_consumed = ( *bytes_consumed ) + 2;
*bytes_consumed = ( *bytes_consumed ) + 2;
}
utf8_to_latin1( str );
return str;
}
else if( encoding == 3 ) // UTF8
{
char* str = (char*)ID3TAG_MALLOC( memctx, (size_t) size + 1 );
memcpy( str, ptr, (size_t) size );
str[ size ] = '\0';
if( bytes_consumed ) *bytes_consumed = (int) strlen( str ) + 1;
utf8_to_latin1( str );
return str;
}
else
{
return 0;
}
}
id3tag_t* id3tag_load_id3v1( void const* data, size_t size, void* memctx )
{
if( size != 128 ) return NULL;
char const* ptr = (char const*) data;
if( strncmp( ptr, "TAG", 3 ) != 0 ) return NULL;
ptr += 3;
id3tag_internal_t* tag = (id3tag_internal_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_internal_t ) + size );
memset( tag, 0, sizeof( id3tag_internal_t ) );
memcpy( tag + 1, data, size );
tag->memctx = memctx;
tag->tag.title = (char*) ID3TAG_MALLOC( memctx, 31 );
strncpy( (char*) tag->tag.title, ptr, 30 );
( (char*) tag->tag.title )[ 30 ] = '\0';
ptr += 30;
tag->tag.artist = (char*) ID3TAG_MALLOC( memctx, 31 );
strncpy( (char*) tag->tag.artist, ptr, 30 );
( (char*) tag->tag.artist )[ 30 ] = '\0';
ptr += 30;
tag->tag.album = (char*) ID3TAG_MALLOC( memctx, 31 );
strncpy( (char*) tag->tag.album, ptr, 30 );
( (char*) tag->tag.album )[ 30 ] = '\0';
ptr += 30;
tag->tag.year = (char*) ID3TAG_MALLOC( memctx, 5 );
strncpy( (char*) tag->tag.year, ptr, 4 );
( (char*) tag->tag.year)[ 4 ] = '\0';
ptr += 4;
char const* comment = ptr;
if( comment[ 28 ] == 0 && (int)(ID3TAG_U8)( ptr[ 29 ] ) > 0 )
{
tag->tag.track = (char*) ID3TAG_MALLOC( memctx, 30 );
sprintf( (char*) tag->tag.track, "%d", (int)(ID3TAG_U8)( ptr[ 29 ] ) );
}
ptr += 30;
int genre = (int)(ID3TAG_U8)( *ptr );
if( genre != 255 )
{
tag->tag.genre = (char*) ID3TAG_MALLOC( memctx, 30 );
sprintf( (char*) tag->tag.genre, "(%d)", genre );
}
return (id3tag_t*) tag;
}
id3tag_t* id3tag_load( void const* data, size_t size, ID3TAG_U32 fields, void* memctx )
{
enum fields_t
{
FIELD_TITLE,
FIELD_ARTIST,
FIELD_ALBUM_ARTIST,
FIELD_ALBUM,
FIELD_SORT_TITLE,
FIELD_SORT_ARTIST,
FIELD_SORT_ALBUM_ARTIST,
FIELD_SORT_ALBUM,
FIELD_GENRE,
FIELD_YEAR,
FIELD_TRACK,
FIELD_TRACKS,
FIELD_DISC,
FIELD_DISCS,
FIELD_COMPILATION,
FIELD_PLAY_COUNTER,
FIELD_TRACK_LENGTH,
FIELDCOUNT,
};
bool fields_found[ FIELDCOUNT ] = { false };
int fields_requested = 0;
if( fields & ID3TAG_FIELD_TITLE ) ++fields_requested;
if( fields & ID3TAG_FIELD_ARTIST ) ++fields_requested;
if( fields & ID3TAG_FIELD_ALBUM_ARTIST ) ++fields_requested;
if( fields & ID3TAG_FIELD_ALBUM ) ++fields_requested;
if( fields & ID3TAG_FIELD_SORT_TITLE ) ++fields_requested;
if( fields & ID3TAG_FIELD_SORT_ARTIST ) ++fields_requested;
if( fields & ID3TAG_FIELD_SORT_ALBUM_ARTIST ) ++fields_requested;
if( fields & ID3TAG_FIELD_SORT_ALBUM ) ++fields_requested;
if( fields & ID3TAG_FIELD_GENRE ) ++fields_requested;
if( fields & ID3TAG_FIELD_YEAR ) ++fields_requested;
if( fields & ID3TAG_FIELD_TRACK ) ++fields_requested;
if( fields & ID3TAG_FIELD_TRACKS ) ++fields_requested;
if( fields & ID3TAG_FIELD_DISC ) ++fields_requested;
if( fields & ID3TAG_FIELD_DISCS ) ++fields_requested;
if( fields & ID3TAG_FIELD_COMPILATION ) ++fields_requested;
if( fields & ID3TAG_FIELD_PLAY_COUNTER ) ++fields_requested;
if( fields & ID3TAG_FIELD_TRACK_LENGTH ) ++fields_requested;
ID3TAG_U8* ptr = (ID3TAG_U8*) data;
// validate data
if( size < 10 ) return NULL; // Not a valid ID3
size_t tag_size = id3tag_size( data );
if( tag_size == 0 ) return NULL; // Not a valid ID3
if( size < tag_size ) return NULL; // Not enough data
id3tag_internal_t* tag = (id3tag_internal_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_internal_t ) + size );
memset( tag, 0, sizeof( id3tag_internal_t ) );
memcpy( tag + 1, data, size );
tag->memctx = memctx;
ptr += 3; // skip past "ID3" marker
// read version and flags
int version = *ptr++;
int revision = *ptr++; (void) revision;
ID3TAG_U8 header_flags = *ptr++;
(void) header_flags; // TODO: inspect flags
ptr += 4; // skip past size field
ID3TAG_U8* end = ( (ID3TAG_U8*) data ) + tag_size;
while( ptr <= ( end - 10 ) )
{
if( ( fields & ID3TAG_FIELD_PICS ) == 0 )
{
int fields_found_count = 0;
for( int i = 0; i < FIELDCOUNT; ++i ) if( fields_found[ i ] ) ++fields_found_count;
if( fields_found_count == fields_requested ) break;
}
if( version >= 3 && ptr[ 0 ] == 0 && ptr[ 1 ] == 0 && ptr[ 2 ] == 0 && ptr[ 3 ] == 0 ) break; // end of frames (in padding)
if( version < 3 && ptr[ 0 ] == 0 && ptr[ 1 ] == 0 && ptr[ 2 ] == 0 ) break; // end of frames (in padding)
char frame_id[ 5 ]= { (char) *ptr++, (char) *ptr++, (char) *ptr++, '\0', '\0' };
if( version >= 3 ) frame_id[ 3 ] = (char) *ptr++;
int frame_size = 0;
if( version >= 4 )
for( int i = 3; i >= 0 ; --i ) frame_size |= ( (ID3TAG_U32) ( ( *ptr++ ) ) ) << ( 7 * i );
else if( version >= 3 )
for( int i = 3; i >= 0 ; --i ) frame_size |= ( (ID3TAG_U32) ( ( *ptr++ ) ) ) << ( 8 * i );
else
for( int i = 2; i >= 0 ; --i ) frame_size |= ( (ID3TAG_U32) ( ( *ptr++ ) ) ) << ( 8 * i );
if( frame_size <= 0 ) break; // end of frames
if( frame_size >= ( end - ptr ) ) break; // invalid frame
ID3TAG_U16 flags = 0;
if( version >= 3 ) flags = (ID3TAG_U16)( ( ( (ID3TAG_U16)( *ptr++ ) ) << 8 ) | ( *ptr++ ) );
if( ( fields & ID3TAG_FIELD_TITLE ) && ( strcmp( frame_id , "TIT2" ) == 0 || strcmp( frame_id , "TT2" ) == 0 ) )
{
if( !tag->tag.title ) tag->tag.title = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_TITLE ] = true;
}
if( ( fields & ID3TAG_FIELD_ARTIST ) && ( strcmp( frame_id , "TPE1" ) == 0 || strcmp( frame_id , "TP1" ) == 0 ) )
{
if( !tag->tag.artist ) tag->tag.artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_ARTIST ] = true;
}
if( ( fields & ID3TAG_FIELD_ALBUM_ARTIST) && ( strcmp( frame_id , "TPE2" ) == 0 || strcmp( frame_id , "TP2" ) == 0 ) )
{
if( !tag->tag.album_artist ) tag->tag.album_artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_ALBUM_ARTIST ] = true;
}
if( ( fields & ID3TAG_FIELD_ALBUM ) && ( strcmp( frame_id , "TALB" ) == 0 || strcmp( frame_id , "TAL" ) == 0 ) )
{
if( !tag->tag.album ) tag->tag.album = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_ALBUM ] = true;
}
if( ( fields & ID3TAG_FIELD_GENRE ) && ( strcmp( frame_id , "TCON" ) == 0 || strcmp( frame_id , "TCO" ) == 0 ) )
{
if( !tag->tag.genre ) tag->tag.genre = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_GENRE ] = true;
}
if( ( fields & ID3TAG_FIELD_YEAR ) && ( strcmp( frame_id , "TYER" ) == 0 || strcmp( frame_id , "TYE" ) == 0 ) )
{
if( !tag->tag.year ) tag->tag.year = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_YEAR ] = true;
}
if( ( fields & ID3TAG_FIELD_COMPILATION ) && ( strcmp( frame_id , "TCMP" ) == 0 || strcmp( frame_id , "TCP" ) == 0 ) )
{
if( !tag->tag.compilation ) tag->tag.compilation = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_COMPILATION ] = true;
}
if( ( fields & ID3TAG_FIELD_SORT_TITLE ) && ( strcmp( frame_id , "TSOT" ) == 0 || strcmp( frame_id , "TST" ) == 0 ) )
{
if( !tag->tag.sort_title ) tag->tag.sort_title = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_SORT_TITLE ] = true;
}
if( ( fields & ID3TAG_FIELD_SORT_ARTIST ) && ( strcmp( frame_id , "TSOP" ) == 0 || strcmp( frame_id , "TSP" ) == 0 ) )
{
if( !tag->tag.sort_artist ) tag->tag.sort_artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_SORT_ARTIST ] = true;
}
if( ( fields & ID3TAG_FIELD_SORT_ALBUM ) && ( strcmp( frame_id , "TSOA" ) == 0 || strcmp( frame_id , "TSA" ) == 0 ) )
{
if( !tag->tag.sort_album ) tag->tag.sort_album = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_SORT_ALBUM ] = true;
}
if( ( fields & ID3TAG_FIELD_SORT_ALBUM_ARTIST ) && ( strcmp( frame_id , "TSO2" ) == 0 || strcmp( frame_id , "TS2" ) == 0 ) )
{
if( !tag->tag.sort_album_artist ) tag->tag.sort_album_artist = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
fields_found[ FIELD_SORT_ALBUM_ARTIST ] = true;
}
if( ( fields & ID3TAG_FIELD_TRACK_LENGTH ) && ( strcmp( frame_id , "TLEN" ) == 0 || strcmp( frame_id , "TLE" ) == 0 ) )
{
char* str = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
if( tag->tag.track_length == 0 ) tag->tag.track_length = atoi( str );
ID3TAG_FREE( memctx, str );
if( tag->tag.track_length > 0 ) fields_found[ FIELD_TRACK_LENGTH ] = true;
}
if( ( ( fields & ID3TAG_FIELD_TRACK ) || ( fields & ID3TAG_FIELD_TRACKS ) ) &&
( strcmp( frame_id , "TRCK" ) == 0 || strcmp( frame_id , "TRK" ) == 0 ) )
{
char* first_str = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
char* separator = strchr( first_str, '/' );
if( separator ) *separator = '\0';
char* second_str = separator ? ( separator + 1 ) : NULL;
int first = atoi( first_str );
int second = second_str ? atoi( second_str ) : 0;
ID3TAG_FREE( memctx, first_str );
char buf[ 64 ];
if( first > 0 )
{
sprintf( buf, "%d", first );
if( strlen( buf ) > 0 && !tag->tag.track )
{
tag->tag.track = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
strcpy( (char*) tag->tag.track, buf );
fields_found[ FIELD_TRACK ] = true;
}
}
if( second > 0 )
{
sprintf( buf, "%d", second );
if( strlen( buf ) > 0 && !tag->tag.tracks )
{
tag->tag.tracks = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
strcpy( (char*) tag->tag.tracks, buf );
fields_found[ FIELD_TRACKS ] = true;
}
}
}
if( ( ( fields & ID3TAG_FIELD_DISC ) || ( fields & ID3TAG_FIELD_DISCS ) ) &&
( strcmp( frame_id , "TPOS" ) == 0 || strcmp( frame_id , "TPA" ) == 0 ) )
{
char* first_str = id3tag_internal_get_string( *ptr, ptr + 1, frame_size - 1, memctx, NULL );
char* separator = strchr( first_str, '/' );
if( separator ) *separator = '\0';
char* second_str = separator ? ( separator + 1 ) : NULL;
int first = atoi( first_str );
int second = second_str ? atoi( second_str ) : 0;
ID3TAG_FREE( memctx, first_str );
char buf[ 64 ];
if( first > 0 )
{
sprintf( buf, "%d", first );
if( strlen( buf ) > 0 && !tag->tag.disc )
{
tag->tag.disc = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
strcpy( (char*) tag->tag.disc, buf );
fields_found[ FIELD_DISC ] = true;
}
}
if( second > 0 )
{
sprintf( buf, "%d", second );
if( strlen( buf ) > 0 && !tag->tag.discs )
{
tag->tag.discs = (char*) ID3TAG_MALLOC( memctx, strlen( buf ) + 1 );
strcpy( (char*) tag->tag.discs, buf );
fields_found[ FIELD_DISCS ] = true;
}
}
}
if( ( fields & ID3TAG_FIELD_PICS ) && ( strcmp( frame_id , "APIC" ) == 0 || strcmp( frame_id , "PIC" ) == 0 ) )
{
if( !tag->tag.pics )
{
tag->pics_capacity = 16;
tag->tag.pics = (id3tag_pic_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_pic_t ) * tag->pics_capacity );
tag->tag.pics_count = 0;
}
if( tag->tag.pics_count >= tag->pics_capacity )
{
tag->pics_capacity *= 2;
id3tag_pic_t* new_pics = (id3tag_pic_t*) ID3TAG_MALLOC( memctx, sizeof( id3tag_pic_t ) * tag->pics_capacity );
memcpy( new_pics, tag->tag.pics, sizeof( id3tag_pic_t ) * tag->tag.pics_count );
ID3TAG_FREE( memctx, (id3tag_pic_t*)tag->tag.pics );
tag->tag.pics = new_pics;
}
int encoding = *ptr++;
--frame_size;
id3tag_pic_t* pic = (id3tag_pic_t*) &tag->tag.pics[ tag->tag.pics_count++ ];
memset( pic, 0, sizeof( *pic ) );
if( version < 3 )
{
char str[ 4 ] = { (char) *ptr++, (char) *ptr++, (char) *ptr++, '\0' };
frame_size -= 3;
pic->mime_type = (char*) ID3TAG_MALLOC( memctx, 32 );
if( strcmp( str, "JPG" ) == 0 )
strcpy( (char*) pic->mime_type, "image/jpeg" );
else if( strcmp( str, "PNG" ) == 0 )
strcpy( (char*) pic->mime_type, "image/png" );
else if( strcmp( str, "GIF" ) == 0 )
strcpy( (char*) pic->mime_type, "image/gif" );
else if( strcmp( str, "BMP" ) == 0 )
strcpy( (char*) pic->mime_type, "image/bmp" );
else
strcpy( (char*) pic->mime_type, str );
}
else
{
int bytes = 0;
pic->mime_type = id3tag_internal_get_string( encoding, ptr, frame_size, memctx, &bytes );
ptr += bytes;
frame_size -= bytes;
}
pic->pic_type = *ptr++;
--frame_size;
if( pic->pic_type != 0 )
ptr = ptr;
int bytes = 0;
pic->description = id3tag_internal_get_string( encoding, ptr, frame_size, memctx, &bytes );
ptr += bytes;
frame_size -= bytes;
if( frame_size )
{
pic->data = ID3TAG_MALLOC( memctx, (size_t) frame_size );
memcpy( (void*) pic->data, ptr, (size_t) frame_size );
pic->size = (size_t) frame_size;
}
else
{
if( pic->mime_type ) ID3TAG_FREE( memctx, (char*) pic->mime_type );
if( pic->description ) ID3TAG_FREE( memctx, (char*) pic->description );
--tag->tag.pics_count;
}
}
ptr += frame_size;
}
return (id3tag_t*) tag;
}
void id3tag_free( id3tag_t* id3tag )
{
id3tag_internal_t* internal = (id3tag_internal_t*) id3tag;
if( internal->tag.title) ID3TAG_FREE( internal->memctx, (char*) internal->tag.title );
if( internal->tag.artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.artist );
if( internal->tag.album_artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.album_artist );
if( internal->tag.album ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.album );
if( internal->tag.sort_title) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_title );
if( internal->tag.sort_artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_artist );
if( internal->tag.sort_album_artist ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_album_artist );
if( internal->tag.sort_album ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.sort_album );
if( internal->tag.genre ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.genre );
if( internal->tag.year ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.year );
if( internal->tag.track ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.track );
if( internal->tag.tracks ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.tracks );
if( internal->tag.disc ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.disc );
if( internal->tag.discs ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.discs );
if( internal->tag.compilation ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.compilation );
if( internal->tag.pics )
{
for( int i = 0; i < internal->tag.pics_count; ++i )
{
if( internal->tag.pics[ i ].description ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.pics[ i ].description );
if( internal->tag.pics[ i ].mime_type ) ID3TAG_FREE( internal->memctx, (char*) internal->tag.pics[ i ].mime_type );
if( internal->tag.pics[ i ].data ) ID3TAG_FREE( internal->memctx, (void*) internal->tag.pics[ i ].data );
}
if( internal->tag.pics ) ID3TAG_FREE( internal->memctx, (id3tag_pic_t*) internal->tag.pics );
}
ID3TAG_FREE( internal->memctx, id3tag );
}
size_t id3tag_save( id3tag_t const* id3tag, ID3TAG_U32 fields, void const* data, size_t capacity )
{
(void) id3tag, fields, data, capacity;
return 0;
}
#endif /* ID3TAG_IMPLEMENTATION */
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2018 Mattias Gustavsson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/