Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenid3.c
Go to the documentation of this file.
00001 /* 00002 id3: ID3v2.3 and ID3v2.4 parsing (a relevant subset) 00003 00004 copyright 2006-2008 by the mpg123 project - free software under the terms of the LGPL 2.1 00005 see COPYING and AUTHORS files in distribution or http://mpg123.org 00006 initially written by Thomas Orgis 00007 */ 00008 00009 #include "mpg123lib_intern.h" 00010 #include "id3.h" 00011 #include "debug.h" 00012 00013 #ifndef NO_ID3V2 /* Only the main parsing routine will always be there. */ 00014 00015 /* We know the usual text frames plus some specifics. */ 00016 #define KNOWN_FRAMES 4 00017 static const char frame_type[KNOWN_FRAMES][5] = { "COMM", "TXXX", "RVA2", "USLT" }; 00018 enum frame_types { unknown = -2, text = -1, comment, extra, rva2, uslt }; 00019 00020 /* UTF support definitions */ 00021 00022 typedef void (*text_converter)(mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet); 00023 00024 static void convert_latin1 (mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet); 00025 static void convert_utf16bom(mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet); 00026 static void convert_utf8 (mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet); 00027 00028 static const text_converter text_converters[4] = 00029 { 00030 convert_latin1, 00031 /* We always check for (multiple) BOM in 16bit unicode. Without BOM, UTF16 BE is the default. 00032 Errors in encoding are detected anyway. */ 00033 convert_utf16bom, 00034 convert_utf16bom, 00035 convert_utf8 00036 }; 00037 00038 const unsigned int encoding_widths[4] = { 1, 2, 2, 1 }; 00039 00040 /* the code starts here... */ 00041 00042 static void null_id3_links(mpg123_handle *fr) 00043 { 00044 fr->id3v2.title = NULL; 00045 fr->id3v2.artist = NULL; 00046 fr->id3v2.album = NULL; 00047 fr->id3v2.year = NULL; 00048 fr->id3v2.genre = NULL; 00049 fr->id3v2.comment = NULL; 00050 } 00051 00052 void init_id3(mpg123_handle *fr) 00053 { 00054 fr->id3v2.version = 0; /* nothing there */ 00055 null_id3_links(fr); 00056 fr->id3v2.comments = 0; 00057 fr->id3v2.comment_list = NULL; 00058 fr->id3v2.texts = 0; 00059 fr->id3v2.text = NULL; 00060 fr->id3v2.extras = 0; 00061 fr->id3v2.extra = NULL; 00062 } 00063 00064 /* Managing of the text, comment and extra lists. */ 00065 00066 /* Initialize one element. */ 00067 static void init_mpg123_text(mpg123_text *txt) 00068 { 00069 mpg123_init_string(&txt->text); 00070 mpg123_init_string(&txt->description); 00071 txt->id[0] = 0; 00072 txt->id[1] = 0; 00073 txt->id[2] = 0; 00074 txt->id[3] = 0; 00075 txt->lang[0] = 0; 00076 txt->lang[1] = 0; 00077 txt->lang[2] = 0; 00078 } 00079 00080 /* Free memory of one element. */ 00081 static void free_mpg123_text(mpg123_text *txt) 00082 { 00083 mpg123_free_string(&txt->text); 00084 mpg123_free_string(&txt->description); 00085 } 00086 00087 /* Free memory of whole list. */ 00088 #define free_comment(mh) free_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments)) 00089 #define free_text(mh) free_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts)) 00090 #define free_extra(mh) free_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras)) 00091 static void free_id3_text(mpg123_text **list, size_t *size) 00092 { 00093 size_t i; 00094 for(i=0; i<*size; ++i) free_mpg123_text(&((*list)[i])); 00095 00096 free(*list); 00097 *list = NULL; 00098 *size = 0; 00099 } 00100 00101 /* Add items to the list. */ 00102 #define add_comment(mh) add_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments)) 00103 #define add_text(mh) add_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts)) 00104 #define add_extra(mh) add_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras)) 00105 static mpg123_text *add_id3_text(mpg123_text **list, size_t *size) 00106 { 00107 mpg123_text *x = safe_realloc(*list, sizeof(mpg123_text)*(*size+1)); 00108 if(x == NULL) return NULL; /* bad */ 00109 00110 *list = x; 00111 *size += 1; 00112 init_mpg123_text(&((*list)[*size-1])); 00113 00114 return &((*list)[*size-1]); /* Return pointer to the added text. */ 00115 } 00116 00117 /* Remove the last item. */ 00118 #define pop_comment(mh) pop_id3_text(&((mh)->id3v2.comment_list), &((mh)->id3v2.comments)) 00119 #define pop_text(mh) pop_id3_text(&((mh)->id3v2.text), &((mh)->id3v2.texts)) 00120 #define pop_extra(mh) pop_id3_text(&((mh)->id3v2.extra), &((mh)->id3v2.extras)) 00121 static void pop_id3_text(mpg123_text **list, size_t *size) 00122 { 00123 mpg123_text *x; 00124 if(*size < 1) return; 00125 00126 free_mpg123_text(&((*list)[*size-1])); 00127 if(*size > 1) 00128 { 00129 x = safe_realloc(*list, sizeof(mpg123_text)*(*size-1)); 00130 if(x != NULL){ *list = x; *size -= 1; } 00131 } 00132 else 00133 { 00134 free(*list); 00135 *list = NULL; 00136 *size = 0; 00137 } 00138 } 00139 00140 /* OK, back t the higher level functions. */ 00141 00142 void exit_id3(mpg123_handle *fr) 00143 { 00144 free_comment(fr); 00145 free_extra(fr); 00146 free_text(fr); 00147 } 00148 00149 void reset_id3(mpg123_handle *fr) 00150 { 00151 exit_id3(fr); 00152 init_id3(fr); 00153 } 00154 00155 /* Set the id3v2.artist id3v2.title ... links to elements of the array. */ 00156 void id3_link(mpg123_handle *fr) 00157 { 00158 size_t i; 00159 mpg123_id3v2 *v2 = &fr->id3v2; 00160 debug("linking ID3v2"); 00161 null_id3_links(fr); 00162 for(i=0; i<v2->texts; ++i) 00163 { 00164 mpg123_text *entry = &v2->text[i]; 00165 if (!strncmp("TIT2", entry->id, 4)) v2->title = &entry->text; 00166 else if(!strncmp("TALB", entry->id, 4)) v2->album = &entry->text; 00167 else if(!strncmp("TPE1", entry->id, 4)) v2->artist = &entry->text; 00168 else if(!strncmp("TYER", entry->id, 4)) v2->year = &entry->text; 00169 else if(!strncmp("TCON", entry->id, 4)) v2->genre = &entry->text; 00170 } 00171 for(i=0; i<v2->comments; ++i) 00172 { 00173 mpg123_text *entry = &v2->comment_list[i]; 00174 if(entry->description.fill == 0 || entry->description.p[0] == 0) 00175 v2->comment = &entry->text; 00176 } 00177 /* When no generic comment found, use the last non-generic one. */ 00178 if(v2->comment == NULL && v2->comments > 0) 00179 v2->comment = &v2->comment_list[v2->comments-1].text; 00180 } 00181 00182 /* 00183 Store ID3 text data in an mpg123_string; either verbatim copy or everything translated to UTF-8 encoding. 00184 Preserve the zero string separator (I don't need strlen for the total size). 00185 00186 ID3v2 standard says that there should be one text frame of specific type per tag, and subsequent tags overwrite old values. 00187 So, I always replace the text that may be stored already (perhaps with a list of zero-separated strings, though). 00188 */ 00189 void store_id3_text(mpg123_string *sb, char *source, size_t source_size, const int noquiet, const int notranslate) 00190 { 00191 if(!source_size) 00192 { 00193 debug("Empty id3 data!"); 00194 return; 00195 } 00196 00197 /* We shall just copy the data. Client wants to decode itself. */ 00198 if(notranslate) 00199 { 00200 /* Future: Add a path for ID3 errors. */ 00201 if(!mpg123_resize_string(sb, source_size)) 00202 { 00203 if(noquiet) error("Cannot resize target string, out of memory?"); 00204 return; 00205 } 00206 memcpy(sb->p, source, source_size); 00207 sb->fill = source_size; 00208 debug1("stored undecoded ID3 text of size %"SIZE_P, (size_p)source_size); 00209 return; 00210 } 00211 00212 id3_to_utf8(sb, ((unsigned char *)source)[0], (unsigned char*)source+1, source_size-1, noquiet); 00213 00214 if(sb->fill) debug1("UTF-8 string (the first one): %s", sb->p); 00215 else if(noquiet) error("unable to convert string to UTF-8 (out of memory, junk input?)!"); 00216 } 00217 00218 /* On error, sb->size is 0. */ 00219 void id3_to_utf8(mpg123_string *sb, unsigned char encoding, const unsigned char *source, size_t source_size, int noquiet) 00220 { 00221 unsigned int bwidth; 00222 debug1("encoding: %u", encoding); 00223 /* A note: ID3v2.3 uses UCS-2 non-variable 16bit encoding, v2.4 uses UTF16. 00224 UTF-16 uses a reserved/private range in UCS-2 to add the magic, so we just always treat it as UTF. */ 00225 if(encoding > mpg123_id3_enc_max) 00226 { 00227 if(noquiet) error1("Unknown text encoding %u, I take no chances, sorry!", encoding); 00228 00229 mpg123_free_string(sb); 00230 return; 00231 } 00232 bwidth = encoding_widths[encoding]; 00233 /* Hack! I've seen a stray zero byte before BOM. Is that supposed to happen? */ 00234 if(encoding != mpg123_id3_utf16be) /* UTF16be _can_ beging with a null byte! */ 00235 while(source_size > bwidth && source[0] == 0) 00236 { 00237 --source_size; 00238 ++source; 00239 debug("skipped leading zero"); 00240 } 00241 if(source_size % bwidth) 00242 { 00243 /* When we need two bytes for a character, it's strange to have an uneven bytestream length. */ 00244 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); 00245 source_size -= source_size % bwidth; 00246 } 00247 text_converters[encoding](sb, source, source_size, noquiet); 00248 } 00249 00250 char *next_text(char* prev, int encoding, size_t limit) 00251 { 00252 char *text = prev; 00253 size_t width = encoding_widths[encoding]; 00254 00255 /* So I go lengths to find zero or double zero... 00256 Remember bug 2834636: Only check for aligned NULLs! */ 00257 while(text-prev < (long)limit) 00258 { 00259 if(text[0] == 0) 00260 { 00261 if(width <= limit-(text-prev)) 00262 { 00263 size_t i = 1; 00264 for(; i<width; ++i) if(text[i] != 0) break; 00265 00266 if(i == width) /* found a null wide enough! */ 00267 { 00268 text += width; 00269 break; 00270 } 00271 } 00272 else return NULL; /* No full character left? This text is broken */ 00273 } 00274 00275 text += width; 00276 } 00277 if((size_t)(text-prev) >= limit) text = NULL; 00278 00279 return text; 00280 } 00281 00282 static const char *enc_name(int enc) 00283 { 00284 switch(enc) 00285 { 00286 case 0: return "Latin 1"; 00287 case 1: return "UTF-16 BOM"; 00288 case 2: return "UTF-16 BE"; 00289 case 3: return "UTF-8"; 00290 default: return "unknown!"; 00291 } 00292 } 00293 00294 static void process_text(mpg123_handle *fr, char *realdata, size_t realsize, char *id) 00295 { 00296 /* Text encoding $xx */ 00297 /* The text (encoded) ... */ 00298 mpg123_text *t = add_text(fr); 00299 if(VERBOSE4) fprintf(stderr, "Note: Storing text from %s encoding\n", enc_name(realdata[0])); 00300 if(t == NULL) 00301 { 00302 if(NOQUIET) error("Unable to attach new text!"); 00303 return; 00304 } 00305 memcpy(t->id, id, 4); 00306 store_id3_text(&t->text, realdata, realsize, NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT); 00307 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); 00308 } 00309 00310 /* Store a new comment that perhaps is a RVA / RVA_ALBUM/AUDIOPHILE / RVA_MIX/RADIO one 00311 Special gimmik: It also stores USLT to the texts. Stucture is the same as for comments. */ 00312 static void process_comment(mpg123_handle *fr, enum frame_types tt, char *realdata, size_t realsize, int rva_level, char *id) 00313 { 00314 /* Text encoding $xx */ 00315 /* Language $xx xx xx */ 00316 /* Short description (encoded!) <text> $00 (00) */ 00317 /* Then the comment text (encoded) ... */ 00318 char encoding = realdata[0]; 00319 char *lang = realdata+1; /* I'll only use the 3 bytes! */ 00320 char *descr = realdata+4; 00321 char *text = NULL; 00322 mpg123_text *xcom = NULL; 00323 mpg123_text localcom; /* UTF-8 variant for local processing. */ 00324 00325 if((int)realsize < descr-realdata) 00326 { 00327 if(NOQUIET) error1("Invalid frame size of %lu (too small for anything).", (unsigned long)realsize); 00328 return; 00329 } 00330 xcom = (tt == uslt ? add_text(fr) : add_comment(fr)); 00331 if(VERBOSE4) fprintf(stderr, "Note: Storing comment from %s encoding\n", enc_name(realdata[0])); 00332 if(xcom == NULL) 00333 { 00334 if(NOQUIET) error("Unable to attach new comment!"); 00335 return; 00336 } 00337 memcpy(xcom->lang, lang, 3); 00338 memcpy(xcom->id, id, 4); 00339 /* Now I can abuse a byte from lang for the encoding. */ 00340 descr[-1] = encoding; 00341 /* Be careful with finding the end of description, I have to honor encoding here. */ 00342 text = next_text(descr, encoding, realsize-(descr-realdata)); 00343 if(text == NULL) 00344 { 00345 if(NOQUIET) error("No comment text / valid description?"); 00346 pop_comment(fr); 00347 return; 00348 } 00349 00350 init_mpg123_text(&localcom); 00351 /* Store the text, without translation to UTF-8, but for comments always a local copy in UTF-8. 00352 Reminder: No bailing out from here on without freeing the local comment data! */ 00353 store_id3_text(&xcom->description, descr-1, text-descr+1, NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT); 00354 if(tt == comment) 00355 store_id3_text(&localcom.description, descr-1, text-descr+1, NOQUIET, 0); 00356 00357 text[-1] = encoding; /* Byte abusal for encoding... */ 00358 store_id3_text(&xcom->text, text-1, realsize+1-(text-realdata), NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT); 00359 /* Remember: I will probably decode the above (again) for rva comment checking. So no messing around, please. */ 00360 00361 if(VERBOSE4) /* Do _not_ print the verbatim text: The encoding might be funny! */ 00362 { 00363 fprintf(stderr, "Note: ID3 comm/uslt desc of length %"SIZE_P".\n", (size_p)xcom->description.fill); 00364 fprintf(stderr, "Note: ID3 comm/uslt text of length %"SIZE_P".\n", (size_p)xcom->text.fill); 00365 } 00366 /* Look out for RVA info only when we really deal with a straight comment. */ 00367 if(tt == comment && localcom.description.fill > 0) 00368 { 00369 int rva_mode = -1; /* mix / album */ 00370 if( !strcasecmp(localcom.description.p, "rva") 00371 || !strcasecmp(localcom.description.p, "rva_mix") 00372 || !strcasecmp(localcom.description.p, "rva_track") 00373 || !strcasecmp(localcom.description.p, "rva_radio") ) 00374 rva_mode = 0; 00375 else if( !strcasecmp(localcom.description.p, "rva_album") 00376 || !strcasecmp(localcom.description.p, "rva_audiophile") 00377 || !strcasecmp(localcom.description.p, "rva_user") ) 00378 rva_mode = 1; 00379 if((rva_mode > -1) && (fr->rva.level[rva_mode] <= rva_level)) 00380 { 00381 /* Only translate the contents in here where we really need them. */ 00382 store_id3_text(&localcom.text, text-1, realsize+1-(text-realdata), NOQUIET, 0); 00383 if(localcom.text.fill > 0) 00384 { 00385 fr->rva.gain[rva_mode] = (float) atof(localcom.text.p); 00386 if(VERBOSE3) fprintf(stderr, "Note: RVA value %fdB\n", fr->rva.gain[rva_mode]); 00387 fr->rva.peak[rva_mode] = 0; 00388 fr->rva.level[rva_mode] = rva_level; 00389 } 00390 } 00391 } 00392 /* Make sure to free the local memory... */ 00393 free_mpg123_text(&localcom); 00394 } 00395 00396 void process_extra(mpg123_handle *fr, char* realdata, size_t realsize, int rva_level, char *id) 00397 { 00398 /* Text encoding $xx */ 00399 /* Description ... $00 (00) */ 00400 /* Text ... */ 00401 char encoding = realdata[0]; 00402 char *descr = realdata+1; /* remember, the encoding is descr[-1] */ 00403 char *text; 00404 mpg123_text *xex; 00405 mpg123_text localex; 00406 00407 if((int)realsize < descr-realdata) 00408 { 00409 if(NOQUIET) error1("Invalid frame size of %lu (too small for anything).", (unsigned long)realsize); 00410 return; 00411 } 00412 text = next_text(descr, encoding, realsize-(descr-realdata)); 00413 if(VERBOSE4) fprintf(stderr, "Note: Storing extra from %s encoding\n", enc_name(realdata[0])); 00414 if(text == NULL) 00415 { 00416 if(NOQUIET) error("No extra frame text / valid description?"); 00417 return; 00418 } 00419 xex = add_extra(fr); 00420 if(xex == NULL) 00421 { 00422 if(NOQUIET) error("Unable to attach new extra text!"); 00423 return; 00424 } 00425 memcpy(xex->id, id, 4); 00426 init_mpg123_text(&localex); /* For our local copy. */ 00427 00428 /* The outside storage gets reencoded to UTF-8 only if not requested otherwise. 00429 Remember that we really need the -1 here to hand in the encoding byte!*/ 00430 store_id3_text(&xex->description, descr-1, text-descr+1, NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT); 00431 /* Our local copy is always stored in UTF-8! */ 00432 store_id3_text(&localex.description, descr-1, text-descr+1, NOQUIET, 0); 00433 /* At first, only store the outside copy of the payload. We may not need the local copy. */ 00434 text[-1] = encoding; 00435 store_id3_text(&xex->text, text-1, realsize-(text-realdata)+1, NOQUIET, fr->p.flags & MPG123_PLAIN_ID3TEXT); 00436 00437 /* Now check if we would like to interpret this extra info for RVA. */ 00438 if(localex.description.fill > 0) 00439 { 00440 int is_peak = 0; 00441 int rva_mode = -1; /* mix / album */ 00442 00443 if(!strncasecmp(localex.description.p, "replaygain_track_",17)) 00444 { 00445 if(VERBOSE3) fprintf(stderr, "Note: RVA ReplayGain track gain/peak\n"); 00446 00447 rva_mode = 0; 00448 if(!strcasecmp(localex.description.p, "replaygain_track_peak")) is_peak = 1; 00449 else if(strcasecmp(localex.description.p, "replaygain_track_gain")) rva_mode = -1; 00450 } 00451 else 00452 if(!strncasecmp(localex.description.p, "replaygain_album_",17)) 00453 { 00454 if(VERBOSE3) fprintf(stderr, "Note: RVA ReplayGain album gain/peak\n"); 00455 00456 rva_mode = 1; 00457 if(!strcasecmp(localex.description.p, "replaygain_album_peak")) is_peak = 1; 00458 else if(strcasecmp(localex.description.p, "replaygain_album_gain")) rva_mode = -1; 00459 } 00460 if((rva_mode > -1) && (fr->rva.level[rva_mode] <= rva_level)) 00461 { 00462 /* Now we need the translated copy of the data. */ 00463 store_id3_text(&localex.text, text-1, realsize-(text-realdata)+1, NOQUIET, 0); 00464 if(localex.text.fill > 0) 00465 { 00466 if(is_peak) 00467 { 00468 fr->rva.peak[rva_mode] = (float) atof(localex.text.p); 00469 if(VERBOSE3) fprintf(stderr, "Note: RVA peak %f\n", fr->rva.peak[rva_mode]); 00470 } 00471 else 00472 { 00473 fr->rva.gain[rva_mode] = (float) atof(localex.text.p); 00474 if(VERBOSE3) fprintf(stderr, "Note: RVA gain %fdB\n", fr->rva.gain[rva_mode]); 00475 } 00476 fr->rva.level[rva_mode] = rva_level; 00477 } 00478 } 00479 } 00480 00481 free_mpg123_text(&localex); 00482 } 00483 00484 /* Make a ID3v2.3+ 4-byte ID from a ID3v2.2 3-byte ID 00485 Note that not all frames survived to 2.4; the mapping goes to 2.3 . 00486 A notable miss is the old RVA frame, which is very unspecific anyway. 00487 This function returns -1 when a not known 3 char ID was encountered, 0 otherwise. */ 00488 int promote_framename(mpg123_handle *fr, char *id) /* fr because of VERBOSE macros */ 00489 { 00490 size_t i; 00491 char *old[] = 00492 { 00493 "COM", "TAL", "TBP", "TCM", "TCO", "TCR", "TDA", "TDY", "TEN", "TFT", 00494 "TIM", "TKE", "TLA", "TLE", "TMT", "TOA", "TOF", "TOL", "TOR", "TOT", 00495 "TP1", "TP2", "TP3", "TP4", "TPA", "TPB", "TRC", "TDA", "TRK", "TSI", 00496 "TSS", "TT1", "TT2", "TT3", "TXT", "TXX", "TYE" 00497 }; 00498 char *new[] = 00499 { 00500 "COMM", "TALB", "TBPM", "TCOM", "TCON", "TCOP", "TDAT", "TDLY", "TENC", "TFLT", 00501 "TIME", "TKEY", "TLAN", "TLEN", "TMED", "TOPE", "TOFN", "TOLY", "TORY", "TOAL", 00502 "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPUB", "TSRC", "TRDA", "TRCK", "TSIZ", 00503 "TSSE", "TIT1", "TIT2", "TIT3", "TEXT", "TXXX", "TYER" 00504 }; 00505 for(i=0; i<sizeof(old)/sizeof(char*); ++i) 00506 { 00507 if(!strncmp(id, old[i], 3)) 00508 { 00509 memcpy(id, new[i], 4); 00510 if(VERBOSE3) fprintf(stderr, "Translated ID3v2.2 frame %s to %s\n", old[i], new[i]); 00511 return 0; 00512 } 00513 } 00514 if(VERBOSE3) fprintf(stderr, "Ignoring untranslated ID3v2.2 frame %c%c%c\n", id[0], id[1], id[2]); 00515 return -1; 00516 } 00517 00518 #endif /* NO_ID3V2 */ 00519 00520 /* 00521 trying to parse ID3v2.3 and ID3v2.4 tags... 00522 00523 returns: 0: bad or just unparseable tag 00524 1: good, (possibly) new tag info 00525 <0: reader error (may need more data feed, try again) 00526 */ 00527 int parse_new_id3(mpg123_handle *fr, unsigned long first4bytes) 00528 { 00529 #define UNSYNC_FLAG 128 00530 #define EXTHEAD_FLAG 64 00531 #define EXP_FLAG 32 00532 #define FOOTER_FLAG 16 00533 #define UNKNOWN_FLAGS 15 /* 00001111*/ 00534 unsigned char buf[6]; 00535 unsigned long length=0; 00536 unsigned char flags = 0; 00537 int ret = 1; 00538 int ret2; 00539 unsigned char* tagdata = NULL; 00540 unsigned char major = first4bytes & 0xff; 00541 debug1("ID3v2: major tag version: %i", major); 00542 if(major == 0xff) return 0; /* Invalid... */ 00543 if((ret2 = fr->rd->read_frame_body(fr, buf, 6)) < 0) /* read more header information */ 00544 return ret2; 00545 00546 if(buf[0] == 0xff) return 0; /* Revision, will never be 0xff. */ 00547 00548 /* second new byte are some nice flags, if these are invalid skip the whole thing */ 00549 flags = buf[1]; 00550 debug1("ID3v2: flags 0x%08x", flags); 00551 /* use 4 bytes from buf to construct 28bit uint value and return 1; return 0 if bytes are not synchsafe */ 00552 #define synchsafe_to_long(buf,res) \ 00553 ( \ 00554 (((buf)[0]|(buf)[1]|(buf)[2]|(buf)[3]) & 0x80) ? 0 : \ 00555 (res = (((unsigned long) (buf)[0]) << 21) \ 00556 | (((unsigned long) (buf)[1]) << 14) \ 00557 | (((unsigned long) (buf)[2]) << 7) \ 00558 | ((unsigned long) (buf)[3]) \ 00559 ,1) \ 00560 ) 00561 /* id3v2.3 does not store synchsafe frame sizes, but synchsafe tag size - doh! */ 00562 #define bytes_to_long(buf,res) \ 00563 ( \ 00564 major == 3 ? \ 00565 (res = (((unsigned long) (buf)[0]) << 24) \ 00566 | (((unsigned long) (buf)[1]) << 16) \ 00567 | (((unsigned long) (buf)[2]) << 8) \ 00568 | ((unsigned long) (buf)[3]) \ 00569 ,1) : synchsafe_to_long(buf,res) \ 00570 ) 00571 /* for id3v2.2 only */ 00572 #define threebytes_to_long(buf,res) \ 00573 ( \ 00574 res = (((unsigned long) (buf)[0]) << 16) \ 00575 | (((unsigned long) (buf)[1]) << 8) \ 00576 | ((unsigned long) (buf)[2]) \ 00577 ,1 \ 00578 ) 00579 00580 /* length-10 or length-20 (footer present); 4 synchsafe integers == 28 bit number */ 00581 /* we have already read 10 bytes, so left are length or length+10 bytes belonging to tag */ 00582 if(!synchsafe_to_long(buf+2,length)) 00583 { 00584 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]); 00585 return 0; 00586 } 00587 debug1("ID3v2: tag data length %lu", length); 00588 #ifndef NO_ID3V2 00589 if(VERBOSE2) fprintf(stderr,"Note: ID3v2.%i rev %i tag of %lu bytes\n", major, buf[0], length); 00590 /* skip if unknown version/scary flags, parse otherwise */ 00591 if((flags & UNKNOWN_FLAGS) || (major > 4) || (major < 2)) 00592 { 00593 /* going to skip because there are unknown flags set */ 00594 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); 00595 #endif 00596 if((ret2 = fr->rd->skip_bytes(fr,length)) < 0) /* will not store data in backbuff! */ 00597 ret = ret2; 00598 #ifndef NO_ID3V2 00599 } 00600 else 00601 { 00602 fr->id3v2.version = major; 00603 /* try to interpret that beast */ 00604 if((tagdata = (unsigned char*) malloc(length+1)) != NULL) 00605 { 00606 debug("ID3v2: analysing frames..."); 00607 if((ret2 = fr->rd->read_frame_body(fr,tagdata,length)) > 0) 00608 { 00609 unsigned long tagpos = 0; 00610 debug1("ID3v2: have read at all %lu bytes for the tag now", (unsigned long)length+6); 00611 /* going to apply strlen for strings inside frames, make sure that it doesn't overflow! */ 00612 tagdata[length] = 0; 00613 if(flags & EXTHEAD_FLAG) 00614 { 00615 debug("ID3v2: skipping extended header"); 00616 if(!bytes_to_long(tagdata, tagpos)) 00617 { 00618 ret = 0; 00619 if(NOQUIET) error4("Bad (non-synchsafe) tag offset: 0x%02x%02x%02x%02x", tagdata[0], tagdata[1], tagdata[2], tagdata[3]); 00620 } 00621 } 00622 if(ret > 0) 00623 { 00624 char id[5]; 00625 unsigned long framesize; 00626 unsigned long fflags; /* need 16 bits, actually */ 00627 id[4] = 0; 00628 /* pos now advanced after ext head, now a frame has to follow */ 00629 while(tagpos < length-10) /* I want to read at least a full header */ 00630 { 00631 int i = 0; 00632 unsigned long pos = tagpos; 00633 int head_part = fr->id3v2.version == 2 ? 3 : 4; /* bytes of frame title and of framesize value */ 00634 /* level 1,2,3 - 0 is info from lame/info tag! */ 00635 /* rva tags with ascending significance, then general frames */ 00636 enum frame_types tt = unknown; 00637 /* we may have entered the padding zone or any other strangeness: check if we have valid frame id characters */ 00638 for(i=0; i< head_part; ++i) 00639 if( !( ((tagdata[tagpos+i] > 47) && (tagdata[tagpos+i] < 58)) 00640 || ((tagdata[tagpos+i] > 64) && (tagdata[tagpos+i] < 91)) ) ) 00641 { 00642 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]); 00643 /* This is no hard error... let's just hope that we got something meaningful already (ret==1 in that case). */ 00644 goto tagparse_cleanup; /* Need to escape two loops here. */ 00645 } 00646 if(ret > 0) 00647 { 00648 /* 4 or 3 bytes id */ 00649 strncpy(id, (char*) tagdata+pos, head_part); 00650 pos += head_part; 00651 tagpos += head_part; 00652 /* size as 32 bits or 28 bits */ 00653 if(fr->id3v2.version == 2) threebytes_to_long(tagdata+pos, framesize); 00654 else 00655 if(!bytes_to_long(tagdata+pos, framesize)) 00656 { 00657 /* Just assume that up to now there was some good data. */ 00658 if(NOQUIET) error1("ID3v2: non-syncsafe size of %s frame, skipping the remainder of tag", id); 00659 break; 00660 } 00661 if(VERBOSE3) fprintf(stderr, "Note: ID3v2 %s frame of size %lu\n", id, framesize); 00662 tagpos += head_part + framesize; /* the important advancement in whole tag */ 00663 if(tagpos > length) 00664 { 00665 if(NOQUIET) error("Whoa! ID3v2 frame claims to be larger than the whole rest of the tag."); 00666 break; 00667 } 00668 pos += head_part; 00669 if(fr->id3v2.version > 2) 00670 { 00671 fflags = (((unsigned long) tagdata[pos]) << 8) | ((unsigned long) tagdata[pos+1]); 00672 pos += 2; 00673 tagpos += 2; 00674 } 00675 else fflags = 0; 00676 /* for sanity, after full parsing tagpos should be == pos */ 00677 /* debug4("ID3v2: found %s frame, size %lu (as bytes: 0x%08lx), flags 0x%016lx", id, framesize, framesize, fflags); */ 00678 /* %0abc0000 %0h00kmnp */ 00679 #define BAD_FFLAGS (unsigned long) 36784 00680 #define PRES_TAG_FFLAG 16384 00681 #define PRES_FILE_FFLAG 8192 00682 #define READ_ONLY_FFLAG 4096 00683 #define GROUP_FFLAG 64 00684 #define COMPR_FFLAG 8 00685 #define ENCR_FFLAG 4 00686 #define UNSYNC_FFLAG 2 00687 #define DATLEN_FFLAG 1 00688 if(head_part < 4 && promote_framename(fr, id) != 0) continue; 00689 00690 /* shall not or want not handle these */ 00691 if(fflags & (BAD_FFLAGS | COMPR_FFLAG | ENCR_FFLAG)) 00692 { 00693 if(NOQUIET) warning("ID3v2: skipping invalid/unsupported frame"); 00694 continue; 00695 } 00696 00697 for(i = 0; i < KNOWN_FRAMES; ++i) 00698 if(!strncmp(frame_type[i], id, 4)){ tt = i; break; } 00699 00700 if(id[0] == 'T' && tt != extra) tt = text; 00701 00702 if(tt != unknown) 00703 { 00704 int rva_mode = -1; /* mix / album */ 00705 unsigned long realsize = framesize; 00706 unsigned char* realdata = tagdata+pos; 00707 if((flags & UNSYNC_FLAG) || (fflags & UNSYNC_FFLAG)) 00708 { 00709 unsigned long ipos = 0; 00710 unsigned long opos = 0; 00711 debug("Id3v2: going to de-unsync the frame data"); 00712 /* de-unsync: FF00 -> FF; real FF00 is simply represented as FF0000 ... */ 00713 /* damn, that means I have to delete bytes from withing the data block... thus need temporal storage */ 00714 /* standard mandates that de-unsync should always be safe if flag is set */ 00715 realdata = (unsigned char*) malloc(framesize); /* will need <= bytes */ 00716 if(realdata == NULL) 00717 { 00718 if(NOQUIET) error("ID3v2: unable to allocate working buffer for de-unsync"); 00719 continue; 00720 } 00721 /* now going byte per byte through the data... */ 00722 realdata[0] = tagdata[pos]; 00723 opos = 1; 00724 for(ipos = pos+1; ipos < pos+framesize; ++ipos) 00725 { 00726 if(!((tagdata[ipos] == 0) && (tagdata[ipos-1] == 0xff))) 00727 { 00728 realdata[opos++] = tagdata[ipos]; 00729 } 00730 } 00731 realsize = opos; 00732 debug2("ID3v2: de-unsync made %lu out of %lu bytes", realsize, framesize); 00733 } 00734 pos = 0; /* now at the beginning again... */ 00735 switch(tt) 00736 { 00737 case comment: 00738 case uslt: 00739 process_comment(fr, tt, (char*)realdata, realsize, comment+1, id); 00740 break; 00741 case extra: /* perhaps foobar2000's work */ 00742 process_extra(fr, (char*)realdata, realsize, extra+1, id); 00743 break; 00744 case rva2: /* "the" RVA tag */ 00745 { 00746 /* starts with null-terminated identification */ 00747 if(VERBOSE3) fprintf(stderr, "Note: RVA2 identification \"%s\"\n", realdata); 00748 /* default: some individual value, mix mode */ 00749 rva_mode = 0; 00750 if( !strncasecmp((char*)realdata, "album", 5) 00751 || !strncasecmp((char*)realdata, "audiophile", 10) 00752 || !strncasecmp((char*)realdata, "user", 4)) 00753 rva_mode = 1; 00754 if(fr->rva.level[rva_mode] <= rva2+1) 00755 { 00756 pos += strlen((char*) realdata) + 1; 00757 if(realdata[pos] == 1) 00758 { 00759 ++pos; 00760 /* only handle master channel */ 00761 debug("ID3v2: it is for the master channel"); 00762 /* two bytes adjustment, one byte for bits representing peak - n bytes, eh bits, for peak */ 00763 /* 16 bit signed integer = dB * 512 ... the double cast is needed to preserve the sign of negative values! */ 00764 fr->rva.gain[rva_mode] = (float) ( (((short)((signed char)realdata[pos])) << 8) | realdata[pos+1] ) / 512; 00765 pos += 2; 00766 if(VERBOSE3) fprintf(stderr, "Note: RVA value %fdB\n", fr->rva.gain[rva_mode]); 00767 /* heh, the peak value is represented by a number of bits - but in what manner? Skipping that part */ 00768 fr->rva.peak[rva_mode] = 0; 00769 fr->rva.level[rva_mode] = rva2+1; 00770 } 00771 } 00772 } 00773 break; 00774 /* non-rva metainfo, simply store... */ 00775 case text: 00776 process_text(fr, (char*)realdata, realsize, id); 00777 break; 00778 default: if(NOQUIET) error1("ID3v2: unknown frame type %i", tt); 00779 } 00780 if((flags & UNSYNC_FLAG) || (fflags & UNSYNC_FFLAG)) free(realdata); 00781 } 00782 #undef BAD_FFLAGS 00783 #undef PRES_TAG_FFLAG 00784 #undef PRES_FILE_FFLAG 00785 #undef READ_ONLY_FFLAG 00786 #undef GROUP_FFLAG 00787 #undef COMPR_FFLAG 00788 #undef ENCR_FFLAG 00789 #undef UNSYNC_FFLAG 00790 #undef DATLEN_FFLAG 00791 } 00792 else break; 00793 #undef KNOWN_FRAMES 00794 } 00795 } 00796 } 00797 else 00798 { 00799 /* There are tags with zero length. Strictly not an error, then. */ 00800 if(length > 0 && NOQUIET) error("ID3v2: Duh, not able to read ID3v2 tag data."); 00801 ret = ret2; 00802 } 00803 tagparse_cleanup: 00804 free(tagdata); 00805 } 00806 else 00807 { 00808 if(NOQUIET) error1("ID3v2: Arrg! Unable to allocate %lu bytes for interpreting ID3v2 data - trying to skip instead.", length); 00809 if((ret2 = fr->rd->skip_bytes(fr,length)) < 0) ret = ret2; /* will not store data in backbuff! */ 00810 else ret = 0; 00811 } 00812 } 00813 #endif /* NO_ID3V2 */ 00814 /* skip footer if present */ 00815 if((ret > 0) && (flags & FOOTER_FLAG) && ((ret2 = fr->rd->skip_bytes(fr,length)) < 0)) ret = ret2; 00816 00817 return ret; 00818 #undef UNSYNC_FLAG 00819 #undef EXTHEAD_FLAG 00820 #undef EXP_FLAG 00821 #undef FOOTER_FLAG 00822 #undef UNKOWN_FLAGS 00823 } 00824 00825 #ifndef NO_ID3V2 /* Disabling all the rest... */ 00826 00827 static void convert_latin1(mpg123_string *sb, const unsigned char* s, size_t l, const int noquiet) 00828 { 00829 size_t length = l; 00830 size_t i; 00831 unsigned char *p; 00832 /* determine real length, a latin1 character can at most take 2 in UTF8 */ 00833 for(i=0; i<l; ++i) 00834 if(s[i] >= 0x80) ++length; 00835 00836 debug1("UTF-8 length: %lu", (unsigned long)length); 00837 /* one extra zero byte for paranoia */ 00838 if(!mpg123_resize_string(sb, length+1)){ mpg123_free_string(sb); return ; } 00839 00840 p = (unsigned char*) sb->p; /* Signedness doesn't matter but it shows I thought about the non-issue */ 00841 for(i=0; i<l; ++i) 00842 if(s[i] < 0x80){ *p = s[i]; ++p; } 00843 else /* two-byte encoding */ 00844 { 00845 *p = 0xc0 | (s[i]>>6); 00846 *(p+1) = 0x80 | (s[i] & 0x3f); 00847 p+=2; 00848 } 00849 00850 sb->p[length] = 0; 00851 sb->fill = length+1; 00852 } 00853 00854 /* 00855 Check if we have a byte oder mark(s) there, return: 00856 -1: little endian 00857 0: no BOM 00858 1: big endian 00859 00860 This modifies source and len to indicate the data _after_ the BOM(s). 00861 Note on nasty data: The last encountered BOM determines the endianess. 00862 I have seen data with multiple BOMS, namely from "the" id3v2 program. 00863 Not nice, but what should I do? 00864 */ 00865 static int check_bom(const unsigned char** source, size_t *len) 00866 { 00867 int this_bom = 0; 00868 int further_bom = 0; 00869 00870 if(*len < 2) return 0; 00871 00872 if((*source)[0] == 0xff && (*source)[1] == 0xfe) 00873 this_bom = -1; 00874 00875 if((*source)[0] == 0xfe && (*source)[1] == 0xff) 00876 this_bom = 1; 00877 00878 /* Skip the detected BOM. */ 00879 if(this_bom != 0) 00880 { 00881 *source += 2; 00882 *len -= 2; 00883 /* Check for following BOMs. The last one wins! */ 00884 further_bom = check_bom(source, len); 00885 if(further_bom == 0) return this_bom; /* End of the recursion. */ 00886 else return further_bom; 00887 } 00888 else return 0; 00889 } 00890 00891 #define FULLPOINT(f,s) ( (((f)&0x3ff)<<10) + ((s)&0x3ff) + 0x10000 ) 00892 /* Remember: There's a limit at 0x1ffff. */ 00893 #define UTF8LEN(x) ( (x)<0x80 ? 1 : ((x)<0x800 ? 2 : ((x)<0x10000 ? 3 : 4))) 00894 static void convert_utf16bom(mpg123_string *sb, const unsigned char* s, size_t l, const int noquiet) 00895 { 00896 size_t i; 00897 size_t n; /* number bytes that make up full pairs */ 00898 unsigned char *p; 00899 size_t length = 0; /* the resulting UTF-8 length */ 00900 /* Determine real length... extreme case can be more than utf-16 length. */ 00901 size_t high = 0; 00902 size_t low = 1; 00903 int bom_endian; 00904 00905 debug1("convert_utf16 with length %lu", (unsigned long)l); 00906 00907 bom_endian = check_bom(&s, &l); 00908 debug1("UTF16 endianess check: %i", bom_endian); 00909 00910 if(bom_endian == -1) /* little-endian */ 00911 { 00912 high = 1; /* The second byte is the high byte. */ 00913 low = 0; /* The first byte is the low byte. */ 00914 } 00915 00916 n = (l/2)*2; /* number bytes that make up full pairs */ 00917 00918 /* first: get length, check for errors -- stop at first one */ 00919 for(i=0; i < n; i+=2) 00920 { 00921 unsigned long point = ((unsigned long) s[i+high]<<8) + s[i+low]; 00922 if((point & 0xd800) == 0xd800) /* lead surrogate */ 00923 { 00924 unsigned short second = (i+3 < l) ? (s[i+2+high]<<8) + s[i+2+low] : 0; 00925 if((second & 0xdc00) == 0xdc00) /* good... */ 00926 { 00927 point = FULLPOINT(point,second); 00928 length += UTF8LEN(point); /* possibly 4 bytes */ 00929 i+=2; /* We overstepped one word. */ 00930 } 00931 else /* if no valid pair, break here */ 00932 { 00933 if(noquiet) error2("Invalid UTF16 surrogate pair at %li (0x%04lx).", (unsigned long)i, point); 00934 n = i; /* Forget the half pair, END! */ 00935 break; 00936 } 00937 } 00938 else length += UTF8LEN(point); /* 1,2 or 3 bytes */ 00939 } 00940 00941 if(!mpg123_resize_string(sb, length+1)){ mpg123_free_string(sb); return ; } 00942 00943 /* Now really convert, skip checks as these have been done just before. */ 00944 p = (unsigned char*) sb->p; /* Signedness doesn't matter but it shows I thought about the non-issue */ 00945 for(i=0; i < n; i+=2) 00946 { 00947 unsigned long codepoint = ((unsigned long) s[i+high]<<8) + s[i+low]; 00948 if((codepoint & 0xd800) == 0xd800) /* lead surrogate */ 00949 { 00950 unsigned short second = (s[i+2+high]<<8) + s[i+2+low]; 00951 codepoint = FULLPOINT(codepoint,second); 00952 i+=2; /* We overstepped one word. */ 00953 } 00954 if(codepoint < 0x80) *p++ = (unsigned char) codepoint; 00955 else if(codepoint < 0x800) 00956 { 00957 *p++ = (unsigned char) (0xc0 | (codepoint>>6)); 00958 *p++ = (unsigned char) (0x80 | (codepoint & 0x3f)); 00959 } 00960 else if(codepoint < 0x10000) 00961 { 00962 *p++ = (unsigned char) (0xe0 | (codepoint>>12)); 00963 *p++ = 0x80 | ((codepoint>>6) & 0x3f); 00964 *p++ = 0x80 | (codepoint & 0x3f); 00965 } 00966 else if (codepoint < 0x200000) 00967 { 00968 *p++ = (unsigned char) (0xf0 | codepoint>>18); 00969 *p++ = (unsigned char) (0x80 | ((codepoint>>12) & 0x3f)); 00970 *p++ = (unsigned char) (0x80 | ((codepoint>>6) & 0x3f)); 00971 *p++ = (unsigned char) (0x80 | (codepoint & 0x3f)); 00972 } /* ignore bigger ones (that are not possible here anyway) */ 00973 } 00974 sb->p[sb->size-1] = 0; /* paranoia... */ 00975 sb->fill = sb->size; 00976 } 00977 #undef UTF8LEN 00978 #undef FULLPOINT 00979 00980 static void convert_utf8(mpg123_string *sb, const unsigned char* source, size_t len, const int noquiet) 00981 { 00982 if(mpg123_resize_string(sb, len+1)) 00983 { 00984 memcpy(sb->p, source, len); 00985 sb->p[len] = 0; 00986 sb->fill = len+1; 00987 } 00988 else mpg123_free_string(sb); 00989 } 00990 00991 #endif Generated on Fri May 25 2012 04:32:36 for ReactOS by
1.7.6.1
|