1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
1 /* id3v2.c - functions to handle ID3v2 tags prepended to shn files
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
2 * Copyright (C) 2000-2007 Jason Jordan <shnutils@freeshell.org>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
3 *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
6 * the Free Software Foundation; either version 2 of the License, or
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
7 * (at your option) any later version.
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
8 *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
9 * This program is distributed in the hope that it will be useful,
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
12 * GNU General Public License for more details.
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
13 *
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
14 * You should have received a copy of the GNU General Public License
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
15 * along with this program; if not, write to the Free Software
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
17 */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
18
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
19 /*
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
20 * $Id: id3v2.c,v 1.6 2007/03/23 05:49:48 jason Exp $
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
21 */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
22
|
1961
|
23 #include "config.h"
|
1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
24 #include <stdio.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
25 #include "shorten.h"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
26
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
27 #define ID3V2_MAGIC "ID3"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
28
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
29 typedef struct {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
30 char magic[3];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
31 unsigned char version[2];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
32 unsigned char flags[1];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
33 unsigned char size[4];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
34 } _id3v2_header;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
35
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
36 int tagcmp(char *got,char *expected)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
37 /* compare got against expected, up to the length of expected */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
38 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
39 int i;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
40
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
41 for (i=0;*(expected+i);i++) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
42 if (*(got+i) != *(expected+i))
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
43 return i+1;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
44 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
45
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
46 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
47 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
48
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
49 unsigned long synchsafe_int_to_ulong(unsigned char *buf)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
50 /* converts 4 bytes stored in synchsafe integer format to an unsigned long */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
51 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
52 return (unsigned long)(((buf[0] & 0x7f) << 21) | ((buf[1] & 0x7f) << 14) | ((buf[2] & 0x7f) << 7) | (buf[3] & 0x7f));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
53 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
54
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
55 static unsigned long check_for_id3v2_tag(VFSFile *f)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
56 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
57 _id3v2_header id3v2_header;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
58 unsigned long tag_size;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
59
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
60 /* read an ID3v2 header's size worth of data */
|
1978
|
61 if (sizeof(_id3v2_header) != aud_vfs_fread(&id3v2_header,1,sizeof(_id3v2_header),f)) {
|
1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
62 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
63 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
64
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
65 /* verify this is an ID3v2 header */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
66 if (tagcmp(id3v2_header.magic,ID3V2_MAGIC) ||
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
67 0xff == id3v2_header.version[0] || 0xff == id3v2_header.version[1] ||
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
68 0x80 <= id3v2_header.size[0] || 0x80 <= id3v2_header.size[1] ||
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
69 0x80 <= id3v2_header.size[2] || 0x80 <= id3v2_header.size[3])
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
70 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
71 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
72 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
73
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
74 /* calculate and return ID3v2 tag size */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
75 tag_size = synchsafe_int_to_ulong(id3v2_header.size);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
76
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
77 return tag_size;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
78 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
79
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
80 VFSFile *shn_open_and_discard_id3v2_tag(char *filename,int *file_has_id3v2_tag,long *id3v2_tag_size)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
81 /* opens a file, and if it contains an ID3v2 tag, skips past it */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
82 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
83 VFSFile *f;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
84 unsigned long tag_size;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
85
|
1978
|
86 if (NULL == (f = aud_vfs_fopen(filename,"rb"))) {
|
1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
87 return NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
88 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
89
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
90 if (file_has_id3v2_tag)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
91 *file_has_id3v2_tag = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
92
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
93 if (id3v2_tag_size)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
94 *id3v2_tag_size = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
95
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
96 /* check for ID3v2 tag on input */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
97 if (0 == (tag_size = check_for_id3v2_tag(f))) {
|
1978
|
98 aud_vfs_fclose(f);
|
|
99 return aud_vfs_fopen(filename,"rb");
|
1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
100 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
101
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
102 if (file_has_id3v2_tag)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
103 *file_has_id3v2_tag = 2;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
104
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
105 if (id3v2_tag_size)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
106 *id3v2_tag_size = (long)(tag_size + sizeof(_id3v2_header));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
107
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
108 shn_debug("Discarding %lu-byte ID3v2 tag at beginning of file '%s'.",tag_size+sizeof(_id3v2_header),filename);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
109
|
1978
|
110 if (0 != aud_vfs_fseek(f,(long)tag_size,SEEK_CUR)) {
|
1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
111 shn_debug("Error while discarding ID3v2 tag in file '%s'.",filename);
|
1978
|
112 aud_vfs_fclose(f);
|
|
113 return aud_vfs_fopen(filename,"rb");
|
1305
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
114 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
115
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
116 return f;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
117 }
|