~ubuntu-branches/ubuntu/quantal/rhythmbox/quantal

« back to all changes in this revision

Viewing changes to metadata/monkey-media/stream-info-impl/id3-vfs/id3-vfs.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-03-25 23:58:54 UTC
  • Revision ID: james.westby@ubuntu.com-20050325235854-2212vevw1u4074w8
Tags: upstream-0.8.8
ImportĀ upstreamĀ versionĀ 0.8.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  monkey-sound
 
2
 *  arch-tag: implementation of reading id3 tags over GnomeVFS
 
3
 *  Copyright (C) 2002 Jorn Baayen <jorn@nl.linux.org>
 
4
 *                     Marco Pesenti Gritti <marco@it.gnome.org>
 
5
 *                     Bastien Nocera <hadess@hadess.net>
 
6
 *                     based upon file.c in libid3tag by Robert Leslie
 
7
 *
 
8
 *  This program is free software; you can redistribute it and/or modify
 
9
 *  it under the terms of the GNU General Public License as published by
 
10
 *  the Free Software Foundation; either version 2 of the License, or
 
11
 *  (at your option) any later version.
 
12
 *
 
13
 *  This program is distributed in the hope that it will be useful,
 
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *  GNU General Public License for more details.
 
17
 *
 
18
 *  You should have received a copy of the GNU General Public License
 
19
 *  along with this program; if not, write to the Free Software
 
20
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
21
 */
 
22
 
 
23
#include "id3-vfs.h"
 
24
 
 
25
# ifdef HAVE_CONFIG_H
 
26
#  include "config.h"
 
27
# endif
 
28
 
 
29
# include "global.h"
 
30
 
 
31
# include <stdio.h>
 
32
# include <stdlib.h>
 
33
# include <string.h>
 
34
 
 
35
# ifdef HAVE_ASSERT_H
 
36
#  include <assert.h>
 
37
# endif
 
38
 
 
39
# ifdef HAVE_UNISTD_H
 
40
#  include <unistd.h>
 
41
# endif
 
42
 
 
43
# include "file.h"
 
44
# include "tag.h"
 
45
# include "field.h"
 
46
# include "mp3bitrate.h"
 
47
 
 
48
struct vfstag {
 
49
  struct id3_tag *tag;
 
50
  unsigned long location;
 
51
  id3_length_t length;
 
52
};
 
53
 
 
54
struct id3_vfs_file {
 
55
  GnomeVFSHandle *iofile;
 
56
  enum id3_vfs_mode mode;
 
57
  int flags;
 
58
  int options;
 
59
  struct id3_tag *primary;
 
60
  unsigned int ntags;
 
61
  struct vfstag *tags;
 
62
};
 
63
 
 
64
enum {
 
65
  ID3_FILE_FLAG_ID3V1 = 0x0001
 
66
};
 
67
 
 
68
/*
 
69
 * NAME:        query_tag()
 
70
 * DESCRIPTION: check for a tag at a file's current position
 
71
 */
 
72
static
 
73
signed long query_tag(GnomeVFSHandle *iofile)
 
74
{
 
75
  GnomeVFSFileSize save_position, bytes_read = -1;
 
76
  id3_byte_t query[ID3_TAG_QUERYSIZE];
 
77
  signed long size;
 
78
 
 
79
  if (gnome_vfs_tell(iofile, &save_position) != GNOME_VFS_OK)
 
80
    return 0;
 
81
 
 
82
  gnome_vfs_read(iofile, query, sizeof(query), &bytes_read);
 
83
  size = id3_tag_query(query, bytes_read);
 
84
 
 
85
  if (gnome_vfs_seek(iofile, GNOME_VFS_SEEK_START, save_position)
 
86
      != GNOME_VFS_OK)
 
87
    return 0;
 
88
 
 
89
  return size;
 
90
}
 
91
 
 
92
/*
 
93
 * NAME:        read_tag()
 
94
 * DESCRIPTION: read and parse a tag at a file's current position
 
95
 */
 
96
static
 
97
struct id3_tag *read_tag(GnomeVFSHandle *iofile, id3_length_t size)
 
