util.c 10.2 KB
Newer Older
1
/*	$Id$	*/
2
/*
3 4
 *  ezstream - source client for Icecast with external en-/decoder support
 *  Copyright (C) 2003, 2004, 2005, 2006  Ed Zaleski <oddsock@oddsock.org>
moritz's avatar
moritz committed
5
 *  Copyright (C) 2007, 2009              Moritz Grimm <mdgrimm@gmx.net>
6
 *
7 8 9
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
10
 *
11 12 13 14 15 16 17 18
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 20
 */

21 22 23 24 25
/*
 * This file contains utility functions, as well as a few other unexciting
 * but verbose functions outsourced from ezstream.c to make it more readable.
 */

26 27 28 29
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

30
#include "ezstream.h"
31 32 33 34 35 36 37

#ifdef HAVE_LANGINFO_H
# include <langinfo.h>
#endif
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
38

39 40 41
#ifdef HAVE_ICONV
# include <iconv.h>
#endif
42 43
#include <shout/shout.h>

44
#include "util.h"
45
#include "configfile.h"
46
#include "xalloc.h"
47

48 49 50 51
#ifndef BUFSIZ
# define BUFSIZ 1024
#endif

52 53 54
extern EZCONFIG *pezConfig;
extern char	*__progname;

55
char *	iconvert(const char *, const char *, const char *, int);
moritz's avatar
moritz committed
56

57 58 59 60 61 62 63 64 65 66 67
int
strrcmp(const char *s, const char *sub)
{
	size_t	slen = strlen(s);
	size_t	sublen = strlen(sub);

	if (sublen > slen)
		return (1);

	return (memcmp(s + slen - sublen, sub, sublen));
}
68 69 70 71

int
strrcasecmp(const char *s, const char *sub)
{
72 73 74 75
	char	*s_cpy = xstrdup(s);
	char	*sub_cpy = xstrdup(sub);
	char	*p;
	int	 ret;
76 77 78 79 80 81 82 83 84 85 86 87 88 89

	for (p = s_cpy; *p != '\0'; p++)
		*p = tolower((int)*p);

	for (p = sub_cpy; *p != '\0'; p++)
		*p = tolower((int)*p);

	ret = strrcmp(s_cpy, sub_cpy);

	xfree(s_cpy);
	xfree(sub_cpy);

	return (ret);
}
90 91

