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
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
23 #include <stdio.h>
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
24 #include "shorten.h"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
25
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
26 #ifdef HAVE_CONFIG_H
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
27 #include "config.h"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
28 #endif
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
29
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
30 #define ID3V2_MAGIC "ID3"
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
31
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
32 typedef struct {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
33 char magic[3];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
34 unsigned char version[2];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
35 unsigned char flags[1];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
36 unsigned char size[4];
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
37 } _id3v2_header;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
38
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
39 int tagcmp(char *got,char *expected)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
40 /* compare got against expected, up to the length of expected */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
41 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
42 int i;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
43
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
44 for (i=0;*(expected+i);i++) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
45 if (*(got+i) != *(expected+i))
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
46 return i+1;
|
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 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
50 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
51
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
52 unsigned long synchsafe_int_to_ulong(unsigned char *buf)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
53 /* converts 4 bytes stored in synchsafe integer format to an unsigned long */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
54 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
55 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
|
56 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
57
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
58 static unsigned long check_for_id3v2_tag(VFSFile *f)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
59 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
60 _id3v2_header id3v2_header;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
61 unsigned long tag_size;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
62
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
63 /* read an ID3v2 header's size worth of data */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
64 if (sizeof(_id3v2_header) != vfs_fread(&id3v2_header,1,sizeof(_id3v2_header),f)) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
65 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
66 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
67
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
68 /* verify this is an ID3v2 header */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
69 if (tagcmp(id3v2_header.magic,ID3V2_MAGIC) ||
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
70 0xff == id3v2_header.version[0] || 0xff == id3v2_header.version[1] ||
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
71 0x80 <= id3v2_header.size[0] || 0x80 <= id3v2_header.size[1] ||
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
72 0x80 <= id3v2_header.size[2] || 0x80 <= id3v2_header.size[3])
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
73 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
74 return 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
75 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
76
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
77 /* calculate and return ID3v2 tag size */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
78 tag_size = synchsafe_int_to_ulong(id3v2_header.size);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
79
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
80 return tag_size;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
81 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
82
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
83 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
|
84 /* opens a file, and if it contains an ID3v2 tag, skips past it */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
85 {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
86 VFSFile *f;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
87 unsigned long tag_size;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
88
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
89 if (NULL == (f = vfs_fopen(filename,"rb"))) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
90 return NULL;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
91 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
92
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
93 if (file_has_id3v2_tag)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
94 *file_has_id3v2_tag = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
95
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
96 if (id3v2_tag_size)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
97 *id3v2_tag_size = 0;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
98
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
99 /* check for ID3v2 tag on input */
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
100 if (0 == (tag_size = check_for_id3v2_tag(f))) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
101 vfs_fclose(f);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
102 return vfs_fopen(filename,"rb");
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
103 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
104
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
105 if (file_has_id3v2_tag)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
106 *file_has_id3v2_tag = 2;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
107
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
108 if (id3v2_tag_size)
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
109 *id3v2_tag_size = (long)(tag_size + sizeof(_id3v2_header));
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
110
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
111 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
|
112
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
113 if (0 != vfs_fseek(f,(long)tag_size,SEEK_CUR)) {
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
114 shn_debug("Error while discarding ID3v2 tag in file '%s'.",filename);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
115 vfs_fclose(f);
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
116 return vfs_fopen(filename,"rb");
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
117 }
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
118
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
119 return f;
|
William Pitcock <nenolod@atheme-project.org>
parents:
diff
changeset
|
120 }
|