| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "id3.h" |
| 18 |
#include "id3_genre.dat" |
| 19 |
#include "id3_compat.c" |
| 20 |
#include "id3_frametype.c" |
| 21 |
|
| 22 |
#define NGENRES (sizeof(genre_table) / sizeof(genre_table[0])) |
| 23 |
|
| 24 |
|
| 25 |
static int |
| 26 |
_varint(unsigned char *buf, int length) |
| 27 |
{ |
| 28 |
int i, b, number = 0; |
| 29 |
|
| 30 |
if (buf) { |
| 31 |
for ( i = 0; i < length; i++ ) { |
| 32 |
b = length - 1 - i; |
| 33 |
number = number | (unsigned int)( buf[i] & 0xff ) << ( 8*b ); |
| 34 |
} |
| 35 |
return number; |
| 36 |
} |
| 37 |
else { |
| 38 |
return 0; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
int |
| 43 |
parse_id3(PerlIO *infile, char *file, HV *info, HV *tags, off_t seek, off_t file_size) |
| 44 |
{ |
| 45 |
int err = 0; |
| 46 |
unsigned char *bptr; |
| 47 |
|
| 48 |
id3info *id3; |
| 49 |
Newz(0, id3, sizeof(id3info), id3info); |
| 50 |
Newz(0, id3->buf, sizeof(Buffer), Buffer); |
| 51 |
Newz(0, id3->utf8, sizeof(Buffer), Buffer); |
| 52 |
|
| 53 |
id3->infile = infile; |
| 54 |
id3->file = file; |
| 55 |
id3->info = info; |
| 56 |
id3->tags = tags; |
| 57 |
id3->offset = seek; |
| 58 |
|
| 59 |
buffer_init(id3->buf, ID3_BLOCK_SIZE); |
| 60 |
|
| 61 |
if ( !seek ) { |
| 62 |
|
| 63 |
PerlIO_seek(infile, file_size - 128, SEEK_SET); |
| 64 |
if ( !_check_buf(infile, id3->buf, 128, 128) ) { |
| 65 |
err = -1; |
| 66 |
goto out; |
| 67 |
} |
| 68 |
|
| 69 |
bptr = buffer_ptr(id3->buf); |
| 70 |
if (bptr[0] == 'T' && bptr[1] == 'A' && bptr[2] == 'G') { |
| 71 |
_id3_parse_v1(id3); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
PerlIO_seek(infile, seek, SEEK_SET); |
| 77 |
buffer_clear(id3->buf); |
| 78 |
|
| 79 |
|
| 80 |
if ( !_check_buf(infile, id3->buf, 14, ID3_BLOCK_SIZE) ) { |
| 81 |
err = -1; |
| 82 |
goto out; |
| 83 |
} |
| 84 |
|
| 85 |
bptr = buffer_ptr(id3->buf); |
| 86 |
if (bptr[0] == 'I' && bptr[1] == 'D' && bptr[2] == '3') { |
| 87 |
_id3_parse_v2(id3); |
| 88 |
} |
| 89 |
|
| 90 |
out: |
| 91 |
buffer_free(id3->buf); |
| 92 |
Safefree(id3->buf); |
| 93 |
|
| 94 |
if (id3->utf8->alloc) |
| 95 |
buffer_free(id3->utf8); |
| 96 |
Safefree(id3->utf8); |
| 97 |
|
| 98 |
Safefree(id3); |
| 99 |
|
| 100 |
return err; |
| 101 |
} |
| 102 |
|
| 103 |
int |
| 104 |
_id3_parse_v1(id3info *id3) |
| 105 |
{ |
| 106 |
SV *tmp = NULL; |
| 107 |
uint8_t read = 0; |
| 108 |
unsigned char *bptr; |
| 109 |
uint8_t comment_len; |
| 110 |
uint8_t genre; |
| 111 |
|
| 112 |
buffer_consume(id3->buf, 3); |
| 113 |
|
| 114 |
read = _id3_get_v1_utf8_string(id3, &tmp, 30); |
| 115 |
if (tmp && SvPOK(tmp) && sv_len(tmp)) { |
| 116 |
DEBUG_TRACE("ID3v1 title: %s\n", SvPVX(tmp)); |
| 117 |
my_hv_store( id3->tags, ID3_FRAME_TITLE, tmp ); |
| 118 |
} |
| 119 |
else { |
| 120 |
if (tmp) SvREFCNT_dec(tmp); |
| 121 |
} |
| 122 |
if (read < 30) { |
| 123 |
buffer_consume(id3->buf, 30 - read); |
| 124 |
} |
| 125 |
|
| 126 |
tmp = NULL; |
| 127 |
read = _id3_get_v1_utf8_string(id3, &tmp, 30); |
| 128 |
if (tmp && SvPOK(tmp) && sv_len(tmp)) { |
| 129 |
DEBUG_TRACE("ID3v1 artist: %s\n", SvPVX(tmp)); |
| 130 |
my_hv_store( id3->tags, ID3_FRAME_ARTIST, tmp ); |
| 131 |
tmp = NULL; |
| 132 |
} |
| 133 |
else { |
| 134 |
if (tmp) SvREFCNT_dec(tmp); |
| 135 |
} |
| 136 |
if (read < 30) { |
| 137 |
buffer_consume(id3->buf, 30 - read); |
| 138 |
} |
| 139 |
|
| 140 |
tmp = NULL; |
| 141 |
read = _id3_get_v1_utf8_string(id3, &tmp, 30); |
| 142 |
if (tmp && SvPOK(tmp) && sv_len(tmp)) { |
| 143 |
DEBUG_TRACE("ID3v1 album: %s\n", SvPVX(tmp)); |
| 144 |
my_hv_store( id3->tags, ID3_FRAME_ALBUM, tmp ); |
| 145 |
tmp = NULL; |
| 146 |
} |
| 147 |
else { |
| 148 |
if (tmp) SvREFCNT_dec(tmp); |
| 149 |
} |
| 150 |
if (read < 30) { |
| 151 |
buffer_consume(id3->buf, 30 - read); |
| 152 |
} |
| 153 |
|
| 154 |
tmp = NULL; |
| 155 |
read = _id3_get_v1_utf8_string(id3, &tmp, 4); |
| 156 |
if (tmp && SvPOK(tmp) && sv_len(tmp)) { |
| 157 |
DEBUG_TRACE("ID3v1 year: %s\n", SvPVX(tmp)); |
| 158 |
my_hv_store( id3->tags, ID3_FRAME_YEAR, tmp ); |
| 159 |
tmp = NULL; |
| 160 |
} |
| 161 |
else { |
| 162 |
if (tmp) SvREFCNT_dec(tmp); |
| 163 |
} |
| 164 |
if (read < 4) { |
| 165 |
buffer_consume(id3->buf, 4 - read); |
| 166 |
} |
| 167 |
|
| 168 |
bptr = buffer_ptr(id3->buf); |
| 169 |
if (bptr[28] == 0 && bptr[29] != 0) { |
| 170 |
|
| 171 |
comment_len = 28; |
| 172 |
my_hv_store( id3->tags, ID3_FRAME_TRACK, newSVuv(bptr[29]) ); |
| 173 |
my_hv_store( id3->info, "id3_version", newSVpv( "ID3v1.1", 0 ) ); |
| 174 |
} |
| 175 |
else { |
| 176 |
comment_len = 30; |
| 177 |
my_hv_store( id3->info, "id3_version", newSVpv( "ID3v1", 0 ) ); |
| 178 |
} |
| 179 |
|
| 180 |
tmp = NULL; |
| 181 |
read = _id3_get_v1_utf8_string(id3, &tmp, comment_len); |
| 182 |
if (tmp && SvPOK(tmp) && sv_len(tmp)) { |
| 183 |
AV *comment_array = newAV(); |
| 184 |
av_push( comment_array, newSVpvn("XXX", 3) ); |
| 185 |
av_push( comment_array, newSVpvn("", 0) ); |
| 186 |
av_push( comment_array, tmp ); |
| 187 |
DEBUG_TRACE("ID3v1 comment: %s\n", SvPVX(tmp)); |
| 188 |
my_hv_store( id3->tags, ID3_FRAME_COMMENT, newRV_noinc( (SV *)comment_array ) ); |
| 189 |
tmp = NULL; |
| 190 |
} |
| 191 |
else { |
| 192 |
if (tmp) SvREFCNT_dec(tmp); |
| 193 |
} |
| 194 |
if (read < 30) { |
| 195 |
buffer_consume(id3->buf, 30 - read); |
| 196 |
} |
| 197 |
|
| 198 |
genre = buffer_get_char(id3->buf); |
| 199 |
if (genre < NGENRES) { |
| 200 |
char const *genre_string = _id3_genre_index(genre); |
| 201 |
my_hv_store( id3->tags, ID3_FRAME_GENRE, newSVpv(genre_string, 0) ); |
| 202 |
} |
| 203 |
else if (genre < 255) { |
| 204 |
my_hv_store( id3->tags, ID3_FRAME_GENRE, newSVpvf("Unknown/%d", genre) ); |
| 205 |
} |
| 206 |
|
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
int |
| 211 |
_id3_parse_v2(id3info *id3) |
| 212 |
{ |
| 213 |
int ret = 1; |
| 214 |
unsigned char *bptr; |
| 215 |
|
| 216 |
|
| 217 |
bptr = buffer_ptr(id3->buf); |
| 218 |
if ( !( |
| 219 |
bptr[3] < 0xff && bptr[4] < 0xff && |
| 220 |
bptr[6] < 0x80 && bptr[7] < 0x80 && bptr[8] < 0x80 && bptr[9] < 0x80 |
| 221 |
) ) { |
| 222 |
PerlIO_printf(PerlIO_stderr(), "Invalid ID3v2 tag in %s\n", id3->file); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
buffer_consume(id3->buf, 3); |
| 227 |
|
| 228 |
id3->version_major = buffer_get_char(id3->buf); |
| 229 |
id3->version_minor = buffer_get_char(id3->buf); |
| 230 |
id3->flags = buffer_get_char(id3->buf); |
| 231 |
id3->size = 10 + buffer_get_syncsafe(id3->buf, 4); |
| 232 |
|
| 233 |
id3->size_remain = id3->size - 10; |
| 234 |
|
| 235 |
if (id3->flags & ID3_TAG_FLAG_FOOTERPRESENT) { |
| 236 |
id3->size += 10; |
| 237 |
} |
| 238 |
|
| 239 |
DEBUG_TRACE("Parsing ID3v2.%d.%d tag, flags %x, size %d\n", id3->version_major, id3->version_minor, id3->flags, id3->size); |
| 240 |
|
| 241 |
if (id3->flags & ID3_TAG_FLAG_UNSYNCHRONISATION) { |
| 242 |
if (id3->version_major < 4) { |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if ( !_check_buf(id3->infile, id3->buf, id3->size, id3->size) ) { |
| 251 |
ret = 0; |
| 252 |
goto out; |
| 253 |
} |
| 254 |
|
| 255 |
id3->size_remain = _id3_deunsync( buffer_ptr(id3->buf), id3->size ); |
| 256 |
|
| 257 |
DEBUG_TRACE(" Un-synchronized tag, new_size %d\n", id3->size_remain); |
| 258 |
|
| 259 |
my_hv_store( id3->info, "id3_was_unsynced", newSVuv(1) ); |
| 260 |
} |
| 261 |
else { |
| 262 |
DEBUG_TRACE(" Ignoring v2.4 tag un-synchronize flag\n"); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
if (id3->flags & ID3_TAG_FLAG_EXTENDEDHEADER) { |
| 267 |
uint32_t ehsize; |
| 268 |
|
| 269 |
|
| 270 |
if (id3->version_major == 2) { |
| 271 |
ret = 0; |
| 272 |
goto out; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
if (id3->version_major == 3) { |
| 280 |
|
| 281 |
ehsize = buffer_get_int(id3->buf); |
| 282 |
} |
| 283 |
else { |
| 284 |
|
| 285 |
ehsize = buffer_get_syncsafe(id3->buf, 4); |
| 286 |
|
| 287 |
if (ehsize < 4 ) { |
| 288 |
warn("Error: Invalid ID3 extended header - too short (%s)\n", id3->file); |
| 289 |
ret = 0; |
| 290 |
goto out; |
| 291 |
} |
| 292 |
ehsize -= 4; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
if (ehsize > id3->size_remain - 4) { |
| 297 |
warn("Error: Invalid ID3 extended header size (%s)\n", id3->file); |
| 298 |
ret = 0; |
| 299 |
goto out; |
| 300 |
} |
| 301 |
|
| 302 |
DEBUG_TRACE(" Skipping extended header, size %d\n", ehsize); |
| 303 |
|
| 304 |
if ( !_check_buf(id3->infile, id3->buf, ehsize, ID3_BLOCK_SIZE) ) { |
| 305 |
ret = 0; |
| 306 |
goto out; |
| 307 |
} |
| 308 |
buffer_consume(id3->buf, ehsize); |
| 309 |
|
| 310 |
id3->size_remain -= ehsize + 4; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
while (id3->size_remain > 0) { |
| 315 |
|
| 316 |
if ( !_id3_parse_v2_frame(id3) ) { |
| 317 |
break; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
if (id3->version_major < 4) { |
| 322 |
|
| 323 |
|
| 324 |
_id3_convert_tdrc(id3); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
{ |
| 329 |
SV *version = newSVpvf( "ID3v2.%d.%d", id3->version_major, id3->version_minor ); |
| 330 |
|
| 331 |
if ( my_hv_exists(id3->info, "id3_version") ) { |
| 332 |
SV **entry = my_hv_fetch(id3->info, "id3_version"); |
| 333 |
if (entry != NULL) { |
| 334 |
sv_catpv( version, ", " ); |
| 335 |
sv_catsv( version, *entry ); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
my_hv_store( id3->info, "id3_version", version ); |
| 340 |
} |
| 341 |
|
| 342 |
out: |
| 343 |
return ret; |
| 344 |
} |
| 345 |
|
| 346 |
int |
| 347 |
_id3_parse_v2_frame(id3info *id3) |
| 348 |
{ |
| 349 |
int ret = 1; |
| 350 |
char id[5]; |
| 351 |
uint16_t flags = 0; |
| 352 |
uint32_t size = 0; |
| 353 |
uint32_t decoded_size = 0; |
| 354 |
uint32_t unsync_extra = 0; |
| 355 |
id3_frametype const *frametype; |
| 356 |
Buffer *tmp_buf = 0; |
| 357 |
|
| 358 |
|
| 359 |
Buffer *decompressed = 0; |
| 360 |
|
| 361 |
|
| 362 |
id3->tag_data_safe = 1; |
| 363 |
|
| 364 |
if ( !_check_buf(id3->infile, id3->buf, 10, ID3_BLOCK_SIZE) ) { |
| 365 |
ret = 0; |
| 366 |
goto out; |
| 367 |
} |
| 368 |
|
| 369 |
if (id3->version_major == 2) { |
| 370 |
|
| 371 |
id3_compat const *compat; |
| 372 |
|
| 373 |
|
| 374 |
buffer_get(id3->buf, &id, 3); |
| 375 |
id[3] = 0; |
| 376 |
|
| 377 |
if (id[0] == 0) { |
| 378 |
|
| 379 |
DEBUG_TRACE(" Found start of padding, aborting\n"); |
| 380 |
ret = 0; |
| 381 |
goto out; |
| 382 |
} |
| 383 |
|
| 384 |
size = buffer_get_int24(id3->buf); |
| 385 |
|
| 386 |
DEBUG_TRACE(" %s, size %d\n", id, size); |
| 387 |
|
| 388 |
|
| 389 |
compat = _id3_compat_lookup((char *)&id, 3); |
| 390 |
if (compat && compat->equiv) { |
| 391 |
strncpy(id, compat->equiv, 4); |
| 392 |
id[4] = 0; |
| 393 |
|
| 394 |
DEBUG_TRACE(" compat -> %s\n", id); |
| 395 |
} |
| 396 |
else { |
| 397 |
|
| 398 |
id[4] = 0; |
| 399 |
id[3] = id[2]; |
| 400 |
id[2] = id[1]; |
| 401 |
id[1] = id[0]; |
| 402 |
id[0] = 'Y'; |
| 403 |
|
| 404 |
DEBUG_TRACE(" obsolete/unknown -> %s\n", id); |
| 405 |
} |
| 406 |
|
| 407 |
id3->size_remain -= 6; |
| 408 |
|
| 409 |
if (size > id3->size_remain) { |
| 410 |
DEBUG_TRACE(" frame size too big, aborting\n"); |
| 411 |
ret = 0; |
| 412 |
goto out; |
| 413 |
} |
| 414 |
} |
| 415 |
else { |
| 416 |
|
| 417 |
buffer_get(id3->buf, &id, 4); |
| 418 |
id[4] = 0; |
| 419 |
|
| 420 |
if (id[0] == 0) { |
| 421 |
|
| 422 |
DEBUG_TRACE(" Found start of padding, aborting\n"); |
| 423 |
ret = 0; |
| 424 |
goto out; |
| 425 |
} |
| 426 |
|
| 427 |
id3->size_remain -= 4; |
| 428 |
|
| 429 |
if (id3->version_major == 3) { |
| 430 |
|
| 431 |
id3_compat const *compat; |
| 432 |
|
| 433 |
size = buffer_get_int(id3->buf); |
| 434 |
flags = buffer_get_short(id3->buf); |
| 435 |
|
| 436 |
DEBUG_TRACE(" %s, frame flags %x, size %d\n", id, flags, size); |
| 437 |
|
| 438 |
|
| 439 |
if (id[3] == ' ') { |
| 440 |
|
| 441 |
|
| 442 |
compat = _id3_compat_lookup((char *)&id, 3); |
| 443 |
} |
| 444 |
else { |
| 445 |
compat = _id3_compat_lookup((char *)&id, 4); |
| 446 |
} |
| 447 |
if (compat && compat->equiv) { |
| 448 |
strncpy(id, compat->equiv, 4); |
| 449 |
id[4] = 0; |
| 450 |
|
| 451 |
DEBUG_TRACE(" compat -> %s\n", id); |
| 452 |
} |
| 453 |
|
| 454 |
id3->size_remain -= 6; |
| 455 |
|
| 456 |
if (size > id3->size_remain) { |
| 457 |
DEBUG_TRACE(" frame size too big, aborting\n"); |
| 458 |
ret = 0; |
| 459 |
goto out; |
| 460 |
} |
| 461 |
|
| 462 |
if (flags & ID3_FRAME_FLAG_V23_COMPRESSION) { |
| 463 |
|
| 464 |
decoded_size = buffer_get_int(id3->buf); |
| 465 |
id3->size_remain -= 4; |
| 466 |
size -= 4; |
| 467 |
} |
| 468 |
|
| 469 |
if (flags & ID3_FRAME_FLAG_V23_ENCRYPTION) { |
| 470 |
|
| 471 |
#ifdef AUDIO_SCAN_DEBUG |
| 472 |
DEBUG_TRACE(" encrypted, method %d\n", buffer_get_char(id3->buf)); |
| 473 |
#else |
| 474 |
buffer_consume(id3->buf, 1); |
| 475 |
#endif |
| 476 |
|
| 477 |
id3->size_remain--; |
| 478 |
size--; |
| 479 |
|
| 480 |
DEBUG_TRACE(" skipping encrypted frame\n"); |
| 481 |
_id3_skip(id3, size); |
| 482 |
id3->size_remain -= size; |
| 483 |
goto out; |
| 484 |
} |
| 485 |
|
| 486 |
if (flags & ID3_FRAME_FLAG_V23_GROUPINGIDENTITY) { |
| 487 |
|
| 488 |
#ifdef AUDIO_SCAN_DEBUG |
| 489 |
DEBUG_TRACE(" group_id %d\n", buffer_get_char(id3->buf)); |
| 490 |
#else |
| 491 |
buffer_consume(id3->buf, 1); |
| 492 |
#endif |
| 493 |
|
| 494 |
id3->size_remain--; |
| 495 |
size--; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
if (flags & ID3_FRAME_FLAG_V23_COMPRESSION && decoded_size) { |
| 501 |
unsigned long tmp_size; |
| 502 |
|
| 503 |
if ( !_check_buf(id3->infile, id3->buf, size, ID3_BLOCK_SIZE) ) { |
| 504 |
ret = 0; |
| 505 |
goto out; |
| 506 |
} |
| 507 |
|
| 508 |
DEBUG_TRACE(" decompressing, decoded_size %d\n", decoded_size); |
| 509 |
|
| 510 |
Newz(0, decompressed, sizeof(Buffer), Buffer); |
| 511 |
buffer_init(decompressed, decoded_size); |
| 512 |
|
| 513 |
tmp_size = decoded_size; |
| 514 |
if ( |
| 515 |
uncompress(buffer_ptr(decompressed), &tmp_size, buffer_ptr(id3->buf), size) != Z_OK |
| 516 |
|| |
| 517 |
tmp_size != decoded_size |
| 518 |
) { |
| 519 |
DEBUG_TRACE(" unable to decompress frame\n"); |
| 520 |
buffer_free(decompressed); |
| 521 |
Safefree(decompressed); |
| 522 |
decompressed = 0; |
| 523 |
} |
| 524 |
else { |
| 525 |
|
| 526 |
decompressed->end = decoded_size; |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
else { |
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
if ( _varint(buffer_ptr(id3->buf), 4) & 0x80 ) { |
| 535 |
size = buffer_get_int(id3->buf); |
| 536 |
DEBUG_TRACE(" found non-syncsafe iTunes size for %s, size adjusted to %d\n", id, size); |
| 537 |
} |
| 538 |
else { |
| 539 |
size = buffer_get_syncsafe(id3->buf, 4); |
| 540 |
} |
| 541 |
|
| 542 |
flags = buffer_get_short(id3->buf); |
| 543 |
|
| 544 |
id3->size_remain -= 6; |
| 545 |
|
| 546 |
DEBUG_TRACE(" %s, frame flags %x, size %d\n", id, flags, size); |
| 547 |
|
| 548 |
if (size > id3->size_remain) { |
| 549 |
DEBUG_TRACE(" frame size too big, aborting\n"); |
| 550 |
ret = 0; |
| 551 |
goto out; |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
if (id[3] == ' ') { |
| 557 |
id3_compat const *compat; |
| 558 |
compat = _id3_compat_lookup((char *)&id, 3); |
| 559 |
if (compat && compat->equiv) { |
| 560 |
strncpy(id, compat->equiv, 4); |
| 561 |
id[4] = 0; |
| 562 |
|
| 563 |
DEBUG_TRACE(" bad iTunes v2.4 tag, compat -> %s\n", id); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
if (flags & ID3_FRAME_FLAG_V24_GROUPINGIDENTITY) { |
| 568 |
|
| 569 |
#ifdef AUDIO_SCAN_DEBUG |
| 570 |
DEBUG_TRACE(" group_id %d\n", buffer_get_char(id3->buf)); |
| 571 |
#else |
| 572 |
buffer_consume(id3->buf, 1); |
| 573 |
#endif |
| 574 |
id3->size_remain--; |
| 575 |
size--; |
| 576 |
} |
| 577 |
|
| 578 |
if (flags & ID3_FRAME_FLAG_V24_ENCRYPTION) { |
| 579 |
|
| 580 |
#ifdef AUDIO_SCAN_DEBUG |
| 581 |
DEBUG_TRACE(" encrypted, method %d\n", buffer_get_char(id3->buf)); |
| 582 |
#else |
| 583 |
buffer_consume(id3->buf, 1); |
| 584 |
#endif |
| 585 |
|
| 586 |
id3->size_remain--; |
| 587 |
size--; |
| 588 |
|
| 589 |
DEBUG_TRACE(" skipping encrypted frame\n"); |
| 590 |
_id3_skip(id3, size); |
| 591 |
id3->size_remain -= size; |
| 592 |
goto out; |
| 593 |
} |
| 594 |
|
| 595 |
if (flags & ID3_FRAME_FLAG_V24_DATALENGTHINDICATOR) { |
| 596 |
decoded_size = buffer_get_syncsafe(id3->buf, 4); |
| 597 |
id3->size_remain -= 4; |
| 598 |
size -= 4; |
| 599 |
|
| 600 |
DEBUG_TRACE(" data length indicator, size %d\n", decoded_size); |
| 601 |
} |
| 602 |
|
| 603 |
if (flags & ID3_FRAME_FLAG_V24_UNSYNCHRONISATION) { |
| 604 |
|
| 605 |
|
| 606 |
if ( !strcmp(id, "APIC") && _env_true("AUDIO_SCAN_NO_ARTWORK") ) { |
| 607 |
DEBUG_TRACE(" Would un-synchronize APIC frame, but ignoring because of AUDIO_SCAN_NO_ARTWORK\n"); |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
decoded_size = 0; |
| 612 |
|
| 613 |
id3->tag_data_safe = 0; |
| 614 |
} |
| 615 |
else { |
| 616 |
|
| 617 |
if ( !_check_buf(id3->infile, id3->buf, size, ID3_BLOCK_SIZE) ) { |
| 618 |
ret = 0; |
| 619 |
goto out; |
| 620 |
} |
| 621 |
|
| 622 |
decoded_size = _id3_deunsync( buffer_ptr(id3->buf), size ); |
| 623 |
|
| 624 |
unsync_extra = size - decoded_size; |
| 625 |
|
| 626 |
DEBUG_TRACE(" Un-synchronized frame, new_size %d\n", decoded_size); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
if (flags & ID3_FRAME_FLAG_V24_COMPRESSION) { |
| 631 |
|
| 632 |
|
| 633 |
unsigned long tmp_size; |
| 634 |
|
| 635 |
if ( !_check_buf(id3->infile, id3->buf, size, ID3_BLOCK_SIZE) ) { |
| 636 |
ret = 0; |
| 637 |
goto out; |
| 638 |
} |
| 639 |
|
| 640 |
DEBUG_TRACE(" decompressing\n"); |
| 641 |
|
| 642 |
Newz(0, decompressed, sizeof(Buffer), Buffer); |
| 643 |
buffer_init(decompressed, decoded_size); |
| 644 |
|
| 645 |
tmp_size = decoded_size; |
| 646 |
if ( |
| 647 |
uncompress(buffer_ptr(decompressed), &tmp_size, buffer_ptr(id3->buf), size) != Z_OK |
| 648 |
|| |
| 649 |
tmp_size != decoded_size |
| 650 |
) { |
| 651 |
DEBUG_TRACE(" unable to decompress frame\n"); |
| 652 |
buffer_free(decompressed); |
| 653 |
Safefree(decompressed); |
| 654 |
decompressed = 0; |
| 655 |
} |
| 656 |
else { |
| 657 |
|
| 658 |
decompressed->end = decoded_size; |
| 659 |
} |
| 660 |
} |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
if ( !strcmp(id, "XHD3") || !strcmp(id, "NCON") ) { |
| 667 |
DEBUG_TRACE(" skipping large binary %s frame\n", id); |
| 668 |
_id3_skip(id3, size); |
| 669 |
id3->size_remain -= size; |
| 670 |
goto out; |
| 671 |
} |
| 672 |
|
| 673 |
frametype = _id3_frametype_lookup(id, 4); |
| 674 |
if (frametype == 0) { |
| 675 |
switch ( id[0] ) { |
| 676 |
case 'T': |
| 677 |
frametype = &id3_frametype_text; |
| 678 |
break; |
| 679 |
|
| 680 |
case 'W': |
| 681 |
frametype = &id3_frametype_url; |
| 682 |
break; |
| 683 |
|
| 684 |
case 'X': |
| 685 |
case 'Y': |
| 686 |
case 'Z': |
| 687 |
frametype = &id3_frametype_experimental; |
| 688 |
break; |
| 689 |
|
| 690 |
default: |
| 691 |
frametype = &id3_frametype_unknown; |
| 692 |
break; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
#ifdef AUDIO_SCAN_DEBUG |
| 697 |
{ |
| 698 |
int i; |
| 699 |
DEBUG_TRACE(" nfields %d:", frametype->nfields); |
| 700 |
for (i = 0; i < frametype->nfields; ++i) { |
| 701 |
DEBUG_TRACE(" %d", frametype->fields[i]); |
| 702 |
} |
| 703 |
DEBUG_TRACE("\n"); |
| 704 |
} |
| 705 |
#endif |
| 706 |
|
| 707 |
|
| 708 |
if (decompressed) { |
| 709 |
tmp_buf = id3->buf; |
| 710 |
id3->buf = decompressed; |
| 711 |
} |
| 712 |
|
| 713 |
if ( !_id3_parse_v2_frame_data(id3, (char *)&id, decoded_size ? decoded_size : size, frametype) ) { |
| 714 |
DEBUG_TRACE(" error parsing frame, aborting\n"); |
| 715 |
ret = 0; |
| 716 |
goto out; |
| 717 |
} |
| 718 |
|
| 719 |
if (id3->size_remain > size) { |
| 720 |
id3->size_remain -= size; |
| 721 |
} |
| 722 |
else { |
| 723 |
id3->size_remain = 0; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
if (unsync_extra) { |
| 728 |
DEBUG_TRACE(" consuming extra bytes after unsync: %d\n", unsync_extra); |
| 729 |
buffer_consume(id3->buf, unsync_extra); |
| 730 |
} |
| 731 |
|
| 732 |
out: |
| 733 |
if (decompressed) { |
| 734 |
|
| 735 |
id3->buf = tmp_buf; |
| 736 |
buffer_consume(id3->buf, size); |
| 737 |
|
| 738 |
buffer_free(decompressed); |
| 739 |
Safefree(decompressed); |
| 740 |
} |
| 741 |
|
| 742 |
return ret; |
| 743 |
} |
| 744 |
|
| 745 |
int |
| 746 |
_id3_parse_v2_frame_data(id3info *id3, char const *id, uint32_t size, id3_frametype const *frametype) |
| 747 |
{ |
| 748 |
int ret = 1; |
| 749 |
uint32_t read = 0; |
| 750 |
int8_t encoding = -1; |
| 751 |
|
| 752 |
uint8_t buffer_art = ( !strcmp(id, "APIC") ) ? 1 : 0; |
| 753 |
uint8_t skip_art = ( buffer_art && _env_true("AUDIO_SCAN_NO_ARTWORK") ) ? 1 : 0; |
| 754 |
|
| 755 |
|
| 756 |
if (!size) |
| 757 |
return 1; |
| 758 |
|
| 759 |
if (skip_art) { |
| 760 |
|
| 761 |
|
| 762 |
if ( !_check_buf(id3->infile, id3->buf, 128, ID3_BLOCK_SIZE) ) { |
| 763 |
return 0; |
| 764 |
} |
| 765 |
DEBUG_TRACE(" partial read due to AUDIO_SCAN_NO_ARTWORK\n"); |
| 766 |
} |
| 767 |
else { |
| 768 |
|
| 769 |
|
| 770 |
if (buffer_art) { |
| 771 |
|
| 772 |
if ( !_check_buf(id3->infile, id3->buf, 128, ID3_BLOCK_SIZE) ) { |
| 773 |
return 0; |
| 774 |
} |
| 775 |
} |
| 776 |
else { |
| 777 |
|
| 778 |
if ( !_check_buf(id3->infile, id3->buf, size, ID3_BLOCK_SIZE) ) { |
| 779 |
return 0; |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
if ( frametype->fields[0] == ID3_FIELD_TYPE_TEXTENCODING ) { |
| 785 |
|
| 786 |
encoding = buffer_get_char(id3->buf); |
| 787 |
read++; |
| 788 |
DEBUG_TRACE(" encoding: %d\n", encoding); |
| 789 |
|
| 790 |
if (encoding < 0 || encoding > 3) { |
| 791 |
DEBUG_TRACE(" invalid encoding, skipping frame\n"); |
| 792 |
goto out; |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
if ( !strcmp(id, "TXXX") || !strcmp(id, "WXXX") ) { |
| 798 |
|
| 799 |
SV *key = NULL; |
| 800 |
SV *value = NULL; |
| 801 |
AV *array = NULL; |
| 802 |
int count = 0; |
| 803 |
|
| 804 |
read += _id3_get_utf8_string(id3, &key, size - read, encoding); |
| 805 |
|
| 806 |
if (key != NULL && SvPOK(key) && sv_len(key)) { |
| 807 |
upcase(SvPVX(key)); |
| 808 |
|
| 809 |
|
| 810 |
if (frametype->fields[2] == ID3_FIELD_TYPE_LATIN1) { |
| 811 |
|
| 812 |
encoding = ISO_8859_1; |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
while (read < size) { |
| 818 |
if (count++ == 1 && value != NULL) { |
| 819 |
array = newAV(); |
| 820 |
av_push(array, value); |
| 821 |
} |
| 822 |
value = NULL; |
| 823 |
|
| 824 |
read += _id3_get_utf8_string(id3, &value, size - read, encoding); |
| 825 |
|
| 826 |
if (array != NULL && value != NULL && SvPOK(value)) { |
| 827 |
|
| 828 |
if (sv_len(value) > 0) |
| 829 |
av_push(array, value); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
if (array != NULL) { |
| 834 |
if (av_len(array) == 0) { |
| 835 |
|
| 836 |
my_hv_store_ent( id3->tags, key, av_shift(array) ); |
| 837 |
SvREFCNT_dec(array); |
| 838 |
} |
| 839 |
else { |
| 840 |
my_hv_store_ent( id3->tags, key, newRV_noinc( (SV *)array ) ); |
| 841 |
} |
| 842 |
array = NULL; |
| 843 |
} |
| 844 |
else if (value != NULL && SvPOK(value)) { |
| 845 |
my_hv_store_ent( id3->tags, key, value ); |
| 846 |
} |
| 847 |
else { |
| 848 |
if (value) SvREFCNT_dec(value); |
| 849 |
} |
| 850 |
} |
| 851 |
else { |
| 852 |
DEBUG_TRACE(" invalid/empty (T|W)XXX key, skipping frame\n"); |
| 853 |
} |
| 854 |
|
| 855 |
if (key) SvREFCNT_dec(key); |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
else if ( !strcmp(id, "TCON") ) { |
| 860 |
AV *genres = newAV(); |
| 861 |
char *sptr, *end, *tmp; |
| 862 |
|
| 863 |
while (read < size) { |
| 864 |
SV *value = NULL; |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
read += _id3_get_utf8_string(id3, &value, size - read, encoding); |
| 869 |
if (value != NULL && SvPOK(value)) { |
| 870 |
sptr = SvPVX(value); |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
end = sptr + sv_len(value); |
| 875 |
strtol(sptr, &tmp, 0); |
| 876 |
|
| 877 |
if ( tmp == end ) { |
| 878 |
|
| 879 |
av_push( genres, newSVpv( _id3_genre_name((char *)sptr), 0 ) ); |
| 880 |
|
| 881 |
|
| 882 |
SvREFCNT_dec(value); |
| 883 |
} |
| 884 |
else if ( *sptr == '(' ) { |
| 885 |
|
| 886 |
|
| 887 |
if (id3->version_major < 4) { |
| 888 |
|
| 889 |
char *ptr = sptr; |
| 890 |
char *end = sptr + sv_len(value); |
| 891 |
|
| 892 |
while (end - ptr > 0) { |
| 893 |
if ( *ptr++ == '(' ) { |
| 894 |
char *paren = strchr(ptr, ')'); |
| 895 |
if (paren == NULL) |
| 896 |
paren = end; |
| 897 |
|
| 898 |
if ( isdigit(*ptr) || !strncmp((char *)ptr, "RX", 2) || !strncmp((char *)ptr, "CR", 2) ) { |
| 899 |
av_push( genres, newSVpv( _id3_genre_name((char *)ptr), 0 ) ); |
| 900 |
} |
| 901 |
else { |
| 902 |
|
| 903 |
av_push( genres, newSVpvn(ptr, paren - ptr) ); |
| 904 |
} |
| 905 |
ptr = paren; |
| 906 |
} |
| 907 |
} |
| 908 |
} |
| 909 |
else { |
| 910 |
|
| 911 |
sptr++; |
| 912 |
if ( isdigit(*sptr) || !strncmp(sptr, "RX", 2) || !strncmp(sptr, "CR", 2) ) { |
| 913 |
av_push( genres, newSVpv( _id3_genre_name((char *)sptr), 0 ) ); |
| 914 |
} |
| 915 |
else { |
| 916 |
av_push( genres, newSVpv( (char *)sptr, 0 ) ); |
| 917 |
} |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
SvREFCNT_dec(value); |
| 922 |
} |
| 923 |
else { |
| 924 |
|
| 925 |
if ( !strncmp(sptr, "RX", 2) || !strncmp(sptr, "CR", 2) ) { |
| 926 |
av_push( genres, newSVpv( _id3_genre_name((char *)sptr), 0 ) ); |
| 927 |
|
| 928 |
|
| 929 |
SvREFCNT_dec(value); |
| 930 |
} |
| 931 |
else { |
| 932 |
|
| 933 |
av_push( genres, value ); |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
if (av_len(genres) > 0) { |
| 940 |
my_hv_store( id3->tags, id, newRV_noinc( (SV *)genres ) ); |
| 941 |
} |
| 942 |
else if (av_len(genres) == 0) { |
| 943 |
my_hv_store( id3->tags, id, av_shift(genres) ); |
| 944 |
SvREFCNT_dec(genres); |
| 945 |
} |
| 946 |
else { |
| 947 |
SvREFCNT_dec(genres); |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
else if ( |
| 955 |
frametype->nfields == 1 || |
| 956 |
(frametype->nfields == 2 && frametype->fields[0] == ID3_FIELD_TYPE_TEXTENCODING) |
| 957 |
) { |
| 958 |
int i = frametype->nfields - 1; |
| 959 |
AV *array = NULL; |
| 960 |
SV *value = NULL; |
| 961 |
int count = 0; |
| 962 |
|
| 963 |
switch ( frametype->fields[i] ) { |
| 964 |
case ID3_FIELD_TYPE_LATIN1: |
| 965 |
read += _id3_get_utf8_string(id3, &value, size - read, ISO_8859_1); |
| 966 |
if (value != NULL && SvPOK(value)) |
| 967 |
my_hv_store( id3->tags, id, value ); |
| 968 |
break; |
| 969 |
|
| 970 |
case ID3_FIELD_TYPE_STRINGLIST: |
| 971 |
|
| 972 |
|
| 973 |
while (read < size) { |
| 974 |
if (count++ == 1 && value != NULL) { |
| 975 |
|
| 976 |
array = newAV(); |
| 977 |
av_push(array, value); |
| 978 |
} |
| 979 |
value = NULL; |
| 980 |
|
| 981 |
read += _id3_get_utf8_string(id3, &value, size - read, encoding); |
| 982 |
|
| 983 |
if (array != NULL && value != NULL && SvPOK(value)) { |
| 984 |
|
| 985 |
|
| 986 |
if (sv_len(value) > 0) |
| 987 |
av_push(array, value); |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
if (array != NULL) { |
| 992 |
if (av_len(array) == 0) { |
| 993 |
|
| 994 |
my_hv_store( id3->tags, id, av_shift(array) ); |
| 995 |
SvREFCNT_dec(array); |
| 996 |
} |
| 997 |
else { |
| 998 |
my_hv_store( id3->tags, id, newRV_noinc( (SV *)array ) ); |
| 999 |
} |
| 1000 |
} |
| 1001 |
else if (value != NULL && SvPOK(value)) { |
| 1002 |
my_hv_store( id3->tags, id, value ); |
| 1003 |
} |
| 1004 |
break; |
| 1005 |
|
| 1006 |
case ID3_FIELD_TYPE_INT32: |
| 1007 |
my_hv_store( id3->tags, id, newSViv( buffer_get_int(id3->buf) ) ); |
| 1008 |
read += 4; |
| 1009 |
break; |
| 1010 |
|
| 1011 |
case ID3_FIELD_TYPE_INT32PLUS: |
| 1012 |
my_hv_store( id3->tags, id, newSViv( _varint( buffer_ptr(id3->buf), size - read ) ) ); |
| 1013 |
buffer_consume(id3->buf, size - read); |
| 1014 |
read = size; |
| 1015 |
break; |
| 1016 |
|
| 1017 |
case ID3_FIELD_TYPE_BINARYDATA: |
| 1018 |
|
| 1019 |
if ( !strcmp(id, "RVAD") ) { |
| 1020 |
read += _id3_parse_rvad(id3, id, size - read); |
| 1021 |
} |
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
else if ( !strcmp(id, "RGAD") ) { |
| 1026 |
read += _id3_parse_rgad(id3); |
| 1027 |
} |
| 1028 |
|
| 1029 |
|
| 1030 |
else { |
| 1031 |
|
| 1032 |
my_hv_store( id3->tags, id, newSVpvn( buffer_ptr(id3->buf), size - read ) ); |
| 1033 |
buffer_consume(id3->buf, size - read); |
| 1034 |
read = size; |
| 1035 |
} |
| 1036 |
break; |
| 1037 |
|
| 1038 |
default: |
| 1039 |
|
| 1040 |
warn(" !!! unhandled field type %d\n", frametype->fields[i]); |
| 1041 |
buffer_consume(id3->buf, size - read); |
| 1042 |
read += size - read; |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
else { |
| 1055 |
int i = 0; |
| 1056 |
AV *framedata = newAV(); |
| 1057 |
|
| 1058 |
|
| 1059 |
if (encoding >= 0) |
| 1060 |
i = 1; |
| 1061 |
|
| 1062 |
for (; i < frametype->nfields; i++) { |
| 1063 |
SV *value = NULL; |
| 1064 |
|
| 1065 |
switch ( frametype->fields[i] ) { |
| 1066 |
case ID3_FIELD_TYPE_LATIN1: |
| 1067 |
|
| 1068 |
|
| 1069 |
if ( id3->version_major == 2 && !strcmp(id, "APIC") ) { |
| 1070 |
av_push( framedata, newSVpvn( buffer_ptr(id3->buf), 3 ) ); |
| 1071 |
buffer_consume(id3->buf, 3); |
| 1072 |
read += 3; |
| 1073 |
DEBUG_TRACE(" PIC image format, read %d\n", read); |
| 1074 |
} |
| 1075 |
else { |
| 1076 |
read += _id3_get_utf8_string(id3, &value, size - read, ISO_8859_1); |
| 1077 |
if (value != NULL && SvPOK(value)) |
| 1078 |
av_push( framedata, value ); |
| 1079 |
} |
| 1080 |
break; |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
case ID3_FIELD_TYPE_LATIN1LIST: |
| 1085 |
while (read < size) { |
| 1086 |
read += _id3_get_utf8_string(id3, &value, size - read, ISO_8859_1); |
| 1087 |
if (value != NULL && SvPOK(value)) |
| 1088 |
av_push( framedata, value ); |
| 1089 |
value = NULL; |
| 1090 |
DEBUG_TRACE(" latin1list, read %d\n", read); |
| 1091 |
} |
| 1092 |
break; |
| 1093 |
|
| 1094 |
case ID3_FIELD_TYPE_STRING: |
| 1095 |
read += _id3_get_utf8_string(id3, &value, size - read, encoding); |
| 1096 |
if (value != NULL && SvPOK(value)) { |
| 1097 |
av_push( framedata, value ); |
| 1098 |
DEBUG_TRACE(" string, read %d: %s\n", read, SvPVX(value)); |
| 1099 |
} |
| 1100 |
else { |
| 1101 |
av_push( framedata, &PL_sv_undef ); |
| 1102 |
if (value) SvREFCNT_dec(value); |
| 1103 |
} |
| 1104 |
break; |
| 1105 |
|
| 1106 |
case ID3_FIELD_TYPE_STRINGFULL: |
| 1107 |
{ |
| 1108 |
SV *tmp = newSVpvn( "", 0 ); |
| 1109 |
while (read < size) { |
| 1110 |
read += _id3_get_utf8_string(id3, &value, size - read, encoding); |
| 1111 |
if (value != NULL && SvPOK(value)) { |
| 1112 |
sv_catsv( tmp, value ); |
| 1113 |
SvREFCNT_dec(value); |
| 1114 |
} |
| 1115 |
value = NULL; |
| 1116 |
} |
| 1117 |
av_push( framedata, tmp ); |
| 1118 |
DEBUG_TRACE(" stringfull, read %d: %s\n", read, SvPVX(tmp)); |
| 1119 |
break; |
| 1120 |
} |
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
case ID3_FIELD_TYPE_LANGUAGE: |
| 1125 |
if (size - read >= 3) { |
| 1126 |
av_push( framedata, newSVpvn( buffer_ptr(id3->buf), 3 ) ); |
| 1127 |
buffer_consume(id3->buf, 3); |
| 1128 |
read += 3; |
| 1129 |
DEBUG_TRACE(" language, read %d\n", read); |
| 1130 |
} |
| 1131 |
break; |
| 1132 |
|
| 1133 |
case ID3_FIELD_TYPE_FRAMEID: |
| 1134 |
|
| 1135 |
{ |
| 1136 |
uint8_t len = (id3->version_major == 3) ? 3 : 4; |
| 1137 |
if (size - read >= len) { |
| 1138 |
av_push( framedata, newSVpvn( buffer_ptr(id3->buf), len ) ); |
| 1139 |
buffer_consume(id3->buf, len); |
| 1140 |
read += len; |
| 1141 |
DEBUG_TRACE(" frameid, read %d\n", read); |
| 1142 |
} |
| 1143 |
break; |
| 1144 |
} |
| 1145 |
|
| 1146 |
case ID3_FIELD_TYPE_DATE: |
| 1147 |
if (size - read >= 8) { |
| 1148 |
av_push( framedata, newSVpvn( buffer_ptr(id3->buf), 8 ) ); |
| 1149 |
buffer_consume(id3->buf, 8); |
| 1150 |
read += 8; |
| 1151 |
DEBUG_TRACE(" date, read %d\n", read); |
| 1152 |
} |
| 1153 |
break; |
| 1154 |
|
| 1155 |
case ID3_FIELD_TYPE_INT8: |
| 1156 |
|
| 1157 |
if (size - read >= 1) { |
| 1158 |
av_push( framedata, newSViv( buffer_get_char(id3->buf) ) ); |
| 1159 |
read += 1; |
| 1160 |
DEBUG_TRACE(" int8, read %d\n", read); |
| 1161 |
} |
| 1162 |
break; |
| 1163 |
|
| 1164 |
case ID3_FIELD_TYPE_INT16: |
| 1165 |
if (size - read >= 2) { |
| 1166 |
av_push( framedata, newSViv( buffer_get_short(id3->buf) ) ); |
| 1167 |
read += 2; |
| 1168 |
DEBUG_TRACE(" int16, read %d\n", read); |
| 1169 |
} |
| 1170 |
break; |
| 1171 |
|
| 1172 |
case ID3_FIELD_TYPE_INT24: |
| 1173 |
if (size - read >= 3) { |
| 1174 |
av_push( framedata, newSViv( buffer_get_int24(id3->buf) ) ); |
| 1175 |
read += 3; |
| 1176 |
DEBUG_TRACE(" int24, read %d\n", read); |
| 1177 |
} |
| 1178 |
break; |
| 1179 |
|
| 1180 |
case ID3_FIELD_TYPE_INT32: |
| 1181 |
if (size - read >= 4) { |
| 1182 |
av_push( framedata, newSViv( buffer_get_int(id3->buf) ) ); |
| 1183 |
read += 4; |
| 1184 |
DEBUG_TRACE(" int32, read %d\n", read); |
| 1185 |
} |
| 1186 |
break; |
| 1187 |
|
| 1188 |
case ID3_FIELD_TYPE_INT32PLUS: |
| 1189 |
if (size - read >= 4) { |
| 1190 |
av_push( framedata, newSViv( _varint( buffer_ptr(id3->buf), size - read ) ) ); |
| 1191 |
buffer_consume(id3->buf, size - read); |
| 1192 |
read = size; |
| 1193 |
DEBUG_TRACE(" int32plus, read %d\n", read); |
| 1194 |
} |
| 1195 |
break; |
| 1196 |
|
| 1197 |
case ID3_FIELD_TYPE_BINARYDATA: |
| 1198 |
|
| 1199 |
|
| 1200 |
if (skip_art) { |
| 1201 |
av_push( framedata, newSVuv(size - read) ); |
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
if (id3->flags & ID3_TAG_FLAG_UNSYNCHRONISATION && id3->version_major < 4) { |
| 1206 |
DEBUG_TRACE(" cannot obtain APIC offset due to v2.3 unsync tag\n"); |
| 1207 |
} |
| 1208 |
else { |
| 1209 |
|
| 1210 |
if (id3->tag_data_safe && (size - read) > 0) |
| 1211 |
av_push( framedata, newSVuv(id3->offset + (id3->size - id3->size_remain) + read) ); |
| 1212 |
} |
| 1213 |
|
| 1214 |
_id3_skip(id3, size - read); |
| 1215 |
read = size; |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
else if (buffer_art) { |
| 1220 |
uint32_t remain = size - read; |
| 1221 |
uint32_t chunk_size; |
| 1222 |
SV *artwork = newSVpv("", 0); |
| 1223 |
|
| 1224 |
while (read < size) { |
| 1225 |
if ( !_check_buf(id3->infile, id3->buf, 1, ID3_BLOCK_SIZE) ) { |
| 1226 |
return 0; |
| 1227 |
} |
| 1228 |
|
| 1229 |
chunk_size = remain < buffer_len(id3->buf) ? remain : buffer_len(id3->buf); |
| 1230 |
|
| 1231 |
read += chunk_size; |
| 1232 |
remain -= chunk_size; |
| 1233 |
|
| 1234 |
sv_catpvn( artwork, buffer_ptr(id3->buf), chunk_size ); |
| 1235 |
buffer_consume(id3->buf, chunk_size); |
| 1236 |
|
| 1237 |
DEBUG_TRACE(" buffered %d bytes of APIC data (remaining %d)\n", chunk_size, remain); |
| 1238 |
} |
| 1239 |
|
| 1240 |
av_push( framedata, artwork ); |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
else if ( !strcmp(id, "RVA2") ) { |
| 1245 |
read += _id3_parse_rva2(id3, size, framedata); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
else if ( !strcmp(id, "SYLT") ) { |
| 1250 |
read += _id3_parse_sylt(id3, encoding, size - read, framedata); |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
else if ( !strcmp(id, "ETCO") ) { |
| 1255 |
read += _id3_parse_etco(id3, size - read, framedata); |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
else { |
| 1260 |
if (size - read > 1) { |
| 1261 |
av_push( framedata, newSVpvn( buffer_ptr(id3->buf), size - read ) ); |
| 1262 |
buffer_consume(id3->buf, size - read); |
| 1263 |
read = size; |
| 1264 |
DEBUG_TRACE(" binarydata, read %d\n", read); |
| 1265 |
} |
| 1266 |
} |
| 1267 |
break; |
| 1268 |
|
| 1269 |
default: |
| 1270 |
break; |
| 1271 |
} |
| 1272 |
} |
| 1273 |
|
| 1274 |
_id3_set_array_tag(id3, id, framedata); |
| 1275 |
} |
| 1276 |
|
| 1277 |
out: |
| 1278 |
if (read < size) { |
| 1279 |
buffer_consume(id3->buf, size - read); |
| 1280 |
DEBUG_TRACE(" !!! consuming extra bytes in frame: %d\n", size - read); |
| 1281 |
} |
| 1282 |
|
| 1283 |
return ret; |
| 1284 |
} |
| 1285 |
|
| 1286 |
void |
| 1287 |
_id3_set_array_tag(id3info *id3, char const *id, AV *framedata) |
| 1288 |
{ |
| 1289 |
if ( av_len(framedata) != -1 ) { |
| 1290 |
if ( my_hv_exists( id3->tags, id ) ) { |
| 1291 |
|
| 1292 |
SV **entry = my_hv_fetch( id3->tags, id ); |
| 1293 |
if (entry != NULL) { |
| 1294 |
if ( SvTYPE( SvRV(*entry) ) == SVt_PV ) { |
| 1295 |
|
| 1296 |
AV *ref = newAV(); |
| 1297 |
|
| 1298 |
|
| 1299 |
DEBUG_TRACE(" !!! converting normal string tag to array\n"); |
| 1300 |
|
| 1301 |
av_push( ref, *entry ); |
| 1302 |
av_push( ref, newRV_noinc( (SV *)framedata ) ); |
| 1303 |
my_hv_store( id3->tags, id, newRV_noinc( (SV *)ref ) ); |
| 1304 |
} |
| 1305 |
else if ( SvTYPE( SvRV(*entry) ) == SVt_PVAV ) { |
| 1306 |
|
| 1307 |
SV **first = av_fetch( (AV *)SvRV(*entry), 0, 0 ); |
| 1308 |
if ( first == NULL || ( SvROK(*first) && SvTYPE( SvRV(*first) ) == SVt_PVAV ) ) { |
| 1309 |
av_push( (AV *)SvRV(*entry), newRV_noinc( (SV *)framedata ) ); |
| 1310 |
} |
| 1311 |
else { |
| 1312 |
AV *ref = newAV(); |
| 1313 |
av_push( ref, SvREFCNT_inc(*entry) ); |
| 1314 |
av_push( ref, newRV_noinc( (SV *)framedata) ); |
| 1315 |
my_hv_store( id3->tags, id, newRV_noinc( (SV *)ref ) ); |
| 1316 |
} |
| 1317 |
} |
| 1318 |
} |
| 1319 |
} |
| 1320 |
else { |
| 1321 |
my_hv_store( id3->tags, id, newRV_noinc( (SV *)framedata ) ); |
| 1322 |
} |
| 1323 |
} |
| 1324 |
else { |
| 1325 |
SvREFCNT_dec(framedata); |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
uint32_t |
| 1332 |
_id3_get_v1_utf8_string(id3info *id3, SV **string, uint32_t len) |
| 1333 |
{ |
| 1334 |
uint32_t read = 0; |
| 1335 |
char *ptr; |
| 1336 |
char *str; |
| 1337 |
|
| 1338 |
read = _id3_get_utf8_string(id3, string, len, ISO_8859_1); |
| 1339 |
|
| 1340 |
if (read) { |
| 1341 |
|
| 1342 |
if (*string != NULL) { |
| 1343 |
str = SvPVX(*string); |
| 1344 |
ptr = str + sv_len(*string); |
| 1345 |
|
| 1346 |
while (ptr > str && ptr[-1] == ' ') |
| 1347 |
--ptr; |
| 1348 |
|
| 1349 |
*ptr = 0; |
| 1350 |
SvCUR_set(*string, ptr - str); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
return read; |
| 1355 |
} |
| 1356 |
|
| 1357 |
uint32_t |
| 1358 |
_id3_get_utf8_string(id3info *id3, SV **string, uint32_t len, uint8_t encoding) |
| 1359 |
{ |
| 1360 |
uint8_t byteorder = UTF16_BYTEORDER_ANY; |
| 1361 |
uint32_t read = 0; |
| 1362 |
unsigned char *bptr; |
| 1363 |
|
| 1364 |
|
| 1365 |
if ( !id3->utf8->alloc ) { |
| 1366 |
|
| 1367 |
|
| 1368 |
buffer_init( id3->utf8, encoding == ISO_8859_1 ? len * 2 : len ); |
| 1369 |
} |
| 1370 |
else { |
| 1371 |
|
| 1372 |
buffer_clear(id3->utf8); |
| 1373 |
} |
| 1374 |
|
| 1375 |
if ( *string != NULL ) { |
| 1376 |
warn(" !!! string SV is not null: %s\n", SvPVX(*string)); |
| 1377 |
} |
| 1378 |
|
| 1379 |
switch (encoding) { |
| 1380 |
case ISO_8859_1: |
| 1381 |
read += buffer_get_latin1_as_utf8(id3->buf, id3->utf8, len); |
| 1382 |
break; |
| 1383 |
|
| 1384 |
case UTF_16BE: |
| 1385 |
byteorder = UTF16_BYTEORDER_BE; |
| 1386 |
|
| 1387 |
case UTF_16: |
| 1388 |
bptr = buffer_ptr(id3->buf); |
| 1389 |
|
| 1390 |
switch ( (bptr[0] << 8) | bptr[1] ) { |
| 1391 |
case 0xfeff: |
| 1392 |
DEBUG_TRACE(" UTF-16 BOM is big-endian\n"); |
| 1393 |
byteorder = UTF16_BYTEORDER_BE; |
| 1394 |
buffer_consume(id3->buf, 2); |
| 1395 |
read += 2; |
| 1396 |
break; |
| 1397 |
|
| 1398 |
case 0xfffe: |
| 1399 |
DEBUG_TRACE(" UTF-16 BOM is little-endian\n"); |
| 1400 |
byteorder = UTF16_BYTEORDER_LE; |
| 1401 |
buffer_consume(id3->buf, 2); |
| 1402 |
read += 2; |
| 1403 |
break; |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
if (byteorder == UTF16_BYTEORDER_ANY) { |
| 1410 |
DEBUG_TRACE(" UTF-16 byte order defaulting to little-endian, no BOM\n"); |
| 1411 |
byteorder = UTF16_BYTEORDER_LE; |
| 1412 |
} |
| 1413 |
|
| 1414 |
read += buffer_get_utf16_as_utf8(id3->buf, id3->utf8, len - read, byteorder); |
| 1415 |
break; |
| 1416 |
|
| 1417 |
case UTF_8: |
| 1418 |
read += buffer_get_utf8(id3->buf, id3->utf8, len); |
| 1419 |
break; |
| 1420 |
|
| 1421 |
default: |
| 1422 |
break; |
| 1423 |
} |
| 1424 |
|
| 1425 |
if (read) { |
| 1426 |
if ( buffer_len(id3->utf8) ) { |
| 1427 |
*string = newSVpv( buffer_ptr(id3->utf8), 0 ); |
| 1428 |
sv_utf8_decode(*string); |
| 1429 |
DEBUG_TRACE(" read utf8 string of %d bytes: %s\n", buffer_len(id3->utf8), SvPVX(*string)); |
| 1430 |
} |
| 1431 |
else { |
| 1432 |
DEBUG_TRACE(" empty string\n"); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
return read; |
| 1437 |
} |
| 1438 |
|
| 1439 |
uint32_t |
| 1440 |
_id3_parse_rvad(id3info *id3, char const *id, uint32_t size) |
| 1441 |
{ |
| 1442 |
unsigned char *rva = buffer_ptr(id3->buf); |
| 1443 |
int sign_r = rva[0] & 0x01 ? 1 : -1; |
| 1444 |
int sign_l = rva[0] & 0x02 ? 1 : -1; |
| 1445 |
int bytes = rva[1] / 8; |
| 1446 |
float vol[2]; |
| 1447 |
float peak[2]; |
| 1448 |
int i; |
| 1449 |
AV *framedata = newAV(); |
| 1450 |
|
| 1451 |
|
| 1452 |
if (rva[0] & 0xFE || rva[1] == 0) { |
| 1453 |
return 0; |
| 1454 |
} |
| 1455 |
|
| 1456 |
|
| 1457 |
if (size != 2 + (bytes * 4)) { |
| 1458 |
return 0; |
| 1459 |
} |
| 1460 |
|
| 1461 |
rva += 2; |
| 1462 |
|
| 1463 |
vol[0] = _varint( rva, bytes ) * sign_r / 256.; |
| 1464 |
vol[1] = _varint( rva + bytes, bytes ) * sign_l / 256.; |
| 1465 |
|
| 1466 |
peak[0] = _varint( rva + (bytes * 2), bytes ); |
| 1467 |
peak[1] = _varint( rva + (bytes * 3), bytes ); |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
for (i = 0; i < 2; i++) { |
| 1472 |
if ( vol[i] == -255 ) { |
| 1473 |
vol[i] = -96.0; |
| 1474 |
} |
| 1475 |
else { |
| 1476 |
vol[i] = 20.0 * log( ( vol[i] + 255 ) / 255 ) / log(10); |
| 1477 |
} |
| 1478 |
|
| 1479 |
av_push( framedata, newSVpvf( "%f dB", vol[i] ) ); |
| 1480 |
av_push( framedata, newSVpvf( "%f", peak[i] ) ); |
| 1481 |
} |
| 1482 |
|
| 1483 |
my_hv_store( id3->tags, id, newRV_noinc( (SV *)framedata ) ); |
| 1484 |
|
| 1485 |
buffer_consume(id3->buf, 2 + (bytes * 4)); |
| 1486 |
|
| 1487 |
return 2 + (bytes * 4); |
| 1488 |
} |
| 1489 |
|
| 1490 |
uint32_t |
| 1491 |
_id3_parse_rgad(id3info *id3) |
| 1492 |
{ |
| 1493 |
float radio = 0.0; |
| 1494 |
float audiophile = 0.0; |
| 1495 |
uint8_t sign = 0; |
| 1496 |
HV *framedata = newHV(); |
| 1497 |
uint32_t read = 0; |
| 1498 |
|
| 1499 |
|
| 1500 |
my_hv_store( framedata, "peak", newSVpvf( "%f", (float)buffer_get_float32(id3->buf) ) ); |
| 1501 |
read += 4; |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
buffer_get_bits(id3->buf, 3); |
| 1507 |
|
| 1508 |
my_hv_store( framedata, "track_originator", newSVuv( buffer_get_bits(id3->buf, 3) ) ); |
| 1509 |
|
| 1510 |
|
| 1511 |
sign = buffer_get_bits(id3->buf, 1); |
| 1512 |
|
| 1513 |
|
| 1514 |
radio = (float)buffer_get_bits(id3->buf, 9); |
| 1515 |
radio /= 10.0; |
| 1516 |
if (sign == 1) radio *= -1.0; |
| 1517 |
my_hv_store( framedata, "track_gain", newSVpvf( "%f dB", radio ) ); |
| 1518 |
|
| 1519 |
read += 2; |
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
buffer_get_bits(id3->buf, 3); |
| 1525 |
|
| 1526 |
|
| 1527 |
my_hv_store( framedata, "album_originator", newSVuv( buffer_get_bits(id3->buf, 3) ) ); |
| 1528 |
|
| 1529 |
|
| 1530 |
sign = buffer_get_bits(id3->buf, 1); |
| 1531 |
|
| 1532 |
|
| 1533 |
audiophile = (float)buffer_get_bits(id3->buf, 9); |
| 1534 |
audiophile /= 10.0; |
| 1535 |
if (sign == 1) audiophile *= -1.0; |
| 1536 |
my_hv_store( framedata, "album_gain", newSVpvf( "%f dB", audiophile ) ); |
| 1537 |
|
| 1538 |
read += 2; |
| 1539 |
|
| 1540 |
my_hv_store( id3->tags, "RGAD", newRV_noinc( (SV *)framedata ) ); |
| 1541 |
|
| 1542 |
return read; |
| 1543 |
} |
| 1544 |
|
| 1545 |
uint32_t |
| 1546 |
_id3_parse_rva2(id3info *id3, uint32_t len, AV *framedata) |
| 1547 |
{ |
| 1548 |
float adj = 0.0; |
| 1549 |
int adj_fp; |
| 1550 |
uint8_t peakbits; |
| 1551 |
float peak = 0.0; |
| 1552 |
uint32_t read = 0; |
| 1553 |
unsigned char *bptr; |
| 1554 |
|
| 1555 |
|
| 1556 |
av_push( framedata, newSViv( buffer_get_char(id3->buf) ) ); |
| 1557 |
|
| 1558 |
|
| 1559 |
bptr = buffer_ptr(id3->buf); |
| 1560 |
adj_fp = *(signed char *)(bptr) << 8; |
| 1561 |
adj_fp |= *(unsigned char *)(bptr+1); |
| 1562 |
adj = adj_fp / 512.0; |
| 1563 |
av_push( framedata, newSVpvf( "%f dB", adj ) ); |
| 1564 |
buffer_consume(id3->buf, 2); |
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
peakbits = buffer_get_char(id3->buf); |
| 1569 |
|
| 1570 |
read += 4; |
| 1571 |
|
| 1572 |
if (4 + (peakbits + 7) / 8 <= len) { |
| 1573 |
DEBUG_TRACE(" peakbits: %d\n", peakbits); |
| 1574 |
if (peakbits > 0) { |
| 1575 |
peak += (float)buffer_get_char(id3->buf); |
| 1576 |
read++; |
| 1577 |
} |
| 1578 |
if (peakbits > 8) { |
| 1579 |
peak += (float)buffer_get_char(id3->buf) / 256.0; |
| 1580 |
read++; |
| 1581 |
} |
| 1582 |
if (peakbits > 16) { |
| 1583 |
peak += (float)buffer_get_char(id3->buf) / 65536.0; |
| 1584 |
read++; |
| 1585 |
} |
| 1586 |
|
| 1587 |
if (peakbits > 0) |
| 1588 |
peak /= (float)(1 << ((peakbits - 1) & 7)); |
| 1589 |
} |
| 1590 |
|
| 1591 |
av_push( framedata, newSVpvf( "%f dB", peak ) ); |
| 1592 |
|
| 1593 |
return read; |
| 1594 |
} |
| 1595 |
|
| 1596 |
uint32_t |
| 1597 |
_id3_parse_sylt(id3info *id3, uint8_t encoding, uint32_t len, AV *framedata) |
| 1598 |
{ |
| 1599 |
uint32_t read = 0; |
| 1600 |
AV *content = newAV(); |
| 1601 |
unsigned char *bptr; |
| 1602 |
|
| 1603 |
while (read < len) { |
| 1604 |
SV *value = NULL; |
| 1605 |
HV *lyric = newHV(); |
| 1606 |
|
| 1607 |
read += _id3_get_utf8_string(id3, &value, len - read, encoding); |
| 1608 |
if (value != NULL && SvPOK(value) && sv_len(value)) { |
| 1609 |
my_hv_store( lyric, "text", value ); |
| 1610 |
} |
| 1611 |
else { |
| 1612 |
my_hv_store( lyric, "text", &PL_sv_undef ); |
| 1613 |
if (value) SvREFCNT(value); |
| 1614 |
} |
| 1615 |
|
| 1616 |
my_hv_store( lyric, "timestamp", newSVuv( buffer_get_int(id3->buf) ) ); |
| 1617 |
read += 4; |
| 1618 |
|
| 1619 |
|
| 1620 |
bptr = buffer_ptr(id3->buf); |
| 1621 |
if ( len - read > 0 && bptr[0] == 0x0a ) { |
| 1622 |
buffer_consume(id3->buf, 1); |
| 1623 |
read++; |
| 1624 |
} |
| 1625 |
|
| 1626 |
av_push( content, newRV_noinc( (SV *)lyric ) ); |
| 1627 |
} |
| 1628 |
|
| 1629 |
av_push( framedata, newRV_noinc( (SV *)content ) ); |
| 1630 |
|
| 1631 |
return read; |
| 1632 |
} |
| 1633 |
|
| 1634 |
uint32_t |
| 1635 |
_id3_parse_etco(id3info *id3, uint32_t len, AV *framedata) |
| 1636 |
{ |
| 1637 |
uint32_t read = 0; |
| 1638 |
AV *content = newAV(); |
| 1639 |
|
| 1640 |
while (read < len) { |
| 1641 |
HV *event = newHV(); |
| 1642 |
|
| 1643 |
my_hv_store( event, "type", newSVuv( buffer_get_char(id3->buf) ) ); |
| 1644 |
my_hv_store( event, "timestamp", newSVuv( buffer_get_int(id3->buf) ) ); |
| 1645 |
read += 5; |
| 1646 |
|
| 1647 |
av_push( content, newRV_noinc( (SV *)event ) ); |
| 1648 |
} |
| 1649 |
|
| 1650 |
av_push( framedata, newRV_noinc( (SV *)content ) ); |
| 1651 |
|
| 1652 |
return read; |
| 1653 |
} |
| 1654 |
|
| 1655 |
void |
| 1656 |
_id3_convert_tdrc(id3info *id3) |
| 1657 |
{ |
| 1658 |
char timestamp[17] = { 0 }; |
| 1659 |
|
| 1660 |
if ( my_hv_exists(id3->tags, "TYER") ) { |
| 1661 |
SV *tyer = my_hv_delete(id3->tags, "TYER"); |
| 1662 |
if (SvPOK(tyer) && sv_len(tyer) == 4) { |
| 1663 |
char *ptr = SvPVX(tyer); |
| 1664 |
timestamp[0] = ptr[0]; |
| 1665 |
timestamp[1] = ptr[1]; |
| 1666 |
timestamp[2] = ptr[2]; |
| 1667 |
timestamp[3] = ptr[3]; |
| 1668 |
DEBUG_TRACE(" Converted TYER (%s) to TDRC (%s)\n", SvPVX(tyer), timestamp); |
| 1669 |
} |
| 1670 |
} |
| 1671 |
|
| 1672 |
if ( my_hv_exists(id3->tags, "TDAT") ) { |
| 1673 |
SV *tdat = my_hv_delete(id3->tags, "TDAT"); |
| 1674 |
if (SvPOK(tdat) && sv_len(tdat) == 4) { |
| 1675 |
char *ptr = SvPVX(tdat); |
| 1676 |
timestamp[4] = '-'; |
| 1677 |
timestamp[5] = ptr[2]; |
| 1678 |
timestamp[6] = ptr[3]; |
| 1679 |
timestamp[7] = '-'; |
| 1680 |
timestamp[8] = ptr[0]; |
| 1681 |
timestamp[9] = ptr[1]; |
| 1682 |
DEBUG_TRACE(" Converted TDAT (%s) to TDRC (%s)\n", SvPVX(tdat), timestamp); |
| 1683 |
} |
| 1684 |
} |
| 1685 |
|
| 1686 |
if ( my_hv_exists(id3->tags, "TIME") ) { |
| 1687 |
SV *time = my_hv_delete(id3->tags, "TIME"); |
| 1688 |
if (SvPOK(time) && sv_len(time) == 4) { |
| 1689 |
char *ptr = SvPVX(time); |
| 1690 |
timestamp[10] = 'T'; |
| 1691 |
timestamp[11] = ptr[0]; |
| 1692 |
timestamp[12] = ptr[1]; |
| 1693 |
timestamp[13] = ':'; |
| 1694 |
timestamp[14] = ptr[2]; |
| 1695 |
timestamp[15] = ptr[3]; |
| 1696 |
DEBUG_TRACE(" Converted TIME (%s) to TDRC (%s)\n", SvPVX(time), timestamp); |
| 1697 |
} |
| 1698 |
} |
| 1699 |
|
| 1700 |
if (timestamp[0]) { |
| 1701 |
my_hv_store( id3->tags, "TDRC", newSVpv(timestamp, 0) ); |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
|
| 1706 |
uint32_t |
| 1707 |
_id3_deunsync(unsigned char *data, uint32_t length) |
| 1708 |
{ |
| 1709 |
unsigned char *old; |
| 1710 |
unsigned char *end = data + length; |
| 1711 |
unsigned char *new; |
| 1712 |
|
| 1713 |
if (length == 0) |
| 1714 |
return 0; |
| 1715 |
|
| 1716 |
for (old = new = data; old < end - 1; ++old) { |
| 1717 |
*new++ = *old; |
| 1718 |
if (old[0] == 0xff && old[1] == 0x00) |
| 1719 |
++old; |
| 1720 |
} |
| 1721 |
|
| 1722 |
*new++ = *old; |
| 1723 |
|
| 1724 |
return new - data; |
| 1725 |
} |
| 1726 |
|
| 1727 |
void |
| 1728 |
_id3_skip(id3info *id3, uint32_t size) |
| 1729 |
{ |
| 1730 |
if ( buffer_len(id3->buf) >= size ) { |
| 1731 |
buffer_consume(id3->buf, size); |
| 1732 |
|
| 1733 |
DEBUG_TRACE(" skipped buffer data size %d\n", size); |
| 1734 |
} |
| 1735 |
else { |
| 1736 |
PerlIO_seek(id3->infile, size - buffer_len(id3->buf), SEEK_CUR); |
| 1737 |
buffer_clear(id3->buf); |
| 1738 |
|
| 1739 |
DEBUG_TRACE(" seeked past %d bytes to %d\n", size, (int)PerlIO_tell(id3->infile)); |
| 1740 |
} |
| 1741 |
} |
| 1742 |
|
| 1743 |
|
| 1744 |
char const * |
| 1745 |
_id3_genre_index(unsigned int index) |
| 1746 |
{ |
| 1747 |
return (index < NGENRES) ? genre_table[index] : 0; |
| 1748 |
} |
| 1749 |
|
| 1750 |
|
| 1751 |
char const * |
| 1752 |
_id3_genre_name(char const *string) |
| 1753 |
{ |
| 1754 |
static char const genre_remix[] = { 'R', 'e', 'm', 'i', 'x', 0 }; |
| 1755 |
static char const genre_cover[] = { 'C', 'o', 'v', 'e', 'r', 0 }; |
| 1756 |
unsigned long number; |
| 1757 |
|
| 1758 |
if (string == 0 || *string == 0) |
| 1759 |
return 0; |
| 1760 |
|
| 1761 |
if (string[0] == 'R' && string[1] == 'X') |
| 1762 |
return genre_remix; |
| 1763 |
if (string[0] == 'C' && string[1] == 'R') |
| 1764 |
return genre_cover; |
| 1765 |
|
| 1766 |
number = strtol(string, NULL, 0); |
| 1767 |
|
| 1768 |
return (number < NGENRES) ? genre_table[number] : string; |
| 1769 |
} |