From 85eb864e84476fb2c6e37ed3362b1898c5e7c77c Mon Sep 17 00:00:00 2001 From: Roy Tam Date: Mon, 27 Jan 2020 12:53:45 +0800 Subject: [PATCH] [NSS] sqlite3: fix CP_UTF8 not working in Win95 and NT3.51 --- security/nss/lib/sqlite/sqlite3.c | 165 +++++++++++++++++++++++++++++- 1 file changed, 161 insertions(+), 4 deletions(-) diff --git a/security/nss/lib/sqlite/sqlite3.c b/security/nss/lib/sqlite/sqlite3.c index 90f6bb9b..86f376cc 100644 --- a/security/nss/lib/sqlite/sqlite3.c +++ b/security/nss/lib/sqlite/sqlite3.c @@ -31418,6 +31418,163 @@ SQLITE_PRIVATE void sqlite3MemSetDefault(void){ } #endif /* SQLITE_WIN32_MALLOC */ +/*** UTF16<-->UTF8 functions minicking MultiByteToWideChar/WideCharToMultiByte ***/ +int utf8GetMaskIndex(unsigned char n) { + if((unsigned char)(n + 2) < 0xc2) return 1; // 00~10111111, fe, ff + if(n < 0xe0) return 2; // 110xxxxx + if(n < 0xf0) return 3; // 1110xxxx + if(n < 0xf8) return 4; // 11110xxx + if(n < 0xfc) return 5; // 111110xx + return 6; // 1111110x +} + +int wc2Utf8Len(wchar_t ** n, int *len) { + wchar_t *ch = *n, ch2; + int qch; + if((0xD800 <= *ch && *ch <= 0xDBFF) && *len) { + ch2 = *(ch + 1); + if(0xDC00 <= ch2 && ch2 <= 0xDFFF) { + qch = 0x10000 + (((*ch - 0xD800) & 0x3ff) << 10) + ((ch2 - 0xDC00) & 0x3ff); + (*n)++; + (*len)--; + } + } + else + qch = (int) *ch; + + if (qch <= 0x7f) return 1; + else if (qch <= 0x7ff) return 2; + else if (qch <= 0xffff) return 3; + else if (qch <= 0x1fffff) return 4; + else if (qch <= 0x3ffffff) return 5; + else return 6; +} + +int Utf8ToWideChar(unsigned int unused1, unsigned long unused2, char *sb, int ss, wchar_t * wb, int ws) { + static const unsigned char utf8mask[] = { 0, 0xff, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; + char *p = (char *)(sb); + char *e = (char *)(sb + ss); + wchar_t *w = wb; + int cnt = 0, t, qch; + + if (ss < 1) { + ss = lstrlenA(sb); + e = (char *)(sb + ss); + } + + if (wb && ws) { + for (; p < e; ++w) { + t = utf8GetMaskIndex(*p); + qch = (*p++ & utf8mask[t]); + while(p < e && --t) + qch <<= 6, qch |= (*p++) & 0x3f; + if(qch < 0x10000) { + if(cnt <= ws) + *w = (wchar_t) qch; + cnt++; + } else { + if (cnt + 2 <= ws) { + *w++ = (wchar_t) (0xD800 + (((qch - 0x10000) >> 10) & 0x3ff)), + *w = (wchar_t) (0xDC00 + (((qch - 0x10000)) & 0x3ff)); + } + cnt += 2; + } + } + if(cnt < ws) { + *(wb+cnt) = 0; + return cnt; + } else { + *(wb+ws) = 0; + return ws; + } + } else { + for (t; p < e;) { + t = utf8GetMaskIndex(*p); + qch = (*p++ & utf8mask[t]); + while (p < e && --t) + qch <<= 6, qch |= (*p++) & 0x3f; + if (qch < 0x10000) + cnt++; + else + cnt += 2; + } + return cnt+1; + } +} + +int WideCharToUtf8(unsigned int unused1, unsigned long unused2, wchar_t * wb, int ws, char *sb, int ss) { + wchar_t *p = (wchar_t *)(wb); + wchar_t *e = (wchar_t *)(wb + ws); + wchar_t *oldp; + char *s = sb; + int cnt = 0, qch, t; + + if (ws < 1) { + ws = lstrlenW(wb); + e = (wchar_t *)(wb + ws); + } + + if (sb && ss) { + for (t; p < e; ++p) { + oldp = p; + t = wc2Utf8Len(&p, &ws); + + if (p != oldp) { /* unicode surrogates encountered */ + qch = 0x10000 + (((*oldp - 0xD800) & 0x3ff) << 10) + ((*p - 0xDC00) & 0x3ff); + } else + qch = *p; + + if (qch <= 0x7f) + *s++ = (char) (qch), + cnt++; + else if (qch <= 0x7ff) + *s++ = 0xc0 | (char) (qch >> 6), + *s++ = 0x80 | (char) (qch & 0x3f), + cnt += 2; + else if (qch <= 0xffff) + *s++ = 0xe0 | (char) (qch >> 12), + *s++ = 0x80 | (char) ((qch >> 6) & 0x3f), + *s++ = 0x80 | (char) (qch & 0x3f), + cnt += 3; + else if (qch <= 0x1fffff) + *s++ = 0xf0 | (char) (qch >> 18), + *s++ = 0x80 | (char) ((qch >> 12) & 0x3f), + *s++ = 0x80 | (char) ((qch >> 6) & 0x3f), + *s++ = 0x80 | (char) (qch & 0x3f), + cnt += 4; + else if (qch <= 0x3ffffff) + *s++ = 0xf8 | (char) (qch >> 24), + *s++ = 0x80 | (char) ((qch >> 18) & 0x3f), + *s++ = 0x80 | (char) ((qch >> 12) & 0x3f), + *s++ = 0x80 | (char) ((qch >> 6) & 0x3f), + *s++ = 0x80 | (char) (qch & 0x3f), + cnt += 5; + else + *s++ = 0xfc | (char) (qch >> 30), + *s++ = 0x80 | (char) ((qch >> 24) & 0x3f), + *s++ = 0x80 | (char) ((qch >> 18) & 0x3f), + *s++ = 0x80 | (char) ((qch >> 12) & 0x3f), + *s++ = 0x80 | (char) ((qch >> 6) & 0x3f), + *s++ = 0x80 | (char) (qch & 0x3f), + cnt += 6; + } + if(cnt < ss) { + *(sb+cnt) = 0; + return cnt; + } else { + *(sb+ss) = 0; + return ss; + } + } else { + for (t; p < e; ++p) { + t = wc2Utf8Len(&p, &ws); + cnt += t; + } + return cnt+1; + } +} +/*** Ends ***/ + /* ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?). ** @@ -31427,7 +31584,7 @@ static LPWSTR utf8ToUnicode(const char *zFilename){ int nChar; LPWSTR zWideFilename; - nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); + nChar = Utf8ToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0); if( nChar==0 ){ return 0; } @@ -31435,7 +31592,7 @@ static LPWSTR utf8ToUnicode(const char *zFilename){ if( zWideFilename==0 ){ return 0; } - nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, + nChar = Utf8ToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar); if( nChar==0 ){ sqlite3_free(zWideFilename); @@ -31452,7 +31609,7 @@ static char *unicodeToUtf8(LPCWSTR zWideFilename){ int nByte; char *zFilename; - nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); + nByte = WideCharToUtf8(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0); if( nByte == 0 ){ return 0; } @@ -31460,7 +31617,7 @@ static char *unicodeToUtf8(LPCWSTR zWideFilename){ if( zFilename==0 ){ return 0; } - nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, + nByte = WideCharToUtf8(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte, 0, 0); if( nByte == 0 ){ sqlite3_free(zFilename);