summaryrefslogtreecommitdiff
path: root/src/libaudtag/ape/ape.c
blob: d4944031ecf4251489796d226bc604a7e5a5d65e (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * ape.c
 * Copyright 2010 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

/* TODO:
 * - Support updating files that have their tag at the beginning?
 */

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libaudcore/vfs.h>

#include "ape.h"

#pragma pack(push) /* must be byte-aligned */
#pragma pack(1)
typedef struct
{
    char magic[8];
    uint32_t version; /* LE */
    uint32_t length; /* LE */
    uint32_t items; /* LE */
    uint32_t flags; /* LE */
    uint64_t reserved;
}
APEHeader;
#pragma pack(pop)

typedef struct
{
    char * key, * value;
}
ValuePair;

#define APE_FLAG_HAS_HEADER (1 << 31)
#define APE_FLAG_HAS_NO_FOOTER (1 << 30)
#define APE_FLAG_IS_HEADER (1 << 29)

static bool_t ape_read_header (VFSFile * handle, APEHeader * header)
{
    if (vfs_fread (header, 1, sizeof (APEHeader), handle) != sizeof (APEHeader))
        return FALSE;

    if (strncmp (header->magic, "APETAGEX", 8))
        return FALSE;

    header->version = GUINT32_FROM_LE (header->version);
    header->length = GUINT32_FROM_LE (header->length);
    header->items = GUINT32_FROM_LE (header->items);
    header->flags = GUINT32_FROM_LE (header->flags);

    if (header->length < sizeof (APEHeader))
        return FALSE;

    return TRUE;
}

static bool_t ape_find_header (VFSFile * handle, APEHeader * header, int *
 start, int * length, int * data_start, int * data_length)
{
    APEHeader secondary;

    if (vfs_fseek (handle, 0, SEEK_SET))
        return FALSE;

    if (ape_read_header (handle, header))
    {
        TAGDBG ("Found header at 0, length = %d, version = %d.\n", (int)
         header->length, (int) header->version);
        * start = 0;
        * length = header->length;
        * data_start = sizeof (APEHeader);
        * data_length = header->length - sizeof (APEHeader);

        if (! (header->flags & APE_FLAG_HAS_HEADER) || ! (header->flags &
         APE_FLAG_IS_HEADER))
        {
            TAGDBG ("Invalid header flags (%u).\n", (unsigned int) header->flags);
            return FALSE;
        }

        if (! (header->flags & APE_FLAG_HAS_NO_FOOTER))
        {
            if (vfs_fseek (handle, header->length, SEEK_CUR))
                return FALSE;

            if (! ape_read_header (handle, & secondary))
            {
                TAGDBG ("Expected footer, but found none.\n");
                return FALSE;
            }

            * length += sizeof (APEHeader);
        }

        return TRUE;
    }

    if (vfs_fseek (handle, -(int) sizeof (APEHeader), SEEK_END))
        return FALSE;

    if (ape_read_header (handle, header))
    {
        TAGDBG ("Found footer at %d, length = %d, version = %d.\n", (int)
         vfs_ftell (handle) - (int) sizeof (APEHeader), (int) header->length,
         (int) header->version);
        * start = vfs_ftell (handle) - header->length;
        * length = header->length;
        * data_start = vfs_ftell (handle) - header->length;
        * data_length = header->length - sizeof (APEHeader);

        if ((header->flags & APE_FLAG_HAS_NO_FOOTER) || (header->flags &
         APE_FLAG_IS_HEADER))
        {
            TAGDBG ("Invalid footer flags (%u).\n", (unsigned int) header->flags);
            return FALSE;
        }

        if (header->flags & APE_FLAG_HAS_HEADER)
        {
            if (vfs_fseek (handle, -(int) header->length - sizeof (APEHeader),
             SEEK_CUR))
                return FALSE;

            if (! ape_read_header (handle, & secondary))
            {
                TAGDBG ("Expected header, but found none.\n");
                return FALSE;
            }

            * start -= sizeof (APEHeader);
            * length += sizeof (APEHeader);
        }

        return TRUE;
    }

    TAGDBG ("No header found.\n");
    return FALSE;
}

static bool_t ape_is_our_file (VFSFile * handle)
{
    APEHeader header;
    int start, length, data_start, data_length;

    return ape_find_header (handle, & header, & start, & length, & data_start,
     & data_length);
}

static ValuePair * ape_read_item (void * * data, int length)
{
    uint32_t * header = * data;
    char * value;
    ValuePair * pair;

    if (length < 8)
    {
        TAGDBG ("Expected item, but only %d bytes remain in tag.\n", length);
        return NULL;
    }

    value = memchr ((char *) (* data) + 8, 0, length - 8);

    if (value == NULL)
    {
        TAGDBG ("Unterminated item key (max length = %d).\n", length - 8);
        return NULL;
    }

    value ++;

    if (header[0] > (char *) (* data) + length - value)
    {
        TAGDBG ("Item value of length %d, but only %d bytes remain in tag.\n",
         (int) header[0], (int) ((char *) (* data) + length - value));
        return NULL;
    }

    pair = g_malloc (sizeof (ValuePair));
    pair->key = g_strdup ((char *) (* data) + 8);
    pair->value = g_strndup (value, header[0]);

    * data = value + header[0];

    return pair;
}

static GList * ape_read_items (VFSFile * handle)
{
    GList * list = NULL;
    APEHeader header;
    int start, length, data_start, data_length;
    void * data, * item;

    if (! ape_find_header (handle, & header, & start, & length, & data_start,
     & data_length))
        return NULL;

    if (vfs_fseek (handle, data_start, SEEK_SET))
        return NULL;

    data = g_malloc (data_length);

    if (vfs_fread (data, 1, data_length, handle) != data_length)
    {
        g_free (data);
        return NULL;
    }

    TAGDBG ("Reading %d items:\n", header.items);
    item = data;

    while (header.items --)
    {
        ValuePair * pair = ape_read_item (& item, (char *) data + data_length -
         (char *) item);

        if (pair == NULL)
            break;

        TAGDBG ("Read: %s = %s.\n", pair->key, pair->value);
        list = g_list_prepend (list, pair);
    }

    g_free (data);
    return g_list_reverse (list);
}

static void free_tag_list (GList * list)
{
    while (list != NULL)
    {
        g_free (((ValuePair *) list->data)->key);
        g_free (((ValuePair *) list->data)->value);
        g_free (list->data);
        list = g_list_delete_link (list, list);
    }
}

static void parse_gain_text (const char * text, int * value, int * unit)
{
    int sign = 1;

    * value = 0;
    * unit = 1;

    if (* text == '-')
    {
        sign = -1;
        text ++;
    }

    while (* text >= '0' && * text <= '9')
    {
        * value = * value * 10 + (* text - '0');
        text ++;
    }

    if (* text == '.')
    {
        text ++;

        while (* text >= '0' && * text <= '9' && * value < G_MAXINT / 10)
        {
            * value = * value * 10 + (* text - '0');
            * unit = * unit * 10;
            text ++;
        }
    }

    * value = * value * sign;
}

static void set_gain_info (Tuple * tuple, int field, int unit_field,
 const char * text)
{
    int value, unit;

    parse_gain_text (text, & value, & unit);

    if (tuple_get_value_type (tuple, unit_field, NULL) == TUPLE_INT)
        value = value * (int64_t) tuple_get_int (tuple, unit_field, NULL) / unit;
    else
        tuple_set_int (tuple, unit_field, NULL, unit);

    tuple_set_int (tuple, field, NULL, value);
}

static bool_t ape_read_tag (Tuple * tuple, VFSFile * handle)
{
    GList * list = ape_read_items (handle), * node;

    for (node = list; node != NULL; node = node->next)
    {
        char * key = ((ValuePair *) node->data)->key;
        char * value = ((ValuePair *) node->data)->value;

        if (! strcmp (key, "Artist"))
            tuple_set_str (tuple, FIELD_ARTIST, NULL, value);
        else if (! strcmp (key, "Title"))
            tuple_set_str (tuple, FIELD_TITLE, NULL, value);
        else if (! strcmp (key, "Album"))
            tuple_set_str (tuple, FIELD_ALBUM, NULL, value);
        else if (! strcmp (key, "Comment"))
            tuple_set_str (tuple, FIELD_COMMENT, NULL, value);
        else if (! strcmp (key, "Genre"))
            tuple_set_str (tuple, FIELD_GENRE, NULL, value);
        else if (! strcmp (key, "Track"))
            tuple_set_int (tuple, FIELD_TRACK_NUMBER, NULL, atoi (value));
        else if (! strcmp (key, "Year"))
            tuple_set_int (tuple, FIELD_YEAR, NULL, atoi (value));
        else if (! g_ascii_strcasecmp (key, "REPLAYGAIN_TRACK_GAIN"))
            set_gain_info (tuple, FIELD_GAIN_TRACK_GAIN, FIELD_GAIN_GAIN_UNIT,
             value);
        else if (! g_ascii_strcasecmp (key, "REPLAYGAIN_TRACK_PEAK"))
            set_gain_info (tuple, FIELD_GAIN_TRACK_PEAK, FIELD_GAIN_PEAK_UNIT,
             value);
        else if (! g_ascii_strcasecmp (key, "REPLAYGAIN_ALBUM_GAIN"))
            set_gain_info (tuple, FIELD_GAIN_ALBUM_GAIN, FIELD_GAIN_GAIN_UNIT,
             value);
        else if (! g_ascii_strcasecmp (key, "REPLAYGAIN_ALBUM_PEAK"))
            set_gain_info (tuple, FIELD_GAIN_ALBUM_PEAK, FIELD_GAIN_PEAK_UNIT,
             value);
    }

    free_tag_list (list);
    return TRUE;
}

static bool_t ape_write_item (VFSFile * handle, const char * key,
 const char * value, int * written_length)
{
    int key_len = strlen (key) + 1;
    int value_len = strlen (value);
    uint32_t header[2];

    TAGDBG ("Write: %s = %s.\n", key, value);

    header[0] = GUINT32_TO_LE (value_len);
    header[1] = 0;

    if (vfs_fwrite (header, 1, 8, handle) != 8)
        return FALSE;

    if (vfs_fwrite (key, 1, key_len, handle) != key_len)
        return FALSE;

    if (vfs_fwrite (value, 1, value_len, handle) != value_len)
        return FALSE;

    * written_length += 8 + key_len + value_len;
    return TRUE;
}

static bool_t write_string_item (const Tuple * tuple, int field, VFSFile *
 handle, const char * key, int * written_length, int * written_items)
{
    char * value = tuple_get_str (tuple, field, NULL);

    if (value == NULL)
        return TRUE;

    bool_t success = ape_write_item (handle, key, value, written_length);

    if (success)
        (* written_items) ++;

    str_unref (value);
    return success;
}

static bool_t write_integer_item (const Tuple * tuple, int field, VFSFile *
 handle, const char * key, int * written_length, int * written_items)
{
    int value = tuple_get_int (tuple, field, NULL);
    char scratch[32];

    if (! value)
        return TRUE;

    snprintf (scratch, sizeof scratch, "%d", value);

    if (! ape_write_item (handle, key, scratch, written_length))
        return FALSE;

    (* written_items) ++;
    return TRUE;
}

static bool_t write_header (int data_length, int items, bool_t is_header,
 VFSFile * handle)
{
    APEHeader header;

    memcpy (header.magic, "APETAGEX", 8);
    header.version = GUINT32_TO_LE (2000);
    header.length = GUINT32_TO_LE (data_length + sizeof (APEHeader));
    header.items = GUINT32_TO_LE (items);
    header.flags = is_header ? GUINT32_TO_LE (APE_FLAG_HAS_HEADER |
     APE_FLAG_IS_HEADER) : GUINT32_TO_LE (APE_FLAG_HAS_HEADER);
    header.reserved = 0;

    return vfs_fwrite (& header, 1, sizeof (APEHeader), handle) == sizeof
     (APEHeader);
}

static bool_t ape_write_tag (const Tuple * tuple, VFSFile * handle)
{
    GList * list = ape_read_items (handle), * node;
    APEHeader header;
    int start, length, data_start, data_length, items;

    if (ape_find_header (handle, & header, & start, & length, & data_start,
     & data_length))
    {
        if (start + length != vfs_fsize (handle))
        {
            TAGDBG ("Writing tags is only supported at end of file.\n");
            goto ERR;
        }

        if (vfs_ftruncate (handle, start))
            goto ERR;
    }
    else
    {
        start = vfs_fsize (handle);

        if (start < 0)
            goto ERR;
    }

    if (vfs_fseek (handle, start, SEEK_SET) || ! write_header (0, 0, TRUE,
     handle))
        goto ERR;

    length = 0;
    items = 0;

    if (! write_string_item (tuple, FIELD_ARTIST, handle, "Artist", & length,
     & items) || ! write_string_item (tuple, FIELD_TITLE, handle, "Title",
     & length, & items) || ! write_string_item (tuple, FIELD_ALBUM, handle,
     "Album", & length, & items) || ! write_string_item (tuple, FIELD_COMMENT,
     handle, "Comment", & length, & items) || ! write_string_item (tuple,
     FIELD_GENRE, handle, "Genre", & length, & items) || ! write_integer_item
     (tuple, FIELD_TRACK_NUMBER, handle, "Track", & length, & items) ||
     ! write_integer_item (tuple, FIELD_YEAR, handle, "Year", & length, & items))
        goto ERR;

    for (node = list; node != NULL; node = node->next)
    {
        char * key = ((ValuePair *) node->data)->key;
        char * value = ((ValuePair *) node->data)->value;

        if (! strcmp (key, "Artist") || ! strcmp (key, "Title") || ! strcmp
         (key, "Album") || ! strcmp (key, "Comment") || ! strcmp (key, "Genre")
         || ! strcmp (key, "Track") || ! strcmp (key, "Year"))
            continue;

        if (! ape_write_item (handle, key, value, & length))
            goto ERR;

        items ++;
    }

    TAGDBG ("Wrote %d items, %d bytes.\n", items, length);

    if (! write_header (length, items, FALSE, handle) || vfs_fseek (handle,
     start, SEEK_SET) || ! write_header (length, items, TRUE, handle))
        goto ERR;

    free_tag_list (list);
    return TRUE;

ERR:
    free_tag_list (list);
    return FALSE;
}

tag_module_t ape =
{
    .name = "APE",
    .type = TAG_TYPE_APE,
    .can_handle_file = ape_is_our_file,
    .read_tag = ape_read_tag,
    .write_tag = ape_write_tag,
};