Skip to content
Permalink
Browse files

[modUTF8] Do not fail to decode invalid UTF-8, resulting in dropping …

…received messages.

Removes erroneous use of MB_ERR_INVALID_CHARS which causes strings
containing invalid UTF-8 to be completely dropped. Now replaces with
"?" (U+003F).
  • Loading branch information...
nmbook committed Oct 28, 2017
1 parent 4b4e8ec commit 0a7222bee8f12edc0b0c16f0682fb7e332673cc6
Showing with 2 additions and 2 deletions.
  1. +2 −2 trunk/modUTF8.bas
@@ -45,7 +45,7 @@ Public Function UTF8Decode(ByRef buf() As Byte) As String
Dim UnicodeChars As Long

' grab Length of string after conversion
UnicodeChars = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, VarPtr(buf(0)), UBound(buf) + 1, 0, 0)
UnicodeChars = MultiByteToWideChar(CP_UTF8, 0, VarPtr(buf(0)), UBound(buf) + 1, 0, 0)

If (UnicodeChars = 0) Then
Exit Function
@@ -55,7 +55,7 @@ Public Function UTF8Decode(ByRef buf() As Byte) As String
ReDim UnicodeBuffer(0 To (UnicodeChars * 2 - 1))

' translate utf-8 string to unicode
Call MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, VarPtr(buf(0)), UBound(buf) + 1, VarPtr(UnicodeBuffer(0)), UnicodeChars)
Call MultiByteToWideChar(CP_UTF8, 0, VarPtr(buf(0)), UBound(buf) + 1, VarPtr(UnicodeBuffer(0)), UnicodeChars)

' translate from unicode to ansi
UTF8Decode = StrConv(ByteArrToString(UnicodeBuffer()), vbFromUnicode)

0 comments on commit 0a7222b

Please sign in to comment.
You can’t perform that action at this time.