annotate libmpdemux/cddb.c @ 6504:11c0ddb83168

use built-in yvu9->yv12 code, because it requires all src/dst strides and offsets, the params passed to postproc's converter is simply not enough... (temp. solution, waiting for yvu9 support in swscale)
author arpi
date Sat, 22 Jun 2002 23:05:00 +0000
parents 837ca6fd4adf
children 4cade272ce2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
1 /*
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
2 * CDDB HTTP protocol
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
3 * by Bertrand Baudet <bertrand_baudet@yahoo.com>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
4 * (C) 2002, MPlayer team.
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
5 *
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
6 * Implementation follow the freedb.howto1.06.txt specification
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
7 * from http://freedb.freedb.org
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
8 *
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
9 * discid computation by Jeremy D. Zawodny
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
10 * Copyright (c) 1998-2000 Jeremy D. Zawodny <Jeremy@Zawodny.com>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
11 * Code release under GPL
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
12 *
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
13 * TODO:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
14 * * do a xmcd parser
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
15 */
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
16
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
17 #include "config.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
18
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
19 #if defined(HAVE_CDDA) && defined(STREAMING)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
20
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
21 #include <stdio.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
22 #include <stdlib.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
23 #include <fcntl.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
24 #include <stdarg.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
25 #include <errno.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
26 #include <netdb.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
27 #include <unistd.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
28 #include <string.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
29 #include <sys/ioctl.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
30 #include <sys/types.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
31 #include <sys/stat.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
32
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
33 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
34 #define SYS_BSD 1
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
35 #endif
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
36
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
37 #if defined(__linux__)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
38 #include <linux/cdrom.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
39 #elif defined(SYS_BSD)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
40 #include <sys/cdio.h>
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
41 #endif
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
42
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
43 #include "../version.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
44 #include "stream.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
45 #include "network.h"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
46
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
47 #define DEFAULT_FREEDB_SERVER "freedb.freedb.org"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
48 #define DEFAULT_CACHE_DIR "/.cddb/"
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
49
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
50 stream_t* open_cdda(char *dev, char *track);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
51
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
52 typedef struct {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
53 char cddb_hello[1024];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
54 unsigned long disc_id;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
55 unsigned int tracks;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
56 char *cache_dir;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
57 char *freedb_server;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
58 int freedb_proto_level;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
59 char category[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
60 char *xmcd_file;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
61 size_t xmcd_file_size;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
62 void *user_data;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
63 } cddb_data_t;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
64
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
65
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
66 struct toc {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
67 int min, sec, frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
68 } cdtoc[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
69
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
70 #if defined(__linux__)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
71 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
72 read_toc(void) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
73 int drive = open("/dev/cdrom", O_RDONLY | O_NONBLOCK);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
74 struct cdrom_tochdr tochdr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
75 struct cdrom_tocentry tocentry;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
76 int i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
77
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
78 ioctl(drive, CDROMREADTOCHDR, &tochdr);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
79 for (i = tochdr.cdth_trk0; i <= tochdr.cdth_trk1; i++) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
80 tocentry.cdte_track = i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
81 tocentry.cdte_format = CDROM_MSF;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
82 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
83 cdtoc[i-1].min = tocentry.cdte_addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
84 cdtoc[i-1].sec = tocentry.cdte_addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
85 cdtoc[i-1].frame = tocentry.cdte_addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
86 cdtoc[i-1].frame += cdtoc[i-1].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
87 cdtoc[i-1].frame += cdtoc[i-1].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
88 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
89 tocentry.cdte_track = 0xAA;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
90 tocentry.cdte_format = CDROM_MSF;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
91 ioctl(drive, CDROMREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
92 cdtoc[tochdr.cdth_trk1].min = tocentry.cdte_addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
93 cdtoc[tochdr.cdth_trk1].sec = tocentry.cdte_addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
94 cdtoc[tochdr.cdth_trk1].frame = tocentry.cdte_addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
95 cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
96 cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
97 close(drive);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
98 return tochdr.cdth_trk1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
99 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
100
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
101 #elif defined(SYS_BSD)
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
102 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
103 read_toc(void) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
104 int drive = open("/dev/acd0c", O_RDONLY | O_NONBLOCK);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
105 struct ioc_toc_header tochdr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
106 struct ioc_read_toc_single_entry tocentry;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
107 int i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
108
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
109 ioctl(drive, CDIOREADTOCHEADER, &tochdr);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
110 for (i = tochdr.starting_track; i <= tochdr.ending_track; i++) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
111 tocentry.track = i;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
112 tocentry.address_format = CD_MSF_FORMAT;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
113 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
114 cdtoc[i-1].min = tocentry.entry.addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
115 cdtoc[i-1].sec = tocentry.entry.addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
116 cdtoc[i-1].frame = tocentry.entry.addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
117 cdtoc[i-1].frame += cdtoc[i-1].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
118 cdtoc[i-1].frame += cdtoc[i-1].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
119 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
120 tocentry.track = 0xAA;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
121 tocentry.address_format = CD_MSF_FORMAT;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
122 ioctl(drive, CDIOREADTOCENTRY, &tocentry);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
123 cdtoc[tochdr.ending_track].min = tocentry.entry.addr.msf.minute;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
124 cdtoc[tochdr.ending_track].sec = tocentry.entry.addr.msf.second;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
125 cdtoc[tochdr.ending_track].frame = tocentry.entry.addr.msf.frame;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
126 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].min*60*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
127 cdtoc[tochdr.ending_track].frame += cdtoc[tochdr.ending_track].sec*75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
128 close(drive);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
129 return tochdr.ending_track;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
130 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
131 #endif
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
132
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
133 unsigned int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
134 cddb_sum(int n) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
135 unsigned int ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
136
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
137 ret = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
138 while (n > 0) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
139 ret += (n % 10);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
140 n /= 10;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
141 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
142 return ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
143 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
144
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
145 unsigned long
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
146 cddb_discid(int tot_trks) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
147 unsigned int i, t = 0, n = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
148
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
149 i = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
150 while (i < tot_trks) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
151 n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
152 i++;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
153 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
154 t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
155 ((cdtoc[0].min * 60) + cdtoc[0].sec);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
156 return ((n % 0xff) << 24 | t << 8 | tot_trks);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
157 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
158
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
159
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
160
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
161 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
162 cddb_http_request(char *command, int (*reply_parser)(HTTP_header_t*,cddb_data_t*), cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
163 char request[4096];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
164 int fd, ret = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
165 URL_t *url;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
166 HTTP_header_t *http_hdr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
167
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
168 if( reply_parser==NULL || command==NULL || cddb_data==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
169
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
170 sprintf( request, "http://%s/~cddb/cddb.cgi?cmd=%s%s&proto=%d", cddb_data->freedb_server, command, cddb_data->cddb_hello, cddb_data->freedb_proto_level );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
171 printf("Request[%s]\n", request );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
172
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
173 url = url_new(request);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
174 if( url==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
175 printf("Not a valid URL\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
176 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
177 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
178
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
179 fd = http_send_request(url);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
180 if( fd<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
181 printf("failed to send the http request\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
182 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
183 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
184
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
185 http_hdr = http_read_response( fd );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
186 if( http_hdr==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
187 printf("Failed to read the http response\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
188 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
189 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
190
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
191 http_debug_hdr(http_hdr);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
192 printf("body=[%s]\n", http_hdr->body );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
193
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
194 switch(http_hdr->status_code) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
195 case 200:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
196 ret = reply_parser(http_hdr, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
197 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
198 case 400:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
199 printf("Not Found\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
200 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
201 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
202 printf("Unknown Error code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
203 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
204
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
205 http_free( http_hdr );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
206 url_free( url );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
207
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
208 return ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
209 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
210
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
211 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
212 cddb_read_cache(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
213 char file_name[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
214 struct stat stats;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
215 int file_fd, ret;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
216 size_t file_size;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
217
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
218 if( cddb_data==NULL || cddb_data->cache_dir==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
219
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
220 sprintf( file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
221
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
222 file_fd = open(file_name, O_RDONLY);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
223 if( file_fd<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
224 printf("No cache found\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
225 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
226 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
227
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
228 ret = fstat( file_fd, &stats );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
229 if( ret<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
230 perror("fstat");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
231 file_size = 4096;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
232 } else {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
233 file_size = stats.st_size;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
234 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
235
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
236 cddb_data->xmcd_file = (char*)malloc(file_size);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
237 if( cddb_data->xmcd_file==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
238 printf("Memory allocation failed\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
239 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
240 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
241 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
242 cddb_data->xmcd_file_size = read(file_fd, cddb_data->xmcd_file, file_size);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
243 if( cddb_data->xmcd_file_size!=file_size ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
244 printf("Not all the xmcd file has been read\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
245 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
246 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
247 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
248
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
249 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
250
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
251 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
252 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
253
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
254 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
255 cddb_write_cache(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
256 // We have the file, save it for cache.
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
257 char file_name[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
258 int file_fd;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
259 size_t wrote=0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
260
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
261 if( cddb_data==NULL || cddb_data->cache_dir==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
262
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
263 sprintf( file_name, "%s%08lx", cddb_data->cache_dir, cddb_data->disc_id);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
264
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
265 file_fd = creat(file_name, S_IREAD|S_IWRITE);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
266 if( file_fd<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
267 perror("open");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
268 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
269 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
270
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
271 wrote = write(file_fd, cddb_data->xmcd_file, cddb_data->xmcd_file_size);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
272 if( wrote<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
273 perror("write");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
274 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
275 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
276 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
277 if( wrote!=cddb_data->xmcd_file_size ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
278 printf("Not all the xmcd file has been written\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
279 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
280 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
281 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
282
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
283 close(file_fd);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
284
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
285 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
286 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
287
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
288 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
289 cddb_read_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
290 unsigned long disc_id;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
291 char category[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
292 char *ptr=NULL, *ptr2=NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
293 int ret, status;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
294
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
295 if( http_hdr==NULL || cddb_data==NULL ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
296
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
297 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
298 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
299 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
300 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
301 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
302
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
303 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
304 case 210:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
305 ret = sscanf( http_hdr->body, "%d %s %08lx", &status, category, &disc_id);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
306 if( ret!=3 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
307 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
308 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
309 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
310 // Check if it's a xmcd database file
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
311 ptr = strstr(http_hdr->body, "# xmcd");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
312 if( ptr==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
313 printf("Invalid xmcd database file returned\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
314 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
315 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
316 // Ok found the beginning of the file
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
317 // look for the end
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
318 ptr2 = strstr(ptr, "\r\n.\r\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
319 if( ptr2==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
320 ptr2 = strstr(ptr, "\n.\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
321 if( ptr2==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
322 printf("Unable to find '.'\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
323 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
324 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
325 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
326 // Ok found the end
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
327 // do a sanity check
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
328 if( http_hdr->body_size<(ptr2-ptr) ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
329 printf("Unexpected fix me\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
330 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
331 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
332 cddb_data->xmcd_file = ptr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
333 cddb_data->xmcd_file_size = ptr2-ptr+2;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
334 cddb_data->xmcd_file[cddb_data->xmcd_file_size] = '\0';
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
335 // Avoid the http_free function to free the xmcd file...save a mempcy...
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
336 http_hdr->body = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
337 http_hdr->body_size = 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
338 return cddb_write_cache(cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
339 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
340 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
341 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
342 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
343 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
344
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
345 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
346 cddb_request_titles(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
347 char command[1024];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
348 sprintf( command, "cddb+read+%s+%08lx", cddb_data->category, cddb_data->disc_id);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
349 return cddb_http_request(command, cddb_read_parse, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
350 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
351
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
352 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
353 cddb_query_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
354 char album_title[100];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
355 char *ptr = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
356 int ret, status;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
357
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
358 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
359 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
360 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
361 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
362 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
363
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
364 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
365 case 200:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
366 // Found exact match
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
367 ret = sscanf(http_hdr->body, "%d %s %08lx %s", &status, cddb_data->category, &(cddb_data->disc_id), album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
368 if( ret!=4 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
369 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
370 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
371 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
372 ptr = strstr(http_hdr->body, album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
373 if( ptr!=NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
374 char *ptr2;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
375 int len;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
376 ptr2 = strstr(ptr, "\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
377 if( ptr2==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
378 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
379 } else {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
380 len = ptr2-ptr+1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
381 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
382 strncpy(album_title, ptr, len);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
383 album_title[len-2]='\0';
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
384 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
385 printf("Parse OK, found: %s\n", album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
386 return cddb_request_titles(cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
387 case 202:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
388 // No match found
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
389 printf("Album not found\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
390 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
391 case 210:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
392 // Found exact matches, list follows
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
393 ptr = strstr(http_hdr->body, "\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
394 if( ptr==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
395 printf("Unable to find end of line\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
396 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
397 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
398 ptr++;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
399 // We have a list of exact matches, so which one do
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
400 // we use? So let's take the first one.
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
401 ret = sscanf(ptr, "%s %08lx %s", cddb_data->category, &(cddb_data->disc_id), album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
402 if( ret!=3 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
403 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
404 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
405 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
406 ptr = strstr(http_hdr->body, album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
407 if( ptr!=NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
408 char *ptr2;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
409 int len;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
410 ptr2 = strstr(ptr, "\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
411 if( ptr2==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
412 len = (http_hdr->body_size)-(ptr-(http_hdr->body));
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
413 } else {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
414 len = ptr2-ptr+1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
415 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
416 strncpy(album_title, ptr, len);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
417 album_title[len-2]='\0';
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
418 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
419 printf("Parse OK, found: %s\n", album_title);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
420 return cddb_request_titles(cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
421 /*
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
422 body=[210 Found exact matches, list follows (until terminating `.')
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
423 misc c711930d Santana / Supernatural
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
424 rock c711930d Santana / Supernatural
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
425 blues c711930d Santana / Supernatural
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
426 .]
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
427 */
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
428 case 211:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
429 // Found inexact matches, list follows
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
430 printf("No exact matches found, list follows\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
431 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
432 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
433 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
434 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
435 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
436 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
437
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
438 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
439 cddb_proto_level_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
440 int max;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
441 int ret, status;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
442 char *ptr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
443
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
444 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
445 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
446 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
447 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
448 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
449
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
450 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
451 case 210:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
452 ptr = strstr(http_hdr->body, "max proto:");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
453 if( ptr==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
454 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
455 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
456 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
457 ret = sscanf(ptr, "max proto: %d", &max);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
458 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
459 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
460 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
461 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
462 cddb_data->freedb_proto_level = max;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
463 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
464 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
465 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
466 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
467 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
468 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
469
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
470 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
471 cddb_get_proto_level(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
472 return cddb_http_request("stat", cddb_proto_level_parse, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
473 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
475 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
476 cddb_freedb_sites_parse(HTTP_header_t *http_hdr, cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
477 int ret, status;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
478
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
479 ret = sscanf( http_hdr->body, "%d ", &status);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
480 if( ret!=1 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
481 printf("Parse error\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
482 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
483 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
484
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
485 switch(status) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
486 case 210:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
487 // Parse the sites
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
488 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
489 case 401:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
490 printf("No sites information available\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
491 break;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
492 default:
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
493 printf("Unhandled code\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
494 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
495 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
496 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
497
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
498 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
499 cddb_get_freedb_sites(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
500 return cddb_http_request("sites", cddb_freedb_sites_parse, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
501 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
502
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
503 void
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
504 cddb_create_hello(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
505 char host_name[51];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
506 char *user_name;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
507
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
508 if( gethostname(host_name, 50)<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
509 strcpy(host_name, "localhost");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
510 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
511 user_name = getenv("LOGNAME");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
512 sprintf( cddb_data->cddb_hello, "&hello=%s+%s+%s+%s", user_name, host_name, "MPlayer", VERSION );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
513 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
514
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
515 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
516 cddb_retrieve(cddb_data_t *cddb_data) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
517 char offsets[1024], command[1024];
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
518 char *ptr;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
519 int i, time_len;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
520
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
521 ptr = offsets;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
522 for( i=0; i<cddb_data->tracks ; i++ ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
523 ptr += sprintf(ptr, "%d+", cdtoc[i].frame );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
524 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
525 time_len = (cdtoc[cddb_data->tracks].frame)/75;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
526
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
527 cddb_data->freedb_server = DEFAULT_FREEDB_SERVER;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
528 cddb_data->freedb_proto_level = 1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
529 cddb_data->xmcd_file = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
530
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
531 cddb_create_hello(cddb_data);
6475
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
532 if( cddb_get_proto_level(cddb_data)<0 ) {
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
533 printf("Failed to get the protocol level\n");
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
534 return -1;
837ca6fd4adf Checked the return value when retrieving the protocol level.
bertrand
parents: 6474
diff changeset
535 }
6474
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
536
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
537 //cddb_get_freedb_sites(&cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
538
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
539 sprintf(command, "cddb+query+%08lx+%d+%s%d", cddb_data->disc_id, cddb_data->tracks, offsets, time_len );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
540 i = cddb_http_request(command, cddb_query_parse, cddb_data);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
541 if( i<0 ) return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
542
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
543 if( cddb_data->cache_dir!=NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
544 free(cddb_data->cache_dir);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
545 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
546 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
547 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
548
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
549 int
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
550 cddb_resolve(char *xmcd_file) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
551 char cddb_cache_dir[] = DEFAULT_CACHE_DIR;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
552 char *home_dir = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
553 cddb_data_t cddb_data;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
554
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
555 cddb_data.tracks = read_toc();
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
556 cddb_data.disc_id = cddb_discid(cddb_data.tracks);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
557
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
558 home_dir = getenv("HOME");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
559 if( home_dir==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
560 cddb_data.cache_dir = NULL;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
561 } else {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
562 cddb_data.cache_dir = (char*)malloc(strlen(home_dir)+strlen(cddb_cache_dir)+1);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
563 if( cddb_data.cache_dir==NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
564 printf("Memory allocation failed\n");
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
565 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
566 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
567 sprintf(cddb_data.cache_dir, "%s%s", home_dir, cddb_cache_dir );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
568 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
569
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
570 // Check for a cached file
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
571 if( cddb_read_cache(&cddb_data)<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
572 // No Cache found
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
573 if( cddb_retrieve(&cddb_data)<0 ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
574 return -1;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
575 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
576 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
577
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
578 if( cddb_data.xmcd_file!=NULL ) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
579 printf("%s\n", cddb_data.xmcd_file );
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
580 xmcd_file = cddb_data.xmcd_file;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
581 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
582
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
583 return 0;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
584 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
585
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
586 stream_t*
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
587 cddb_open(char *dev, char *track) {
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
588 char *xmcd_file;
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
589 cddb_resolve(xmcd_file);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
590
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
591 return open_cdda(dev, track);
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
592 }
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
593
654b26c941e5 CDDB support added.
bertrand
parents:
diff changeset
594 #endif