~moody/mpl

mpl/id3.c -rw-r--r-- 3.1 KiB
7309e0f2 — Jacob Moody Archive and point to zuke 6 months ago
                                                                                
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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>

#include "dat.h"
#include "fncs.h"

/* If we can't read the tag, at least default to file names */
void
fillnotag(ID3v1 *id, int fd)
{
	char buf[512];
	char *slash, *dot;
	fd2path(fd, buf, 512);
	slash = strrchr(buf, '/');
	dot = strrchr(buf, '.');
	*dot = '\0';
	id->title = runesmprint("%s", slash+1);
}

/* The following function is stolen from https://git.sr.ht/~ft/libtags/ */
#define synchsafe(d) (uint)(((d)[0]&127)<<21 | ((d)[1]&127)<<14 | ((d)[2]&127)<<7 | ((d)[3]&127)<<0)
#define beuint(d) (uint)((d)[0]<<24 | (d)[1]<<16 | (d)[2]<<8 | (d)[3]<<0)

ID3v1*
readid3v2(int fd)
{
	int sz, framesz;
	int ver;
	char nu[8];
	uchar d[10];
	uchar buf[256];
	ID3v1 *id;

	read(fd, d, sizeof d);

	ver = d[3];
	sz = synchsafe(&d[6]);

	if(ver == 2 && (d[5] & (1<<6)) != 0) /* compression */
		return nil;

	if(ver > 2){
		if((d[5] & (1<<4)) != 0) /* footer */
			sz -= 10;
		if((d[5] & (1<<6)) != 0){ /* we don't do extended header */
			return nil;
		}
	}

	framesz = (ver >= 3) ? 10 : 6;

	id = emalloc(sizeof(ID3v1));
	id->title = id->album = nil;
	for(; sz > framesz;){
		int tsz;

		read(fd, d, framesz);
		sz -= framesz;

		if(memcmp(d, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", framesz) == 0)
			break;

		if(ver >= 3){
			tsz = (ver == 3) ? beuint(&d[4]) : synchsafe(&d[4]);
			if(tsz < 0 || tsz > sz)
				break;

			d[4] = 0;

			if((d[9] & 0x0c) != 0){ /* compression & encryption */
				return nil;
			}
		}else{
			tsz = beuint(&d[3]) >> 8;
			if(tsz > sz)
				return nil;
			d[3] = 0;
		}
		sz -= tsz;

		if(memcmp(d, "TIT2", 4) == 0){
			read(fd, nu, 1);
			switch(nu[0]){
			case 3:
				id->title = emalloc(sizeof(Rune)*tsz);
				read(fd, buf, tsz-1);
				runesprint(id->title, "%s", (char*)buf);
			}
		}else if(memcmp(d, "TALB", 4) == 0){
			read(fd, nu, 1);
			switch(nu[0]){
			case 3:
				id->album = emalloc(sizeof(Rune)*tsz);
				read(fd, buf, tsz-1);
				runesprint(id->album, "%s", (char*)buf);
			}
		}
	}

	if(id->title == nil || id->album == nil)
		return nil;

	return id;
}

ID3v1*
readid3(int fd)
{
	Dir *d;
	ID3v1 *id;
	char buf[128];
	char fieldbuf[31];
	int length;

	d = dirfstat(fd);
	if(d == nil)
		return nil;
	length = d->length;
	free(d);

	if(pread(fd, buf, 128, length-128) != 128)
		return nil;

	id = emalloc(sizeof(ID3v1));
	if(memcmp(buf, "TAG", 3) != 0){
		fillnotag(id, fd);
		return id;
	}

	memcpy(fieldbuf, buf+3, 30);
	fieldbuf[30] = '\0';
	id->title = emalloc(sizeof(Rune) * 31);
	runesprint(id->title, "%s", fieldbuf);

	memcpy(fieldbuf, buf+33, 30);
	id->artist = emalloc(sizeof(Rune) * 31);
	runesprint(id->artist, "%s", fieldbuf);

	memcpy(fieldbuf, buf+63, 30);
	id->album = emalloc(sizeof(Rune) * 31);
	runesprint(id->album, "%s", fieldbuf);

	memcpy(fieldbuf, buf+93, 4);
	fieldbuf[4] = '\0';
	id->year = atoi(fieldbuf);

	memcpy(fieldbuf, buf+97, 30);
	fieldbuf[30] = '\0';
	id->comment = emalloc(sizeof(Rune) * 31);
	runesprint(id->comment, "%s", fieldbuf);

	id->genre = buf[127];
	
	return id;
}

void
destroyid3(ID3v1 *id)
{
	free(id->title);
	free(id->artist);
	free(id->album);
	free(id->comment);
	free(id);
}