aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/old/real_extractor.c
blob: cfac0312640fddcb45ecae29f0a49b6af072c879 (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
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
/*
     This file is part of libextractor.
     Copyright (C) 2002, 2003, 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., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */

#include "platform.h"
#include "extractor.h"
#include <stdint.h>

#define UINT32 uint32_t
#define UINT16 uint16_t
#define UINT8 uint8_t

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 int
processMediaProperties (const Media_Properties *prop,
                        EXTRACTOR_MetaDataProcessor proc,
                        void *proc_cls)
{

  UINT8 mime_type_size;
  UINT32 prop_size;

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

  mime_type_size = prop->data[prop->stream_name_size];
  if (prop_size > prop->stream_name_size + sizeof (UINT8)
      + +mime_type_size + sizeof (Media_Properties))
  {
    char data[mime_type_size + 1];
    memcpy (data, &prop->data[prop->stream_name_size + 1], mime_type_size);
    data[mime_type_size] = '\0';

    return proc (proc_cls,
                 "real",
                 EXTRACTOR_METATYPE_MIMETYPE,
                 EXTRACTOR_METAFORMAT_UTF8,
                 "text/plain",
                 data,
                 strlen (data));
  }
  return 0;
}


static int
processContentDescription (const Content_Description *prop,
                           EXTRACTOR_MetaDataProcessor proc,
                           void *proc_cls)
{
  UINT16 author_len;
  UINT16 copyright_len;
  UINT16 comment_len;
  UINT16 title_len;
  char *title;
  char *author;
  char *copyright;
  char *comment;
  UINT32 prop_size;
  int ret;

  prop_size = ntohl (prop->size);
  if (prop_size <= sizeof (Content_Description))
    return 0;
  if (0 != prop->object_version)
    return 0;
  title_len = ntohs (prop->title_len);
  if (prop_size <= title_len + sizeof (UINT16) + sizeof (Content_Description))
    return 0;
  author_len = ntohs (*(UINT16 *) &prop->data[title_len]);
  if (prop_size <= title_len + sizeof (UINT16)
      + author_len + sizeof (Content_Description))
    return 0;

  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 0;

  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 0;

  ret = 0;
  title = malloc (title_len + 1);
  if (title != NULL)
  {
    memcpy (title, &prop->data[0], title_len);
    title[title_len] = '\0';
    ret = proc (proc_cls,
                "real",
                EXTRACTOR_METATYPE_TITLE,
                EXTRACTOR_METAFORMAT_UTF8,
                "text/plain",
                title,
                strlen (title) + 1);
    free (title);
  }
  if (ret != 0)
    return ret;

  author = malloc (author_len + 1);
  if (author != NULL)
  {
    memcpy (author, &prop->data[title_len + sizeof (UINT16)], author_len);
    author[author_len] = '\0';
    ret = proc (proc_cls,
                "real",
                EXTRACTOR_METATYPE_AUTHOR_NAME,
                EXTRACTOR_METAFORMAT_UTF8,
                "text/plain",
                author,
                strlen (author) + 1);
    free (author);
  }
  if (ret != 0)
    return ret;

  copyright = malloc (copyright_len + 1);
  if (copyright != NULL)
  {
    memcpy (copyright,
            &prop->data[title_len + sizeof (UINT16) * 2 + author_len],
            copyright_len);
    copyright[copyright_len] = '\0';
    ret = proc (proc_cls,
                "real",
                EXTRACTOR_METATYPE_COPYRIGHT,
                EXTRACTOR_METAFORMAT_UTF8,
                "text/plain",
                copyright,
                strlen (copyright) + 1);
    free (copyright);
  }
  if (ret != 0)
    return ret;

  comment = malloc (comment_len + 1);
  if (comment != NULL)
  {
    memcpy (comment,
            &prop->data[title_len + sizeof (UINT16) * 3 + author_len
                        + copyright_len], comment_len);
    comment[comment_len] = '\0';
    ret = proc (proc_cls,
                "real",
                EXTRACTOR_METATYPE_COMMENT,
                EXTRACTOR_METAFORMAT_UTF8,
                "text/plain",
                comment,
                strlen (comment) + 1);
    free (comment);
  }
  if (ret != 0)
    return ret;
  return 0;
}


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);
  if (tmp == NULL)
    return NULL;
  tmp[n] = '\0';
  memcpy (tmp, str, n);
  return tmp;
}


