diff -uNr mt-daapd-0.2.1.1.orig/src/mp3-scanner.c mt-daapd-0.2.1.1_/src/mp3-scanner.c --- mt-daapd-0.2.1.1.orig/src/mp3-scanner.c 2005-02-06 06:27:19.000000000 +0900 +++ mt-daapd-0.2.1.1_/src/mp3-scanner.c 2005-06-20 03:14:22.000000000 +0900 @@ -26,6 +26,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define USE_CP932 + #ifdef HAVE_CONFIG_H # include "config.h" #endif @@ -45,6 +47,8 @@ #include #include /* why here? For osx 10.2, of course! */ +#include + #include "daapd.h" #include "db-memory.h" #include "err.h" @@ -739,6 +743,64 @@ return 0; } +#ifdef USE_CP932 +static size_t do_convert(const char* to_ces, const char* from_ces, + char *inbuf, size_t inbytesleft, + char *outbuf, size_t outbytesleft) { + iconv_t cd = iconv_open(to_ces, from_ces); + size_t ret = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); + iconv_close(cd); + return ret; +} + +#define TAG_STR_SIZE 1024 +#define UNKNOWN_STR "UNKNOWN" + +static unsigned char* get_utf8_text(const id3_ucs4_t* native_text) { + unsigned char* utf8_text = NULL; + + char* const in_ptr = (char*)id3_ucs4_latin1duplicate(native_text); + char* const in8_ptr = (char*)id3_ucs4_utf8duplicate(native_text); + char converted_text[TAG_STR_SIZE] = {0}; + + if (in_ptr && in8_ptr) { + + /* (1) try utf8 -> cp932 */ + size_t rc = do_convert("CP932", "UTF-8", in8_ptr, strlen(in8_ptr), converted_text, TAG_STR_SIZE - 1); + if(rc == (size_t)-1) { + if (errno != E2BIG) { + /* (2) try cp932 -> utf8 */ + memset(converted_text, '\0', TAG_STR_SIZE); + rc = do_convert("UTF-8", "CP932", in_ptr, strlen(in_ptr), converted_text, TAG_STR_SIZE - 1); + if(rc == (size_t)-1) { + if (errno != E2BIG) { + /* utf-8 including non-japanese char? fallback. */ + utf8_text=(char*)id3_ucs4_utf8duplicate(native_text); + } + } else { + /* valid cp932: in_ptr */ + utf8_text = (unsigned char*)calloc(strlen(converted_text) + 1, sizeof(unsigned char)); + if(utf8_text) { + memcpy(utf8_text, converted_text, strlen(converted_text)); + } + } + } + free(in8_ptr); + } else { + /* valid utf8: in8_ptr */ + utf8_text = in8_ptr; + } + free(in_ptr); + + } + + if(!utf8_text) { + utf8_text = strdup(UNKNOWN_STR); + } + + return utf8_text; +} +#endif int scan_get_mp3tags(char *file, MP3FILE *pmp3) { struct id3_file *pid3file; @@ -796,8 +858,12 @@ if(native_text) { have_utf8=1; +#ifdef USE_CP932 + utf8_text = get_utf8_text(native_text); +#else utf8_text=id3_ucs4_utf8duplicate(native_text); - MEMNOTIFY(utf8_text); +#endif + MEMNOTIFY(utf8_text); if(!strcmp(pid3frame->id,"TIT2")) { /* Title */ used=1; @@ -1762,8 +1828,20 @@ song->data_kind=0; } +#ifdef USE_CP932 + if(!song->title) { + char converted_text[TAG_STR_SIZE] = {0}; + size_t rc = do_convert("UTF-8", "CP932", song->fname, strlen(song->fname), converted_text, TAG_STR_SIZE - 1); + if(rc == (size_t)-1) { + song->title = strdup(UNKNOWN_STR); + } else { + song->title = strdup(converted_text); + } + } +#else if(!song->title) song->title = strdup(song->fname); +#endif /* Ogg used to be set as an item_kind of 4. Dunno why */ song->item_kind = 2;