diff options
author | Robin Watts <robin.watts@artifex.com> | 2013-03-01 16:42:26 +0000 |
---|---|---|
committer | Robin Watts <robin.watts@artifex.com> | 2013-03-01 16:50:22 +0000 |
commit | a1bfb03748a46e757ae937888c20b72720e1ac9d (patch) | |
tree | 8f91a64a44337df16c6119262ca67ab4b796ec3e /fitz | |
parent | d887cb0dd0b861371c0b5ae7629795349f5aa6f9 (diff) | |
download | mupdf-a1bfb03748a46e757ae937888c20b72720e1ac9d.tar.xz |
Bug 693624: Ensure that windows copes with utf8 filenames
When running under Windows, replace fopen with our own fopen_utf8
that converts from utf8 to unicode before calling the unicode
version of fopen.
Diffstat (limited to 'fitz')
-rw-r--r-- | fitz/base_string.c | 10 | ||||
-rw-r--r-- | fitz/base_time.c | 35 | ||||
-rw-r--r-- | fitz/fitz.h | 7 |
3 files changed, 46 insertions, 6 deletions
diff --git a/fitz/base_string.c b/fitz/base_string.c index 7385b500..d61f8cf3 100644 --- a/fitz/base_string.c +++ b/fitz/base_string.c @@ -101,7 +101,7 @@ enum }; int -fz_chartorune(int *rune, char *str) +fz_chartorune(int *rune, const char *str) { int c, c1, c2, c3; long l; @@ -110,7 +110,7 @@ fz_chartorune(int *rune, char *str) * one character sequence * 00000-0007F => T1 */ - c = *(unsigned char*)str; + c = *(const unsigned char*)str; if(c < Tx) { *rune = c; return 1; @@ -120,7 +120,7 @@ fz_chartorune(int *rune, char *str) * two character sequence * 0080-07FF => T2 Tx */ - c1 = *(unsigned char*)(str+1) ^ Tx; + c1 = *(const unsigned char*)(str+1) ^ Tx; if(c1 & Testx) goto bad; if(c < T3) { @@ -137,7 +137,7 @@ fz_chartorune(int *rune, char *str) * three character sequence * 0800-FFFF => T3 Tx Tx */ - c2 = *(unsigned char*)(str+2) ^ Tx; + c2 = *(const unsigned char*)(str+2) ^ Tx; if(c2 & Testx) goto bad; if(c < T4) { @@ -152,7 +152,7 @@ fz_chartorune(int *rune, char *str) * four character sequence (21-bit value) * 10000-1FFFFF => T4 Tx Tx Tx */ - c3 = *(unsigned char*)(str+3) ^ Tx; + c3 = *(const unsigned char*)(str+3) ^ Tx; if (c3 & Testx) goto bad; if (c < T5) { diff --git a/fitz/base_time.c b/fitz/base_time.c index 476749a7..01877a30 100644 --- a/fitz/base_time.c +++ b/fitz/base_time.c @@ -5,6 +5,7 @@ #include <winsock2.h> #endif #include <windows.h> +#include "fitz.h" #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 @@ -37,6 +38,40 @@ int gettimeofday(struct timeval *tv, struct timezone *tz) return 0; } +FILE *fopen_utf8(const char *name, const char *mode) +{ + wchar_t *wname, *wmode, *d; + const char *s; + int c; + FILE *file; + + d = wname = malloc((strlen(name)+1) * sizeof(wchar_t)); + if (d == NULL) + return NULL; + s = name; + while (*s) { + s += fz_chartorune(&c, s); + *d++ = c; + } + *d = 0; + d = wmode = malloc((strlen(mode)+1) * sizeof(wchar_t)); + if (d == NULL) + { + free(wname); + return NULL; + } + s = mode; + while (*s) { + s += fz_chartorune(&c, s); + *d++ = c; + } + *d = 0; + file = _wfopen(wname, wmode); + free(wname); + free(wmode); + return file; +} + #else void fz_gettimeofday_dummy() { } diff --git a/fitz/fitz.h b/fitz/fitz.h index 29b9cee4..f915d308 100644 --- a/fitz/fitz.h +++ b/fitz/fitz.h @@ -59,12 +59,17 @@ int gettimeofday(struct timeval *tv, struct timezone *tz); #define isnan _isnan #define hypotf _hypotf +#define fopen fopen_utf8 + +FILE *fopen_utf8(const char *name, const char *mode); + #else /* Unix or close enough */ #include <unistd.h> #ifndef O_BINARY #define O_BINARY 0 + #endif #endif @@ -628,7 +633,7 @@ int fz_strlcat(char *dst, const char *src, int n); Returns the number of bytes consumed. Does not throw exceptions. */ -int fz_chartorune(int *rune, char *str); +int fz_chartorune(int *rune, const char *str); /* fz_runetochar: UTF8 encode a rune to a sequence of chars. |