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
30 unsigned char ver_major
;
31 unsigned char ver_minor
;
36 struct v2_extended_header
{
40 struct v2_frame_header
{
46 #define V2_HEADER_UNSYNC (1 << 7)
47 #define V2_HEADER_EXTENDED (1 << 6)
48 #define V2_HEADER_EXPERIMENTAL (1 << 5)
49 #define V2_HEADER_FOOTER (1 << 4)
51 #define V2_FRAME_COMPRESSED (1 << 3) /* great idea!!1 */
52 #define V2_FRAME_ENCRYPTHED (1 << 2) /* wow, this is very neat! */
53 #define V2_FRAME_UNSYNC (1 << 1)
54 #define V2_FRAME_LEN_INDICATOR (1 << 0)
58 static const char *genres
[NR_GENRES
] = {
195 "Christian Gangsta Rap",
199 "Contemporary Christian",
211 #define id3_debug(...) d_print(__VA_ARGS__)
213 #define id3_debug(...) do { } while (0)
216 const char * const id3_key_names
[NUM_ID3_KEYS
] = {
227 "replaygain_track_gain",
228 "replaygain_track_peak",
229 "replaygain_album_gain",
230 "replaygain_album_peak"
233 static int utf16_is_special(const uchar uch
)
235 if (UTF16_IS_HSURROGATE(uch
) || UTF16_IS_LSURROGATE(uch
) || UTF16_IS_BOM(uch
))
240 static char *utf16_to_utf8(const unsigned char *buf
, int buf_size
)
245 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
247 while (buf_size
- i
>= 2) {
250 u
= buf
[i
] + (buf
[i
+ 1] << 8);
251 if (u_is_unicode(u
)) {
252 if (utf16_is_special(u
) == 0)
253 u_set_char(out
, &idx
, u
);
262 u_set_char(out
, &idx
, 0);
266 static char *utf16be_to_utf8(const unsigned char *buf
, int buf_size
)
271 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
274 while (buf_size
- i
>= 2) {
277 u
= buf
[i
+ 1] + (buf
[i
] << 8);
278 if (u_is_unicode(u
)) {
279 if (utf16_is_special(u
) == 0)
280 u_set_char(out
, &idx
, u
);
289 u_set_char(out
, &idx
, 0);
293 static int is_v1(const char *buf
)
295 return buf
[0] == 'T' && buf
[1] == 'A' && buf
[2] == 'G';
298 static int u32_unsync(const unsigned char *buf
, uint32_t *up
)
303 for (i
= 0; i
< 4; i
++) {
314 static void get_u32(const unsigned char *buf
, uint32_t *up
)
319 for (i
= 0; i
< 4; i
++) {
327 static void get_u24(const unsigned char *buf
, uint32_t *up
)
332 for (i
= 0; i
< 3; i
++) {
340 static int v2_header_footer_parse(struct v2_header
*header
, const char *buf
)
342 const unsigned char *b
= (const unsigned char *)buf
;
344 header
->ver_major
= b
[3];
345 header
->ver_minor
= b
[4];
346 header
->flags
= b
[5];
347 if (header
->ver_major
== 0xff || header
->ver_minor
== 0xff)
349 return u32_unsync(b
+ 6, &header
->size
);
352 static int v2_header_parse(struct v2_header
*header
, const char *buf
)
354 if (buf
[0] != 'I' || buf
[1] != 'D' || buf
[2] != '3')
356 return v2_header_footer_parse(header
, buf
);
359 static int v2_footer_parse(struct v2_header
*header
, const char *buf
)
361 if (buf
[0] != '3' || buf
[1] != 'D' || buf
[2] != 'I')
363 return v2_header_footer_parse(header
, buf
);
366 static int v2_extended_header_parse(struct v2_extended_header
*header
, const char *buf
)
368 return u32_unsync((const unsigned char *)buf
, &header
->size
);
371 static int is_frame_id_char(char ch
)
373 return (ch
>= 'A' && ch
<= 'Z') || (ch
>= '0' && ch
<= '9');
382 * YYY is frame size excluding this 6 byte header
384 static int v2_2_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
388 for (i
= 0; i
< 3; i
++) {
389 if (!is_frame_id_char(buf
[i
]))
391 header
->id
[i
] = buf
[i
];
394 get_u24((const unsigned char *)(buf
+ 3), &header
->size
);
396 if (header
->size
== 0)
398 id3_debug("%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2], header
->size
);
409 * YYYY is frame size excluding this 10 byte header
412 static int v2_3_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
416 for (i
= 0; i
< 4; i
++) {
417 if (!is_frame_id_char(buf
[i
]))
419 header
->id
[i
] = buf
[i
];
421 get_u32((const unsigned char *)(buf
+ 4), &header
->size
);
422 header
->flags
= (buf
[8] << 8) | buf
[9];
423 if (header
->size
== 0)
425 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
426 header
->id
[3], header
->size
);
430 /* same as 2.3 but header size is sync safe */
431 static int v2_4_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
435 for (i
= 0; i
< 4; i
++) {
436 if (!is_frame_id_char(buf
[i
]))
438 header
->id
[i
] = buf
[i
];
440 if (!u32_unsync((const unsigned char *)(buf
+ 4), &header
->size
))
442 header
->flags
= (buf
[8] << 8) | buf
[9];
443 if (header
->size
== 0)
445 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
446 header
->id
[3], header
->size
);
450 static int read_all(int fd
, char *buf
, size_t size
)
455 int rc
= read(fd
, buf
+ pos
, size
- pos
);
458 if (errno
== EINTR
|| errno
== EAGAIN
)
467 static char *parse_genre(const char *str
)
473 if (strncasecmp(str
, "(RX", 3) == 0)
474 return xstrdup("Remix");
476 if (strncasecmp(str
, "(CR", 3) == 0)
477 return xstrdup("Cover");
484 idx
= strtol(str
, &end
, 10);
486 /* Number parsed but there may be some crap after the number.
487 * I don't care, ID3v2 by definition contains crap.
489 if (idx
>= 0 && idx
< NR_GENRES
)
490 return xstrdup(genres
[idx
]);
494 const char *ptr
= strchr(str
, ')');
497 /* genre name after random crap in parenthesis,
498 * return the genre name */
499 return xstrdup(ptr
+ 1);
504 /* random crap, just return it and wait for a bug report */
508 /* http://www.id3.org/id3v2.4.0-structure.txt */
514 { "TDRC", ID3_DATE
}, // recording date
515 { "TDRL", ID3_DATE
}, // release date
516 { "TDOR", ID3_DATE
}, // original release date
517 { "TSOP", ID3_ARTISTSORT
},
520 { "TPE1", ID3_ARTIST
},
521 { "TALB", ID3_ALBUM
},
522 { "TIT2", ID3_TITLE
},
523 { "TYER", ID3_DATE
},
524 { "TCON", ID3_GENRE
},
525 { "TPOS", ID3_DISC
},
526 { "TRCK", ID3_TRACK
},
527 { "TPE2", ID3_ALBUMARTIST
},
528 { "XSOP", ID3_ARTISTSORT
}, // obsolete
530 /* obsolete frames (2.2.0) */
531 { "TP1", ID3_ARTIST
},
532 { "TAL", ID3_ALBUM
},
533 { "TT2", ID3_TITLE
},
535 { "TCO", ID3_GENRE
},
537 { "TRK", ID3_TRACK
},
542 static int frame_tab_index(const char *id
)
546 for (i
= 0; frame_tab
[i
].key
!= -1; i
++) {
547 if (!strncmp(id
, frame_tab
[i
].name
, 4))
553 static void fix_date(char *buf
)
555 const char *ptr
= buf
;
560 if (ch
>= '0' && ch
<= '9') {
565 // number which length is 4, must be year
566 memmove(buf
, ptr
- 5, 4);
575 static char *decode_str(const char *buf
, int len
, int encoding
)
577 char *in
, *out
= NULL
;
581 case 0x00: /* ISO-8859-1 */
582 in
= xstrndup(buf
, len
);
583 rc
= utf8_encode(in
, id3_default_charset
, &out
);
586 case 0x03: /* UTF-8 */
587 in
= xstrndup(buf
, len
);
588 if (u_is_valid(in
)) {
591 rc
= utf8_encode(in
, id3_default_charset
, &out
);
595 case 0x01: /* UTF-16 */
596 out
= utf16_to_utf8((const unsigned char *)buf
, len
);
598 case 0x02: /* UTF-16BE */
599 out
= utf16be_to_utf8((const unsigned char *)buf
, len
);
605 static void v2_add_frame(struct id3tag
*id3
, struct v2_frame_header
*fh
, const char *buf
)
607 int idx
, encoding
= *buf
++, len
= fh
->size
- 1;
608 enum id3_key key
= NUM_ID3_KEYS
;
614 idx
= frame_tab_index(fh
->id
);
616 key
= frame_tab
[idx
].key
;
617 out
= decode_str(buf
, len
, encoding
);
621 if (key
== ID3_GENRE
) {
624 id3_debug("genre before: '%s'\n", out
);
625 tmp
= parse_genre(out
);
629 if (key
== ID3_DATE
) {
630 id3_debug("date before: '%s'\n", out
);
633 id3_debug("date parsing failed\n");
639 id3_debug("%s '%s'\n", frame_tab
[idx
].name
, out
);
640 } else if (!strncmp(fh
->id
, "TXXX", 4)) {
645 /* TXXX<len><encoding><key><val> */
646 out
= decode_str(buf
, len
, encoding
);
650 id3_debug("TXXX, key = '%s'\n", out
);
651 if (!strcasecmp(out
, "replaygain_track_gain"))
652 key
= ID3_RG_TRACK_GAIN
;
653 if (!strcasecmp(out
, "replaygain_track_peak"))
654 key
= ID3_RG_TRACK_PEAK
;
655 if (!strcasecmp(out
, "replaygain_album_gain"))
656 key
= ID3_RG_ALBUM_GAIN
;
657 if (!strcasecmp(out
, "replaygain_album_peak"))
658 key
= ID3_RG_ALBUM_PEAK
;
659 if (!strcasecmp(out
, "album artist"))
660 key
= ID3_ALBUMARTIST
;
661 if (!strcasecmp(out
, "albumartistsort"))
662 key
= ID3_ALBUMARTISTSORT
;
664 size
= strlen(out
) + 1;
667 if (key
== NUM_ID3_KEYS
)
675 out
= decode_str(buf
, len
, encoding
);
679 id3_debug("TXXX, val = '%s'\n", out
);
689 static void unsync(unsigned char *buf
, int *lenp
)
695 while (s
< len
- 1) {
696 if (buf
[s
] == 0xff && buf
[s
+ 1] == 0x00) {
697 /* 0xff 0x00 -> 0xff */
701 if (s
< len
- 2 && buf
[s
] == 0x00) {
702 /* 0xff 0x00 0x00 -> 0xff 0x00 */
713 d_print("unsyncronization removed %d bytes\n", s
- d
);
717 static int v2_read(struct id3tag
*id3
, int fd
, const struct v2_header
*header
)
722 int frame_header_size
;
724 buf_size
= header
->size
;
725 buf
= xnew(char, buf_size
);
726 rc
= read_all(fd
, buf
, buf_size
);
733 if (header
->flags
& V2_HEADER_EXTENDED
) {
734 struct v2_extended_header ext
;
736 v2_extended_header_parse(&ext
, buf
);
737 if (ext
.size
> buf_size
) {
738 id3_debug("extended header corrupted\n");
742 frame_start
= ext
.size
;
743 /* should check if update flag is set */
746 if (header
->flags
& V2_HEADER_UNSYNC
) {
747 int len
= buf_size
- frame_start
;
749 unsync((unsigned char *)(buf
+ frame_start
), &len
);
750 buf_size
= len
+ frame_start
;
753 frame_header_size
= 10;
754 if (header
->ver_major
== 2)
755 frame_header_size
= 6;
758 while (i
< buf_size
- frame_header_size
) {
759 struct v2_frame_header fh
;
762 if (header
->ver_major
== 2) {
763 if (!v2_2_0_frame_header_parse(&fh
, buf
+ i
))
765 } else if (header
->ver_major
== 3) {
766 if (!v2_3_0_frame_header_parse(&fh
, buf
+ i
))
770 if (!v2_4_0_frame_header_parse(&fh
, buf
+ i
))
774 i
+= frame_header_size
;
775 if (fh
.size
> buf_size
- i
) {
776 id3_debug("frame too big\n");
781 if (fh
.flags
& V2_FRAME_UNSYNC
) {
784 unsync((unsigned char *)(buf
+ i
), &tmp
);
787 v2_add_frame(id3
, &fh
, buf
+ i
);
795 int id3_tag_size(const char *buf
, int buf_size
)
797 struct v2_header header
;
801 if (v2_header_parse(&header
, buf
)) {
802 if (header
.flags
& V2_HEADER_FOOTER
) {
803 /* header + data + footer */
804 id3_debug("v2.%d.%d with footer\n", header
.ver_major
, header
.ver_minor
);
805 return 10 + header
.size
+ 10;
808 id3_debug("v2.%d.%d\n", header
.ver_major
, header
.ver_minor
);
809 return 10 + header
.size
;
811 if (buf_size
>= 3 && is_v1(buf
)) {
818 void id3_free(struct id3tag
*id3
)
822 for (i
= 0; i
< NUM_ID3_KEYS
; i
++)
826 int id3_read_tags(struct id3tag
*id3
, int fd
, unsigned int flags
)
831 if (flags
& ID3_V2
) {
832 struct v2_header header
;
835 rc
= read_all(fd
, buf
, 10);
838 if (v2_header_parse(&header
, buf
)) {
839 rc
= v2_read(id3
, fd
, &header
);
842 /* get v1 if needed */
844 /* get v2 from end and optionally v1 */
846 off
= lseek(fd
, -138, SEEK_END
);
849 rc
= read_all(fd
, buf
, 138);
853 if (is_v1(buf
+ 10)) {
854 if (flags
& ID3_V1
) {
855 memcpy(id3
->v1
, buf
+ 10, 128);
858 if (v2_footer_parse(&header
, buf
)) {
859 /* footer at end of file - 128 */
860 off
= lseek(fd
, -(header
.size
+ 138), SEEK_END
);
863 rc
= v2_read(id3
, fd
, &header
);
867 } else if (v2_footer_parse(&header
, buf
+ 128)) {
868 /* footer at end of file */
869 off
= lseek(fd
, -(header
.size
+ 10), SEEK_END
);
872 rc
= v2_read(id3
, fd
, &header
);
879 if (flags
& ID3_V1
) {
880 off
= lseek(fd
, -128, SEEK_END
);
883 rc
= read_all(fd
, id3
->v1
, 128);
886 id3
->has_v1
= is_v1(id3
->v1
);
895 static char *v1_get_str(const char *buf
, int len
)
901 for (i
= len
- 1; i
>= 0; i
--) {
902 if (buf
[i
] != 0 && buf
[i
] != ' ')
907 memcpy(in
, buf
, i
+ 1);
911 if (utf8_encode(in
, id3_default_charset
, &out
))
916 char *id3_get_comment(struct id3tag
*id3
, enum id3_key key
)
920 return xstrdup(id3
->v2
[key
]);
925 return v1_get_str(id3
->v1
+ 33, 30);
927 return v1_get_str(id3
->v1
+ 63, 30);
929 return v1_get_str(id3
->v1
+ 3, 30);
931 return v1_get_str(id3
->v1
+ 93, 4);
934 unsigned char idx
= id3
->v1
[127];
936 if (idx
>= NR_GENRES
)
938 return xstrdup(genres
[idx
]);
944 if (id3
->v1
[125] != 0)
947 snprintf(t
, 4, "%d", ((unsigned char *)id3
->v1
)[126]);