summaryrefslogtreecommitdiff
path: root/lib/rbcodec
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2022-03-13 14:31:02 -0400
committerWilliam Wilgus <wilgus.william@gmail.com>2022-03-13 14:31:02 -0400
commit2352cef6d0757a4d31a18561a09a10f031388e12 (patch)
tree81d555acb1f7382d7c8c7c4180cfb9aa5b028d8b /lib/rbcodec
parenta62d36d9e7fd89adfd55ae2428fa9df4243bc435 (diff)
downloadrockbox-2352cef6d0757a4d31a18561a09a10f031388e12.tar.gz
rockbox-2352cef6d0757a4d31a18561a09a10f031388e12.zip
replace more strcmp if then trees with string_option()
1 Change-Id: Ic89bbb2ab41068d09c7bd9caa5ba7f38749b9084
Diffstat (limited to 'lib/rbcodec')
-rw-r--r--lib/rbcodec/metadata/asap.c19
-rw-r--r--lib/rbcodec/metadata/asf.c54
-rw-r--r--lib/rbcodec/metadata/id3tags.c14
-rw-r--r--lib/rbcodec/metadata/metadata_common.c30
4 files changed, 66 insertions, 51 deletions
diff --git a/lib/rbcodec/metadata/asap.c b/lib/rbcodec/metadata/asap.c
index 47eb2a3d50..db23dd69fa 100644
--- a/lib/rbcodec/metadata/asap.c
+++ b/lib/rbcodec/metadata/asap.c
@@ -185,38 +185,41 @@ static bool parse_sap_header(int fd, struct mp3entry* id3, int file_len)
185 break; 185 break;
186 } 186 }
187 } 187 }
188 188 static const char *tg_options[] = {"SAP", "AUTHOR", "NAME", "DATE",
189 "SONGS", "DEFSONG", "TIME", NULL};
189 /* parse tags */ 190 /* parse tags */
190 if(strcmp(line, "SAP") == 0) 191 int tg_op = string_option(line, tg_options, false);
192 if (tg_op == 0) /*SAP*/
191 sap_signature = 1; 193 sap_signature = 1;
192 if (sap_signature == -1) 194 if (sap_signature == -1)
193 return false; 195 return false;
194 if (strcmp(line, "AUTHOR") == 0) 196
197 if (tg_op == 1) /*AUTHOR*/
195 { 198 {
196 if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false) 199 if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false)
197 return false; 200 return false;
198 } 201 }
199 else if(strcmp(line, "NAME") == 0) 202 else if(tg_op == 2) /*NAME*/
200 { 203 {
201 if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false) 204 if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false)
202 return false; 205 return false;
203 } 206 }
204 else if(strcmp(line, "DATE") == 0) 207 else if(tg_op == 3) /*DATE*/
205 { 208 {
206 if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false) 209 if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false)
207 return false; 210 return false;
208 } 211 }
209 else if (strcmp(line, "SONGS") == 0) 212 else if (tg_op == 4) /*SONGS*/
210 { 213 {
211 if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false ) 214 if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false )
212 return false; 215 return false;
213 } 216 }
214 else if (strcmp(line, "DEFSONG") == 0) 217 else if (tg_op == 5) /*DEFSONG*/
215 { 218 {
216 if (parse_dec(&defSong, p, 0, MAX_SONGS) == false) 219 if (parse_dec(&defSong, p, 0, MAX_SONGS) == false)
217 return false; 220 return false;
218 } 221 }
219 else if (strcmp(line, "TIME") == 0) 222 else if (tg_op == 6) /*TIME*/
220 { 223 {
221 int durationTemp = ASAP_ParseDuration(p); 224 int durationTemp = ASAP_ParseDuration(p);
222 if (durationTemp < 0 || duration_index >= MAX_SONGS) 225 if (durationTemp < 0 || duration_index >= MAX_SONGS)
diff --git a/lib/rbcodec/metadata/asf.c b/lib/rbcodec/metadata/asf.c
index d90487b36b..82873a43a6 100644
--- a/lib/rbcodec/metadata/asf.c
+++ b/lib/rbcodec/metadata/asf.c
@@ -437,6 +437,18 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
437 read_uint16le(fd, &count); 437 read_uint16le(fd, &count);
438 bytesleft -= 2; 438 bytesleft -= 2;
439 //DEBUGF("extended metadata count = %u\n",count); 439 //DEBUGF("extended metadata count = %u\n",count);
440 enum
441 {
442 eWM_TrackNumber, eWM_Genre, eWM_AlbumTitle,
443 eWM_AlbumArtist, eWM_Composer, eWM_Year,
444 eWM_MusicBrainz_Track_Id, eWM_Picture
445 };
446
447 static const char *tagops[] =
448 { "WM/TrackNumber", "WM/Genre", "WM/AlbumTitle",
449 "WM/AlbumArtist", "WM/Composer", "WM/Year",
450 "MusicBrainz/Track Id", "WM/Picture", NULL
451 };
440 452
441 for (i=0; i < count; i++) { 453 for (i=0; i < count; i++) {
442 uint16_t length, type; 454 uint16_t length, type;
@@ -450,7 +462,9 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
450 read_uint16le(fd, &type); 462 read_uint16le(fd, &type);
451 read_uint16le(fd, &length); 463 read_uint16le(fd, &length);
452 464
453 if (!strcmp("WM/TrackNumber",utf8buf)) { 465 int itag = string_option(utf8buf, tagops, false);
466
467 if (itag == eWM_TrackNumber) {
454 if (type == 0) { 468 if (type == 0) {
455 id3->track_string = id3buf; 469 id3->track_string = id3buf;
456 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 470 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
@@ -460,19 +474,19 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
460 } else { 474 } else {
461 lseek(fd, length, SEEK_CUR); 475 lseek(fd, length, SEEK_CUR);
462 } 476 }
463 } else if ((!strcmp("WM/Genre", utf8buf)) && (type == 0)) { 477 } else if ((itag == eWM_Genre) && (type == 0)) {
464 id3->genre_string = id3buf; 478 id3->genre_string = id3buf;
465 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 479 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
466 } else if ((!strcmp("WM/AlbumTitle", utf8buf)) && (type == 0)) { 480 } else if ((itag == eWM_AlbumTitle) && (type == 0)) {
467 id3->album = id3buf; 481 id3->album = id3buf;
468 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 482 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
469 } else if ((!strcmp("WM/AlbumArtist", utf8buf)) && (type == 0)) { 483 } else if ((itag == eWM_AlbumArtist) && (type == 0)) {
470 id3->albumartist = id3buf; 484 id3->albumartist = id3buf;
471 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 485 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
472 } else if ((!strcmp("WM/Composer", utf8buf)) && (type == 0)) { 486 } else if ((itag == eWM_Composer) && (type == 0)) {
473 id3->composer = id3buf; 487 id3->composer = id3buf;
474 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 488 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
475 } else if (!strcmp("WM/Year", utf8buf)) { 489 } else if (itag == eWM_Year) {
476 if (type == 0) { 490 if (type == 0) {
477 id3->year_string = id3buf; 491 id3->year_string = id3buf;
478 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 492 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
@@ -482,15 +496,11 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
482 } else { 496 } else {
483 lseek(fd, length, SEEK_CUR); 497 lseek(fd, length, SEEK_CUR);
484 } 498 }
485 } else if (!strncmp("replaygain_", utf8buf, 11)) { 499 } else if (itag == eWM_MusicBrainz_Track_Id) {
486 char *value = id3buf;
487 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
488 parse_replaygain(utf8buf, value, id3);
489 } else if (!strcmp("MusicBrainz/Track Id", utf8buf)) {
490 id3->mb_track_id = id3buf; 500 id3->mb_track_id = id3buf;
491 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining); 501 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
492#ifdef HAVE_ALBUMART 502#ifdef HAVE_ALBUMART
493 } else if (!strcmp("WM/Picture", utf8buf)) { 503 } else if (itag == eWM_Picture) {
494 uint32_t datalength = 0; 504 uint32_t datalength = 0;
495 uint32_t strlength; 505 uint32_t strlength;
496 /* Expected is either "01 00 xx xx 03 yy yy yy yy" or 506 /* Expected is either "01 00 xx xx 03 yy yy yy yy" or
@@ -521,13 +531,23 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
521 asf_utf16LEdecode(fd, 32, &utf8, &utf8length); 531 asf_utf16LEdecode(fd, 32, &utf8, &utf8length);
522 strlength = (strlen(utf8buf) + 2) * 2; 532 strlength = (strlen(utf8buf) + 2) * 2;
523 lseek(fd, strlength-32, SEEK_CUR); 533 lseek(fd, strlength-32, SEEK_CUR);
524 if (!strcmp("image/jpeg", utf8buf)) { 534
535 static const char *aa_options[] = {"image/jpeg",
536 "image/jpg","image/png", NULL};
537 int aa_op = string_option(utf8buf, aa_options, false);
538
539 if (aa_op == 0) /*image/jpeg*/
540 {
525 id3->albumart.type = AA_TYPE_JPG; 541 id3->albumart.type = AA_TYPE_JPG;
526 } else if (!strcmp("image/jpg", utf8buf)) { 542 }
543 else if (aa_op == 1) /*image/jpg*/
544 {
527 /* image/jpg is technically invalid, 545 /* image/jpg is technically invalid,
528 * but it does occur in the wild */ 546 * but it does occur in the wild */
529 id3->albumart.type = AA_TYPE_JPG; 547 id3->albumart.type = AA_TYPE_JPG;
530 } else if (!strcmp("image/png", utf8buf)) { 548 }
549 else if (aa_op == 2) /*image/png*/
550 {
531 id3->albumart.type = AA_TYPE_PNG; 551 id3->albumart.type = AA_TYPE_PNG;
532 } else { 552 } else {
533 id3->albumart.type = AA_TYPE_UNKNOWN; 553 id3->albumart.type = AA_TYPE_UNKNOWN;
@@ -543,6 +563,10 @@ static int asf_parse_header(int fd, struct mp3entry* id3,
543 563
544 lseek(fd, datalength, SEEK_CUR); 564 lseek(fd, datalength, SEEK_CUR);
545#endif 565#endif
566 } else if (!strncmp("replaygain_", utf8buf, 11)) {
567 char *value = id3buf;
568 asf_utf16LEdecode(fd, length, &id3buf, &id3buf_remaining);
569 parse_replaygain(utf8buf, value, id3);
546 } else { 570 } else {
547 lseek(fd, length, SEEK_CUR); 571 lseek(fd, length, SEEK_CUR);
548 } 572 }
diff --git a/lib/rbcodec/metadata/id3tags.c b/lib/rbcodec/metadata/id3tags.c
index 458e24cf61..18258c73ac 100644
--- a/lib/rbcodec/metadata/id3tags.c
+++ b/lib/rbcodec/metadata/id3tags.c
@@ -308,24 +308,26 @@ static int parsealbumart( struct mp3entry* entry, char* tag, int bufferpos )
308 char *start = tag; 308 char *start = tag;
309 /* skip text encoding */ 309 /* skip text encoding */
310 tag += 1; 310 tag += 1;
311 static const char *img_options[] = {"jpeg", "jpg", "png", NULL};
311 312
312 if (memcmp(tag, "image/", 6) == 0) 313 if (memcmp(tag, "image/", 6) == 0)
313 { 314 {
315 int tg_op = string_option(tag, img_options, false);
314 /* ID3 v2.3+ */ 316 /* ID3 v2.3+ */
315 tag += 6; 317 tag += 6;
316 if (strcmp(tag, "jpeg") == 0) 318 if (tg_op == 0) /*jpeg*/
317 { 319 {
318 entry->albumart.type = AA_TYPE_JPG; 320 entry->albumart.type = AA_TYPE_JPG;
319 tag += 5; 321 tag += 5;
320 } 322 }
321 else if (strcmp(tag, "jpg") == 0) 323 else if (tg_op == 1) /*jpg*/
322 { 324 {
323 /* image/jpg is technically invalid, but it does occur in 325 /* image/jpg is technically invalid, but it does occur in
324 * the wild */ 326 * the wild */
325 entry->albumart.type = AA_TYPE_JPG; 327 entry->albumart.type = AA_TYPE_JPG;
326 tag += 4; 328 tag += 4;
327 } 329 }
328 else if (strcmp(tag, "png") == 0) 330 else if (tg_op == 2) /*png*/
329 { 331 {
330 entry->albumart.type = AA_TYPE_PNG; 332 entry->albumart.type = AA_TYPE_PNG;
331 tag += 4; 333 tag += 4;
@@ -434,9 +436,11 @@ static int parserva2( struct mp3entry* entry, char* tag, int bufferpos)
434 } 436 }
435 } 437 }
436 438
437 if (strcasecmp(tag, "album") == 0) { 439 static const char *tg_options[] = {"album", "track", NULL};
440 int tg_op = string_option(tag, tg_options, true);
441 if (tg_op == 0) { /*album*/
438 album = true; 442 album = true;
439 } else if (strcasecmp(tag, "track") != 0) { 443 } else if (tg_op != 1) { /*!track*/
440 /* Only accept non-track values if we don't have any previous 444 /* Only accept non-track values if we don't have any previous
441 * value. 445 * value.
442 */ 446 */
diff --git a/lib/rbcodec/metadata/metadata_common.c b/lib/rbcodec/metadata/metadata_common.c
index 202da49522..59c2f01840 100644
--- a/lib/rbcodec/metadata/metadata_common.c
+++ b/lib/rbcodec/metadata/metadata_common.c
@@ -260,32 +260,16 @@ bool skip_id3v2(int fd, struct mp3entry *id3)
260 */ 260 */
261int string_option(const char *option, const char *const oplist[], bool ignore_case) 261int string_option(const char *option, const char *const oplist[], bool ignore_case)
262{ 262{
263 int i;
264 int ifound = -1;
265 const char *op; 263 const char *op;
266 if (ignore_case) 264 int (*cmp_fn)(const char*, const char*) = &strcasecmp;
265 if (!ignore_case)
266 cmp_fn = strcmp;
267 for (int i=0; (op=oplist[i]) != NULL; i++)
267 { 268 {
268 for (i=0; (op=oplist[i]) != NULL; i++) 269 if (cmp_fn(op, option) == 0)
269 { 270 return i;
270 if (strcasecmp(op, option) == 0)
271 {
272 ifound = i;
273 break;
274 }
275 }
276 }
277 else
278 {
279 for (i=0; (op=oplist[i]) != NULL; i++)
280 {
281 if (strcmp(op, option) == 0)
282 {
283 ifound = i;
284 break;
285 }
286 }
287 } 271 }
288 return ifound; 272 return -1;
289} 273}
290#endif 274#endif
291/* Parse the tag (the name-value pair) and fill id3 and buffer accordingly. 275/* Parse the tag (the name-value pair) and fill id3 and buffer accordingly.