Debian Codegraph @

src/libmpg123/id3.c

raw
1 /*
2 id3: ID3v2.3 and ID3v2.4 parsing (a relevant subset)
3
4 copyright 2006-2023 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
7
8 WIP: Handling of multiple ID3 tags in a stream.
9
10 1. With update flag: Add non-unique data, replace unique.
11 - Only one TALB, TPE1, etc.
12 - Only one TXXX with a certain description.
13 - Only one COMM with certain language and description.
14 - Only one APIC with certain type and description, generally only one
15 of type 1 and 2 each.
16 2. Without update flag: wipe whole data and only store new stuff.
17
18 BIG BAD BUT: How to properly handle seeks in a stream that make
19 the parser encounter the same tags again in random order? Is
20 there even a correct way to handle that without storing an
21 ordered list of all tags? I could simplify the code here and just
22 communicate that a frame should be an update to previous, and
23 at which stream position the frame was encountered. But since
24 libmpg123 is driven by MPEG frames, there could be multiple
25 ID3v2 tags in direct succession treated by the parser without
26 the library user being able to interfere.
27
28 This is severely fucked. All that complexity also doesn't matter
29 in practice, as streams use ICY and individual files have just one
30 ID3v2 tag (relevant for libmpg123). It's an academic problem. But
31 for seekable files, I could implement some jumping logic to find
32 and parse all ID3v2 for once and then set a flag that only jumps
33 the frames on seeks. That covers all local disk playback. For
34 streams, seeking is no issue (seeking back, at least), so the
35 update/replace logic works.
36
37 Look at the standard:
38
39 ------
40 5. Tag location
41
42 The default location of an ID3v2 tag is prepended to the audio so
43 that players can benefit from the information when the data is
44 streamed. It is however possible to append the tag, or make a
45 prepend/append combination. When deciding upon where an unembedded
46 tag should be located, the following order of preference SHOULD be
47 considered.
48
49 1. Prepend the tag.
50
51 2. Prepend a tag with all vital information and add a second tag at
52 the end of the file, before tags from other tagging systems. The
53 first tag is required to have a SEEK frame.
54
55 3. Add a tag at the end of the file, before tags from other tagging
56 systems.
57
58 In case 2 and 3 the tag can simply be appended if no other known tags
59 are present. The suggested method to find ID3v2 tags are:
60
61 1. Look for a prepended tag using the pattern found in section 3.1.
62
63 2. If a SEEK frame was found, use its values to guide further
64 searching.
65
66 3. Look for a tag footer, scanning from the back of the file.
67
68 For every new tag that is found, the old tag should be discarded
69 unless the update flag in the extended header (section 3.2) is set.
70 ------
71
72 For seekable streams, I simply need to implement explicit ID3v2 search along
73 that recommendation and keep the complete information. Streams that continue
74 growing during playback will not recognize added ID3v2 tags. So be it.
75 For non-seekable streams, a tag is always parsed when encountered, assuming
76 the order of update tags always matches.
77
78 First step for the 1.26 release shall be the implementaton of the update
79 logic and glossing over the theoretical problem of re-parsing update
80 frames in the wrong order by ignoring it. They are not that relevant.
81
82 TODO: Cave in and add the missing frames from the spec. Not that far to go.
83 But need another data structure to communicate those ...
84 */
85
86 #include "mpg123lib_intern.h"
87 #include "id3.h"
88 #include "../common/debug.h"
89
90 #ifndef NO_ID3V2 /* Only the main parsing routine will always be there. */
91
92 /* We know the usual text frames plus some specifics. */
93 #define KNOWN_FRAMES 5
94 static const char frame_type[KNOWN_FRAMES][5] = { "COMM", "TXXX", "RVA2", "USLT", "APIC" };
95 enum frame_types { unknown = -2, text = -1, comment, extra, rva2, uslt, picture };
96
97 /* UTF support definitions */
98
99 typedef void (*text_converter)(mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet);
100
101 static void convert_latin1 (mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet);
102 static void convert_utf16bom(mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet);
103 static void convert_utf8 (mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet);
104
105 static const text_converter text_converters[4] =
106 {
107 convert_latin1,
108 /* We always check for (multiple) BOM in 16bit unicode. Without BOM, UTF16 BE is the default.
109 Errors in encoding are detected anyway. */
110 convert_utf16bom,
111 convert_utf16bom,
112 convert_utf8
113 };
114
115 static const unsigned int encoding_widths[4] = { 1, 2, 2, 1 };
116
117 /* the code starts here... */
118
119 static void null_id3_links(mpg123_handle *fr)
120 {
121 fr->id3v2.title = NULL;
122 fr->id3v2.artist = NULL;
123 fr->id3v2.album = NULL;
124 fr->id3v2.year = NULL;
125 fr->id3v2.genre = NULL;
126 fr->id3v2.comment = NULL;
127 }
128
129 void INT123_init_id3(mpg123_handle *fr)
130 {
131 fr->id3v2.version = 0; /* nothing there */
132 null_id3_links(fr);
133 fr->id3v2.comments = 0;
134 fr->id3v2.comment_list = NULL;
135 fr->id3v2.texts = 0;
136 fr->id3v2.text = NULL;
137 fr->id3v2.extras = 0;
138 fr->id3v2.extra = NULL;
139 fr->id3v2.pictures = 0;
140 fr->id3v2.picture = NULL;
141 }
142
143 /* Managing of the text, comment and extra lists. */
144
145 /* Initialize one element. */
146 static void init_mpg123_text(mpg123_text *txt)
147 {
148 mpg123_init_string(&txt->text);
149 mpg123_init_string(&txt->description);
150 txt->id[0] = 0;
151 txt->id[1] = 0;
152 txt->id[2] = 0;
153 txt->id[3] = 0;
154 txt->lang[0] = 0;
155 txt->lang[1] = 0;
156 txt->lang[2] = 0;
157 }
158
159 static void init_mpg123_picture(mpg123_picture *pic)
160 {
161 mpg123_init_string(&pic->mime_type);
162 mpg123_init_string(&pic->description);
163 pic->type = 0;
164 pic->size = 0;
165 pic->data = NULL;
166 }
167
168 /* Free memory of one element. */
169 static void free_mpg123_text(mpg123_text *txt)
170 {
171 mpg123_free_string(&txt->text);
172 mpg123_free_string(&txt->description);
173 }
174
175 static void free_mpg123_picture(mpg123_picture * pic)
176 {
177 mpg123_free_string(&pic->mime_type);
178 mpg123_free_string(&pic->description);
179 if (pic->data != NULL)
180 free(pic->data);
181 }
182
183 /* Free memory of whole list. */
184 #define free_comment(mh) free_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments))
185 #define free_text(mh) free_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts))
186 #define free_extra(mh) free_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras))
187 #define free_picture(mh) free_id3_picture(&((mh)->id3v2.picture), &((mh)->id3v2.pictures))
188 static void free_id3_text(mpg123_text **list, size_t *size)
189 {
190 size_t i;
191 for(i=0; i<*size; ++i) free_mpg123_text(&((*list)[i]));
192
193 free(*list);
194 *list = NULL;
195 *size = 0;
196 }
197 static void free_id3_picture(mpg123_picture **list, size_t *size)
198 {
199 size_t i;
200 for(i=0; i<*size; ++i) free_mpg123_picture(&((*list)[i]));
201
202 free(*list);
203 *list = NULL;
204 *size = 0;
205 }
206
207 /* Add items to the list. */
208
209 #define add_comment(mh, l, d) add_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments), NULL, l, d)
210 #define add_text(mh, id) add_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts), id, NULL, NULL)
211 #define add_uslt(mh, l, d) add_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts), id, l, d)
212 #define add_extra(mh, d) add_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras), NULL, NULL, d)
213 #define add_picture(mh, t, d) add_id3_picture(&((mh)->id3v2.picture), &((mh)->id3v2.pictures), t, d)
214 static mpg123_text *add_id3_text( mpg123_text **list, size_t *size
215 , char id[4], char lang[3], mpg123_string *description )
216 {
217 mdebug( "add_id3_text id=%s lang=%s, desc=%s"
218 , id ? (char[5]) { id[0], id[1], id[2], id[3], 0 } : "(nil)"
219 , lang ? (char[4]) { lang[0], lang[1], lang[2], 0 } : "(nil)"
220 , description ? (description->fill ? description->p : "(empty)") : "(nil)" );
221 if(lang && !description)
222 return NULL; // no lone language intended
223 if(id || description)
224 {
225 // Look through list of existing texts and return an existing entry
226 // if it should be overwritten.
227 for(size_t i=0; i<*size; ++i)
228 {
229 mpg123_text *entry = *list+i;
230 if(description)
231 { // Overwrite entry with same description and same ID and language.
232 if( (!id || !memcmp(id, entry->id, 4))
233 && (!lang || !memcmp(entry->lang, lang, 3))
234 && mpg123_same_string(&(entry->description), description)
235 )
236 return entry;
237 } else if(id && !memcmp(id, entry->id, 4))
238 return entry; // Just ovewrite because of same ID.
239 mdebug("add_id3_text: entry %zu was no match", i);
240 }
241 }
242 mdebug("add_id3_text: append to list of %zu", *size);
243 // Nothing found, add new one.
244 mpg123_text *x = INT123_safe_realloc(*list, sizeof(mpg123_text)*(*size+1));
245 if(x == NULL) return NULL; /* bad */
246
247 *list = x;
248 *size += 1;
249 init_mpg123_text(&((*list)[*size-1]));
250
251 return &((*list)[*size-1]); /* Return pointer to the added text. */
252 }
253
254
255 static mpg123_picture *add_id3_picture(mpg123_picture **list, size_t *size, char type, mpg123_string *description)
256 {
257 if(!description)
258 return NULL;
259
260 // Return entry to overwrite, if appropriate.
261 for(size_t i=0; i<*size; ++i)
262 {
263 mpg123_picture *entry = *list+i;
264 if( type == entry->type
265 && ( type == 1 || type == 2 ||
266 mpg123_same_string(&entry->description, description)
267 )
268 )
269 return entry;
270 }
271 // Append a new one.
272 mpg123_picture *x = INT123_safe_realloc(*list, sizeof(mpg123_picture)*(*size+1));
273 if(x == NULL) return NULL; /* bad */
274
275 *list = x;
276 *size += 1;
277 init_mpg123_picture(&((*list)[*size-1]));
278
279 return &((*list)[*size-1]); /* Return pointer to the added picture. */
280 }
281
282 /* OK, back to the higher level functions. */
283
284 void INT123_exit_id3(mpg123_handle *fr)
285 {
286 free_picture(fr);
287 free_comment(fr);
288 free_extra(fr);
289 free_text(fr);
290 }
291
292 void INT123_reset_id3(mpg123_handle *fr)
293 {
294 INT123_exit_id3(fr);
295 INT123_init_id3(fr);
296 }
297
298 /* Set the id3v2.artist id3v2.title ... links to elements of the array. */
299 void INT123_id3_link(mpg123_handle *fr)
300 {
301 size_t i;
302 mpg123_id3v2 *v2 = &fr->id3v2;
303 debug("linking ID3v2");
304 null_id3_links(fr);
305 for(i=0; i<v2->texts; ++i)
306 {
307 mpg123_text *entry = &v2->text[i];
308 if (!strncmp("TIT2", entry->id, 4)) v2->title = &entry->text;
309 else if(!strncmp("TALB", entry->id, 4)) v2->album = &entry->text;
310 else if(!strncmp("TPE1", entry->id, 4)) v2->artist = &entry->text;
311 else if(!strncmp("TYER", entry->id, 4)) v2->year = &entry->text;
312 else if(!strncmp("TCON", entry->id, 4)) v2->genre = &entry->text;
313 }
314 for(i=0; i<v2->comments; ++i)
315 {
316 mpg123_text *entry = &v2->comment_list[i];
317 if(entry->description.fill == 0 || entry->description.p[0] == 0)
318 v2->comment = &entry->text;
319 }
320 /* When no generic comment found, use the last non-generic one. */
321 if(v2->comment == NULL && v2->comments > 0)
322 v2->comment = &v2->comment_list[v2->comments-1].text;
323 }
324
325 /*
326 Store ID3 text data in an mpg123_string; either verbatim copy or
327 everything translated to UTF-8 encoding.
328 Preserve the zero string separator (I don't need strlen for the total size).
329
330 Since we can overwrite strings with ID3 update frames, don't free
331 memory, just grow strings.
332 */
333 static void store_id3_text(mpg123_string *sb, unsigned char encoding, unsigned char *source
334 , size_t source_size, const int noquiet, const int notranslate)
335 {
336 if(sb) // Always overwrite, even with nothing.
337 sb->fill = 0;
338 if(!source_size)
339 {
340 debug("Empty id3 data!");
341 return;
342 }
343
344 /* We shall just copy the data. Client wants to decode itself. */
345 if(notranslate)
346 {
347 /* Future: Add a path for ID3 errors. */
348 if(!mpg123_grow_string(sb, source_size+1))
349 {
350 if(noquiet) error("Cannot resize target string, out of memory?");
351 return;
352 }
353 sb->p[0] = (char)encoding;
354 memcpy(sb->p+1, source, source_size);
355 sb->fill = source_size+1;
356 debug1("stored undecoded ID3 text of size %zu", source_size+1);
357 return;
358 }
359
360 if(encoding > mpg123_id3_enc_max)
361 {
362 if(noquiet)
363 error1("Unknown text encoding %u, I take no chances, sorry!", encoding);
364 return;
365 }
366 INT123_id3_to_utf8(sb, encoding, source, source_size, noquiet);
367
368 if(sb->fill) debug1("UTF-8 string (the first one): %s", sb->p);
369 else if(noquiet) error("unable to convert string to UTF-8 (out of memory, junk input?)!");
370 }
371
372 /* On error, sb->size is 0. */
373 /* Also, encoding has been checked already! */
374 void INT123_id3_to_utf8(mpg123_string *sb, unsigned char encoding, const unsigned char *source, size_t source_size, int noquiet)
375 {
376 unsigned int bwidth;
377 if(sb)
378 sb->fill = 0;
379 debug1("encoding: %u", encoding);
380 /* A note: ID3v2.3 uses UCS-2 non-variable 16bit encoding, v2.4 uses UTF16.
381 UTF-16 uses a reserved/private range in UCS-2 to add the magic, so we just always treat it as UTF. */
382 bwidth = encoding_widths[encoding];
383 /* Hack! I've seen a stray zero byte before BOM. Is that supposed to happen? */
384 if(encoding != mpg123_id3_utf16be) /* UTF16be _can_ beging with a null byte! */
385 while(source_size > bwidth && source[0] == 0)
386 {
387 --source_size;
388 ++source;
389 debug("skipped leading zero");
390 }
391 if(source_size % bwidth)
392 {
393 /* When we need two bytes for a character, it's strange to have an uneven bytestream length. */
394 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);
395 source_size -= source_size % bwidth;
396 }
397 text_converters[encoding](sb, source, source_size, noquiet);
398 }
399
400 /* You have checked encoding to be in the range already. */
401 static unsigned char *next_text(unsigned char* prev, unsigned char encoding, size_t limit)
402 {
403 unsigned char *text = prev;
404 size_t width = encoding_widths[encoding];
405
406 if(limit > PTRDIFF_MAX)
407 return NULL;
408
409 /* So I go lengths to find zero or double zero...
410 Remember bug 2834636: Only check for aligned NULLs! */
411 while(text-prev < (ptrdiff_t)limit)
412 {
413 if(text[0] == 0)
414 {
415 if(width <= limit-(text-prev))
416 {
417 size_t i = 1;
418 for(; i<width; ++i) if(text[i] != 0) break;
419
420 if(i == width) /* found a null wide enough! */
421 {
422 text += width;
423 break;
424 }
425 }
426 else return NULL; /* No full character left? This text is broken */
427 }
428
429 text += width;
430 }
431 if((size_t)(text-prev) >= limit) text = NULL;
432
433 return text;
434 }
435
436 static const char *enc_name(unsigned char enc)
437 {
438 switch(enc)
439 {
440 case 0: return "Latin 1";
441 case 1: return "UTF-16 BOM";
442 case 2: return "UTF-16 BE";
443 case 3: return "UTF-8";
444 default: return "unknown!";
445 }
446 }
447
448 static void process_text(mpg123_handle *fr, unsigned char *realdata, size_t realsize, char *id)
449 {
450 /* Text encoding $xx */
451 /* The text (encoded) ... */
452 if(realsize < 1)
453 {
454 if(NOQUIET) error("Not even an encoding byte?");
455 return;
456 }
457 mpg123_text *t = add_text(fr, id);
458 if(VERBOSE4) fprintf(stderr, "Note: Storing text from %s encoding\n", enc_name(realdata[0]));
459 if(t == NULL)
460 {
461 if(NOQUIET) error("Unable to attach new text!");
462 return;
463 }
464 mdebug("process_text: (over)writing entry with ID %s", t->id[0]
465 ? (char[5]) { t->id[0], t->id[1], t->id[2], t->id[3], 0 }
466 : "(nil)" );
467 memcpy(t->id, id, 4);
468 store_id3_text( &t->text, realdata[0], realdata+1, realsize-1
469 , NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT );
470 if(VERBOSE4) // Do not print unsanitized text to terminals!
471 fprintf(stderr, "Note: ID3v2 %c%c%c%c text frame stored\n", id[0], id[1], id[2], id[3]);
472 }
473
474 static void process_picture(mpg123_handle *fr, unsigned char *realdata, size_t realsize)
475 {
476 unsigned char encoding;
477 mpg123_picture *i = NULL;
478 unsigned char* workpoint = NULL;
479 mpg123_string mime; mpg123_init_string(&mime);
480 unsigned char image_type = 0;
481 mpg123_string description; mpg123_init_string(&description);
482 unsigned char *image_data = NULL;
483 if(realsize < 1)
484 {
485 debug("Empty id3 data!");
486 return;
487 }
488 encoding = realdata[0];
489 realdata++; realsize--;
490 if(encoding > mpg123_id3_enc_max)
491 {
492 if(NOQUIET)
493 error1("Unknown text encoding %u, I take no chances, sorry!", encoding);
494 return;
495 }
496 if(VERBOSE4) fprintf(stderr, "Note: Storing picture from APIC frame.\n");
497
498 /* get mime type (encoding is always latin-1) */
499 workpoint = next_text(realdata, 0, realsize);
500 if(!workpoint)
501 {
502 if(NOQUIET)
503 error("Unable to get mime type for picture; skipping picture.");
504 return;
505 }
506 INT123_id3_to_utf8(&mime, 0, realdata, workpoint - realdata, NOQUIET);
507 realsize -= workpoint - realdata;
508 realdata = workpoint;
509 /* get picture type */
510 image_type = realdata[0];
511 realdata++; realsize--;
512 /* get description (encoding is encoding) */
513 workpoint = next_text(realdata, encoding, realsize);
514 if(!workpoint)
515 {
516 if(NOQUIET)
517 error("Unable to get description for picture; skipping picture.");
518 mpg123_free_string(&mime);
519 return;
520 }
521 INT123_id3_to_utf8(&description, encoding, realdata, workpoint - realdata, NOQUIET);
522 realsize -= workpoint - realdata;
523 if(realsize)
524 image_data = (unsigned char*)malloc(realsize);
525 if(!realsize || !image_data) {
526 if(NOQUIET)
527 error("No picture data or malloc failure; skipping picture.");
528 mpg123_free_string(&description);
529 mpg123_free_string(&mime);
530 return;
531 }
532 memcpy(image_data, workpoint, realsize);
533
534 // All data ready now, append to/replace in list.
535 i = add_picture(fr, image_type, &description);
536 if(!i)
537 {
538 if(NOQUIET)
539 error("Unable to attach new picture!");
540 free(image_data);
541 mpg123_free_string(&description);
542 mpg123_free_string(&mime);
543 return;
544 }
545
546 // Either this is a fresh image, or one to be replaced.
547 // We hand over memory, so old storage needs to be freed.
548 free_mpg123_picture(i);
549 i->type = image_type;
550 i->size = realsize;
551 i->data = image_data;
552 mpg123_move_string(&mime, &i->mime_type);
553 mpg123_move_string(&description, &i->description);
554 if(VERBOSE4)
555 fprintf(stderr, "Note: ID3v2 APIC picture frame of type: %d\n", i->type);
556 }
557
558 /* Store a new comment that perhaps is a RVA / RVA_ALBUM/AUDIOPHILE / RVA_MIX/RADIO one
559 Special gimmik: It also stores USLT to the texts. Stucture is the same as for comments. */
560 static void process_comment(mpg123_handle *fr, enum frame_types tt, unsigned char *realdata, size_t realsize, int rva_level, char *id)
561 {
562 /* Text encoding $xx */
563 /* Language $xx xx xx */
564 /* Short description (encoded!) <text> $00 (00) */
565 /* Then the comment text (encoded) ... */
566 unsigned char encoding = realdata[0];
567 char lang[3]; // realdata + 1
568 unsigned char *descr = realdata+4;
569 unsigned char *text = NULL;
570 mpg123_text *xcom = NULL;
571 mpg123_text localcom; // UTF-8 variant for local processing, remember to clean up!
572 init_mpg123_text(&localcom);
573
574 if(realsize < (size_t)(descr-realdata))
575 {
576 if(NOQUIET) error1("Invalid frame size of %zu (too small for anything).", realsize);
577 return;
578 }
579 if(encoding > mpg123_id3_enc_max)
580 {
581 if(NOQUIET)
582 error1("Unknown text encoding %u, I take no chances, sorry!", encoding);
583 return;
584 }
585 memcpy(lang, realdata+1, 3);
586 /* Be careful with finding the end of description, I have to honor encoding here. */
587 text = next_text(descr, encoding, realsize-(descr-realdata));
588 if(text == NULL)
589 {
590 if(NOQUIET)
591 error("No comment text / valid description?");
592 return;
593 }
594 { // just vor variable scope
595 mpg123_string description;
596 mpg123_init_string(&description);
597 // Store the text, with desired encoding, but for comments always a local copy in UTF-8.
598 store_id3_text( &description, encoding, descr, text-descr
599 , NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT );
600 if(tt == comment)
601 store_id3_text( &localcom.description, encoding, descr, text-descr
602 , NOQUIET, 0 );
603 if(VERBOSE4)
604 fprintf( stderr, "Note: Storing comment from %s encoding\n"
605 , enc_name(encoding) );
606 xcom = tt == uslt
607 ? add_uslt(fr, lang, &description)
608 : add_comment(fr, lang, &description);
609 if(xcom == NULL)
610 {
611 if(NOQUIET)
612 error("Unable to attach new comment!");
613 mpg123_free_string(&description);
614 free_mpg123_text(&localcom);
615 return;
616 }
617 memcpy(xcom->id, id, 4);
618 memcpy(xcom->lang, lang, 3);
619 // That takes over the description allocation.
620 mpg123_move_string(&description, &xcom->description);
621 }
622
623 store_id3_text( &xcom->text, encoding, text, realsize-(text-realdata)
624 , NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT );
625 /* Remember: I will probably decode the above (again) for rva comment checking. So no messing around, please. */
626
627 if(VERBOSE4) /* Do _not_ print the verbatim text: The encoding might be funny! */
628 {
629 fprintf(stderr, "Note: ID3 comm/uslt desc of length %zu.\n", xcom->description.fill);
630 fprintf(stderr, "Note: ID3 comm/uslt text of length %zu.\n", xcom->text.fill);
631 }
632 /* Look out for RVA info only when we really deal with a straight comment. */
633 if(tt == comment && localcom.description.fill > 0)
634 {
635 int rva_mode = -1; /* mix / album */
636 if( !strcasecmp(localcom.description.p, "rva")
637 || !strcasecmp(localcom.description.p, "rva_mix")
638 || !strcasecmp(localcom.description.p, "rva_track")
639 || !strcasecmp(localcom.description.p, "rva_radio") )
640 rva_mode = 0;
641 else if( !strcasecmp(localcom.description.p, "rva_album")
642 || !strcasecmp(localcom.description.p, "rva_audiophile")
643 || !strcasecmp(localcom.description.p, "rva_user") )
644 rva_mode = 1;
645 if((rva_mode > -1) && (fr->rva.level[rva_mode] <= rva_level))
646 {
647 /* Only translate the contents in here where we really need them. */
648 store_id3_text( &localcom.text, encoding, text, realsize-(text-realdata)
649 , NOQUIET, 0 );
650 if(localcom.text.fill > 0)
651 {
652 fr->rva.gain[rva_mode] = (float) atof(localcom.text.p);
653 if(VERBOSE3) fprintf(stderr, "Note: RVA value %fdB\n", fr->rva.gain[rva_mode]);
654 fr->rva.peak[rva_mode] = 0;
655 fr->rva.level[rva_mode] = rva_level;
656 }
657 }
658 }
659 /* Make sure to free the local memory... */
660 free_mpg123_text(&localcom);
661 }
662
663 static void process_extra(mpg123_handle *fr, unsigned char* realdata, size_t realsize, int rva_level, char *id)
664 {
665 /* Text encoding $xx */
666 /* Description ... $00 (00) */
667 /* Text ... */
668 unsigned char encoding = realdata[0];
669 unsigned char *descr = realdata+1; /* remember, the encoding is descr[-1] */
670 unsigned char *text;
671 mpg123_text *xex;
672 mpg123_text localex;
673
674 if((int)realsize < descr-realdata)
675 {
676 if(NOQUIET) error1("Invalid frame size of %lu (too small for anything).", (unsigned long)realsize);
677 return;
678 }
679 if(encoding > mpg123_id3_enc_max)
680 {
681 if(NOQUIET)
682 error1("Unknown text encoding %u, I take no chances, sorry!", encoding);
683 return;
684 }
685 text = next_text(descr, encoding, realsize-(descr-realdata));
686 if(VERBOSE4) fprintf(stderr, "Note: Storing extra from %s encoding\n", enc_name(realdata[0]));
687 if(text == NULL)
688 {
689 if(NOQUIET) error("No extra frame text / valid description?");
690 return;
691 }
692 { // just vor variable scope
693 mpg123_string description;
694 mpg123_init_string(&description);
695 /* The outside storage gets reencoded to UTF-8 only if not requested otherwise.
696 Remember that we really need the -1 here to hand in the encoding byte!*/
697 store_id3_text( &description, encoding, descr, text-descr
698 , NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT );
699 xex = add_extra(fr, &description);
700 if(xex)
701 mpg123_move_string(&description, &xex->description);
702 else
703 mpg123_free_string(&description);
704 }
705 if(xex == NULL)
706 {
707 if(NOQUIET) error("Unable to attach new extra text!");
708 return;
709 }
710 memcpy(xex->id, id, 4);
711 init_mpg123_text(&localex); /* For our local copy. */
712
713 /* Our local copy is always stored in UTF-8! */
714 store_id3_text(&localex.description, encoding, descr, text-descr, NOQUIET, 0);
715 /* At first, only store the outside copy of the payload. We may not need the local copy. */
716 store_id3_text( &xex->text, encoding, text, realsize-(text-realdata)
717 , NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT );
718
719 /* Now check if we would like to interpret this extra info for RVA. */
720 if(localex.description.fill > 0)
721 {
722 int is_peak = 0;
723 int rva_mode = -1; /* mix / album */
724
725 if(!strncasecmp(localex.description.p, "replaygain_track_",17))
726 {
727 if(VERBOSE3) fprintf(stderr, "Note: RVA ReplayGain track gain/peak\n");
728
729 rva_mode = 0;
730 if(!strcasecmp(localex.description.p, "replaygain_track_peak")) is_peak = 1;
731 else if(strcasecmp(localex.description.p, "replaygain_track_gain")) rva_mode = -1;
732 }
733 else
734 if(!strncasecmp(localex.description.p, "replaygain_album_",17))
735 {
736 if(VERBOSE3) fprintf(stderr, "Note: RVA ReplayGain album gain/peak\n");
737
738 rva_mode = 1;
739 if(!strcasecmp(localex.description.p, "replaygain_album_peak")) is_peak = 1;
740 else if(strcasecmp(localex.description.p, "replaygain_album_gain")) rva_mode = -1;
741 }
742 if((rva_mode > -1) && (fr->rva.level[rva_mode] <= rva_level))
743 {
744 /* Now we need the translated copy of the data. */
745 store_id3_text( &localex.text, encoding, text, realsize-(text-realdata)
746 , NOQUIET, 0 );
747 if(localex.text.fill > 0)
748 {
749 if(is_peak)
750 {
751 fr->rva.peak[rva_mode] = (float) atof(localex.text.p);
752 if(VERBOSE3) fprintf(stderr, "Note: RVA peak %f\n", fr->rva.peak[rva_mode]);
753 }
754 else
755 {
756 fr->rva.gain[rva_mode] = (float) atof(localex.text.p);
757 if(VERBOSE3) fprintf(stderr, "Note: RVA gain %fdB\n", fr->rva.gain[rva_mode]);
758 }
759 fr->rva.level[rva_mode] = rva_level;
760 }
761 }
762 }
763
764 free_mpg123_text(&localex);
765 }
766
767 /* Make a ID3v2.3+ 4-byte ID from a ID3v2.2 3-byte ID
768 Note that not all frames survived to 2.4; the mapping goes to 2.3 .
769 A notable miss is the old RVA frame, which is very unspecific anyway.
770 This function returns -1 when a not known 3 char ID was encountered, 0 otherwise. */
771 static int promote_framename(mpg123_handle *fr, char *id) /* fr because of VERBOSE macros */
772 {
773 size_t i;
774 char *old[] =
775 {
776 "COM", "TAL", "TBP", "TCM", "TCO", "TCR", "TDA", "TDY", "TEN", "TFT",
777 "TIM", "TKE", "TLA", "TLE", "TMT", "TOA", "TOF", "TOL", "TOR", "TOT",
778 "TP1", "TP2", "TP3", "TP4", "TPA", "TPB", "TRC", "TDA", "TRK", "TSI",
779 "TSS", "TT1", "TT2", "TT3", "TXT", "TXX", "TYE"
780 };
781 char *new[] =
782 {
783 "COMM", "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDAT", "TDLY", "TENC", "TFLT",
784 "TIME", "TKEY", "TLAN", "TLEN", "TMED", "TOPE", "TOFN", "TOLY", "TORY", "TOAL",
785 "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPUB", "TSRC", "TRDA", "TRCK", "TSIZ",
786 "TSSE", "TIT1", "TIT2", "TIT3", "TEXT", "TXXX", "TYER"
787 };
788 for(i=0; i<sizeof(old)/sizeof(char*); ++i)
789 {
790 if(!strncmp(id, old[i], 3))
791 {
792 memcpy(id, new[i], 4);
793 if(VERBOSE3) fprintf(stderr, "Translated ID3v2.2 frame %s to %s\n", old[i], new[i]);
794 return 0;
795 }
796 }
797 if(VERBOSE3) fprintf(stderr, "Ignoring untranslated ID3v2.2 frame %c%c%c\n", id[0], id[1], id[2]);
798 return -1;
799 }
800
801 #endif /* NO_ID3V2 */
802
803 static int store_id3v2( mpg123_handle *fr
804 , unsigned long first4bytes, unsigned char buf[6], unsigned long length )
805 {
806 int ret = 1;
807 int64_t ret2;
808 unsigned long fullen = 10+length;
809 if(fr->id3v2_raw)
810 free(fr->id3v2_raw);
811 fr->id3v2_size = 0;
812 /* Allocate one byte more for a closing zero as safety catch for strlen(). */
813 fr->id3v2_raw = malloc(fullen+1);
814 if(!fr->id3v2_raw)
815 {
816 fr->err = MPG123_OUT_OF_MEM;
817 if(NOQUIET)
818 error1("ID3v2: Arrg! Unable to allocate %lu bytes"
819 " for ID3v2 data - trying to skip instead.", length+1);
820 if((ret2=fr->rd->skip_bytes(fr,length)) < 0)
821 ret = ret2;
822 else
823 ret = 0;
824 }
825 else
826 {
827 fr->id3v2_raw[0] = (first4bytes>>24) & 0xff;
828 fr->id3v2_raw[1] = (first4bytes>>16) & 0xff;
829 fr->id3v2_raw[2] = (first4bytes>>8) & 0xff;
830 fr->id3v2_raw[3] = first4bytes & 0xff;
831 memcpy(fr->id3v2_raw+4, buf, 6);
832 if((ret2=fr->rd->read_frame_body(fr, fr->id3v2_raw+10, length)) < 0)
833 {
834 ret=ret2;
835 free(fr->id3v2_raw);
836 fr->id3v2_raw = NULL;
837 }
838 else
839 { /* Closing with a zero for paranoia. */
840 fr->id3v2_raw[fullen] = 0;
841 fr->id3v2_size = fullen;
842 }
843 }
844 return ret;
845 }
846
847 /*
848 trying to parse ID3v2.3 and ID3v2.4 tags...
849
850 returns: 0: bad or just unparseable tag
851 1: good, (possibly) new tag info
852 <0: reader error (may need more data feed, try again)
853 */
854 int INT123_parse_new_id3(mpg123_handle *fr, unsigned long first4bytes)
855 {
856 #define UNSYNC_FLAG 128
857 #define EXTHEAD_FLAG 64 /* ID3v2.3+ */
858 #define COMPRESS_FLAG 64 /* ID3v2.2 */
859 #define EXP_FLAG 32
860 #define FOOTER_FLAG 16
861 #define EXT_UPDATE_FLAG 64 /* ID3v2.4 only: extended header update flag */
862 #define UNKNOWN_FLAGS 15 /* 00001111*/
863 unsigned char buf[6];
864 unsigned long length=0;
865 unsigned char flags = 0;
866 int ret = 1;
867 int64_t ret2;
868 int storetag = 0;
869 unsigned int footlen = 0;
870 #ifndef NO_ID3V2
871 int skiptag = 0;
872 #endif
873 unsigned char major = first4bytes & 0xff;
874 debug1("ID3v2: major tag version: %i", major);
875
876 if(major == 0xff) return 0; /* Invalid... */
877 if((ret2 = fr->rd->read_frame_body(fr, buf, 6)) < 0) /* read more header information */
878 return ret2;
879
880 if(buf[0] == 0xff) return 0; /* Revision, will never be 0xff. */
881
882 if(fr->p.flags & MPG123_STORE_RAW_ID3)
883 storetag = 1;
884 /* second new byte are some nice flags, if these are invalid skip the whole thing */
885 flags = buf[1];
886 debug1("ID3v2: flags 0x%08x", flags);
887 /* use 4 bytes from buf to construct 28bit uint value and return 1; return 0 if bytes are not synchsafe */
888 #define synchsafe_to_long(buf,res) \
889 ( \
890 (((buf)[0]|(buf)[1]|(buf)[2]|(buf)[3]) & 0x80) ? 0 : \
891 (res = (((unsigned long) (buf)[0]) << 21) \
892 | (((unsigned long) (buf)[1]) << 14) \
893 | (((unsigned long) (buf)[2]) << 7) \
894 | ((unsigned long) (buf)[3]) \
895 ,1) \
896 )
897 /* id3v2.3 does not store synchsafe frame sizes, but synchsafe tag size - doh! */
898 /* Remember: bytes_to_long() can yield ULONG_MAX on 32 bit platforms! */
899 #define bytes_to_long(buf,res) \
900 ( \
901 major == 3 ? \
902 (res = (((unsigned long) (buf)[0]) << 24) \
903 | (((unsigned long) (buf)[1]) << 16) \
904 | (((unsigned long) (buf)[2]) << 8) \
905 | ((unsigned long) (buf)[3]) \
906 ,1) : synchsafe_to_long(buf,res) \
907 )
908 /* for id3v2.2 only */
909 #define threebytes_to_long(buf,res) \
910 ( \
911 res = (((unsigned long) (buf)[0]) << 16) \
912 | (((unsigned long) (buf)[1]) << 8) \
913 | ((unsigned long) (buf)[2]) \
914 )
915
916 /* length-10 or length-20 (footer present); 4 synchsafe integers == 28 bit number */
917 /* we have already read 10 bytes, so left are length or length+10 bytes belonging to tag */
918 /* Note: This is an 28 bit value in 32 bit storage, plenty of space for */
919 /* length+x for reasonable x. */
920 if(!synchsafe_to_long(buf+2,length))
921 {
922 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]);
923 return 0;
924 }
925 if(flags & FOOTER_FLAG)
926 footlen = 10;
927 debug1("ID3v2: tag data length %lu", length);
928 #ifndef NO_ID3V2
929 if(VERBOSE2) fprintf(stderr,"Note: ID3v2.%i rev %i tag of %lu bytes\n", major, buf[0], length);
930 /* skip if unknown version/scary flags, parse otherwise */
931 if(fr->p.flags & MPG123_SKIP_ID3V2)
932 {
933 if(VERBOSE3)
934 fprintf(stderr, "Note: Skipping ID3v2 tag per user request.\n");
935 skiptag = 1;
936 }
937 if((flags & UNKNOWN_FLAGS) || (major > 4) || (major < 2))
938 {
939 if(NOQUIET)
940 warning2( "ID3v2: Won't parse the ID3v2 tag with major version"
941 " %u and flags 0x%xu - some extra code may be needed"
942 , major, flags );
943 skiptag = 1;
944 }
945 // Standard says that compressed tags should be ignored as there isn't an agreed
946 // compressoion scheme.
947 if(major == 2 && flags & COMPRESS_FLAG)
948 {
949 if(NOQUIET)
950 warning("ID3v2: ignoring compressed ID3v2.2 tag");
951 skiptag = 1;
952 }
953 if(length < 10)
954 {
955 if(NOQUIET)
956 warning1("ID3v2: unrealistic small tag lengh %lu, skipping", length);
957 skiptag = 1;
958 }
959 if(!skiptag)
960 storetag = 1;
961 #endif
962 if(storetag)
963 {
964 /* Stores whole tag with footer and an additonal trailing zero. */
965 if((ret2 = store_id3v2(fr, first4bytes, buf, length+footlen)) <= 0)
966 return ret2;
967 }
968 #ifndef NO_ID3V2
969 if(skiptag)
970 {
971 if(VERBOSE3)
972 fprintf(stderr, "Note: skipped tag clearing possibly existing ID3v2 data");
973 INT123_reset_id3(fr); // Old data is invalid.
974 #endif
975 if(!storetag && (ret2=fr->rd->skip_bytes(fr,length+footlen))<0)
976 ret=ret2;
977 #ifndef NO_ID3V2
978 }
979 else
980 {
981 unsigned char* tagdata = fr->id3v2_raw+10;
982 /* try to interpret that beast */
983 debug("ID3v2: analysing frames...");
984 if(length > 0)
985 {
986 unsigned char extflags = 0;
987 unsigned long tagpos = 0;
988 /* bytes of frame title and of framesize value */
989 unsigned int head_part = major > 2 ? 4 : 3;
990 unsigned int flag_part = major > 2 ? 2 : 0;
991 /* The amount of bytes that are unconditionally read for each frame: */
992 /* ID, size, flags. */
993 unsigned int framebegin = head_part+head_part+flag_part;
994 debug1("ID3v2: have read at all %lu bytes for the tag now", (unsigned long)length+6);
995 if(flags & EXTHEAD_FLAG)
996 {
997 debug("ID3v2: extended header");
998 if(!bytes_to_long(tagdata, tagpos) || tagpos >= length)
999 {
1000 ret = 0;
1001 if(NOQUIET)
1002 error4( "Bad (non-synchsafe/too large) tag offset from extended header:"
1003 "0x%02x%02x%02x%02x"
1004 , tagdata[0], tagdata[1], tagdata[2], tagdata[3] );
1005 } else if(tagpos < 6)
1006 {
1007 ret = 0;
1008 if(NOQUIET)
1009 merror("Extended header too small (%lu).", tagpos);
1010 }
1011 if(major == 3)
1012 {
1013 tagpos += 4; // The size itself is not included.
1014 if(tagpos >= length)
1015 {
1016 ret = 0;
1017 if(NOQUIET)
1018 error("Too much extended v2.3 header.");
1019 }
1020 } else if(ret) // v2.4 and at least got my 6 bytes of ext header
1021 {
1022 // Only v4 knows update frames, check for that.
1023 // Need to step back. Header is 4 bytes length, one byte flag size,
1024 // one byte flags. Flag size has to be 1!
1025 if(tagdata[4] == 1 && tagdata[5] & EXT_UPDATE_FLAG)
1026 {
1027 if(VERBOSE3)
1028 fprintf(stderr, "Note: ID3v2.4 update tag\n");
1029 extflags |= EXT_UPDATE_FLAG;
1030 }
1031 }
1032 }
1033 if(!(extflags & EXT_UPDATE_FLAG))
1034 {
1035 if(VERBOSE3)
1036 fprintf(stderr, "Note: non-update tag replacing existing ID3v2 data\n");
1037 INT123_reset_id3(fr);
1038 }
1039 if(ret > 0)
1040 {
1041 char id[5];
1042 unsigned long framesize;
1043 unsigned long fflags; /* need 16 bits, actually */
1044 id[4] = 0;
1045 fr->id3v2.version = major;
1046 /* Pos now advanced after ext head, now a frame has to follow. */
1047 /* Note: tagpos <= length, which is 28 bit integer, so both */
1048 /* far away from overflow for adding known small values. */
1049 /* I want to read at least one full header now. */
1050 while(length >= tagpos+framebegin)
1051 {
1052 int i = 0;
1053 unsigned long pos = tagpos;
1054 /* level 1,2,3 - 0 is info from lame/info tag! */
1055 /* rva tags with ascending significance, then general frames */
1056 enum frame_types tt = unknown;
1057 /* we may have entered the padding zone or any other strangeness: check if we have valid frame id characters */
1058 for(i=0; i< head_part; ++i)
1059 if( !( ((tagdata[tagpos+i] > 47) && (tagdata[tagpos+i] < 58))
1060 || ((tagdata[tagpos+i] > 64) && (tagdata[tagpos+i] < 91)) ) )
1061 {
1062 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]);
1063 /* This is no hard error... let's just hope that we got something meaningful already (ret==1 in that case). */
1064 goto tagparse_cleanup; /* Need to escape two loops here. */
1065 }
1066 if(ret > 0)
1067 {
1068 /* 4 or 3 bytes id */
1069 strncpy(id, (char*) tagdata+pos, head_part);
1070 id[head_part] = 0; /* terminate for 3 or 4 bytes */
1071 pos += head_part;
1072 tagpos += head_part;
1073 /* size as 32 bits or 28 bits */
1074 if(fr->id3v2.version == 2) threebytes_to_long(tagdata+pos, framesize);
1075 else
1076 if(!bytes_to_long(tagdata+pos, framesize))
1077 {
1078 /* Just assume that up to now there was some good data. */
1079 if(NOQUIET) error1("ID3v2: non-syncsafe size of %s frame, skipping the remainder of tag", id);
1080 break;
1081 }
1082 if(VERBOSE3) fprintf(stderr, "Note: ID3v2 %s frame of size %lu\n", id, framesize);
1083 tagpos += head_part;
1084 pos += head_part;
1085 if(fr->id3v2.version > 2)
1086 {
1087 fflags = (((unsigned long) tagdata[pos]) << 8) | ((unsigned long) tagdata[pos+1]);
1088 pos += 2;
1089 tagpos += 2;
1090 }
1091 else fflags = 0;
1092
1093 if(length - tagpos < framesize)
1094 {
1095 if(NOQUIET) error("Whoa! ID3v2 frame claims to be larger than the whole rest of the tag.");
1096 break;
1097 }
1098 tagpos += framesize; /* the important advancement in whole tag */
1099 /* for sanity, after full parsing tagpos should be == pos */
1100 /* debug4("ID3v2: found %s frame, size %lu (as bytes: 0x%08lx), flags 0x%016lx", id, framesize, framesize, fflags); */
1101 /* v2.4: %0abc0000 %0h00kmnp */
1102 /* v2.3: %abc00000 %ijk00000 */
1103 /* v2.2: just zero */
1104 #define V3 (major == 3)
1105 #define BAD_FFLAGS (unsigned long) (V3 ? 7967 : 36784)
1106 #define PRES_TAG_FFLAG (unsigned long) (V3 ? 32768 : 16384)
1107 #define PRES_FILE_FFLAG (unsigned long) (V3 ? 16384 : 8192)
1108 #define READ_ONLY_FFLAG (unsigned long) (V3 ? 8192 : 4096)
1109 #define GROUP_FFLAG (unsigned long) (V3 ? 32 : 64)
1110 #define COMPR_FFLAG (unsigned long) (V3 ? 128 : 8)
1111 #define ENCR_FFLAG (unsigned long) (V3 ? 64 : 4)
1112 #define UNSYNC_FFLAG (unsigned long) (V3 ? 0 : 2)
1113 #define DATLEN_FFLAG (unsigned long) (V3 ? 0 : 1)
1114 if(head_part < 4 && promote_framename(fr, id) != 0) continue;
1115
1116 /* shall not or want not handle these */
1117 if(fflags & (BAD_FFLAGS | COMPR_FFLAG | ENCR_FFLAG))
1118 {
1119 if(NOQUIET) warning("ID3v2: skipping invalid/unsupported frame");
1120 continue;
1121 }
1122
1123 for(i = 0; i < KNOWN_FRAMES; ++i)
1124 if(!strncmp(frame_type[i], id, 4)){ tt = i; break; }
1125
1126 if(id[0] == 'T' && tt != extra) tt = text;
1127
1128 if(tt != unknown)
1129 {
1130 int rva_mode = -1; /* mix / album */
1131 unsigned long realsize = framesize;
1132 unsigned char* realdata = tagdata+pos;
1133 unsigned char* unsyncbuffer = NULL;
1134 if(((flags & UNSYNC_FLAG) || (fflags & UNSYNC_FFLAG)) && framesize > 0)
1135 {
1136 unsigned long ipos = 0;
1137 unsigned long opos = 0;
1138 debug("Id3v2: going to de-unsync the frame data");
1139 /* de-unsync: FF00 -> FF; real FF00 is simply represented as FF0000 ... */
1140 /* damn, that means I have to delete bytes from withing the data block... thus need temporal storage */
1141 /* standard mandates that de-unsync should always be safe if flag is set */
1142 realdata = unsyncbuffer = malloc(framesize+1); /* will need <= bytes, plus a safety zero */
1143 if(realdata == NULL)
1144 {
1145 if(NOQUIET) error("ID3v2: unable to allocate working buffer for de-unsync");
1146 continue;
1147 }
1148 /* now going byte per byte through the data... */
1149 realdata[0] = tagdata[pos];
1150 opos = 1;
1151 for(ipos = pos+1; ipos < pos+framesize; ++ipos)
1152 {
1153 if(!((tagdata[ipos] == 0) && (tagdata[ipos-1] == 0xff)))
1154 {
1155 realdata[opos++] = tagdata[ipos];
1156 }
1157 }
1158 realsize = opos;
1159 /* Append a zero to keep strlen() safe. */
1160 realdata[realsize] = 0;
1161 debug2("ID3v2: de-unsync made %lu out of %lu bytes", realsize, framesize);
1162 }
1163 // The spec says there is a group byte, without explicitly saying that it is
1164 // the first thing following the header. I just assume so, because of the
1165 // ordering of the flags.
1166 if(fflags & GROUP_FFLAG)
1167 { // Just skip group byte.
1168 if(realsize)
1169 {
1170 if(VERBOSE3)
1171 fprintf(stderr, "Note: frame of group %d\n", realdata[0]);
1172 --realsize;
1173 ++realdata;
1174 } else if(NOQUIET)
1175 error("Grouped frame without group byte, even.");
1176 }
1177 if(fflags & DATLEN_FFLAG)
1178 {
1179 // Spec says the original (without compression or unsync) data length follows,
1180 // so it should match de-unsynced data now.
1181 if(realsize >= 4)
1182 {
1183 unsigned long datlen;
1184 if(bytes_to_long(realdata, datlen) && datlen == realsize-4)
1185 {
1186 realsize -= 4;
1187 realdata += 4;
1188 } else
1189 {
1190 if(NOQUIET)
1191 error("frame data length bad, skipping");
1192 realsize = 0;
1193 }
1194 } else
1195 {
1196 realsize = 0;
1197 if(NOQUIET)
1198 error("frame truncated at frame data length, skipping");
1199 }
1200 }
1201 pos = 0; /* now at the beginning again... */
1202 /* Avoid reading over boundary, even if there is a */
1203 /* zero byte of padding for safety. */
1204 if(realsize) switch(tt)
1205 {
1206 case comment:
1207 case uslt:
1208 process_comment(fr, tt, realdata, realsize, comment+1, id);
1209 break;
1210 case extra: /* perhaps foobar2000's work */
1211 process_extra(fr, realdata, realsize, extra+1, id);
1212 break;
1213 case rva2: /* "the" RVA tag */
1214 {
1215 /* starts with null-terminated identification */
1216 if(VERBOSE3) fprintf(stderr, "Note: RVA2 identification \"%s\"\n", realdata);
1217 /* default: some individual value, mix mode */
1218 rva_mode = 0;
1219 if( !strncasecmp((char*)realdata, "album", 5)
1220 || !strncasecmp((char*)realdata, "audiophile", 10)
1221 || !strncasecmp((char*)realdata, "user", 4))
1222 rva_mode = 1;
1223 if(fr->rva.level[rva_mode] <= rva2+1)
1224 {
1225 pos += strlen((char*) realdata) + 1;
1226 debug2("got my pos: %lu - %lu", realsize, pos);
1227 // channel and two bytes for RVA value
1228 // pos possibly just past the safety zero, so one more than realsize
1229 if(pos > realsize || realsize-pos < 3)
1230 {
1231 if(NOQUIET)
1232 error("bad RVA2 tag (truncated?)");
1233 }
1234 else if(realdata[pos] == 1)
1235 {
1236 ++pos;
1237 /* only handle master channel */
1238 debug("ID3v2: it is for the master channel");
1239 /* two bytes adjustment, one byte for bits representing peak - n bytes, eh bits, for peak */
1240 /* 16 bit signed integer = dB * 512. Do not shift signed integers! Multiply instead.
1241 Also no implementation-defined casting. Reinterpret the pointer to signed char, then do
1242 proper casting. */
1243 fr->rva.gain[rva_mode] = (float) (
1244 ((short)((signed char*)realdata)[pos]) * 256 + (short)realdata[pos+1] ) / 512;
1245 pos += 2;
1246 if(VERBOSE3) fprintf(stderr, "Note: RVA value %fdB\n", fr->rva.gain[rva_mode]);
1247 /* heh, the peak value is represented by a number of bits - but in what manner? Skipping that part */
1248 fr->rva.peak[rva_mode] = 0;
1249 fr->rva.level[rva_mode] = rva2+1;
1250 }
1251 }
1252 }
1253 break;
1254 /* non-rva metainfo, simply store... */
1255 case text:
1256 process_text(fr, realdata, realsize, id);
1257 break;
1258 case picture:
1259 if (fr->p.flags & MPG123_PICTURE)
1260 process_picture(fr, realdata, realsize);
1261
1262 break;
1263 default: if(NOQUIET) error1("ID3v2: unknown frame type %i", tt);
1264 }
1265 if(unsyncbuffer)
1266 free(unsyncbuffer);
1267 }
1268 #undef V3
1269 #undef BAD_FFLAGS
1270 #undef PRES_TAG_FFLAG
1271 #undef PRES_FILE_FFLAG
1272 #undef READ_ONLY_FFLAG
1273 #undef GROUP_FFLAG
1274 #undef COMPR_FFLAG
1275 #undef ENCR_FFLAG
1276 #undef UNSYNC_FFLAG
1277 #undef DATLEN_FFLAG
1278 }
1279 else break;
1280 #undef KNOWN_FRAMES
1281 }
1282 } else
1283 {
1284 if(VERBOSE3)
1285 fprintf(stderr, "Note: faulty ID3v2 tag still clearing old data\n");
1286 INT123_reset_id3(fr);
1287 }
1288 } else // No new data, but still there was a tag that invalidates old data.
1289 {
1290 if(VERBOSE3)
1291 fprintf(stderr, "Note: empty ID3v2 clearing old data\n");
1292 INT123_reset_id3(fr);
1293 }
1294 tagparse_cleanup:
1295 /* Get rid of stored raw data that should not be kept. */
1296 if(!(fr->p.flags & MPG123_STORE_RAW_ID3))
1297 {
1298 free(fr->id3v2_raw);
1299 fr->id3v2_raw = NULL;
1300 fr->id3v2_size = 0;
1301 }
1302 }
1303 #endif /* NO_ID3V2 */
1304 return ret;
1305 #undef UNSYNC_FLAG
1306 #undef EXTHEAD_FLAG
1307 #undef COMPRESS_FLAG
1308 #undef EXP_FLAG
1309 #undef FOOTER_FLAG
1310 #undef EXT_UPDATE_FLAG
1311 #undef UNKOWN_FLAGS
1312 }
1313
1314 #ifndef NO_ID3V2 /* Disabling all the rest... */
1315
1316 static void convert_latin1(mpg123_string *sb, const unsigned char* s, size_t l, const int noquiet)
1317 {
1318 size_t length = l;
1319 size_t i;
1320 unsigned char *p;
1321 /* determine real length, a latin1 character can at most take 2 in UTF8 */
1322 for(i=0; i<l; ++i)
1323 if(s[i] >= 0x80) ++length;
1324
1325 debug1("UTF-8 length: %lu", (unsigned long)length);
1326 /* one extra zero byte for paranoia */
1327 if(!mpg123_grow_string(sb, length+1))
1328 return;
1329
1330 p = (unsigned char*) sb->p; /* Signedness doesn't matter but it shows I thought about the non-issue */
1331 for(i=0; i<l; ++i)
1332 if(s[i] < 0x80){ *p = s[i]; ++p; }
1333 else /* two-byte encoding */
1334 {
1335 *p = 0xc0 | (s[i]>>6);
1336 *(p+1) = 0x80 | (s[i] & 0x3f);
1337 p+=2;
1338 }
1339
1340 sb->p[length] = 0;
1341 sb->fill = length+1;
1342 }
1343
1344 /*
1345 Check if we have a byte oder mark(s) there, return:
1346 -1: little endian
1347 0: no BOM
1348 1: big endian
1349
1350 This modifies source and len to indicate the data _after_ the BOM(s).
1351 Note on nasty data: The last encountered BOM determines the endianness.
1352 I have seen data with multiple BOMS, namely from "the" id3v2 program.
1353 Not nice, but what should I do?
1354 */
1355 static int check_bom(const unsigned char** source, size_t *len)
1356 {
1357 int last_bom = 0;
1358
1359 while(*len >= 2)
1360 {
1361 int this_bom = 0;
1362 if((*source)[0] == 0xff && (*source)[1] == 0xfe)
1363 this_bom = -1;
1364 if((*source)[0] == 0xfe && (*source)[1] == 0xff)
1365 this_bom = 1;
1366 if(this_bom == 0)
1367 break;
1368 /* Skip the detected BOM. */
1369 last_bom = this_bom;
1370 *source += 2;
1371 *len -= 2;
1372 }
1373 return last_bom;
1374 }
1375
1376 #define FULLPOINT(f,s) ( (((f)&0x3ff)<<10) + ((s)&0x3ff) + 0x10000 )
1377 /* Remember: There's a limit at 0x1ffff. */
1378 #define UTF8LEN(x) ( (x)<0x80 ? 1 : ((x)<0x800 ? 2 : ((x)<0x10000 ? 3 : 4)))
1379 static void convert_utf16bom(mpg123_string *sb, const unsigned char* s, size_t l, const int noquiet)
1380 {
1381 size_t i;
1382 size_t n; /* number bytes that make up full pairs */
1383 unsigned char *p;
1384 size_t length = 0; /* the resulting UTF-8 length */
1385 /* Determine real length... extreme case can be more than utf-16 length. */
1386 size_t high = 0;
1387 size_t low = 1;
1388 int bom_endian;
1389
1390 debug1("convert_utf16 with length %lu", (unsigned long)l);
1391
1392 bom_endian = check_bom(&s, &l);
1393 debug1("UTF16 endianness check: %i", bom_endian);
1394
1395 if(bom_endian == -1) /* little-endian */
1396 {
1397 high = 1; /* The second byte is the high byte. */
1398 low = 0; /* The first byte is the low byte. */
1399 }
1400
1401 n = (l/2)*2; /* number bytes that make up full pairs */
1402
1403 /* first: get length, check for errors -- stop at first one */
1404 for(i=0; i < n; i+=2)
1405 {
1406 unsigned long point = ((unsigned long) s[i+high]<<8) + s[i+low];
1407 if((point & 0xfc00) == 0xd800) /* lead surrogate */
1408 {
1409 unsigned short second = (i+3 < l) ? (s[i+2+high]<<8) + s[i+2+low] : 0;
1410 if((second & 0xfc00) == 0xdc00) /* good... */
1411 {
1412 point = FULLPOINT(point,second);
1413 length += UTF8LEN(point); /* possibly 4 bytes */
1414 i+=2; /* We overstepped one word. */
1415 }
1416 else /* if no valid pair, break here */
1417 {
1418 if(noquiet) error2("Invalid UTF16 surrogate pair at %li (0x%04lx).", (unsigned long)i, point);
1419 n = i; /* Forget the half pair, END! */
1420 break;
1421 }
1422 }
1423 else length += UTF8LEN(point); /* 1,2 or 3 bytes */
1424 }
1425
1426 if(!mpg123_grow_string(sb, length+1))
1427 return;
1428
1429 /* Now really convert, skip checks as these have been done just before. */
1430 p = (unsigned char*) sb->p; /* Signedness doesn't matter but it shows I thought about the non-issue */
1431 for(i=0; i < n; i+=2)
1432 {
1433 unsigned long codepoint = ((unsigned long) s[i+high]<<8) + s[i+low];
1434 if((codepoint & 0xfc00) == 0xd800) /* lead surrogate */
1435 {
1436 unsigned short second = (s[i+2+high]<<8) + s[i+2+low];
1437 codepoint = FULLPOINT(codepoint,second);
1438 i+=2; /* We overstepped one word. */
1439 }
1440 if(codepoint < 0x80) *p++ = (unsigned char) codepoint;
1441 else if(codepoint < 0x800)
1442 {
1443 *p++ = (unsigned char) (0xc0 | (codepoint>>6));
1444 *p++ = (unsigned char) (0x80 | (codepoint & 0x3f));
1445 }
1446 else if(codepoint < 0x10000)
1447 {
1448 *p++ = (unsigned char) (0xe0 | (codepoint>>12));
1449 *p++ = 0x80 | ((codepoint>>6) & 0x3f);
1450 *p++ = 0x80 | (codepoint & 0x3f);
1451 }
1452 else if (codepoint < 0x200000)
1453 {
1454 *p++ = (unsigned char) (0xf0 | codepoint>>18);
1455 *p++ = (unsigned char) (0x80 | ((codepoint>>12) & 0x3f));
1456 *p++ = (unsigned char) (0x80 | ((codepoint>>6) & 0x3f));
1457 *p++ = (unsigned char) (0x80 | (codepoint & 0x3f));
1458 } /* ignore bigger ones (that are not possible here anyway) */
1459 }
1460 sb->p[sb->size-1] = 0; /* paranoia... */
1461 sb->fill = sb->size;
1462 }
1463 #undef UTF8LEN
1464 #undef FULLPOINT
1465
1466 static void convert_utf8(mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet)
1467 {
1468 if(mpg123_grow_string(sb, len+1))
1469 {
1470 memcpy(sb->p, source, len);
1471 sb->p[len] = 0;
1472 sb->fill = len+1;
1473 }
1474 }
1475
1476 #endif