cleanup: Silence compilation warnings on MinGW-w64
[mplayer.git] / stream / stream_cddb.c
blobafc512e5b77e481055d60b4a9c4c4d0967900d34
1 /*
2 * CDDB HTTP protocol
4 * Copyright (C) 2002 Bertrand Baudet <bertrand_baudet@yahoo.com>
6 * Implementation follow the freedb.howto1.06.txt specification
7 * from http://freedb.freedb.org
9 * discid computation by Jeremy D. Zawodny
10 * Copyright (c) 1998-2000 Jeremy D. Zawodny <Jeremy@Zawodny.com>
12 * This file is part of MPlayer.
14 * MPlayer is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * MPlayer is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "config.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <stdarg.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <limits.h>
39 #if defined(__MINGW32__) || defined(__CYGWIN__)
40 #ifdef __MINGW32__
41 #include <path.h>
42 #define mkdir(a,b) mkdir(a)
43 #endif
44 #include <windows.h>
45 #if HAVE_WINSOCK2_H
46 #include <winsock2.h>
47 #endif
48 #else
49 #include <netdb.h>
50 #include <sys/ioctl.h>
51 #endif
52 #include <sys/types.h>
53 #include <sys/stat.h>
55 #include "mp_msg.h"
57 #if defined(__linux__)
58 #include <linux/cdrom.h>
59 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
60 #include <sys/cdio.h>
61 #elif defined(__MINGW32__) || defined(__CYGWIN__)
62 #include <ddk/ntddcdrm.h>
63 #elif defined(__bsdi__)
64 #include <dvd.h>
65 #elif defined(__APPLE__) || defined(__DARWIN__)
66 #include <IOKit/storage/IOCDTypes.h>
67 #include <IOKit/storage/IOCDMediaBSDClient.h>
68 #include "mpbswap.h"
69 #endif
71 #include "osdep/osdep.h"
73 #include "cdd.h"
74 #include "mpcommon.h"
75 #include "stream.h"
76 #include "network.h"
77 #include "libavutil/common.h"
79 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
80 #define DEFAULT_CACHE_DIR "/.cddb/"
82 typedef struct {
83 char cddb_hello[1024];
84 unsigned long disc_id;
85 unsigned int tracks;
86 char *cache_dir;
87 char *freedb_server;
88 int freedb_proto_level;
89 int anonymous;
90 char category[100];
91 char *xmcd_file;
92 size_t xmcd_file_size;
93 void *user_data;
94 } cddb_data_t;
96 typedef struct {
97 unsigned int min, sec, frame;
98 } cd_toc_t;
100 static cd_toc_t cdtoc[100];
101 static int cdtoc_last_track;
103 static int read_toc(const char *dev)
105 int first = 0, last = -1;
106 int i;
107 #if defined(__MINGW32__) || defined(__CYGWIN__)
108 HANDLE drive;
109 DWORD r;
110 CDROM_TOC toc;
111 char device[10];
113 sprintf(device, "\\\\.\\%s", dev);
114 drive = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
115 OPEN_EXISTING, 0, 0);
117 if (!DeviceIoControl(drive, IOCTL_CDROM_READ_TOC, NULL, 0, &toc,
118 sizeof(CDROM_TOC), &r, 0)) {
119 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
120 return 0;
123 first = toc.FirstTrack - 1; last = toc.LastTrack;
124 for (i = first; i <= last; i++) {
125 cdtoc[i].min = toc.TrackData[i].Address[1];
126 cdtoc[i].sec = toc.TrackData[i].Address[2];
127 cdtoc[i].frame = toc.TrackData[i].Address[3];
129 CloseHandle(drive);
131 #elif defined(__OS2__)
132 UCHAR auchParamDisk[4] = {'C', 'D', '0', '1'};
134 struct {
135 BYTE bFirstTrack;
136 BYTE bLastTrack;
137 BYTE bLeadOutF;
138 BYTE bLeadOutS;
139 BYTE bLeadOutM;
140 BYTE bLeadOutReserved;
141 } __attribute__((packed)) sDataDisk;
143 struct {
144 UCHAR auchSign[4];
145 BYTE bTrack;
146 } __attribute__((packed)) sParamTrack = {{'C', 'D', '0', '1'},};
148 struct {
149 BYTE bStartF;
150 BYTE bStartS;
151 BYTE bStartM;
152 BYTE bStartReserved;
153 BYTE bControlInfo;
154 } __attribute__((packed)) sDataTrack;
156 HFILE hcd;
157 ULONG ulAction;
158 ULONG ulParamLen;
159 ULONG ulDataLen;
160 ULONG rc;
162 rc = DosOpen(dev, &hcd, &ulAction, 0, FILE_NORMAL,
163 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
164 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
165 NULL);
166 if (rc) {
167 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
168 return -1;
171 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIODISK,
172 auchParamDisk, sizeof(auchParamDisk), &ulParamLen,
173 &sDataDisk, sizeof(sDataDisk), &ulDataLen);
174 if (!rc) {
175 first = sDataDisk.bFirstTrack - 1;
176 last = sDataDisk.bLastTrack;
177 for (i = first; i <= last; i++) {
178 if (i == last) {
179 sDataTrack.bStartM = sDataDisk.bLeadOutM;
180 sDataTrack.bStartS = sDataDisk.bLeadOutS;
181 sDataTrack.bStartF = sDataDisk.bLeadOutF;
182 } else {
183 sParamTrack.bTrack = i + 1;
184 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIOTRACK,
185 &sParamTrack, sizeof(sParamTrack), &ulParamLen,
186 &sDataTrack, sizeof(sDataTrack), &ulDataLen);
187 if (rc)
188 break;
191 cdtoc[i].min = sDataTrack.bStartM;
192 cdtoc[i].sec = sDataTrack.bStartS;
193 cdtoc[i].frame = sDataTrack.bStartF;
197 DosClose(hcd);
199 if (rc) {
200 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
201 return -1;
203 #else
204 int drive;
205 drive = open(dev, O_RDONLY | O_NONBLOCK);
206 if (drive < 0) {
207 return drive;
210 #if defined(__linux__) || defined(__bsdi__)
212 struct cdrom_tochdr tochdr;
213 ioctl(drive, CDROMREADTOCHDR, &tochdr);
214 first = tochdr.cdth_trk0 - 1; last = tochdr.cdth_trk1;
216 for (i = first; i <= last; i++) {
217 struct cdrom_tocentry tocentry;
218 tocentry.cdte_track = (i == last) ? 0xAA : i + 1;
219 tocentry.cdte_format = CDROM_MSF;
220 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
221 cdtoc[i].min = tocentry.cdte_addr.msf.minute;
222 cdtoc[i].sec = tocentry.cdte_addr.msf.second;
223 cdtoc[i].frame = tocentry.cdte_addr.msf.frame;
225 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
227 struct ioc_toc_header tochdr;
228 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
229 first = tochdr.starting_track - 1; last = tochdr.ending_track;
231 for (i = first; i <= last; i++) {
232 struct ioc_read_toc_single_entry tocentry;
233 tocentry.track = (i == last) ? 0xAA : i + 1;
234 tocentry.address_format = CD_MSF_FORMAT;
235 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
236 cdtoc[i].min = tocentry.entry.addr.msf.minute;
237 cdtoc[i].sec = tocentry.entry.addr.msf.second;
238 cdtoc[i].frame = tocentry.entry.addr.msf.frame;
240 #elif defined(__NetBSD__) || defined(__OpenBSD__)
242 struct ioc_toc_header tochdr;
243 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
244 first = tochdr.starting_track - 1; last = tochdr.ending_track;
246 for (i = first; i <= last; i++) {
247 struct ioc_read_toc_entry tocentry;
248 struct cd_toc_entry toc_buffer;
249 tocentry.starting_track = (i == last) ? 0xAA : i + 1;
250 tocentry.address_format = CD_MSF_FORMAT;
251 tocentry.data = &toc_buffer;
252 tocentry.data_len = sizeof(toc_buffer);
253 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
254 cdtoc[i].min = toc_buffer.addr.msf.minute;
255 cdtoc[i].sec = toc_buffer.addr.msf.second;
256 cdtoc[i].frame = toc_buffer.addr.msf.frame;
258 #elif defined(__APPLE__) || defined(__DARWIN__)
260 dk_cd_read_toc_t tochdr;
261 uint8_t buf[4];
262 uint8_t buf2[100 * sizeof(CDTOCDescriptor) + sizeof(CDTOC)];
263 memset(&tochdr, 0, sizeof(tochdr));
264 tochdr.bufferLength = sizeof(buf);
265 tochdr.buffer = &buf;
266 if (!ioctl(drive, DKIOCCDREADTOC, &tochdr)
267 && tochdr.bufferLength == sizeof(buf)) {
268 first = buf[2] - 1;
269 last = buf[3];
271 if (last >= 0) {
272 memset(&tochdr, 0, sizeof(tochdr));
273 tochdr.bufferLength = sizeof(buf2);
274 tochdr.buffer = &buf2;
275 tochdr.format = kCDTOCFormatTOC;
276 if (ioctl(drive, DKIOCCDREADTOC, &tochdr)
277 || tochdr.bufferLength < sizeof(CDTOC))
278 last = -1;
280 if (last >= 0) {
281 CDTOC *cdToc = (CDTOC *)buf2;
282 CDTrackInfo lastTrack;
283 dk_cd_read_track_info_t trackInfoParams;
284 for (i = first; i < last; ++i) {
285 CDMSF msf = CDConvertTrackNumberToMSF(i + 1, cdToc);
286 cdtoc[i].min = msf.minute;
287 cdtoc[i].sec = msf.second;
288 cdtoc[i].frame = msf.frame;
290 memset(&trackInfoParams, 0, sizeof(trackInfoParams));
291 trackInfoParams.addressType = kCDTrackInfoAddressTypeTrackNumber;
292 trackInfoParams.bufferLength = sizeof(lastTrack);
293 trackInfoParams.address = last;
294 trackInfoParams.buffer = &lastTrack;
295 if (!ioctl(drive, DKIOCCDREADTRACKINFO, &trackInfoParams)) {
296 CDMSF msf = CDConvertLBAToMSF(be2me_32(lastTrack.trackStartAddress)
297 + be2me_32(lastTrack.trackSize));
298 cdtoc[last].min = msf.minute;
299 cdtoc[last].sec = msf.second;
300 cdtoc[last].frame = msf.frame;
304 #endif
305 close(drive);
306 #endif
307 for (i = first; i <= last; i++)
308 cdtoc[i].frame += (cdtoc[i].min * 60 + cdtoc[i].sec) * 75;
309 return last;
313 \brief Reads TOC from CD in the given device and prints the number of tracks
314 and the length of each track in minute:second:frame format.
315 \param *dev the device to analyse
316 \return if the command line -identify is given, returns the last track of
317 the TOC or -1 if the TOC can't be read,
318 otherwise just returns 0 and let cddb_resolve the TOC
320 int cdd_identify(const char *dev)
322 cdtoc_last_track = 0;
323 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
324 int i, min, sec, frame;
325 cdtoc_last_track = read_toc(dev);
326 if (cdtoc_last_track < 0) {
327 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
328 return -1;
330 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track);
331 for (i = 1; i <= cdtoc_last_track; i++) {
332 frame = cdtoc[i].frame - cdtoc[i-1].frame;
333 sec = frame / 75;
334 frame -= sec * 75;
335 min = sec / 60;
336 sec -= min * 60;
337 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
338 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i, min, sec, frame);
341 return cdtoc_last_track;
344 static unsigned int cddb_sum(int n)
346 unsigned int ret;
348 ret = 0;
349 while (n > 0) {
350 ret += (n % 10);
351 n /= 10;
353 return ret;
356 static unsigned long cddb_discid(int tot_trks)
358 unsigned int i, t = 0, n = 0;
360 i = 0;
361 while (i < (unsigned int)tot_trks) {
362 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
363 i++;
365 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
366 ((cdtoc[0].min * 60) + cdtoc[0].sec);
367 return (n % 0xff) << 24 | t << 8 | tot_trks;
372 static int cddb_http_request(char *command,
373 int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
374 cddb_data_t *cddb_data)
376 char request[4096];
377 int fd, ret = 0;
378 URL_t *url;
379 HTTP_header_t *http_hdr;
381 if (reply_parser == NULL || command == NULL || cddb_data == NULL)
382 return -1;
384 sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
385 cddb_data->freedb_server, command, cddb_data->cddb_hello,
386 cddb_data->freedb_proto_level);
387 mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);
389 url = url_new(request);
390 if (url == NULL) {
391 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n");
392 return -1;
395 fd = http_send_request(url,0);
396 if (fd < 0) {
397 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n");
398 return -1;
401 http_hdr = http_read_response(fd);
402 if (http_hdr == NULL) {
403 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n");
404 return -1;
407 http_debug_hdr(http_hdr);
408 mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);
410 switch (http_hdr->status_code) {
411 case 200:
412 ret = reply_parser(http_hdr, cddb_data);
413 break;
414 case 400:
415 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n");
416 break;
417 default:
418 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n");
421 http_free(http_hdr);
422 url_free(url);
424 return ret;
427 static int cddb_read_cache(cddb_data_t *cddb_data)
429 char file_name[100];
430 struct stat stats;
431 int file_fd, ret;
432 size_t file_size;
434 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
435 return -1;
437 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
439 file_fd = open(file_name, O_RDONLY | O_BINARY);
440 if (file_fd < 0) {
441 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No cache found.\n");
442 return -1;
445 ret = fstat(file_fd, &stats);
446 if (ret < 0) {
447 perror("fstat");
448 file_size = 4096;
449 } else {
450 file_size = stats.st_size < UINT_MAX ? stats.st_size : UINT_MAX - 1;
453 cddb_data->xmcd_file = malloc(file_size + 1);
454 if (cddb_data->xmcd_file == NULL) {
455 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
456 close(file_fd);
457 return -1;
459 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
460 if (cddb_data->xmcd_file_size != file_size) {
461 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all the xmcd file has been read.\n");
462 close(file_fd);
463 return -1;
465 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = 0;
467 close(file_fd);
469 return 0;
472 static int cddb_write_cache(cddb_data_t *cddb_data)
474 // We have the file, save it for cache.
475 struct stat file_stat;
476 char file_name[100];
477 int file_fd, ret;
478 int wrote = 0;
480 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
481 return -1;
483 // Check if the CDDB cache dir exist
484 ret = stat(cddb_data->cache_dir, &file_stat);
485 if (ret < 0) {
486 // Directory not present, create it.
487 ret = mkdir(cddb_data->cache_dir, 0755);
488 #ifdef __MINGW32__
489 if (ret < 0 && errno != EEXIST) {
490 #else
491 if (ret < 0) {
492 #endif
493 perror("mkdir");
494 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to create directory %s.\n",
495 cddb_data->cache_dir);
496 return -1;
500 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
502 file_fd = creat(file_name, S_IRUSR | S_IWUSR);
503 if (file_fd < 0) {
504 perror("create");
505 return -1;
508 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
509 if (wrote < 0) {
510 perror("write");
511 close(file_fd);
512 return -1;
514 if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
515 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all of the xmcd file has been written.\n");
516 close(file_fd);
517 return -1;
520 close(file_fd);
522 return 0;
525 static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
527 unsigned long disc_id;
528 char category[100];
529 char *ptr = NULL, *ptr2 = NULL;
530 int ret, status;
532 if (http_hdr == NULL || cddb_data == NULL)
533 return -1;
535 ret = sscanf(http_hdr->body, "%d ", &status);
536 if (ret != 1) {
537 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
538 return -1;
541 switch (status) {
542 case 210:
543 ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
544 category, &disc_id);
545 if (ret != 3) {
546 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
547 return -1;
549 // Check if it's a xmcd database file
550 ptr = strstr(http_hdr->body, "# xmcd");
551 if (ptr == NULL) {
552 mp_tmsg(MSGT_DEMUX, MSGL_ERR,
553 "Invalid xmcd database file returned.\n");
554 return -1;
556 ptr = strdup(ptr);
557 // Ok found the beginning of the file
558 // look for the end
559 ptr2 = strstr(ptr, "\n.\r\n");
560 if (!ptr2)
561 ptr2 = strstr(ptr, "\n.\n");
562 if (ptr2) {
563 ptr2++;
564 } else {
565 mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
566 ptr2 = ptr + strlen(ptr); //return -1;
568 // Ok found the end
569 // do a sanity check
570 if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
571 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unexpected FIXME\n");
572 return -1;
574 cddb_data->xmcd_file = ptr;
575 cddb_data->xmcd_file_size = ptr2 - ptr;
576 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
577 return cddb_write_cache(cddb_data);
578 default:
579 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
581 return 0;
584 static int cddb_request_titles(cddb_data_t *cddb_data)
586 char command[1024];
587 sprintf(command, "cddb+read+%s+%08lx",
588 cddb_data->category, cddb_data->disc_id);
589 return cddb_http_request(command, cddb_read_parse, cddb_data);
592 static int cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
594 char album_title[100];
595 char *ptr = NULL;
596 int ret;
598 ptr = strstr(http_hdr->body, "\n");
599 if (ptr == NULL) {
600 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Unable to find end of line.\n");
601 return -1;
603 ptr++;
604 // We have a list of exact/inexact matches, so which one do we use?
605 // So let's take the first one.
606 ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category,
607 &(cddb_data->disc_id), album_title);
608 if (ret != 3) {
609 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
610 return -1;
612 ptr = strstr(http_hdr->body, album_title);
613 if (ptr != NULL) {
614 char *ptr2;
615 int len;
616 ptr2 = strstr(ptr, "\n");
617 if (ptr2 == NULL) {
618 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
619 } else {
620 len = ptr2-ptr+1;
622 len = FFMIN(sizeof(album_title) - 1, len);
623 strncpy(album_title, ptr, len);
624 album_title[len]='\0';
626 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
627 return 0;
630 static int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
632 char album_title[100];
633 char *ptr = NULL;
634 int ret, status;
636 ret = sscanf(http_hdr->body, "%d ", &status);
637 if (ret != 1) {
638 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
639 return -1;
642 switch (status) {
643 case 200:
644 // Found exact match
645 ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status,
646 cddb_data->category, &(cddb_data->disc_id), album_title);
647 if (ret != 4) {
648 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
649 return -1;
651 ptr = strstr(http_hdr->body, album_title);
652 if (ptr != NULL) {
653 char *ptr2;
654 int len;
655 ptr2 = strstr(ptr, "\n");
656 if (ptr2 == NULL) {
657 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
658 } else {
659 len = ptr2-ptr+1;
661 len = FFMIN(sizeof(album_title) - 1, len);
662 strncpy(album_title, ptr, len);
663 album_title[len]='\0';
665 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
666 return cddb_request_titles(cddb_data);
667 case 202:
668 // No match found
669 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Album not found.\n");
670 break;
671 case 210:
672 // Found exact matches, list follows
673 cddb_parse_matches_list(http_hdr, cddb_data);
674 return cddb_request_titles(cddb_data);
676 body=[210 Found exact matches, list follows (until terminating `.')
677 misc c711930d Santana / Supernatural
678 rock c711930d Santana / Supernatural
679 blues c711930d Santana / Supernatural
682 case 211:
683 // Found inexact matches, list follows
684 cddb_parse_matches_list(http_hdr, cddb_data);
685 return cddb_request_titles(cddb_data);
686 case 500:
687 mp_tmsg(MSGT_DEMUX, MSGL_FIXME,
688 "Server returns: Command syntax error\n");
689 break;
690 default:
691 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
693 return -1;
696 static int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
698 int max;
699 int ret, status;
700 char *ptr;
702 ret = sscanf(http_hdr->body, "%d ", &status);
703 if (ret != 1) {
704 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
705 return -1;
708 switch (status) {
709 case 210:
710 ptr = strstr(http_hdr->body, "max proto:");
711 if (ptr == NULL) {
712 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
713 return -1;
715 ret = sscanf(ptr, "max proto: %d", &max);
716 if (ret != 1) {
717 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
718 return -1;
720 cddb_data->freedb_proto_level = max;
721 return 0;
722 default:
723 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
725 return -1;
728 static int cddb_get_proto_level(cddb_data_t *cddb_data)
730 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
733 static void cddb_create_hello(cddb_data_t *cddb_data)
735 char host_name[51];
736 char *user_name;
738 if (cddb_data->anonymous) { // Default is anonymous
739 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
740 * We don't send current user/host name in hello to prevent spam.
741 * Software that sends this is considered spyware
742 * that most people don't like.
744 user_name = "anonymous";
745 strcpy(host_name, "localhost");
746 } else {
747 if (gethostname(host_name, 50) < 0) {
748 strcpy(host_name, "localhost");
750 user_name = getenv("LOGNAME");
752 sprintf(cddb_data->cddb_hello, "&hello=%s+%s+%s",
753 user_name, host_name, mplayer_version);
756 static int cddb_retrieve(cddb_data_t *cddb_data)
758 char offsets[1024], command[1024];
759 char *ptr;
760 unsigned int i, time_len;
761 int ret;
763 ptr = offsets;
764 for (i = 0; i < cddb_data->tracks ; i++) {
765 ptr += sprintf(ptr, "%d+", cdtoc[i].frame);
766 if (ptr-offsets > sizeof offsets - 40) break;
768 ptr[0] = 0;
769 time_len = (cdtoc[cddb_data->tracks].frame)/75;
771 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
772 cddb_data->freedb_proto_level = 1;
773 cddb_data->xmcd_file = NULL;
775 cddb_create_hello(cddb_data);
776 if (cddb_get_proto_level(cddb_data) < 0) {
777 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to get the protocol level.\n");
778 return -1;
781 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id,
782 cddb_data->tracks, offsets, time_len);
783 ret = cddb_http_request(command, cddb_query_parse, cddb_data);
784 if (ret < 0)
785 return -1;
787 free(cddb_data->cache_dir);
788 return 0;
791 int cddb_resolve(const char *dev, char **xmcd_file)
793 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
794 char *home_dir = NULL;
795 cddb_data_t cddb_data;
797 if (cdtoc_last_track <= 0) {
798 cdtoc_last_track = read_toc(dev);
799 if (cdtoc_last_track < 0) {
800 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
801 return -1;
804 cddb_data.tracks = cdtoc_last_track;
805 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
806 cddb_data.anonymous = 1; // Don't send user info by default
808 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDB_DISCID=%08lx\n",
809 cddb_data.disc_id);
811 // Check if there is a CD in the drive
812 // FIXME: That's not really a good way to check
813 if (cddb_data.disc_id == 0) {
814 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No CD in the drive.\n");
815 return -1;
818 home_dir = getenv("HOME");
819 #ifdef __MINGW32__
820 if (home_dir == NULL)
821 home_dir = getenv("USERPROFILE");
822 if (home_dir == NULL)
823 home_dir = getenv("HOMEPATH");
824 // Last resort, store the cddb cache in the mplayer directory
825 if (home_dir == NULL)
826 home_dir = (char *)get_path("");
827 #endif
828 if (home_dir == NULL) {
829 cddb_data.cache_dir = NULL;
830 } else {
831 cddb_data.cache_dir = malloc(strlen(home_dir)
832 + strlen(cddb_cache_dir) + 1);
833 if (cddb_data.cache_dir == NULL) {
834 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
835 return -1;
837 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir);
840 // Check for a cached file
841 if (cddb_read_cache(&cddb_data) < 0) {
842 // No Cache found
843 if (cddb_retrieve(&cddb_data) < 0) {
844 return -1;
848 if (cddb_data.xmcd_file != NULL) {
849 // printf("%s\n", cddb_data.xmcd_file);
850 *xmcd_file = cddb_data.xmcd_file;
851 return 0;
854 return -1;
857 /***************
858 * xmcd parser *
859 ***************/
860 static char *xmcd_parse_dtitle(cd_info_t *cd_info, char *line)
862 char *ptr, *album;
863 ptr = strstr(line, "DTITLE=");
864 if (ptr != NULL) {
865 ptr += 7;
866 album = strstr(ptr, "/");
867 if (album == NULL)
868 return NULL;
869 cd_info->album = malloc(strlen(album + 2) + 1);
870 if (cd_info->album == NULL) {
871 return NULL;
873 strcpy(cd_info->album, album + 2);
874 album--;
875 album[0] = '\0';
876 cd_info->artist = malloc(strlen(ptr) + 1);
877 if (cd_info->artist == NULL) {
878 return NULL;
880 strcpy(cd_info->artist, ptr);
882 return ptr;
885 static char *xmcd_parse_dgenre(cd_info_t *cd_info, char *line)
887 char *ptr;
888 ptr = strstr(line, "DGENRE=");
889 if (ptr != NULL) {
890 ptr += 7;
891 cd_info->genre = malloc(strlen(ptr)+1);
892 if (cd_info->genre == NULL) {
893 return NULL;
895 strcpy(cd_info->genre, ptr);
897 return ptr;
900 static char *xmcd_parse_ttitle(cd_info_t *cd_info, char *line)
902 unsigned int track_nb;
903 unsigned long sec, off;
904 char *ptr;
905 ptr = strstr(line, "TTITLE");
906 if (ptr != NULL) {
907 ptr += 6;
908 // Here we point to the track number
909 track_nb = atoi(ptr);
910 ptr = strstr(ptr, "=");
911 if (ptr == NULL)
912 return NULL;
913 ptr++;
915 sec = cdtoc[track_nb].frame;
916 off = cdtoc[track_nb + 1].frame - sec + 1;
918 cd_info_add_track(cd_info, ptr, track_nb + 1,
919 (unsigned int) (off / (60 * 75)),
920 (unsigned int) ((off / 75) % 60),
921 (unsigned int) (off % 75),
922 sec, off);
924 return ptr;
927 cd_info_t *cddb_parse_xmcd(char *xmcd_file)
929 cd_info_t *cd_info = NULL;
930 int length, pos = 0;
931 char *ptr, *ptr2;
932 unsigned int audiolen;
933 if (xmcd_file == NULL)
934 return NULL;
936 cd_info = cd_info_new();
937 if (cd_info == NULL) {
938 return NULL;
941 length = strlen(xmcd_file);
942 ptr = xmcd_file;
943 while (ptr != NULL && pos < length) {
944 // Read a line
945 ptr2 = ptr;
946 while(ptr2[0] != '\0' && ptr2[0] != '\r' && ptr2[0] != '\n')
947 ptr2++;
948 if (ptr2[0] == '\0') {
949 break;
951 ptr2[0] = '\0';
952 // Ignore comments
953 if (ptr[0] != '#') {
954 // Search for the album title
955 if (xmcd_parse_dtitle(cd_info, ptr))
957 // Search for the genre
958 else if (xmcd_parse_dgenre(cd_info, ptr))
960 // Search for a track title
961 else if (xmcd_parse_ttitle(cd_info, ptr))
962 audiolen++; // <-- audiolen++ to shut up gcc warning
964 if (ptr2[1] == '\n')
965 ptr2++;
966 pos = (ptr2 + 1) - ptr;
967 ptr = ptr2 + 1;
970 audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
971 cd_info->min = (unsigned int) (audiolen / (60 * 75));
972 cd_info->sec = (unsigned int) ((audiolen / 75) % 60);
973 cd_info->msec = (unsigned int) (audiolen % 75);
975 return cd_info;