summaryrefslogtreecommitdiff
path: root/src/libaudtag/id3/id3-common.cc
blob: 78e3d0665cbb4b8f130767e519024511f481424e (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
/*
 * id3-common.c
 * Copyright 2010-2013 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.
 */

#include "id3-common.h"

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

#include <libaudcore/audstrings.h>
#include <libaudcore/runtime.h>

#include "../util.h"

#define MAX_STRING 65536

#define ID3_ENCODING_LATIN1    0
#define ID3_ENCODING_UTF16     1
#define ID3_ENCODING_UTF16_BE  2
#define ID3_ENCODING_UTF8      3

static void * memchr16 (const void * mem, int16_t chr, int len)
{
    while (len >= 2)
    {
        if (* (int16_t *) mem == chr)
            return (void *) mem;

        mem = (char *) mem + 2;
        len -= 2;
    }

    return nullptr;
}

static void id3_strnlen (const char * data, int size, int encoding,
 int * bytes_without_nul, int * bytes_with_nul)
{
    bool is16 = (encoding == ID3_ENCODING_UTF16 || encoding == ID3_ENCODING_UTF16_BE);
    char * nul = is16 ? (char *) memchr16 (data, 0, size) : (char *) memchr (data, 0, size);

    if (nul)
    {
        if (bytes_without_nul)
            * bytes_without_nul = nul - data;
        if (bytes_with_nul)
            * bytes_with_nul = is16 ? nul + 2 - data : nul + 1 - data;
    }
    else
    {
        if (bytes_without_nul)
            * bytes_without_nul = size;
        if (bytes_with_nul)
            * bytes_with_nul = size;
    }
}

static StringBuf id3_convert (const char * data, int size, int encoding)
{
    if (encoding == ID3_ENCODING_UTF16)
        return str_convert (data, aud::min (size, 2 * MAX_STRING), "UTF-16", "UTF-8");
    else if (encoding == ID3_ENCODING_UTF16_BE)
        return str_convert (data, aud::min (size, 2 * MAX_STRING), "UTF-16BE", "UTF-8");
    else
        return str_to_utf8 (data, aud::min (size, MAX_STRING));
}

static StringBuf id3_decode_text (const char * data, int size)
{
    if (size < 1)
        return StringBuf ();

    int real_size;
    id3_strnlen (data + 1, size - 1, data[0], & real_size, nullptr);
    return id3_convert (data + 1, real_size, data[0]);
}

void id3_associate_string (Tuple & tuple, Tuple::Field field, const char * data, int size)
{
    StringBuf text = id3_decode_text (data, size);

    if (text && text[0])
    {
        AUDDBG ("Field %i = %s.\n", field, (const char *) text);
        tuple.set_str (field, text);
    }
}

void id3_associate_int (Tuple & tuple, Tuple::Field field, const char * data, int size)
{
    StringBuf text = id3_decode_text (data, size);

    /* Ignore zeros here.  In particular, there are many ID3 tags with invalid
     * TLEN fields floating around, and we want to let mpg123 recalculate the
     * length in such cases. */
    if (text && atoi (text) > 0)
    {
        AUDDBG ("Field %i = %s.\n", field, (const char *) text);
        tuple.set_int (field, atoi (text));
    }
}

void id3_associate_length (Tuple & tuple, const char * data, int size)
{
    StringBuf text = id3_decode_text (data, size);
    int decoder_length = tuple.get_int (Tuple::Length);
    int tlen_length;

    AUDDBG ("Length, decoder length: %i, tag length: %s.\n", decoder_length, (const char *) text);

    if (text && (tlen_length = atoi (text)))
    {
        if (decoder_length <= 0 ||
            (tlen_length > (decoder_length / 2) && tlen_length < (decoder_length * 2)))
            tuple.set_int (Tuple::Length, tlen_length);
    }
}

void id3_decode_genre (Tuple & tuple, const char * data, int size)
{
    StringBuf text = id3_decode_text (data, size);
    int numericgenre;

    if (! text)
        return;

    if (text[0] == '(')
        numericgenre = atoi (text + 1);
    else
        numericgenre = atoi (text);

    if (numericgenre > 0)
        tuple.set_str (Tuple::Genre, convert_numericgenre_to_text (numericgenre));
    else
        tuple.set_str (Tuple::Genre, text);
}

void id3_decode_comment (Tuple & tuple, const char * data, int size)
{
    if (size < 4)
        return;

    int before_nul, after_nul;
    id3_strnlen (data + 4, size - 4, data[0], & before_nul, & after_nul);

    const char * lang = data + 1;
    StringBuf type = id3_convert (data + 4, before_nul, data[0]);
    StringBuf value = id3_convert (data + 4 + after_nul, size - 4 - after_nul, data[0]);

    AUDDBG ("Comment: lang = %.3s, type = %s, value = %s.\n", lang,
     (const char *) type, (const char *) value);

    if (type && ! type[0] && value) /* blank type = actual comment */
        tuple.set_str (Tuple::Comment, value);
}

static bool decode_rva_block (const char * * _data, int * _size,
 int * channel, int * adjustment, int * adjustment_unit, int * peak,
 int * peak_unit)
{
    const char * data = * _data;
    int size = * _size;
    int peak_bits;

    if (size < 4)
        return false;

    * channel = (unsigned char) data[0];
    * adjustment = (char) data[1]; /* first byte is signed */
    * adjustment = (* adjustment << 8) | (unsigned char) data[2];
    * adjustment_unit = 512;
    peak_bits = (unsigned char) data[3];

    data += 4;
    size -= 4;

    AUDDBG ("RVA block: channel = %d, adjustment = %d/%d, peak bits = %d\n",
     * channel, * adjustment, * adjustment_unit, peak_bits);

    if (peak_bits > 0 && peak_bits < (int) sizeof (int) * 8)
    {
        int bytes = (peak_bits + 7) / 8;
        int count;

        if (bytes > size)
            return false;

        * peak = 0;
        * peak_unit = 1 << peak_bits;

        for (count = 0; count < bytes; count ++)
            * peak = (* peak << 8) | (unsigned char) data[count];

        data += bytes;
        size -= count;

        AUDDBG ("RVA block: peak = %d/%d\n", * peak, * peak_unit);
    }
    else
    {
        * peak = 0;
        * peak_unit = 0;
    }

    * _data = data;
    * _size = size;
    return true;
}

void id3_decode_rva (Tuple & tuple, const char * data, int size)
{
    const char * domain;
    int channel, adjustment, adjustment_unit, peak, peak_unit;

    if (memchr (data, 0, size) == nullptr)
        return;

    domain = data;

    AUDDBG ("RVA domain: %s\n", domain);

    size -= strlen (domain) + 1;
    data += strlen (domain) + 1;

    while (size > 0)
    {
        if (! decode_rva_block (& data, & size, & channel, & adjustment,
         & adjustment_unit, & peak, & peak_unit))
            break;

        if (channel != 1) /* specific channel? */
            continue;

        if (tuple.get_value_type (Tuple::GainDivisor) == Tuple::Int)
            adjustment = adjustment * (int64_t) tuple.get_int
             (Tuple::GainDivisor) / adjustment_unit;
        else
            tuple.set_int (Tuple::GainDivisor, adjustment_unit);

        if (peak_unit)
        {
            if (tuple.get_value_type (Tuple::PeakDivisor) == Tuple::Int)
                peak = peak * (int64_t) tuple.get_int (Tuple::PeakDivisor) / peak_unit;
            else
                tuple.set_int (Tuple::PeakDivisor, peak_unit);
        }

        if (! strcmp_nocase (domain, "album"))
        {
            tuple.set_int (Tuple::AlbumGain, adjustment);

            if (peak_unit)
                tuple.set_int (Tuple::AlbumPeak, peak);
        }
        else if (! strcmp_nocase (domain, "track"))
        {
            tuple.set_int (Tuple::TrackGain, adjustment);

            if (peak_unit)
                tuple.set_int (Tuple::TrackPeak, peak);
        }
    }
}

void id3_decode_txxx (Tuple & tuple, const char * data, int size)
{
    if (size < 1)
        return;

    int before_nul, after_nul;
    id3_strnlen (data + 1, size - 1, data[0], & before_nul, & after_nul);

    StringBuf key = id3_convert (data + 1, before_nul, data[0]);
    StringBuf value = id3_convert (data + 1 + after_nul, size - 1 - after_nul, data[0]);

    AUDDBG ("Key-value: %s = %s.\n", (const char *) key, (const char *) value);

    if (key && value)
    {
        if (! strcmp_nocase (key, "REPLAYGAIN_TRACK_GAIN"))
            tuple.set_gain (Tuple::TrackGain, Tuple::GainDivisor, value);
        else if (! strcmp_nocase (key, "REPLAYGAIN_TRACK_PEAK"))
            tuple.set_gain (Tuple::TrackPeak, Tuple::PeakDivisor, value);
        else if (! strcmp_nocase (key, "REPLAYGAIN_ALBUM_GAIN"))
            tuple.set_gain (Tuple::AlbumGain, Tuple::GainDivisor, value);
        else if (! strcmp_nocase (key, "REPLAYGAIN_ALBUM_PEAK"))
            tuple.set_gain (Tuple::AlbumPeak, Tuple::PeakDivisor, value);
    }
}

Index<char> id3_decode_picture (const char * data, int size)
{
    Index<char> buf;

    const char * nul;
    if (size < 2 || ! (nul = (char *) memchr (data + 1, 0, size - 2)))
        return buf;

    int type = (unsigned char) nul[1];

    const char * body = nul + 2;
    int body_size = data + size - body;

    int before_nul2, after_nul2;
    id3_strnlen (body, body_size, data[0], & before_nul2, & after_nul2);

    const char * mime = data + 1;
    StringBuf desc = id3_convert (body, before_nul2, data[0]);

    int image_size = body_size - after_nul2;

    AUDDBG ("Picture: mime = %s, type = %d, desc = %s, size = %d.\n", mime,
     type, (const char *) desc, image_size);

    if (type == 3 || type == 0)  /* album cover or iTunes */
        buf.insert (body + after_nul2, 0, image_size);

    return buf;
}