/* audio/vnd.rn-realaudio */
int
EXTRACTOR_real_extract (const unsigned char *data,
                        size_t size,
                        EXTRACTOR_MetaDataProcessor proc,
                        void *proc_cls,
                        const char *options)
{
  const unsigned char *pos;
  const unsigned char *end;
  unsigned int length;
  const RAFF4_header *hdr;
  unsigned char tlen;
  unsigned char alen;
  unsigned char clen;
  unsigned char aplen;
  char *x;
  int ret;

  if (size <= 2 * sizeof (int))
    return 0;
  if (RAFF4_HEADER == ntohl (*(int *) data))
  {
    /* HELIX */
    if (size <= RAFF4_HDR_SIZE + 16 + 4)
      return 0;
    if (0 != proc (proc_cls,
                   "real",
                   EXTRACTOR_METATYPE_MIMETYPE,
                   EXTRACTOR_METAFORMAT_UTF8,
                   "text/plain",
                   "audio/vnd.rn-realaudio",
                   strlen ("audio/vnd.rn-realaudio") + 1))
      return 1;
    hdr = (const RAFF4_header *) &data[16];
    if (ntohs (hdr->header_length) + 16 > size)
      return 0;
    tlen = data[16 + RAFF4_HDR_SIZE];
    if (tlen + RAFF4_HDR_SIZE + 20 > size)
      return 0;
    alen = data[17 + tlen + RAFF4_HDR_SIZE];
    if (tlen + alen + RAFF4_HDR_SIZE + 20 > size)
      return 0;
    clen = data[18 + tlen + alen + RAFF4_HDR_SIZE];
    if (tlen + alen + clen + RAFF4_HDR_SIZE + 20 > size)
      return 0;
    aplen = data[19 + tlen + clen + alen + RAFF4_HDR_SIZE];
    if (tlen + alen + clen + aplen + RAFF4_HDR_SIZE + 20 > size)
      return 0;
    ret = 0;
    if ( (tlen > 0) && (ret == 0) )
    {
      x = stndup ((const char *) &data[17 + RAFF4_HDR_SIZE], tlen);
      if (x != NULL)
      {
        ret = proc (proc_cls,
                    "real",
                    EXTRACTOR_METATYPE_MIMETYPE,
                    EXTRACTOR_METAFORMAT_UTF8,
                    "text/plain",
                    x,
                    strlen (x) + 1);
        free (x);
      }
    }
    if ( (alen > 0) && (ret == 0) )
    {
      x = stndup ((const char *) &data[18 + RAFF4_HDR_SIZE + tlen], alen);
      if (x != NULL)
      {
        ret = proc (proc_cls,
                    "real",
                    EXTRACTOR_METATYPE_MIMETYPE,
                    EXTRACTOR_METAFORMAT_UTF8,
                    "text/plain",
                    x,
                    strlen (x) + 1);
        free (x);
      }
    }
    if ( (clen > 0) && (ret == 0) )
    {
      x = stndup ((const char *) &data[19 + RAFF4_HDR_SIZE + tlen + alen],
                  clen);
      if (x != NULL)
      {
        ret = proc (proc_cls,
                    "real",
                    EXTRACTOR_METATYPE_MIMETYPE,
                    EXTRACTOR_METAFORMAT_UTF8,
                    "text/plain",
                    x,
                    strlen (x) + 1);
        free (x);
      }
    }
    if ( (aplen > 0) && (ret == 0) )
    {
      x = stndup ((const char *) &data[20 + RAFF4_HDR_SIZE + tlen + alen
                                       + clen], aplen);
      if (x != NULL)
      {
        ret = proc (proc_cls,
                    "real",
                    EXTRACTOR_METATYPE_MIMETYPE,
                    EXTRACTOR_METAFORMAT_UTF8,
                    "text/plain",
                    x,
                    strlen (x) + 1);
        free (x);
      }
    }
    return ret;
  }
  if (REAL_HEADER == ntohl (*(int *) data))
  {
    /* old real */
    end = &data[size];
    pos = &data[0];
    ret = 0;
    while (0 == ret)
    {
      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:
        ret = processMediaProperties ((Media_Properties *) pos,
                                      proc,
                                      proc_cls);
        pos += length;
        break;
      case CONT_HEADER:
        ret = processContentDescription ((Content_Description *) pos,
                                         proc,
                                         proc_cls);
        pos += length;
        break;
      case REAL_HEADER:        /* treat like default */
      default:
        pos += length;
        break;
      }
    }
    return ret;
  }
  return 0;
}