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
] = {
233 "replaygain_track_gain",
234 "replaygain_track_peak",
235 "replaygain_album_gain",
236 "replaygain_album_peak"
239 static int utf16_is_special(const uchar uch
)
241 if (UTF16_IS_HSURROGATE(uch
) || UTF16_IS_LSURROGATE(uch
) || UTF16_IS_BOM(uch
))
246 static char *utf16_to_utf8(const unsigned char *buf
, int buf_size
)
251 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
253 while (buf_size
- i
>= 2) {
256 u
= buf
[i
] + (buf
[i
+ 1] << 8);
257 if (u_is_unicode(u
)) {
258 if (utf16_is_special(u
) == 0)
259 u_set_char(out
, &idx
, u
);
268 u_set_char(out
, &idx
, 0);
272 static char *utf16be_to_utf8(const unsigned char *buf
, int buf_size
)
277 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
280 while (buf_size
- i
>= 2) {
283 u
= buf
[i
+ 1] + (buf
[i
] << 8);
284 if (u_is_unicode(u
)) {
285 if (utf16_is_special(u
) == 0)
286 u_set_char(out
, &idx
, u
);
295 u_set_char(out
, &idx
, 0);
299 static int is_v1(const char *buf
)
301 return buf
[0] == 'T' && buf
[1] == 'A' && buf
[2] == 'G';
304 static int u32_unsync(const unsigned char *buf
, uint32_t *up
)
309 for (i
= 0; i
< 4; i
++) {
320 static void get_u32(const unsigned char *buf
, uint32_t *up
)
325 for (i
= 0; i
< 4; i
++) {
333 static void get_u24(const unsigned char *buf
, uint32_t *up
)
338 for (i
= 0; i
< 3; i
++) {
346 static int v2_header_footer_parse(struct v2_header
*header
, const char *buf
)
348 const unsigned char *b
= (const unsigned char *)buf
;
350 header
->ver_major
= b
[3];
351 header
->ver_minor
= b
[4];
352 header
->flags
= b
[5];
353 if (header
->ver_major
== 0xff || header
->ver_minor
== 0xff)
355 return u32_unsync(b
+ 6, &header
->size
);
358 static int v2_header_parse(struct v2_header
*header
, const char *buf
)
360 if (buf
[0] != 'I' || buf
[1] != 'D' || buf
[2] != '3')
362 return v2_header_footer_parse(header
, buf
);
365 static int v2_footer_parse(struct v2_header
*header
, const char *buf
)
367 if (buf
[0] != '3' || buf
[1] != 'D' || buf
[2] != 'I')
369 return v2_header_footer_parse(header
, buf
);
372 static int v2_extended_header_parse(struct v2_extended_header
*header
, const char *buf
)
374 return u32_unsync((const unsigned char *)buf
, &header
->size
);
377 static int is_frame_id_char(char ch
)
379 return (ch
>= 'A' && ch
<= 'Z') || (ch
>= '0' && ch
<= '9');
388 * YYY is frame size excluding this 6 byte header
390 static int v2_2_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
394 for (i
= 0; i
< 3; i
++) {
395 if (!is_frame_id_char(buf
[i
]))
397 header
->id
[i
] = buf
[i
];
400 get_u24((const unsigned char *)(buf
+ 3), &header
->size
);
402 if (header
->size
== 0)
404 id3_debug("%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2], header
->size
);
415 * YYYY is frame size excluding this 10 byte header
418 static int v2_3_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
422 for (i
= 0; i
< 4; i
++) {
423 if (!is_frame_id_char(buf
[i
]))
425 header
->id
[i
] = buf
[i
];
427 get_u32((const unsigned char *)(buf
+ 4), &header
->size
);
428 header
->flags
= (buf
[8] << 8) | buf
[9];
429 if (header
->size
== 0)
431 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
432 header
->id
[3], header
->size
);
436 /* same as 2.3 but header size is sync safe */
437 static int v2_4_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
441 for (i
= 0; i
< 4; i
++) {
442 if (!is_frame_id_char(buf
[i
]))
444 header
->id
[i
] = buf
[i
];
446 if (!u32_unsync((const unsigned char *)(buf
+ 4), &header
->size
))
448 header
->flags
= (buf
[8] << 8) | buf
[9];
449 if (header
->size
== 0)
451 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
452 header
->id
[3], header
->size
);
456 static int read_all(int fd
, char *buf
, size_t size
)
461 int rc
= read(fd
, buf
+ pos
, size
- pos
);
464 if (errno
== EINTR
|| errno
== EAGAIN
)
473 static char *parse_genre(const char *str
)
479 if (strncasecmp(str
, "(RX", 3) == 0)
480 return xstrdup("Remix");
482 if (strncasecmp(str
, "(CR", 3) == 0)
483 return xstrdup("Cover");
490 idx
= strtol(str
, &end
, 10);
492 /* Number parsed but there may be some crap after the number.
493 * I don't care, ID3v2 by definition contains crap.
495 if (idx
>= 0 && idx
< NR_GENRES
)
496 return xstrdup(genres
[idx
]);
500 const char *ptr
= strchr(str
, ')');
503 /* genre name after random crap in parenthesis,
504 * return the genre name */
505 return xstrdup(ptr
+ 1);
510 /* random crap, just return it and wait for a bug report */
514 /* http://www.id3.org/id3v2.4.0-structure.txt */
520 { "TDRC", ID3_DATE
}, // recording date
521 { "TDRL", ID3_DATE
}, // release date
522 { "TDOR", ID3_DATE
}, // original release date
525 { "TPE1", ID3_ARTIST
},
526 { "TALB", ID3_ALBUM
},
527 { "TIT2", ID3_TITLE
},
528 { "TYER", ID3_DATE
},
529 { "TCON", ID3_GENRE
},
530 { "TPOS", ID3_DISC
},
531 { "TRCK", ID3_TRACK
},
532 { "TPE2", ID3_ALBUMARTIST
},
534 /* obsolete frames (2.2.0) */
535 { "TP1", ID3_ARTIST
},
536 { "TAL", ID3_ALBUM
},
537 { "TT2", ID3_TITLE
},
539 { "TCO", ID3_GENRE
},
541 { "TRK", ID3_TRACK
},
546 static int frame_tab_index(const char *id
)
550 for (i
= 0; frame_tab
[i
].key
!= -1; i
++) {
551 if (!strncmp(id
, frame_tab
[i
].name
, 4))
557 static void fix_date(char *buf
)
559 const char *ptr
= buf
;
564 if (ch
>= '0' && ch
<= '9') {
569 // number which length is 4, must be year
570 memmove(buf
, ptr
- 5, 4);
579 static char *decode_str(const char *buf
, int len
, int encoding
)
581 char *in
, *out
= NULL
;
585 case 0x00: /* ISO-8859-1 */
586 in
= xstrndup(buf
, len
);
587 rc
= utf8_encode(in
, id3_default_charset
, &out
);
590 case 0x03: /* UTF-8 */
591 in
= xstrndup(buf
, len
);
592 if (u_is_valid(in
)) {
595 rc
= utf8_encode(in
, id3_default_charset
, &out
);
599 case 0x01: /* UTF-16 */
600 out
= utf16_to_utf8((const unsigned char *)buf
, len
);
602 case 0x02: /* UTF-16BE */
603 out
= utf16be_to_utf8((const unsigned char *)buf
, len
);
609 static void v2_add_frame(ID3
*id3
, struct v2_frame_header
*fh
, const char *buf
)
611 int idx
, encoding
= *buf
++, len
= fh
->size
- 1;
612 enum id3_key key
= NUM_ID3_KEYS
;
618 idx
= frame_tab_index(fh
->id
);
620 key
= frame_tab
[idx
].key
;
621 out
= decode_str(buf
, len
, encoding
);
625 if (key
== ID3_TRACK
|| key
== ID3_DISC
)
626 fix_track_or_disc(out
);
627 if (key
== ID3_GENRE
) {
630 id3_debug("genre before: '%s'\n", out
);
631 tmp
= parse_genre(out
);
635 if (key
== ID3_DATE
) {
636 id3_debug("date before: '%s'\n", out
);
639 id3_debug("date parsing failed\n");
645 id3_debug("%s '%s'\n", frame_tab
[idx
].name
, out
);
646 } else if (!strncmp(fh
->id
, "TXXX", 4)) {
651 /* TXXX<len><encoding><key><val> */
652 out
= decode_str(buf
, len
, encoding
);
656 id3_debug("TXXX, key = '%s'\n", out
);
657 if (!strcasecmp(out
, "replaygain_track_gain"))
658 key
= ID3_RG_TRACK_GAIN
;
659 if (!strcasecmp(out
, "replaygain_track_peak"))
660 key
= ID3_RG_TRACK_PEAK
;
661 if (!strcasecmp(out
, "replaygain_album_gain"))
662 key
= ID3_RG_ALBUM_GAIN
;
663 if (!strcasecmp(out
, "replaygain_album_peak"))
664 key
= ID3_RG_ALBUM_PEAK
;
666 size
= strlen(out
) + 1;
669 if (key
== NUM_ID3_KEYS
)
677 out
= decode_str(buf
, len
, encoding
);
681 id3_debug("TXXX, val = '%s'\n", out
);
691 static void unsync(unsigned char *buf
, int *lenp
)
697 while (s
< len
- 1) {
698 if (buf
[s
] == 0xff && buf
[s
+ 1] == 0x00) {
699 /* 0xff 0x00 -> 0xff */
703 if (s
< len
- 2 && buf
[s
] == 0x00) {
704 /* 0xff 0x00 0x00 -> 0xff 0x00 */
715 d_print("unsyncronization removed %d bytes\n", s
- d
);
719 static int v2_read(ID3
*id3
, int fd
, const struct v2_header
*header
)
724 int frame_header_size
;
726 buf_size
= header
->size
;
727 buf
= xnew(char, buf_size
);
728 rc
= read_all(fd
, buf
, buf_size
);
735 if (header
->flags
& V2_HEADER_EXTENDED
) {
736 struct v2_extended_header ext
;
738 v2_extended_header_parse(&ext
, buf
);
739 if (ext
.size
> buf_size
) {
740 id3_debug("extended header corrupted\n");
744 frame_start
= ext
.size
;
745 /* should check if update flag is set */
748 if (header
->flags
& V2_HEADER_UNSYNC
) {
749 int len
= buf_size
- frame_start
;
751 unsync((unsigned char *)(buf
+ frame_start
), &len
);
752 buf_size
= len
+ frame_start
;
755 frame_header_size
= 10;
756 if (header
->ver_major
== 2)
757 frame_header_size
= 6;
760 while (i
< buf_size
- frame_header_size
) {
761 struct v2_frame_header fh
;
764 if (header
->ver_major
== 2) {
765 if (!v2_2_0_frame_header_parse(&fh
, buf
+ i
))
767 } else if (header
->ver_major
== 3) {
768 if (!v2_3_0_frame_header_parse(&fh
, buf
+ i
))
772 if (!v2_4_0_frame_header_parse(&fh
, buf
+ i
))
776 i
+= frame_header_size
;
777 if (fh
.size
> buf_size
- i
) {
778 id3_debug("frame too big\n");
783 if (fh
.flags
& V2_FRAME_UNSYNC
) {
786 unsync((unsigned char *)(buf
+ i
), &tmp
);
789 v2_add_frame(id3
, &fh
, buf
+ i
);
797 int id3_tag_size(const char *buf
, int buf_size
)
799 struct v2_header header
;
803 if (v2_header_parse(&header
, buf
)) {
804 if (header
.flags
& V2_HEADER_FOOTER
) {
805 /* header + data + footer */
806 id3_debug("v2.%d.%d with footer\n", header
.ver_major
, header
.ver_minor
);
807 return 10 + header
.size
+ 10;
810 id3_debug("v2.%d.%d\n", header
.ver_major
, header
.ver_minor
);
811 return 10 + header
.size
;
813 if (buf_size
>= 3 && is_v1(buf
)) {
822 return xnew0(ID3
, 1);
825 void id3_free(ID3
*id3
)
829 for (i
= 0; i
< NUM_ID3_KEYS
; i
++)
834 int id3_read_tags(ID3
*id3
, int fd
, unsigned int flags
)
839 if (flags
& ID3_V2
) {
840 struct v2_header header
;
843 rc
= read_all(fd
, buf
, 10);
846 if (v2_header_parse(&header
, buf
)) {
847 rc
= v2_read(id3
, fd
, &header
);
850 /* get v1 if needed */
852 /* get v2 from end and optionally v1 */
854 off
= lseek(fd
, -138, SEEK_END
);
857 rc
= read_all(fd
, buf
, 138);
861 if (is_v1(buf
+ 10)) {
862 if (flags
& ID3_V1
) {
863 memcpy(id3
->v1
, buf
+ 10, 128);
866 if (v2_footer_parse(&header
, buf
)) {
867 /* footer at end of file - 128 */
868 off
= lseek(fd
, -(header
.size
+ 138), SEEK_END
);
871 rc
= v2_read(id3
, fd
, &header
);
875 } else if (v2_footer_parse(&header
, buf
+ 128)) {
876 /* footer at end of file */
877 off
= lseek(fd
, -(header
.size
+ 10), SEEK_END
);
880 rc
= v2_read(id3
, fd
, &header
);
887 if (flags
& ID3_V1
) {
888 off
= lseek(fd
, -128, SEEK_END
);
891 rc
= read_all(fd
, id3
->v1
, 128);
894 id3
->has_v1
= is_v1(id3
->v1
);
903 static char *v1_get_str(const char *buf
, int len
)
909 for (i
= len
- 1; i
>= 0; i
--) {
910 if (buf
[i
] != 0 && buf
[i
] != ' ')
915 memcpy(in
, buf
, i
+ 1);
919 if (utf8_encode(in
, id3_default_charset
, &out
))
924 char *id3_get_comment(ID3
*id3
, enum id3_key key
)
928 return xstrdup(id3
->v2
[key
]);
933 return v1_get_str(id3
->v1
+ 33, 30);
935 return v1_get_str(id3
->v1
+ 63, 30);
937 return v1_get_str(id3
->v1
+ 3, 30);
939 return v1_get_str(id3
->v1
+ 93, 4);
942 unsigned char idx
= id3
->v1
[127];
944 if (idx
>= NR_GENRES
)
946 return xstrdup(genres
[idx
]);
952 if (id3
->v1
[125] != 0)
955 snprintf(t
, 4, "%d", ((unsigned char *)id3
->v1
)[126]);