1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include "icy.h"
6 #include "tool.h"
7 #include "debug.h"
8
readicyheaders(char * headers)9 icyHeaders *readicyheaders(char *headers)
10 {
11 icyHeaders *icy;
12 int i, j, k;
13 char c;
14 char line[50][1024]; /* une ligne ne peut d�passer 1024 octets. */
15 int linecount;
16 char prefix[512];
17 char suffix[512];
18 int state = 0;
19 int headerslen = 0;
20
21 prefix[0] = 0;
22 suffix[0] = 0;
23
24 if (headers == NULL)
25 return NULL;
26 else
27 headerslen = strlen(headers);
28
29 icy = (icyHeaders *)memory_allocation(sizeof(icyHeaders));
30
31 /* Usefull when radio doesn't specify icy name */
32
33 icy->name = NULL;
34 icy->content_type = NULL;
35 icy->url = NULL;
36 icy->notice1 = NULL;
37 icy->notice2 = NULL;
38 icy->genre = NULL;
39 icy->metaint = 0;
40
41 for(i = 0, j = 0, linecount = 0; i < headerslen; i++)
42 {
43 if (linecount >= 50) break;
44 if (j >= 1023)
45 {
46 line[linecount][j] = 0;
47 linecount++;
48 j = 0;
49 }
50
51 c = headers[i];
52
53 if ((c == '\r') && (j == 0))
54 break;
55
56 if (c == '\n')
57 {
58 line[linecount][j] = '\n';
59 line[linecount][j+1] = '\0';
60 linecount++;
61 j = 0;
62 }
63 else if (c != '\r')
64 line[linecount][j++] = c;
65 }
66
67 for (i = 0; i < linecount; i++)
68 {
69 prefix[0] = 0;
70 suffix[0] = 0;
71 k = 0;
72 state = 0;
73
74 for (j = 0; j < strlen(line[i]); j++)
75 {
76 c = line[i][j];
77
78 if (state == 0)
79 {
80 if (c == ':')
81 {
82 prefix[k] = '\0';
83 k = 0;
84 state = 1;
85 } else
86 prefix[k++] = c;
87
88 } else if (state == 1) { /* On a rencontr� les ":" */
89 if (c != '\n')
90 {
91 if ((c == ' ') && (k == 0))
92 continue;
93 suffix[k++] = c;
94 } else
95 suffix[k] = '\0';
96 }
97 } /* for each linechar */
98
99 if ((prefix[0] != '\0') && (suffix[0] != '\0'))
100 {
101 if (strncmp(prefix, "icy-notice1", 11) == 0)
102 icy->notice1 = strdup(suffix);
103
104 else if (strncmp(prefix, "icy-notice2", 11) == 0)
105 {
106 if (strstr("SHOUTcast", suffix) != NULL)
107 icy->type = SERVERTYPE_SHOUTCAST;
108
109 icy->notice2 = strdup(suffix);
110 }
111
112 else if (strncmp(prefix, "icy-name", 8) == 0)
113 icy->name = strdup(suffix);
114
115 else if (strncmp(prefix, "icy-genre", 9) == 0)
116 icy->genre = strdup(suffix);
117
118 else if (strncmp(prefix, "icy-url", 7) == 0)
119 icy->url = strdup(suffix);
120
121 else if (strncmp(prefix, "icy-pub", 7) == 0)
122 icy->pub = (int) atoi((char *) &suffix);
123
124 else if (strncmp(prefix, "icy-metaint", 11) == 0)
125 icy->metaint = (int) atoi((char*) &suffix);
126
127 else if (strncmp(prefix, "icy-br", 6) == 0 )
128 icy->br = (int) atoi((char *) &suffix[0]);
129
130 else if ((strncmp(prefix, "content-type", 12) == 0 ) ||
131 (strncmp(prefix, "Content-Type", 12) == 0))
132 icy->content_type = strdup(suffix);
133
134 else if (strncmp(prefix, "Server", 6) == 0)
135 {
136 if (strstr(suffix, "Icecast") != NULL)
137 icy->type = SERVERTYPE_ICECAST;
138 }
139 }
140 } /* for eachline */
141
142
143 if (icy->name == NULL)
144 icy->name = strdup("No Name");
145
146 if (icy->content_type == NULL)
147 icy->content_type = strdup("audio/mpeg");
148
149 if (icy->metaint != 0)
150 VERBOSE("Using metaint: %d\n", icy->metaint);
151 else {
152 VERBOSE("Using default metaint: %d\n", 32 * 1024);
153 icy->metaint = 32*1024;
154 }
155
156 free(headers);
157 return icy;
158 }
159
free_icy_headers(icyHeaders * icy)160 void free_icy_headers(icyHeaders *icy)
161 {
162 if (! icy) return;
163 if (icy->name) free(icy->name);
164 if (icy->genre) free(icy->genre);
165 if (icy->notice1) free(icy->notice1);
166 if (icy->notice2) free(icy->notice2);
167 if (icy->url) free(icy->url);
168 if (icy->content_type) free(icy->content_type);
169 }
170
print_icyheaders(icyHeaders * _icy_headers)171 void print_icyheaders(icyHeaders *_icy_headers )
172 {
173 if (_icy_headers == NULL)
174 return;
175
176 printf("metaint: %d\n", _icy_headers->metaint);
177 printf("bitrate: %d\n", _icy_headers->br);
178 printf("icy-notice1: %s\n", _icy_headers->notice1);
179 printf("icy-notice2: %s\n", _icy_headers->notice2);
180 printf("icy-name: %s\n", _icy_headers->name);
181 printf("icy-genre: %s\n", _icy_headers->genre);
182 printf("icy-url: %s\n", _icy_headers->url);
183 printf("content_type: %s\n", _icy_headers->content_type);
184 }
185