summaryrefslogtreecommitdiff
path: root/id3v2.c
blob: deaa0e1d61c04b60bd9243444a61572abbf57bc6 (plain)
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
/*
 * A simple id3 tag reader
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#define MP3FILE "song2.mp3"

struct HEADERV2
{
	uint8_t id[3];
	uint8_t ver[2];
	uint8_t flags;
	uint8_t size[4];
	uint32_t tagsize; /* does NOT include 10 byte header */
} __attribute__((__packed__));

struct HEADERFRAME
{
	uint8_t id[4];
	uint8_t size[4];
	uint8_t flags[2];
	uint32_t framesize;
} __attribute__((__packed__));

struct HEADERV1
{
	uint8_t id[3];
	uint8_t title[30];
	uint8_t artist[30];
	uint8_t album[30];
	uint8_t year[4];
	uint8_t comment[30];
	uint8_t genre;
} __attribute__((__packed__));

int synchsafe(int in);
int unsynchsafe(int in);
void readid3v2header(FILE *f, struct HEADERV2 *header);
void printid3v2headerinfo(struct HEADERV2 *header);
void readid3v2frames(FILE *f, struct HEADERV2 *header);
void printid3v2frameinfo(struct HEADERFRAME *frame);
void readid3v1header(FILE *f, struct HEADERV1 *header);
void printid3v1headerinfo(struct HEADERV1 *header);

int
main(void)
{
	printf("parsing mp3 file %s\n", MP3FILE);
	FILE *f = fopen(MP3FILE, "rb");
	if (!f)
	{
		fprintf(stderr, "fopen failed on %s\n", MP3FILE);
		exit(1);
	}
	struct HEADERV2 *headerv2 = malloc(sizeof(struct HEADERV2));
	if (!headerv2)
	{
		fprintf(stderr, "malloc failed\n");
		exit(1);
	}
	struct HEADERV1 *headerv1 = malloc(sizeof(struct HEADERV1));
	if (!headerv1)
	{
		fprintf(stderr, "malloc failed\n");
		free(headerv2);
		exit(1);
	}
	readid3v1header(f, headerv1);
	printid3v1headerinfo(headerv1);
	puts("");
	readid3v2header(f, headerv2);
	printid3v2headerinfo(headerv2);
	puts("");
	readid3v2frames(f, headerv2);
	fclose(f);
	free(headerv2);
	free(headerv1);
	return 0;
}

int
synchsafe(int in) // not used yet
{
	int out, mask = 0x7F;

	while (mask ^ 0x7FFFFFFF)
	{
		out = in & ~mask;
		out <<= 1;
		out |= in & mask;
		mask = ((mask + 1) << 8) - 1;
		in = out;
	}

	return out;
}

int
unsynchsafe(int in) // not used yet
{
	int out = 0, mask = 0x7F000000;

	while (mask)
	{
		out >>= 1;
		out |= in & mask;
		mask >>= 8;
	}

	return out;
}

void
readid3v1header(FILE *f, struct HEADERV1 *header)
{
	/* read 128 byte id3v1 header */
	fpos_t oldpos;
	/* save our file position */
	fgetpos(f, &oldpos);

	fseek(f, -128L, SEEK_END);
	fread(header, 1, 128, f);

	/* restore our file position */
	fsetpos(f, &oldpos);


}

void
printid3v1headerinfo(struct HEADERV1 *header)
{
	printf("TAG v1 header data\n");
	char title[31];
	char artist[31];
	char album[31];
	char year[5];
	char comment[31];
	strncpy(title, (char *)header->title, 30);
	strncpy(artist, (char *)header->artist, 30);
	strncpy(album, (char *)header->album, 30);
	strncpy(year, (char *)header->year, 4);
	strncpy(comment, (char *)header->comment, 30);
	printf("title:\t%s\n", title);
	printf("artist:\t%s\n", artist);
	printf("album:\t%s\n", album);
	printf("year:\t%c%c%c%c\n", year[0], year[1], year[2], year[3]);
	printf("comment:\t%s\n", comment);
}

void
readid3v2header(FILE *f, struct HEADERV2 *header)
{
	/*
	 * read 10 byte id3v2 header
	 * 3 bytes->id
	 * 2 bytes->version
	 * 1 bytes->flags
	 * 4 bytes->size
	 * the size is encoded into 4 bytes as a synchsafe integer, the most significant bit is ignored
	 * so we need to convert it
	 */
	fread(header, 1, 10, f);
	/*
	 * the tag size does NOT include the 10 byte header
	 * so it is safe to read tagsize bytes from the file
	 * to obtain the entire header
	 */
	header->tagsize =  header->size[0] << 21 |
			   header->size[1] << 14 |
			   header->size[2] << 7	 |
			   header->size[3];
}

void
printid3v2headerinfo(struct HEADERV2 *header)
{
	puts("TAGv2 header raw bytes");
	int i;
	printf("id:\t");
	for (i = 0; i < 3; i++)
		printf("%02X ", header->id[i]);
	printf("(%c%c%c)\n", header->id[0], header->id[1], header->id[2]);
	printf("ver:\t");
	for (i = 0; i < 2; i++)
		printf("%02X ", header->ver[i]);
	puts("");

	printf("flags:\t");
	printf("%02X\n", header->flags);
	printf("size:\t");
	for (i = 0; i < 4; i++)
		printf("%02X ", header->size[i]);
	printf("(%u bytes)\n", header->tagsize);

}

void
readid3v2frames(FILE *f, struct HEADERV2 *header)
{
	/* we read at absolute most header->tagsize bytes */
	size_t bytesread = 0;
	struct HEADERFRAME *frame = malloc(sizeof(struct HEADERFRAME));
	bytesread += fread(frame, 1, 10, f);
	frame->framesize = frame->size[0] << 21 |
			   frame->size[1] << 14 |
			   frame->size[2] << 7  |
			   frame->size[3];

	printid3v2frameinfo(frame);
	free(frame);
}

void
printid3v2frameinfo(struct HEADERFRAME *frame)
{
	puts("TAGv2 frame raw bytes");
	int i;
	printf("id:\t");
	for (i = 0; i < 4; i++)
		printf("%02X ", frame->id[i]);
	printf("(%c%c%c%c)\n", frame->id[0], frame->id[1], frame->id[2], frame->id[3]);
	printf("size:\t");
	for (i = 0; i < 4; i++)
		printf("%02X ", frame->size[i]);
	printf("(%u bytes)\n", frame->framesize);

}