98
{
 
99
  GnomeVFSFileSize bytes_read = -1;
 
100
  GnomeVFSResult res;
 
101
  id3_byte_t *data;
 
102
  struct id3_tag *tag = 0;
 
103
 
 
104
  data = malloc(size);
 
105
  if (data) {
 
106
    res = gnome_vfs_read (iofile, data, size, &bytes_read);
 
107
    if (res == GNOME_VFS_OK)
 
108
    {
 
109
      tag = id3_tag_parse(data, bytes_read);
 
110
    }
 
111
 
 
112
    free(data);
 
113
  }
 
114
 
 
115
  return tag;
 
116
}
 
117
 
 
118
/*
 
119
 * NAME:        update_primary()
 
120
 * DESCRIPTION: update the primary tag with data from a new tag
 
121
 */
 
122
static
 
123
int update_primary(struct id3_tag *tag, struct id3_tag const *new)
 
124
{
 
125
  unsigned int i;
 
126
 
 
127
  if (!(new->extendedflags & ID3_TAG_EXTENDEDFLAG_TAGISANUPDATE))
 
128
    id3_tag_clearframes(tag);
 
129
 
 
130
  for (i = 0; i < new->nframes; ++i) {
 
131
    if (id3_tag_attachframe(tag, new->frames[i]) == -1)
 
132
      return -1;
 
133
  }
 
134
 
 
135
  return 0;
 
136
}
 
137
 
 
138
/*
 
139
 * NAME:        add_tag()
 
140
 * DESCRIPTION: read, parse, and add a tag to a file structure
 
141
 */
 
142
static
 
143
int add_tag(struct id3_vfs_file *file, id3_length_t length)
 
144
{
 
145
  GnomeVFSFileSize location = -1;
 
146
  unsigned int i;
 
147
  struct vfstag filetag, *tags;
 
148
  struct id3_tag *tag;
 
149
 
 
150
  if (gnome_vfs_tell(file->iofile, &location) != GNOME_VFS_OK)
 
151
    return -1;
 
152
 
 
153
  /* check for duplication/overlap */
 
154
  {
 
155
    unsigned long begin1, end1, begin2, end2;
 
156
 
 
157
    begin1 = location;
 
158
    end1   = begin1 + length;
 
159
 
 
160
    for (i = 0; i < file->ntags; ++i) {
 
161
      begin2 = file->tags[i].location;
 
162
      end2   = begin2 + file->tags[i].length;
 
163
 
 
164
      if (begin1 == begin2 && end1 == end2)
 
165
        return 0;  /* duplicate */
 
166
 
 
167
      if (begin1 < end2 && end1 > begin2)
 
168
        return -1;  /* overlap */
 
169
    }
 
170
  }
 
171
 
 
172
  tags = realloc(file->tags, (file->ntags + 1) * sizeof(*tags));
 
173
  if (tags == 0)
 
174
    return -1;
 
175
 
 
176
  file->tags = tags;
 
177
 
 
178
  tag = read_tag(file->iofile, length);
 
179
  if (tag == 0)
 
180
    return -1;
 
181
 
 
182
  if (update_primary(file->primary, tag) == -1) {
 
183
    id3_tag_delete(tag);
 
184
    return -1;
 
185
  }
 
186
 
 
187
  filetag.tag      = tag;
 
188
  filetag.location = location;
 
189
  filetag.length   = length;
 
190
 
 
191
  file->tags[file->ntags++] = filetag;
 
192
 
 
193
  id3_tag_addref(tag);
 
194
 
 
195
  return 0;
 
196
}
 
197
 
 
198
/*
 
199
 * NAME:        search_tags()
 
200
 * DESCRIPTION: search for tags in a file
 
201
 */
 
202
static
 
203
int search_tags(struct id3_vfs_file *file)
 
