File: nid3.c

package info (click to toggle)
normalize-audio 0.7.7-21
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,032 kB
  • sloc: sh: 9,255; ansic: 8,286; perl: 635; makefile: 139; sed: 16
file content (1408 lines) | stat: -rw-r--r-- 30,754 bytes parent folder | download | duplicates (10)
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
/* Copyright (C) 2001--2005 Chris Vaill
   This file is part of nid3lib.

   This program 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 of the License, or
   (at your option) any later version.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA*/

/*
 * General tag and frame manipulation routines
 */

#define _POSIX_C_SOURCE 2

#include "config.h"

#include <stdio.h>
#if STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#else
# if HAVE_STDLIB_H
#  include <stdlib.h>
# endif
# if HAVE_STRING_H
#  include <string.h>
# else
#  ifndef HAVE_MEMCPY
#   define memcpy(d,s,n) bcopy((s),(d),(n))
#   define memmove(d,s,n) bcopy((s),(d),(n))
#  endif
# endif
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_ERRNO_H
# include <errno.h>
#endif
#if HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#if HAVE_FCNTL_H
# include <fcntl.h>
#endif
#if HAVE_STDINT_H
# include <stdint.h>
#endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#if HAVE_BYTESWAP_H
# include <byteswap.h>
#else
# define bswap_16(x) \
    ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
