diff options
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/id3.c | 75 |
1 files changed, 41 insertions, 34 deletions
diff --git a/firmware/id3.c b/firmware/id3.c index b509d7905b..b510195cb1 100644 --- a/firmware/id3.c +++ b/firmware/id3.c | |||
@@ -28,6 +28,10 @@ | |||
28 | #include <string.h> | 28 | #include <string.h> |
29 | #include <errno.h> | 29 | #include <errno.h> |
30 | 30 | ||
31 | #ifdef SIMULATOR | ||
32 | #include <fcntl.h> | ||
33 | #endif | ||
34 | |||
31 | struct mp3entry { | 35 | struct mp3entry { |
32 | char *path; | 36 | char *path; |
33 | char *title; | 37 | char *title; |
@@ -126,20 +130,19 @@ stripspaces(char *buffer) | |||
126 | * Returns: TRUE if a title was found and created, else FALSE | 130 | * Returns: TRUE if a title was found and created, else FALSE |
127 | */ | 131 | */ |
128 | static bool | 132 | static bool |
129 | setid3v1title(FILE *file, mp3entry *entry) | 133 | setid3v1title(int fd, mp3entry *entry) |
130 | { | 134 | { |
131 | char buffer[31]; | 135 | char buffer[31]; |
132 | int offsets[3] = {-95,-65,-125}; | 136 | int offsets[3] = {-95,-65,-125}; |
133 | int i; | 137 | int i; |
134 | |||
135 | static char keepit[3][32]; | 138 | static char keepit[3][32]; |
136 | 139 | ||
137 | for(i=0;i<3;i++) { | 140 | for(i=0;i<3;i++) { |
138 | if(fseek(file, offsets[i], SEEK_END) != 0) | 141 | if(-1 == lseek(fd, offsets[i], SEEK_END)) |
139 | return FALSE; | 142 | return FALSE; |
140 | 143 | ||
141 | buffer[0]=0; | 144 | buffer[0]=0; |
142 | fgets(buffer, 31, file); | 145 | read(fd, buffer, 31); |
143 | stripspaces(buffer); | 146 | stripspaces(buffer); |
144 | 147 | ||
145 | if(buffer[0]) { | 148 | if(buffer[0]) { |
@@ -173,10 +176,10 @@ setid3v1title(FILE *file, mp3entry *entry) | |||
173 | * Returns: TRUE if a title was found and created, else FALSE | 176 | * Returns: TRUE if a title was found and created, else FALSE |
174 | */ | 177 | */ |
175 | static void | 178 | static void |
176 | setid3v2title(FILE *file, mp3entry *entry) | 179 | setid3v2title(int fd, mp3entry *entry) |
177 | { | 180 | { |
178 | unsigned int minframesize; | 181 | unsigned int minframesize; |
179 | unsigned int size; | 182 | int size; |
180 | unsigned int readsize = 0, headerlen; | 183 | unsigned int readsize = 0, headerlen; |
181 | char *title = NULL; | 184 | char *title = NULL; |
182 | char *artist = NULL; | 185 | char *artist = NULL; |
@@ -191,17 +194,17 @@ setid3v2title(FILE *file, mp3entry *entry) | |||
191 | return; | 194 | return; |
192 | 195 | ||
193 | /* Check version */ | 196 | /* Check version */ |
194 | fseek(file, 0, SEEK_SET); | 197 | lseek(fd, 0, SEEK_SET); |
195 | fread(header, sizeof(char), 10, file); | 198 | read(fd, header, 10); |
196 | version = (unsigned short int)header[3]; | 199 | version = (unsigned short int)header[3]; |
197 | 200 | ||
198 | /* Read all frames in the tag */ | 201 | /* Read all frames in the tag */ |
199 | size = entry->id3v2len - 10; | 202 | size = entry->id3v2len - 10; |
200 | 203 | ||
201 | if(size >= sizeof(buffer)) | 204 | if(size >= (int)sizeof(buffer)) |
202 | size = sizeof(buffer)-1; | 205 | size = sizeof(buffer)-1; |
203 | 206 | ||
204 | if(size != fread(buffer, sizeof(char), size, file)) { | 207 | if(size != read(fd, buffer, size)) { |
205 | free(buffer); | 208 | free(buffer); |
206 | return; | 209 | return; |
207 | } | 210 | } |
@@ -291,18 +294,18 @@ setid3v2title(FILE *file, mp3entry *entry) | |||
291 | * Returns: the size of the tag or 0 if none was found | 294 | * Returns: the size of the tag or 0 if none was found |
292 | */ | 295 | */ |
293 | static int | 296 | static int |
294 | getid3v2len(FILE *file) | 297 | getid3v2len(int fd) |
295 | { | 298 | { |
296 | char buf[6]; | 299 | char buf[6]; |
297 | int offset; | 300 | int offset; |
298 | 301 | ||
299 | /* Make sure file has a ID3 tag */ | 302 | /* Make sure file has a ID3 tag */ |
300 | if((fseek(file, 0, SEEK_SET) != 0) || | 303 | if((-1 == lseek(fd, 0, SEEK_SET)) || |
301 | (fread(buf, sizeof(char), 6, file) != 6) || | 304 | (read(fd, buf, 6) != 6) || |
302 | (strncmp(buf, "ID3", strlen("ID3")) != 0)) | 305 | (strncmp(buf, "ID3", strlen("ID3")) != 0)) |
303 | offset = 0; | 306 | offset = 0; |
304 | /* Now check what the ID3v2 size field says */ | 307 | /* Now check what the ID3v2 size field says */ |
305 | else if(fread(buf, sizeof(char), 4, file) != 4) | 308 | else if(read(fd, buf, 4) != 4) |
306 | offset = 0; | 309 | offset = 0; |
307 | else | 310 | else |
308 | offset = UNSYNC(buf[0], buf[1], buf[2], buf[3]) + 10; | 311 | offset = UNSYNC(buf[0], buf[1], buf[2], buf[3]) + 10; |
@@ -311,13 +314,16 @@ getid3v2len(FILE *file) | |||
311 | } | 314 | } |
312 | 315 | ||
313 | static int | 316 | static int |
314 | getfilesize(FILE *file) | 317 | getfilesize(int fd) |
315 | { | 318 | { |
319 | int size; | ||
320 | |||
316 | /* seek to the end of it */ | 321 | /* seek to the end of it */ |
317 | if(fseek(file, 0, SEEK_END)) | 322 | size = lseek(fd, 0, SEEK_END); |
323 | if(-1 == size) | ||
318 | return 0; /* unknown */ | 324 | return 0; /* unknown */ |
319 | 325 | ||
320 | return ftell(file); | 326 | return size; |
321 | } | 327 | } |
322 | 328 | ||
323 | /* | 329 | /* |
@@ -328,14 +334,14 @@ getfilesize(FILE *file) | |||
328 | * Returns: the size of the tag or 0 if none was found | 334 | * Returns: the size of the tag or 0 if none was found |
329 | */ | 335 | */ |
330 | static int | 336 | static int |
331 | getid3v1len(FILE *file) | 337 | getid3v1len(int fd) |
332 | { | 338 | { |
333 | char buf[3]; | 339 | char buf[3]; |
334 | int offset; | 340 | int offset; |
335 | 341 | ||
336 | /* Check if we find "TAG" 128 bytes from EOF */ | 342 | /* Check if we find "TAG" 128 bytes from EOF */ |
337 | if((fseek(file, -128, SEEK_END) != 0) || | 343 | if((lseek(fd, -128, SEEK_END) != 0) || |
338 | (fread(buf, sizeof(char), 3, file) != 3) || | 344 | (read(fd, buf, 3) != 3) || |
339 | (strncmp(buf, "TAG", 3) != 0)) | 345 | (strncmp(buf, "TAG", 3) != 0)) |
340 | offset = 0; | 346 | offset = 0; |
341 | else | 347 | else |
@@ -359,7 +365,7 @@ getid3v1len(FILE *file) | |||
359 | * -1 means that it couldn't be calculated | 365 | * -1 means that it couldn't be calculated |
360 | */ | 366 | */ |
361 | static int | 367 | static int |
362 | getsonglength(FILE *file, mp3entry *entry) | 368 | getsonglength(int fd, mp3entry *entry) |
363 | { | 369 | { |
364 | long header; | 370 | long header; |
365 | int version; | 371 | int version; |
@@ -373,13 +379,13 @@ getsonglength(FILE *file, mp3entry *entry) | |||
373 | long tpf; | 379 | long tpf; |
374 | 380 | ||
375 | /* Start searching after ID3v2 header */ | 381 | /* Start searching after ID3v2 header */ |
376 | if(fseek(file, entry->id3v2len, SEEK_SET)) | 382 | if(-1 == lseek(fd, entry->id3v2len, SEEK_SET)) |
377 | return -1; | 383 | return -1; |
378 | 384 | ||
379 | /* Fill up header with first 24 bits */ | 385 | /* Fill up header with first 24 bits */ |
380 | for(version = 0; version < 3; version++) { | 386 | for(version = 0; version < 3; version++) { |
381 | header <<= 8; | 387 | header <<= 8; |
382 | if(!fread(&header, 1, 1, file)) | 388 | if(!read(fd, &header, 1)) |
383 | return -1; | 389 | return -1; |
384 | } | 390 | } |
385 | 391 | ||
@@ -387,7 +393,7 @@ getsonglength(FILE *file, mp3entry *entry) | |||
387 | restart: | 393 | restart: |
388 | do { | 394 | do { |
389 | header <<= 8; | 395 | header <<= 8; |
390 | if(!fread(&header, 1, 1, file)) | 396 | if(!read(fd, &header, 1)) |
391 | return -1; | 397 | return -1; |
392 | } while(!CHECKSYNC(header)); | 398 | } while(!CHECKSYNC(header)); |
393 | 399 | ||
@@ -491,27 +497,28 @@ getsonglength(FILE *file, mp3entry *entry) | |||
491 | bool | 497 | bool |
492 | mp3info(mp3entry *entry, char *filename) | 498 | mp3info(mp3entry *entry, char *filename) |
493 | { | 499 | { |
494 | FILE *file; | 500 | int fd; |
495 | if((file = fopen(filename, "r")) == NULL) | 501 | fd = open(filename, O_RDONLY); |
502 | if(-1 == fd) | ||
496 | return TRUE; | 503 | return TRUE; |
497 | 504 | ||
498 | memset(entry, 0, sizeof(mp3entry)); | 505 | memset(entry, 0, sizeof(mp3entry)); |
499 | 506 | ||
500 | entry->path = filename; | 507 | entry->path = filename; |
501 | 508 | ||
502 | entry->filesize = getfilesize(file); | 509 | entry->filesize = getfilesize(fd); |
503 | entry->id3v2len = getid3v2len(file); | 510 | entry->id3v2len = getid3v2len(fd); |
504 | entry->id3v1len = getid3v1len(file); | 511 | entry->id3v1len = getid3v1len(fd); |
505 | entry->length = getsonglength(file, entry); | 512 | entry->length = getsonglength(fd, entry); |
506 | entry->title = NULL; | 513 | entry->title = NULL; |
507 | 514 | ||
508 | if(HASID3V2(entry)) | 515 | if(HASID3V2(entry)) |
509 | setid3v2title(file, entry); | 516 | setid3v2title(fd, entry); |
510 | 517 | ||
511 | if(HASID3V1(entry) && !entry->title) | 518 | if(HASID3V1(entry) && !entry->title) |
512 | setid3v1title(file, entry); | 519 | setid3v1title(fd, entry); |
513 | 520 | ||
514 | fclose(file); | 521 | close(fd); |
515 | 522 | ||
516 | return FALSE; | 523 | return FALSE; |
517 | } | 524 | } |