shout_t *
92
stream_setup(const char *host, unsigned short port, const char *mount)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
{
	shout_t *shout = NULL;

	if ((shout = shout_new()) == NULL) {
		printf("%s: shout_new(): %s", __progname, strerror(ENOMEM));
		return (NULL);
	}

	if (shout_set_host(shout, host) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_host(): %s\n", __progname,
		       shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (shout_set_protocol(shout, SHOUT_PROTOCOL_HTTP) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_protocol(): %s\n", __progname,
			shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (shout_set_port(shout, port) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_port: %s\n", __progname,
			shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (shout_set_password(shout, pezConfig->password) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_password(): %s\n", __progname,
			shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (shout_set_mount(shout, mount) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_mount(): %s\n", __progname,
			shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (shout_set_user(shout, "source") != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_user(): %s\n", __progname,
			shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}

	if (!strcmp(pezConfig->format, MP3_FORMAT) &&
	    shout_set_format(shout, SHOUT_FORMAT_MP3) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_format(MP3): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if ((!strcmp(pezConfig->format, VORBIS_FORMAT) ||
	     !strcmp(pezConfig->format, THEORA_FORMAT)) &&
	    shout_set_format(shout, SHOUT_FORMAT_OGG) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_format(OGG): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}

154 155 156 157 158 159 160
	if (pezConfig->username &&
	    shout_set_user(shout, pezConfig->username) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_user(): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	if (pezConfig->serverName &&
	    shout_set_name(shout, pezConfig->serverName) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_name(): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (pezConfig->serverURL &&
	    shout_set_url(shout, pezConfig->serverURL) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_url(): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (pezConfig->serverGenre &&
	    shout_set_genre(shout, pezConfig->serverGenre) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_genre(): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (pezConfig->serverDescription &&
	    shout_set_description(shout, pezConfig->serverDescription) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_description(): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (pezConfig->serverBitrate &&
	    shout_set_audio_info(shout, SHOUT_AI_BITRATE, pezConfig->serverBitrate) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_audio_info(AI_BITRATE): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (pezConfig->serverChannels &&
	    shout_set_audio_info(shout, SHOUT_AI_CHANNELS, pezConfig->serverChannels) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_audio_info(AI_CHANNELS): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (pezConfig->serverSamplerate &&
	    shout_set_audio_info(shout, SHOUT_AI_SAMPLERATE, pezConfig->serverSamplerate) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_audio_info(AI_SAMPLERATE): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}
	if (pezConfig->serverQuality &&
	    shout_set_audio_info(shout, SHOUT_AI_QUALITY, pezConfig->serverQuality) != SHOUTERR_SUCCESS) {
		printf("%s: shout_set_audio_info(AI_QUALITY): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}

218
	if (shout_set_public(shout, (unsigned int)pezConfig->serverPublic) != SHOUTERR_SUCCESS) {
219 220 221 222 223 224 225 226
		printf("%s: shout_set_public(): %s\n",
		       __progname, shout_get_error(shout));
		shout_free(shout);
		return (NULL);
	}

	return (shout);
}
227 228

char *
229
CHARtoUTF8(const char *in_str, int mode)
230
{
231
#ifndef WIN32
moritz's avatar
moritz committed
232
	char	*codeset;
233

234
# if defined(HAVE_NL_LANGINFO) && defined(HAVE_SETLOCALE) && defined(CODESET)
235
	setlocale(LC_CTYPE, "");
236
	codeset = nl_langinfo((nl_item)CODESET);
237
	setlocale(LC_CTYPE, "C");
238
# else
239
	codeset = (char *)"";
moritz's avatar
moritz committed
240
# endif /* HAVE_NL_LANGINFO && HAVE_SETLOCALE && CODESET */
241
#else
moritz's avatar
moritz committed
242
	char	 codeset[24];
243 244

	snprintf(codeset, sizeof(codeset), "CP%u", GetACP());
245
#endif /* !WIN32 */
246

247
	return (iconvert(in_str, codeset, "UTF-8", mode));
248 249 250
}

char *
251
UTF8toCHAR(const char *in_str, int mode)
252
{
253
#ifndef WIN32
moritz's avatar
moritz committed
254
	char	*codeset;
255

256
# if defined(HAVE_NL_LANGINFO) && defined(HAVE_SETLOCALE) && defined(CODESET)
257
	setlocale(LC_CTYPE, "");
258
	codeset = nl_langinfo((nl_item)CODESET);
259
	setlocale(LC_CTYPE, "C");
260
# else
261
	codeset = (char *)"";
moritz's avatar
moritz committed
262
# endif /* HAVE_NL_LANGINFO && HAVE_SETLOCALE && CODESET */
263
#else
moritz's avatar
moritz committed
264
	char	 codeset[24];
265 266

	snprintf(codeset, sizeof(codeset), "CP%u", GetACP());
267
#endif /* !WIN32 */
268

269
	return (iconvert(in_str, "UTF-8", codeset, mode));
moritz's avatar
moritz committed
270 271
}

272 273 274 275
char *
iconvert(const char *in_str, const char *from, const char *to, int mode)
{
#ifdef HAVE_ICONV
moritz's avatar
moritz committed
276 277 278 279 280 281 282 283
	iconv_t 		 cd;
	ICONV_CONST char	*input, *ip;
	size_t			 input_len;
	char			*output;
	size_t			 output_size;
	char			 buf[BUFSIZ], *bp;
	size_t			 bufavail;
	size_t			 out_pos;
284 285
	char			*tocode;

286 287 288
	if (NULL == in_str)
		return (xstrdup(""));

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	switch (mode) {
		size_t	siz;

	case ICONV_TRANSLIT:
		siz = strlen(to) + strlen("//TRANSLIT") + 1;
		tocode = xcalloc(siz, sizeof(char));
		snprintf(tocode, siz, "%s//TRANSLIT", to);
		break;
	case ICONV_IGNORE:
		siz = strlen(to) + strlen("//IGNORE") + 1;
		tocode = xcalloc(siz, sizeof(char));
		snprintf(tocode, siz, "%s//IGNORE", to);
		break;
	case ICONV_REPLACE:
		/* FALLTHROUGH */
	default:
		tocode = xstrdup(to);
		break;
	}
moritz's avatar
moritz committed
308

309
	if ((cd = iconv_open(tocode, from)) == (iconv_t)-1 &&
310 311
	    (cd = iconv_open("", from)) == (iconv_t)-1 &&
	    (cd = iconv_open(tocode, "")) == (iconv_t)-1) {
312
		xfree(tocode);
moritz's avatar
moritz committed
313
		printf("%s: iconv_open(): %s\n", __progname, strerror(errno));
314
		return (xstrdup(in_str));
315 316 317 318 319 320 321 322 323 324 325 326 327 328
	}

	ip = input = (ICONV_CONST char *)in_str;
	input_len = strlen(input);
	output_size = 1;
	output = xcalloc(output_size, sizeof(char));
	out_pos = 0;
	output[out_pos] = '\0';
	while (input_len > 0) {
		char	*op;
		size_t	 count;

		buf[0] = '\0';
		bp = buf;
329
		bufavail = sizeof(buf) - 1;
330 331 332 333 334 335 336 337 338 339

		if (iconv(cd, &ip, &input_len, &bp, &bufavail) == (size_t)-1 &&
		    errno != E2BIG) {
			*bp++ = '?';
			ip++;
			input_len--;
			bufavail--;
		}
		*bp = '\0';

340
		count = sizeof(buf) - bufavail - 1;
341 342 343 344 345 346 347 348 349 350 351

		output_size += count;
		op = output = xrealloc(output, output_size, sizeof(char));
		op += out_pos;
		memcpy(op, buf, count);
		out_pos += count;
		op += count;
		*op = '\0';
	}

	if (iconv_close(cd) == -1) {
moritz's avatar
moritz committed
352
		printf("%s: iconv_close(): %s\n", __progname, strerror(errno));
353
		xfree(output);
354
		xfree(tocode);
355
		return (xstrdup(in_str));
356 357
	}

358
	xfree(tocode);
359
	return (output);
360
#else
361 362 363 364
	(void)from;
	(void)to;
	(void)mode;

365 366 367
	if (NULL == in_str)
		return (xstrdup(""));

368
	return (xstrdup(in_str));
moritz's avatar
moritz committed
369
#endif /* HAVE_ICONV */
370
}
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

int
ez_gettimeofday(void *tp_arg)
{
	struct timeval	*tp = (struct timeval *)tp_arg;
	int		 ret = -1;

#ifdef HAVE_GETTIMEOFDAY
	ret = gettimeofday(tp, NULL);
#else /* HAVE_GETTIMEOFDAY */
# ifdef WIN32
	/*
	 * Idea for this way of implementing gettimeofday()-like functionality
	 * on Windows taken from cURL, (C) 1998 - 2007 Daniel Steinberg, et al.
	 * http://curl.haxx.se/docs/copyright.html
	 */
	SYSTEMTIME	 st;
	struct tm	 tm;

	GetLocalTime(&st);
	tm.tm_sec = st.wSecond;
	tm.tm_min = st.wMinute;
	tm.tm_hour = st.wHour;
	tm.tm_mday = st.wDay;
	tm.tm_mon = st.wMonth - 1;
	tm.tm_year = st.wYear - 1900;
	tm.tm_isdst = -1;
	tp->tv_sec = (long)mktime(&tm);
	tp->tv_usec = st.wMilliseconds * 1000;
	ret = 0;
# else /* WIN32 */
	/* Fallback to time(): */
	tp->tv_sec = (long)time(NULL);
	tp->tv_usec = 0;
	ret = 0;
# endif /* WIN32 */
#endif /* HAVE_GETTIMEOFDAY */

	return (ret);
}