diff --git a/configure.in b/configure.in index 62a72e5c..e1dc7f3d 100644 --- a/configure.in +++ b/configure.in @@ -37,14 +37,6 @@ fi AC_DEFINE_UNQUOTED(CONFFILE,"${CONFFILE}",Where the config file is) use_upnp=false; -use_iconv=true - -AC_ARG_ENABLE(iconv, AC_HELP_STRING([--enable-iconv], [Enable iconv conversion]), - [ case "${enableval}" in - yes) use_iconv=true;; - no) use_iconv=false;; - *) AC_MSG_ERROR(bad value ${enableval} for --enable-iconv);; - esac ]) AC_ARG_ENABLE(flac, AC_HELP_STRING([--enable-flac], [Enable FLAC support]), use_flac=true; @@ -64,10 +56,6 @@ AM_CONDITIONAL(COND_UPNP,test x$use_upnp = xtrue) AC_CHECK_FUNCS(strcasestr strsep) dnl Checks for libraries. -if test x$use_iconv = xtrue; then - AM_ICONV -fi - PKG_CHECK_MODULES(AVAHI, [ avahi-client >= 0.6.24 ]) PKG_CHECK_MODULES(SQLITE3, [ sqlite3 ], AC_DEFINE(HAVE_SQLITE3, 1, [define if sqlite3 is available])) diff --git a/src/Makefile.am b/src/Makefile.am index b8931918..f8df5f92 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,7 +17,7 @@ endif mt_daapd_CPPFLAGS = @AVAHI_CFLAGS@ @SQLITE3_CFLAGS@ @FFMPEG_CFLAGS@ @TAGLIB_CFLAGS@ -mt_daapd_LDADD = @AVAHI_LIBS@ @SQLITE3_LIBS@ @FFMPEG_LIBS@ @FLAC_LIBS@ @TAGLIB_LIBS@ @LIBICONV@ @LIBDL@ +mt_daapd_LDADD = @AVAHI_LIBS@ @SQLITE3_LIBS@ @FFMPEG_LIBS@ @FLAC_LIBS@ @TAGLIB_LIBS@ @LIBDL@ mt_daapd_SOURCES = main.c daapd.h rend.h webserver.c \ webserver.h configfile.c configfile.h err.c err.h \ mp3-scanner.h mp3-scanner.c \ diff --git a/src/util.c b/src/util.c index f267c2fc..0df35bb4 100644 --- a/src/util.c +++ b/src/util.c @@ -19,10 +19,6 @@ #include #include -#ifdef HAVE_ICONV -# include -#endif - #include "daapd.h" #include "err.h" #include "util.h" @@ -68,389 +64,6 @@ int util_must_exit(void) { return config.stop; } -#ifdef HAVE_ICONV -int util_utf8toutf16(unsigned char *utf16, int dlen, unsigned char *utf8, int slen) { - int result; - DPRINTF(E_DBG,L_MISC,"Converting %s to utf-16le (slen=%d, dlen=%d)\n",utf8,slen,dlen); - - result=util_xtoy(utf16, dlen, utf8, slen, "UTF-8","UTF-16LE"); - DPRINTF(E_DBG,L_MISC,"Result: %d\n",result); -// _util_hexdump(utf16,32); - return result; -} - -int util_utf16toutf8(unsigned char *utf8, int dlen, unsigned char *utf16, int slen) { - int result; - - DPRINTF(E_DBG,L_MISC,"Converting *something* to utf-8 (slen=%d, dlen=%d)\n",slen,dlen); -// _util_hexdump(utf16,32); - result = util_xtoy(utf8, dlen, utf16, slen, "UTF-16LE","UTF-8"); - - DPRINTF(E_DBG,L_MISC,"Converted to %s\n",utf8); - - return result; -} - -int util_utf16_byte_len(unsigned char *utf16) { - unsigned char *src = utf16; - int len = 0; - - while(1) { - if((src[0] == 0) && (src[1]==0)) - return len; - len += 2; - src += 2; - } - return len; /* ?? */ -} - - -unsigned char *util_utf8toutf16_alloc(unsigned char *utf8) { - unsigned char *utf16; - - utf16 = calloc(1,strlen((char*)utf8) * 4 + 1); - if(util_xtoy(utf16,strlen((char*)utf8) * 4 + 1, utf8, strlen((char*)utf8),"UTF-8","UTF-16LE")) { - return utf16; - } - - free(utf16); - return NULL; -} - -unsigned char *util_utf16toutf8_alloc(unsigned char *utf16, int slen) { - unsigned char *utf8; - - utf8=calloc(1, slen * 2 + 1); - if(util_xtoy(utf8,slen * 2 + 1,utf16,slen,"UTF-16LE","UTF-8")) { - return utf8; - } - - free(utf8); - return NULL; -} - -unsigned char *util_xtoutf8_alloc(unsigned char *x, int slen, char *from) { - unsigned char *utf8; - - utf8 = calloc(1, slen * 4 + 1); - if(util_xtoy(utf8,slen * 4 + 1, x, slen, from, "UTF-8")) { - return utf8; - } - free(utf8); - return NULL; -} - -int util_xtoy(unsigned char *dbuffer, int dlen, unsigned char *sbuffer, int slen, char *from, char *to) { - iconv_t iv; - size_t csize; - - /* type punning warnings */ - size_t st_dlen = (size_t)dlen; - size_t st_slen = (size_t)slen; - char *st_dbuffer = (char*)dbuffer; - ICONV_CONST char *st_sbuffer = (char*)sbuffer; - - memset(dbuffer,0,dlen); - - iv=iconv_open(to,from); - if(iv == (iconv_t)-1) { - DPRINTF(E_LOG,L_MISC,"iconv error: iconv_open failed with %d\n",errno); - } - - csize = iconv(iv,&st_sbuffer,&st_slen, - &st_dbuffer,&st_dlen); - if(csize == (size_t)-1) { - switch(errno) { - case EILSEQ: - DPRINTF(E_LOG,L_MISC,"iconv error: Invalid multibyte sequence\n"); - break; - case EINVAL: - DPRINTF(E_LOG,L_MISC,"iconv error: Incomplete multibyte sequence\n"); - break; - case E2BIG: - DPRINTF(E_LOG,L_MISC,"iconv error: Insufficient buffer size\n"); - break; - default: - DPRINTF(E_LOG,L_MISC,"iconv error: unknown (%d)\n",errno); - break; - } - } - iconv_close(iv); - - return (csize != (size_t)-1); -} - -#else - -/* homerolled conversions */ -int util_utf16_byte_len(unsigned char *utf16) { - unsigned char *src = utf16; - int len = 0; - - while(1) { - if((src[0] == 0) && (src[1]==0)) - return len; - len += 2; - src += 2; - } - return len; /* ?? */ -} - -/** - * calculate how long a utf16le string will be once converted - */ -int util_utf16toutf8_len(unsigned char *utf16, int len) { - unsigned char *src = utf16; - int out_len = 0; - uint32_t temp_dword; - - while(src+2 <= utf16 + len) { - temp_dword = src[1] << 8 | src[0]; - - if((temp_dword & 0xFC00) == 0xD800) { - src += 2; - if(src + 2 <= utf16 + len) { - out_len += 4; - } else { - return -1; - } - } else { - if(temp_dword <= 0x7F) - out_len += 1; - else if(temp_dword <= 0x7FF) - out_len += 2; - else if(temp_dword <= 0xFFFF) - out_len += 3; - } - - src += 2; - } - return out_len; -} - -/** - * convert utf16 string to utf8. This is a bit naive, but... - * Since utf-8 can't expand past 4 bytes per code point, and - * we're converting utf-16, we can't be more than 2n+1 bytes, so - * we'll just allocate that much. - * - * Probably it could be more efficiently calculated, but this will - * always work. Besides, these are small strings, and will be freed - * after the db insert. - * - * We assume this is utf-16LE, as it comes from windows - * - * @param utf16 utf-16 to convert - * @param len length of utf-16 string - */ - -int util_utf16toutf8(unsigned char *utf8, int dlen, unsigned char *utf16, int len) { - unsigned char *src=utf16; - unsigned char *dst; - unsigned int w1, w2; - int bytes; - int new_len; - - if(!len) - return FALSE; - - new_len = util_utf16toutf8_len(utf16,len); - if((new_len == -1) || (dlen <= new_len)) { - DPRINTF(E_LOG,L_MISC,"Cannot convert %s to utf8; E2BIG (%d vs %d)\n",utf8,new_len,dlen); - return FALSE; - } - - dst=utf8; - while((src+2) <= utf16+len) { - w1=src[1] << 8 | src[0]; - src += 2; - if((w1 & 0xFC00) == 0xD800) { // could be surrogate pair - if(src+2 > utf16+len) { - DPRINTF(E_INF,L_SCAN,"Invalid utf-16 in file\n"); - return FALSE; - } - w2 = src[3] << 8 | src[2]; - if((w2 & 0xFC00) != 0xDC00) { - DPRINTF(E_INF,L_SCAN,"Invalid utf-16 in file\n"); - return FALSE; - } - - // get bottom 10 of each - w1 = w1 & 0x03FF; - w1 = w1 << 10; - w1 = w1 | (w2 & 0x03FF); - - // add back the 0x10000 - w1 += 0x10000; - } - - // now encode the original code point in utf-8 - if (w1 < 0x80) { - *dst++ = w1; - bytes=0; - } else if (w1 < 0x800) { - *dst++ = 0xC0 | (w1 >> 6); - bytes=1; - } else if (w1 < 0x10000) { - *dst++ = 0xE0 | (w1 >> 12); - bytes=2; - } else { - *dst++ = 0xF0 | (w1 >> 18); - bytes=3; - } - - while(bytes) { - *dst++ = 0x80 | ((w1 >> (6*(bytes-1))) & 0x3f); - bytes--; - } - } - - *dst = '\x0'; - - return new_len; -} - -/** - * calculate how long a utf8 string will be once converted - */ -int util_utf8toutf16_len(unsigned char *utf8) { - int len,out_len,trailing_bytes; - unsigned char *src = utf8; - - len=(int)strlen((char *)utf8); - out_len = 0; - - while(src < utf8 + len) { - trailing_bytes = 0; - if((*src & 0xE0) == 0xC0) trailing_bytes = 1; - else if((*src & 0xF0) == 0xE0) trailing_bytes = 2; - else if((*src & 0xF8) == 0xF0) trailing_bytes = 3; - - if(src + trailing_bytes > utf8 + len) - return -1; - - out_len += 2; - if(trailing_bytes == 3) /* surrogate pair */ - out_len += 2; - - src += (1 + trailing_bytes); - } - - out_len += 1; - return out_len; -} - -unsigned char *util_utf8toutf16_alloc(unsigned char *utf8) { - unsigned char *out; - int new_len; - - new_len = util_utf8toutf16_len(utf8); - if(new_len == -1) - return NULL; - - out = calloc(1,new_len + 2); - if(!util_utf8toutf16(out,new_len + 2,utf8,(int)strlen((char*)utf8))) { - free(out); - return NULL; - } - - return out; -} - -unsigned char *util_utf16touft8_alloc(unsigned char *utf16, int len) { - unsigned char *out; - int new_len; - - new_len = util_utf16toutf8_len(utf16,len); - if(new_len == -1) - return NULL; - - out = calloc(1,new_len + 1); - if(!util_utf16toutf8(out,new_len + 1,utf16,len)) { - free(out); - return NULL; - } - return out; -} - -int util_utf8toutf16(unsigned char *utf16, int dlen, unsigned char *utf8, int len) { - unsigned char *src=utf8; - unsigned char *dst; - int new_len; - int trailing_bytes; - uint32_t utf32; - uint16_t temp_word; - - len=(int)strlen((char*)utf8); /* ignore passed length, might be wrong! */ - if(!len) - return FALSE; - - new_len = util_utf8toutf16_len(utf8); - if((new_len == -1) || (dlen <= (new_len+1))) { - DPRINTF(E_LOG,L_MISC,"Cannot convert %s to utf16; E2BIG (%d vs %d)\n",utf8,new_len,dlen); - return FALSE; - } - - dst=utf16; - - while(src < utf8 + len) { - utf32=0; - trailing_bytes=0; - - if((*src & 0xE0) == 0xC0) trailing_bytes = 1; - else if((*src & 0xF0) == 0xE0) trailing_bytes = 2; - else if((*src & 0xF8) == 0xF0) trailing_bytes = 3; - - if(src + trailing_bytes > utf8 + len) { - DPRINTF(E_LOG,L_SCAN,"Invalid UTF8 string\n"); - return FALSE; - } - - switch(trailing_bytes) { - case 0: - utf32 = *src; - break; - case 1: - utf32 = ((src[0] & 0x1F) << 6) | - (src[1] & 0x3F); - break; - case 2: - utf32 = ((src[0] & 0x0F) << 12) | - ((src[1] & 0x3F) << 6) | - ((src[2] & 0x3F)); - break; - case 3: - utf32 = ((src[0] & 0x07) << 18) | - ((src[1] & 0x3F) << 12) | - ((src[2] & 0x3F) << 6) | - ((src[3] & 0x3F)); - break; - } - - if(utf32 <= 0xFFFF) { - /* we are encoding LE style... */ - *dst++ = utf32 & 0xFF; - *dst++ = (utf32 & 0xFF00) >> 8; - } else { - /* Encode with surrogates */ - temp_word = 0xD800 | ((utf32 & 0x0FFC00) >> 10); - *dst++ = temp_word & 0xFF; - *dst++ = (temp_word & 0xFF00) >> 8; - temp_word = 0xDC00 | (utf32 & 0x3FF); - *dst++ = temp_word & 0xFF; - *dst++ = (temp_word & 0xFF00) >> 8; - } - - src += (trailing_bytes + 1); - } - - *dst++ = '\x0'; - *dst = '\x0'; - return new_len; -} - -#endif - void util_hexdump(unsigned char *block, int len) { char charmap[256]; int index; diff --git a/src/util.h b/src/util.h index 75ded560..c85e80b0 100644 --- a/src/util.h +++ b/src/util.h @@ -40,14 +40,6 @@ extern int util_must_exit(void); int util_split(char *s, char *delimiters, char ***argvp); void util_dispose_split(char **argv); -extern unsigned char *util_utf8toutf16_alloc(unsigned char *utf8); -extern unsigned char *util_utf16touft8_alloc(unsigned char *utf16, int slen); -extern int util_utf8toutf16(unsigned char *utf16, int dlen, unsigned char *utf8, int slen); -extern int util_utf16toutf8(unsigned char *utf8, int dlen, unsigned char *utf16, int slen); -extern int util_xtoy(unsigned char *dbuffer, int dlen, unsigned char *sbuffer, int slen, char *from, char *to); -extern unsigned char *util_xtoutf8_alloc(unsigned char *x,int slen,char *from); -extern int util_utf16_byte_len(unsigned char *utf16); - extern void util_hexdump(unsigned char *block, int len); extern char *util_vasprintf(char *fmt, va_list ap); extern char *util_asprintf(char *fmt, ...);