aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/old/id3v2_extractor.c
blob: 0991a429effee1a59275eefa2cf74dc4ebeded8c (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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
     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"

#include "extractor_plugins.h"

#define DEBUG_EXTRACT_ID3v2 0

enum Id3v2Fmt
  {
    T, /* simple, 0-terminated string, prefixed by encoding */
    U, /* 0-terminated ASCII string, no encoding */
    UL, /* unsync'ed lyrics */
    SL, /* sync'ed lyrics */
    L, /* string with language prefix */
    I /* image */
  };

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

static Matches tmap[] = {
  /* skipping UFI */
  {"TT1 ", EXTRACTOR_METATYPE_SECTION, T},
  {"TT2 ", EXTRACTOR_METATYPE_TITLE, T},
  {"TT3 ", EXTRACTOR_METATYPE_SONG_VERSION, T},
  {"TP1 ", EXTRACTOR_METATYPE_ARTIST, T},
  {"TP2 ", EXTRACTOR_METATYPE_PERFORMER, T},
  {"TP3 ", EXTRACTOR_METATYPE_CONDUCTOR, T},
  {"TP4 ", EXTRACTOR_METATYPE_INTERPRETATION, T},
  {"TCM ", EXTRACTOR_METATYPE_COMPOSER, T},
  {"TXT ", EXTRACTOR_METATYPE_WRITER, T},
  {"TLA ", EXTRACTOR_METATYPE_LANGUAGE, T},
  {"TCO ", EXTRACTOR_METATYPE_GENRE, T},
  {"TAL ", EXTRACTOR_METATYPE_ALBUM, T},
  {"TPA ", EXTRACTOR_METATYPE_DISC_NUMBER, T},
  {"TRK ", EXTRACTOR_METATYPE_TRACK_NUMBER, T},
  {"TRC ", EXTRACTOR_METATYPE_ISRC, T},
  {"TYE ", EXTRACTOR_METATYPE_PUBLICATION_YEAR, T},
  /*
    FIXME: these two and TYE should be combined into
    the actual publication date (if TRD is missing)
  {"TDA ", EXTRACTOR_METATYPE_PUBLICATION_DATE},
  {"TIM ", EXTRACTOR_METATYPE_PUBLICATION_DATE},
  */
  {"TRD ", EXTRACTOR_METATYPE_CREATION_TIME, T},
  {"TMT ", EXTRACTOR_METATYPE_SOURCE, T},
  {"TFT ", EXTRACTOR_METATYPE_FORMAT_VERSION, T},
  {"TBP ", EXTRACTOR_METATYPE_BEATS_PER_MINUTE, T},
  {"TCR ", EXTRACTOR_METATYPE_COPYRIGHT, T},
  {"TPB ", EXTRACTOR_METATYPE_PUBLISHER, T},
  {"TEN ", EXTRACTOR_METATYPE_ENCODED_BY, T},
  {"TSS ", EXTRACTOR_METATYPE_PRODUCED_BY_SOFTWARE, T},
  {"TOF ", EXTRACTOR_METATYPE_FILENAME, T},
  {"TLE ", EXTRACTOR_METATYPE_DURATION, T}, /* FIXME: should append 'ms' as unit */
  {"TSI ", EXTRACTOR_METATYPE_EMBEDDED_FILE_SIZE, T},
  /* skipping TDY, TKE */
  {"TOT ", EXTRACTOR_METATYPE_ORIGINAL_TITLE, T},
  {"TOA ", EXTRACTOR_METATYPE_ORIGINAL_ARTIST, T},
  {"TOL ", EXTRACTOR_METATYPE_ORIGINAL_WRITER, T},
  {"TOR ", EXTRACTOR_METATYPE_ORIGINAL_RELEASE_YEAR, T},
  /* skipping TXX */

  {"WAF ", EXTRACTOR_METATYPE_URL, U},
  {"WAR ", EXTRACTOR_METATYPE_URL, U},
  {"WAS ", EXTRACTOR_METATYPE_URL, U},
  {"WCM ", EXTRACTOR_METATYPE_URL, U},
  {"WCP ", EXTRACTOR_METATYPE_RIGHTS, U},
  {"WCB ", EXTRACTOR_METATYPE_URL, U},
  /* skipping WXX */
  {"IPL ", EXTRACTOR_METATYPE_CONTRIBUTOR_NAME, T},
  /* skipping MCI */
  /* skipping ETC */
  /* skipping MLL */
  /* skipping STC */
  {"ULT ", EXTRACTOR_METATYPE_LYRICS, UL},
  {"SLT ", EXTRACTOR_METATYPE_LYRICS, SL},
  {"COM ", EXTRACTOR_METATYPE_COMMENT, L},
  /* skipping RVA */
  /* skipping EQU */
  /* skipping REV */
  {"PIC ", EXTRACTOR_METATYPE_PICTURE, I},
  /* skipping GEN */
  /* {"CNT ", EXTRACTOR_METATYPE_PLAY_COUNTER, XXX}, */
  /* {"POP ", EXTRACTOR_METATYPE_POPULARITY_METER, XXX}, */
  /* skipping BUF */
  /* skipping CRM */
  /* skipping CRA */
  /* {"LNK ", EXTRACTOR_METATYPE_URL, XXX}, */


  {"TALB", EXTRACTOR_METATYPE_ALBUM, T},
  {"TBPM", EXTRACTOR_METATYPE_BEATS_PER_MINUTE, T},
  {"TCOM", EXTRACTOR_METATYPE_COMPOSER, T},
  {"TCON", EXTRACTOR_METATYPE_SONG_VERSION, T},
  {"TCOP", EXTRACTOR_METATYPE_COPYRIGHT, T},
  {"TDAT", EXTRACTOR_METATYPE_CREATION_DATE, T}, /* idv23 only */
  /* TDLY */
  {"TENC", EXTRACTOR_METATYPE_ENCODED_BY, T},
  {"TEXT", EXTRACTOR_METATYPE_WRITER, T},  
  {"TFLT", EXTRACTOR_METATYPE_FORMAT_VERSION, T},
  /* TIME, idv23 only */
  {"TIT1", EXTRACTOR_METATYPE_SECTION, T},
  {"TIT2", EXTRACTOR_METATYPE_TITLE, T},
  {"TIT3", EXTRACTOR_METATYPE_SONG_VERSION, T},
  /* TKEY */
  {"TLAN", EXTRACTOR_METATYPE_LANGUAGE, T},
  {"TLEN", EXTRACTOR_METATYPE_DURATION, T}, /* FIXME: should append 'ms' as unit */
  {"TMED", EXTRACTOR_METATYPE_SOURCE, T}, 
  {"TOAL", EXTRACTOR_METATYPE_ORIGINAL_TITLE, T},
  {"TOFN", EXTRACTOR_METATYPE_ORIGINAL_ARTIST, T},
  {"TOLY", EXTRACTOR_METATYPE_ORIGINAL_WRITER, T},
  {"TOPE", EXTRACTOR_METATYPE_ORIGINAL_PERFORMER, T},
  {"TORY", EXTRACTOR_METATYPE_ORIGINAL_RELEASE_YEAR, T}, /* idv23 only */
  {"TOWN", EXTRACTOR_METATYPE_LICENSEE, T},
  {"TPE1", EXTRACTOR_METATYPE_ARTIST, T},
  {"TPE2", EXTRACTOR_METATYPE_PERFORMER, T},
  {"TPE3", EXTRACTOR_METATYPE_CONDUCTOR, T},
  {"TPE4", EXTRACTOR_METATYPE_INTERPRETATION, T}, 
  {"TPOS", EXTRACTOR_METATYPE_DISC_NUMBER, T},
  {"TPUB", EXTRACTOR_METATYPE_PUBLISHER, T},
  {"TRCK", EXTRACTOR_METATYPE_TRACK_NUMBER, T},
  /* TRDA, idv23 only */
  {"TRSN", EXTRACTOR_METATYPE_NETWORK_NAME, T},
  /* TRSO */
  {"TSIZ", EXTRACTOR_METATYPE_EMBEDDED_FILE_SIZE, T}, /* idv23 only */
  {"TSRC", EXTRACTOR_METATYPE_ISRC, T},
  /* TSSE */
  {"TYER", EXTRACTOR_METATYPE_PUBLICATION_YEAR, T}, /* idv23 only */
  {"WCOM", EXTRACTOR_METATYPE_URL, U},
  {"WCOP", EXTRACTOR_METATYPE_URL, U},
  {"WOAF", EXTRACTOR_METATYPE_URL, U},
  {"WOAS", EXTRACTOR_METATYPE_URL, U},
  {"WORS", EXTRACTOR_METATYPE_URL, U},
  {"WPAY", EXTRACTOR_METATYPE_URL, U},
  {"WPUB", EXTRACTOR_METATYPE_URL, U},
  {"WXXX", EXTRACTOR_METATYPE_URL, T},
  {"IPLS", EXTRACTOR_METATYPE_CONTRIBUTOR_NAME, T}, /* idv23 only */
  /* ... */
  {"USLT", EXTRACTOR_METATYPE_LYRICS, UL },
  {"SYLT", EXTRACTOR_METATYPE_LYRICS, SL },
  {"COMM", EXTRACTOR_METATYPE_COMMENT, L},
  /* ... */
  {"APIC", EXTRACTOR_METATYPE_PICTURE, I},
  /* ... */
  {"LINK", EXTRACTOR_METATYPE_URL, U},
  /* ... */
  {"USER", EXTRACTOR_METATYPE_LICENSE, T},
  /* ... */

  /* new frames in id3v24 */
  /* ASPI, EQU2, RVA2, SEEK, SIGN, TDEN */
  {"TDOR", EXTRACTOR_METATYPE_PUBLICATION_DATE, T},
  /* TDRC, TDRL, TDTG */
  {"TIPL", EXTRACTOR_METATYPE_CONTRIBUTOR_NAME, T},
  {"TMCL", EXTRACTOR_METATYPE_MUSICIAN_CREDITS_LIST, T},
  {"TMOO", EXTRACTOR_METATYPE_MOOD, T},
  {"TPRO", EXTRACTOR_METATYPE_COPYRIGHT, T},
  {"TSOA", EXTRACTOR_METATYPE_ALBUM, T},
  {"TSOP", EXTRACTOR_METATYPE_PERFORMER, T},
  {"TSOT", EXTRACTOR_METATYPE_TITLE, T},
  {"TSST", EXTRACTOR_METATYPE_SUBTITLE, T},

  {NULL, 0, T},
};

struct id3v2_state
{
  int state;
  unsigned int tsize;
  size_t csize;
  char id[4];
  int32_t ti;
  char ver;
  char extended_header;
  uint16_t frame_flags;
  char *mime;
};

enum ID3v2State
{
  ID3V2_INVALID = -1,
  ID3V2_READING_HEADER = 0,
  ID3V2_READING_FRAME_HEADER,
  ID3V23_READING_EXTENDED_HEADER,
  ID3V24_READING_EXTENDED_HEADER,
  ID3V2_READING_FRAME
};

struct id3v2_state *
EXTRACTOR_id3v2_init_state_method ()
{
  struct id3v2_state *state;
  state = malloc (sizeof (struct id3v2_state));
  if (state == NULL)
    return NULL;
  memset (state, 0, sizeof (struct id3v2_state));
  state->state = ID3V2_READING_HEADER;
  state->ti = -1;
  state->mime = NULL;
  return state;
}

static int
EXTRACTOR_id3v2_discard_state_method (struct id3v2_state *state)
{
  if (state != NULL)
  {
    if (state->mime != NULL)
      free (state->mime);
    free (state);
  }
  return 1;
}

static int
find_type (const char *id, size_t len)
{
  int i;
  for (i = 0; tmap[i].text != NULL; i++)
    if (0 == strncmp (tmap[i].text, id, len))
      return i;
  return -1;
}

int
EXTRACTOR_id3v2_extract_method (struct EXTRACTOR_PluginList *plugin,
    EXTRACTOR_MetaDataProcessor proc, void *proc_cls)
{
  int64_t offset = 0;
  struct id3v2_state *state;
  unsigned char *data;
  char *word = NULL;
  unsigned int off;
  enum EXTRACTOR_MetaType type;
  unsigned char picture_type;

  if (plugin == NULL)
    return 1;

  state = EXTRACTOR_id3v2_init_state_method ();
  if (state == NULL)
    return 1;

  while (1)
  {
    switch (state->state)
    {
    case ID3V2_INVALID:
      plugin->seek_request = -1;
      return EXTRACTOR_id3v2_discard_state_method (state);
    case ID3V2_READING_HEADER:
      /* TODO: support id3v24 tags at the end of file. Here's a quote from id3 faq:
       * Q: Where is an ID3v2 tag located in an MP3 file?
       * A: It is most likely located at the beginning of the file. Look for the
       *    marker "ID3" in the first 3 bytes of the file. If it's not there, it
       *    could be at the end of the file (if the tag is ID3v2.4). Look for the
       *    marker "3DI" 10 bytes from the end of the file, or 10 bytes before the
       *    beginning of an ID3v1 tag. Finally it is possible to embed ID3v2 tags
       *    in the actual MPEG stream, on an MPEG frame boundry. Almost nobody does
       *    this.
       * Parsing of such tags will not be completely correct, because we can't
       * seek backwards. (OK, now we CAN seek backwards, but we still need to mind the
       * chunk size). We will have to seek to file_size - chunk_size instead
       * (by the way, chunk size is theoretically unknown, LE is free to use any chunk
       * size, even though plugins often make assumptions about chunk size being large
       * enough to make one atomic read without seeking, if offset == 0) and search
       * for id3v1 at -128 offset, then look if there's a 3DI marker 10 bytes before
       *  it (or 10 bytes before the end of file, if id3v1 is not there; not sure
       * about APETAGs; we should probably just scan byte-by-byte from the end of file,
       * until we hit 3DI, or reach the offset == 0), and use it set offset to the
       * start of ID3v24 header, adjust the following file_position check and data
       * indices (use offset), and otherwise proceed as normal (maybe file size checks
       * along the way will have to be adjusted by -1, or made ">" instead of ">=";
       * these problems do not arise for tags at the beginning of the file, since
       * audio itself is usually at least 1-byte long; when the tag is at the end of
       * file, these checks will have to be 100% correct).
       * If there are two tags (at the beginning and at the end of the file),
       * a SEEK in the one at the beginning of the file can be used to seek to the
       * one at the end.
       */
      /* TODO: merge id3v1 and id3v2 parsers. There's an "update" flag in id3v2 that
       * tells the parser to augument id3v1 values with the values from id3v2 (if this
       * flag is not set, id3v2 parser must discard id3v1 data).
       * At the moment id3v1 and id3v2 are parsed separately, and update flag is ignored.
       */
      if (10 != pl_read (plugin, &data, 10))
      {
        state->state = ID3V2_INVALID;
        break;
      }
      if ((data[0] != 0x49) || (data[1] != 0x44) || (data[2] != 0x33) || ((data[3] != 0x02) && (data[3] != 0x03) && (data[3] != 0x04))/* || (data[4] != 0x00) minor verisons are backward-compatible*/)
      {
        state->state = ID3V2_INVALID;
        break;
      }
      state->ver = data[3];
      if (state->ver == 0x02)
      {
        state->extended_header = 0;
      }
      else if ((state->ver == 0x03) || (state->ver == 0x04))
      {
        if ((data[5] & 0x80) > 0)
        {
          /* unsync is not supported in id3v23 or id3v24*/
          state->state = ID3V2_INVALID;
          break;
        }
        state->extended_header = (data[5] & 0x40) > 0;
        if ((data[5] & 0x20) > 0)
        {
          /* experimental is not supported in id3v23 or id3v24*/
          state->state = ID3V2_INVALID;
          break;
        }
      }
      state->tsize = (((data[6] & 0x7F) << 21) | ((data[7] & 0x7F) << 14) | ((data[8] & 0x7F) << 07) | ((data[9] & 0x7F) << 00));
      if (state->ver == 0x03 && state->extended_header)
        state->state = ID3V23_READING_EXTENDED_HEADER;
      else if (state->ver == 0x04 && state->extended_header)
        state->state = ID3V24_READING_EXTENDED_HEADER;
      else
        state->state = ID3V2_READING_FRAME_HEADER;
      break;
    case ID3V23_READING_EXTENDED_HEADER:
      if (10 != pl_read (plugin, &data, 10))
      {
        state->state = ID3V2_INVALID;
        break;
      }
      if (state->ver == 0x03 && state->extended_header)
      {
        uint32_t extended_header_size;
        extended_header_size = (((data[0]) << 24) | ((data[1]) << 16) | ((data[2]) << 8) | ((data[3]) << 0));
        // padding = (((data[6]) << 24) | ((data[7]) << 16) | ((data[8]) << 8) | ((data[9]) << 0));
        if (extended_header_size - 6 != pl_read (plugin, &data, extended_header_size - 6))
        {
          state->state = ID3V2_INVALID;
          break;
        }
      }
      break;
    case ID3V24_READING_EXTENDED_HEADER:
      if (4 != pl_read (plugin, &data, 4))
      {
        state->state = ID3V2_INVALID;
        break;
      }
      if ((state->ver == 0x04) && (state->extended_header))
      {
	uint32_t extended_header_size;

        extended_header_size = (((data[0]) << 24) | 
				((data[1]) << 16) | 
				((data[2]) << 8) | 
				((data[3]) << 0));
        if (extended_header_size != pl_read (plugin, &data, extended_header_size))
        {
          state->state = ID3V2_INVALID;
          break;
        }
      }
      break;
    case ID3V2_READING_FRAME_HEADER:
      if (state->ver == 0x02)
      {
        if (6 != pl_read (plugin, &data, 6))
        {
          state->state = ID3V2_INVALID;
          break;
        }
      }
      else if ((state->ver == 0x03) || (state->ver == 0x04))
      {
        if (10 != pl_read (plugin, &data, 10))
        {
          state->state = ID3V2_INVALID;
          break;
        }
      }
      if (state->ver == 0x02)
      {
        memcpy (state->id, &data[0], 3);
        state->csize = (data[3] << 16) + (data[4] << 8) + data[5];
        if (state->csize == 0)
        {
          state->state = ID3V2_INVALID;
          break;
        }
        state->frame_flags = 0;
      }
      else if ((state->ver == 0x03) || (state->ver == 0x04))
      {
        memcpy (state->id, &data[0], 4);
        if (state->ver == 0x03)
          state->csize = (data[4] << 24) + (data[5] << 16) + (data[6] << 8) + data[7];
        else if (state->ver == 0x04)
          state->csize = ((data[4] & 0x7F) << 21) | ((data[5] & 0x7F) << 14) | ((data[6] & 0x7F) << 07) | ((data[7] & 0x7F) << 00);
        if (state->csize == 0)
        {
          state->state = ID3V2_INVALID;
          break;
        }
        state->frame_flags = (data[8] << 8) + data[9];
        if (state->ver == 0x03)
        {
          if (((state->frame_flags & 0x80) > 0) /* compressed, not yet supported */ ||
              ((state->frame_flags & 0x40) > 0) /* encrypted, not supported */)
          {
            /* Skip to next frame header */
            if (state->csize != pl_read (plugin, &data, state->csize))
              state->state = ID3V2_INVALID;
            break;
          }
        }
        else if (state->ver == 0x04)
        {
          if (((state->frame_flags & 0x08) > 0) /* compressed, not yet supported */ ||
              ((state->frame_flags & 0x04) > 0) /* encrypted, not supported */ ||
              ((state->frame_flags & 0x02) > 0) /* unsynchronization, not supported */)
          {
            /* Skip to next frame header */
            if (state->csize != pl_read (plugin, &data, state->csize))
              state->state = ID3V2_INVALID;
            break;
          }
          if ((state->frame_flags & 0x01) > 0)
          {
            /* Skip data length indicator */
            state->csize -= 4;
            if (4 != pl_read (plugin, &data, 4))
            {
              state->state = ID3V2_INVALID;
              break;
            }
          }
        }
      }

      state->ti = find_type ((const char *) state->id, (state->ver == 0x02) ? 3 : (((state->ver == 0x03) || (state->ver == 0x04)) ? 4 : 0));
      if (state->ti == -1)
      {
        if (state->csize != pl_read (plugin, &data, state->csize))
          state->state = ID3V2_INVALID;
        break;
      }
      state->state = ID3V2_READING_FRAME;
      break;
    case ID3V2_READING_FRAME:
      if (0 > (offset = pl_get_pos (plugin)))
      {
        state->state = ID3V2_INVALID;
        break;
      }
      word = NULL;
      if (((state->ver == 0x03) && ((state->frame_flags & 0x20) > 0)) ||
          ((state->ver == 0x04) && ((state->frame_flags & 0x40) > 0)))
      {
        /* "group" identifier, skip a byte */
        if (1 != pl_read (plugin, &data, 1))
        {
          state->state = ID3V2_INVALID;
          break;
        }
        state->csize--;
      }
      if (state->csize != pl_read (plugin, &data, state->csize))
      {
        state->state = ID3V2_INVALID;
        break;
      }
      switch (tmap[state->ti].fmt)
      {
      case T:
        if (data[0] == 0x00)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[1],
              state->csize - 1, "ISO-8859-1");
        else if (data[0] == 0x01)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[1],
              state->csize - 1, "UCS-2");
        else if ((state->ver == 0x04) && (data[0] == 0x02))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[1],
              state->csize - 1, "UTF-16BE");
        else if ((state->ver == 0x04) && (data[0] == 0x03))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[1],
              state->csize - 1, "UTF-8");
        else
          /* bad encoding byte, try to convert from iso-8859-1 */
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[1],
              state->csize - 1, "ISO-8859-1");
        break;
      case U:
        word = EXTRACTOR_common_convert_to_utf8 ((const char *) data,
            state->csize, "ISO-8859-1");
        break;
      case UL:
        if (state->csize < 6)
        {
          /* malformed */
          state->state = ID3V2_INVALID;
          break;
        }
        /* find end of description */
        off = 4;
        while ((off < state->csize) && (data[off] != '\0'))
          off++;
        if ((off >= state->csize) || (data[off] != '\0'))
        {
          /* malformed */
          state->state = ID3V2_INVALID;
          break;
        }
        off++;
        if (data[0] == 0x00)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "ISO-8859-1");
        else if (data[0] == 0x01)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "UCS-2");
        else if ((state->ver == 0x04) && (data[0] == 0x02))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "UTF-16BE");
        else if ((state->ver == 0x04) && (data[0] == 0x03))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "UTF-8");
        else
          /* bad encoding byte, try to convert from iso-8859-1 */
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "ISO-8859-1");
        break;
      case SL:
        if (state->csize < 7)
        {
          /* malformed */
          state->state = ID3V2_INVALID;
          break;
        }
        if (data[0] == 0x00)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[6],
              state->csize - 6, "ISO-8859-1");
        else if (data[0] == 0x01)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[6],
              state->csize - 6, "UCS-2");
        else if ((state->ver == 0x04) && (data[0] == 0x02))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[6],
              state->csize - 6, "UTF-16BE");
        else if ((state->ver == 0x04) && (data[0] == 0x03))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[6],
              state->csize - 6, "UTF-8");
        else
          /* bad encoding byte, try to convert from iso-8859-1 */
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[6],
              state->csize - 6, "ISO-8859-1");
        break;
      case L:
        if (state->csize < 5)
        {
          /* malformed */
          state->state = ID3V2_INVALID;
          break;
        }
        /* find end of description */
        off = 4;
        while ((off < state->csize) && (data[off] != '\0'))
          off++;
        if ((off >= state->csize) || (data[off] != '\0'))
        {
          /* malformed */
          state->state = ID3V2_INVALID;
          break;
        }
        off++;

        if (data[0] == 0x00)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "ISO-8859-1");
        else if (data[0] == 0x01)
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "UCS-2");
        else if ((state->ver == 0x04) && (data[0] == 0x02))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "UTF-1offBE");
        else if ((state->ver == 0x04) && (data[0] == 0x03))
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "UTF-8");
        else
          /* bad encoding byte, try to convert from iso-8859-1 */
          word = EXTRACTOR_common_convert_to_utf8 ((const char *) &data[off],
              state->csize - off, "ISO-8859-1");
        break;
      case I:
        if ( ( (state->ver == 0x02) && 
	       (state->csize < 7) ) ||
	     ( ( (state->ver == 0x03) || 
		 (state->ver == 0x04)) && (state->csize < 5)) )
        {
          /* malformed */
          state->state = ID3V2_INVALID;
          break;
        }
        if (state->mime != NULL)
          free (state->mime);
        state->mime = NULL;
        if (state->ver == 0x02)
        {
          off = 5;
          picture_type = data[4];
        }
        else if ((state->ver == 0x03) || (state->ver == 0x04))
        {
          off = 1;
          while ((off < state->csize) && (data[off] != '\0'))
            off++;
          if ((off >= state->csize) || (data[off] != '\0'))
          {
            /* malformed */
            state->state = ID3V2_INVALID;
            break;
          }
          state->mime = malloc (off);
          memcpy (state->mime, &data[1], off - 1);
          state->mime[off - 1] = '\0';
          off += 1;
          picture_type = data[off];
          off += 1;
          /* find end of mime type*/
          while ((off < state->csize) && (data[off] != '\0'))
            off++;
          if ((off >= state->csize) || (data[off] != '\0'))
          {
            free (state->mime);
            state->mime = NULL;
            /* malformed */
            state->state = ID3V2_INVALID;
            break;
          }
          off++;
        }
        switch (picture_type)
        {
        case 0x03:
        case 0x04:
          type = EXTRACTOR_METATYPE_COVER_PICTURE;
          break;
        case 0x07:
        case 0x08:
        case 0x09:
        case 0x0A:
        case 0x0B:
        case 0x0C:
          type = EXTRACTOR_METATYPE_CONTRIBUTOR_PICTURE;
          break;
        case 0x0D:
        case 0x0E:
        case 0x0F:
          type = EXTRACTOR_METATYPE_EVENT_PICTURE;
          break;
        case 0x14:
          type = EXTRACTOR_METATYPE_LOGO;
          type = EXTRACTOR_METATYPE_LOGO;
          break;
        default:
          type = EXTRACTOR_METATYPE_PICTURE;
          break;
        }
        if (state->ver == 0x02)
        {
          if (0 == strncasecmp ("PNG", (const char *) &data[1], 3))
            state->mime = strdup ("image/png");
          else if (0 == strncasecmp ("JPG", (const char *) &data[1], 3))
            state->mime = strdup ("image/jpeg");
          else
            state->mime = NULL;
        }
        else if (((state->ver == 0x03) || (state->ver == 0x04)) && (strchr (state->mime, '/') == NULL))
        {
          size_t mime_len = strlen (state->mime);
          char *type_mime = malloc (mime_len + 6 + 1);
          snprintf (type_mime, mime_len + 6 + 1, "image/%s", state->mime);
          free (state->mime);
          state->mime = type_mime;
        }
        if ((state->mime != NULL) && (0 == strcmp (state->mime, "-->")))
        {
          /* not supported */
          free (state->mime);
          state->mime = NULL;
        }
        else
        {
          if (0 != proc (proc_cls, "id3v2", type, EXTRACTOR_METAFORMAT_BINARY, state->mime, (const char*) &data[off], state->csize - off))
          {
            if (state->mime != NULL)
              free (state->mime);
            state->mime = NULL;
            return 1;
          }
          if (state->mime != NULL)
            free (state->mime);
          state->mime = NULL;
        }
        word = NULL;
        break;
      default:
        return 1;
      }
      if ((word != NULL) && (strlen (word) > 0))
      {
        if (0 != proc (proc_cls, "id3v2", tmap[state->ti].type, EXTRACTOR_METAFORMAT_UTF8, "text/plain", word, strlen (word) + 1))
        {
          free (word);
          return 1;
        }
      }
      if (word != NULL)
        free (word);
      state->state = ID3V2_READING_FRAME_HEADER;
    break;
    }
  }
  return 1;
}

/* end of id3v2_extractor.c */