Android系统中U盘卷名识别异常

Android系统中U盘卷名识别异常

类别

问题缺陷

索引类别

U盘卷名乱码 无法正确识别中文

问题描述

U盘在windows下或者在Linux环境下,将对应的U盘卷名修改为名字或者命名有多国语言的时候,通常在Android系统下会不能正常识别到正确的卷名。那么下面修改就是可以正确的支持:

代码关联

对应代码如下:(android/external/e2fsprogs/misc/blkid.c )

diff --git a/misc/blkid.c b/misc/blkid.c
index 5aa949e..0dc3512 100644
--- a/misc/blkid.c
+++ b/misc/blkid.c
@@ -55,11 +55,15 @@ int code_convert(char *from_charset, char *to_charset, char *inbuf, size_t inlen
     char **pout = &outbuf;
 
     cd = iconv_open(to_charset, from_charset);
-    if (cd == 0)
+    if (cd == 0) {
+	iconv_close(cd);
         return -1;
+    }
     memset(outbuf, 0, outlen);
-    if (iconv(cd, pin, &inlen, pout, &outlen) == (size_t)(-1))
+    if (iconv(cd, pin, &inlen, pout, &outlen) == (size_t)(-1)) {
+	iconv_close(cd);
         return -2;
+    }
     iconv_close(cd);
     return 1;
 }
@@ -88,10 +92,11 @@ void print_hex(char *s)
 
 int ansi_to_utf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
     ALOGD("ansi_to_utf8...");
+    int count = 0;
     char szSysLocale[PROP_VALUE_MAX] = { 0 };
     char szFromLang[50]="GB2312";
-     property_get("persist.sys.locale", szSysLocale, "");
+	  //默认设置地区为中国
+     property_get("persist.sys.locale", szSysLocale, "zh-CN");

    //Preferred to use locale transcode
     /*print_hex(inbuf); for debug only, don't unmark it at release */
     if(strcmp(szSysLocale, "zh-CN")==0) // china
          strcpy(szFromLang, "GB2312"); 
@@ -103,8 +108,31 @@ int ansi_to_utf8(char *inbuf, size_t inlen, char *outbuf, size_t outlen) {
          strcpy(szFromLang, "KOREAN");
     else strcpy(szFromLang, "US");
 
-    ALOGD("Get locale: %s, convert to: %s", szSysLocale, szFromLang);
-    int iRet =  code_convert(szFromLang, "utf-8", inbuf, inlen, outbuf, outlen);
+    //添加loop的原因是:是因为对应的如果只会判断一次用什么编码来解析文字,如果是不同国家的文字那么就要将对应的编码修改
+    //对应的不同国家的编码不同,输出的值(outbuf)也不同,如果为空可进行下一个编码方式进行尝试解码
+loop:
+    iRet =  code_convert(szFromLang, "utf-8", inbuf, inlen, outbuf, outlen);
+    ALOGD("Get locale: %s, convert to: %s   ,%d", szSysLocale, szFromLang,iRet);
+    if (strlen(outbuf) == 0 && count <= 4) {
+        //second-choice  transcode
+        switch (count) {
+            case 0:
+                strcpy(szFromLang, "GB2312");break;
+            case 1:
+                strcpy(szFromLang, "BIG5-HKSCS:2004");break;
+            case 2:
+                strcpy(szFromLang, "SHIFT_JIS");break;
+            case 3:
+                strcpy(szFromLang, "KOREAN");break;
+            case 4:
+                strcpy(szFromLang, "US");break;
+            default :
+                break;
+        }
+        count++;
+        goto loop;
+    } else {
+        ALOGD(" There are still loopholes in (utf-8) transcoding ");
+    }
+
     if(iRet!=1)
         strcpy(outbuf, inbuf);
         return iRet;
@@ -145,7 +173,7 @@ static void safe_print(const char *cp, int len, int iPartitionType, int iConv)
 {
     char out[200] = "";
     int iReturnCode = 0;
-
+    //RT uniform transcoding utf-8
     if(iConv){
         switch(iPartitionType)
         {
@@ -157,7 +185,9 @@ static void safe_print(const char *cp, int len, int iPartitionType, int iConv)
             iReturnCode = ansi_to_utf8((char*)cp, strlen(cp), out, 200); break;
         }
         ALOGD("safe_print - iconv ReturnCode=%d", iReturnCode);
-        fputs(out, stdout);
+	if (fputs(out, stdout) == EOF) {
+		ALOGD("write error");
+	}
     }
     else{
         unsigned char ch;

改进建议

如果编码方式过多的情况下,相应的解码可能会有错误的解码方式,最好是能做到一种全球统一的编码方式,这样就不存在解码为乱码的情况了。

工作记录…

评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值