forked from BB9z/LAME-xcframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid3tag.c
1926 lines (1770 loc) · 52.5 KB
/
id3tag.c
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* id3tag.c -- Write ID3 version 1 and 2 tags.
*
* Copyright (C) 2000 Don Melton
* Copyright (C) 2011-2017 Robert Hegemann
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* HISTORY: This source file is part of LAME (see http://www.mp3dev.org)
* and was originally adapted by Conrad Sanderson <[email protected]>
* from mp3info by Ricardo Cerqueira <[email protected]> to write only ID3 version 1
* tags. Don Melton <[email protected]> COMPLETELY rewrote it to support version
* 2 tags and be more conformant to other standards while remaining flexible.
*
* NOTE: See http://id3.org/ for more information about ID3 tag formats.
*/
/* $Id: id3tag.c,v 1.80 2017/08/28 15:39:51 robert Exp $ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef STDC_HEADERS
# include <stddef.h>
# include <stdlib.h>
# include <string.h>
# include <ctype.h>
#else
# ifndef HAVE_STRCHR
# define strchr index
# define strrchr rindex
# endif
char *strchr(), *strrchr();
# ifndef HAVE_MEMCPY
# define memcpy(d, s, n) bcopy ((s), (d), (n))
# endif
#endif
#include "lame.h"
#include "machine.h"
#include "encoder.h"
#include "id3tag.h"
#include "lame_global_flags.h"
#include "util.h"
#include "bitstream.h"
static const char *const genre_names[] = {
/*
* NOTE: The spelling of these genre names is identical to those found in
* Winamp and mp3info.
*/
"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
"Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
"Death Metal", "Pranks", "Soundtrack", "Euro-Techno", "Ambient", "Trip-Hop",
"Vocal", "Jazz+Funk", "Fusion", "Trance", "Classical", "Instrumental",
"Acid", "House", "Game", "Sound Clip", "Gospel", "Noise", "Alternative Rock",
"Bass", "Soul", "Punk", "Space", "Meditative", "Instrumental Pop",
"Instrumental Rock", "Ethnic", "Gothic", "Darkwave", "Techno-Industrial",
"Electronic", "Pop-Folk", "Eurodance", "Dream", "Southern Rock", "Comedy",
"Cult", "Gangsta", "Top 40", "Christian Rap", "Pop/Funk", "Jungle",
"Native US", "Cabaret", "New Wave", "Psychedelic", "Rave",
"Showtunes", "Trailer", "Lo-Fi", "Tribal", "Acid Punk", "Acid Jazz",
"Polka", "Retro", "Musical", "Rock & Roll", "Hard Rock", "Folk",
"Folk-Rock", "National Folk", "Swing", "Fast Fusion", "Bebob", "Latin",
"Revival", "Celtic", "Bluegrass", "Avantgarde", "Gothic Rock",
"Progressive Rock", "Psychedelic Rock", "Symphonic Rock", "Slow Rock",
"Big Band", "Chorus", "Easy Listening", "Acoustic", "Humour", "Speech",
"Chanson", "Opera", "Chamber Music", "Sonata", "Symphony", "Booty Bass",
"Primus", "Porn Groove", "Satire", "Slow Jam", "Club", "Tango", "Samba",
"Folklore", "Ballad", "Power Ballad", "Rhythmic Soul", "Freestyle", "Duet",
"Punk Rock", "Drum Solo", "A Cappella", "Euro-House", "Dance Hall",
"Goa", "Drum & Bass", "Club-House", "Hardcore", "Terror", "Indie",
"BritPop", "Negerpunk", "Polsk Punk", "Beat", "Christian Gangsta",
"Heavy Metal", "Black Metal", "Crossover", "Contemporary Christian",
"Christian Rock", "Merengue", "Salsa", "Thrash Metal", "Anime", "JPop",
"SynthPop"
};
#define GENRE_NAME_COUNT \
((int)(sizeof genre_names / sizeof (const char *const)))
static const int genre_alpha_map[] = {
123, 34, 74, 73, 99, 20, 40, 26, 145, 90, 116, 41, 135, 85, 96, 138, 89, 0,
107, 132, 65, 88, 104, 102, 97, 136, 61, 141, 32, 1, 112, 128, 57, 140, 2,
139, 58, 3, 125, 50, 22, 4, 55, 127, 122, 120, 98, 52, 48, 54, 124, 25, 84,
80, 115, 81, 119, 5, 30, 36, 59, 126, 38, 49, 91, 6, 129, 79, 137, 7, 35,
100, 131, 19, 33, 46, 47, 8, 29, 146, 63, 86, 71, 45, 142, 9, 77, 82, 64,
133, 10, 66, 39, 11, 103, 12, 75, 134, 13, 53, 62, 109, 117, 23, 108, 92,
67, 93, 43, 121, 15, 68, 14, 16, 76, 87, 118, 17, 78, 143, 114, 110, 69, 21,
111, 95, 105, 42, 37, 24, 56, 44, 101, 83, 94, 106, 147, 113, 18, 51, 130,
144, 60, 70, 31, 72, 27, 28
};
#define GENRE_ALPHA_COUNT ((int)(sizeof genre_alpha_map / sizeof (int)))
#define GENRE_INDEX_OTHER 12
#define FRAME_ID(a, b, c, d) \
( ((unsigned long)(a) << 24) \
| ((unsigned long)(b) << 16) \
| ((unsigned long)(c) << 8) \
| ((unsigned long)(d) << 0) )
typedef enum UsualStringIDs { ID_TITLE = FRAME_ID('T', 'I', 'T', '2')
, ID_ARTIST = FRAME_ID('T', 'P', 'E', '1')
, ID_ALBUM = FRAME_ID('T', 'A', 'L', 'B')
, ID_GENRE = FRAME_ID('T', 'C', 'O', 'N')
, ID_ENCODER = FRAME_ID('T', 'S', 'S', 'E')
, ID_PLAYLENGTH = FRAME_ID('T', 'L', 'E', 'N')
, ID_COMMENT = FRAME_ID('C', 'O', 'M', 'M') /* full text string */
} UsualStringIDs;
typedef enum NumericStringIDs { ID_DATE = FRAME_ID('T', 'D', 'A', 'T') /* "ddMM" */
, ID_TIME = FRAME_ID('T', 'I', 'M', 'E') /* "hhmm" */
, ID_TPOS = FRAME_ID('T', 'P', 'O', 'S') /* '0'-'9' and '/' allowed */
, ID_TRACK = FRAME_ID('T', 'R', 'C', 'K') /* '0'-'9' and '/' allowed */
, ID_YEAR = FRAME_ID('T', 'Y', 'E', 'R') /* "yyyy" */
} NumericStringIDs;
typedef enum MiscIDs { ID_TXXX = FRAME_ID('T', 'X', 'X', 'X')
, ID_WXXX = FRAME_ID('W', 'X', 'X', 'X')
, ID_SYLT = FRAME_ID('S', 'Y', 'L', 'T')
, ID_APIC = FRAME_ID('A', 'P', 'I', 'C')
, ID_GEOB = FRAME_ID('G', 'E', 'O', 'B')
, ID_PCNT = FRAME_ID('P', 'C', 'N', 'T')
, ID_AENC = FRAME_ID('A', 'E', 'N', 'C')
, ID_LINK = FRAME_ID('L', 'I', 'N', 'K')
, ID_ENCR = FRAME_ID('E', 'N', 'C', 'R')
, ID_GRID = FRAME_ID('G', 'R', 'I', 'D')
, ID_PRIV = FRAME_ID('P', 'R', 'I', 'V')
, ID_USLT = FRAME_ID('U', 'S', 'L', 'T') /* full text string */
, ID_USER = FRAME_ID('U', 'S', 'E', 'R') /* full text string */
, ID_PCST = FRAME_ID('P', 'C', 'S', 'T') /* iTunes Podcast indicator, only presence important */
, ID_WFED = FRAME_ID('W', 'F', 'E', 'D') /* iTunes Podcast URL as TEXT FRAME !!! violates standard */
} MiscIDs;
static int
frame_id_matches(int id, int mask)
{
int result = 0, i, window = 0xff;
for (i = 0; i < 4; ++i, window <<= 8) {
int const mw = (mask & window);
int const iw = (id & window);
if (mw != 0 && mw != iw) {
result |= iw;
}
}
return result;
}
static int
isFrameIdMatching(int id, int mask)
{
return frame_id_matches(id, mask) == 0 ? 1 : 0;
}
static int
test_tag_spec_flags(lame_internal_flags const *gfc, unsigned int tst)
{
return (gfc->tag_spec.flags & tst) != 0u ? 1 : 0;
}
#if 0
static void
debug_tag_spec_flags(lame_internal_flags * gfc, const char* info)
{
MSGF(gfc, "%s\n", info);
MSGF(gfc, "CHANGED_FLAG : %d\n", test_tag_spec_flags(gfc, CHANGED_FLAG ));
MSGF(gfc, "ADD_V2_FLAG : %d\n", test_tag_spec_flags(gfc, ADD_V2_FLAG ));
MSGF(gfc, "V1_ONLY_FLAG : %d\n", test_tag_spec_flags(gfc, V1_ONLY_FLAG ));
MSGF(gfc, "V2_ONLY_FLAG : %d\n", test_tag_spec_flags(gfc, V2_ONLY_FLAG ));
MSGF(gfc, "SPACE_V1_FLAG : %d\n", test_tag_spec_flags(gfc, SPACE_V1_FLAG));
MSGF(gfc, "PAD_V2_FLAG : %d\n", test_tag_spec_flags(gfc, PAD_V2_FLAG ));
}
#endif
static int
is_lame_internal_flags_null(lame_t gfp)
{
return (gfp && gfp->internal_flags) ? 0 : 1;
}
static int
id3v2_add_ucs2_lng(lame_t gfp, uint32_t frame_id, unsigned short const *desc, unsigned short const *text);
static int
id3v2_add_latin1_lng(lame_t gfp, uint32_t frame_id, char const *desc, char const *text);
static void
copyV1ToV2(lame_t gfp, int frame_id, char const *s)
{
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0;
if (gfc != 0) {
unsigned int flags = gfc->tag_spec.flags;
id3v2_add_latin1_lng(gfp, frame_id, 0, s);
gfc->tag_spec.flags = flags;
#if 0
debug_tag_spec_flags(gfc, "copyV1ToV2");
#endif
}
}
static void
id3v2AddLameVersion(lame_t gfp)
{
char buffer[1024];
const char *b = get_lame_os_bitness();
const char *v = get_lame_version();
const char *u = get_lame_url();
const size_t lenb = strlen(b);
if (lenb > 0) {
sprintf(buffer, "LAME %s version %s (%s)", b, v, u);
}
else {
sprintf(buffer, "LAME version %s (%s)", v, u);
}
copyV1ToV2(gfp, ID_ENCODER, buffer);
}
static void
id3v2AddAudioDuration(lame_t gfp, double ms)
{
SessionConfig_t const *const cfg = &gfp->internal_flags->cfg; /* caller checked pointers */
char buffer[1024];
double const max_ulong = MAX_U_32_NUM;
unsigned long playlength_ms;
ms *= 1000;
ms /= cfg->samplerate_in;
if (ms > max_ulong) {
playlength_ms = max_ulong;
}
else if (ms < 0) {
playlength_ms = 0;
}
else {
playlength_ms = ms;
}
sprintf(buffer, "%lu", playlength_ms);
copyV1ToV2(gfp, ID_PLAYLENGTH, buffer);
}
void
id3tag_genre_list(void (*handler) (int, const char *, void *), void *cookie)
{
if (handler) {
int i;
for (i = 0; i < GENRE_NAME_COUNT; ++i) {
if (i < GENRE_ALPHA_COUNT) {
int j = genre_alpha_map[i];
handler(j, genre_names[j], cookie);
}
}
}
}
#define GENRE_NUM_UNKNOWN 255
void
id3tag_init(lame_t gfp)
{
lame_internal_flags *gfc = 0;
if (is_lame_internal_flags_null(gfp)) {
return;
}
gfc = gfp->internal_flags;
free_id3tag(gfc);
memset(&gfc->tag_spec, 0, sizeof gfc->tag_spec);
gfc->tag_spec.genre_id3v1 = GENRE_NUM_UNKNOWN;
gfc->tag_spec.padding_size = 128;
id3v2AddLameVersion(gfp);
}
void
id3tag_add_v2(lame_t gfp)
{
lame_internal_flags *gfc = 0;
if (is_lame_internal_flags_null(gfp)) {
return;
}
gfc = gfp->internal_flags;
gfc->tag_spec.flags &= ~V1_ONLY_FLAG;
gfc->tag_spec.flags |= ADD_V2_FLAG;
}
void
id3tag_v1_only(lame_t gfp)
{
lame_internal_flags *gfc = 0;
if (is_lame_internal_flags_null(gfp)) {
return;
}
gfc = gfp->internal_flags;
gfc->tag_spec.flags &= ~(ADD_V2_FLAG | V2_ONLY_FLAG);
gfc->tag_spec.flags |= V1_ONLY_FLAG;
}
void
id3tag_v2_only(lame_t gfp)
{
lame_internal_flags *gfc = 0;
if (is_lame_internal_flags_null(gfp)) {
return;
}
gfc = gfp->internal_flags;
gfc->tag_spec.flags &= ~V1_ONLY_FLAG;
gfc->tag_spec.flags |= V2_ONLY_FLAG;
}
void
id3tag_space_v1(lame_t gfp)
{
lame_internal_flags *gfc = 0;
if (is_lame_internal_flags_null(gfp)) {
return;
}
gfc = gfp->internal_flags;
gfc->tag_spec.flags &= ~V2_ONLY_FLAG;
gfc->tag_spec.flags |= SPACE_V1_FLAG;
}
void
id3tag_pad_v2(lame_t gfp)
{
id3tag_set_pad(gfp, 128);
}
void
id3tag_set_pad(lame_t gfp, size_t n)
{
lame_internal_flags *gfc = 0;
if (is_lame_internal_flags_null(gfp)) {
return;
}
gfc = gfp->internal_flags;
gfc->tag_spec.flags &= ~V1_ONLY_FLAG;
gfc->tag_spec.flags |= PAD_V2_FLAG;
gfc->tag_spec.flags |= ADD_V2_FLAG;
gfc->tag_spec.padding_size = (unsigned int)n;
}
static int
hasUcs2ByteOrderMarker(unsigned short bom)
{
if (bom == 0xFFFEu || bom == 0xFEFFu) {
return 1;
}
return 0;
}
static unsigned short
swap_bytes(unsigned short w)
{
return (0xff00u & (w << 8)) | (0x00ffu & (w >> 8));
}
static unsigned short
toLittleEndian(unsigned short bom, unsigned short c)
{
if (bom == 0xFFFEu) {
return swap_bytes(c);
}
return c;
}
static unsigned short
fromLatin1Char(const unsigned short* s, unsigned short c)
{
if (s[0] == 0xFFFEu) {
return swap_bytes(c);
}
return c;
}
static size_t
local_strdup(char **dst, const char *src)
{
if (dst == 0) {
return 0;
}
free(*dst);
*dst = 0;
if (src != 0) {
size_t n;
for (n = 0; src[n] != 0; ++n) { /* calc src string length */
}
if (n > 0) { /* string length without zero termination */
assert(sizeof(*src) == sizeof(**dst));
*dst = lame_calloc(char, n + 1);
if (*dst != 0) {
memcpy(*dst, src, n * sizeof(**dst));
(*dst)[n] = 0;
return n;
}
}
}
return 0;
}
static size_t
local_ucs2_strdup(unsigned short **dst, unsigned short const *src)
{
if (dst == 0) {
return 0;
}
free(*dst); /* free old string pointer */
*dst = 0;
if (src != 0) {
size_t n;
for (n = 0; src[n] != 0; ++n) { /* calc src string length */
}
if (n > 0) { /* string length without zero termination */
assert(sizeof(*src) >= 2);
assert(sizeof(*src) == sizeof(**dst));
*dst = lame_calloc(unsigned short, n + 1);
if (*dst != 0) {
memcpy(*dst, src, n * sizeof(**dst));
(*dst)[n] = 0;
return n;
}
}
}
return 0;
}
static size_t
local_ucs2_strlen(unsigned short const *s)
{
size_t n = 0;
if (s != 0) {
while (*s++) {
++n;
}
}
return n;
}
static size_t
local_ucs2_substr(unsigned short** dst, unsigned short const* src, size_t start, size_t end)
{
size_t const len = 1 + 1 + ((start < end) ? (end - start) : 0);
size_t n = 0;
unsigned short *ptr = lame_calloc(unsigned short, len);
*dst = ptr;
if (ptr == 0 || src == 0) {
return 0;
}
if (hasUcs2ByteOrderMarker(src[0])) {
ptr[n++] = src[0];
if (start == 0) {
++start;
}
}
while (start < end) {
ptr[n++] = src[start++];
}
ptr[n] = 0;
return n;
}
static int
local_ucs2_pos(unsigned short const* str, unsigned short c)
{
int i;
for (i = 0; str != 0 && str[i] != 0; ++i) {
if (str[i] == c) {
return i;
}
}
return -1;
}
static int
local_char_pos(char const* str, char c)
{
int i;
for (i = 0; str != 0 && str[i] != 0; ++i) {
if (str[i] == c) {
return i;
}
}
return -1;
}
static int
maybeLatin1(unsigned short const* text)
{
if (text) {
unsigned short bom = *text++;
while (*text) {
unsigned short c = toLittleEndian(bom, *text++);
if (c > 0x00fe) return 0;
}
}
return 1;
}
static int searchGenre(char const* genre);
static int sloppySearchGenre(char const* genre);
static int
lookupGenre(char const* genre)
{
char *str;
int num = strtol(genre, &str, 10);
/* is the input a string or a valid number? */
if (*str) {
num = searchGenre(genre);
if (num == GENRE_NAME_COUNT) {
num = sloppySearchGenre(genre);
}
if (num == GENRE_NAME_COUNT) {
return -2; /* no common genre text found */
}
}
else {
if ((num < 0) || (num >= GENRE_NAME_COUNT)) {
return -1; /* number unknown */
}
}
return num;
}
static unsigned char *
writeLoBytes(unsigned char *frame, unsigned short const *str, size_t n);
static char*
local_strdup_utf16_to_latin1(unsigned short const* utf16)
{
size_t len = local_ucs2_strlen(utf16);
unsigned char* latin1 = lame_calloc(unsigned char, len+1);
writeLoBytes(latin1, utf16, len);
return (char*)latin1;
}
static int
id3tag_set_genre_utf16(lame_t gfp, unsigned short const* text)
{
lame_internal_flags* gfc = gfp->internal_flags;
int ret;
if (text == 0) {
return -3;
}
if (!hasUcs2ByteOrderMarker(text[0])) {
return -3;
}
if (maybeLatin1(text)) {
char* latin1 = local_strdup_utf16_to_latin1(text);
int num = lookupGenre(latin1);
free(latin1);
if (num == -1) return -1; /* number out of range */
if (num >= 0) { /* common genre found */
gfc->tag_spec.flags |= CHANGED_FLAG;
gfc->tag_spec.genre_id3v1 = num;
copyV1ToV2(gfp, ID_GENRE, genre_names[num]);
return 0;
}
}
ret = id3v2_add_ucs2_lng(gfp, ID_GENRE, 0, text);
if (ret == 0) {
gfc->tag_spec.flags |= CHANGED_FLAG;
gfc->tag_spec.genre_id3v1 = GENRE_INDEX_OTHER;
}
return ret;
}
/*
Some existing options for ID3 tag can be specified by --tv option
as follows.
--tt <value>, --tv TIT2=value
--ta <value>, --tv TPE1=value
--tl <value>, --tv TALB=value
--ty <value>, --tv TYER=value
--tn <value>, --tv TRCK=value
--tg <value>, --tv TCON=value
(although some are not exactly same)*/
int
id3tag_set_albumart(lame_t gfp, const char *image, size_t size)
{
int mimetype = MIMETYPE_NONE;
lame_internal_flags *gfc = 0;
if (is_lame_internal_flags_null(gfp)) {
return 0;
}
gfc = gfp->internal_flags;
if (image != 0) {
unsigned char const *data = (unsigned char const *) image;
/* determine MIME type from the actual image data */
if (2 < size && data[0] == 0xFF && data[1] == 0xD8) {
mimetype = MIMETYPE_JPEG;
}
else if (4 < size && data[0] == 0x89 && strncmp((const char *) &data[1], "PNG", 3) == 0) {
mimetype = MIMETYPE_PNG;
}
else if (4 < size && strncmp((const char *) data, "GIF8", 4) == 0) {
mimetype = MIMETYPE_GIF;
}
else {
return -1;
}
}
if (gfc->tag_spec.albumart != 0) {
free(gfc->tag_spec.albumart);
gfc->tag_spec.albumart = 0;
gfc->tag_spec.albumart_size = 0;
gfc->tag_spec.albumart_mimetype = MIMETYPE_NONE;
}
if (size < 1 || mimetype == MIMETYPE_NONE) {
return 0;
}
gfc->tag_spec.albumart = lame_calloc(unsigned char, size);
if (gfc->tag_spec.albumart != 0) {
memcpy(gfc->tag_spec.albumart, image, size);
gfc->tag_spec.albumart_size = (unsigned int)size;
gfc->tag_spec.albumart_mimetype = mimetype;
gfc->tag_spec.flags |= CHANGED_FLAG;
id3tag_add_v2(gfp);
}
return 0;
}
static unsigned char *
set_4_byte_value(unsigned char *bytes, uint32_t value)
{
int i;
for (i = 3; i >= 0; --i) {
bytes[i] = value & 0xffUL;
value >>= 8;
}
return bytes + 4;
}
static uint32_t
toID3v2TagId(char const *s)
{
unsigned int i, x = 0;
if (s == 0) {
return 0;
}
for (i = 0; i < 4 && s[i] != 0; ++i) {
char const c = s[i];
unsigned int const u = 0x0ff & c;
x <<= 8;
x |= u;
if (c < 'A' || 'Z' < c) {
if (c < '0' || '9' < c) {
return 0;
}
}
}
return x;
}
static uint32_t
toID3v2TagId_ucs2(unsigned short const *s)
{
unsigned int i, x = 0;
unsigned short bom = 0;
if (s == 0) {
return 0;
}
bom = s[0];
if (hasUcs2ByteOrderMarker(bom)) {
++s;
}
for (i = 0; i < 4 && s[i] != 0; ++i) {
unsigned short const c = toLittleEndian(bom, s[i]);
if (c < 'A' || 'Z' < c) {
if (c < '0' || '9' < c) {
return 0;
}
}
x <<= 8;
x |= c;
}
return x;
}
#if 0
static int
isNumericString(uint32_t frame_id)
{
switch (frame_id) {
case ID_DATE:
case ID_TIME:
case ID_TPOS:
case ID_TRACK:
case ID_YEAR:
return 1;
}
return 0;
}
#endif
static int
isMultiFrame(uint32_t frame_id)
{
switch (frame_id) {
case ID_TXXX:
case ID_WXXX:
case ID_COMMENT:
case ID_SYLT:
case ID_APIC:
case ID_GEOB:
case ID_PCNT:
case ID_AENC:
case ID_LINK:
case ID_ENCR:
case ID_GRID:
case ID_PRIV:
return 1;
}
return 0;
}
#if 0
static int
isFullTextString(int frame_id)
{
switch (frame_id) {
case ID_VSLT:
case ID_COMMENT:
return 1;
}
return 0;
}
#endif
static FrameDataNode *
findNode(id3tag_spec const *tag, uint32_t frame_id, FrameDataNode const *last)
{
FrameDataNode *node = last ? last->nxt : tag->v2_head;
while (node != 0) {
if (node->fid == frame_id) {
return node;
}
node = node->nxt;
}
return 0;
}
static void
appendNode(id3tag_spec * tag, FrameDataNode * node)
{
if (tag->v2_tail == 0 || tag->v2_head == 0) {
tag->v2_head = node;
tag->v2_tail = node;
}
else {
tag->v2_tail->nxt = node;
tag->v2_tail = node;
}
}
static void
setLang(char *dst, char const *src)
{
int i;
if (src == 0 || src[0] == 0) {
dst[0] = 'e';
dst[1] = 'n';
dst[2] = 'g';
}
else {
for (i = 0; i < 3 && src && *src; ++i) {
dst[i] = src[i];
}
for (; i < 3; ++i) {
dst[i] = ' ';
}
}
}
static int
isSameLang(char const *l1, char const *l2)
{
char d[3];
int i;
setLang(d, l2);
for (i = 0; i < 3; ++i) {
char a = tolower(l1[i]);
char b = tolower(d[i]);
if (a < ' ')
a = ' ';
if (b < ' ')
b = ' ';
if (a != b) {
return 0;
}
}
return 1;
}
static int
isSameDescriptor(FrameDataNode const *node, char const *dsc)
{
size_t i;
if (node->dsc.enc == 1 && node->dsc.dim > 0) {
return 0;
}
for (i = 0; i < node->dsc.dim; ++i) {
if (!dsc || node->dsc.ptr.l[i] != dsc[i]) {
return 0;
}
}
return 1;
}
static int
isSameDescriptorUcs2(FrameDataNode const *node, unsigned short const *dsc)
{
size_t i;
if (node->dsc.enc != 1 && node->dsc.dim > 0) {
return 0;
}
for (i = 0; i < node->dsc.dim; ++i) {
if (!dsc || node->dsc.ptr.u[i] != dsc[i]) {
return 0;
}
}
return 1;
}
static int
id3v2_add_ucs2(lame_t gfp, uint32_t frame_id, char const *lng, unsigned short const *desc, unsigned short const *text)
{
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0;
if (gfc != 0) {
FrameDataNode *node = findNode(&gfc->tag_spec, frame_id, 0);
char lang[4];
setLang(lang, lng);
if (isMultiFrame(frame_id)) {
while (node) {
if (isSameLang(node->lng, lang)) {
if (isSameDescriptorUcs2(node, desc)) {
break;
}
}
node = findNode(&gfc->tag_spec, frame_id, node);
}
}
if (node == 0) {
node = lame_calloc(FrameDataNode, 1);
if (node == 0) {
return -254; /* memory problem */
}
appendNode(&gfc->tag_spec, node);
}
node->fid = frame_id;
setLang(node->lng, lang);
node->dsc.dim = local_ucs2_strdup(&node->dsc.ptr.u, desc);
node->dsc.enc = 1;
node->txt.dim = local_ucs2_strdup(&node->txt.ptr.u, text);
node->txt.enc = 1;
gfc->tag_spec.flags |= (CHANGED_FLAG | ADD_V2_FLAG);
return 0;
}
return -255;
}
static int
id3v2_add_latin1(lame_t gfp, uint32_t frame_id, char const *lng, char const *desc, char const *text)
{
lame_internal_flags *gfc = gfp != 0 ? gfp->internal_flags : 0;
if (gfc != 0) {
FrameDataNode *node = findNode(&gfc->tag_spec, frame_id, 0);
char lang[4];
setLang(lang, lng);
if (isMultiFrame(frame_id)) {
while (node) {
if (isSameLang(node->lng, lang)) {
if (isSameDescriptor(node, desc)) {
break;
}
}
node = findNode(&gfc->tag_spec, frame_id, node);
}
}
if (node == 0) {
node = lame_calloc(FrameDataNode, 1);
if (node == 0) {
return -254; /* memory problem */
}
appendNode(&gfc->tag_spec, node);
}
node->fid = frame_id;
setLang(node->lng, lang);
node->dsc.dim = local_strdup(&node->dsc.ptr.l, desc);
node->dsc.enc = 0;
node->txt.dim = local_strdup(&node->txt.ptr.l, text);
node->txt.enc = 0;
gfc->tag_spec.flags |= (CHANGED_FLAG | ADD_V2_FLAG);
return 0;
}
return -255;
}
static char const*
id3v2_get_language(lame_t gfp)
{
lame_internal_flags const* gfc = gfp ? gfp->internal_flags : 0;
if (gfc) return gfc->tag_spec.language;
return 0;
}
static int
id3v2_add_ucs2_lng(lame_t gfp, uint32_t frame_id, unsigned short const *desc, unsigned short const *text)
{
char const* lang = id3v2_get_language(gfp);
return id3v2_add_ucs2(gfp, frame_id, lang, desc, text);
}
static int
id3v2_add_latin1_lng(lame_t gfp, uint32_t frame_id, char const *desc, char const *text)
{
char const* lang = id3v2_get_language(gfp);
return id3v2_add_latin1(gfp, frame_id, lang, desc, text);
}
static int
id3tag_set_userinfo_latin1(lame_t gfp, uint32_t id, char const *fieldvalue)
{
char const separator = '=';
int rc = -7;
int a = local_char_pos(fieldvalue, separator);
if (a >= 0) {
char* dup = 0;
local_strdup(&dup, fieldvalue);
dup[a] = 0;
rc = id3v2_add_latin1_lng(gfp, id, dup, dup+a+1);
free(dup);
}
return rc;
}
static int
id3tag_set_userinfo_ucs2(lame_t gfp, uint32_t id, unsigned short const *fieldvalue)
{
unsigned short const separator = fromLatin1Char(fieldvalue,'=');
int rc = -7;
size_t b = local_ucs2_strlen(fieldvalue);
int a = local_ucs2_pos(fieldvalue, separator);
if (a >= 0) {
unsigned short* dsc = 0, *val = 0;
local_ucs2_substr(&dsc, fieldvalue, 0, a);
local_ucs2_substr(&val, fieldvalue, a+1, b);
rc = id3v2_add_ucs2_lng(gfp, id, dsc, val);
free(dsc);
free(val);
}
return rc;
}
int
id3tag_set_textinfo_utf16(lame_t gfp, char const *id, unsigned short const *text)
{
uint32_t const frame_id = toID3v2TagId(id);
if (frame_id == 0) {
return -1;