1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
|
/*
* Copyright 2005 Timo Hirvonen
*/
#include "id3.h"
#include <comment.h>
#include <xmalloc.h>
#include <utf8_encode.h>
#include <uchar.h>
#include <options.h>
#include <debug.h>
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#include <stdio.h>
/*
* position:
*
* 0 "ID3"
* -10 "3DI"
* -128 "TAG"
* -138 "3DI"
*
* if v2 is at beginning _and_ at end then there must be a seek tag at beginning
*/
struct ID3 {
char v1[128];
char *v2[NUM_ID3_KEYS];
unsigned int has_v1 : 1;
unsigned int has_v2 : 1;
};
struct v2_header {
unsigned char ver_major;
unsigned char ver_minor;
unsigned char flags;
uint32_t size;
};
struct v2_extended_header {
uint32_t size;
};
struct v2_frame_header {
char id[4];
uint32_t size;
uint16_t flags;
};
#define V2_HEADER_UNSYNC (1 << 7)
#define V2_HEADER_EXTENDED (1 << 6)
#define V2_HEADER_EXPERIMENTAL (1 << 5)
#define V2_HEADER_FOOTER (1 << 4)
#define V2_FRAME_COMPRESSED (1 << 3) /* great idea!!1 */
#define V2_FRAME_ENCRYPTHED (1 << 2) /* wow, this is very neat! */
#define V2_FRAME_UNSYNC (1 << 1)
#define V2_FRAME_LEN_INDICATOR (1 << 0)
#define NR_GENRES 148
/* genres {{{ */
static const char *genres[NR_GENRES] = {
"Blues",
"Classic Rock",
"Country",
"Dance",
"Disco",
"Funk",
"Grunge",
"Hip-Hop",
"Jazz",
"Metal",
"New Age",
"Oldies",
"Other",
"Pop",
"R&B",
"Rap",
"Reggae",
"Rock",
"Techno",
"Industrial",
"Alternative",
"Ska",
"Death Metal",
"Pranks",
"Soundtrack",
"Euro-Techno",
"Ambient",
"Trip-Hop",
"Vocal",
"Jazz+Funk",
"Fusion",
"Trance",
"Classical",
"Instrumental",
"Acid",
"House",
"Game",
"Sound Clip",
"Gospel",
"Noise",
"Alt",
"Bass",
"Soul",
"Punk",
"Space",
"Meditative",
"Instrumental Pop",
"Instrumental Rock",
"Ethnic",
"Gothic",
"Darkwave",
"Techno-Industrial",
"Electronic",
"Pop-Folk",
"Eurodance",
"Dream",
"Southern Rock",
"Comedy",
"Cult",
"Gangsta Rap",
"Top 40",
"Christian Rap",
"Pop/Funk",
"Jungle",
"Native American",
"Cabaret",
"New Wave",
"Psychedelic",
"Rave",
"Showtunes",
"Trailer",
"Lo-Fi",
"Tribal",
"Acid Punk",
"Acid Jazz",
"Polka",
"Retro",
"Musical",
"Rock & Roll",
"Hard Rock",
"Folk",
"Folk/Rock",
"National Folk",
"Swing",
"Fast-Fusion",
"Bebob",
"Latin",
"Revival",
"Celtic",
"Bluegrass",
"Avantgarde",
"Gothic Rock",
"Progressive Rock",
"Psychedelic Rock",
"Symphonic Rock",
"Slow Rock",
"Big Band",
"Chorus",
"Easy Listening",
"Acoustic",
"Humour",
"Speech",
"Chanson",
"Opera",
"Chamber Music",
"Sonata",
"Symphony",
"Booty Bass",
"Primus",
"Porn Groove",
"Satire",
"Slow Jam",
"Club",
"Tango",
"Samba",
"Folklore",
"Ballad",
"Power Ballad",
"Rhythmic Soul",
"Freestyle",
"Duet",
"Punk Rock",
"Drum Solo",
"A Cappella",
"Euro-House",
"Dance Hall",
"Goa",
"Drum & Bass",
"Club-House",
"Hardcore",
"Terror",
"Indie",
"BritPop",
"Negerpunk",
"Polsk Punk",
"Beat",
"Christian Gangsta Rap",
"Heavy Metal",
"Black Metal",
"Crossover",
"Contemporary Christian",
"Christian Rock",
"Merengue",
"Salsa",
"Thrash Metal",
"Anime",
"JPop",
"Synthpop"
};
/* }}} */
#if 1
#define id3_debug(...) d_print(__VA_ARGS__)
#else
#define id3_debug(...) do { } while (0)
#endif
static int utf16_is_special(const uchar uch)
{
if (UTF16_IS_HSURROGATE(uch) || UTF16_IS_LSURROGATE(uch) || UTF16_IS_BOM(uch))
return -1;
return 0;
}
static char *utf16_to_utf8(const unsigned char *buf, int buf_size)
{
char *out;
int i, idx;
out = xnew(char, (buf_size / 2) * 4 + 1);
i = idx = 0;
while (buf_size - i >= 2) {
uchar u;
u = buf[i] + (buf[i + 1] << 8);
if (u_is_unicode(u)) {
if (utf16_is_special(u) == 0)
u_set_char(out, &idx, u);
} else {
free(out);
return NULL;
}
if (u == 0)
return out;
i += 2;
}
u_set_char(out, &idx, 0);
return out;
}
static char *utf16be_to_utf8(const unsigned char *buf, int buf_size)
{
char *out;
int i, idx;
out = xnew(char, (buf_size / 2) * 4 + 1);
i = 0;
idx = 0;
while (buf_size - i >= 2) {
uchar u;
u = buf[i + 1] + (buf[i] << 8);
if (u_is_unicode(u)) {
if (utf16_is_special(u) == 0)
u_set_char(out, &idx, u);
} else {
free(out);
return NULL;
}
if (u == 0)
return out;
i += 2;
}
u_set_char(out, &idx, 0);
return out;
}
static int is_v1(const char *buf)
{
return buf[0] == 'T' && buf[1] == 'A' && buf[2] == 'G';
}
static int u32_unsync(const unsigned char *buf, uint32_t *up)
{
uint32_t b, u = 0;
int i;
for (i = 0; i < 4; i++) {
b = buf[i];
if (b >= 0x80)
return 0;
u <<= 7;
u |= b;
}
*up = u;
return 1;
}
static void get_u32(const unsigned char *buf, uint32_t *up)
{
uint32_t b, u = 0;
int i;
for (i = 0; i < 4; i++) {
b = buf[i];
u <<= 8;
u |= b;
}
*up = u;
}
static void get_u24(const unsigned char *buf, uint32_t *up)
{
uint32_t b, u = 0;
int i;
for (i = 0; i < 3; i++) {
b = buf[i];
u <<= 8;
u |= b;
}
*up = u;
}
static int v2_header_footer_parse(struct v2_header *header, const char *buf)
{
const unsigned char *b = (const unsigned char *)buf;
header->ver_major = b[3];
header->ver_minor = b[4];
header->flags = b[5];
if (header->ver_major == 0xff || header->ver_minor == 0xff)
return 0;
return u32_unsync(b + 6, &header->size);
}
static int v2_header_parse(struct v2_header *header, const char *buf)
{
if (buf[0] != 'I' || buf[1] != 'D' || buf[2] != '3')
return 0;
return v2_header_footer_parse(header, buf);
}
static int v2_footer_parse(struct v2_header *header, const char *buf)
{
if (buf[0] != '3' || buf[1] != 'D' || buf[2] != 'I')
return 0;
return v2_header_footer_parse(header, buf);
}
static int v2_extended_header_parse(struct v2_extended_header *header, const char *buf)
{
return u32_unsync((const unsigned char *)buf, &header->size);
}
static int is_frame_id_char(char ch)
{
return (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9');
}
/* XXXYYY
*
* X = [A-Z0-9]
* Y = byte
*
* XXX is frame
* YYY is frame size excluding this 6 byte header
*/
static int v2_2_0_frame_header_parse(struct v2_frame_header *header, const char *buf)
{
int i;
for (i = 0; i < 3; i++) {
if (!is_frame_id_char(buf[i]))
return 0;
header->id[i] = buf[i];
}
header->id[3] = 0;
get_u24((const unsigned char *)(buf + 3), &header->size);
header->flags = 0;
if (header->size == 0)
return 0;
id3_debug("%c%c%c %d\n", header->id[0], header->id[1], header->id[2], header->size);
return 1;
}
/* XXXXYYYYZZ
*
* X = [A-Z0-9]
* Y = byte
* Z = byte
*
* XXXX is frame
* YYYY is frame size excluding this 10 byte header
* ZZ is flags
*/
static int v2_3_0_frame_header_parse(struct v2_frame_header *header, const char *buf)
{
int i;
for (i = 0; i < 4; i++) {
if (!is_frame_id_char(buf[i]))
return 0;
header->id[i] = buf[i];
}
get_u32((const unsigned char *)(buf + 4), &header->size);
header->flags = (buf[8] << 8) | buf[9];
if (header->size == 0)
return 0;
id3_debug("%c%c%c%c %d\n", header->id[0], header->id[1], header->id[2],
header->id[3], header->size);
return 1;
}
/* same as 2.3 but header size is sync safe */
static int v2_4_0_frame_header_parse(struct v2_frame_header *header, const char *buf)
{
int i;
for (i = 0; i < 4; i++) {
if (!is_frame_id_char(buf[i]))
return 0;
header->id[i] = buf[i];
}
if (!u32_unsync((const unsigned char *)(buf + 4), &header->size))
return 0;
header->flags = (buf[8] << 8) | buf[9];
if (header->size == 0)
return 0;
id3_debug("%c%c%c%c %d\n", header->id[0], header->id[1], header->id[2],
header->id[3], header->size);
return 1;
}
static int read_all(int fd, char *buf, size_t size)
{
size_t pos = 0;
while (pos < size) {
int rc = read(fd, buf + pos, size - pos);
if (rc == -1) {
if (errno == EINTR || errno == EAGAIN)
continue;
return -1;
}
pos += rc;
}
return 0;
}
static char *parse_genre(const char *str)
{
int parenthesis = 0;
long int idx;
char *end;
if (strncasecmp(str, "(RX", 3) == 0)
return xstrdup("Remix");
if (strncasecmp(str, "(CR", 3) == 0)
return xstrdup("Cover");
if (*str == '(') {
parenthesis = 1;
str++;
}
idx = strtol(str, &end, 10);
if (str != end) {
/* Number parsed but there may be some crap after the number.
* I don't care, ID3v2 by definition contains crap.
*/
if (idx >= 0 && idx < NR_GENRES)
return xstrdup(genres[idx]);
}
if (parenthesis) {
const char *ptr = strchr(str, ')');
if (ptr && ptr[1]) {
/* genre name after random crap in parenthesis,
* return the genre name */
return xstrdup(ptr + 1);
}
str--;
}
/* random crap, just return it and wait for a bug report */
return xstrdup(str);
}
/* http://www.id3.org/id3v2.4.0-structure.txt */
static struct {
const char name[8];
enum id3_key key;
} frame_tab[] = {
/* 2.4.0 */
{ "TDRC", ID3_DATE },
/* >= 2.3.0 */
{ "TPE1", ID3_ARTIST },
{ "TALB", ID3_ALBUM },
{ "TIT2", ID3_TITLE },
{ "TYER", ID3_DATE },
{ "TCON", ID3_GENRE },
{ "TPOS", ID3_DISC },
{ "TRCK", ID3_TRACK },
/* obsolete frames (2.2.0) */
{ "TP1", ID3_ARTIST },
{ "TAL", ID3_ALBUM },
{ "TT2", ID3_TITLE },
{ "TYE", ID3_DATE },
{ "TCO", ID3_GENRE },
{ "TPA", ID3_DISC },
{ "TRK", ID3_TRACK },
{ "", -1 }
};
static void v2_add_frame(ID3 *id3, struct v2_frame_header *fh, const char *buf)
{
int i, encoding = *buf++, len = fh->size - 1;
if (encoding > 3)
return;
for (i = 0; frame_tab[i].key != -1; i++) {
enum id3_key key = frame_tab[i].key;
char *in, *out;
int rc;
if (strncmp(fh->id, frame_tab[i].name, 4))
continue;
switch (encoding) {
case 0x00: /* ISO-8859-1 */
in = xstrndup(buf, len);
rc = utf8_encode(in, id3_default_charset, &out);
free(in);
if (rc)
return;
break;
case 0x03: /* UTF-8 */
in = xstrndup(buf, len);
if (u_is_valid(in)) {
out = in;
} else {
rc = utf8_encode(in, id3_default_charset, &out);
free(in);
if (rc)
return;
}
break;
case 0x01: /* UTF-16 */
out = utf16_to_utf8((const unsigned char *)buf, len);
if (out == NULL)
return;
break;
case 0x02: /* UTF-16BE */
out = utf16be_to_utf8((const unsigned char *)buf, len);
if (out == NULL)
return;
break;
}
if (key == ID3_TRACK || key == ID3_DISC)
fix_track_or_disc(out);
if (key == ID3_GENRE) {
char *tmp;
id3_debug("genre before: '%s'\n", out);
tmp = parse_genre(out);
free(out);
out = tmp;
}
free(id3->v2[key]);
id3->v2[key] = out;
id3->has_v2 = 1;
id3_debug("%s '%s'\n", frame_tab[i].name, out);
break;
}
}
static void unsync(unsigned char *buf, int *lenp)
{
int len = *lenp;
int s, d;
s = d = 0;
while (s < len - 1) {
if (buf[s] == 0xff && buf[s + 1] == 0x00) {
/* 0xff 0x00 -> 0xff */
buf[d++] = 0xff;
s += 2;
if (s < len - 2 && buf[s] == 0x00) {
/* 0xff 0x00 0x00 -> 0xff 0x00 */
buf[d++] = 0x00;
s++;
}
continue;
}
buf[d++] = buf[s++];
}
if (s < len)
buf[d++] = buf[s++];
d_print("unsyncronization removed %d bytes\n", s - d);
*lenp = d;
}
static int v2_read(ID3 *id3, int fd, const struct v2_header *header)
{
char *buf;
int rc, buf_size;
int frame_start, i;
int frame_header_size;
buf_size = header->size;
buf = xnew(char, buf_size);
rc = read_all(fd, buf, buf_size);
if (rc) {
free(buf);
return rc;
}
frame_start = 0;
if (header->flags & V2_HEADER_EXTENDED) {
struct v2_extended_header ext;
v2_extended_header_parse(&ext, buf);
if (ext.size > buf_size) {
id3_debug("extended header corrupted\n");
free(buf);
return -2;
}
frame_start = ext.size;
/* should check if update flag is set */
}
if (header->flags & V2_HEADER_UNSYNC) {
int len = buf_size - frame_start;
unsync((unsigned char *)(buf + frame_start), &len);
buf_size = len + frame_start;
}
frame_header_size = 10;
if (header->ver_major == 2)
frame_header_size = 6;
i = frame_start;
while (i < buf_size - frame_header_size) {
struct v2_frame_header fh;
int len;
if (header->ver_major == 2) {
if (!v2_2_0_frame_header_parse(&fh, buf + i))
break;
} else if (header->ver_major == 3) {
if (!v2_3_0_frame_header_parse(&fh, buf + i))
break;
} else {
/* assume v2.4 */
if (!v2_4_0_frame_header_parse(&fh, buf + i))
break;
}
i += frame_header_size;
if (fh.size > buf_size - i) {
id3_debug("frame too big\n");
break;
}
len = fh.size;
if (fh.flags & V2_FRAME_UNSYNC) {
int tmp = len;
unsync((unsigned char *)(buf + i), &tmp);
fh.size = tmp;
}
v2_add_frame(id3, &fh, buf + i);
i += len;
}
free(buf);
return 0;
}
int id3_tag_size(const char *buf, int buf_size)
{
struct v2_header header;
if (buf_size < 10)
return 0;
if (v2_header_parse(&header, buf)) {
if (header.flags & V2_HEADER_FOOTER) {
/* header + data + footer */
id3_debug("v2.%d.%d with footer\n", header.ver_major, header.ver_minor);
return 10 + header.size + 10;
}
/* header */
id3_debug("v2.%d.%d\n", header.ver_major, header.ver_minor);
return 10 + header.size;
}
if (buf_size >= 3 && is_v1(buf)) {
id3_debug("v1\n");
return 128;
}
return 0;
}
ID3 *id3_new(void)
{
return xnew0(ID3, 1);
}
void id3_free(ID3 *id3)
{
int i;
for (i = 0; i < NUM_ID3_KEYS; i++)
free(id3->v2[i]);
free(id3);
}
int id3_read_tags(ID3 *id3, int fd, unsigned int flags)
{
off_t off;
int rc;
if (flags & ID3_V2) {
struct v2_header header;
char buf[138];
rc = read_all(fd, buf, 10);
if (rc)
goto rc_error;
if (v2_header_parse(&header, buf)) {
rc = v2_read(id3, fd, &header);
if (rc)
goto rc_error;
/* get v1 if needed */
} else {
/* get v2 from end and optionally v1 */
off = lseek(fd, -138, SEEK_END);
if (off == -1)
goto error;
rc = read_all(fd, buf, 138);
if (rc)
goto rc_error;
if (is_v1(buf + 10)) {
if (flags & ID3_V1) {
memcpy(id3->v1, buf + 10, 128);
id3->has_v1 = 1;
}
if (v2_footer_parse(&header, buf)) {
/* footer at end of file - 128 */
off = lseek(fd, -(header.size + 138), SEEK_END);
if (off == -1)
goto error;
rc = v2_read(id3, fd, &header);
if (rc)
goto rc_error;
}
} else if (v2_footer_parse(&header, buf + 128)) {
/* footer at end of file */
off = lseek(fd, -(header.size + 10), SEEK_END);
if (off == -1)
goto error;
rc = v2_read(id3, fd, &header);
if (rc)
goto rc_error;
}
return 0;
}
}
if (flags & ID3_V1) {
off = lseek(fd, -128, SEEK_END);
if (off == -1)
goto error;
rc = read_all(fd, id3->v1, 128);
if (rc)
goto rc_error;
id3->has_v1 = is_v1(id3->v1);
}
return 0;
error:
rc = -1;
rc_error:
return rc;
}
static char *v1_get_str(const char *buf, int len)
{
char in[32];
char *out;
int i;
for (i = len - 1; i >= 0; i--) {
if (buf[i] != 0 && buf[i] != ' ')
break;
}
if (i == -1)
return NULL;
memcpy(in, buf, i + 1);
in[i + 1] = 0;
if (u_is_valid(in))
return xstrdup(in);
if (utf8_encode(in, id3_default_charset, &out))
return NULL;
return out;
}
char *id3_get_comment(ID3 *id3, enum id3_key key)
{
if (id3->has_v2) {
if (id3->v2[key])
return xstrdup(id3->v2[key]);
}
if (id3->has_v1) {
switch (key) {
case ID3_ARTIST:
return v1_get_str(id3->v1 + 33, 30);
case ID3_ALBUM:
return v1_get_str(id3->v1 + 63, 30);
case ID3_TITLE:
return v1_get_str(id3->v1 + 3, 30);
case ID3_DATE:
return v1_get_str(id3->v1 + 93, 4);
case ID3_GENRE:
{
unsigned char idx = id3->v1[127];
if (idx >= NR_GENRES)
return NULL;
return xstrdup(genres[idx]);
}
case ID3_DISC:
return NULL;
case ID3_TRACK:
{
char *t;
if (id3->v1[125] != 0)
return NULL;
t = xnew(char, 4);
snprintf(t, 4, "%d", ((unsigned char *)id3->v1)[126]);
return t;
}
}
}
return NULL;
}
|