Moved atoi declaration to stdlib.h. Deleted atoi.h
[kugel-rb.git] / apps / metadata / spc.c
blob144ac97b7a5b7786cc775ce5c9aae30d84bd831b
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <inttypes.h>
25 #include "system.h"
26 #include "id3.h"
27 #include "metadata_common.h"
28 #include "debug.h"
29 #include "rbunicode.h"
31 bool get_spc_metadata(int fd, struct mp3entry* id3)
33 /* Use the trackname part of the id3 structure as a temporary buffer */
34 unsigned char * buf = (unsigned char *)id3->path;
35 int read_bytes;
36 char * p;
38 unsigned long length;
39 unsigned long fade;
40 bool isbinary = true;
41 int i;
43 /* try to get the ID666 tag */
44 if ((lseek(fd, 0x2e, SEEK_SET) < 0)
45 || ((read_bytes = read(fd, buf, 0xD2)) < 0xD2))
47 DEBUGF("lseek or read failed\n");
48 return false;
51 p = id3->id3v2buf;
53 id3->title = p;
54 buf[31] = 0;
55 p = iso_decode(buf, p, 0, 32);
56 buf += 32;
58 id3->album = p;
59 buf[31] = 0;
60 p = iso_decode(buf, p, 0, 32);
61 buf += 48;
63 id3->comment = p;
64 buf[31] = 0;
65 p = iso_decode(buf, p, 0, 32);
66 buf += 32;
68 /* Date check */
69 if(buf[2] == '/' && buf[5] == '/')
70 isbinary = false;
72 /* Reserved bytes check */
73 if(buf[0xD2 - 0x2E - 112] >= '0' &&
74 buf[0xD2 - 0x2E - 112] <= '9' &&
75 buf[0xD3 - 0x2E - 112] == 0x00)
76 isbinary = false;
78 /* is length & fade only digits? */
79 for (i=0;i<8 && (
80 (buf[0xA9 - 0x2E - 112+i]>='0'&&buf[0xA9 - 0x2E - 112+i]<='9') ||
81 buf[0xA9 - 0x2E - 112+i]=='\0');
82 i++);
83 if (i==8) isbinary = false;
85 if(isbinary) {
86 id3->year = buf[0] | (buf[1]<<8);
87 buf += 11;
89 length = (buf[0] | (buf[1]<<8) | (buf[2]<<16)) * 1000;
90 buf += 3;
92 fade = (buf[0] | (buf[1]<<8) | (buf[2]<<16) | (buf[3]<<24));
93 buf += 4;
94 } else {
95 char tbuf[6];
97 buf += 6;
98 buf[4] = 0;
99 id3->year = atoi(buf);
100 buf += 5;
102 memcpy(tbuf, buf, 3);
103 tbuf[3] = 0;
104 length = atoi(tbuf) * 1000;
105 buf += 3;
107 memcpy(tbuf, buf, 5);
108 tbuf[5] = 0;
109 fade = atoi(tbuf);
110 buf += 5;
113 id3->artist = p;
114 buf[31] = 0;
115 p = iso_decode(buf, p, 0, 32);
117 if (length==0) {
118 length=3*60*1000; /* 3 minutes */
119 fade=5*1000; /* 5 seconds */
122 id3->length = length+fade;
124 return true;