204
{
 
205
  GnomeVFSFileSize save_position = -1;
 
206
  signed long size;
 
207
  int result = 0;
 
208
 
 
209
  if (gnome_vfs_tell(file->iofile, &save_position) != GNOME_VFS_OK)
 
210
    return -1;
 
211
 
 
212
  /* look for an ID3v1 tag */
 
213
 
 
214
  if (gnome_vfs_seek(file->iofile, GNOME_VFS_SEEK_END,
 
215
      (GnomeVFSFileOffset)-128) == GNOME_VFS_OK) {
 
216
    size = query_tag(file->iofile);
 
217
    if (size > 0) {
 
218
      if (add_tag(file, size) == -1)
 
219
        goto fail;
 
220
 
 
221
      file->flags   |= ID3_FILE_FLAG_ID3V1;
 
222
      file->options |= ID3_FILE_OPTION_ID3V1;
 
223
    }
 
224
  }
 
225
 
 
226
  /* look for a tag at the beginning of the file */
 
227
 
 
228
  gnome_vfs_seek(file->iofile, GNOME_VFS_SEEK_START, 0);
 
229
 
 
230
  size = query_tag(file->iofile);
 
231
  if (size > 0) {
 
232
    struct id3_frame const *frame;
 
233
 
 
234
    if (add_tag(file, size) == -1)
 
235
      goto fail;
 
236
 
 
237
    /* locate tags indicated by SEEK frames */
 
238
 
 
239
    while ((frame =
 
240
            id3_tag_findframe(file->tags[file->ntags - 1].tag, "SEEK", 0))) {
 
241
      long seek;
 
242
 
 
243
      seek = id3_field_getint(&frame->fields[0]);
 
244
      if (seek < 0 || gnome_vfs_seek
 
245
          (file->iofile, GNOME_VFS_SEEK_CURRENT,
 
246
           (GnomeVFSFileOffset)seek) != GNOME_VFS_OK)
 
247
        break;
 
248
 
 
249
      size = query_tag(file->iofile);
 
250
      if (size <= 0)
 
251
        break;
 
252
      else if (add_tag(file, size) == -1)
 
253
        goto fail;
 
254
    }
 
255
  }
 
256
 
 
257
  /* look for a tag at the end of the file (before any ID3v1 tag) */
 
258
 
 
259
  if (gnome_vfs_seek(file->iofile, GNOME_VFS_SEEK_END,
 
260
      ((file->flags & ID3_FILE_FLAG_ID3V1) ? -128 : 0) + -10) == GNOME_VFS_OK) {
 
261
    size = query_tag(file->iofile);
 
262
    if (size < 0 && gnome_vfs_seek
 
263
        (file->iofile, GNOME_VFS_SEEK_CURRENT, (GnomeVFSFileOffset)size)
 
264
        == GNOME_VFS_OK) {
 
265
      size = query_tag(file->iofile);
 
266
      if (size > 0 && add_tag(file, size) == -1)
 
267
        goto fail;
 
268
    }
 
269
  }
 
270
 
 
271
  if (0) {
 
272
  fail:
 
273
    if (file->flags & ID3_FILE_FLAG_ID3V1)
 
274
            result = 0;
 
275
    else
 
276
           result = -1;
 
277
  }
 
278
 
 
279
  if (gnome_vfs_tell(file->iofile, &save_position) != GNOME_VFS_OK)
 
280
    return -1;
 
281
 
 
282
  return result;
 
283
}
 
284
 
 
285
/*
 
286
 * NAME:        finish_file()
 
287
 * DESCRIPTION: release memory associated with a file
 
288
 */
 
289
static
 
290
void finish_file(struct id3_vfs_file *file)
 
291
{
 
292
  unsigned int i;
 
293
 
 
294
  if (file->primary) {
 
295
    id3_tag_delref(file->primary);
 
296
    id3_tag_delete(file->primary);
 
297
  }
 
298
 
 
299
  for (i = 0; i < file->ntags; ++i) {
 
300
    id3_tag_delref(file->tags[i].tag);
 
301
    id3_tag_delete(file->tags[i].tag);
 
302
  }
 
303
 
 
304
  if (file->tags)
 
305
    free(file->tags);
 
306
 
 
307
  free(file);
 
308
}
 
309
 
 
310
/*
 
311
 * NAME:        new_file()
 
312
 * DESCRIPTION: create a new file structure and load tags
 
313
 */
 
314
static
 
315
struct id3_vfs_file *new_file(GnomeVFSHandle *iofile, enum id3_vfs_mode mode)
 
