2 id3: ID3v2.3 and ID3v2.4 parsing (a relevant subset)
4 copyright 2006-2008 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Thomas Orgis
9 #include "mpg123lib_intern.h"
13 #ifndef NO_ID3V2 /* Only the main parsing routine will always be there. */
15 /* We know the usual text frames plus some specifics. */
16 #define KNOWN_FRAMES 4
17 static const char frame_type
[KNOWN_FRAMES
][5] = { "COMM", "TXXX", "RVA2", "USLT" };
18 enum frame_types
{ unknown
= -2, text
= -1, comment
, extra
, rva2
, uslt
};
20 /* UTF support definitions */
22 typedef void (*text_converter
)(mpg123_string
*sb
, const unsigned char* source
, size_t len
, const int noquiet
);
24 static void convert_latin1 (mpg123_string
*sb
, const unsigned char* source
, size_t len
, const int noquiet
);
25 static void convert_utf16bom(mpg123_string
*sb
, const unsigned char* source
, size_t len
, const int noquiet
);
26 static void convert_utf8 (mpg123_string
*sb
, const unsigned char* source
, size_t len
, const int noquiet
);
28 static const text_converter text_converters
[4] =
31 /* We always check for (multiple) BOM in 16bit unicode. Without BOM, UTF16 BE is the default.
32 Errors in encoding are detected anyway. */
38 const unsigned int encoding_widths
[4] = { 1, 2, 2, 1 };
40 /* the code starts here... */
42 static void null_id3_links(mpg123_handle
*fr
)
44 fr
->id3v2
.title
= NULL
;
45 fr
->id3v2
.artist
= NULL
;
46 fr
->id3v2
.album
= NULL
;
47 fr
->id3v2
.year
= NULL
;
48 fr
->id3v2
.genre
= NULL
;
49 fr
->id3v2
.comment
= NULL
;
52 void init_id3(mpg123_handle
*fr
)
54 fr
->id3v2
.version
= 0; /* nothing there */
56 fr
->id3v2
.comments
= 0;
57 fr
->id3v2
.comment_list
= NULL
;
59 fr
->id3v2
.text
= NULL
;
61 fr
->id3v2
.extra
= NULL
;
64 /* Managing of the text, comment and extra lists. */
66 /* Initialize one element. */
67 static void init_mpg123_text(mpg123_text
*txt
)
69 mpg123_init_string(&txt
->text
);
70 mpg123_init_string(&txt
->description
);
80 /* Free memory of one element. */
81 static void free_mpg123_text(mpg123_text
*txt
)
83 mpg123_free_string(&txt
->text
);
84 mpg123_free_string(&txt
->description
);
87 /* Free memory of whole list. */
88 #define free_comment(mh) free_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments))
89 #define free_text(mh) free_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts))
90 #define free_extra(mh) free_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras))
91 static void free_id3_text(mpg123_text
**list
, size_t *size
)
94 for(i
=0; i
<*size
; ++i
) free_mpg123_text(&((*list
)[i
]));
101 /* Add items to the list. */
102 #define add_comment(mh) add_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments))
103 #define add_text(mh) add_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts))
104 #define add_extra(mh) add_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras))
105 static mpg123_text
*add_id3_text(mpg123_text
**list
, size_t *size
)
107 mpg123_text
*x
= safe_realloc(*list
, sizeof(mpg123_text
)*(*size
+1));
108 if(x
== NULL
) return NULL
; /* bad */
112 init_mpg123_text(&((*list
)[*size
-1]));
114 return &((*list
)[*size
-1]); /* Return pointer to the added text. */
117 /* Remove the last item. */
118 #define pop_comment(mh) pop_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments))
119 #define pop_text(mh) pop_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts))
120 #define pop_extra(mh) pop_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras))
121 static void pop_id3_text(mpg123_text
**list
, size_t *size
)
124 if(*size
< 1) return;
126 free_mpg123_text(&((*list
)[*size
-1]));
129 x
= safe_realloc(*list
, sizeof(mpg123_text
)*(*size
-1));
130 if(x
!= NULL
){ *list
= x
; *size
-= 1; }
140 /* OK, back t the higher level functions. */
142 void exit_id3(mpg123_handle
*fr
)
149 void reset_id3(mpg123_handle
*fr
)
155 /* Set the id3v2.artist id3v2.title ... links to elements of the array. */
156 void id3_link(mpg123_handle
*fr
)
159 mpg123_id3v2
*v2
= &fr
->id3v2
;
160 debug("linking ID3v2");
162 for(i
=0; i
<v2
->texts
; ++i
)
164 mpg123_text
*entry
= &v2
->text
[i
];
165 if (!strncmp("TIT2", entry
->id
, 4)) v2
->title
= &entry
->text
;
166 else if(!strncmp("TALB", entry
->id
, 4)) v2
->album
= &entry
->text
;
167 else if(!strncmp("TPE1", entry
->id
, 4)) v2
->artist
= &entry
->text
;
168 else if(!strncmp("TYER", entry
->id
, 4)) v2
->year
= &entry
->text
;
169 else if(!strncmp("TCON", entry
->id
, 4)) v2
->genre
= &entry
->text
;
171 for(i
=0; i
<v2
->comments
; ++i
)
173 mpg123_text
*entry
= &v2
->comment_list
[i
];
174 if(entry
->description
.fill
== 0 || entry
->description
.p
[0] == 0)
175 v2
->comment
= &entry
->text
;
177 /* When no generic comment found, use the last non-generic one. */
178 if(v2
->comment
== NULL
&& v2
->comments
> 0)
179 v2
->comment
= &v2
->comment_list
[v2
->comments
-1].text
;
183 Store ID3 text data in an mpg123_string; either verbatim copy or everything translated to UTF-8 encoding.
184 Preserve the zero string separator (I don't need strlen for the total size).
186 ID3v2 standard says that there should be one text frame of specific type per tag, and subsequent tags overwrite old values.
187 So, I always replace the text that may be stored already (perhaps with a list of zero-separated strings, though).
189 void store_id3_text(mpg123_string
*sb
, char *source
, size_t source_size
, const int noquiet
, const int notranslate
)
193 debug("Empty id3 data!");
197 /* We shall just copy the data. Client wants to decode itself. */
200 /* Future: Add a path for ID3 errors. */
201 if(!mpg123_resize_string(sb
, source_size
))
203 if(noquiet
) error("Cannot resize target string, out of memory?");
206 memcpy(sb
->p
, source
, source_size
);
207 sb
->fill
= source_size
;
208 debug1("stored undecoded ID3 text of size %"SIZE_P
, (size_p
)source_size
);
212 id3_to_utf8(sb
, ((unsigned char *)source
)[0], (unsigned char*)source
+1, source_size
-1, noquiet
);
214 if(sb
->fill
) debug1("UTF-8 string (the first one): %s", sb
->p
);
215 else if(noquiet
) error("unable to convert string to UTF-8 (out of memory, junk input?)!");
218 /* On error, sb->size is 0. */
219 void id3_to_utf8(mpg123_string
*sb
, unsigned char encoding
, const unsigned char *source
, size_t source_size
, int noquiet
)
222 debug1("encoding: %u", encoding
);
223 /* A note: ID3v2.3 uses UCS-2 non-variable 16bit encoding, v2.4 uses UTF16.
224 UTF-16 uses a reserved/private range in UCS-2 to add the magic, so we just always treat it as UTF. */
225 if(encoding
> mpg123_id3_enc_max
)
227 if(noquiet
) error1("Unknown text encoding %u, I take no chances, sorry!", encoding
);
229 mpg123_free_string(sb
);
232 bwidth
= encoding_widths
[encoding
];
233 /* Hack! I've seen a stray zero byte before BOM. Is that supposed to happen? */
234 if(encoding
!= mpg123_id3_utf16be
) /* UTF16be _can_ beging with a null byte! */
235 while(source_size
> bwidth
&& source
[0] == 0)
239 debug("skipped leading zero");
241 if(source_size
% bwidth
)
243 /* When we need two bytes for a character, it's strange to have an uneven bytestream length. */
244 if(noquiet
) warning2("Weird tag size %d for encoding %u - I will probably trim too early or something but I think the MP3 is broken.", (int)source_size
, encoding
);
245 source_size
-= source_size
% bwidth
;
247 text_converters
[encoding
](sb
, source
, source_size
, noquiet
);
250 char *next_text(char* prev
, int encoding
, size_t limit
)
253 size_t width
= encoding_widths
[encoding
];
255 /* So I go lengths to find zero or double zero...
256 Remember bug 2834636: Only check for aligned NULLs! */
257 while(text
-prev
< (long)limit
)
261 if(width
<= limit
-(text
-prev
))
264 for(; i
<width
; ++i
) if(text
[i
] != 0) break;
266 if(i
== width
) /* found a null wide enough! */
272 else return NULL
; /* No full character left? This text is broken */
277 if((size_t)(text
-prev
) >= limit
) text
= NULL
;
282 static const char *enc_name(int enc
)
286 case 0: return "Latin 1";
287 case 1: return "UTF-16 BOM";
288 case 2: return "UTF-16 BE";
289 case 3: return "UTF-8";
290 default: return "unknown!";
294 static void process_text(mpg123_handle
*fr
, char *realdata
, size_t realsize
, char *id
)
296 /* Text encoding $xx */
297 /* The text (encoded) ... */
298 mpg123_text
*t
= add_text(fr
);
299 if(VERBOSE4
) fprintf(stderr
, "Note: Storing text from %s encoding\n", enc_name(realdata
[0]));
302 if(NOQUIET
) error("Unable to attach new text!");
305 memcpy(t
->id
, id
, 4);
306 store_id3_text(&t
->text
, realdata
, realsize
, NOQUIET
, fr
->p
.flags
& MPG123_PLAIN_ID3TEXT
);
307 if(VERBOSE4
) fprintf(stderr
, "Note: ID3v2 %c%c%c%c text frame: %s\n", id
[0], id
[1], id
[2], id
[3], t
->text
.p
);
310 /* Store a new comment that perhaps is a RVA / RVA_ALBUM/AUDIOPHILE / RVA_MIX/RADIO one
311 Special gimmik: It also stores USLT to the texts. Stucture is the same as for comments. */
312 static void process_comment(mpg123_handle
*fr
, enum frame_types tt
, char *realdata
, size_t realsize
, int rva_level
, char *id
)
314 /* Text encoding $xx */
315 /* Language $xx xx xx */
316 /* Short description (encoded!) <text> $00 (00) */
317 /* Then the comment text (encoded) ... */
318 char encoding
= realdata
[0];
319 char *lang
= realdata
+1; /* I'll only use the 3 bytes! */
320 char *descr
= realdata
+4;
322 mpg123_text
*xcom
= NULL
;
323 mpg123_text localcom
; /* UTF-8 variant for local processing. */
325 if((int)realsize
< descr
-realdata
)
327 if(NOQUIET
) error1("Invalid frame size of %lu (too small for anything).", (unsigned long)realsize
);
330 xcom
= (tt
== uslt
? add_text(fr
) : add_comment(fr
));
331 if(VERBOSE4
) fprintf(stderr
, "Note: Storing comment from %s encoding\n", enc_name(realdata
[0]));
334 if(NOQUIET
) error("Unable to attach new comment!");
337 memcpy(xcom
->lang
, lang
, 3);
338 memcpy(xcom
->id
, id
, 4);
339 /* Now I can abuse a byte from lang for the encoding. */
340 descr
[-1] = encoding
;
341 /* Be careful with finding the end of description, I have to honor encoding here. */
342 text
= next_text(descr
, encoding
, realsize
-(descr
-realdata
));
345 if(NOQUIET
) error("No comment text / valid description?");
350 init_mpg123_text(&localcom
);
351 /* Store the text, without translation to UTF-8, but for comments always a local copy in UTF-8.
352 Reminder: No bailing out from here on without freeing the local comment data! */
353 store_id3_text(&xcom
->description
, descr
-1, text
-descr
+1, NOQUIET
, fr
->p
.flags
& MPG123_PLAIN_ID3TEXT
);
355 store_id3_text(&localcom
.description
, descr
-1, text
-descr
+1, NOQUIET
, 0);
357 text
[-1] = encoding
; /* Byte abusal for encoding... */
358 store_id3_text(&xcom
->text
, text
-1, realsize
+1-(text
-realdata
), NOQUIET
, fr
->p
.flags
& MPG123_PLAIN_ID3TEXT
);
359 /* Remember: I will probably decode the above (again) for rva comment checking. So no messing around, please. */
361 if(VERBOSE4
) /* Do _not_ print the verbatim text: The encoding might be funny! */
363 fprintf(stderr
, "Note: ID3 comm/uslt desc of length %"SIZE_P
".\n", (size_p
)xcom
->description
.fill
);
364 fprintf(stderr
, "Note: ID3 comm/uslt text of length %"SIZE_P
".\n", (size_p
)xcom
->text
.fill
);
366 /* Look out for RVA info only when we really deal with a straight comment. */
367 if(tt
== comment
&& localcom
.description
.fill
> 0)
369 int rva_mode
= -1; /* mix / album */
370 if( !strcasecmp(localcom
.description
.p
, "rva")
371 || !strcasecmp(localcom
.description
.p
, "rva_mix")
372 || !strcasecmp(localcom
.description
.p
, "rva_track")
373 || !strcasecmp(localcom
.description
.p
, "rva_radio") )
375 else if( !strcasecmp(localcom
.description
.p
, "rva_album")
376 || !strcasecmp(localcom
.description
.p
, "rva_audiophile")
377 || !strcasecmp(localcom
.description
.p
, "rva_user") )
379 if((rva_mode
> -1) && (fr
->rva
.level
[rva_mode
] <= rva_level
))
381 /* Only translate the contents in here where we really need them. */
382 store_id3_text(&localcom
.text
, text
-1, realsize
+1-(text
-realdata
), NOQUIET
, 0);
383 if(localcom
.text
.fill
> 0)
385 fr
->rva
.gain
[rva_mode
] = (float) atof(localcom
.text
.p
);
386 if(VERBOSE3
) fprintf(stderr
, "Note: RVA value %fdB\n", fr
->rva
.gain
[rva_mode
]);
387 fr
->rva
.peak
[rva_mode
] = 0;
388 fr
->rva
.level
[rva_mode
] = rva_level
;
392 /* Make sure to free the local memory... */
393 free_mpg123_text(&localcom
);
396 void process_extra(mpg123_handle
*fr
, char* realdata
, size_t realsize
, int rva_level
, char *id
)
398 /* Text encoding $xx */
399 /* Description ... $00 (00) */
401 char encoding
= realdata
[0];
402 char *descr
= realdata
+1; /* remember, the encoding is descr[-1] */
407 if((int)realsize
< descr
-realdata
)
409 if(NOQUIET
) error1("Invalid frame size of %lu (too small for anything).", (unsigned long)realsize
);
412 text
= next_text(descr
, encoding
, realsize
-(descr
-realdata
));
413 if(VERBOSE4
) fprintf(stderr
, "Note: Storing extra from %s encoding\n", enc_name(realdata
[0]));
416 if(NOQUIET
) error("No extra frame text / valid description?");
422 if(NOQUIET
) error("Unable to attach new extra text!");
425 memcpy(xex
->id
, id
, 4);
426 init_mpg123_text(&localex
); /* For our local copy. */
428 /* The outside storage gets reencoded to UTF-8 only if not requested otherwise.
429 Remember that we really need the -1 here to hand in the encoding byte!*/
430 store_id3_text(&xex
->description
, descr
-1, text
-descr
+1, NOQUIET
, fr
->p
.flags
& MPG123_PLAIN_ID3TEXT
);
431 /* Our local copy is always stored in UTF-8! */
432 store_id3_text(&localex
.description
, descr
-1, text
-descr
+1, NOQUIET
, 0);
433 /* At first, only store the outside copy of the payload. We may not need the local copy. */
435 store_id3_text(&xex
->text
, text
-1, realsize
-(text
-realdata
)+1, NOQUIET
, fr
->p
.flags
& MPG123_PLAIN_ID3TEXT
);
437 /* Now check if we would like to interpret this extra info for RVA. */
438 if(localex
.description
.fill
> 0)
441 int rva_mode
= -1; /* mix / album */
443 if(!strncasecmp(localex
.description
.p
, "replaygain_track_",17))
445 if(VERBOSE3
) fprintf(stderr
, "Note: RVA ReplayGain track gain/peak\n");
448 if(!strcasecmp(localex
.description
.p
, "replaygain_track_peak")) is_peak
= 1;
449 else if(strcasecmp(localex
.description
.p
, "replaygain_track_gain")) rva_mode
= -1;
452 if(!strncasecmp(localex
.description
.p
, "replaygain_album_",17))
454 if(VERBOSE3
) fprintf(stderr
, "Note: RVA ReplayGain album gain/peak\n");
457 if(!strcasecmp(localex
.description
.p
, "replaygain_album_peak")) is_peak
= 1;
458 else if(strcasecmp(localex
.description
.p
, "replaygain_album_gain")) rva_mode
= -1;
460 if((rva_mode
> -1) && (fr
->rva
.level
[rva_mode
] <= rva_level
))
462 /* Now we need the translated copy of the data. */
463 store_id3_text(&localex
.text
, text
-1, realsize
-(text
-realdata
)+1, NOQUIET
, 0);
464 if(localex
.text
.fill
> 0)
468 fr
->rva
.peak
[rva_mode
] = (float) atof(localex
.text
.p
);
469 if(VERBOSE3
) fprintf(stderr
, "Note: RVA peak %f\n", fr
->rva
.peak
[rva_mode
]);
473 fr
->rva
.gain
[rva_mode
] = (float) atof(localex
.text
.p
);
474 if(VERBOSE3
) fprintf(stderr
, "Note: RVA gain %fdB\n", fr
->rva
.gain
[rva_mode
]);
476 fr
->rva
.level
[rva_mode
] = rva_level
;
481 free_mpg123_text(&localex
);
484 /* Make a ID3v2.3+ 4-byte ID from a ID3v2.2 3-byte ID
485 Note that not all frames survived to 2.4; the mapping goes to 2.3 .
486 A notable miss is the old RVA frame, which is very unspecific anyway.
487 This function returns -1 when a not known 3 char ID was encountered, 0 otherwise. */
488 int promote_framename(mpg123_handle
*fr
, char *id
) /* fr because of VERBOSE macros */
493 "COM", "TAL", "TBP", "TCM", "TCO", "TCR", "TDA", "TDY", "TEN", "TFT",
494 "TIM", "TKE", "TLA", "TLE", "TMT", "TOA", "TOF", "TOL", "TOR", "TOT",
495 "TP1", "TP2", "TP3", "TP4", "TPA", "TPB", "TRC", "TDA", "TRK", "TSI",
496 "TSS", "TT1", "TT2", "TT3", "TXT", "TXX", "TYE"
500 "COMM", "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDAT", "TDLY", "TENC", "TFLT",
501 "TIME", "TKEY", "TLAN", "TLEN", "TMED", "TOPE", "TOFN", "TOLY", "TORY", "TOAL",
502 "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPUB", "TSRC", "TRDA", "TRCK", "TSIZ",
503 "TSSE", "TIT1", "TIT2", "TIT3", "TEXT", "TXXX", "TYER"
505 for(i
=0; i
<sizeof(old
)/sizeof(char*); ++i
)
507 if(!strncmp(id
, old
[i
], 3))
509 memcpy(id
, new[i
], 4);
510 if(VERBOSE3
) fprintf(stderr
, "Translated ID3v2.2 frame %s to %s\n", old
[i
], new[i
]);
514 if(VERBOSE3
) fprintf(stderr
, "Ignoring untranslated ID3v2.2 frame %c%c%c\n", id
[0], id
[1], id
[2]);
518 #endif /* NO_ID3V2 */
521 trying to parse ID3v2.3 and ID3v2.4 tags...
523 returns: 0: bad or just unparseable tag
524 1: good, (possibly) new tag info
525 <0: reader error (may need more data feed, try again)
527 int parse_new_id3(mpg123_handle
*fr
, unsigned long first4bytes
)
529 #define UNSYNC_FLAG 128
530 #define EXTHEAD_FLAG 64
532 #define FOOTER_FLAG 16
533 #define UNKNOWN_FLAGS 15 /* 00001111*/
534 unsigned char buf
[6];
535 unsigned long length
=0;
536 unsigned char flags
= 0;
539 unsigned char* tagdata
= NULL
;
540 unsigned char major
= first4bytes
& 0xff;
541 debug1("ID3v2: major tag version: %i", major
);
542 if(major
== 0xff) return 0; /* Invalid... */
543 if((ret2
= fr
->rd
->read_frame_body(fr
, buf
, 6)) < 0) /* read more header information */
546 if(buf
[0] == 0xff) return 0; /* Revision, will never be 0xff. */
548 /* second new byte are some nice flags, if these are invalid skip the whole thing */
550 debug1("ID3v2: flags 0x%08x", flags
);
551 /* use 4 bytes from buf to construct 28bit uint value and return 1; return 0 if bytes are not synchsafe */
552 #define synchsafe_to_long(buf,res) \
554 (((buf)[0]|(buf)[1]|(buf)[2]|(buf)[3]) & 0x80) ? 0 : \
555 (res = (((unsigned long) (buf)[0]) << 21) \
556 | (((unsigned long) (buf)[1]) << 14) \
557 | (((unsigned long) (buf)[2]) << 7) \
558 | ((unsigned long) (buf)[3]) \
561 /* id3v2.3 does not store synchsafe frame sizes, but synchsafe tag size - doh! */
562 #define bytes_to_long(buf,res) \
565 (res = (((unsigned long) (buf)[0]) << 24) \
566 | (((unsigned long) (buf)[1]) << 16) \
567 | (((unsigned long) (buf)[2]) << 8) \
568 | ((unsigned long) (buf)[3]) \
569 ,1) : synchsafe_to_long(buf,res) \
571 /* for id3v2.2 only */
572 #define threebytes_to_long(buf,res) \
574 res = (((unsigned long) (buf)[0]) << 16) \
575 | (((unsigned long) (buf)[1]) << 8) \
576 | ((unsigned long) (buf)[2]) \
580 /* length-10 or length-20 (footer present); 4 synchsafe integers == 28 bit number */
581 /* we have already read 10 bytes, so left are length or length+10 bytes belonging to tag */
582 if(!synchsafe_to_long(buf
+2,length
))
584 if(NOQUIET
) error4("Bad tag length (not synchsafe): 0x%02x%02x%02x%02x; You got a bad ID3 tag here.", buf
[2],buf
[3],buf
[4],buf
[5]);
587 debug1("ID3v2: tag data length %lu", length
);
589 if(VERBOSE2
) fprintf(stderr
,"Note: ID3v2.%i rev %i tag of %lu bytes\n", major
, buf
[0], length
);
590 /* skip if unknown version/scary flags, parse otherwise */
591 if((flags
& UNKNOWN_FLAGS
) || (major
> 4) || (major
< 2))
593 /* going to skip because there are unknown flags set */
594 if(NOQUIET
) warning2("ID3v2: Won't parse the ID3v2 tag with major version %u and flags 0x%xu - some extra code may be needed", major
, flags
);
596 if((ret2
= fr
->rd
->skip_bytes(fr
,length
)) < 0) /* will not store data in backbuff! */
602 fr
->id3v2
.version
= major
;
603 /* try to interpret that beast */
604 if((tagdata
= (unsigned char*) malloc(length
+1)) != NULL
)
606 debug("ID3v2: analysing frames...");
607 if((ret2
= fr
->rd
->read_frame_body(fr
,tagdata
,length
)) > 0)
609 unsigned long tagpos
= 0;
610 debug1("ID3v2: have read at all %lu bytes for the tag now", (unsigned long)length
+6);
611 /* going to apply strlen for strings inside frames, make sure that it doesn't overflow! */
613 if(flags
& EXTHEAD_FLAG
)
615 debug("ID3v2: skipping extended header");
616 if(!bytes_to_long(tagdata
, tagpos
))
619 if(NOQUIET
) error4("Bad (non-synchsafe) tag offset: 0x%02x%02x%02x%02x", tagdata
[0], tagdata
[1], tagdata
[2], tagdata
[3]);
625 unsigned long framesize
;
626 unsigned long fflags
; /* need 16 bits, actually */
628 /* pos now advanced after ext head, now a frame has to follow */
629 while(tagpos
< length
-10) /* I want to read at least a full header */
632 unsigned long pos
= tagpos
;
633 int head_part
= fr
->id3v2
.version
== 2 ? 3 : 4; /* bytes of frame title and of framesize value */
634 /* level 1,2,3 - 0 is info from lame/info tag! */
635 /* rva tags with ascending significance, then general frames */
636 enum frame_types tt
= unknown
;
637 /* we may have entered the padding zone or any other strangeness: check if we have valid frame id characters */
638 for(i
=0; i
< head_part
; ++i
)
639 if( !( ((tagdata
[tagpos
+i
] > 47) && (tagdata
[tagpos
+i
] < 58))
640 || ((tagdata
[tagpos
+i
] > 64) && (tagdata
[tagpos
+i
] < 91)) ) )
642 debug5("ID3v2: real tag data apparently ended after %lu bytes with 0x%02x%02x%02x%02x", tagpos
, tagdata
[tagpos
], tagdata
[tagpos
+1], tagdata
[tagpos
+2], tagdata
[tagpos
+3]);
643 /* This is no hard error... let's just hope that we got something meaningful already (ret==1 in that case). */
644 goto tagparse_cleanup
; /* Need to escape two loops here. */
648 /* 4 or 3 bytes id */
649 strncpy(id
, (char*) tagdata
+pos
, head_part
);
652 /* size as 32 bits or 28 bits */
653 if(fr
->id3v2
.version
== 2) threebytes_to_long(tagdata
+pos
, framesize
);
655 if(!bytes_to_long(tagdata
+pos
, framesize
))
657 /* Just assume that up to now there was some good data. */
658 if(NOQUIET
) error1("ID3v2: non-syncsafe size of %s frame, skipping the remainder of tag", id
);
661 if(VERBOSE3
) fprintf(stderr
, "Note: ID3v2 %s frame of size %lu\n", id
, framesize
);
662 tagpos
+= head_part
+ framesize
; /* the important advancement in whole tag */
665 if(NOQUIET
) error("Whoa! ID3v2 frame claims to be larger than the whole rest of the tag.");
669 if(fr
->id3v2
.version
> 2)
671 fflags
= (((unsigned long) tagdata
[pos
]) << 8) | ((unsigned long) tagdata
[pos
+1]);
676 /* for sanity, after full parsing tagpos should be == pos */
677 /* debug4("ID3v2: found %s frame, size %lu (as bytes: 0x%08lx), flags 0x%016lx", id, framesize, framesize, fflags); */
678 /* %0abc0000 %0h00kmnp */
679 #define BAD_FFLAGS (unsigned long) 36784
680 #define PRES_TAG_FFLAG 16384
681 #define PRES_FILE_FFLAG 8192
682 #define READ_ONLY_FFLAG 4096
683 #define GROUP_FFLAG 64
684 #define COMPR_FFLAG 8
686 #define UNSYNC_FFLAG 2
687 #define DATLEN_FFLAG 1
688 if(head_part
< 4 && promote_framename(fr
, id
) != 0) continue;
690 /* shall not or want not handle these */
691 if(fflags
& (BAD_FFLAGS
| COMPR_FFLAG
| ENCR_FFLAG
))
693 if(NOQUIET
) warning("ID3v2: skipping invalid/unsupported frame");
697 for(i
= 0; i
< KNOWN_FRAMES
; ++i
)
698 if(!strncmp(frame_type
[i
], id
, 4)){ tt
= i
; break; }
700 if(id
[0] == 'T' && tt
!= extra
) tt
= text
;
704 int rva_mode
= -1; /* mix / album */
705 unsigned long realsize
= framesize
;
706 unsigned char* realdata
= tagdata
+pos
;
707 if((flags
& UNSYNC_FLAG
) || (fflags
& UNSYNC_FFLAG
))
709 unsigned long ipos
= 0;
710 unsigned long opos
= 0;
711 debug("Id3v2: going to de-unsync the frame data");
712 /* de-unsync: FF00 -> FF; real FF00 is simply represented as FF0000 ... */
713 /* damn, that means I have to delete bytes from withing the data block... thus need temporal storage */
714 /* standard mandates that de-unsync should always be safe if flag is set */
715 realdata
= (unsigned char*) malloc(framesize
); /* will need <= bytes */
718 if(NOQUIET
) error("ID3v2: unable to allocate working buffer for de-unsync");
721 /* now going byte per byte through the data... */
722 realdata
[0] = tagdata
[pos
];
724 for(ipos
= pos
+1; ipos
< pos
+framesize
; ++ipos
)
726 if(!((tagdata
[ipos
] == 0) && (tagdata
[ipos
-1] == 0xff)))
728 realdata
[opos
++] = tagdata
[ipos
];
732 debug2("ID3v2: de-unsync made %lu out of %lu bytes", realsize
, framesize
);
734 pos
= 0; /* now at the beginning again... */
739 process_comment(fr
, tt
, (char*)realdata
, realsize
, comment
+1, id
);
741 case extra
: /* perhaps foobar2000's work */
742 process_extra(fr
, (char*)realdata
, realsize
, extra
+1, id
);
744 case rva2
: /* "the" RVA tag */
746 /* starts with null-terminated identification */
747 if(VERBOSE3
) fprintf(stderr
, "Note: RVA2 identification \"%s\"\n", realdata
);
748 /* default: some individual value, mix mode */
750 if( !strncasecmp((char*)realdata
, "album", 5)
751 || !strncasecmp((char*)realdata
, "audiophile", 10)
752 || !strncasecmp((char*)realdata
, "user", 4))
754 if(fr
->rva
.level
[rva_mode
] <= rva2
+1)
756 pos
+= strlen((char*) realdata
) + 1;
757 if(realdata
[pos
] == 1)
760 /* only handle master channel */
761 debug("ID3v2: it is for the master channel");
762 /* two bytes adjustment, one byte for bits representing peak - n bytes, eh bits, for peak */
763 /* 16 bit signed integer = dB * 512 ... the double cast is needed to preserve the sign of negative values! */
764 fr
->rva
.gain
[rva_mode
] = (float) ( (((short)((signed char)realdata
[pos
])) << 8) | realdata
[pos
+1] ) / 512;
766 if(VERBOSE3
) fprintf(stderr
, "Note: RVA value %fdB\n", fr
->rva
.gain
[rva_mode
]);
767 /* heh, the peak value is represented by a number of bits - but in what manner? Skipping that part */
768 fr
->rva
.peak
[rva_mode
] = 0;
769 fr
->rva
.level
[rva_mode
] = rva2
+1;
774 /* non-rva metainfo, simply store... */
776 process_text(fr
, (char*)realdata
, realsize
, id
);
778 default: if(NOQUIET
) error1("ID3v2: unknown frame type %i", tt
);
780 if((flags
& UNSYNC_FLAG
) || (fflags
& UNSYNC_FFLAG
)) free(realdata
);
783 #undef PRES_TAG_FFLAG
784 #undef PRES_FILE_FFLAG
785 #undef READ_ONLY_FFLAG
799 /* There are tags with zero length. Strictly not an error, then. */
800 if(length
> 0 && NOQUIET
) error("ID3v2: Duh, not able to read ID3v2 tag data.");
808 if(NOQUIET
) error1("ID3v2: Arrg! Unable to allocate %lu bytes for interpreting ID3v2 data - trying to skip instead.", length
);
809 if((ret2
= fr
->rd
->skip_bytes(fr
,length
)) < 0) ret
= ret2
; /* will not store data in backbuff! */
813 #endif /* NO_ID3V2 */
814 /* skip footer if present */
815 if((ret
> 0) && (flags
& FOOTER_FLAG
) && ((ret2
= fr
->rd
->skip_bytes(fr
,length
)) < 0)) ret
= ret2
;
825 #ifndef NO_ID3V2 /* Disabling all the rest... */
827 static void convert_latin1(mpg123_string
*sb
, const unsigned char* s
, size_t l
, const int noquiet
)
832 /* determine real length, a latin1 character can at most take 2 in UTF8 */
834 if(s
[i
] >= 0x80) ++length
;
836 debug1("UTF-8 length: %lu", (unsigned long)length
);
837 /* one extra zero byte for paranoia */
838 if(!mpg123_resize_string(sb
, length
+1)){ mpg123_free_string(sb
); return ; }
840 p
= (unsigned char*) sb
->p
; /* Signedness doesn't matter but it shows I thought about the non-issue */
842 if(s
[i
] < 0x80){ *p
= s
[i
]; ++p
; }
843 else /* two-byte encoding */
845 *p
= 0xc0 | (s
[i
]>>6);
846 *(p
+1) = 0x80 | (s
[i
] & 0x3f);
855 Check if we have a byte oder mark(s) there, return:
860 This modifies source and len to indicate the data _after_ the BOM(s).
861 Note on nasty data: The last encountered BOM determines the endianess.
862 I have seen data with multiple BOMS, namely from "the" id3v2 program.
863 Not nice, but what should I do?
865 static int check_bom(const unsigned char** source
, size_t *len
)
870 if(*len
< 2) return 0;
872 if((*source
)[0] == 0xff && (*source
)[1] == 0xfe)
875 if((*source
)[0] == 0xfe && (*source
)[1] == 0xff)
878 /* Skip the detected BOM. */
883 /* Check for following BOMs. The last one wins! */
884 further_bom
= check_bom(source
, len
);
885 if(further_bom
== 0) return this_bom
; /* End of the recursion. */
886 else return further_bom
;
891 #define FULLPOINT(f,s) ( (((f)&0x3ff)<<10) + ((s)&0x3ff) + 0x10000 )
892 /* Remember: There's a limit at 0x1ffff. */
893 #define UTF8LEN(x) ( (x)<0x80 ? 1 : ((x)<0x800 ? 2 : ((x)<0x10000 ? 3 : 4)))
894 static void convert_utf16bom(mpg123_string
*sb
, const unsigned char* s
, size_t l
, const int noquiet
)
897 size_t n
; /* number bytes that make up full pairs */
899 size_t length
= 0; /* the resulting UTF-8 length */
900 /* Determine real length... extreme case can be more than utf-16 length. */
905 debug1("convert_utf16 with length %lu", (unsigned long)l
);
907 bom_endian
= check_bom(&s
, &l
);
908 debug1("UTF16 endianess check: %i", bom_endian
);
910 if(bom_endian
== -1) /* little-endian */
912 high
= 1; /* The second byte is the high byte. */
913 low
= 0; /* The first byte is the low byte. */
916 n
= (l
/2)*2; /* number bytes that make up full pairs */
918 /* first: get length, check for errors -- stop at first one */
919 for(i
=0; i
< n
; i
+=2)
921 unsigned long point
= ((unsigned long) s
[i
+high
]<<8) + s
[i
+low
];
922 if((point
& 0xd800) == 0xd800) /* lead surrogate */
924 unsigned short second
= (i
+3 < l
) ? (s
[i
+2+high
]<<8) + s
[i
+2+low
] : 0;
925 if((second
& 0xdc00) == 0xdc00) /* good... */
927 point
= FULLPOINT(point
,second
);
928 length
+= UTF8LEN(point
); /* possibly 4 bytes */
929 i
+=2; /* We overstepped one word. */
931 else /* if no valid pair, break here */
933 if(noquiet
) error2("Invalid UTF16 surrogate pair at %li (0x%04lx).", (unsigned long)i
, point
);
934 n
= i
; /* Forget the half pair, END! */
938 else length
+= UTF8LEN(point
); /* 1,2 or 3 bytes */
941 if(!mpg123_resize_string(sb
, length
+1)){ mpg123_free_string(sb
); return ; }
943 /* Now really convert, skip checks as these have been done just before. */
944 p
= (unsigned char*) sb
->p
; /* Signedness doesn't matter but it shows I thought about the non-issue */
945 for(i
=0; i
< n
; i
+=2)
947 unsigned long codepoint
= ((unsigned long) s
[i
+high
]<<8) + s
[i
+low
];
948 if((codepoint
& 0xd800) == 0xd800) /* lead surrogate */
950 unsigned short second
= (s
[i
+2+high
]<<8) + s
[i
+2+low
];
951 codepoint
= FULLPOINT(codepoint
,second
);
952 i
+=2; /* We overstepped one word. */
954 if(codepoint
< 0x80) *p
++ = (unsigned char) codepoint
;
955 else if(codepoint
< 0x800)
957 *p
++ = (unsigned char) (0xc0 | (codepoint
>>6));
958 *p
++ = (unsigned char) (0x80 | (codepoint
& 0x3f));
960 else if(codepoint
< 0x10000)
962 *p
++ = (unsigned char) (0xe0 | (codepoint
>>12));
963 *p
++ = 0x80 | ((codepoint
>>6) & 0x3f);
964 *p
++ = 0x80 | (codepoint
& 0x3f);
966 else if (codepoint
< 0x200000)
968 *p
++ = (unsigned char) (0xf0 | codepoint
>>18);
969 *p
++ = (unsigned char) (0x80 | ((codepoint
>>12) & 0x3f));
970 *p
++ = (unsigned char) (0x80 | ((codepoint
>>6) & 0x3f));
971 *p
++ = (unsigned char) (0x80 | (codepoint
& 0x3f));
972 } /* ignore bigger ones (that are not possible here anyway) */
974 sb
->p
[sb
->size
-1] = 0; /* paranoia... */
980 static void convert_utf8(mpg123_string
*sb
, const unsigned char* source
, size_t len
, const int noquiet
)
982 if(mpg123_resize_string(sb
, len
+1))
984 memcpy(sb
->p
, source
, len
);
988 else mpg123_free_string(sb
);