summaryrefslogtreecommitdiff
path: root/src/libaudtag/id3/id3v1.cc
blob: 96d7b5a73f2353984d25257f0cce0817f0146289 (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
/*
 * id3v1.c
 * Copyright 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 <stdlib.h>
#include <string.h>

#include <glib.h>  /* for g_strchomp */

#include <libaudcore/audstrings.h>
#include <libaudtag/builtin.h>

#pragma pack(push)
#pragma pack(1)

struct ID3v1Tag {
    char header[3];
    char title[30];
    char artist[30];
    char album[30];
    char year[4];
    char comment[30];
    unsigned char genre;
};

struct ID3v1Ext {
    char header[4];
    char title[60];
    char artist[60];
    char album[60];
    unsigned char speed;
    char genre[30];
    char start[6];
    char end[6];
};

#pragma pack(pop)

namespace audtag {

static bool read_id3v1_tag (VFSFile & file, ID3v1Tag * tag)
{
    if (file.fseek (-sizeof (ID3v1Tag), VFS_SEEK_END) < 0)
        return false;
    if (file.fread (tag, 1, sizeof (ID3v1Tag)) != sizeof (ID3v1Tag))
        return false;

    return ! strncmp (tag->header, "TAG", 3);
}

static bool read_id3v1_ext (VFSFile & file, ID3v1Ext * ext)
{
    if (file.fseek (-(sizeof (ID3v1Ext) + sizeof (ID3v1Tag)), VFS_SEEK_END) < 0)
        return false;
    if (file.fread (ext, 1, sizeof (ID3v1Ext)) != sizeof (ID3v1Ext))
        return false;

    return ! strncmp (ext->header, "TAG+", 4);
}

bool ID3v1TagModule::can_handle_file (VFSFile & file)
{
    ID3v1Tag tag;
    return read_id3v1_tag (file, & tag);
}

static bool combine_string (Tuple & tuple, Tuple::Field field,
 const char * str1, int size1, const char * str2, int size2)
{
    StringBuf str = str_copy (str1, strlen_bounded (str1, size1));
    str.insert (-1, str2, strlen_bounded (str2, size2));

    g_strchomp (str);
    str.resize (strlen (str));

    if (! str.len ())
        return false;

    tuple.set_str (field, str);
    return true;
}

bool ID3v1TagModule::read_tag (VFSFile & file, Tuple * ptuple, Index<char> * image)
{
    if (! ptuple)
        return true; // nothing to do

    Tuple & tuple = * ptuple;
    ID3v1Tag tag;
    ID3v1Ext ext;

    if (! read_id3v1_tag (file, & tag))
        return false;

    if (! read_id3v1_ext (file, & ext))
        memset (& ext, 0, sizeof (ID3v1Ext));

    combine_string (tuple, Tuple::Title, tag.title, sizeof tag.title, ext.title, sizeof ext.title);
    combine_string (tuple, Tuple::Artist, tag.artist, sizeof tag.artist, ext.artist, sizeof ext.artist);
    combine_string (tuple, Tuple::Album, tag.album, sizeof tag.album, ext.album, sizeof ext.album);
    combine_string (tuple, Tuple::Comment, tag.comment, sizeof tag.comment, nullptr, 0);

    StringBuf year = str_copy (tag.year, strlen_bounded (tag.year, 4));
    if (atoi (year))
        tuple.set_int (Tuple::Year, atoi (year));

    if (! tag.comment[28] && tag.comment[29])
        tuple.set_int (Tuple::Track, (unsigned char) tag.comment[29]);

    if (! combine_string (tuple, Tuple::Genre, ext.genre, sizeof ext.genre, nullptr, 0))
    {
        const char * genre = convert_numericgenre_to_text (tag.genre);
        if (genre)
            tuple.set_str (Tuple::Genre, genre);
    }

    return true;
}

}