316
{
 
317
  struct id3_vfs_file *file;
 
318
 
 
319
  file = malloc(sizeof(*file));
 
320
  if (file == 0)
 
321
    goto fail;
 
322
 
 
323
  file->iofile  = iofile;
 
324
  file->mode    = mode;
 
325
  file->flags   = 0;
 
326
  file->options = 0;
 
327
 
 
328
  file->ntags   = 0;
 
329
  file->tags    = 0;
 
330
 
 
331
  file->primary = id3_tag_new();
 
332
  if (file->primary == 0)
 
333
    goto fail;
 
334
 
 
335
  id3_tag_addref(file->primary);
 
336
 
 
337
  /* load tags from the file */
 
338
 
 
339
  if (search_tags(file) == -1)
 
340
    goto fail;
 
341
 
 
342
  if (0) {
 
343
  fail:
 
344
    if (file) {
 
345
      finish_file(file);
 
346
      file = NULL;
 
347
    }
 
348
  }
 
349
 
 
350
  return file;
 
351
}
 
352
 
 
353
/*
 
354
 * NAME:        file->open()
 
355
 * DESCRIPTION: open a file given its pathname
 
356
 */
 
357
struct id3_vfs_file *id3_vfs_open(char const *path, enum id3_vfs_mode mode)
 
358
{
 
359
  GnomeVFSHandle *iofile = NULL;
 
360
  struct id3_vfs_file *file;
 
361
 
 
362
  /* Try opening the file */
 
363
  if (gnome_vfs_open(&iofile, path,
 
364
      (mode == ID3_VFS_MODE_READWRITE) ?
 
365
      GNOME_VFS_OPEN_READ&GNOME_VFS_OPEN_WRITE : GNOME_VFS_OPEN_READ)
 
366
        != GNOME_VFS_OK)
 
367
    return 0;
 
368
 
 
369
  /* Try the different SEEK types that might fail */
 
370
  if ((gnome_vfs_seek(iofile, GNOME_VFS_SEEK_END, 0) != GNOME_VFS_OK)
 
371
      || gnome_vfs_seek(iofile, GNOME_VFS_SEEK_START, 0) != GNOME_VFS_OK)
 
372
  {
 
373
          gnome_vfs_close (iofile);
 
374
          return 0;
 
375
  }
 
376
 
 
377
  file = new_file(iofile, mode);
 
378
  if (file == 0)
 
379
    gnome_vfs_close(iofile);
 
380
 
 
381
  return file;
 
382
}
 
383
 
 
384
/*
 
385
 * NAME:        file->fdopen()
 
386
 * DESCRIPTION: open a file using an existing file descriptor
 
387
 *
 
388
 * Unsupported with the vfs interface
 
389
 */
 
390
 
 
391
/*
 
392
 * NAME:        file->close()
 
393
 * DESCRIPTION: close a file and delete its associated tags
 
394
 */
 
395
void id3_vfs_close(struct id3_vfs_file *file)
 
396
{
 
397
  gnome_vfs_close(file->iofile);
 
398
 
 
399
  finish_file(file);
 
400
}
 
401
 
 
402
/*
 
403
 * NAME:        file->tag()
 
404
 * DESCRIPTION: return the primary tag structure for a file
 
405
 */
 
406
struct id3_tag *id3_vfs_tag(struct id3_vfs_file const *file)
 
407
{
 
408
  return file->primary;
 
409
}
 
410
 
 
411
/*
 
412
 * NAME:        file->update()
 
413
 * DESCRIPTION: rewrite tag(s) to a file
 
414
 */
 
415
int id3_vfs_update(struct id3_vfs_file *file)
 
