Line 0
Link Here
|
|
|
1 |
--- src/scan-mp3.c.orig 2007-09-25 16:55:23.000000000 +0900 |
2 |
+++ src/scan-mp3.c 2011-09-05 13:52:16.000000000 +0900 |
3 |
@@ -18,6 +18,15 @@ |
4 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
5 |
*/ |
6 |
|
7 |
+#define USE_CP932 |
8 |
+ |
9 |
+#ifdef USE_CP932 |
10 |
+ #define UNKNOWN_STR "UNKNOWN" |
11 |
+ #ifndef FILESYSTEM_CES |
12 |
+ #define FILESYSTEM_CES "CP932" |
13 |
+ #endif |
14 |
+#endif |
15 |
+ |
16 |
#ifdef HAVE_CONFIG_H |
17 |
# include "config.h" |
18 |
#endif |
19 |
@@ -34,6 +43,10 @@ |
20 |
#include <string.h> |
21 |
#include <time.h> |
22 |
|
23 |
+#ifdef USE_CP932 |
24 |
+ #include <iconv.h> |
25 |
+#endif |
26 |
+ |
27 |
#include "daapd.h" |
28 |
#include "conf.h" |
29 |
#include "err.h" |
30 |
@@ -289,6 +302,96 @@ |
31 |
return 1; |
32 |
} |
33 |
|
34 |
+#ifdef USE_CP932 |
35 |
+#define MAX_ICONV_BUF 1024 |
36 |
+ |
37 |
+typedef enum { |
38 |
+ ICONV_OK, |
39 |
+ ICONV_TRYNEXT, |
40 |
+ ICONV_FATAL |
41 |
+} iconv_result; |
42 |
+ |
43 |
+static iconv_result do_convert(const char* to_ces, const char* from_ces, |
44 |
+ char *inbuf, size_t inbytesleft, |
45 |
+ char *outbuf_orig, size_t outbytesleft_orig) { |
46 |
+ size_t rc; |
47 |
+ iconv_result ret = ICONV_OK; |
48 |
+ |
49 |
+ size_t outbytesleft = outbytesleft_orig - 1; |
50 |
+ char* outbuf = outbuf_orig; |
51 |
+ |
52 |
+ iconv_t cd = iconv_open(to_ces, from_ces); |
53 |
+ if (cd == (iconv_t)-1) { |
54 |
+ return ICONV_FATAL; |
55 |
+ } |
56 |
+ rc = iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft); |
57 |
+ if (rc == (size_t)-1) { |
58 |
+ if (errno == E2BIG) { |
59 |
+ ret = ICONV_FATAL; |
60 |
+ } else { |
61 |
+ ret = ICONV_TRYNEXT; |
62 |
+ memset(outbuf_orig, '\0', outbytesleft_orig); |
63 |
+ } |
64 |
+ } |
65 |
+ iconv_close(cd); |
66 |
+ |
67 |
+ return ret; |
68 |
+} |
69 |
+ |
70 |
+static unsigned char* get_utf8_text(const id3_ucs4_t* native_text) { |
71 |
+ unsigned char* utf8_text = NULL; |
72 |
+ char * in, * in8, * iconv_buf; |
73 |
+ iconv_result rc; |
74 |
+ |
75 |
+ in = (char*)id3_ucs4_latin1duplicate(native_text); |
76 |
+ if (!in) goto out; |
77 |
+ |
78 |
+ in8 = (char*)id3_ucs4_utf8duplicate(native_text); |
79 |
+ if (!in8) { |
80 |
+ free(in); |
81 |
+ goto out; |
82 |
+ } |
83 |
+ |
84 |
+ iconv_buf = (char*)calloc(MAX_ICONV_BUF, sizeof(char)); |
85 |
+ if (!iconv_buf) { |
86 |
+ free(in); free(in8); |
87 |
+ goto out; |
88 |
+ } |
89 |
+ |
90 |
+ /* (1) try utf8 -> cp932 */ |
91 |
+ rc = do_convert("CP932", "UTF-8", in8, strlen(in8), iconv_buf, MAX_ICONV_BUF); |
92 |
+ if (rc == ICONV_OK) { |
93 |
+ utf8_text = (unsigned char*)in8; |
94 |
+ free(iconv_buf); |
95 |
+ } else if (rc == ICONV_TRYNEXT) { |
96 |
+ /* (2) try cp932 -> utf8 */ |
97 |
+ rc = do_convert("UTF-8", "CP932", in, strlen(in), iconv_buf, MAX_ICONV_BUF); |
98 |
+ if (rc == ICONV_OK) { |
99 |
+ utf8_text = (unsigned char*)iconv_buf; |
100 |
+ } else if (rc == ICONV_TRYNEXT) { |
101 |
+ /* (3) try euc-jp -> utf8 */ |
102 |
+ rc = do_convert("UTF-8", "EUC-JP", in, strlen(in), iconv_buf, MAX_ICONV_BUF); |
103 |
+ if (rc == ICONV_OK) { |
104 |
+ utf8_text = (unsigned char*)iconv_buf; |
105 |
+ } else if (rc == ICONV_TRYNEXT) { |
106 |
+ /* utf-8 including non-japanese char? fallback. */ |
107 |
+ utf8_text = (unsigned char*)id3_ucs4_utf8duplicate(native_text); |
108 |
+ free(iconv_buf); |
109 |
+ } |
110 |
+ } |
111 |
+ free(in8); |
112 |
+ } |
113 |
+ free(in); |
114 |
+ |
115 |
+ out: |
116 |
+ if (!utf8_text) { |
117 |
+ utf8_text = (unsigned char*)strdup(UNKNOWN_STR); |
118 |
+ } |
119 |
+ |
120 |
+ return utf8_text; |
121 |
+} |
122 |
+#endif |
123 |
+ |
124 |
int scan_mp3_get_mp3tags(char *file, MP3FILE *pmp3) { |
125 |
struct id3_file *pid3file; |
126 |
struct id3_tag *pid3tag; |
127 |
@@ -352,8 +455,11 @@ |
128 |
if(native_text) { |
129 |
have_utf8=1; |
130 |
|
131 |
- |
132 |
+#ifdef USE_CP932 |
133 |
+ utf8_text = (char *)get_utf8_text(native_text); |
134 |
+#else |
135 |
utf8_text = (char*)id3_ucs4_utf8duplicate(native_text); |
136 |
+#endif |
137 |
if(utf8_text) |
138 |
mem_register(utf8_text,0); |
139 |
|