More refactoring and cleanup work on the Win32 string conversion and utility routines.

FossilOrigin-Name: 02ccb444a3d0b9a52ffd04cae9d9b6f654128c35
This commit is contained in:
mistachkin 2016-04-04 02:05:46 +00:00
parent 5daed673b8
commit 1293120345
3 changed files with 142 additions and 107 deletions

View File

@ -1,5 +1,5 @@
C Revise\sand\senhance\sthe\sWin32\sstring\sconversion\sroutines. C More\srefactoring\sand\scleanup\swork\son\sthe\sWin32\sstring\sconversion\sand\sutility\sroutines.
D 2016-04-03T22:44:16.657 D 2016-04-04T02:05:46.170
F Makefile.in e812bb732d7af01baa09f1278bd4f4a2e3a09449 F Makefile.in e812bb732d7af01baa09f1278bd4f4a2e3a09449
F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
F Makefile.msc fe57d7e3e74fa383fd01ced796c0ffd966fc094a F Makefile.msc fe57d7e3e74fa383fd01ced796c0ffd966fc094a
@ -358,7 +358,7 @@ F src/os.h 91ff889115ecd01f436d3611f7f5ea4dc12d92f1
F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85 F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85
F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa F src/os_setup.h c9d4553b5aaa6f73391448b265b89bed0b890faa
F src/os_unix.c b1ccb273771f41dbdbe0ba7c1ad63c38ad5972ec F src/os_unix.c b1ccb273771f41dbdbe0ba7c1ad63c38ad5972ec
F src/os_win.c 01ae58949a28edaecd5645abbe29ac9d2ee983fd F src/os_win.c b3ba9573d8d893e70a6a8015bbee572ecf7ffbef
F src/os_win.h eb7a47aa17b26b77eb97e4823f20a00b8bda12ca F src/os_win.h eb7a47aa17b26b77eb97e4823f20a00b8bda12ca
F src/pager.c 38718a019ca762ba4f6795425d5a54db70d1790d F src/pager.c 38718a019ca762ba4f6795425d5a54db70d1790d
F src/pager.h e1d38a2f14849e219df0f91f8323504d134c8a56 F src/pager.h e1d38a2f14849e219df0f91f8323504d134c8a56
@ -1480,7 +1480,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P f76c3a0ca40989fe9401c3b6f662f8e6ef2a730c P 345860c92195544aad44ea9b0d14c9ebbd50adf2
R 9a8e623b20453c0fbff989d22019add7 R 8daf8efac2ee8825007360be7ce04871
U mistachkin U mistachkin
Z 149573e0b667520348654eca6c5f3228 Z a69ce6a98d15b1e2f1ce825b8343a90e

View File

@ -1 +1 @@
345860c92195544aad44ea9b0d14c9ebbd50adf2 02ccb444a3d0b9a52ffd04cae9d9b6f654128c35

View File

