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 static int utf16_is_special(const uchar uch
)
226 if (UTF16_IS_HSURROGATE(uch
) || UTF16_IS_LSURROGATE(uch
) || UTF16_IS_BOM(uch
))
231 static char *utf16_to_utf8(const unsigned char *buf
, int buf_size
)
236 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
238 while (buf_size
- i
>= 2) {
241 u
= buf
[i
] + (buf
[i
+ 1] << 8);
242 if (u_is_unicode(u
)) {
243 if (utf16_is_special(u
) == 0)
244 u_set_char(out
, &idx
, u
);
253 u_set_char(out
, &idx
, 0);
257 static char *utf16be_to_utf8(const unsigned char *buf
, int buf_size
)
262 out
= xnew(char, (buf_size
/ 2) * 4 + 1);
265 while (buf_size
- i
>= 2) {
268 u
= buf
[i
+ 1] + (buf
[i
] << 8);
269 if (u_is_unicode(u
)) {
270 if (utf16_is_special(u
) == 0)
271 u_set_char(out
, &idx
, u
);
280 u_set_char(out
, &idx
, 0);
284 static int is_v1(const char *buf
)
286 return buf
[0] == 'T' && buf
[1] == 'A' && buf
[2] == 'G';
289 static int u32_unsync(const unsigned char *buf
, uint32_t *up
)
294 for (i
= 0; i
< 4; i
++) {
305 static void get_u32(const unsigned char *buf
, uint32_t *up
)
310 for (i
= 0; i
< 4; i
++) {
318 static void get_u24(const unsigned char *buf
, uint32_t *up
)
323 for (i
= 0; i
< 3; i
++) {
331 static int v2_header_footer_parse(struct v2_header
*header
, const char *buf
)
333 const unsigned char *b
= (const unsigned char *)buf
;
335 header
->ver_major
= b
[3];
336 header
->ver_minor
= b
[4];
337 header
->flags
= b
[5];
338 if (header
->ver_major
== 0xff || header
->ver_minor
== 0xff)
340 return u32_unsync(b
+ 6, &header
->size
);
343 static int v2_header_parse(struct v2_header
*header
, const char *buf
)
345 if (buf
[0] != 'I' || buf
[1] != 'D' || buf
[2] != '3')
347 return v2_header_footer_parse(header
, buf
);
350 static int v2_footer_parse(struct v2_header
*header
, const char *buf
)
352 if (buf
[0] != '3' || buf
[1] != 'D' || buf
[2] != 'I')
354 return v2_header_footer_parse(header
, buf
);
357 static int v2_extended_header_parse(struct v2_extended_header
*header
, const char *buf
)
359 return u32_unsync((const unsigned char *)buf
, &header
->size
);
362 static int is_frame_id_char(char ch
)
364 return (ch
>= 'A' && ch
<= 'Z') || (ch
>= '0' && ch
<= '9');
373 * YYY is frame size excluding this 6 byte header
375 static int v2_2_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
379 for (i
= 0; i
< 3; i
++) {
380 if (!is_frame_id_char(buf
[i
]))
382 header
->id
[i
] = buf
[i
];
385 get_u24((const unsigned char *)(buf
+ 3), &header
->size
);
387 if (header
->size
== 0)
389 id3_debug("%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2], header
->size
);
400 * YYYY is frame size excluding this 10 byte header
403 static int v2_3_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
407 for (i
= 0; i
< 4; i
++) {
408 if (!is_frame_id_char(buf
[i
]))
410 header
->id
[i
] = buf
[i
];
412 get_u32((const unsigned char *)(buf
+ 4), &header
->size
);
413 header
->flags
= (buf
[8] << 8) | buf
[9];
414 if (header
->size
== 0)
416 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
417 header
->id
[3], header
->size
);
421 /* same as 2.3 but header size is sync safe */
422 static int v2_4_0_frame_header_parse(struct v2_frame_header
*header
, const char *buf
)
426 for (i
= 0; i
< 4; i
++) {
427 if (!is_frame_id_char(buf
[i
]))
429 header
->id
[i
] = buf
[i
];
431 if (!u32_unsync((const unsigned char *)(buf
+ 4), &header
->size
))
433 header
->flags
= (buf
[8] << 8) | buf
[9];
434 if (header
->size
== 0)
436 id3_debug("%c%c%c%c %d\n", header
->id
[0], header
->id
[1], header
->id
[2],
437 header
->id
[3], header
->size
);
441 static int read_all(int fd
, char *buf
, size_t size
)
446 int rc
= read(fd
, buf
+ pos
, size
- pos
);
449 if (errno
== EINTR
|| errno
== EAGAIN
)
458 static char *parse_genre(const char *str
)
464 if (strncasecmp(str
, "(RX", 3) == 0)
465 return xstrdup("Remix");
467 if (strncasecmp(str
, "(CR", 3) == 0)
468 return xstrdup("Cover");
475 idx
= strtol(str
, &end
, 10);
477 /* Number parsed but there may be some crap after the number.
478 * I don't care, ID3v2 by definition contains crap.
480 if (idx
>= 0 && idx
< NR_GENRES
)
481 return xstrdup(genres
[idx
]);
485 const char *ptr
= strchr(str
, ')');
488 /* genre name after random crap in parenthesis,
489 * return the genre name */
490 return xstrdup(ptr
+ 1);
495 /* random crap, just return it and wait for a bug report */
499 /* http://www.id3.org/id3v2.4.0-structure.txt */
505 { "TDRC", ID3_DATE
}, // recording date
506 { "TDRL", ID3_DATE
}, // release date
507 { "TDOR", ID3_DATE
}, // original release date
510 { "TPE1", ID3_ARTIST
},
511 { "TALB", ID3_ALBUM
},
512 { "TIT2", ID3_TITLE
},
513 { "TYER", ID3_DATE
},
514 { "TCON", ID3_GENRE
},
515 { "TPOS", ID3_DISC
},
516 { "TRCK", ID3_TRACK
},
517 { "TPE2", ID3_ALBUMARTIST
},
519 /* obsolete frames (2.2.0) */
520 { "TP1", ID3_ARTIST
},
521 { "TAL", ID3_ALBUM
},
522 { "TT2", ID3_TITLE
},
524 { "TCO", ID3_GENRE
},
526 { "TRK", ID3_TRACK
},
531 static int frame_tab_index(const char *id
)
535 for (i
= 0; frame_tab
[i
].key
!= -1; i
++) {
536 if (!strncmp(id
, frame_tab
[i
].name
, 4))
542 static void fix_date(char *buf
)
544 const char *ptr
= buf
;
549 if (ch
>= '0' && ch
<= '9') {
554 // number which length is 4, must be year
555 memmove(buf
, ptr
- 5, 4);
564 static char *decode_str(const char *buf
, int len
, int encoding
)
566 char *in
, *out
= NULL
;
570 case 0x00: /* ISO-8859-1 */
571 in
= xstrndup(buf
, len
);
572 rc
= utf8_encode(in
, id3_default_charset
, &out
);
575 case 0x03: /* UTF-8 */
576 in
= xstrndup(buf
, len
);
577 if (u_is_valid(in
)) {
580 rc
= utf8_encode(in
, id3_default_charset
, &out
);
584 case 0x01: /* UTF-16 */
585 out
= utf16_to_utf8((const unsigned char *)buf
, len
);
587 case 0x02: /* UTF-16BE */
588 out
= utf16be_to_utf8((const unsigned char *)buf
, len
);
594 static void v2_add_frame(ID3
*id3
, struct v2_frame_header
*fh
, const char *buf
)
596 int idx
, encoding
= *buf
++, len
= fh
->size
- 1;
597 enum id3_key key
= NUM_ID3_KEYS
;
603 idx
= frame_tab_index(fh
->id
);
605 key
= frame_tab
[idx
].key
;
606 out
= decode_str(buf
, len
, encoding
);
610 if (key
== ID3_TRACK
|| key
== ID3_DISC
)
611 fix_track_or_disc(out
);
612 if (key
== ID3_GENRE
) {
615 id3_debug("genre before: '%s'\n", out
);
616 tmp
= parse_genre(out
);
620 if (key
== ID3_DATE
) {
621 id3_debug("date before: '%s'\n", out
);
624 id3_debug("date parsing failed\n");
630 id3_debug("%s '%s'\n", frame_tab
[idx
].name
, out
);
631 } else if (!strncmp(fh
->id
, "TXXX", 4)) {
636 /* TXXX<len><encoding><key><val> */
637 out
= decode_str(buf
, len
, encoding
);
641 id3_debug("TXXX, key = '%s'\n", out
);
642 if (!strcasecmp(out
, "replaygain_track_gain"))
643 key
= ID3_RG_TRACK_GAIN
;
644 if (!strcasecmp(out
, "replaygain_track_peak"))
645 key
= ID3_RG_TRACK_PEAK
;
646 if (!strcasecmp(out
, "replaygain_album_gain"))
647 key
= ID3_RG_ALBUM_GAIN
;
648 if (!strcasecmp(out
, "replaygain_album_peak"))
649 key
= ID3_RG_ALBUM_PEAK
;
651 size
= strlen(out
) + 1;
654 if (key
== NUM_ID3_KEYS
)
662 out
= decode_str(buf
, len
, encoding
);
666 id3_debug("TXXX, val = '%s'\n", out
);
676 static void unsync(unsigned char *buf
, int *lenp
)
682 while (s
< len
- 1) {
683 if (buf
[s
] == 0xff && buf
[s
+ 1] == 0x00) {
684 /* 0xff 0x00 -> 0xff */
688 if (s
< len
- 2 && buf
[s
] == 0x00) {
689 /* 0xff 0x00 0x00 -> 0xff 0x00 */
700 d_print("unsyncronization removed %d bytes\n", s
- d
);
704 static int v2_read(ID3
*id3
, int fd
, const struct v2_header
*header
)
709 int frame_header_size
;
711 buf_size
= header
->size
;
712 buf
= xnew(char, buf_size
);
713 rc
= read_all(fd
, buf
, buf_size
);
720 if (header
->flags
& V2_HEADER_EXTENDED
) {
721 struct v2_extended_header ext
;
723 v2_extended_header_parse(&ext
, buf
);
724 if (ext
.size
> buf_size
) {
725 id3_debug("extended header corrupted\n");
729 frame_start
= ext
.size
;
730 /* should check if update flag is set */
733 if (header
->flags
& V2_HEADER_UNSYNC
) {
734 int len
= buf_size
- frame_start
;
736 unsync((unsigned char *)(buf
+ frame_start
), &len
);
737 buf_size
= len
+ frame_start
;
740 frame_header_size
= 10;
741 if (header
->ver_major
== 2)
742 frame_header_size
= 6;
745 while (i
< buf_size
- frame_header_size
) {
746 struct v2_frame_header fh
;
749 if (header
->ver_major
== 2) {
750 if (!v2_2_0_frame_header_parse(&fh
, buf
+ i
))
752 } else if (header
->ver_major
== 3) {
753 if (!v2_3_0_frame_header_parse(&fh
, buf
+ i
))
757 if (!v2_4_0_frame_header_parse(&fh
, buf
+ i
))
761 i
+= frame_header_size
;
762 if (fh
.size
> buf_size
- i
) {
763 id3_debug("frame too big\n");
768 if (fh
.flags
& V2_FRAME_UNSYNC
) {
771 unsync((unsigned char *)(buf
+ i
), &tmp
);
774 v2_add_frame(id3
, &fh
, buf
+ i
);
782 int id3_tag_size(const char *buf
, int buf_size
)
784 struct v2_header header
;
788 if (v2_header_parse(&header
, buf
)) {
789 if (header
.flags
& V2_HEADER_FOOTER
) {
790 /* header + data + footer */
791 id3_debug("v2.%d.%d with footer\n", header
.ver_major
, header
.ver_minor
);
792 return 10 + header
.size
+ 10;
795 id3_debug("v2.%d.%d\n", header
.ver_major
, header
.ver_minor
);
796 return 10 + header
.size
;
798 if (buf_size
>= 3 && is_v1(buf
)) {
807 return xnew0(ID3
, 1);
810 void id3_free(ID3
*id3
)
814 for (i
= 0; i
< NUM_ID3_KEYS
; i
++)
819 int id3_read_tags(ID3
*id3
, int fd
, unsigned int flags
)
824 if (flags
& ID3_V2
) {
825 struct v2_header header
;
828 rc
= read_all(fd
, buf
, 10);
831 if (v2_header_parse(&header
, buf
)) {
832 rc
= v2_read(id3
, fd
, &header
);
835 /* get v1 if needed */
837 /* get v2 from end and optionally v1 */
839 off
= lseek(fd
, -138, SEEK_END
);
842 rc
= read_all(fd
, buf
, 138);
846 if (is_v1(buf
+ 10)) {
847 if (flags
& ID3_V1
) {
848 memcpy(id3
->v1
, buf
+ 10, 128);
851 if (v2_footer_parse(&header
, buf
)) {
852 /* footer at end of file - 128 */
853 off
= lseek(fd
, -(header
.size
+ 138), SEEK_END
);
856 rc
= v2_read(id3
, fd
, &header
);
860 } else if (v2_footer_parse(&header
, buf
+ 128)) {
861 /* footer at end of file */
862 off
= lseek(fd
, -(header
.size
+ 10), SEEK_END
);
865 rc
= v2_read(id3
, fd
, &header
);
872 if (flags
& ID3_V1
) {
873 off
= lseek(fd
, -128, SEEK_END
);
876 rc
= read_all(fd
, id3
->v1
, 128);
879 id3
->has_v1
= is_v1(id3
->v1
);
888 static char *v1_get_str(const char *buf
, int len
)
894 for (i
= len
- 1; i
>= 0; i
--) {
895 if (buf
[i
] != 0 && buf
[i
] != ' ')
900 memcpy(in
, buf
, i
+ 1);
904 if (utf8_encode(in
, id3_default_charset
, &out
))
909 char *id3_get_comment(ID3
*id3
, enum id3_key key
)
913 return xstrdup(id3
->v2
[key
]);
918 return v1_get_str(id3
->v1
+ 33, 30);
920 return v1_get_str(id3
->v1
+ 63, 30);
922 return v1_get_str(id3
->v1
+ 3, 30);
924 return v1_get_str(id3
->v1
+ 93, 4);
927 unsigned char idx
= id3
->v1
[127];
929 if (idx
>= NR_GENRES
)
931 return xstrdup(genres
[idx
]);
937 if (id3
->v1
[125] != 0)
940 snprintf(t
, 4, "%d", ((unsigned char *)id3
->v1
)[126]);