loader: Move fs_seg extern variable declaration to ldt_keeper.h
[mplayer/greg.git] / stream / stream_cddb.c
blobabc6a86966e993732fe39aff7eb23802f5058f7e
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 #define mkdir(a,b) mkdir(a)
42 #endif
43 #include <windows.h>
44 #if HAVE_WINSOCK2_H
45 #include <winsock2.h>
46 #endif
47 #else
48 #include <netdb.h>
49 #include <sys/ioctl.h>
50 #endif
51 #include <sys/types.h>
52 #include <sys/stat.h>
54 #include "mp_msg.h"
56 #if defined(__linux__)
57 #include <linux/cdrom.h>
58 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
59 #include <sys/cdio.h>
60 #elif defined(__MINGW32__) || defined(__CYGWIN__)
61 #include <ddk/ntddcdrm.h>
62 #elif defined(__bsdi__)
63 #include <dvd.h>
64 #elif defined(__APPLE__) || defined(__DARWIN__)
65 #include <IOKit/storage/IOCDTypes.h>
66 #include <IOKit/storage/IOCDMediaBSDClient.h>
67 #include "mpbswap.h"
68 #endif
70 #include "osdep/osdep.h"
72 #include "cdd.h"
73 #include "mpcommon.h"
74 #include "stream.h"
75 #include "network.h"
76 #include "libavutil/common.h"
78 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
79 #define DEFAULT_CACHE_DIR "/.cddb/"
81 typedef struct {
82 char cddb_hello[1024];
83 unsigned long disc_id;
84 unsigned int tracks;
85 char *cache_dir;
86 char *freedb_server;
87 int freedb_proto_level;
88 int anonymous;
89 char category[100];
90 char *xmcd_file;
91 size_t xmcd_file_size;
92 void *user_data;
93 } cddb_data_t;
95 typedef struct {
96 unsigned int min, sec, frame;
97 } cd_toc_t;
99 static cd_toc_t cdtoc[100];
100 static int cdtoc_last_track;
102 static int read_toc(const char *dev)
104 int first = 0, last = -1;
105 int i;
106 #if defined(__MINGW32__) || defined(__CYGWIN__)
107 HANDLE drive;
108 DWORD r;
109 CDROM_TOC toc;
110 char device[10];
112 sprintf(device, "\\\\.\\%s", dev);
113 drive = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
114 OPEN_EXISTING, 0, 0);
116 if (!DeviceIoControl(drive, IOCTL_CDROM_READ_TOC, NULL, 0, &toc,
117 sizeof(CDROM_TOC), &r, 0)) {
118 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
119 return 0;
122 first = toc.FirstTrack - 1; last = toc.LastTrack;
123 for (i = first; i <= last; i++) {
124 cdtoc[i].min = toc.TrackData[i].Address[1];
125 cdtoc[i].sec = toc.TrackData[i].Address[2];
126 cdtoc[i].frame = toc.TrackData[i].Address[3];
128 CloseHandle(drive);
130 #elif defined(__OS2__)
131 UCHAR auchParamDisk[4] = {'C', 'D', '0', '1'};
133 struct {
134 BYTE bFirstTrack;
135 BYTE bLastTrack;
136 BYTE bLeadOutF;
137 BYTE bLeadOutS;
138 BYTE bLeadOutM;
139 BYTE bLeadOutReserved;
140 } __attribute__((packed)) sDataDisk;
142 struct {
143 UCHAR auchSign[4];
144 BYTE bTrack;
145 } __attribute__((packed)) sParamTrack = {{'C', 'D', '0', '1'},};
147 struct {
148 BYTE bStartF;
149 BYTE bStartS;
150 BYTE bStartM;
151 BYTE bStartReserved;
152 BYTE bControlInfo;
153 } __attribute__((packed)) sDataTrack;
155 HFILE hcd;
156 ULONG ulAction;
157 ULONG ulParamLen;
158 ULONG ulDataLen;
159 ULONG rc;
161 rc = DosOpen(dev, &hcd, &ulAction, 0, FILE_NORMAL,
162 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
163 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
164 NULL);
165 if (rc) {
166 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
167 return -1;
170 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIODISK,
171 auchParamDisk, sizeof(auchParamDisk), &ulParamLen,
172 &sDataDisk, sizeof(sDataDisk), &ulDataLen);
173 if (!rc) {
174 first = sDataDisk.bFirstTrack - 1;
175 last = sDataDisk.bLastTrack;
176 for (i = first; i <= last; i++) {
177 if (i == last) {
178 sDataTrack.bStartM = sDataDisk.bLeadOutM;
179 sDataTrack.bStartS = sDataDisk.bLeadOutS;
180 sDataTrack.bStartF = sDataDisk.bLeadOutF;
181 } else {
182 sParamTrack.bTrack = i + 1;
183 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIOTRACK,
184 &sParamTrack, sizeof(sParamTrack), &ulParamLen,
185 &sDataTrack, sizeof(sDataTrack), &ulDataLen);
186 if (rc)
187 break;
190 cdtoc[i].min = sDataTrack.bStartM;
191 cdtoc[i].sec = sDataTrack.bStartS;
192 cdtoc[i].frame = sDataTrack.bStartF;
196 DosClose(hcd);
198 if (rc) {
199 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to read TOC.\n");
200 return -1;
202 #else
203 int drive;
204 drive = open(dev, O_RDONLY | O_NONBLOCK);
205 if (drive < 0) {
206 return drive;
209 #if defined(__linux__) || defined(__bsdi__)
211 struct cdrom_tochdr tochdr;
212 ioctl(drive, CDROMREADTOCHDR, &tochdr);
213 first = tochdr.cdth_trk0 - 1; last = tochdr.cdth_trk1;
215 for (i = first; i <= last; i++) {
216 struct cdrom_tocentry tocentry;
217 tocentry.cdte_track = (i == last) ? 0xAA : i + 1;
218 tocentry.cdte_format = CDROM_MSF;
219 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
220 cdtoc[i].min = tocentry.cdte_addr.msf.minute;
221 cdtoc[i].sec = tocentry.cdte_addr.msf.second;
222 cdtoc[i].frame = tocentry.cdte_addr.msf.frame;
224 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
226 struct ioc_toc_header tochdr;
227 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
228 first = tochdr.starting_track - 1; last = tochdr.ending_track;
230 for (i = first; i <= last; i++) {
231 struct ioc_read_toc_single_entry tocentry;
232 tocentry.track = (i == last) ? 0xAA : i + 1;
233 tocentry.address_format = CD_MSF_FORMAT;
234 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
235 cdtoc[i].min = tocentry.entry.addr.msf.minute;
236 cdtoc[i].sec = tocentry.entry.addr.msf.second;
237 cdtoc[i].frame = tocentry.entry.addr.msf.frame;
239 #elif defined(__NetBSD__) || defined(__OpenBSD__)
241 struct ioc_toc_header tochdr;
242 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
243 first = tochdr.starting_track - 1; last = tochdr.ending_track;
245 for (i = first; i <= last; i++) {
246 struct ioc_read_toc_entry tocentry;
247 struct cd_toc_entry toc_buffer;
248 tocentry.starting_track = (i == last) ? 0xAA : i + 1;
249 tocentry.address_format = CD_MSF_FORMAT;
250 tocentry.data = &toc_buffer;
251 tocentry.data_len = sizeof(toc_buffer);
252 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
253 cdtoc[i].min = toc_buffer.addr.msf.minute;
254 cdtoc[i].sec = toc_buffer.addr.msf.second;
255 cdtoc[i].frame = toc_buffer.addr.msf.frame;
257 #elif defined(__APPLE__) || defined(__DARWIN__)
259 dk_cd_read_toc_t tochdr;
260 uint8_t buf[4];
261 uint8_t buf2[100 * sizeof(CDTOCDescriptor) + sizeof(CDTOC)];
262 memset(&tochdr, 0, sizeof(tochdr));
263 tochdr.bufferLength = sizeof(buf);
264 tochdr.buffer = &buf;
265 if (!ioctl(drive, DKIOCCDREADTOC, &tochdr)
266 && tochdr.bufferLength == sizeof(buf)) {
267 first = buf[2] - 1;
268 last = buf[3];
270 if (last >= 0) {
271 memset(&tochdr, 0, sizeof(tochdr));
272 tochdr.bufferLength = sizeof(buf2);
273 tochdr.buffer = &buf2;
274 tochdr.format = kCDTOCFormatTOC;
275 if (ioctl(drive, DKIOCCDREADTOC, &tochdr)
276 || tochdr.bufferLength < sizeof(CDTOC))
277 last = -1;
279 if (last >= 0) {
280 CDTOC *cdToc = (CDTOC *)buf2;
281 CDTrackInfo lastTrack;
282 dk_cd_read_track_info_t trackInfoParams;
283 for (i = first; i < last; ++i) {
284 CDMSF msf = CDConvertTrackNumberToMSF(i + 1, cdToc);
285 cdtoc[i].min = msf.minute;
286 cdtoc[i].sec = msf.second;
287 cdtoc[i].frame = msf.frame;
289 memset(&trackInfoParams, 0, sizeof(trackInfoParams));
290 trackInfoParams.addressType = kCDTrackInfoAddressTypeTrackNumber;
291 trackInfoParams.bufferLength = sizeof(lastTrack);
292 trackInfoParams.address = last;
293 trackInfoParams.buffer = &lastTrack;
294 if (!ioctl(drive, DKIOCCDREADTRACKINFO, &trackInfoParams)) {
295 CDMSF msf = CDConvertLBAToMSF(be2me_32(lastTrack.trackStartAddress)
296 + be2me_32(lastTrack.trackSize));
297 cdtoc[last].min = msf.minute;
298 cdtoc[last].sec = msf.second;
299 cdtoc[last].frame = msf.frame;
303 #endif
304 close(drive);
305 #endif
306 for (i = first; i <= last; i++)
307 cdtoc[i].frame += (cdtoc[i].min * 60 + cdtoc[i].sec) * 75;
308 return last;
312 \brief Reads TOC from CD in the given device and prints the number of tracks
313 and the length of each track in minute:second:frame format.
314 \param *dev the device to analyse
315 \return if the command line -identify is given, returns the last track of
316 the TOC or -1 if the TOC can't be read,
317 otherwise just returns 0 and let cddb_resolve the TOC
319 int cdd_identify(const char *dev)
321 cdtoc_last_track = 0;
322 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
323 int i, min, sec, frame;
324 cdtoc_last_track = read_toc(dev);
325 if (cdtoc_last_track < 0) {
326 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
327 return -1;
329 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track);
330 for (i = 1; i <= cdtoc_last_track; i++) {
331 frame = cdtoc[i].frame - cdtoc[i-1].frame;
332 sec = frame / 75;
333 frame -= sec * 75;
334 min = sec / 60;
335 sec -= min * 60;
336 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
337 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i, min, sec, frame);
340 return cdtoc_last_track;
343 static unsigned int cddb_sum(int n)
345 unsigned int ret;
347 ret = 0;
348 while (n > 0) {
349 ret += (n % 10);
350 n /= 10;
352 return ret;
355 static unsigned long cddb_discid(int tot_trks)
357 unsigned int i, t = 0, n = 0;
359 i = 0;
360 while (i < (unsigned int)tot_trks) {
361 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
362 i++;
364 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
365 ((cdtoc[0].min * 60) + cdtoc[0].sec);
366 return (n % 0xff) << 24 | t << 8 | tot_trks;
371 static int cddb_http_request(char *command,
372 int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
373 cddb_data_t *cddb_data)
375 char request[4096];
376 int fd, ret = 0;
377 URL_t *url;
378 HTTP_header_t *http_hdr;
380 if (reply_parser == NULL || command == NULL || cddb_data == NULL)
381 return -1;
383 sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
384 cddb_data->freedb_server, command, cddb_data->cddb_hello,
385 cddb_data->freedb_proto_level);
386 mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);
388 url = url_new(request);
389 if (url == NULL) {
390 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "not a valid URL\n");
391 return -1;
394 fd = http_send_request(url,0);
395 if (fd < 0) {
396 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to send the HTTP request.\n");
397 return -1;
400 http_hdr = http_read_response(fd);
401 if (http_hdr == NULL) {
402 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to read the HTTP response.\n");
403 return -1;
406 http_debug_hdr(http_hdr);
407 mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);
409 switch (http_hdr->status_code) {
410 case 200:
411 ret = reply_parser(http_hdr, cddb_data);
412 break;
413 case 400:
414 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Not Found.\n");
415 break;
416 default:
417 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unknown error code\n");
420 http_free(http_hdr);
421 url_free(url);
423 return ret;
426 static int cddb_read_cache(cddb_data_t *cddb_data)
428 char file_name[100];
429 struct stat stats;
430 int file_fd, ret;
431 size_t file_size;
433 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
434 return -1;
436 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
438 file_fd = open(file_name, O_RDONLY | O_BINARY);
439 if (file_fd < 0) {
440 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No cache found.\n");
441 return -1;
444 ret = fstat(file_fd, &stats);
445 if (ret < 0) {
446 perror("fstat");
447 file_size = 4096;
448 } else {
449 file_size = stats.st_size < UINT_MAX ? stats.st_size : UINT_MAX - 1;
452 cddb_data->xmcd_file = malloc(file_size + 1);
453 if (cddb_data->xmcd_file == NULL) {
454 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
455 close(file_fd);
456 return -1;
458 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
459 if (cddb_data->xmcd_file_size != file_size) {
460 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all the xmcd file has been read.\n");
461 close(file_fd);
462 return -1;
464 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = 0;
466 close(file_fd);
468 return 0;
471 static int cddb_write_cache(cddb_data_t *cddb_data)
473 // We have the file, save it for cache.
474 struct stat file_stat;
475 char file_name[100];
476 int file_fd, ret;
477 int wrote = 0;
479 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
480 return -1;
482 // Check if the CDDB cache dir exist
483 ret = stat(cddb_data->cache_dir, &file_stat);
484 if (ret < 0) {
485 // Directory not present, create it.
486 ret = mkdir(cddb_data->cache_dir, 0755);
487 #ifdef __MINGW32__
488 if (ret < 0 && errno != EEXIST) {
489 #else
490 if (ret < 0) {
491 #endif
492 perror("mkdir");
493 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to create directory %s.\n",
494 cddb_data->cache_dir);
495 return -1;
499 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
501 file_fd = creat(file_name, S_IRUSR | S_IWUSR);
502 if (file_fd < 0) {
503 perror("create");
504 return -1;
507 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
508 if (wrote < 0) {
509 perror("write");
510 close(file_fd);
511 return -1;
513 if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
514 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Not all of the xmcd file has been written.\n");
515 close(file_fd);
516 return -1;
519 close(file_fd);
521 return 0;
524 static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
526 unsigned long disc_id;
527 char category[100];
528 char *ptr = NULL, *ptr2 = NULL;
529 int ret, status;
531 if (http_hdr == NULL || cddb_data == NULL)
532 return -1;
534 ret = sscanf(http_hdr->body, "%d ", &status);
535 if (ret != 1) {
536 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
537 return -1;
540 switch (status) {
541 case 210:
542 ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
543 category, &disc_id);
544 if (ret != 3) {
545 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
546 return -1;
548 // Check if it's a xmcd database file
549 ptr = strstr(http_hdr->body, "# xmcd");
550 if (ptr == NULL) {
551 mp_tmsg(MSGT_DEMUX, MSGL_ERR,
552 "Invalid xmcd database file returned.\n");
553 return -1;
555 ptr = strdup(ptr);
556 // Ok found the beginning of the file
557 // look for the end
558 ptr2 = strstr(ptr, "\n.\r\n");
559 if (!ptr2)
560 ptr2 = strstr(ptr, "\n.\n");
561 if (ptr2) {
562 ptr2++;
563 } else {
564 mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
565 ptr2 = ptr + strlen(ptr); //return -1;
567 // Ok found the end
568 // do a sanity check
569 if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
570 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "unexpected FIXME\n");
571 return -1;
573 cddb_data->xmcd_file = ptr;
574 cddb_data->xmcd_file_size = ptr2 - ptr;
575 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
576 return cddb_write_cache(cddb_data);
577 default:
578 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
580 return 0;
583 static int cddb_request_titles(cddb_data_t *cddb_data)
585 char command[1024];
586 sprintf(command, "cddb+read+%s+%08lx",
587 cddb_data->category, cddb_data->disc_id);
588 return cddb_http_request(command, cddb_read_parse, cddb_data);
591 static int cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
593 char album_title[100];
594 char *ptr = NULL;
595 int ret;
597 ptr = strstr(http_hdr->body, "\n");
598 if (ptr == NULL) {
599 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Unable to find end of line.\n");
600 return -1;
602 ptr++;
603 // We have a list of exact/inexact matches, so which one do we use?
604 // So let's take the first one.
605 ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category,
606 &(cddb_data->disc_id), album_title);
607 if (ret != 3) {
608 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
609 return -1;
611 ptr = strstr(http_hdr->body, album_title);
612 if (ptr != NULL) {
613 char *ptr2;
614 int len;
615 ptr2 = strstr(ptr, "\n");
616 if (ptr2 == NULL) {
617 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
618 } else {
619 len = ptr2-ptr+1;
621 len = FFMIN(sizeof(album_title) - 1, len);
622 strncpy(album_title, ptr, len);
623 album_title[len]='\0';
625 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
626 return 0;
629 static int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
631 char album_title[100];
632 char *ptr = NULL;
633 int ret, status;
635 ret = sscanf(http_hdr->body, "%d ", &status);
636 if (ret != 1) {
637 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
638 return -1;
641 switch (status) {
642 case 200:
643 // Found exact match
644 ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status,
645 cddb_data->category, &(cddb_data->disc_id), album_title);
646 if (ret != 4) {
647 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
648 return -1;
650 ptr = strstr(http_hdr->body, album_title);
651 if (ptr != NULL) {
652 char *ptr2;
653 int len;
654 ptr2 = strstr(ptr, "\n");
655 if (ptr2 == NULL) {
656 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
657 } else {
658 len = ptr2-ptr+1;
660 len = FFMIN(sizeof(album_title) - 1, len);
661 strncpy(album_title, ptr, len);
662 album_title[len]='\0';
664 mp_tmsg(MSGT_DEMUX, MSGL_STATUS, "Parse OK, found: %s\n", album_title);
665 return cddb_request_titles(cddb_data);
666 case 202:
667 // No match found
668 mp_tmsg(MSGT_DEMUX, MSGL_WARN, "Album not found.\n");
669 break;
670 case 210:
671 // Found exact matches, list follows
672 cddb_parse_matches_list(http_hdr, cddb_data);
673 return cddb_request_titles(cddb_data);
675 body=[210 Found exact matches, list follows (until terminating `.')
676 misc c711930d Santana / Supernatural
677 rock c711930d Santana / Supernatural
678 blues c711930d Santana / Supernatural
681 case 211:
682 // Found inexact matches, list follows
683 cddb_parse_matches_list(http_hdr, cddb_data);
684 return cddb_request_titles(cddb_data);
685 case 500:
686 mp_tmsg(MSGT_DEMUX, MSGL_FIXME,
687 "Server returns: Command syntax error\n");
688 break;
689 default:
690 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
692 return -1;
695 static int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
697 int max;
698 int ret, status;
699 char *ptr;
701 ret = sscanf(http_hdr->body, "%d ", &status);
702 if (ret != 1) {
703 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
704 return -1;
707 switch (status) {
708 case 210:
709 ptr = strstr(http_hdr->body, "max proto:");
710 if (ptr == NULL) {
711 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
712 return -1;
714 ret = sscanf(ptr, "max proto: %d", &max);
715 if (ret != 1) {
716 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "parse error");
717 return -1;
719 cddb_data->freedb_proto_level = max;
720 return 0;
721 default:
722 mp_tmsg(MSGT_DEMUX, MSGL_FIXME, "unhandled code\n");
724 return -1;
727 static int cddb_get_proto_level(cddb_data_t *cddb_data)
729 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
732 static void cddb_create_hello(cddb_data_t *cddb_data)
734 char host_name[51];
735 char *user_name;
737 if (cddb_data->anonymous) { // Default is anonymous
738 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
739 * We don't send current user/host name in hello to prevent spam.
740 * Software that sends this is considered spyware
741 * that most people don't like.
743 user_name = "anonymous";
744 strcpy(host_name, "localhost");
745 } else {
746 if (gethostname(host_name, 50) < 0) {
747 strcpy(host_name, "localhost");
749 user_name = getenv("LOGNAME");
751 sprintf(cddb_data->cddb_hello, "&hello=%s+%s+%s",
752 user_name, host_name, mplayer_version);
755 static int cddb_retrieve(cddb_data_t *cddb_data)
757 char offsets[1024], command[1024];
758 char *ptr;
759 unsigned int i, time_len;
760 int ret;
762 ptr = offsets;
763 for (i = 0; i < cddb_data->tracks ; i++) {
764 ptr += sprintf(ptr, "%d+", cdtoc[i].frame);
765 if (ptr-offsets > sizeof offsets - 40) break;
767 ptr[0] = 0;
768 time_len = (cdtoc[cddb_data->tracks].frame)/75;
770 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
771 cddb_data->freedb_proto_level = 1;
772 cddb_data->xmcd_file = NULL;
774 cddb_create_hello(cddb_data);
775 if (cddb_get_proto_level(cddb_data) < 0) {
776 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Failed to get the protocol level.\n");
777 return -1;
780 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id,
781 cddb_data->tracks, offsets, time_len);
782 ret = cddb_http_request(command, cddb_query_parse, cddb_data);
783 if (ret < 0)
784 return -1;
786 if (cddb_data->cache_dir != NULL) {
787 free(cddb_data->cache_dir);
789 return 0;
792 int cddb_resolve(const char *dev, char **xmcd_file)
794 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
795 char *home_dir = NULL;
796 cddb_data_t cddb_data;
798 if (cdtoc_last_track <= 0) {
799 cdtoc_last_track = read_toc(dev);
800 if (cdtoc_last_track < 0) {
801 mp_tmsg(MSGT_OPEN, MSGL_ERR, "Failed to open %s device.\n", dev);
802 return -1;
805 cddb_data.tracks = cdtoc_last_track;
806 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
807 cddb_data.anonymous = 1; // Don't send user info by default
809 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDB_DISCID=%08lx\n",
810 cddb_data.disc_id);
812 // Check if there is a CD in the drive
813 // FIXME: That's not really a good way to check
814 if (cddb_data.disc_id == 0) {
815 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "No CD in the drive.\n");
816 return -1;
819 home_dir = getenv("HOME");
820 #ifdef __MINGW32__
821 if (home_dir == NULL)
822 home_dir = getenv("USERPROFILE");
823 if (home_dir == NULL)
824 home_dir = getenv("HOMEPATH");
825 // Last resort, store the cddb cache in the mplayer directory
826 if (home_dir == NULL)
827 home_dir = (char *)get_path("");
828 #endif
829 if (home_dir == NULL) {
830 cddb_data.cache_dir = NULL;
831 } else {
832 cddb_data.cache_dir = malloc(strlen(home_dir)
833 + strlen(cddb_cache_dir) + 1);
834 if (cddb_data.cache_dir == NULL) {
835 mp_tmsg(MSGT_DEMUX, MSGL_ERR, "Memory allocation failed.\n");
836 return -1;
838 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir);
841 // Check for a cached file
842 if (cddb_read_cache(&cddb_data) < 0) {
843 // No Cache found
844 if (cddb_retrieve(&cddb_data) < 0) {
845 return -1;
849 if (cddb_data.xmcd_file != NULL) {
850 // printf("%s\n", cddb_data.xmcd_file);
851 *xmcd_file = cddb_data.xmcd_file;
852 return 0;
855 return -1;
858 /***************
859 * xmcd parser *
860 ***************/
861 static char *xmcd_parse_dtitle(cd_info_t *cd_info, char *line)
863 char *ptr, *album;
864 ptr = strstr(line, "DTITLE=");
865 if (ptr != NULL) {
866 ptr += 7;
867 album = strstr(ptr, "/");
868 if (album == NULL)
869 return NULL;
870 cd_info->album = malloc(strlen(album + 2) + 1);
871 if (cd_info->album == NULL) {
872 return NULL;
874 strcpy(cd_info->album, album + 2);
875 album--;
876 album[0] = '\0';
877 cd_info->artist = malloc(strlen(ptr) + 1);
878 if (cd_info->artist == NULL) {
879 return NULL;
881 strcpy(cd_info->artist, ptr);
883 return ptr;
886 static char *xmcd_parse_dgenre(cd_info_t *cd_info, char *line)
888 char *ptr;
889 ptr = strstr(line, "DGENRE=");
890 if (ptr != NULL) {
891 ptr += 7;
892 cd_info->genre = malloc(strlen(ptr)+1);
893 if (cd_info->genre == NULL) {
894 return NULL;
896 strcpy(cd_info->genre, ptr);
898 return ptr;
901 static char *xmcd_parse_ttitle(cd_info_t *cd_info, char *line)
903 unsigned int track_nb;
904 unsigned long sec, off;
905 char *ptr;
906 ptr = strstr(line, "TTITLE");
907 if (ptr != NULL) {
908 ptr += 6;
909 // Here we point to the track number
910 track_nb = atoi(ptr);
911 ptr = strstr(ptr, "=");
912 if (ptr == NULL)
913 return NULL;
914 ptr++;
916 sec = cdtoc[track_nb].frame;
917 off = cdtoc[track_nb + 1].frame - sec + 1;
919 cd_info_add_track(cd_info, ptr, track_nb + 1,
920 (unsigned int) (off / (60 * 75)),
921 (unsigned int) ((off / 75) % 60),
922 (unsigned int) (off % 75),
923 sec, off);
925 return ptr;
928 cd_info_t *cddb_parse_xmcd(char *xmcd_file)
930 cd_info_t *cd_info = NULL;
931 int length, pos = 0;
932 char *ptr, *ptr2;
933 unsigned int audiolen;
934 if (xmcd_file == NULL)
935 return NULL;
937 cd_info = cd_info_new();
938 if (cd_info == NULL) {
939 return NULL;
942 length = strlen(xmcd_file);
943 ptr = xmcd_file;
944 while (ptr != NULL && pos < length) {
945 // Read a line
946 ptr2 = ptr;
947 while(ptr2[0] != '\0' && ptr2[0] != '\r' && ptr2[0] != '\n')
948 ptr2++;
949 if (ptr2[0] == '\0') {
950 break;
952 ptr2[0] = '\0';
953 // Ignore comments
954 if (ptr[0] != '#') {
955 // Search for the album title
956 if (xmcd_parse_dtitle(cd_info, ptr))
958 // Search for the genre
959 else if (xmcd_parse_dgenre(cd_info, ptr))
961 // Search for a track title
962 else if (xmcd_parse_ttitle(cd_info, ptr))
963 audiolen++; // <-- audiolen++ to shut up gcc warning
965 if (ptr2[1] == '\n')
966 ptr2++;
967 pos = (ptr2 + 1) - ptr;
968 ptr = ptr2 + 1;
971 audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
972 cd_info->min = (unsigned int) (audiolen / (60 * 75));
973 cd_info->sec = (unsigned int) ((audiolen / 75) % 60);
974 cd_info->msec = (unsigned int) (audiolen % 75);
976 return cd_info;