@ -1306,6 +1306,12 @@ void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */ int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
if( nMin<-1 ) nMin = -1; /* all negative values become -1. */ if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE ); assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
#ifdef SQLITE_ENABLE_API_ARMOR
if( !zBuf ){
(void)SQLITE_MISUSE_BKPT;
return;
}
#endif
#if defined(SQLITE_WIN32_HAS_ANSI) #if defined(SQLITE_WIN32_HAS_ANSI)
if( nMin>0 ){ if( nMin>0 ){
memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE); memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
@ -1631,9 +1637,9 @@ void sqlite3MemSetDefault(void){
#endif /* SQLITE_WIN32_MALLOC */ #endif /* SQLITE_WIN32_MALLOC */
/* /*
** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). ** Convert a UTF-8 string to Microsoft Unicode.
** **
** Space to hold the returned string is obtained from malloc. ** Space to hold the returned string is obtained from sqlite3_malloc().
*/ */
static LPWSTR winUtf8ToUnicode(const char *zText){ static LPWSTR winUtf8ToUnicode(const char *zText){
int nChar; int nChar;
@ -1657,8 +1663,9 @@ static LPWSTR winUtf8ToUnicode(const char *zText){
} }
/* /*
** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is ** Convert a Microsoft Unicode string to UTF-8.
** obtained from sqlite3_malloc(). **
** Space to hold the returned string is obtained from sqlite3_malloc().
*/ */
static char *winUnicodeToUtf8(LPCWSTR zWideText){ static char *winUnicodeToUtf8(LPCWSTR zWideText){
int nByte; int nByte;
@ -1682,15 +1689,14 @@ static char *winUnicodeToUtf8(LPCWSTR zWideText){
} }
/* /*
** Convert an ANSI string to Microsoft Unicode, based on the ** Convert an ANSI string to Microsoft Unicode, using the ANSI or OEM
** current codepage settings for file apis. ** code page.
** **
** Space to hold the returned string is obtained ** Space to hold the returned string is obtained from sqlite3_malloc().
** from sqlite3_malloc.
*/ */
static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){ static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
int nByte; int nByte;
LPWSTR zMbcsFilename; LPWSTR zMbcsText;
int codepage = useAnsi ? CP_ACP : CP_OEMCP; int codepage = useAnsi ? CP_ACP : CP_OEMCP;
nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL, nByte = osMultiByteToWideChar(codepage, 0, zText, -1, NULL,
@ -1698,25 +1704,24 @@ static LPWSTR winMbcsToUnicode(const char *zText, int useAnsi){
if( nByte==0 ){ if( nByte==0 ){
return 0; return 0;
} }
zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) ); zMbcsText = sqlite3MallocZero( nByte*sizeof(WCHAR) );
if( zMbcsFilename==0 ){ if( zMbcsText==0 ){
return 0; return 0;
} }
nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsFilename, nByte = osMultiByteToWideChar(codepage, 0, zText, -1, zMbcsText,
nByte); nByte);
if( nByte==0 ){ if( nByte==0 ){
sqlite3_free(zMbcsFilename); sqlite3_free(zMbcsText);
zMbcsFilename = 0; zMbcsText = 0;
} }
return zMbcsFilename; return zMbcsText;
} }
/* /*
** Convert Microsoft Unicode to multi-byte character string, based on the ** Convert a Microsoft Unicode string to a multi-byte character string,
** user's ANSI codepage. ** using the ANSI or OEM code page.
** **
** Space to hold the returned string is obtained from ** Space to hold the returned string is obtained from sqlite3_malloc().
** sqlite3_malloc().
*/ */
static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){ static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
int nByte; int nByte;
@ -1741,105 +1746,135 @@ static char *winUnicodeToMbcs(LPCWSTR zWideText, int useAnsi){
} }
/* /*
** Convert multibyte character string to UTF-8. Space to hold the ** Convert a multi-byte character string to UTF-8.
** returned string is obtained from sqlite3_malloc(). **
** Space to hold the returned string is obtained from sqlite3_malloc().
*/
static char *winMbcsToUtf8(const char *zText, int useAnsi){
char *zTextUtf8;
LPWSTR zTmpWide;
zTmpWide = winMbcsToUnicode(zText, useAnsi);
if( zTmpWide==0 ){
return 0;
}
zTextUtf8 = winUnicodeToUtf8(zTmpWide);
sqlite3_free(zTmpWide);
return zTextUtf8;
}
/*
** Convert a UTF-8 string to a multi-byte character string.
**
** Space to hold the returned string is obtained from sqlite3_malloc().
*/
static char *winUtf8ToMbcs(const char *zText, int useAnsi){
char *zTextMbcs;
LPWSTR zTmpWide;
zTmpWide = winUtf8ToUnicode(zText);
if( zTmpWide==0 ){
return 0;
}
zTextMbcs = winUnicodeToMbcs(zTmpWide, useAnsi);
sqlite3_free(zTmpWide);
return zTextMbcs;
}
/*
** This is a public wrapper for the winUtf8ToUnicode() function.
*/
LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText){
#ifdef SQLITE_ENABLE_API_ARMOR
if( !zText ){
(void)SQLITE_MISUSE_BKPT;
return 0;
}
#endif
#ifndef SQLITE_OMIT_AUTOINIT
if( sqlite3_initialize() ) return 0;
#endif
return winUtf8ToUnicode(zText);
}
/*
** This is a public wrapper for the winUnicodeToUtf8() function.
*/
char *sqlite3_win32_unicode_to_utf8(LPCWSTR zWideText){
#ifdef SQLITE_ENABLE_API_ARMOR
if( !zWideText ){
(void)SQLITE_MISUSE_BKPT;
return 0;
}
#endif
#ifndef SQLITE_OMIT_AUTOINIT
if( sqlite3_initialize() ) return 0;
#endif
return winUnicodeToUtf8(zWideText);
}
/*
** This is a public wrapper for the winMbcsToUtf8() function.
*/ */
char *sqlite3_win32_mbcs_to_utf8(const char *zText){ char *sqlite3_win32_mbcs_to_utf8(const char *zText){
char *zTextUtf8; #ifdef SQLITE_ENABLE_API_ARMOR
LPWSTR zTmpWide; if( !zText ){
(void)SQLITE_MISUSE_BKPT;
zTmpWide = winMbcsToUnicode(zText, osAreFileApisANSI());
if( zTmpWide==0 ){
return 0; return 0;
} }
zTextUtf8 = winUnicodeToUtf8(zTmpWide); #endif
sqlite3_free(zTmpWide); #ifndef SQLITE_OMIT_AUTOINIT
return zTextUtf8; if( sqlite3_initialize() ) return 0;
#endif
return winMbcsToUtf8(zText, osAreFileApisANSI());
} }
/* /*
** Convert multibyte character string to UTF-8 using the ANSI codepage. ** This is a public wrapper for the winMbcsToUtf8() function.
** Space to hold the returned string is obtained from sqlite3_malloc().
*/ */
char *sqlite3_win32_mbcs_to_utf8_via_ansi(const char *zText){ char *sqlite3_win32_mbcs_to_utf8_v2(const char *zText, int useAnsi){
char *zTextUtf8; #ifdef SQLITE_ENABLE_API_ARMOR
LPWSTR zTmpWide; if( !zText ){
(void)SQLITE_MISUSE_BKPT;
zTmpWide = winMbcsToUnicode(zText, 1);
if( zTmpWide==0 ){
return 0; return 0;
} }
zTextUtf8 = winUnicodeToUtf8(zTmpWide); #endif
sqlite3_free(zTmpWide); #ifndef SQLITE_OMIT_AUTOINIT
return zTextUtf8; if( sqlite3_initialize() ) return 0;
#endif
return winMbcsToUtf8(zText, useAnsi);
} }
/* /*
** Convert multibyte character string to UTF-8 using the OEM codepage. ** This is a public wrapper for the winUtf8ToMbcs() function.
** Space to hold the returned string is obtained from sqlite3_malloc().
*/
char *sqlite3_win32_mbcs_to_utf8_via_oem(const char *zText){
char *zTextUtf8;
LPWSTR zTmpWide;
zTmpWide = winMbcsToUnicode(zText, 0);
if( zTmpWide==0 ){
return 0;
}
zTextUtf8 = winUnicodeToUtf8(zTmpWide);
sqlite3_free(zTmpWide);
return zTextUtf8;
}
/*
** Convert UTF-8 to multibyte character string. Space to hold the
** returned string is obtained from sqlite3_malloc().
*/ */
char *sqlite3_win32_utf8_to_mbcs(const char *zText){ char *sqlite3_win32_utf8_to_mbcs(const char *zText){
char *zTextMbcs; #ifdef SQLITE_ENABLE_API_ARMOR
LPWSTR zTmpWide; if( !zText ){
(void)SQLITE_MISUSE_BKPT;
zTmpWide = winUtf8ToUnicode(zText);
if( zTmpWide==0 ){
return 0; return 0;
} }
zTextMbcs = winUnicodeToMbcs(zTmpWide, osAreFileApisANSI()); #endif
sqlite3_free(zTmpWide); #ifndef SQLITE_OMIT_AUTOINIT
return zTextMbcs; if( sqlite3_initialize() ) return 0;
#endif
return winUtf8ToMbcs(zText, osAreFileApisANSI());
} }
/* /*
** Convert UTF-8 to multibyte character string using the ANSI codepage. ** This is a public wrapper for the winUtf8ToMbcs() function.
** Space to hold the returned string is obtained from sqlite3_malloc().
*/ */
char *sqlite3_win32_utf8_to_mbcs_via_ansi(const char *zText){ char *sqlite3_win32_utf8_to_mbcs_v2(const char *zText, int useAnsi){
char *zTextMbcs; #ifdef SQLITE_ENABLE_API_ARMOR
LPWSTR zTmpWide; if( !zText ){
(void)SQLITE_MISUSE_BKPT;
zTmpWide = winUtf8ToUnicode(zText);
if( zTmpWide==0 ){
return 0; return 0;
} }
zTextMbcs = winUnicodeToMbcs(zTmpWide, 1); #endif
sqlite3_free(zTmpWide); #ifndef SQLITE_OMIT_AUTOINIT
return zTextMbcs; if( sqlite3_initialize() ) return 0;
} #endif
return winUtf8ToMbcs(zText, useAnsi);
/*
** Convert UTF-8 to multibyte character string using the OEM codepage.
** Space to hold the returned string is obtained from sqlite3_malloc().
*/
char *sqlite3_win32_utf8_to_mbcs_via_oem(const char *zText){
char *zTextMbcs;
LPWSTR zTmpWide;
zTmpWide = winUtf8ToUnicode(zText);
if( zTmpWide==0 ){
return 0;
}
zTextMbcs = winUnicodeToMbcs(zTmpWide, 0);
sqlite3_free(zTmpWide);
return zTextMbcs;
} }
/* /*
@ -1941,7 +1976,7 @@ static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
if( dwLen > 0 ){ if( dwLen > 0 ){
/* allocate a buffer and convert to UTF8 */ /* allocate a buffer and convert to UTF8 */
sqlite3BeginBenignMalloc(); sqlite3BeginBenignMalloc();
zOut = sqlite3_win32_mbcs_to_utf8(zTemp); zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
sqlite3EndBenignMalloc(); sqlite3EndBenignMalloc();
/* free the system buffer allocated by FormatMessage */ /* free the system buffer allocated by FormatMessage */
osLocalFree(zTemp); osLocalFree(zTemp);
@ -4386,7 +4421,7 @@ static char *winConvertToUtf8Filename(const void *zFilename){
} }
#ifdef SQLITE_WIN32_HAS_ANSI #ifdef SQLITE_WIN32_HAS_ANSI
else{ else{
zConverted = sqlite3_win32_mbcs_to_utf8(zFilename); zConverted = winMbcsToUtf8(zFilename, osAreFileApisANSI());
} }
#endif #endif
/* caller will handle out of memory */ /* caller will handle out of memory */
@ -4407,7 +4442,7 @@ static void *winConvertFromUtf8Filename(const char *zFilename){
} }
#ifdef SQLITE_WIN32_HAS_ANSI #ifdef SQLITE_WIN32_HAS_ANSI
else{ else{
zConverted = sqlite3_win32_utf8_to_mbcs(zFilename); zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
} }
#endif #endif
/* caller will handle out of memory */ /* caller will handle out of memory */
@ -4608,7 +4643,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(), return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
"winGetTempname3", 0); "winGetTempname3", 0);
} }
zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath); zUtf8 = winMbcsToUtf8(zMbcsPath, osAreFileApisANSI());
if( zUtf8 ){ if( zUtf8 ){
sqlite3_snprintf(nMax, zBuf, "%s", zUtf8); sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
sqlite3_free(zUtf8); sqlite3_free(zUtf8);
@ -5386,7 +5421,7 @@ static int winFullPathname(
"winFullPathname4", zRelative); "winFullPathname4", zRelative);
} }
sqlite3_free(zConverted); sqlite3_free(zConverted);
zOut = sqlite3_win32_mbcs_to_utf8(zTemp); zOut = winMbcsToUtf8(zTemp, osAreFileApisANSI());
sqlite3_free(zTemp); sqlite3_free(zTemp);
} }
#endif #endif