109
|
1 #include <stdlib.h>
|
|
2 #include <stdio.h>
|
|
3 #include <string.h>
|
|
4 #include <unistd.h>
|
|
5 #include <fcntl.h>
|
|
6 #include <wchar.h>
|
|
7 #include <audacious/util.h>
|
|
8 #include "tags.h"
|
|
9
|
|
10 struct APETagFooterStruct {
|
|
11 unsigned char ID[8];
|
|
12 unsigned char Version[4];
|
|
13 unsigned char Length[4];
|
|
14 unsigned char TagCount[4];
|
|
15 unsigned char Flags[4];
|
|
16 unsigned char Reserved[8];
|
|
17 };
|
|
18
|
|
19 typedef struct {
|
|
20 char *key;
|
|
21 size_t keylen;
|
|
22 unsigned char *value;
|
|
23 size_t valuelen;
|
|
24 unsigned int flags;
|
|
25 } TagItem;
|
|
26
|
|
27 unsigned long
|
|
28 Read_LE_Uint32(const unsigned char *p)
|
|
29 {
|
|
30 return ((unsigned long) p[0] << 0) |
|
|
31 ((unsigned long) p[1] << 8) |
|
|
32 ((unsigned long) p[2] << 16) | ((unsigned long) p[3] << 24);
|
|
33 }
|
|
34
|
|
35 // Convert UTF-8 coded string to UNICODE
|
|
36 // Return number of characters converted
|
|
37 int
|
|
38 utf8ToUnicode(const char *lpMultiByteStr, wchar_t * lpWideCharStr,
|
|
39 int cmbChars)
|
|
40 {
|
|
41 const unsigned char *pmb = (unsigned char *) lpMultiByteStr;
|
|
42 unsigned short *pwc = (unsigned short *) lpWideCharStr;
|
|
43 const unsigned char *pmbe;
|
|
44 size_t cwChars = 0;
|
|
45
|
|
46 if (cmbChars >= 0) {
|
|
47 pmbe = pmb + cmbChars;
|
|
48 }
|
|
49 else {
|
|
50 pmbe = NULL;
|
|
51 }
|
|
52
|
|
53 while ((pmbe == NULL) || (pmb < pmbe)) {
|
|
54 char mb = *pmb++;
|
|
55 unsigned int cc = 0;
|
|
56 unsigned int wc;
|
|
57
|
|
58 while ((cc < 7) && (mb & (1 << (7 - cc)))) {
|
|
59 cc++;
|
|
60 }
|
|
61
|
|
62 if (cc == 1 || cc > 6) // illegal character combination for UTF-8
|
|
63 continue;
|
|
64
|
|
65 if (cc == 0) {
|
|
66 wc = mb;
|
|
67 }
|
|
68 else {
|
|
69 wc = (mb & ((1 << (7 - cc)) - 1)) << ((cc - 1) * 6);
|
|
70 while (--cc > 0) {
|
|
71 if (pmb == pmbe) // reached end of the buffer
|
|
72 return cwChars;
|
|
73 mb = *pmb++;
|
|
74 if (((mb >> 6) & 0x03) != 2) // not part of multibyte character
|
|
75 return cwChars;
|
|
76 wc |= (mb & 0x3F) << ((cc - 1) * 6);
|
|
77 }
|
|
78 }
|
|
79
|
|
80 if (wc & 0xFFFF0000)
|
|
81 wc = L'?';
|
|
82 *pwc++ = wc;
|
|
83 cwChars++;
|
|
84 if (wc == L'\0')
|
|
85 return cwChars;
|
|
86 }
|
|
87
|
|
88 return cwChars;
|
|
89 }
|
|
90
|
|
91 void
|
|
92 tag_insert(char *buffer, const char *value, long unsigned int len,
|
|
93 long unsigned int maxlen, bool decode_utf8)
|
|
94 {
|
|
95 char *p;
|
|
96 wchar_t wValue[MAX_LEN];
|
|
97 char temp[MAX_LEN];
|
|
98 long unsigned int c;
|
|
99 const wchar_t *src = wValue;
|
|
100
|
|
101 if (len >= maxlen)
|
|
102 len = maxlen - 1;
|
|
103 if (decode_utf8) {
|
|
104 if ((c = utf8ToUnicode(value, wValue, len)) <= 0)
|
|
105 return;
|
|
106 if (wValue[c] != L'\0')
|
|
107 wValue[c++] = L'\0';
|
|
108 if ((c = wcsrtombs(temp, &src, MAX_LEN, NULL)) == 0)
|
|
109 return;
|
|
110 }
|
|
111 else {
|
|
112 c = len;
|
|
113 strncpy(temp, value, len);
|
|
114 while (temp[len - 1] == 0x20 || len < 1) {
|
|
115 len--;
|
|
116 }
|
|
117 temp[len] = '\0';
|
|
118 }
|
|
119
|
|
120 //if ( *buffer == '\0' ) { // new value
|
|
121 p = buffer;
|
|
122 //} else { // append to existing value
|
|
123 // p = strchr (buffer, '\0' );
|
|
124 // p += sprintf ( p, ", " );
|
|
125 //}
|
|
126
|
|
127 if ((p - buffer) + c >= maxlen)
|
|
128 c = maxlen - (p - buffer) - 1;
|
|
129 strncpy(p, temp, c);
|
|
130 p[c] = '\0';
|
|
131 }
|
|
132
|
|
133 // Returns the Type of Tag (Ape or ID3)
|
|
134 int
|
|
135 GetTageType(FILE * fp)
|
|
136 {
|
|
137 struct APETagFooterStruct T;
|
|
138 unsigned char tagheader[3];
|
|
139 int size;
|
|
140
|
|
141 if (fp == NULL) {
|
|
142 return TAG_NONE;
|
|
143 }
|
|
144
|
|
145 if (fseek(fp, 0, SEEK_END) != 0)
|
|
146 return TAG_NONE;
|
|
147 size = ftell(fp);
|
|
148 if (fseek(fp, size - sizeof T, SEEK_SET) != 0)
|
|
149 return TAG_NONE;
|
|
150 if (fread(&T, 1, sizeof T, fp) != sizeof T)
|
|
151 return TAG_NONE;
|
|
152 if (memcmp(T.ID, "APETAGEX", sizeof T.ID) == 0)
|
|
153 return TAG_APE;
|
|
154 if (fseek(fp, -128L, SEEK_END) != 0)
|
|
155 return TAG_NONE;
|
|
156 if (fread(tagheader, 1, 3, fp) != 3)
|
|
157 return TAG_NONE;
|
|
158 if (0 == memcmp(tagheader, "TAG", 3))
|
|
159 return TAG_ID3;
|
|
160 return TAG_NONE;
|
|
161 }
|
|
162
|
|
163
|
|
164 int
|
|
165 ReadID3Tag(FILE * fp, ape_tag * Tag)
|
|
166 {
|
|
167 char *tag;
|
|
168 char *buff;
|
|
169 unsigned int genre;
|
|
170
|
|
171 buff = (char *) malloc(128);
|
|
172
|
|
173 *(Tag->title) = '\0';
|
|
174 *(Tag->artist) = '\0';
|
|
175 *(Tag->album) = '\0';
|
|
176 *(Tag->comment) = '\0';
|
|
177 *(Tag->genre) = '\0';
|
|
178 *(Tag->track) = '\0';
|
|
179 *(Tag->year) = '\0';
|
|
180
|
|
181 if (fseek(fp, -128L, SEEK_END) != 0)
|
|
182 return 0;
|
|
183 if (fread(buff, 1, 128, fp) != 128)
|
|
184 return 0;
|
|
185 tag = buff;
|
|
186 tag_insert(Tag->title, (tag + 3), 30, 32, false);
|
|
187 tag_insert(Tag->artist, (tag + 33), 30, 32, false);
|
|
188 tag_insert(Tag->album, (tag + 63), 30, 32, false);
|
|
189 tag_insert(Tag->year, (tag + 93), 4, 32, false);
|
|
190 tag_insert(Tag->comment, (tag + 97), 30, 32, false);
|
|
191 genre = (unsigned char) tag[127];
|
|
192 if (genre >= sizeof(GenreList) / sizeof(int))
|
|
193 genre = 12;
|
|
194 tag_insert(Tag->genre, GenreList[genre], 30, 32, false);
|
|
195 sprintf(tag, "%u", tag[126]);
|
|
196 tag_insert(Tag->track, tag, 30, 32, false);
|
|
197 free(buff);
|
|
198 return 1;
|
|
199 }
|
|
200
|
|
201 // Reads APE v2.0 tag
|
|
202 int
|
|
203 ReadAPE2Tag(FILE * fp, ape_tag * Tag)
|
|
204 {
|
|
205 unsigned long vsize;
|
|
206 unsigned long isize;
|
|
207 unsigned long flags;
|
|
208 unsigned char *buff;
|
|
209 unsigned char *p;
|
|
210 unsigned char *end;
|
|
211 struct APETagFooterStruct T;
|
|
212 unsigned long TagLen;
|
|
213 unsigned long TagCount;
|
|
214 long size;
|
|
215
|
|
216 *(Tag->title) = '\0';
|
|
217 *(Tag->artist) = '\0';
|
|
218 *(Tag->album) = '\0';
|
|
219 *(Tag->comment) = '\0';
|
|
220 *(Tag->genre) = '\0';
|
|
221 *(Tag->track) = '\0';
|
|
222 *(Tag->year) = '\0';
|
|
223
|
|
224 if (fseek(fp, 0, SEEK_END) != 0)
|
|
225 return 0;
|
|
226 size = ftell(fp);
|
|
227 if (fseek(fp, size - sizeof T, SEEK_SET) != 0)
|
|
228 return 0;
|
|
229 if (fread(&T, 1, sizeof T, fp) != sizeof T)
|
|
230 return 0;
|
|
231 if (memcmp(T.ID, "APETAGEX", sizeof T.ID) != 0)
|
|
232 return 0;
|
|
233 if (Read_LE_Uint32(T.Version) != 2000)
|
|
234 return 0;
|
|
235 TagLen = Read_LE_Uint32(T.Length);
|
|
236 if (TagLen < sizeof T)
|
|
237 return 0;
|
|
238 if (fseek(fp, size - TagLen, SEEK_SET) != 0)
|
|
239 return 0;
|
|
240 if ((buff = (unsigned char *) malloc(TagLen)) == NULL)
|
|
241 return 0;
|
|
242 if (fread(buff, 1, TagLen - sizeof T, fp) != TagLen - sizeof T) {
|
|
243 free(buff);
|
|
244 return 0;
|
|
245 }
|
|
246
|
|
247 TagCount = Read_LE_Uint32(T.TagCount);
|
|
248 end = buff + TagLen - sizeof(T);
|
|
249 for (p = buff; p < end && TagCount--;) {
|
|
250 vsize = Read_LE_Uint32(p);
|
|
251 p += 4;
|
|
252 flags = Read_LE_Uint32(p);
|
|
253 p += 4;
|
|
254 isize = strlen((char *) p);
|
|
255
|
|
256 if (isize > 0 && vsize > 0) {
|
|
257 if (!(flags & 1 << 1)) { // insert UTF-8 string (skip binary values)
|
|
258 if (!strcasecmp((char *) p, "Title")) {
|
|
259 tag_insert(Tag->title, (char *) (p + isize + 1), vsize,
|
|
260 MAX_LEN, false);
|
|
261 }
|
|
262 else if (!strcasecmp((char *) p, "Artist")) {
|
|
263 tag_insert(Tag->artist, (char *) (p + isize + 1), vsize,
|
|
264 MAX_LEN, false);
|
|
265 }
|
|
266 else if (!strcasecmp((char *) p, "Album")) {
|
|
267 tag_insert(Tag->album, (char *) (p + isize + 1), vsize,
|
|
268 MAX_LEN, false);
|
|
269 }
|
|
270 else if (!strcasecmp((char *) p, "Comment")) {
|
|
271 tag_insert(Tag->comment, (char *) (p + isize + 1), vsize,
|
|
272 MAX_LEN, false);
|
|
273 }
|
|
274 else if (!strcasecmp((char *) p, "Genre")) {
|
|
275 tag_insert(Tag->genre, (char *) (p + isize + 1), vsize,
|
|
276 MAX_LEN, false);
|
|
277 }
|
|
278 else if (!strcasecmp((char *) p, "Track")) {
|
|
279 tag_insert(Tag->track, (char *) (p + isize + 1), vsize,
|
|
280 128, false);
|
|
281 }
|
|
282 else if (!strcasecmp((char *) p, "Year")) {
|
|
283 tag_insert(Tag->year, (char *) (p + isize + 1), vsize,
|
|
284 128, false);
|
|
285 }
|
|
286 }
|
|
287 }
|
|
288 p += isize + 1 + vsize;
|
|
289 }
|
|
290 free(buff);
|
|
291 return 1;
|
|
292 }
|
|
293
|
|
294 int
|
|
295 DeleteTag(char *filename)
|
|
296 {
|
|
297
|
|
298 FILE *fp = fopen(filename, "rb+");
|
|
299 int tagtype;
|
|
300 int fd;
|
|
301 long filelength = 0;
|
|
302 long dellength = -1;
|
|
303 char *tagheader;
|
|
304 unsigned long *apelength;
|
|
305 int res = -1;
|
|
306
|
|
307 if (fp == NULL) {
|
|
308 char text[256];
|
|
309
|
|
310 sprintf(text, "File \"%s\" not found or is read protected!\n",
|
|
311 filename);
|
|
312 xmms_show_message("File-Error", (gchar *) text, "Ok", FALSE, NULL,
|
|
313 NULL);
|
|
314 return -1;
|
|
315 }
|
|
316 tagtype = GetTageType(fp);
|
|
317
|
|
318 // get Length of File
|
|
319 fseek(fp, 0L, SEEK_END);
|
|
320 filelength = ftell(fp);
|
|
321
|
|
322 apelength = (unsigned long *) malloc(4);
|
|
323 tagheader = (char *) malloc(9);
|
|
324
|
|
325 if (tagtype == TAG_ID3) {
|
|
326 dellength = 128L;
|
|
327 }
|
|
328 else if (tagtype == TAG_APE) {
|
|
329 fseek(fp, -32L, SEEK_END);
|
|
330 fread(tagheader, 8, 1, fp);
|
|
331 if (0 == memcmp(tagheader, "APETAGEX", 8)) {
|
|
332 fseek(fp, -20L, SEEK_END);
|
|
333 fread(apelength, 4, 1, fp);
|
|
334 dellength = *apelength + 32;
|
|
335 }
|
|
336 }
|
|
337
|
|
338
|
|
339 if (dellength > -1) //if TAG was found, delete it
|
|
340 {
|
|
341 fd = open(filename, O_RDWR);
|
|
342 res = ftruncate(fd, (off_t) (filelength - dellength));
|
|
343 close(fd);
|
|
344 }
|
|
345
|
|
346 free(tagheader);
|
|
347 free(apelength);
|
|
348
|
|
349 //returns 0 if everything is ok
|
|
350 return res;
|
|
351 }
|
|
352
|
|
353 // Returns bytes used in APE-Tag for this value
|
|
354 int
|
|
355 addValue(TagItem * item, char *key, char *value)
|
|
356 {
|
|
357 item->keylen = strlen(key);
|
|
358 item->valuelen = strlen(value);
|
|
359 item->key = (char *) malloc(item->keylen + 1);
|
|
360 item->value = (unsigned char *) malloc(item->valuelen + 1);
|
|
361 strcpy((char *) item->value, value);
|
|
362 strcpy(item->key, key);
|
|
363 item->flags = 0;
|
|
364 return (9 + item->keylen + item->valuelen);
|
|
365 }
|
|
366
|
|
367 int
|
|
368 WriteAPE2Tag(char *filename, ape_tag * Tag)
|
|
369 {
|
|
370 FILE *fp;
|
|
371 unsigned char H[32] = "APETAGEX";
|
|
372 unsigned long Version = 2000;
|
|
373 unsigned char dw[8];
|
|
374 unsigned long estimatedbytes = 32; // 32 byte footer + all items, these are the 32 bytes footer, the items are added later
|
|
375 long writtenbytes = -32; // actually writtenbytes-32, which should be equal to estimatedbytes (= footer + all items)
|
|
376 unsigned int TagCount = 0;
|
|
377 TagItem T[7];
|
|
378
|
|
379
|
|
380 // Delete Tag if there is one
|
|
381 fp = fopen(filename, "rb+");
|
|
382 if (fp == NULL) {
|
|
383 char text[256];
|
|
384
|
|
385 sprintf(text, "File \"%s\" not found or is read protected!\n",
|
|
386 filename);
|
|
387 xmms_show_message("File-Error", (gchar *) text, "Ok", FALSE, NULL,
|
|
388 NULL);
|
|
389 return -1;
|
|
390 }
|
|
391
|
|
392 int tagtype = GetTageType(fp);
|
|
393
|
|
394 if (tagtype != TAG_NONE)
|
|
395 if (DeleteTag(filename) != 0)
|
|
396 return 0;
|
|
397
|
|
398 // Produce TagItem-Array
|
|
399 if (strlen(Tag->title) > 0) {
|
|
400 char *value = (char *) malloc(strlen(Tag->title) + 1);
|
|
401
|
|
402 strcpy(value, Tag->title);
|
|
403 int res = addValue(&T[TagCount], "Title", value);
|
|
404
|
|
405 estimatedbytes += res;
|
|
406 if (res > 0)
|
|
407 TagCount++;
|
|
408 free(value);
|
|
409 }
|
|
410
|
|
411 if (strlen(Tag->artist) > 0) {
|
|
412 char *value = (char *) malloc(strlen(Tag->artist) + 1);
|
|
413
|
|
414 strcpy(value, Tag->artist);
|
|
415 int res = addValue(&T[TagCount], "Artist", value);
|
|
416
|
|
417 estimatedbytes += res;
|
|
418 if (res > 0)
|
|
419 TagCount++;
|
|
420 free(value);
|
|
421 }
|
|
422
|
|
423 if (strlen(Tag->album) > 0) {
|
|
424 char *value = (char *) malloc(strlen(Tag->album) + 1);
|
|
425
|
|
426 strcpy(value, Tag->album);
|
|
427 int res = addValue(&T[TagCount], "Album", value);
|
|
428
|
|
429 estimatedbytes += res;
|
|
430 if (res > 0)
|
|
431 TagCount++;
|
|
432 free(value);
|
|
433 }
|
|
434
|
|
435 if (strlen(Tag->comment) > 0) {
|
|
436 char *value = (char *) malloc(strlen(Tag->comment) + 1);
|
|
437
|
|
438 strcpy(value, Tag->comment);
|
|
439 int res = addValue(&T[TagCount], "Comment", value);
|
|
440
|
|
441 estimatedbytes += res;
|
|
442 if (res > 0)
|
|
443 TagCount++;
|
|
444 free(value);
|
|
445 }
|
|
446
|
|
447 if (strlen(Tag->genre) > 0) {
|
|
448 char *value = (char *) malloc(strlen(Tag->genre) + 1);
|
|
449
|
|
450 strcpy(value, Tag->genre);
|
|
451 int res = addValue(&T[TagCount], "Genre", value);
|
|
452
|
|
453 estimatedbytes += res;
|
|
454 if (res > 0)
|
|
455 TagCount++;
|
|
456 free(value);
|
|
457 }
|
|
458
|
|
459 if (strlen(Tag->track) > 0) {
|
|
460 char *value = (char *) malloc(strlen(Tag->track) + 1);
|
|
461
|
|
462 strcpy(value, Tag->track);
|
|
463 int res = addValue(&T[TagCount], "Track", value);
|
|
464
|
|
465 estimatedbytes += res;
|
|
466 if (res > 0)
|
|
467 TagCount++;
|
|
468 free(value);
|
|
469 }
|
|
470
|
|
471 if (strlen(Tag->year) > 0) {
|
|
472 char *value = (char *) malloc(strlen(Tag->year) + 1);
|
|
473
|
|
474 strcpy(value, Tag->year);
|
|
475 int res = addValue(&T[TagCount], "Year", value);
|
|
476
|
|
477 estimatedbytes += res;
|
|
478 if (res > 0)
|
|
479 TagCount++;
|
|
480 free(value);
|
|
481 }
|
|
482 // Start writing the new Ape2 Tag
|
|
483 fseek(fp, 0L, SEEK_END);
|
|
484
|
|
485 if (TagCount == 0) {
|
|
486 printf("no tag to write");
|
|
487 return 0;
|
|
488 }
|
|
489
|
|
490 if (estimatedbytes >= 8192 + 103) {
|
|
491 printf
|
|
492 ("\nTag is %.1f Kbyte long. This is longer than the maximum recommended 8 KByte.\n\a",
|
|
493 estimatedbytes / 1024.);
|
|
494 return 0;
|
|
495 }
|
|
496
|
|
497 H[8] = Version >> 0;
|
|
498 H[9] = Version >> 8;
|
|
499 H[10] = Version >> 16;
|
|
500 H[11] = Version >> 24;
|
|
501 H[12] = estimatedbytes >> 0;
|
|
502 H[13] = estimatedbytes >> 8;
|
|
503 H[14] = estimatedbytes >> 16;
|
|
504 H[15] = estimatedbytes >> 24;
|
|
505 H[16] = TagCount >> 0;
|
|
506 H[17] = TagCount >> 8;
|
|
507 H[18] = TagCount >> 16;
|
|
508 H[19] = TagCount >> 24;
|
|
509
|
|
510 H[23] = 0x80 | 0x20;
|
|
511 writtenbytes += fwrite(H, 1, 32, fp);
|
|
512
|
|
513 for (unsigned int i = 0; i < TagCount; i++) {
|
|
514 dw[0] = T[i].valuelen >> 0;
|
|
515 dw[1] = T[i].valuelen >> 8;
|
|
516 dw[2] = T[i].valuelen >> 16;
|
|
517 dw[3] = T[i].valuelen >> 24;
|
|
518 dw[4] = T[i].flags >> 0;
|
|
519 dw[5] = T[i].flags >> 8;
|
|
520 dw[6] = T[i].flags >> 16;
|
|
521 dw[7] = T[i].flags >> 24;
|
|
522 writtenbytes += fwrite(dw, 1, 8, fp);
|
|
523 writtenbytes += fwrite(T[i].key, 1, T[i].keylen, fp);
|
|
524 writtenbytes += fwrite("", 1, 1, fp);
|
|
525 if (T[i].valuelen > 0)
|
|
526 writtenbytes += fwrite(T[i].value, 1, T[i].valuelen, fp);
|
|
527 }
|
|
528
|
|
529 H[23] = 0x80;
|
|
530 writtenbytes += fwrite(H, 1, 32, fp);
|
|
531
|
|
532 if (estimatedbytes != (unsigned long) writtenbytes)
|
|
533 printf("\nError writing APE tag.\n");
|
|
534 fclose(fp);
|
|
535 TagCount = 0;
|
|
536 return 0;
|
|
537 }
|