aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/realextractor.c
blob: 2c56d810e75ba7e2f9c0e4ee46b5983629148a08 (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
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
/*
     This file is part of libextractor.
     (C) 2002, 2003 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"

#define UINT32 unsigned int
#define UINT16 unsigned short
#define UINT8 unsigned char

typedef struct
{
  UINT32 object_id;
  UINT32 size;
  UINT16 object_version;        /* must be 0 */
  UINT16 stream_number;
  UINT32 max_bit_rate;
  UINT32 avg_bit_rate;
  UINT32 max_packet_size;
  UINT32 avg_packet_size;
  UINT32 start_time;
  UINT32 preroll;
  UINT32 duration;
  UINT8 stream_name_size;
  UINT8 data[0];                /* variable length section */
  /*
     UINT8[stream_name_size]     stream_name;
     UINT8                       mime_type_size;
     UINT8[mime_type_size]       mime_type;
     UINT32                      type_specific_len;
     UINT8[type_specific_len]    type_specific_data;
   */
} Media_Properties;

typedef struct
{
  UINT32 object_id;
  UINT32 size;
  UINT16 object_version;        /* must be 0 */
  UINT16 title_len;
  UINT8 data[0];                /* variable length section */
  /*
     UINT8[title_len]  title;
     UINT16    author_len;
     UINT8[author_len]  author;
     UINT16    copyright_len;
     UINT8[copyright_len]  copyright;
     UINT16    comment_len;
     UINT8[comment_len]  comment;
   */
} Content_Description;
/* author, copyright and comment are supposed to be ASCII */

#define REAL_HEADER 0x2E524d46
#define MDPR_HEADER 0x4D445052
#define CONT_HEADER 0x434F4e54

#define RAFF4_HEADER 0x2E7261FD

static struct EXTRACTOR_Keywords *
addKeyword (EXTRACTOR_KeywordType type,
            char *keyword, struct EXTRACTOR_Keywords *next)
{
  EXTRACTOR_KeywordList *result;

  if (keyword == NULL)
    return next;
  result = malloc (sizeof (EXTRACTOR_KeywordList));
  result->next = next;
  result->keyword = keyword;
  result->keywordType = type;
  return result;
}

static struct EXTRACTOR_Keywords *
processMediaProperties (const Media_Properties * prop,
                        struct EXTRACTOR_Keywords *prev)
{

  UINT8 mime_type_size;
  UINT32 prop_size;
  char *data;

  prop_size = ntohl (prop->size);
  if (prop_size <= sizeof (Media_Properties))
    return prev;
  if (0 != prop->object_version)
    return prev;
  if (prop_size <= prop->stream_name_size + sizeof (UINT8)
      + sizeof (Media_Properties))
    return prev;

  mime_type_size = prop->data[prop->stream_name_size];
  if (prop_size <= prop->stream_name_size + sizeof (UINT8) +
      +mime_type_size + sizeof (Media_Properties))
    return prev;

  data = malloc (mime_type_size + 1);
  memcpy (data, &prop->data[prop->stream_name_size + 1], mime_type_size);
  data[mime_type_size] = '\0';

  return addKeyword (EXTRACTOR_MIMETYPE, data, prev);
}

static struct EXTRACTOR_Keywords *
processContentDescription (const Content_Description * prop,
                           struct EXTRACTOR_Keywords *prev)
{


  UINT16 author_len;
  UINT16 copyright_len;
  UINT16 comment_len;
  UINT16 title_len;
  char *title;
  char *author;
  char *copyright;
  char *comment;
  UINT32 prop_size;

  prop_size = ntohl (prop->size);
  if (prop_size <= sizeof (Content_Description))
    return prev;
  if (0 != prop->object_version)
    return prev;
  title_len = ntohs (prop->title_len);
  if (prop_size <= title_len + sizeof (UINT16) + sizeof (Content_Description))
    return prev;


  author_len = ntohs (*(UINT16 *) & prop->data[title_len]);

  if (prop_size <= title_len + sizeof (UINT16)
      + author_len + sizeof (Content_Description))
    return prev;

  copyright_len = ntohs (*(UINT16 *) & prop->data[title_len +
                                                  author_len +
                                                  sizeof (UINT16)]);

  if (prop_size <= title_len + 2 * sizeof (UINT16)
      + author_len + copyright_len + sizeof (Content_Description))
    return prev;

  comment_len = ntohs (*(UINT16 *) & prop->data[title_len +
                                                author_len +
                                                copyright_len +
                                                2 * sizeof (UINT16)]);

  if (prop_size < title_len + 3 * sizeof (UINT16)
      + author_len + copyright_len + comment_len
      + sizeof (Content_Description))
    return prev;

  title = malloc (title_len + 1);
  memcpy (title, &prop->data[0], title_len);
  title[title_len] = '\0';

  prev = addKeyword (EXTRACTOR_TITLE, title, prev);

  author = malloc (author_len + 1);
  memcpy (author, &prop->data[title_len + sizeof (UINT16)], author_len);
  author[author_len] = '\0';

  prev = addKeyword (EXTRACTOR_AUTHOR, author, prev);

  copyright = malloc (copyright_len + 1);
  memcpy (copyright,
          &prop->data[title_len + sizeof (UINT16) * 2 + author_len],
          copyright_len);
  copyright[copyright_len] = '\0';


