aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/id3v2_extractor.c
blob: fa5fea60f3d5820fc6858763364ef6bcbe828ab8 (plain) (blame)
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
/*
     This file is part of libextractor.
     (C) 2002, 2003, 2004, 2006, 2009 Vidyut Samanta and Christian Grothoff

     libextractor 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, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.

 */

#include "platform.h"
#include "extractor.h"
#ifndef MINGW
#include <sys/mman.h>
#endif
#include "convert.h"

#define DEBUG_EXTRACT_ID3v2 0

typedef struct
{
  const char *text;
  enum EXTRACTOR_MetaType type;
} Matches;

static Matches tmap[] = {
  {"TAL", EXTRACTOR_METATYPE_TITLE},
  {"TT1", EXTRACTOR_METATYPE_GROUP},
  {"TT2", EXTRACTOR_METATYPE_TITLE},
  {"TT3", EXTRACTOR_METATYPE_TITLE},
  {"TXT", EXTRACTOR_METATYPE_DESCRIPTION},
  {"TPB", EXTRACTOR_METATYPE_PUBLISHER},
  {"WAF", EXTRACTOR_METATYPE_LOCATION},
  {"WAR", EXTRACTOR_METATYPE_LOCATION},
  {"WAS", EXTRACTOR_METATYPE_LOCATION},
  {"WCP", EXTRACTOR_METATYPE_COPYRIGHT},
  {"WAF", EXTRACTOR_METATYPE_LOCATION},
  {"WCM", EXTRACTOR_METATYPE_DISCLAIMER},
  {"TSS", EXTRACTOR_METATYPE_FORMAT},
  {"TYE", EXTRACTOR_METATYPE_DATE},
  {"TLA", EXTRACTOR_METATYPE_LANGUAGE},
  {"TP1", EXTRACTOR_METATYPE_ARTIST},
  {"TP2", EXTRACTOR_METATYPE_ARTIST},
  {"TP3", EXTRACTOR_METATYPE_CONDUCTOR},
  {"TP4", EXTRACTOR_METATYPE_INTERPRET},
  {"IPL", EXTRACTOR_METATYPE_CONTRIBUTOR},
  {"TOF", EXTRACTOR_METATYPE_FILENAME},
  {"TEN", EXTRACTOR_METATYPE_PRODUCER},
  {"TCO", EXTRACTOR_METATYPE_SUBJECT},
  {"TCR", EXTRACTOR_METATYPE_COPYRIGHT},
  {"SLT", EXTRACTOR_METATYPE_LYRICS},
  {"TOA", EXTRACTOR_METATYPE_ARTIST},
  {"TRC", EXTRACTOR_METATYPE_ISRC},
  {"TRK", EXTRACTOR_METATYPE_TRACK_NUMBER},
  {"TCM", EXTRACTOR_METATYPE_CREATOR},
  {"TOT", EXTRACTOR_METATYPE_ALBUM},
  {"TOL", EXTRACTOR_METATYPE_AUTHOR},
  {"COM", EXTRACTOR_METATYPE_COMMENT},
  {"", EXTRACTOR_METATYPE_KEYWORDS},
  {NULL, 0},
};


/* mimetype = audio/mpeg */
int 
EXTRACTOR_id3v2_extract (const unsigned char *data,
			 size_t size,
			 EXTRACTOR_MetaDataProcessor proc,
			 void *proc_cls,
			 const char *options)
{
  int unsync;
  unsigned int tsize;
  unsigned int pos;

  if ((size < 16) ||
      (data[0] != 0x49) ||
      (data[1] != 0x44) ||
      (data[2] != 0x33) || (data[3] != 0x02) || (data[4] != 0x00))
    return 0;
  unsync = (data[5] & 0x80) > 0;
  tsize = (((data[6] & 0x7F) << 21) |
           ((data[7] & 0x7F) << 14) |
           ((data[8] & 0x7F) << 07) | ((data[9] & 0x7F) << 00));

  if (tsize + 10 > size)
    return 0;
  pos = 10;
  while (pos < tsize)
    {
      size_t csize;
      int i;

      if (pos + 6 > tsize)
        return 0;
      csize = (data[pos + 3] << 16) + (data[pos + 4] << 8) + data[pos + 5];
      if ((pos + 6 + csize > tsize) || (csize > tsize) || (csize == 0))
        break;
      i = 0;
      while (tmap[i].text != NULL)
        {
          if (0 == strncmp (tmap[i].text, (const char *) &data[pos], 3))
            {
              char *word;
              /* this byte describes the encoding
                 try to convert strings to UTF-8
                 if it fails, then forget it */
              switch (data[pos + 6])
                {
                case 0x00:
                  word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[pos + 7],
                                        csize, "ISO-8859-1");
                  break;
                case 0x01:
                  word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[pos + 7],
                                        csize, "UCS-2");
                  break;
                default:
                  /* bad encoding byte,
                     try to convert from iso-8859-1 */
                  word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[pos + 7],
                                        csize, "ISO-8859-1");
                  break;
                }
              pos++;
              csize--;
              if ((word != NULL) && (strlen (word) > 0))
                {
                  prev = addKeyword (prev, word, tmap[i].type);
                }
              else
                {
                  free (word);
                }
              break;
            }
          i++;
        }
      pos += 6 + csize;
    }
  return 0;
}

/* end of id3v2_extractor.c */