Skip to content

Commit 7d45079

Browse files
committed
Merge branch 'dev' of https://github.com/Microsoft/msphpsql into dev
2 parents 2b3b7da + 94c5a67 commit 7d45079

7 files changed

Lines changed: 841 additions & 208 deletions

File tree

CHANGELOG.md

Lines changed: 94 additions & 61 deletions
Large diffs are not rendered by default.

source/shared/core_stmt.cpp

Lines changed: 142 additions & 138 deletions
Large diffs are not rendered by default.

source/shared/core_util.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,13 @@ bool convert_string_from_utf16( _In_ SQLSRV_ENCODING encoding, _In_reads_bytes_(
127127
flags = WC_ERR_INVALID_CHARS;
128128
}
129129

130-
// calculate the number of characters needed
131130
#ifndef _WIN32
132-
cchOutLen = SystemLocale::FromUtf16Strict( encoding, inString, cchInLen, NULL, 0 );
131+
// Allocate enough space to hold the largest possible number of bytes for UTF-8 conversion
132+
// instead of calling FromUtf16, for performance reasons
133+
cchOutLen = 4*cchInLen;
133134
#else
135+
// Calculate the number of output bytes required - no performance hit here because
136+
// WideCharToMultiByte is highly optimised
134137
cchOutLen = WideCharToMultiByte( encoding, flags,
135138
inString, cchInLen,
136139
NULL, 0, NULL, NULL );
@@ -142,9 +145,10 @@ bool convert_string_from_utf16( _In_ SQLSRV_ENCODING encoding, _In_reads_bytes_(
142145

143146
// Create a buffer to fit the encoded string
144147
char* newString = reinterpret_cast<char*>( sqlsrv_malloc( cchOutLen + 1 /* NULL char*/ ));
148+
memset(newString, '\0', cchOutLen+1);
145149

146150
#ifndef _WIN32
147-
int rc = SystemLocale::FromUtf16( encoding, inString, cchInLen, newString, static_cast<int>(cchOutLen));
151+
int rc = SystemLocale::FromUtf16Strict( encoding, inString, cchInLen, newString, static_cast<int>(cchOutLen));
148152
#else
149153
int rc = WideCharToMultiByte( encoding, flags, inString, cchInLen, newString, static_cast<int>(cchOutLen), NULL, NULL );
150154
#endif // !_WIN32
@@ -153,9 +157,13 @@ bool convert_string_from_utf16( _In_ SQLSRV_ENCODING encoding, _In_reads_bytes_(
153157
sqlsrv_free( newString );
154158
return false;
155159
}
160+
char* newString2 = reinterpret_cast<char*>( sqlsrv_malloc( rc + 1 /* NULL char*/ ));
161+
memset(newString2, '\0', rc+1);
162+
memcpy_s(newString2, rc, newString, rc);
163+
sqlsrv_free( newString );
156164

157-
*outString = newString;
158-
newString[cchOutLen] = '\0'; // null terminate the encoded string
165+
*outString = newString2;
166+
cchOutLen = rc;
159167

160168
return true;
161169
}

source/shared/globalization.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ class EncodingConverter
261261
return 0;
262262
}
263263
}
264+
//if a shift sequence is encountered, we need to advance output buffer
265+
iconv_ret = iconv( m_pCvtCache->GetIConv(), NULL, NULL, &dest.m_pBytes, &dest.m_nBytesLeft );
264266
}
265267

266268
return cchDest - (dest.m_nBytesLeft / sizeof(DestType));

source/shared/localization.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,14 @@ class SystemLocale
169169
static size_t FromUtf16Strict(UINT destCodePage, const WCHAR * src, SSIZE_T cchSrc,
170170
__out_ecount_opt(cchDest) char * dest, size_t cchDest,
171171
bool * pHasDataLoss = NULL, DWORD * pErrorCode = NULL);
172-
173-
172+
// CP1252 to UTF16 conversion which does not involve iconv
173+
static size_t CP1252ToUtf16( const char *src, SSIZE_T cchSrc, WCHAR *dest, size_t cchDest, DWORD *pErrorCode );
174+
175+
// UTF8/16 conversion which does not involve iconv
176+
static size_t Utf8To16( const char *src, SSIZE_T cchSrc, WCHAR *dest, size_t cchDest, DWORD *pErrorCode );
177+
static size_t Utf8From16( const WCHAR *src, SSIZE_T cchSrc, char *dest, size_t cchDest, DWORD *pErrorCode );
178+
static size_t Utf8To16Strict( const char *src, SSIZE_T cchSrc, WCHAR *dest, size_t cchDest, DWORD *pErrorCode );
179+
static size_t Utf8From16Strict( const WCHAR *src, SSIZE_T cchSrc, char *dest, size_t cchDest, DWORD *pErrorCode );
174180

175181
// -----------------------------------------------------------------------
176182
// Public Member Functions

0 commit comments

Comments
 (0)