# define bswap_32(x) \
    ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |       \
     (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
#endif /* HAVE_BYTESWAP_H */

#include "nid3P.h"

static unsigned short v3_fflag_masks[] = {
  0x8000, 0x4000, 0x2000, 0x0020, 0x0080, 0x0040, 0x0000, 0x0000
};

static unsigned short v4_fflag_masks[] = {
  0x4000, 0x2000, 0x1000, 0x0040, 0x0008, 0x0004, 0x0002, 0x0001
};

static inline unsigned int
get_be_int(unsigned char *buf)
{
  unsigned int retval;
  retval = buf[0];
  retval <<= 8;
  retval += buf[1];
  retval <<= 8;
  retval += buf[2];
  retval <<= 8;
  retval += buf[3];
  return retval;
}

/* return the integer corresponding to the syncsafe encoding starting at buf */
static unsigned int
unsyncsafe_int(unsigned char *buf)
{
  unsigned int retval = 0;
  retval = buf[0];
  retval <<= 7;
  retval |= buf[1];
  retval <<= 7;
  retval |= buf[2];
  retval <<= 7;
  retval |= buf[3];
  return retval;
}

/*
 * Copy sz bytes of src to dest, de-unsyncing along the way.
 * Return value is number of bytes written to dest.
 */
static int
decode_unsync(unsigned char *dest, unsigned char *src, int sz)
{
  unsigned char *dest_save = dest;

  while (sz > 0) {
    if (*src != 0xFF) {
      *dest++ = *src++;
      sz--;
    } else {
      *dest++ = *src++;
      sz--;
      if (sz <= 0)
	break;
      if (*src == 0x00)
	src++; /* remove 0x00 to de-unsync */
    }
  }

  return dest - dest_save;
}

id3_frame_t
_id3_frame_new(void)
{
  id3_frame_t f;

  f = (id3_frame_t)calloc(1, sizeof(struct id3_frame_struct));
  if (f == NULL)
    return NULL;
  f->offset = -1;

  return f;
}

void
_id3_frame_destroy(id3_frame_t f)
{
  if (f->data)
    free(f->data);
  free(f);
}

void
_id3_frame_add(id3_t tag, id3_frame_t f)
{
  f->next = NULL;
  if (tag->frame_tl == NULL) {
    tag->frame_hd = tag->frame_tl = f;
  } else {
    tag->frame_tl->next = f;
    tag->frame_tl = f;
  }
  tag->nframes++;
}


/**
 * Creates a new tag, and associates it with a file.
 *
 * @param fname the name of the file
 *
 * @param mode one of ID3_RDONLY or ID3_RDWR
 *
 * @return a handle to a new tag
 */
id3_t
id3_open(const char *fname, int mode)
{
  int fd, omode, err;
  char *fmode;
  id3_t newtag = NULL;

  /*
   * We want to create the file if it doesn't exist, and start at the
   * beginning of the file, but we don't want to truncate, hence all
   * the open/fdopen rigamarole.
   */
  switch (mode) {
  case ID3_RDONLY:
    omode = O_RDONLY | O_BINARY;
    fmode = "rb";
    break;
  case ID3_RDWR:
    omode = O_RDWR | O_CREAT | O_BINARY;
    fmode = "rb+";
    break;
  default:
    errno = EINVAL;
    return NULL;
  }
  fd = open(fname, omode, 0666);
  if (fd == -1)
    return NULL;

  newtag = (id3_t)calloc(1, sizeof(struct id3_struct));
  if (newtag == NULL)
    goto error_close;

  newtag->fp = fdopen(fd, fmode);
  if (newtag->fp == NULL)
    goto error_free;

  newtag->fname = (char *)malloc(strlen(fname) + 1);
  if (newtag->fname == NULL)
    goto error_fclose;
  strcpy(newtag->fname, fname);
  newtag->mode = mode;
  newtag->pad_policy = ID3_PADDING_DEFAULT;
  newtag->nframes = -1;
  newtag->tagsz = -1;
  newtag->seekable = 1;
  newtag->version = 3; /* write 2.3 tags by default */
  newtag->v1.requested = 1; /* also write ID3v1 tags by default */
  newtag->v1.genre = 255; /* initialize to "unknown" */

  return newtag;

 error_fclose:
  err = errno;
  fclose(newtag->fp);
  free(newtag);
  errno = err;
  return NULL;

 error_free:
  err = errno;
  free(newtag);
  errno = err;
 error_close:
  err = errno;
  close(fd);
  errno = err;
  return NULL;
}

/*
 * Frontend to fread that de-unsync's.
 * size and the return value are the number of de-unsynced bytes.
 * The number of bytes actually read from the stream (i.e. unsynced bytes)
 * is returned in consumed, unless consumed == NULL.
 * No more than consume_limit bytes will actually be read from the stream.
 */
static size_t
unsync_fread(void *buf, size_t size, FILE *stream,
	     size_t consume_limit, size_t *consumed)
{
  unsigned char *p = (unsigned char *)buf;
  unsigned char *p_save;
  size_t cons;
  int c;

  cons = 0;
  p_save = p;

  while (size > 0 && cons < consume_limit) {
    c = getc(stream);
    if (c == EOF)
      break;
    *p++ = c; size--; cons++;
    if (c == 0xFF) {
      c = getc(stream);
      if (c == EOF)
	break;
      if (c == 0x00 && cons < consume_limit) {
	/* drop 0x00 to de-unsync, but count the consumed byte */
	cons++;
      } else if (size > 0 && cons < consume_limit) {
	*p++ = c; size--; cons++;
      } else {
	/*
	 * We had to check that it wasn't a dropped byte, but it
	 * wasn't, and we can't hold another byte, so we put it back.
	 */
	if (ungetc(c, stream) == EOF)
	  break;
      }
    }
  }

  if (consumed)
    *consumed = cons;

  return p - p_save;
}

/*
 * warning: fseek'ing backwards in an unsynced stream is probably slow!
 * warning: this won't seek past the end of a file!
 */
static int
unsync_fseek(FILE *stream, long offset, int whence, long *real_offset)
{
  long roff = 0;
  int c;

  if (whence != SEEK_CUR)
    if (fseek(stream, 0, whence) == -1)
      return -1;

  if (offset > 0) {

    /* seek forward */
    while (offset > 0) {
      c = getc(stream);
      if (c == EOF)
	break;
      offset--;
      roff++;
      if (c == 0xFF) {
	c = getc(stream);
	if (c == EOF)
	  break;
	roff++;
	if (c == 0x00) {
	  ; /* drop 0x00 to de-unsync */
	} else if (offset > 0) {
	  offset--;
	} else {
	  if (ungetc(c, stream) == EOF) /* put it back */
	    break;
	}
      }
    }

  } else if (offset < 0) {

    /* seek backward */
    getc(stream);
    while (offset < 0) {
      /* one step forward, two steps back... */
      if (fseek(stream, -2, SEEK_CUR) == -1)
	return -1;
      c = getc(stream);
      if (c == EOF)
	break;
      roff--;
      if (c == 0x00 && ftell(stream) >= 2) {
	if (fseek(stream, -2, SEEK_CUR) == -1)
	  return -1;
	c = getc(stream);
	if (c == EOF)
	  break;
	roff--;
	if (c == 0xFF)
	  ; /* drop 0x00 to de-unsync */
	else if (offset < 0)
	  offset++;
	else
	  getc(stream); /* seek ahead 1 byte */
      } else {
	offset++;
      }
    }
    if (fseek(stream, -1, SEEK_CUR) == -1)
      return -1;

  }

  if (real_offset)
    *real_offset = roff;

  if (ferror(stream))
    return -1;

  return 0;
}

static int
_read_v2_header(id3_t tag, unsigned char *hdr)
{
  tag->unsync = (hdr[5] >> 7) & 1;
  tag->tagsz = unsyncsafe_int(hdr + 6);
  return 0;
}

static int
_read_v3_header(id3_t tag, unsigned char *hdr)
{
  unsigned char xhdr[10];
  size_t consumed;
  long offset;
  int sz;

  tag->unsync       = (hdr[5] >> 7) & 1;
  tag->has_ext_hdr  = (hdr[5] >> 6) & 1;
  tag->experimental = (hdr[5] >> 5) & 1;
  tag->tagsz = unsyncsafe_int(hdr + 6);

  /* read the extended header, if it exists */
  if (tag->has_ext_hdr) {
    if (tag->unsync) {
      if (unsync_fread(xhdr, 10, tag->fp, 20, &consumed) < 10)
	return -1;
    } else {
      if (fread(xhdr, 1, 10, tag->fp) < 10)
	return -1;
    }
    sz = get_be_int(xhdr);
    tag->has_crc = (xhdr[4] & 0x80) ? 1 : 0;

    /* we could read the CRC here, but does anyone really care? */

    if (tag->unsync) {
      if (unsync_fseek(tag->fp, sz - 6, SEEK_CUR, &offset) == -1)
	return -1;
      tag->curr_off += consumed;
      tag->curr_off += offset;
    } else {
      if (tag->seekable) {
	if (fseek(tag->fp, sz - 6, SEEK_CUR) == -1)
	  tag->seekable = 0;
      }
      if (!tag->seekable) {
	sz -= 6;
	while (sz-- > 0)
	  if (getc(tag->fp) == EOF)
	    return -1;
      }
      tag->curr_off += sz;
    }
  }

  return 0;
}

static int
_read_v4_header(id3_t tag, unsigned char *hdr)
{
  unsigned char xhdr[6];
  int sz;

  tag->unsync       = (hdr[5] >> 7) & 1;
  tag->has_ext_hdr  = (hdr[5] >> 6) & 1;
  tag->experimental = (hdr[5] >> 5) & 1;
  tag->has_footer   = (hdr[5] >> 4) & 1;
  tag->tagsz = unsyncsafe_int(hdr + 6);

  /* read the extended header, if it exists */
  if (tag->has_ext_hdr) {
    if (fread(xhdr, 1, 6, tag->fp) < 6)
      return -1;
    sz = unsyncsafe_int(xhdr);
    tag->is_update = (xhdr[5] & 0x40) ? 1 : 0;
    tag->has_crc = (xhdr[5] & 0x20) ? 1 : 0;
    tag->has_restrict = (xhdr[5] & 0x10) ? 1 : 0;

    /* FIXME: read CRC and restrictions here */

    tag->curr_off += sz;
    if (tag->seekable) {
      if (fseek(tag->fp, sz - 6, SEEK_CUR) == -1)
	tag->seekable = 0;
    }
    if (!tag->seekable) {
      sz -= 6;
      while (sz-- > 0)
	if (getc(tag->fp) == EOF)
	  return -1;
    }
  }

  return 0;
}

static int
_look_for_v1tag(id3_t tag)
{
  int sz;
  char buf[128];
  if (fseek(tag->fp, -128, SEEK_END) == -1)
    return 0;
  sz = fread(buf, 1, 128, tag->fp);
  if (sz == 128 && memcmp(buf, "TAG", 3) == 0) {
    tag->v1.exists = 1;
    strncpy(tag->v1.title, buf + 3, 30);
    strncpy(tag->v1.artist, buf + 33, 30);
    strncpy(tag->v1.album, buf + 63, 30);
    strncpy(tag->v1.year, buf + 93, 4);
    strncpy(tag->v1.comment, buf + 97, 30);
    /* make sure track field is not part of a v1.0 comment */
    tag->v1.track = (buf[125] == 0) ? buf[126] : 0;
    tag->v1.genre = buf[127];
  }
  return tag->v1.exists;
}

static int
_is_id3_header(unsigned char *buf)
{
  if (strncmp((char *)buf, "ID3", 3) != 0
      || buf[3] == 0xFF || buf[4] == 0xFF
      || buf[6] >= 0x80 || buf[7] >= 0x80
      || buf[8] >= 0x80 || buf[9] >= 0x80)
    /* first 10 bytes do not match id3 tag header pattern */
    return 0;
  return 1;
}

static int
_is_id3_footer(unsigned char *buf)
{
  if (strncmp((char *)buf, "3DI", 3) != 0
      || buf[3] == 0xFF || buf[4] == 0xFF
      || buf[6] >= 0x80 || buf[7] >= 0x80
      || buf[8] >= 0x80 || buf[9] >= 0x80)
    /* first 10 bytes do not match id3 tag footer pattern */
    return 0;
  return 1;
}

static int
_look_for_footer(id3_t tag, unsigned char *hdr, long offset_from_end)
{
  int sz, tag_found = 0;
  if (fseek(tag->fp, offset_from_end, SEEK_END) == -1)
    return 0;
  sz = fread(hdr, 1, 10, tag->fp);
  if (sz == 10 && _is_id3_footer(hdr)) {
    /* seek to beginning of tag */
    tag->tagsz = unsyncsafe_int(hdr + 6);
    if (fseek(tag->fp, -tag->tagsz - 10, SEEK_CUR) == -1)
      return -1;
    tag->curr_off = ftell(tag->fp);
    tag->offset = tag->curr_off - 10;
    tag->version = hdr[3];
    tag->revision = hdr[4];
    tag->append_req = tag->append = tag->has_footer = 1;
    tag_found = 1;
  }
  return tag_found;
}

/**
 * Gets the data size of the tag.
 *
 * @param tag the tag
 *
 * @return the size of the tag, not including header and footer.  The
 *         first time this is called on a file with an unsupported tag
 *         version, it sets errno to ENOSYS and returns -1.  The user
 *         can ignore the existing tag, by calling this again, in
 *         which case it will return zero, as if no tag exists on the
 *         file.
 */
int
id3_get_size(id3_t tag)
{
  unsigned char buf[10];
  int tag_found = 0;
  int sz;

  if (tag->tagsz == -1) {

    /*
     * We haven't read the header yet -- do it now
     */
    if (tag->seekable) {
      if (fseek(tag->fp, 0, SEEK_SET) == -1)
	tag->seekable = 0;
    }
    tag->offset = 0;
    tag->curr_off = 0; /* if not seekable, we just start here */

    tag->tagsz = 0;

    sz = fread(buf, 1, 10, tag->fp);
    tag->curr_off += sz;
    if (sz < 10) {
      if (ferror(tag->fp))
	return -1;
      /* there is no tag, so just leave it at size zero */
      ;
    } else if (!_is_id3_header(buf)) {
      /* first 10 bytes do not match id3 tag pattern, so no tag */
      ;
    } else {
      tag->version = buf[3];
      tag->revision = buf[4];
      tag_found = 1;
    }

    /* check for an appended tag */
    if (tag->seekable) {
      /* check for ID3v1 tag */
      if (_look_for_v1tag(tag) == -1)
	return -1;

      if (!tag_found) {
	if (tag->v1.exists) {
	  /* check just before id3v1 tag */
	  tag_found = _look_for_footer(tag, buf, -138);
	  if (tag_found == -1)
	    return -1;
	} else {
	  /* check at the very end */
	  tag_found = _look_for_footer(tag, buf, -10);
	  if (tag_found == -1)
	    return -1;
	}
      }

      fseek(tag->fp, tag->offset + 10, SEEK_SET);
    }

    if (tag_found) {
      switch (tag->version) {
      case 2:
	if (_read_v2_header(tag, buf) == -1) {
	  errno = EINVAL;
	  return -1;
	}
	break;
      case 3:
	if (_read_v3_header(tag, buf) == -1) {
	  errno = EINVAL;
	  return -1;
	}
	break;
      case 4:
	if (_read_v4_header(tag, buf) == -1) {
	  errno = EINVAL;
	  return -1;
	}
	break;
      default:
	/* unsupported id3v2 version */
	tag->tagsz = 0;
	errno = ENOSYS;
	return -1;
      }
#if DEBUG
      fprintf(stderr, "DEBUG: v2.%d.%d tag, size %d, offset=%ld\n",
	      tag->version, tag->revision, tag->tagsz, tag->offset);
#endif
    }
  }

  return tag->tagsz;
}


/*
 * Stream must be positioned at beginning of frame data on entering.
 * Returns number of bytes consumed.
 */
static int
_read_v4_frame_data(id3_frame_t f)
{
  int err;
  id3_t tag = f->id3;

  if (tag->seekable)
    if (fseek(tag->fp, f->offset, SEEK_SET) == -1)
      tag->seekable = 0;

  /* calloc instead of malloc to ensure termination of text data */
  f->data = (unsigned char *)calloc(f->sz + 2, 1);
  if (f->data == NULL)
    goto error;
  if (fread(f->data, 1, f->sz, tag->fp) < f->sz)
    goto error_free;
  if (_frame_is_unsynced(f))
    f->sz = decode_unsync(f->data, f->data, f->sz);

  if (f->id[0] == 'T') {
    /* for text frames, set the curr_txt pointer */
    f->curr_txt = (char *)f->data + 1;
  }

  return f->sz;
 error_free:
  err = errno;
  free(f->data);
  f->data = NULL;
  errno = err;
 error:
  return -1;
}

static int
_read_v4_frame_headers(id3_t tag)
{
  unsigned char buf[10];
  id3_frame_t newframe;
  int tagsz;

  tag->nframes = 0;
  tagsz = id3_get_size(tag); /* make sure we've read the tag header */
  if (tagsz < 1)
    return tagsz;

  /* scan frames */
  while (1) {
    if (tag->curr_off + 10 > tag->offset + tagsz + 10)
      break;
    /* read the frame header */
    if (fread(buf, 1, 10, tag->fp) < 10)
      goto error;
    tag->curr_off += 10;

    /* if the frame id is all zeroes, we've reached padding */
    if (memcmp(buf, "\0\0\0\0", 4) == 0)
      break;

    newframe = _id3_frame_new();
    if (newframe == NULL)
      goto error;
    memcpy(newframe->id, buf, 4);
    newframe->sz = unsyncsafe_int(buf + 4);
    newframe->flags = ((unsigned int)buf[8] << 8) | (unsigned int)buf[9];
    newframe->offset = tag->curr_off;
    /* check for bad size (frame would exceed end of tag) */
    if (newframe->offset + newframe->sz > tag->offset + 10 + tag->tagsz) {
      _id3_frame_destroy(newframe);
      break;
    }
    newframe->id3 = tag;
    _id3_frame_add(tag, newframe);

#if 0
      fprintf(stderr, "DEBUG: found frame %s, size %d\n",
	      newframe->id, newframe->sz);
#endif

    /*
     * seek through or read frame data
     */
    if (tag->seekable) {
      if (fseek(tag->fp, newframe->sz, SEEK_CUR) == -1)
	tag->seekable = 0;
    }
    if (!tag->seekable) {
      /* if we can't seek, we need to read everything now */
      if (_read_v4_frame_data(newframe) == -1)
	goto error;
    }
    tag->curr_off += newframe->sz;
  }

  return tag->nframes;

 error:
  return -1;
}


/*
 * Stream must be positioned at beginning of frame data on entering.
 * Returns number of bytes consumed.
 */
static int
_read_v3_frame_data(id3_frame_t f)
{
  int err;
  size_t consumed;
  id3_t tag = f->id3;

  if (tag->seekable)
    if (fseek(tag->fp, f->offset, SEEK_SET) == -1)
      tag->seekable = 0;

  /* calloc instead of malloc to ensure termination of text data */
  f->data = (unsigned char *)calloc(f->sz + 2, 1);
  if (f->data == NULL)
    goto error;
  if (tag->unsync) {
    if (unsync_fread(f->data, f->sz, tag->fp, f->offset - tag->tagsz,
		     &consumed) < f->sz)
      goto error_free;
  } else {
    if (fread(f->data, 1, f->sz, tag->fp) < f->sz)
      goto error_free;
    consumed = f->sz;
  }

  if (f->id[0] == 'T') {
    /* for text frames, set the curr_txt pointer */
    f->curr_txt = (char *)f->data + 1;
  }

  return (int)consumed;
 error_free:
  err = errno;
  free(f->data);
  f->data = NULL;
  errno = err;
 error:
  return -1;
}

static int
_read_v3_frame_headers(id3_t tag)
{
  unsigned char buf[10];
  id3_frame_t newframe;
  size_t consumed, limit;
  long offset;
  int tagsz;

  tag->nframes = 0;
  tagsz = id3_get_size(tag); /* make sure we've read the tag header */
  if (tagsz < 1)
    return tagsz;

  /* scan frames */
  while (1) {
    if (tag->unsync) {

      /*
       * Read the unsynced frame header.  We have to be careful not to
       * go past the end of the tag here, so we set the consume limit
       * on unsync_fread().
       */
      limit = tag->offset + tagsz + 10 - tag->curr_off;
      if (unsync_fread(buf, 10, tag->fp, limit, &consumed) < 10)
	break;
      tag->curr_off += consumed;

    } else {

      /*
       * Read the non-unsynced frame header normally.
       */
      /* The next line, if written for readability, would be
       *     if (tag->curr_off + 10 > tagsz + 10)
       * because:
       *     +10 to offset because we want to read that many,
       *     +10 to tagsz because it doesn't include the header */
      if (tag->curr_off > tagsz)
	break;
      if (fread(buf, 1, 10, tag->fp) < 10)
	goto error;
      tag->curr_off += 10;

    }
    /* if the frame id is all zeroes, we've reached padding */
    if (memcmp(buf, "\0\0\0\0", 4) == 0)
      break;

    newframe = _id3_frame_new();
    if (newframe == NULL)
      goto error;
    memcpy(newframe->id, buf, 4);
    newframe->sz = get_be_int(buf + 4);
    newframe->flags = ((unsigned int)buf[8] << 8) | (unsigned int)buf[9];
    newframe->offset = tag->curr_off;
    /* check for bad size (frame would exceed end of tag) */
    if (newframe->offset + newframe->sz > tag->offset + 10 + tag->tagsz) {
      _id3_frame_destroy(newframe);
      break;
    }
    newframe->id3 = tag;
    _id3_frame_add(tag, newframe);

#if 0
    fprintf(stderr, "DEBUG: found frame %s, size %d\n",
	    newframe->id, newframe->sz);
#endif

    /*
     * seek through or read frame data
     */
    if (tag->seekable) {
      if (tag->unsync) {
	if (unsync_fseek(tag->fp, newframe->sz, SEEK_CUR, &offset) == -1)
	  return -1;
	tag->curr_off += offset;
      } else {
	if (fseek(tag->fp, newframe->sz, SEEK_CUR) == -1)
	  tag->seekable = 0;
	else
	  tag->curr_off += newframe->sz;
      }
    }
    if (!tag->seekable) {
      /* if we can't seek, we need to read everything now */
      offset = _read_v3_frame_data(newframe);
      if (offset == -1)
	goto error;
      tag->curr_off += offset;
    }
  }

  return tag->nframes;

 error:
  return -1;
}


/*
 * Stream must be positioned at beginning of frame data on entering.
 * Returns number of bytes consumed.
 */
static int
_read_v2_frame_data(id3_frame_t f)
{
  /* the v3 routine happens to work for v2 also */
  return _read_v3_frame_data(f);
}

static int
_read_v2_frame_headers(id3_t tag)
{
  unsigned char buf[6];
  id3_frame_t newframe;
  size_t consumed;
  long offset;
  int tagsz;

  tag->nframes = 0;
  tagsz = id3_get_size(tag); /* make sure we've read the tag header */
  if (tagsz < 1)
    return tagsz;

  /* scan frames */
  while (1) {
    if (tag->unsync) {

      /*
       * Read the unsynced frame header.  We have to be careful not to
       * go past the end of the tag here, so we set the consume limit
       * on unsync_fread().
       */
      if (unsync_fread(buf, 6, tag->fp,
		       tagsz+10 - tag->curr_off, &consumed) < 6)
	break;
      tag->curr_off += consumed;

    } else {

      /*
       * Read the non-unsynced frame header normally.
       */
      if (tag->curr_off + 6 > tagsz + 10)
	break;
      if (fread(buf, 1, 6, tag->fp) < 6)
	goto error;
      tag->curr_off += 6;

    }
    /* if the frame id is all zeroes, we've reached padding */
    if (memcmp(buf, "\0\0\0", 3) == 0)
      break;

    newframe = _id3_frame_new();
    if (newframe == NULL)
      goto error;
    memcpy(newframe->id, buf, 3);
    newframe->sz = get_be_int(buf + 2) & 0xFFFFFF; /* read 4 bytes & drop 1 */
    newframe->offset = tag->curr_off;
    /* check for bad size (frame would exceed end of tag) */
    if (newframe->offset + newframe->sz > tag->offset + 10 + tag->tagsz) {
      _id3_frame_destroy(newframe);
      break;
    }
    newframe->id3 = tag;
    _id3_frame_add(tag, newframe);

#if 0
    fprintf(stderr, "DEBUG: found frame %s, size %d\n",
	    newframe->id, newframe->sz);
#endif

    /*
     * seek through or read frame data
     */
    if (tag->seekable) {
      if (tag->unsync) {
	if (unsync_fseek(tag->fp, newframe->sz, SEEK_CUR, &offset) == -1)
	  return -1;
	tag->curr_off += offset;
      } else {
	if (fseek(tag->fp, newframe->sz, SEEK_CUR) == -1)
	  tag->seekable = 0;
	else
	  tag->curr_off += newframe->sz;
      }
    }
    if (!tag->seekable) {
      /* if we can't seek, we need to read everything now */
      offset = _read_v2_frame_data(newframe);
      if (offset == -1)
	goto error;
      tag->curr_off += offset;
    }
  }

  return tag->nframes;

 error:
  return -1;
}


/**
 * Gets the number of frames in the tag.
 *
 * @param tag the tag
 *
 * @return the number of frames in the tag. The first time this is
 *         called on a file with an unsupported tag version, it sets
 *         errno to ENOSYS and returns -1.  The user can ignore the
 *         existing tag, by calling this again, in which case it will
 *         return zero, as if no tag exists on the file.
 */
int
id3_frame_count(id3_t tag)
{
  if (id3_get_size(tag) == -1)
    return -1;

  if (tag->nframes == -1) {
    /*
     * At this point, we should already be at the end of the header
     * (and extended header, if any), at the beginning of the first
     * frame.  We now read the headers of all the frames.
     */
    switch (tag->version) {
    case 4:
      if (_read_v4_frame_headers(tag) == -1)
	return -1;
      break;
    case 3:
      if (_read_v3_frame_headers(tag) == -1)
	return -1;
      break;
    case 2:
      if (_read_v2_frame_headers(tag) == -1)
	return -1;
      break;
    default:
      tag->nframes = 0;
    }
  }

  return tag->nframes;
}

/**
 * Deallocates the tag.
 *
 * @param tag the tag
 *
 * @return 0, or -1 on error.
 */
/* FIXME: should we flush to disk here? */
int
id3_close(id3_t tag)
{
  id3_frame_t f, tmp;

  f = tag->frame_hd;
  while (f) {
    tmp = f;
    f = f->next;
    _id3_frame_destroy(tmp);
  }

  if (tag->fname)
    free(tag->fname);
  fclose(tag->fp);
  free(tag);

  return 0;
}


/**
 * Sets the final size of the tag when written.
 * Padding policy is set to ID3_PADDING_CUSTOM if this is called.
 * @see id3_set_pad_policy()
 * @param tag the tag
 * @param size the size of the tag on disk, including header, padding,
 *        and footer.
 */
void
id3_set_size(id3_t tag, int size)
{
  tag->requested_sz = size;
  tag->pad_policy = ID3_PADDING_CUSTOM;
}


/**
 * Sets padding policy of the tag when it is written.
 * <ul>
 * <li> ID3_PADDING_NONE: no padding will be added to the end of the tag.
 * <li> ID3_PADDING_CUSTOM: the total size of the tag, with header and
 *      footer, will be at least the size set by id3_set_size().
 * <li> ID3_PADDING_DEFAULT: padding will be added according to the
 *      following rules:
 *    <ol>
 *    <li> We always add at least 32 bytes of padding.
 *    <li> For tags less than 256 bytes, we pad up to 256 bytes.
 *    <li> For tags larger than 256 bytes but smaller than 32k, we pad
 *         up to the next largest power of 2.
 *    <li> For tags larger than 32k, we pad up to the nearest 16k.
 *    </ol>
 * </ul>
 * @see id3_set_size()
 * @see id3_get_pad_policy()
 * @param tag the tag
 * @param policy the padding policy
 */
void
id3_set_pad_policy(id3_t tag, enum id3_pad_policy policy)
{
  switch (policy) {
  case ID3_PADDING_NONE:
  case ID3_PADDING_CUSTOM:
  case ID3_PADDING_DEFAULT:
    tag->pad_policy = policy;
    break;
  }
}

/**
 * Gets padding policy of the tag.
 * @see id3_set_pad_policy()
 * @param tag the tag
 * @return the padding policy
 */
enum id3_pad_policy
id3_get_pad_policy(id3_t tag)
{
  return tag->pad_policy;
}

/**
 * Set whether to prepend or append the tag when it is written.
 * Only ID3v2.4 and higher tags may be appended, so turning append on
 * will cause the tag to be converted to version 2.4.0.
 * @param tag the tag
 * @param append non-zero to turn append on, zero to turn append off
 *        (i.e. prepend)
 * @return 0, or -1 on error.  Errors can occur during the v2.4
 *         conversion process
 */
int
id3_set_append(id3_t tag, int append)
{
  /* only v2.4 tags can be appended */
  if (append)
    if (id3_set_version(tag, ID3_VERSION_2_4) == -1)
      return -1;
  tag->append_req = append;
  return 0;
}


/**
 * Set whether to unsync the tag when it is written.
 * @param tag the tag
 * @param unsync non-zero to turn unsync on, zero to turn unsync off
 * @return 0, or -1 on error.
 */
int
id3_set_unsync(id3_t tag, int unsync)
{
  tag->unsync = unsync;
  return 0;
}


/**
 * Gets the nth frame in the tag.
 * @param tag the tag
 * @param n the index of the frame to return
 * @return the nth frame, or NULL on error.
 */
/* FIXME: this is O(n) right now */
id3_frame_t
id3_get_frame(id3_t tag, int n)
{
  id3_frame_t f;

  /* make sure we've read the headers */
  id3_frame_count(tag);

  for (f = tag->frame_hd; f && n > 0; n--)
    f = f->next;

  return f;
}

/*
 * returns the first frame with the given id
 */
/* FIXME: this is O(n) */
/* FIXME: need some way to get matching frames other than the first */
id3_frame_t
id3_get_frame_by_id(id3_t tag, const char *id)
{
  id3_frame_t f;

  /* make sure we've read the headers */
  id3_frame_count(tag);

  for (f = tag->frame_hd; f; f = f->next)
    if (strcmp(id, f->id) == 0)
      break;

  return f;
}

/* FIXME: O(n) */
void
id3_frame_delete(id3_frame_t f)
{
  id3_t tag = f->id3;
  id3_frame_t prev;

  /* make sure we've read the headers */
  id3_frame_count(tag);

  if (tag->frame_hd == f) {
    tag->frame_hd = f->next;
    if (tag->frame_hd == NULL)
      tag->frame_tl = NULL;
  } else {
    /* find the previous frame */
    for (prev = tag->frame_hd; prev; prev = prev->next)
      if (prev->next == f)
	break;
    if (prev)
      prev->next = f->next;
  }
  _id3_frame_destroy(f);

  tag->nframes--;
}


/**
 * Gets the string identifier of a frame.
 * @param f the frame
 * @return a NUL-terminated string containing the frame ID.  This is
 *         part of another structure and should not be free()ed.
 */
char *
id3_frame_get_id(id3_frame_t f)
{
  return f->id;
}

int
id3_frame_set_id(id3_frame_t f, const char *id)
{
  if (strlen(id) > 4)
    return -1;
  strcpy(f->id, id);
  return 0;
}


/**
 * Gets the size of a frame.
 * @param f the frame
 * @return the size of the frame, in bytes, not including the header
 */
int
id3_frame_get_size(id3_frame_t f)
{
  return f->sz;
}


/**
 * Gets the raw data of a frame.
 * @param f the frame
 * @return a pointer to a buffer containing the raw data of the frame.
 *         This pointer is deallocated automatically and should not be
 *         free()ed.
 */
void *
id3_frame_get_raw(id3_frame_t f)
{
  id3_t tag = f->id3;

  if (f->data == NULL && !_frame_is_compressed(f) && !_frame_is_encrypted(f)) {
    /* if the tag is not seekable, we should already have the data */
    if (tag->seekable) {
      switch (tag->version) {
      case 4:
	_read_v4_frame_data(f);
	break;
      case 3:
	_read_v3_frame_data(f);
	break;
      case 2:
	_read_v2_frame_data(f);
      }
    }
  }

  return (void *)f->data;
}

int
id3_frame_set_raw(id3_frame_t f, void *buf, int size)
{
  if (f->data)
    free(f->data);
  f->data = (unsigned char *)malloc(size);
  if (f->data == NULL)
    return -1;
  memcpy(f->data, buf, size);
  f->sz = size;
  return 0;
}

/* FIXME: make static? */
id3_frame_t
id3_frame_add(id3_t tag, const char *id)
{
  id3_frame_t fr;

  fr = id3_get_frame_by_id(tag, id);

  if (fr == NULL) {
    fr = _id3_frame_new();
    if (fr == NULL)
      return NULL;

    strncpy(fr->id, id, 4);

    fr->id3 = tag;
    _id3_frame_add(tag, fr);
  }

  return fr;
}

int
id3_frame_get_flag(id3_frame_t f, enum id3_fflag flg)
{
  switch (f->id3->version) {
  case 4:
    return (f->flags & v4_fflag_masks[flg]) ? 1 : 0;
  case 3:
    return (f->flags & v3_fflag_masks[flg]) ? 1 : 0;
  }
  return 0;
}

void
id3_frame_set_flag(id3_frame_t f, enum id3_fflag flg)
{
  switch (f->id3->version) {
  case 4:
    f->flags |= v4_fflag_masks[flg];
    break;
  case 3:
    f->flags |= v3_fflag_masks[flg];
    break;
  case 2:
  default:
    return;
  }
}

void
id3_frame_clear_flag(id3_frame_t f, enum id3_fflag flg)
{
  switch (f->id3->version) {
  case 4:
    f->flags &= ~v4_fflag_masks[flg];
    break;
  case 3:
    f->flags &= ~v3_fflag_masks[flg];
    break;
  case 2:
  default:
    return;
  }
}

void
id3_strip(id3_t tag)
{
  id3_frame_t f, tmp;

  f = tag->frame_hd;
  while (f) {
    tmp = f;
    f = f->next;
    _id3_frame_destroy(tmp);
  }

  tag->frame_hd = tag->frame_tl = NULL;
  tag->nframes = 0;
  tag->v1.requested = 0;
}