cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: using CURLOPT_CAINFO with unicode path

From: Tom Bishop, Wenlin Institute <tangmu_at_wenlin.com>
Date: Wed, 14 Mar 2012 14:52:51 -0700

On Mar 14, 2012, at 2:33 PM, JonathonS wrote:

> Hi,
>
> Is there a recommended approach for passing in unicode paths for
> CURLOPT_CAINFO? I've read through the documentation and I can't find
> anything on how the paths should be encoded.
>
> After digging through the source code, it looks like the path
> parameter eventually gets passed into Openssl's x509_load_crl_file.
> This function then passes it into Openssl's BIO_read_filename which
> then uses fopen to open the file. I am currently developing on
> Windows and so fopen does not "understand" unicode.
>
> Are there any workarounds that people have done to get this to work?

If you can modify the code that calls fopen(), you could replace fopen() with something like this, assuming of course that the path is UTF-8:

FILE *fopen_win32(char *fname, char *mode)
{
        // First try fopen, assuming nothing about character encodings.
        FILE *file = fopen(fname, mode);
        if (file == NULL) {
                /* fopen failed. Assume the filename is UTF-8, convert to UTF-16, and try _wfopen.
                 These max sizes are somewhat arbitrary; could use dynamic allocation instead,
                 and call MultiByteToWideChar(..., 0) to get size needed. */
                #define MAX_WIN32_FNAME 2048
                #define MAX_FOPEN_MODE 100
                TCHAR fnameW[MAX_WIN32_FNAME], modeW[MAX_FOPEN_MODE];
                if (MultiByteToWideChar(CP_UTF8, 0, fname, strlen(fname),
                                        fnameW, MAX_WIN32_FNAME) == 0) {
                        return NULL;
                }
                if (MultiByteToWideChar(CP_UTF8, 0, mode, strlen(mode),
                                        modeW, MAX_FOPEN_MODE) == 0) {
                        return NULL;
                }
                file = _wfopen(fnameW, modeW);
        }
        return file;
} /* fopen_win32 */

Best wishes,

Tom

文林 Wenlin Institute, Inc. Software for Learning Chinese
E-mail: wenlin@wenlin.com Web: http://www.wenlin.com
Telephone: 1-877-4-WENLIN (1-877-493-6546)

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-03-14