2 * Copyright 2005 Timo Hirvonen
8 #include "utf8_encode.h"
26 * if v2 is at beginning _and_ at end then there must be a seek tag at beginning
31 char *v2
[NUM_ID3_KEYS
];
33 unsigned int has_v1
: 1;
34 unsigned int has_v2
: 1;
38 unsigned char ver_major
;
39 unsigned char ver_minor
;
44 struct v2_extended_header
{
48 struct v2_frame_header
{
54 #define V2_HEADER_UNSYNC (1 << 7)
55 #define V2_HEADER_EXTENDED (1 << 6)
56 #define V2_HEADER_EXPERIMENTAL (1 << 5)
57 #define V2_HEADER_FOOTER (1 << 4)
59 #define V2_FRAME_COMPRESSED (1 << 3) /* great idea!!1 */
60 #define V2_FRAME_ENCRYPTHED (1 << 2) /* wow, this is very neat! */
61 #define V2_FRAME_UNSYNC (1 << 1)
62 #define V2_FRAME_LEN_INDICATOR (1 << 0)
66 static const char *genres
[NR_GENRES
] = {
203 "Christian Gangsta Rap",
207 "Contemporary Christian",
219 #define id3_debug(...) d_print(__VA_ARGS__)
221 #define id3_debug(...) do { } while (0)
224 const char * const id3_key_names
[NUM_ID3_KEYS
] = {
235 "replaygain_track_gain",
236 "replaygain_track_peak",
237 "replaygain_album_gain",
238 "replaygain_album_peak"
241 static int utf16_is_special(const uchar uch
)
243 if (UTF16_IS_HSURROGATE(uch
) || UTF16_IS_LSURROGATE(uch
) || UTF16_IS_BOM(uch
))
248 static char *utf16_to_utf8(const unsigned char *buf
, int buf_size
)
253 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
255 while (buf_size
- i
>= 2) {
258 u
= buf
[i
] + (buf
[i
+ 1] << 8);
259 if (u_is_unicode(u
)) {
260 if (utf16_is_special(u
) == 0)
261 u_set_char(out
, &idx
, u
);
270 u_set_char(out
, &idx
, 0);
274 static char *utf16be_to_utf8(const unsigned char *buf
, int buf_size
)
279 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
282 while (buf_size
- i
>= 2) {
285 u
= buf
[i
+ 1] + (buf
[i
] << 8);
286 if (u_is_unicode(u
)) {
287 if (utf16_is_special(u
) == 0)
288 u_set_char(out
, &idx
, u
);
297 u_set_char(out
, &idx
, 0);
301 static int is_v1(const char *buf
)
303 return buf
[0] == 'T' && buf
[1] == 'A' && buf
[2] == 'G';
306 static int u32_unsync(const unsigned char *buf
, uint32_t *up
)
311 for (i
= 0; i
< 4; i
++) {
322 static void get_u32(const unsigned char *buf
, uint32_t *up
)
327 for (i
= 0; i
< 4; i
++) {
335 static void get_u24(const unsigned char *buf
, uint32_t *up
)
340 for (i
= 0; i
< 3; i
++) {
348 static int v2_header_footer_parse(struct v2_header
*header
, const char *buf
)
350 const unsigned char *b
= (const unsigned char *)buf
;
352 header
->ver_major
= b
[3];
353 header
->ver_minor
= b
[4];
354 header
->flags
= b
[5];
355 if (header
->ver_major
== 0xff || header
->ver_minor
== 0xff)
357 return u32_unsync(b
+ 6, &header
->size
);
360 static int v2_header_parse(struct v2_header
*header
, const char *buf
)
362 if (buf
[0] != 'I' || buf
[1] != 'D' || buf
[2] != '3')
364 return v2_header_footer_parse(header
, buf
);
367 static int v2_footer_parse(struct v2_header
*header
, const char *buf
)
369 if (buf
[0] != '3' || buf
[1] != 'D' || buf
[2] != 'I')
371 return v2_header_footer_parse(header
, buf
);
374 static int v2_extended_header_parse(struct v2_extended_header
*header
, const char *buf
)
376 return u32_unsync((const unsigned char *)buf
, &header
->size
);
379 static int is_frame_id_char(char ch
)
381 return (ch
>= 'A' && ch
<= 'Z') || (ch
>= '0' && ch
<= '9');
390 * YYY is frame size excluding this 6 byte header
392 static int v2_2_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
396 for (i
= 0; i
< 3; i
++) {
397 if (!is_frame_id_char(buf
[i
]))
399 header
->id
[i
] = buf
[i
];
402 get_u24((const unsigned char *)(buf
+ 3), &header
->size
);
404 if (header
->size
== 0)
406 id3_debug("%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2], header
->size
);
417 * YYYY is frame size excluding this 10 byte header
420 static int v2_3_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
424 for (i
= 0; i
< 4; i
++) {
425 if (!is_frame_id_char(buf
[i
]))
427 header
->id
[i
] = buf
[i
];
429 get_u32((const unsigned char *)(buf
+ 4), &header
->size
);
430 header
->flags
= (buf
[8] << 8) | buf
[9];
431 if (header
->size
== 0)
433 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
434 header
->id
[3], header
->size
);
438 /* same as 2.3 but header size is sync safe */
439 static int v2_4_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
443 for (i
= 0; i
< 4; i
++) {
444 if (!is_frame_id_char(buf
[i
]))
446 header
->id
[i
] = buf
[i
];
448 if (!u32_unsync((const unsigned char *)(buf
+ 4), &header
->size
))
450 header
->flags
= (buf
[8] << 8) | buf
[9];
451 if (header
->size
== 0)
453 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
454 header
->id
[3], header
->size
);
458 static int read_all(int fd
, char *buf
, size_t size
)
463 int rc
= read(fd
, buf
+ pos
, size
- pos
);
466 if (errno
== EINTR
|| errno
== EAGAIN
)
475 static char *parse_genre(const char *str
)
481 if (strncasecmp(str
, "(RX", 3) == 0)
482 return xstrdup("Remix");
484 if (strncasecmp(str
, "(CR", 3) == 0)
485 return xstrdup("Cover");
492 idx
= strtol(str
, &end
, 10);
494 /* Number parsed but there may be some crap after the number.
495 * I don't care, ID3v2 by definition contains crap.
497 if (idx
>= 0 && idx
< NR_GENRES
)
498 return xstrdup(genres
[idx
]);
502 const char *ptr
= strchr(str
, ')');
505 /* genre name after random crap in parenthesis,
506 * return the genre name */
507 return xstrdup(ptr
+ 1);
512 /* random crap, just return it and wait for a bug report */
516 /* http://www.id3.org/id3v2.4.0-structure.txt */
522 { "TDRC", ID3_DATE
}, // recording date
523 { "TDRL", ID3_DATE
}, // release date
524 { "TDOR", ID3_DATE
}, // original release date
525 { "TSOP", ID3_ARTISTSORT
},
528 { "TPE1", ID3_ARTIST
},
529 { "TALB", ID3_ALBUM
},
530 { "TIT2", ID3_TITLE
},
531 { "TYER", ID3_DATE
},
532 { "TCON", ID3_GENRE
},
533 { "TPOS", ID3_DISC
},
534 { "TRCK", ID3_TRACK
},
535 { "TPE2", ID3_ALBUMARTIST
},
536 { "XSOP", ID3_ARTISTSORT
}, // obsolete
538 /* obsolete frames (2.2.0) */
539 { "TP1", ID3_ARTIST
},
540 { "TAL", ID3_ALBUM
},
541 { "TT2", ID3_TITLE
},
543 { "TCO", ID3_GENRE
},
545 { "TRK", ID3_TRACK
},
550 static int frame_tab_index(const char *id
)
554 for (i
= 0; frame_tab
[i
].key
!= -1; i
++) {
555 if (!strncmp(id
, frame_tab
[i
].name
, 4))
561 static void fix_date(char *buf
)
563 const char *ptr
= buf
;
568 if (ch
>= '0' && ch
<= '9') {
573 // number which length is 4, must be year
574 memmove(buf
, ptr
- 5, 4);
583 static char *decode_str(const char *buf
, int len
, int encoding
)
585 char *in
, *out
= NULL
;
589 case 0x00: /* ISO-8859-1 */
590 in
= xstrndup(buf
, len
);
591 rc
= utf8_encode(in
, id3_default_charset
, &out
);
594 case 0x03: /* UTF-8 */
595 in
= xstrndup(buf
, len
);
596 if (u_is_valid(in
)) {
599 rc
= utf8_encode(in
, id3_default_charset
, &out
);
603 case 0x01: /* UTF-16 */
604 out
= utf16_to_utf8((const unsigned char *)buf
, len
);
606 case 0x02: /* UTF-16BE */
607 out
= utf16be_to_utf8((const unsigned char *)buf
, len
);
613 static void v2_add_frame(ID3
*id3
, struct v2_frame_header
*fh
, const char *buf
)
615 int idx
, encoding
= *buf
++, len
= fh
->size
- 1;
616 enum id3_key key
= NUM_ID3_KEYS
;
622 idx
= frame_tab_index(fh
->id
);
624 key
= frame_tab
[idx
].key
;
625 out
= decode_str(buf
, len
, encoding
);
629 if (key
== ID3_GENRE
) {
632 id3_debug("genre before: '%s'\n", out
);
633 tmp
= parse_genre(out
);
637 if (key
== ID3_DATE
) {
638 id3_debug("date before: '%s'\n", out
);
641 id3_debug("date parsing failed\n");
647 id3_debug("%s '%s'\n", frame_tab
[idx
].name
, out
);
648 } else if (!strncmp(fh
->id
, "TXXX", 4)) {
653 /* TXXX<len><encoding><key><val> */
654 out
= decode_str(buf
, len
, encoding
);
658 id3_debug("TXXX, key = '%s'\n", out
);
659 if (!strcasecmp(out
, "replaygain_track_gain"))
660 key
= ID3_RG_TRACK_GAIN
;
661 if (!strcasecmp(out
, "replaygain_track_peak"))
662 key
= ID3_RG_TRACK_PEAK
;
663 if (!strcasecmp(out
, "replaygain_album_gain"))
664 key
= ID3_RG_ALBUM_GAIN
;
665 if (!strcasecmp(out
, "replaygain_album_peak"))
666 key
= ID3_RG_ALBUM_PEAK
;
667 if (!strcasecmp(out
, "album artist"))
668 key
= ID3_ALBUMARTIST
;
669 if (!strcasecmp(out
, "albumartistsort"))
670 key
= ID3_ALBUMARTISTSORT
;
672 size
= strlen(out
) + 1;
675 if (key
== NUM_ID3_KEYS
)
683 out
= decode_str(buf
, len
, encoding
);
687 id3_debug("TXXX, val = '%s'\n", out
);
697 static void unsync(unsigned char *buf
, int *lenp
)
703 while (s
< len
- 1) {
704 if (buf
[s
] == 0xff && buf
[s
+ 1] == 0x00) {
705 /* 0xff 0x00 -> 0xff */
709 if (s
< len
- 2 && buf
[s
] == 0x00) {
710 /* 0xff 0x00 0x00 -> 0xff 0x00 */
721 d_print("unsyncronization removed %d bytes\n", s
- d
);
725 static int v2_read(ID3
*id3
, int fd
, const struct v2_header
*header
)
730 int frame_header_size
;
732 buf_size
= header
->size
;
733 buf
= xnew(char, buf_size
);
734 rc
= read_all(fd
, buf
, buf_size
);
741 if (header
->flags
& V2_HEADER_EXTENDED
) {
742 struct v2_extended_header ext
;
744 v2_extended_header_parse(&ext
, buf
);
745 if (ext
.size
> buf_size
) {
746 id3_debug("extended header corrupted\n");
750 frame_start
= ext
.size
;
751 /* should check if update flag is set */
754 if (header
->flags
& V2_HEADER_UNSYNC
) {
755 int len
= buf_size
- frame_start
;
757 unsync((unsigned char *)(buf
+ frame_start
), &len
);
758 buf_size
= len
+ frame_start
;
761 frame_header_size
= 10;
762 if (header
->ver_major
== 2)
763 frame_header_size
= 6;
766 while (i
< buf_size
- frame_header_size
) {
767 struct v2_frame_header fh
;
770 if (header
->ver_major
== 2) {
771 if (!v2_2_0_frame_header_parse(&fh
, buf
+ i
))
773 } else if (header
->ver_major
== 3) {
774 if (!v2_3_0_frame_header_parse(&fh
, buf
+ i
))
778 if (!v2_4_0_frame_header_parse(&fh
, buf
+ i
))
782 i
+= frame_header_size
;
783 if (fh
.size
> buf_size
- i
) {
784 id3_debug("frame too big\n");
789 if (fh
.flags
& V2_FRAME_UNSYNC
) {
792 unsync((unsigned char *)(buf
+ i
), &tmp
);
795 v2_add_frame(id3
, &fh
, buf
+ i
);
803 int id3_tag_size(const char *buf
, int buf_size
)
805 struct v2_header header
;
809 if (v2_header_parse(&header
, buf
)) {
810 if (header
.flags
& V2_HEADER_FOOTER
) {
811 /* header + data + footer */
812 id3_debug("v2.%d.%d with footer\n", header
.ver_major
, header
.ver_minor
);
813 return 10 + header
.size
+ 10;
816 id3_debug("v2.%d.%d\n", header
.ver_major
, header
.ver_minor
);
817 return 10 + header
.size
;
819 if (buf_size
>= 3 && is_v1(buf
)) {
828 return xnew0(ID3
, 1);
831 void id3_free(ID3
*id3
)
835 for (i
= 0; i
< NUM_ID3_KEYS
; i
++)
840 int id3_read_tags(ID3
*id3
, int fd
, unsigned int flags
)
845 if (flags
& ID3_V2
) {
846 struct v2_header header
;
849 rc
= read_all(fd
, buf
, 10);
852 if (v2_header_parse(&header
, buf
)) {
853 rc
= v2_read(id3
, fd
, &header
);
856 /* get v1 if needed */
858 /* get v2 from end and optionally v1 */
860 off
= lseek(fd
, -138, SEEK_END
);
863 rc
= read_all(fd
, buf
, 138);
867 if (is_v1(buf
+ 10)) {
868 if (flags
& ID3_V1
) {
869 memcpy(id3
->v1
, buf
+ 10, 128);
872 if (v2_footer_parse(&header
, buf
)) {
873 /* footer at end of file - 128 */
874 off
= lseek(fd
, -(header
.size
+ 138), SEEK_END
);
877 rc
= v2_read(id3
, fd
, &header
);
881 } else if (v2_footer_parse(&header
, buf
+ 128)) {
882 /* footer at end of file */
883 off
= lseek(fd
, -(header
.size
+ 10), SEEK_END
);
886 rc
= v2_read(id3
, fd
, &header
);
893 if (flags
& ID3_V1
) {
894 off
= lseek(fd
, -128, SEEK_END
);
897 rc
= read_all(fd
, id3
->v1
, 128);
900 id3
->has_v1
= is_v1(id3
->v1
);
909 static char *v1_get_str(const char *buf
, int len
)
915 for (i
= len
- 1; i
>= 0; i
--) {
916 if (buf
[i
] != 0 && buf
[i
] != ' ')
921 memcpy(in
, buf
, i
+ 1);
925 if (utf8_encode(in
, id3_default_charset
, &out
))
930 char *id3_get_comment(ID3
*id3
, enum id3_key key
)
934 return xstrdup(id3
->v2
[key
]);
939 return v1_get_str(id3
->v1
+ 33, 30);
941 return v1_get_str(id3
->v1
+ 63, 30);
943 return v1_get_str(id3
->v1
+ 3, 30);
945 return v1_get_str(id3
->v1
+ 93, 4);
948 unsigned char idx
= id3
->v1
[127];
950 if (idx
>= NR_GENRES
)
952 return xstrdup(genres
[idx
]);
958 if (id3
->v1
[125] != 0)
961 snprintf(t
, 4, "%d", ((unsigned char *)id3
->v1
)[126]);