416
{
 
417
  id3_length_t size;
 
418
  unsigned char id3v1_data[128], *id3v1 = 0, *id3v2 = 0;
 
419
 
 
420
  if (file->mode != ID3_VFS_MODE_READWRITE)
 
421
    return -1;
 
422
 
 
423
  if (file->options & ID3_FILE_OPTION_ID3V1) {
 
424
    file->primary->options |= ID3_TAG_OPTION_ID3V1;
 
425
 
 
426
    size = id3_tag_render(file->primary, 0);
 
427
    if (size) {
 
428
      assert(size == sizeof(id3v1_data));
 
429
 
 
430
      size = id3_tag_render(file->primary, id3v1_data);
 
431
      if (size) {
 
432
        assert(size == sizeof(id3v1_data));
 
433
        id3v1 = id3v1_data;
 
434
      }
 
435
    }
 
436
  }
 
437
 
 
438
  file->primary->options &= ~ID3_TAG_OPTION_ID3V1;
 
439
 
 
440
  size = id3_tag_render(file->primary, 0);
 
441
  if (size) {
 
442
    id3v2 = malloc(size);
 
443
    if (id3v2 == 0)
 
444
      return -1;
 
445
 
 
446
    size = id3_tag_render(file->primary, id3v2);
 
447
    if (size == 0) {
 
448
      free(id3v2);
 
449
      id3v2 = 0;
 
450
    }
 
451
  }
 
452
 
 
453
  /* ... */
 
454
 
 
455
  if (id3v2)
 
456
    free(id3v2);
 
457
 
 
458
  return 0;
 
459
}
 
460
 
 
461
static int
 
462
id3_vfs_is_wave (guchar *buffer)
 
463
{
 
464
  if (buffer[8] != 'W')
 
465
    return 0;
 
466
  if (buffer[9] != 'A')
 
467
    return 0;
 
468
  if (buffer[10] != 'V')
 
469
    return 0;
 
470
  if (buffer[11] != 'E' && buffer[11] != ' ')
 
471
    return 0;
 
472
 
 
473
  return 1;
 
474
}
 
475
 
 
476
int
 
477
id3_vfs_bitrate (struct id3_vfs_file *file, int *bitrate, int *samplerate,
 
478
                int *time, int *version, int *vbr, int *channels)
 
479
{
 
480
  GnomeVFSFileSize save_position, length_read;
 
481
  GnomeVFSHandle *iofile = file->iofile;
 
482
  GnomeVFSResult res;
 
483
  guchar buffer[16384];
 
484
  int is_wave, found, i;
 
485
 
 
486
  *bitrate = 0;
 
487
  *samplerate = 0;
 
488
  *time = 0;
 
489
  *channels = 0;
 
490
  *version = 0;
 
491
  *vbr = 0;
 
492
  found = 0;
 
493
 
 
494
  if (gnome_vfs_tell(iofile, &save_position) != GNOME_VFS_OK)
 
495
    return 0;
 
496
 
 
497
  gnome_vfs_seek (iofile, GNOME_VFS_SEEK_START, 0);
 
498
 
 
499
  res = gnome_vfs_read (iofile, buffer, sizeof (buffer), &length_read);
 
500
  if( res != GNOME_VFS_OK || length_read < 512 )
 
501
    goto bitdone;
 
502
 
 
503
  /* Reduce false positive by castrating the search if we have a WAVE file */
 
504
  is_wave = id3_vfs_is_wave (buffer);
 
505
  if (is_wave == 1)
 
506
    length_read = 4096;
 
507
 
 
508
  for (i = 0; i + 4 < length_read; i++)
 
509
  {
 
510
    if (mp3_bitrate_parse_header (buffer+i, length_read - i, bitrate, samplerate, time, version, vbr, channels))
 
511
    {
 
512
      found = 1;
 
513
      break;
 
514
    }
 
515
  }
 
516
 
 
517
  /* If we haven't found anything, try again with 8 more kB */
 
518
  if (is_wave == 0 && found == 0)
 
519
  {
 
520
    res = gnome_vfs_read (iofile, buffer, sizeof (buffer), &length_read);
 
521
 
 
522
    if( res != GNOME_VFS_OK || length_read < 512 )
 
523
      goto bitdone;
 
524
 
 
525
    for (i = 0; i + 4 < length_read; i++)
 
526
    {
 
527
      if (mp3_bitrate_parse_header (buffer+i, length_read - i, bitrate, samplerate, time, version, vbr, channels))
 
528
      {
 
529
              found = 1;
 
530
              break;
 
531
      }
 
532
    }
 
533
  }
 
534
 
 
535
bitdone:
 
536
  if (gnome_vfs_seek(iofile, GNOME_VFS_SEEK_START, save_position) != GNOME_VFS_OK)
 
537
    return 0;
 
538
 
 
539
  return found;
 
540
}
 
541