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
] = {
228 "replaygain_track_gain",
229 "replaygain_track_peak",
230 "replaygain_album_gain",
231 "replaygain_album_peak"
234 static int utf16_is_special(const uchar uch
)
236 if (UTF16_IS_HSURROGATE(uch
) || UTF16_IS_LSURROGATE(uch
) || UTF16_IS_BOM(uch
))
241 static char *utf16_to_utf8(const unsigned char *buf
, int buf_size
)
246 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
248 while (buf_size
- i
>= 2) {
251 u
= buf
[i
] + (buf
[i
+ 1] << 8);
252 if (u_is_unicode(u
)) {
253 if (utf16_is_special(u
) == 0)
254 u_set_char(out
, &idx
, u
);
263 u_set_char(out
, &idx
, 0);
267 static char *utf16be_to_utf8(const unsigned char *buf
, int buf_size
)
272 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
275 while (buf_size
- i
>= 2) {
278 u
= buf
[i
+ 1] + (buf
[i
] << 8);
279 if (u_is_unicode(u
)) {
280 if (utf16_is_special(u
) == 0)
281 u_set_char(out
, &idx
, u
);
290 u_set_char(out
, &idx
, 0);
294 static int is_v1(const char *buf
)
296 return buf
[0] == 'T' && buf
[1] == 'A' && buf
[2] == 'G';
299 static int u32_unsync(const unsigned char *buf
, uint32_t *up
)
304 for (i
= 0; i
< 4; i
++) {
315 static void get_u32(const unsigned char *buf
, uint32_t *up
)
320 for (i
= 0; i
< 4; i
++) {
328 static void get_u24(const unsigned char *buf
, uint32_t *up
)
333 for (i
= 0; i
< 3; i
++) {
341 static int v2_header_footer_parse(struct v2_header
*header
, const char *buf
)
343 const unsigned char *b
= (const unsigned char *)buf
;
345 header
->ver_major
= b
[3];
346 header
->ver_minor
= b
[4];
347 header
->flags
= b
[5];
348 if (header
->ver_major
== 0xff || header
->ver_minor
== 0xff)
350 return u32_unsync(b
+ 6, &header
->size
);
353 static int v2_header_parse(struct v2_header
*header
, const char *buf
)
355 if (buf
[0] != 'I' || buf
[1] != 'D' || buf
[2] != '3')
357 return v2_header_footer_parse(header
, buf
);
360 static int v2_footer_parse(struct v2_header
*header
, const char *buf
)
362 if (buf
[0] != '3' || buf
[1] != 'D' || buf
[2] != 'I')
364 return v2_header_footer_parse(header
, buf
);
367 static int v2_extended_header_parse(struct v2_extended_header
*header
, const char *buf
)
369 return u32_unsync((const unsigned char *)buf
, &header
->size
);
372 static int is_frame_id_char(char ch
)
374 return (ch
>= 'A' && ch
<= 'Z') || (ch
>= '0' && ch
<= '9');
383 * YYY is frame size excluding this 6 byte header
385 static int v2_2_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
389 for (i
= 0; i
< 3; i
++) {
390 if (!is_frame_id_char(buf
[i
]))
392 header
->id
[i
] = buf
[i
];
395 get_u24((const unsigned char *)(buf
+ 3), &header
->size
);
397 if (header
->size
== 0)
399 id3_debug("%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2], header
->size
);
410 * YYYY is frame size excluding this 10 byte header
413 static int v2_3_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
417 for (i
= 0; i
< 4; i
++) {
418 if (!is_frame_id_char(buf
[i
]))
420 header
->id
[i
] = buf
[i
];
422 get_u32((const unsigned char *)(buf
+ 4), &header
->size
);
423 header
->flags
= (buf
[8] << 8) | buf
[9];
424 if (header
->size
== 0)
426 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
427 header
->id
[3], header
->size
);
431 /* same as 2.3 but header size is sync safe */
432 static int v2_4_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
436 for (i
= 0; i
< 4; i
++) {
437 if (!is_frame_id_char(buf
[i
]))
439 header
->id
[i
] = buf
[i
];
441 if (!u32_unsync((const unsigned char *)(buf
+ 4), &header
->size
))
443 header
->flags
= (buf
[8] << 8) | buf
[9];
444 if (header
->size
== 0)
446 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
447 header
->id
[3], header
->size
);
451 static int read_all(int fd
, char *buf
, size_t size
)
456 int rc
= read(fd
, buf
+ pos
, size
- pos
);
459 if (errno
== EINTR
|| errno
== EAGAIN
)
468 static char *parse_genre(const char *str
)
474 if (strncasecmp(str
, "(RX", 3) == 0)
475 return xstrdup("Remix");
477 if (strncasecmp(str
, "(CR", 3) == 0)
478 return xstrdup("Cover");
485 idx
= strtol(str
, &end
, 10);
487 /* Number parsed but there may be some crap after the number.
488 * I don't care, ID3v2 by definition contains crap.
490 if (idx
>= 0 && idx
< NR_GENRES
)
491 return xstrdup(genres
[idx
]);
495 const char *ptr
= strchr(str
, ')');
498 /* genre name after random crap in parenthesis,
499 * return the genre name */
500 return xstrdup(ptr
+ 1);
505 /* random crap, just return it and wait for a bug report */
509 /* http://www.id3.org/id3v2.4.0-structure.txt */
515 { "TDRC", ID3_DATE
}, // recording date
516 { "TDRL", ID3_DATE
}, // release date
517 { "TDOR", ID3_DATE
}, // original release date
518 { "TSOP", ID3_ARTISTSORT
},
521 { "TPE1", ID3_ARTIST
},
522 { "TALB", ID3_ALBUM
},
523 { "TIT2", ID3_TITLE
},
524 { "TYER", ID3_DATE
},
525 { "TCON", ID3_GENRE
},
526 { "TPOS", ID3_DISC
},
527 { "TRCK", ID3_TRACK
},
528 { "TPE2", ID3_ALBUMARTIST
},
529 { "XSOP", ID3_ARTISTSORT
}, // obsolete
530 { "TCMP", ID3_COMPILATION
},
532 /* obsolete frames (2.2.0) */
533 { "TP1", ID3_ARTIST
},
534 { "TAL", ID3_ALBUM
},
535 { "TT2", ID3_TITLE
},
537 { "TCO", ID3_GENRE
},
539 { "TRK", ID3_TRACK
},
544 static int frame_tab_index(const char *id
)
548 for (i
= 0; frame_tab
[i
].key
!= -1; i
++) {
549 if (!strncmp(id
, frame_tab
[i
].name
, 4))
555 static void fix_date(char *buf
)
557 const char *ptr
= buf
;
562 if (ch
>= '0' && ch
<= '9') {
567 // number which length is 4, must be year
568 memmove(buf
, ptr
- 5, 4);
577 static char *decode_str(const char *buf
, int len
, int encoding
)
579 char *in
, *out
= NULL
;
583 case 0x00: /* ISO-8859-1 */
584 in
= xstrndup(buf
, len
);
585 rc
= utf8_encode(in
, id3_default_charset
, &out
);
588 case 0x03: /* UTF-8 */
589 in
= xstrndup(buf
, len
);
590 if (u_is_valid(in
)) {
593 rc
= utf8_encode(in
, id3_default_charset
, &out
);
597 case 0x01: /* UTF-16 */
598 out
= utf16_to_utf8((const unsigned char *)buf
, len
);
600 case 0x02: /* UTF-16BE */
601 out
= utf16be_to_utf8((const unsigned char *)buf
, len
);
607 static void v2_add_frame(struct id3tag
*id3
, struct v2_frame_header
*fh
, const char *buf
)
609 int idx
, encoding
= *buf
++, len
= fh
->size
- 1;
610 enum id3_key key
= NUM_ID3_KEYS
;
616 idx
= frame_tab_index(fh
->id
);
618 key
= frame_tab
[idx
].key
;
619 out
= decode_str(buf
, len
, encoding
);
623 if (key
== ID3_GENRE
) {
626 id3_debug("genre before: '%s'\n", out
);
627 tmp
= parse_genre(out
);
631 if (key
== ID3_DATE
) {
632 id3_debug("date before: '%s'\n", out
);
635 id3_debug("date parsing failed\n");
641 id3_debug("%s '%s'\n", frame_tab
[idx
].name
, out
);
642 } else if (!strncmp(fh
->id
, "TXXX", 4)) {
647 /* TXXX<len><encoding><key><val> */
648 out
= decode_str(buf
, len
, encoding
);
652 id3_debug("TXXX, key = '%s'\n", out
);
653 if (!strcasecmp(out
, "replaygain_track_gain"))
654 key
= ID3_RG_TRACK_GAIN
;
655 if (!strcasecmp(out
, "replaygain_track_peak"))
656 key
= ID3_RG_TRACK_PEAK
;
657 if (!strcasecmp(out
, "replaygain_album_gain"))
658 key
= ID3_RG_ALBUM_GAIN
;
659 if (!strcasecmp(out
, "replaygain_album_peak"))
660 key
= ID3_RG_ALBUM_PEAK
;
661 if (!strcasecmp(out
, "album artist"))
662 key
= ID3_ALBUMARTIST
;
663 if (!strcasecmp(out
, "albumartistsort"))
664 key
= ID3_ALBUMARTISTSORT
;
665 if (!strcasecmp(out
, "compilation"))
666 key
= ID3_COMPILATION
;
668 size
= strlen(out
) + 1;
671 if (key
== NUM_ID3_KEYS
)
679 out
= decode_str(buf
, len
, encoding
);
683 id3_debug("TXXX, val = '%s'\n", out
);
693 static void unsync(unsigned char *buf
, int *lenp
)
699 while (s
< len
- 1) {
700 if (buf
[s
] == 0xff && buf
[s
+ 1] == 0x00) {
701 /* 0xff 0x00 -> 0xff */
705 if (s
< len
- 2 && buf
[s
] == 0x00) {
706 /* 0xff 0x00 0x00 -> 0xff 0x00 */
717 d_print("unsyncronization removed %d bytes\n", s
- d
);
721 static int v2_read(struct id3tag
*id3
, int fd
, const struct v2_header
*header
)
726 int frame_header_size
;
728 buf_size
= header
->size
;
729 buf
= xnew(char, buf_size
);
730 rc
= read_all(fd
, buf
, buf_size
);
737 if (header
->flags
& V2_HEADER_EXTENDED
) {
738 struct v2_extended_header ext
;
740 v2_extended_header_parse(&ext
, buf
);
741 if (ext
.size
> buf_size
) {
742 id3_debug("extended header corrupted\n");
746 frame_start
= ext
.size
;
747 /* should check if update flag is set */
750 if (header
->flags
& V2_HEADER_UNSYNC
) {
751 int len
= buf_size
- frame_start
;
753 unsync((unsigned char *)(buf
+ frame_start
), &len
);
754 buf_size
= len
+ frame_start
;
757 frame_header_size
= 10;
758 if (header
->ver_major
== 2)
759 frame_header_size
= 6;
762 while (i
< buf_size
- frame_header_size
) {
763 struct v2_frame_header fh
;
766 if (header
->ver_major
== 2) {
767 if (!v2_2_0_frame_header_parse(&fh
, buf
+ i
))
769 } else if (header
->ver_major
== 3) {
770 if (!v2_3_0_frame_header_parse(&fh
, buf
+ i
))
774 if (!v2_4_0_frame_header_parse(&fh
, buf
+ i
))
778 i
+= frame_header_size
;
779 if (fh
.size
> buf_size
- i
) {
780 id3_debug("frame too big\n");
785 if (fh
.flags
& V2_FRAME_UNSYNC
) {
788 unsync((unsigned char *)(buf
+ i
), &tmp
);
791 v2_add_frame(id3
, &fh
, buf
+ i
);
799 int id3_tag_size(const char *buf
, int buf_size
)
801 struct v2_header header
;
805 if (v2_header_parse(&header
, buf
)) {
806 if (header
.flags
& V2_HEADER_FOOTER
) {
807 /* header + data + footer */
808 id3_debug("v2.%d.%d with footer\n", header
.ver_major
, header
.ver_minor
);
809 return 10 + header
.size
+ 10;
812 id3_debug("v2.%d.%d\n", header
.ver_major
, header
.ver_minor
);
813 return 10 + header
.size
;
815 if (buf_size
>= 3 && is_v1(buf
)) {
822 void id3_free(struct id3tag
*id3
)
826 for (i
= 0; i
< NUM_ID3_KEYS
; i
++)
830 int id3_read_tags(struct id3tag
*id3
, int fd
, unsigned int flags
)
835 if (flags
& ID3_V2
) {
836 struct v2_header header
;
839 rc
= read_all(fd
, buf
, 10);
842 if (v2_header_parse(&header
, buf
)) {
843 rc
= v2_read(id3
, fd
, &header
);
846 /* get v1 if needed */
848 /* get v2 from end and optionally v1 */
850 off
= lseek(fd
, -138, SEEK_END
);
853 rc
= read_all(fd
, buf
, 138);
857 if (is_v1(buf
+ 10)) {
858 if (flags
& ID3_V1
) {
859 memcpy(id3
->v1
, buf
+ 10, 128);
862 if (v2_footer_parse(&header
, buf
)) {
863 /* footer at end of file - 128 */
864 off
= lseek(fd
, -(header
.size
+ 138), SEEK_END
);
867 rc
= v2_read(id3
, fd
, &header
);
871 } else if (v2_footer_parse(&header
, buf
+ 128)) {
872 /* footer at end of file */
873 off
= lseek(fd
, -(header
.size
+ 10), SEEK_END
);
876 rc
= v2_read(id3
, fd
, &header
);
883 if (flags
& ID3_V1
) {
884 off
= lseek(fd
, -128, SEEK_END
);
887 rc
= read_all(fd
, id3
->v1
, 128);
890 id3
->has_v1
= is_v1(id3
->v1
);
899 static char *v1_get_str(const char *buf
, int len
)
905 for (i
= len
- 1; i
>= 0; i
--) {
906 if (buf
[i
] != 0 && buf
[i
] != ' ')
911 memcpy(in
, buf
, i
+ 1);
915 if (utf8_encode(in
, id3_default_charset
, &out
))
920 char *id3_get_comment(struct id3tag
*id3
, enum id3_key key
)
924 return xstrdup(id3
->v2
[key
]);
929 return v1_get_str(id3
->v1
+ 33, 30);
931 return v1_get_str(id3
->v1
+ 63, 30);
933 return v1_get_str(id3
->v1
+ 3, 30);
935 return v1_get_str(id3
->v1
+ 93, 4);
938 unsigned char idx
= id3
->v1
[127];
940 if (idx
>= NR_GENRES
)
942 return xstrdup(genres
[idx
]);
948 if (id3
->v1
[125] != 0)
951 snprintf(t
, 4, "%d", ((unsigned char *)id3
->v1
)[126]);