Chinese En/Decode for filesystem

本文介绍了解决路由器USB存储服务中遇到的中文路径乱码问题的方法。首先通过使用UTF-8编码挂载USB磁盘,并确保网页文件正确解析UTF-8编码。其次,详细介绍了FTP服务器如何处理来自客户端的中文编码请求及响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.        En/Decode for Chinese path

For theusb storage service on router UI, there may be Chinese path when access somefolder on usb disk, it will show messy code if there is no related deal work.

First,the usb disk should be mounted with utf-8 encode in file dal_usb.c, as below,

else if ((strstr(fs_type, "fat32")!= NULL) || (strstr(fs_type, "FAT32") != NULL) || (strstr(fs_type,"vfat") != NULL))

    {

       sprintf(fs_type, "FAT32|");

       sprintf(cmd, "mount -t vfat -o iocharset=utf8 %s %s>/dev/null", partition, mountpoint);

       rut_doSystemAction("mount", cmd);

    }

……

Then, therelated page html file will receive the utf-8 encoded data, and should resolveit with utf-8, so there should set charset as utf-8 on page head, as below,

<head>

 <meta http-equiv="Content-Type"content="text/html; charset=UTF-8">

 <link rel="stylesheet"type="text/css" media="screen"href="/corner.css"> 

<scriptlanguage="JavaScript"src="/routine_data.js"></script> 

<scriptlanguage="JavaScript" type="text/javascript"src="/msg.js"></script>

……

Also,when user submit page data to router, page html file should encode the data as utf-8 by encodeURI(), and send it out, as below,

……

else

{

 var tempPath =encodeURI(document.tF0.curPath.value).replace("'", +"%27");

 loc += '&path=' + tempPath;

 }

……

2.        En/Decode for ftp Chinese folderand file

2.1 Transfer to local encode forftp server from ftp client encode

The usb diskfor ftp server usually use UTF-8 encode, while ftp client may use some otherencode such as gbk related(Chinese encode) to access server, then ftp server willreceived data encoded by Chinese encode, ftp server must transfer data receivedfrom client to utf-8 from Chinese encode so as to work successfully.

There need the transferfunction to transfer to local encode(UTF-8) for ftp server from ftp clientencode(GBK). It is implemented in majorly file bftpd\mod_codeconv.cand include files bftpd\include\*.h.

The major transfer functionis as below, it firstly transfers the gbk related encode to wchar by function gb18030_mbtowc(), thentransfers wchar to encode uft-8 by function utf8_wctomb().

char* EnCodeString(char* inString, intcharset)

{

       ……

       for(iLoopA=0,iLoopB=0;iLoopA<iLenA;iLoopB++)

       {

              ucs4Out= 0;

              switch(charset)

              {

              case1:    

#ifdef GB2312      

                     iRet= gb18030_mbtowc(0,&ucs4Out,(unsigned char*)inString+iLoopA,2);

#endif

                     break;

                  ……

              }

              ……

       }

       ……

       for(iLoopA=0,iLoopB=0;iLoopA<iLenA;iLoopA++)

       {

              iRet= utf8_wctomb(0,(unsigned char*)outStringB+iLoopB,outStringA[iLoopA],iLenA*3-iLoopB);

              ……

       }

       free(outStringA);

       returnoutStringB;

}

 

char* remote2local(char* remote, int charset)

{

       char*out_ptr;

       out_ptr= EnCodeString(remote,charset);

 

       returnout_ptr;

}

While, theftp command functions in file commands.c will call the transfer function remote2local() to deal with therequest(include gbk encode data) of ftp client. Such as below,

void command_cwd(char *dir)

{

   char *p;

    intret = 0;

 

    p =remote2local(dir, ftp_characterset);

    if(p)

       ret = bftpd_cwd_chdir(p);

   else

       ret = bftpd_cwd_chdir(dir);

    if(ret)

    {

              bftpd_log("Error:'%s' while changing directory to '%s'.\n",

                              strerror(errno), dir);

              control_printf(SL_FAILURE,"451 Error: %s.", strerror(errno));

       }else {

              bftpd_log("Changeddirectory to '%s'.\n", dir);

              control_printf(SL_SUCCESS,"250 OK");

       }

    if(p)

       free(p);

}

2.2 Transfer to client encode fromlocal encode

As theresponse to ftp client, ftp server also must firstly transfer utf-8data(encoded by itself) to the right encode(ftp client encode), and then sendthe transferred data to ftp client.

The major transferfunction is as below, it firstly transfers the uft-8 encoded data to wchar byfunction utf8_mbtowc(), thentransfers wchar to gbk encode by function gb18030_wctomb().

char* DeCodeString(char* inString,intcharset)

{

       ……

       for(iLoopA=0,iLoopB=0;iLoopA<iLenA;iLoopB++)

       {

              ucs4Out= 0;

              iRet= utf8_mbtowc(0,&ucs4Out,(unsigned char*)inString+iLoopA,4);

              ……

       }

       ……

       for(iLoopA=0,iLoopB=0;iLoopA<iLenA;iLoopA++)

       {

              switch(charset)

              {

              case1:    

#ifdef GB2312      

                     iRet= gb18030_wctomb(0,(unsignedchar*)outStringB+iLoopB,outStringA[iLoopA],iLenA*3-iLoopB);

#endif

                     break;

              ……

              }           

              ……

       }

       free(outStringA);

       returnoutStringB;

}

 

char* local2remote(char* local,int charset)

{

       char*out_ptr;

       out_ptr= DeCodeString(local,charset);

       returnout_ptr;

}

While, as thereponse function to ftp client in file commands.c, it will call the transfer function local2remote()to deal with local data(utf-8 encode) to ftp client encode and send it out.Such as below,

void dirlist_one_file(char *name, FILE*client, char verbose)

{

    ……

    if(verbose)

       bftpd_stat(name, client);

   else

    {

       p = local2remote(filename_index, ftp_characterset);

       if (p)

       {

           fprintf(client, "%s\r\n", p);

           free(p);

       }

       else

           fprintf(client, "%s\r\n", filename_index);

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值