  prev = addKeyword (EXTRACTOR_COPYRIGHT, copyright, prev);


  comment = malloc (comment_len + 1);
  memcpy (comment,
          &prop->data[title_len + sizeof (UINT16) * 3 + author_len +
                      copyright_len], comment_len);
  comment[comment_len] = '\0';

  prev = addKeyword (EXTRACTOR_COMMENT, comment, prev);

  return prev;
}

typedef struct RAFF4_header
{
  unsigned short version;
  unsigned short revision;
  unsigned short header_length;
  unsigned short compression_type;
  unsigned int granularity;
  unsigned int total_bytes;
  unsigned int bytes_per_minute;
  unsigned int bytes_per_minute2;
  unsigned short interleave_factor;
  unsigned short interleave_block_size;
  unsigned int user_data;
  float sample_rate;
  unsigned short sample_size;
  unsigned short channels;
  unsigned char interleave_code[5];
  unsigned char compression_code[5];
  unsigned char is_interleaved;
  unsigned char copy_byte;
  unsigned char stream_type;
  /*
     unsigned char tlen;
     unsigned char title[tlen];
     unsigned char alen;
     unsigned char author[alen];
     unsigned char clen;
     unsigned char copyright[clen];
     unsigned char aplen;
     unsigned char app[aplen]; */
} RAFF4_header;

#define RAFF4_HDR_SIZE 53

static char *
stndup (const char *str, size_t n)
{
  char *tmp;
  tmp = malloc (n + 1);
  tmp[n] = '\0';
  memcpy (tmp, str, n);
  return tmp;
}

/* audio/vnd.rn-realaudio */
struct EXTRACTOR_Keywords *
libextractor_real_extract (unsigned char *filename,
                           const unsigned char *data,
                           size_t size, struct EXTRACTOR_Keywords *prev)
{
  const unsigned char *pos;
  const unsigned char *end;
  struct EXTRACTOR_Keywords *result;
  unsigned int length;
  const RAFF4_header *hdr;
  unsigned char tlen;
  unsigned char alen;
  unsigned char clen;
  unsigned char aplen;

  if (size <= 2 * sizeof (int))
    return prev;

  if (RAFF4_HEADER == ntohl (*(int *) data))
    {
      /* HELIX */
      if (size <= RAFF4_HDR_SIZE + 16 + 4)
        return prev;
      prev = addKeyword (EXTRACTOR_MIMETYPE,
                         strdup ("audio/vnd.rn-realaudio"), prev);
      hdr = (const RAFF4_header *) &data[16];
      if (ntohs (hdr->header_length) + 16 > size)
        return prev;
      tlen = data[16 + RAFF4_HDR_SIZE];
      if (tlen + RAFF4_HDR_SIZE + 20 > size)
        return prev;
      alen = data[17 + tlen + RAFF4_HDR_SIZE];
      if (tlen + alen + RAFF4_HDR_SIZE + 20 > size)
        return prev;
      clen = data[18 + tlen + alen + RAFF4_HDR_SIZE];
      if (tlen + alen + clen + RAFF4_HDR_SIZE + 20 > size)
        return prev;
      aplen = data[19 + tlen + clen + alen + RAFF4_HDR_SIZE];
      if (tlen + alen + clen + aplen + RAFF4_HDR_SIZE + 20 > size)
        return prev;

      if (tlen > 0)
        prev = addKeyword (EXTRACTOR_TITLE,
                           stndup ((const char *) &data[17 + RAFF4_HDR_SIZE],
                                   tlen), prev);
      if (alen > 0)
        prev = addKeyword (EXTRACTOR_AUTHOR,
                           stndup ((const char *)
                                   &data[18 + RAFF4_HDR_SIZE + tlen], alen),
                           prev);
      if (clen > 0)
        prev = addKeyword (EXTRACTOR_COPYRIGHT,
                           stndup ((const char *)
                                   &data[19 + RAFF4_HDR_SIZE + tlen + alen],
                                   clen), prev);
      if (aplen > 0)
        prev = addKeyword (EXTRACTOR_SOFTWARE,
                           stndup ((const char *)
                                   &data[20 + RAFF4_HDR_SIZE + tlen + alen +
                                         clen], aplen), prev);
      return prev;

    }
  if (REAL_HEADER == ntohl (*(int *) data))
    {
      /* old real */
      result = prev;
      end = &data[size];
      pos = &data[0];
      while (1)
        {
          if ((pos + 8 >= end) || (pos + 8 < pos))
            break;
          length = ntohl (*(((unsigned int *) pos) + 1));
          if (length <= 0)
            break;
          if ((pos + length >= end) || (pos + length < pos))
            break;
          switch (ntohl (*((unsigned int *) pos)))
            {
            case MDPR_HEADER:
              result = processMediaProperties ((Media_Properties *) pos,
                                               result);
              pos += length;
              break;
            case CONT_HEADER:
              result = processContentDescription ((Content_Description *) pos,
                                                  result);
              pos += length;
              break;
            case REAL_HEADER:  /* treat like default */
            default:
              pos += length;
              break;
            }
        }
      return result;
    }
  return prev;
}