summaryrefslogtreecommitdiff
path: root/apps/metadata/asap.c
blob: 9e7f227031cbb0fb94d54de1f6168b0408fe7704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 Dominik Wenger
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <inttypes.h>

#include "system.h"
#include "metadata.h"
#include "metadata_common.h"
#include "metadata_parsers.h"
#include "rbunicode.h"
#include "debug.h"

#define MAX_SONGS  32

static bool parse_dec(int *retval, const char *p, int minval, int maxval)
{
    int r = 0;
    do {
        char c = *p;
        if (c >= '0' && c <= '9')
            r = 10 * r + c - '0';
        else
            return false;
        if (r > maxval)
            return false;
    } while (*++p != '\0');
    if (r < minval)
        return false;
    *retval = r;
    return true;
}

static bool parse_text(char *retval, const char *p)
{
    int i;
    if (*p != '"')
        return false;
    p++;
    if (p[0] == '<' && p[1] == '?' && p[2] == '>' && p[3] == '"')
        return true;
    i = 0;
    while (*p != '"') {
        if (i >= 127)
            return false;
        if (*p == '\0')
            return false;
        retval[i++] = *p++;
    }
    retval[i] = '\0';
    return true;
}

static int ASAP_ParseDuration(const char *s)
{
    int r;
    if (*s < '0' || *s > '9')
        return -1;
    r = *s++ - '0';
    if (*s >= '0' && *s <= '9')
        r = 10 * r + *s++ - '0';
    if (*s == ':') {
        s++;
        if (*s < '0' || *s > '5')
            return -1;
        r = 60 * r + (*s++ - '0') * 10;
        if (*s < '0' || *s > '9')
            return -1;
        r += *s++ - '0';
    }
    r *= 1000;
    if (*s != '.')
        return r;
    s++;
    if (*s < '0' || *s > '9')
        return r;
    r += 100 * (*s++ - '0');
    if (*s < '0' || *s > '9')
        return r;
    r += 10 * (*s++ - '0');
    if (*s < '0' || *s > '9')
        return r;
    r += *s - '0';
    return r;
}

static bool read_asap_string(char* source, char** buf, char** buffer_end, char** dest)
{
    if(parse_text(*buf,source) == false)
        return false;
  
    /* set dest pointer */
    *dest = *buf;
    
    /* move buf ptr */
    *buf += strlen(*buf)+1;
       
    /* check size */
    if(*buf >= *buffer_end)
    {
        DEBUGF("Buffer full\n");
        return false;
    }
    return true;
}

static bool parse_sap_header(int fd, struct mp3entry* id3, int file_len)
{
    int module_index = 0;
    int sap_signature = -1;
    int duration_index = 0;
    unsigned char cur_char = 0; 
    int i;
    
    /* set defaults */
    int numSongs = 1;
    int defSong = 0;
    int durations[MAX_SONGS];
    for (i = 0; i < MAX_SONGS; i++)
        durations[i] = -1;
    
    /* use id3v2 buffer for our strings */
    char* buffer = id3->id3v2buf;
    char* buffer_end = id3->id3v2buf + ID3V2_BUF_SIZE;
    
    /* parse file */
    while (1)
    {
        char line[256];
        char *p;
        
        if (module_index + 8 >= file_len)
            return false;
        /* read a char */
        read(fd,&cur_char,1);
        /* end of header */
        if (cur_char == 0xff)
            break;
            
        i = 0;
        while (cur_char != 0x0d) 
        {
            line[i++] = cur_char; 
            module_index++;
            if (module_index >= file_len || (unsigned)i >= sizeof(line) - 1)
                return false;
            /* read a char */
            read(fd,&cur_char,1);
        }
        if (++module_index >= file_len )
            return false;
        /* read a char */
        read(fd,&cur_char,1);
        if ( cur_char != 0x0a)    
            return false;    
            
        line[i] = '\0';
        for (p = line; *p != '\0'; p++) {
            if (*p == ' ') {
                *p++ = '\0';
                break;
            }
        }
        
        /* parse tags */
        if(strcmp(line, "SAP") == 0)
            sap_signature = 1;
        if (sap_signature == -1)
            return false;
        if (strcmp(line, "AUTHOR") == 0)
        {
            if(read_asap_string(p, &buffer, &buffer_end, &id3->artist) == false)
                return false;
        }
        else if(strcmp(line, "NAME") == 0)
        {
            if(read_asap_string(p, &buffer, &buffer_end, &id3->title) == false)
                return false;
        }
        else if(strcmp(line, "DATE") == 0)
        {
            if(read_asap_string(p, &buffer, &buffer_end, &id3->year_string) == false)
                return false;          
        }
        else if (strcmp(line, "SONGS") == 0)
        {
            if (parse_dec(&numSongs, p, 1, MAX_SONGS) == false )
                return false; 
        }
        else if (strcmp(line, "DEFSONG") == 0)
        {
            if (parse_dec(&defSong, p, 0, MAX_SONGS) == false)
                return false; 
        }
        else if (strcmp(line, "TIME") == 0) 
        {
            int durationTemp = ASAP_ParseDuration(p);
            if (durationTemp < 0 || duration_index >= MAX_SONGS)
                return false;
            durations[duration_index++] = durationTemp;
        }
    }    
    
    /* set length: */
    int length =  durations[defSong];
    if (length < 0)
        length = 180 * 1000;
    id3->length = length;
    
    lseek(fd, 0, SEEK_SET);
    return true;
}


bool get_asap_metadata(int fd, struct mp3entry* id3)
{  

    int filelength = filesize(fd);
 
    if(parse_sap_header(fd, id3, filelength) == false)
    {
        DEBUGF("parse sap header failed.\n");
        return false;
    }   
        
    id3->bitrate = 706;
    id3->frequency = 44100;

    id3->vbr = false;
    id3->filesize = filelength;
    id3->genre_string = id3_get_num_genre(36);

    return true;
}