Fix compilation with gcc 2.95.3 (with --disable-ass).
[mplayer/glamo.git] / stream / stream_cddb.c
bloba679a9898cb0f9661733e22e11a99fc5fce0dd78
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"
55 #include "help_mp.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 "version.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 static cd_toc_t cdtoc[100];
83 static int cdtoc_last_track;
85 static int read_toc(const char *dev)
87 int first = 0, last = -1;
88 int i;
89 #if defined(__MINGW32__) || defined(__CYGWIN__)
90 HANDLE drive;
91 DWORD r;
92 CDROM_TOC toc;
93 char device[10];
95 sprintf(device, "\\\\.\\%s", dev);
96 drive = CreateFile(device, GENERIC_READ, FILE_SHARE_READ, NULL,
97 OPEN_EXISTING, 0, 0);
99 if (!DeviceIoControl(drive, IOCTL_CDROM_READ_TOC, NULL, 0, &toc,
100 sizeof(CDROM_TOC), &r, 0)) {
101 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToReadTOC);
102 return 0;
105 first = toc.FirstTrack - 1; last = toc.LastTrack;
106 for (i = first; i <= last; i++) {
107 cdtoc[i].min = toc.TrackData[i].Address[1];
108 cdtoc[i].sec = toc.TrackData[i].Address[2];
109 cdtoc[i].frame = toc.TrackData[i].Address[3];
111 CloseHandle(drive);
113 #elif defined(__OS2__)
114 UCHAR auchParamDisk[4] = {'C', 'D', '0', '1'};
116 struct {
117 BYTE bFirstTrack;
118 BYTE bLastTrack;
119 BYTE bLeadOutF;
120 BYTE bLeadOutS;
121 BYTE bLeadOutM;
122 BYTE bLeadOutReserved;
123 } __attribute__((packed)) sDataDisk;
125 struct {
126 UCHAR auchSign[4];
127 BYTE bTrack;
128 } __attribute__((packed)) sParamTrack = {{'C', 'D', '0', '1'},};
130 struct {
131 BYTE bStartF;
132 BYTE bStartS;
133 BYTE bStartM;
134 BYTE bStartReserved;
135 BYTE bControlInfo;
136 } __attribute__((packed)) sDataTrack;
138 HFILE hcd;
139 ULONG ulAction;
140 ULONG ulParamLen;
141 ULONG ulDataLen;
142 ULONG rc;
144 rc = DosOpen(dev, &hcd, &ulAction, 0, FILE_NORMAL,
145 OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
146 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE | OPEN_FLAGS_DASD,
147 NULL);
148 if (rc) {
149 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToReadTOC);
150 return -1;
153 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIODISK,
154 auchParamDisk, sizeof(auchParamDisk), &ulParamLen,
155 &sDataDisk, sizeof(sDataDisk), &ulDataLen);
156 if (!rc) {
157 first = sDataDisk.bFirstTrack - 1;
158 last = sDataDisk.bLastTrack;
159 for (i = first; i <= last; i++) {
160 if (i == last) {
161 sDataTrack.bStartM = sDataDisk.bLeadOutM;
162 sDataTrack.bStartS = sDataDisk.bLeadOutS;
163 sDataTrack.bStartF = sDataDisk.bLeadOutF;
164 } else {
165 sParamTrack.bTrack = i + 1;
166 rc = DosDevIOCtl(hcd, IOCTL_CDROMAUDIO, CDROMAUDIO_GETAUDIOTRACK,
167 &sParamTrack, sizeof(sParamTrack), &ulParamLen,
168 &sDataTrack, sizeof(sDataTrack), &ulDataLen);
169 if (rc)
170 break;
173 cdtoc[i].min = sDataTrack.bStartM;
174 cdtoc[i].sec = sDataTrack.bStartS;
175 cdtoc[i].frame = sDataTrack.bStartF;
179 DosClose(hcd);
181 if (rc) {
182 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToReadTOC);
183 return -1;
185 #else
186 int drive;
187 drive = open(dev, O_RDONLY | O_NONBLOCK);
188 if (drive < 0) {
189 return drive;
192 #if defined(__linux__) || defined(__bsdi__)
194 struct cdrom_tochdr tochdr;
195 ioctl(drive, CDROMREADTOCHDR, &tochdr);
196 first = tochdr.cdth_trk0 - 1; last = tochdr.cdth_trk1;
198 for (i = first; i <= last; i++) {
199 struct cdrom_tocentry tocentry;
200 tocentry.cdte_track = (i == last) ? 0xAA : i + 1;
201 tocentry.cdte_format = CDROM_MSF;
202 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
203 cdtoc[i].min = tocentry.cdte_addr.msf.minute;
204 cdtoc[i].sec = tocentry.cdte_addr.msf.second;
205 cdtoc[i].frame = tocentry.cdte_addr.msf.frame;
207 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
209 struct ioc_toc_header tochdr;
210 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
211 first = tochdr.starting_track - 1; last = tochdr.ending_track;
213 for (i = first; i <= last; i++) {
214 struct ioc_read_toc_single_entry tocentry;
215 tocentry.track = (i == last) ? 0xAA : i + 1;
216 tocentry.address_format = CD_MSF_FORMAT;
217 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
218 cdtoc[i].min = tocentry.entry.addr.msf.minute;
219 cdtoc[i].sec = tocentry.entry.addr.msf.second;
220 cdtoc[i].frame = tocentry.entry.addr.msf.frame;
222 #elif defined(__NetBSD__) || defined(__OpenBSD__)
224 struct ioc_toc_header tochdr;
225 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
226 first = tochdr.starting_track - 1; last = tochdr.ending_track;
228 for (i = first; i <= last; i++) {
229 struct ioc_read_toc_entry tocentry;
230 struct cd_toc_entry toc_buffer;
231 tocentry.starting_track = (i == last) ? 0xAA : i + 1;
232 tocentry.address_format = CD_MSF_FORMAT;
233 tocentry.data = &toc_buffer;
234 tocentry.data_len = sizeof(toc_buffer);
235 ioctl(drive, CDIOREADTOCENTRYS, &tocentry);
236 cdtoc[i].min = toc_buffer.addr.msf.minute;
237 cdtoc[i].sec = toc_buffer.addr.msf.second;
238 cdtoc[i].frame = toc_buffer.addr.msf.frame;
240 #elif defined(__APPLE__) || defined(__DARWIN__)
242 dk_cd_read_toc_t tochdr;
243 uint8_t buf[4];
244 uint8_t buf2[100 * sizeof(CDTOCDescriptor) + sizeof(CDTOC)];
245 memset(&tochdr, 0, sizeof(tochdr));
246 tochdr.bufferLength = sizeof(buf);
247 tochdr.buffer = &buf;
248 if (!ioctl(drive, DKIOCCDREADTOC, &tochdr)
249 && tochdr.bufferLength == sizeof(buf)) {
250 first = buf[2] - 1;
251 last = buf[3];
253 if (last >= 0) {
254 memset(&tochdr, 0, sizeof(tochdr));
255 tochdr.bufferLength = sizeof(buf2);
256 tochdr.buffer = &buf2;
257 tochdr.format = kCDTOCFormatTOC;
258 if (ioctl(drive, DKIOCCDREADTOC, &tochdr)
259 || tochdr.bufferLength < sizeof(CDTOC))
260 last = -1;
262 if (last >= 0) {
263 CDTOC *cdToc = (CDTOC *)buf2;
264 CDTrackInfo lastTrack;
265 dk_cd_read_track_info_t trackInfoParams;
266 for (i = first; i < last; ++i) {
267 CDMSF msf = CDConvertTrackNumberToMSF(i + 1, cdToc);
268 cdtoc[i].min = msf.minute;
269 cdtoc[i].sec = msf.second;
270 cdtoc[i].frame = msf.frame;
272 memset(&trackInfoParams, 0, sizeof(trackInfoParams));
273 trackInfoParams.addressType = kCDTrackInfoAddressTypeTrackNumber;
274 trackInfoParams.bufferLength = sizeof(lastTrack);
275 trackInfoParams.address = last;
276 trackInfoParams.buffer = &lastTrack;
277 if (!ioctl(drive, DKIOCCDREADTRACKINFO, &trackInfoParams)) {
278 CDMSF msf = CDConvertLBAToMSF(be2me_32(lastTrack.trackStartAddress)
279 + be2me_32(lastTrack.trackSize));
280 cdtoc[last].min = msf.minute;
281 cdtoc[last].sec = msf.second;
282 cdtoc[last].frame = msf.frame;
286 #endif
287 close(drive);
288 #endif
289 for (i = first; i <= last; i++)
290 cdtoc[i].frame += (cdtoc[i].min * 60 + cdtoc[i].sec) * 75;
291 return last;
295 \brief Reads TOC from CD in the given device and prints the number of tracks
296 and the length of each track in minute:second:frame format.
297 \param *dev the device to analyse
298 \return if the command line -identify is given, returns the last track of
299 the TOC or -1 if the TOC can't be read,
300 otherwise just returns 0 and let cddb_resolve the TOC
302 int cdd_identify(const char *dev)
304 cdtoc_last_track = 0;
305 if (mp_msg_test(MSGT_IDENTIFY, MSGL_INFO)) {
306 int i, min, sec, frame;
307 cdtoc_last_track = read_toc(dev);
308 if (cdtoc_last_track < 0) {
309 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToOpenDevice,
310 dev);
311 return -1;
313 mp_msg(MSGT_GLOBAL, MSGL_INFO, "ID_CDDA_TRACKS=%d\n", cdtoc_last_track);
314 for (i = 1; i <= cdtoc_last_track; i++) {
315 frame = cdtoc[i].frame - cdtoc[i-1].frame;
316 sec = frame / 75;
317 frame -= sec * 75;
318 min = sec / 60;
319 sec -= min * 60;
320 mp_msg(MSGT_IDENTIFY, MSGL_INFO,
321 "ID_CDDA_TRACK_%d_MSF=%02d:%02d:%02d\n", i, min, sec, frame);
324 return cdtoc_last_track;
327 static unsigned int cddb_sum(int n)
329 unsigned int ret;
331 ret = 0;
332 while (n > 0) {
333 ret += (n % 10);
334 n /= 10;
336 return ret;
339 static unsigned long cddb_discid(int tot_trks)
341 unsigned int i, t = 0, n = 0;
343 i = 0;
344 while (i < (unsigned int)tot_trks) {
345 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
346 i++;
348 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
349 ((cdtoc[0].min * 60) + cdtoc[0].sec);
350 return (n % 0xff) << 24 | t << 8 | tot_trks;
355 static int cddb_http_request(char *command,
356 int (*reply_parser)(HTTP_header_t*, cddb_data_t*),
357 cddb_data_t *cddb_data)
359 char request[4096];
360 int fd, ret = 0;
361 URL_t *url;
362 HTTP_header_t *http_hdr;
364 if (reply_parser == NULL || command == NULL || cddb_data == NULL)
365 return -1;
367 sprintf(request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d",
368 cddb_data->freedb_server, command, cddb_data->cddb_hello,
369 cddb_data->freedb_proto_level);
370 mp_msg(MSGT_OPEN, MSGL_INFO,"Request[%s]\n", request);
372 url = url_new(request);
373 if (url == NULL) {
374 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_NotAValidURL);
375 return -1;
378 fd = http_send_request(url,0);
379 if (fd < 0) {
380 mp_msg(MSGT_DEMUX, MSGL_ERR,
381 MSGTR_MPDEMUX_CDDB_FailedToSendHTTPRequest);
382 return -1;
385 http_hdr = http_read_response(fd);
386 if (http_hdr == NULL) {
387 mp_msg(MSGT_DEMUX, MSGL_ERR,
388 MSGTR_MPDEMUX_CDDB_FailedToReadHTTPResponse);
389 return -1;
392 http_debug_hdr(http_hdr);
393 mp_msg(MSGT_OPEN, MSGL_INFO,"body=[%s]\n", http_hdr->body);
395 switch (http_hdr->status_code) {
396 case 200:
397 ret = reply_parser(http_hdr, cddb_data);
398 break;
399 case 400:
400 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_HTTPErrorNOTFOUND);
401 break;
402 default:
403 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_HTTPErrorUnknown);
406 http_free(http_hdr);
407 url_free(url);
409 return ret;
412 static int cddb_read_cache(cddb_data_t *cddb_data)
414 char file_name[100];
415 struct stat stats;
416 int file_fd, ret;
417 size_t file_size;
419 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
420 return -1;
422 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
424 file_fd = open(file_name, O_RDONLY | O_BINARY);
425 if (file_fd < 0) {
426 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_NoCacheFound);
427 return -1;
430 ret = fstat(file_fd, &stats);
431 if (ret < 0) {
432 perror("fstat");
433 file_size = 4096;
434 } else {
435 file_size = stats.st_size < UINT_MAX ? stats.st_size : UINT_MAX - 1;
438 cddb_data->xmcd_file = malloc(file_size + 1);
439 if (cddb_data->xmcd_file == NULL) {
440 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MemAllocFailed);
441 close(file_fd);
442 return -1;
444 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
445 if (cddb_data->xmcd_file_size != file_size) {
446 mp_msg(MSGT_DEMUX, MSGL_WARN,
447 MSGTR_MPDEMUX_CDDB_NotAllXMCDFileHasBeenRead);
448 close(file_fd);
449 return -1;
451 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = 0;
453 close(file_fd);
455 return 0;
458 static int cddb_write_cache(cddb_data_t *cddb_data)
460 // We have the file, save it for cache.
461 struct stat file_stat;
462 char file_name[100];
463 int file_fd, ret;
464 int wrote = 0;
466 if (cddb_data == NULL || cddb_data->cache_dir == NULL)
467 return -1;
469 // Check if the CDDB cache dir exist
470 ret = stat(cddb_data->cache_dir, &file_stat);
471 if (ret < 0) {
472 // Directory not present, create it.
473 ret = mkdir(cddb_data->cache_dir, 0755);
474 #ifdef __MINGW32__
475 if (ret < 0 && errno != EEXIST) {
476 #else
477 if (ret < 0) {
478 #endif
479 perror("mkdir");
480 mp_msg(MSGT_DEMUX, MSGL_ERR,
481 MSGTR_MPDEMUX_CDDB_FailedToCreateDirectory,
482 cddb_data->cache_dir);
483 return -1;
487 sprintf(file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
489 file_fd = creat(file_name, S_IRUSR | S_IWUSR);
490 if (file_fd < 0) {
491 perror("create");
492 return -1;
495 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
496 if (wrote < 0) {
497 perror("write");
498 close(file_fd);
499 return -1;
501 if ((unsigned int) wrote != cddb_data->xmcd_file_size) {
502 mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_CDDB_NotAllXMCDFileHasBeenWritten);
503 close(file_fd);
504 return -1;
507 close(file_fd);
509 return 0;
512 static int cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
514 unsigned long disc_id;
515 char category[100];
516 char *ptr = NULL, *ptr2 = NULL;
517 int ret, status;
519 if (http_hdr == NULL || cddb_data == NULL)
520 return -1;
522 ret = sscanf(http_hdr->body, "%d ", &status);
523 if (ret != 1) {
524 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
525 return -1;
528 switch (status) {
529 case 210:
530 ret = sscanf(http_hdr->body, "%d %99s %08lx", &status,
531 category, &disc_id);
532 if (ret != 3) {
533 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
534 return -1;
536 // Check if it's a xmcd database file
537 ptr = strstr(http_hdr->body, "# xmcd");
538 if (ptr == NULL) {
539 mp_msg(MSGT_DEMUX, MSGL_ERR,
540 MSGTR_MPDEMUX_CDDB_InvalidXMCDDatabaseReturned);
541 return -1;
543 ptr = strdup(ptr);
544 // Ok found the beginning of the file
545 // look for the end
546 ptr2 = strstr(ptr, "\n.\r\n");
547 if (!ptr2)
548 ptr2 = strstr(ptr, "\n.\n");
549 if (ptr2) {
550 ptr2++;
551 } else {
552 mp_msg(MSGT_DEMUX, MSGL_FIXME, "Unable to find '.'\n");
553 ptr2 = ptr + strlen(ptr); //return -1;
555 // Ok found the end
556 // do a sanity check
557 if (http_hdr->body_size < (unsigned int)(ptr2 - ptr)) {
558 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_UnexpectedFIXME);
559 return -1;
561 cddb_data->xmcd_file = ptr;
562 cddb_data->xmcd_file_size = ptr2 - ptr;
563 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
564 return cddb_write_cache(cddb_data);
565 default:
566 mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode);
568 return 0;
571 static int cddb_request_titles(cddb_data_t *cddb_data)
573 char command[1024];
574 sprintf(command, "cddb+read+%s+%08lx",
575 cddb_data->category, cddb_data->disc_id);
576 return cddb_http_request(command, cddb_read_parse, cddb_data);
579 static int cddb_parse_matches_list(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
581 char album_title[100];
582 char *ptr = NULL;
583 int ret;
585 ptr = strstr(http_hdr->body, "\n");
586 if (ptr == NULL) {
587 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_UnableToFindEOL);
588 return -1;
590 ptr++;
591 // We have a list of exact/inexact matches, so which one do we use?
592 // So let's take the first one.
593 ret = sscanf(ptr, "%99s %08lx %99s", cddb_data->category,
594 &(cddb_data->disc_id), album_title);
595 if (ret != 3) {
596 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
597 return -1;
599 ptr = strstr(http_hdr->body, album_title);
600 if (ptr != NULL) {
601 char *ptr2;
602 int len;
603 ptr2 = strstr(ptr, "\n");
604 if (ptr2 == NULL) {
605 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
606 } else {
607 len = ptr2-ptr+1;
609 len = FFMIN(sizeof(album_title) - 1, len);
610 strncpy(album_title, ptr, len);
611 album_title[len]='\0';
613 mp_msg(MSGT_DEMUX, MSGL_STATUS, MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle,
614 album_title);
615 return 0;
618 static int cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
620 char album_title[100];
621 char *ptr = NULL;
622 int ret, status;
624 ret = sscanf(http_hdr->body, "%d ", &status);
625 if (ret != 1) {
626 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
627 return -1;
630 switch (status) {
631 case 200:
632 // Found exact match
633 ret = sscanf(http_hdr->body, "%d %99s %08lx %99s", &status,
634 cddb_data->category, &(cddb_data->disc_id), album_title);
635 if (ret != 4) {
636 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
637 return -1;
639 ptr = strstr(http_hdr->body, album_title);
640 if (ptr != NULL) {
641 char *ptr2;
642 int len;
643 ptr2 = strstr(ptr, "\n");
644 if (ptr2 == NULL) {
645 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
646 } else {
647 len = ptr2-ptr+1;
649 len = FFMIN(sizeof(album_title) - 1, len);
650 strncpy(album_title, ptr, len);
651 album_title[len]='\0';
653 mp_msg(MSGT_DEMUX, MSGL_STATUS,
654 MSGTR_MPDEMUX_CDDB_ParseOKFoundAlbumTitle, album_title);
655 return cddb_request_titles(cddb_data);
656 case 202:
657 // No match found
658 mp_msg(MSGT_DEMUX, MSGL_WARN, MSGTR_MPDEMUX_CDDB_AlbumNotFound);
659 break;
660 case 210:
661 // Found exact matches, list follows
662 cddb_parse_matches_list(http_hdr, cddb_data);
663 return cddb_request_titles(cddb_data);
665 body=[210 Found exact matches, list follows (until terminating `.')
666 misc c711930d Santana / Supernatural
667 rock c711930d Santana / Supernatural
668 blues c711930d Santana / Supernatural
671 case 211:
672 // Found inexact matches, list follows
673 cddb_parse_matches_list(http_hdr, cddb_data);
674 return cddb_request_titles(cddb_data);
675 case 500:
676 mp_msg(MSGT_DEMUX, MSGL_FIXME,
677 MSGTR_MPDEMUX_CDDB_ServerReturnsCommandSyntaxErr);
678 break;
679 default:
680 mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode);
682 return -1;
685 static int cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
687 int max;
688 int ret, status;
689 char *ptr;
691 ret = sscanf(http_hdr->body, "%d ", &status);
692 if (ret != 1) {
693 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
694 return -1;
697 switch (status) {
698 case 210:
699 ptr = strstr(http_hdr->body, "max proto:");
700 if (ptr == NULL) {
701 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
702 return -1;
704 ret = sscanf(ptr, "max proto: %d", &max);
705 if (ret != 1) {
706 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
707 return -1;
709 cddb_data->freedb_proto_level = max;
710 return 0;
711 default:
712 mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode);
714 return -1;
717 static int cddb_get_proto_level(cddb_data_t *cddb_data)
719 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
722 static int cddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data)
724 int ret, status;
726 ret = sscanf(http_hdr->body, "%d ", &status);
727 if (ret != 1) {
728 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_ParseError);
729 return -1;
732 switch (status) {
733 case 210:
734 // TODO: Parse the sites
735 ret = cddb_data->anonymous; // For gcc complaining about unused parameter.
736 return 0;
737 case 401:
738 mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_NoSitesInfoAvailable);
739 break;
740 default:
741 mp_msg(MSGT_DEMUX, MSGL_FIXME, MSGTR_MPDEMUX_CDDB_UnhandledCode);
743 return -1;
746 static int cddb_get_freedb_sites(cddb_data_t *cddb_data)
748 return cddb_http_request("sites", cddb_freedb_sites_parse, cddb_data);
751 static void cddb_create_hello(cddb_data_t *cddb_data)
753 char host_name[51];
754 char *user_name;
756 if (cddb_data->anonymous) { // Default is anonymous
757 /* Note from Eduardo Pérez Ureta <eperez@it.uc3m.es> :
758 * We don't send current user/host name in hello to prevent spam.
759 * Software that sends this is considered spyware
760 * that most people don't like.
762 user_name = "anonymous";
763 strcpy(host_name, "localhost");
764 } else {
765 if (gethostname(host_name, 50) < 0) {
766 strcpy(host_name, "localhost");
768 user_name = getenv("LOGNAME");
770 sprintf(cddb_data->cddb_hello, "&hello=%s+%s+%s+%s",
771 user_name, host_name, "MPlayer", VERSION);
774 static int cddb_retrieve(cddb_data_t *cddb_data)
776 char offsets[1024], command[1024];
777 char *ptr;
778 unsigned int i, time_len;
779 int ret;
781 ptr = offsets;
782 for (i = 0; i < cddb_data->tracks ; i++) {
783 ptr += sprintf(ptr, "%d+", cdtoc[i].frame);
784 if (ptr-offsets > sizeof offsets - 40) break;
786 ptr[0] = 0;
787 time_len = (cdtoc[cddb_data->tracks].frame)/75;
789 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
790 cddb_data->freedb_proto_level = 1;
791 cddb_data->xmcd_file = NULL;
793 cddb_create_hello(cddb_data);
794 if (cddb_get_proto_level(cddb_data) < 0) {
795 mp_msg(MSGT_DEMUX, MSGL_ERR,
796 MSGTR_MPDEMUX_CDDB_FailedToGetProtocolLevel);
797 return -1;
800 //cddb_get_freedb_sites(&cddb_data);
802 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id,
803 cddb_data->tracks, offsets, time_len);
804 ret = cddb_http_request(command, cddb_query_parse, cddb_data);
805 if (ret < 0)
806 return -1;
808 if (cddb_data->cache_dir != NULL) {
809 free(cddb_data->cache_dir);
811 return 0;
814 int cddb_resolve(const char *dev, char **xmcd_file)
816 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
817 char *home_dir = NULL;
818 cddb_data_t cddb_data;
820 if (cdtoc_last_track <= 0) {
821 cdtoc_last_track = read_toc(dev);
822 if (cdtoc_last_track < 0) {
823 mp_msg(MSGT_OPEN, MSGL_ERR, MSGTR_MPDEMUX_CDDB_FailedToOpenDevice,
824 dev);
825 return -1;
828 cddb_data.tracks = cdtoc_last_track;
829 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
830 cddb_data.anonymous = 1; // Don't send user info by default
832 mp_msg(MSGT_IDENTIFY, MSGL_INFO, "ID_CDDB_DISCID=%08lx\n",
833 cddb_data.disc_id);
835 // Check if there is a CD in the drive
836 // FIXME: That's not really a good way to check
837 if (cddb_data.disc_id == 0) {
838 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MPDEMUX_CDDB_NoCDInDrive);
839 return -1;
842 home_dir = getenv("HOME");
843 #ifdef __MINGW32__
844 if (home_dir == NULL)
845 home_dir = getenv("USERPROFILE");
846 if (home_dir == NULL)
847 home_dir = getenv("HOMEPATH");
848 // Last resort, store the cddb cache in the mplayer directory
849 if (home_dir == NULL)
850 home_dir = (char *)get_path("");
851 #endif
852 if (home_dir == NULL) {
853 cddb_data.cache_dir = NULL;
854 } else {
855 cddb_data.cache_dir = malloc(strlen(home_dir)
856 + strlen(cddb_cache_dir) + 1);
857 if (cddb_data.cache_dir == NULL) {
858 mp_msg(MSGT_DEMUX, MSGL_ERR, MSGTR_MemAllocFailed);
859 return -1;
861 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir);
864 // Check for a cached file
865 if (cddb_read_cache(&cddb_data) < 0) {
866 // No Cache found
867 if (cddb_retrieve(&cddb_data) < 0) {
868 return -1;
872 if (cddb_data.xmcd_file != NULL) {
873 // printf("%s\n", cddb_data.xmcd_file);
874 *xmcd_file = cddb_data.xmcd_file;
875 return 0;
878 return -1;
881 /***************
882 * xmcd parser *
883 ***************/
884 static char *xmcd_parse_dtitle(cd_info_t *cd_info, char *line)
886 char *ptr, *album;
887 ptr = strstr(line, "DTITLE=");
888 if (ptr != NULL) {
889 ptr += 7;
890 album = strstr(ptr, "/");
891 if (album == NULL)
892 return NULL;
893 cd_info->album = malloc(strlen(album + 2) + 1);
894 if (cd_info->album == NULL) {
895 return NULL;
897 strcpy(cd_info->album, album + 2);
898 album--;
899 album[0] = '\0';
900 cd_info->artist = malloc(strlen(ptr) + 1);
901 if (cd_info->artist == NULL) {
902 return NULL;
904 strcpy(cd_info->artist, ptr);
906 return ptr;
909 static char *xmcd_parse_dgenre(cd_info_t *cd_info, char *line)
911 char *ptr;
912 ptr = strstr(line, "DGENRE=");
913 if (ptr != NULL) {
914 ptr += 7;
915 cd_info->genre = malloc(strlen(ptr)+1);
916 if (cd_info->genre == NULL) {
917 return NULL;
919 strcpy(cd_info->genre, ptr);
921 return ptr;
924 static char *xmcd_parse_ttitle(cd_info_t *cd_info, char *line)
926 unsigned int track_nb;
927 unsigned long sec, off;
928 char *ptr;
929 ptr = strstr(line, "TTITLE");
930 if (ptr != NULL) {
931 ptr += 6;
932 // Here we point to the track number
933 track_nb = atoi(ptr);
934 ptr = strstr(ptr, "=");
935 if (ptr == NULL)
936 return NULL;
937 ptr++;
939 sec = cdtoc[track_nb].frame;
940 off = cdtoc[track_nb + 1].frame - sec + 1;
942 cd_info_add_track(cd_info, ptr, track_nb + 1,
943 (unsigned int) (off / (60 * 75)),
944 (unsigned int) ((off / 75) % 60),
945 (unsigned int) (off % 75),
946 sec, off);
948 return ptr;
951 cd_info_t *cddb_parse_xmcd(char *xmcd_file)
953 cd_info_t *cd_info = NULL;
954 int length, pos = 0;
955 char *ptr, *ptr2;
956 unsigned int audiolen;
957 if (xmcd_file == NULL)
958 return NULL;
960 cd_info = cd_info_new();
961 if (cd_info == NULL) {
962 return NULL;
965 length = strlen(xmcd_file);
966 ptr = xmcd_file;
967 while (ptr != NULL && pos < length) {
968 // Read a line
969 ptr2 = ptr;
970 while(ptr2[0] != '\0' && ptr2[0] != '\r' && ptr2[0] != '\n')
971 ptr2++;
972 if (ptr2[0] == '\0') {
973 break;
975 ptr2[0] = '\0';
976 // Ignore comments
977 if (ptr[0] != '#') {
978 // Search for the album title
979 if (xmcd_parse_dtitle(cd_info, ptr))
981 // Search for the genre
982 else if (xmcd_parse_dgenre(cd_info, ptr))
984 // Search for a track title
985 else if (xmcd_parse_ttitle(cd_info, ptr))
986 audiolen++; // <-- audiolen++ to shut up gcc warning
988 if (ptr2[1] == '\n')
989 ptr2++;
990 pos = (ptr2 + 1) - ptr;
991 ptr = ptr2 + 1;
994 audiolen = cdtoc[cd_info->nb_tracks].frame-cdtoc[0].frame;
995 cd_info->min = (unsigned int) (audiolen / (60 * 75));
996 cd_info->sec = (unsigned int) ((audiolen / 75) % 60);
997 cd_info->msec = (unsigned int) (audiolen % 75);
999 return cd_info;