diff --git a/specs/Specs_WebHelpers.bas b/specs/Specs_WebHelpers.bas index 18988108..2b6fb64c 100644 --- a/specs/Specs_WebHelpers.bas +++ b/specs/Specs_WebHelpers.bas @@ -416,6 +416,11 @@ Public Function Specs() As SpecSuite ' --------------------------------------------- ' With Specs.It("should Base64 encode string") .Expect(WebHelpers.Base64Encode("Howdy!")).ToEqual "SG93ZHkh" + + .Expect(WebHelpers.Base64Encode("üöäÄÜÖß", "Windows-1250")).ToEqual "/PbkxNzW3w==" + .Expect(WebHelpers.Base64Encode("üöäÄÜÖß", "UTF-8")).ToEqual "77u/w7zDtsOkw4TDnMOWw58=" + .Expect(WebHelpers.Base64Encode("üöäÄÜÖß", "UTF-16")).ToEqual "//78APYA5ADEANwA1gDfAA==" + End With ' Base64Decode @@ -425,6 +430,11 @@ Public Function Specs() As SpecSuite ' The following implicitly has padding of "=" and "==" at end, base-64 decoding should handle this .Expect(WebHelpers.Base64Decode("SG93ZHk")).ToEqual "Howdy" + + .Expect(WebHelpers.Base64Decode("/PbkxNzW3w==", "Windows-1250")).ToEqual "üöäÄÜÖß" + .Expect(WebHelpers.Base64Decode("77u/w7zDtsOkw4TDnMOWw58=", "UTF-8")).ToEqual "üöäÄÜÖß" + .Expect(WebHelpers.Base64Decode("//78APYA5ADEANwA1gDfAA==", "UTF-16")).ToEqual "üöäÄÜÖß" + .Expect(WebHelpers.Base64Decode("eyJzdWIiOjEyMzQ1Njc4OTAsIm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlfQ")).ToEqual _ "{""sub"":1234567890,""name"":""John Doe"",""admin"":true}" End With diff --git a/src/WebHelpers.bas b/src/WebHelpers.bas index b1940a20..26a19add 100644 --- a/src/WebHelpers.bas +++ b/src/WebHelpers.bas @@ -1062,9 +1062,10 @@ End Function ' Base64-encode text. ' ' @param {Variant} Text Text to encode +' @param {String} [Charset=Windows-1250] - Charset for encoding (UTF-8, UTF-16, Windows-1250) ' @return {String} Encoded string '' -Public Function Base64Encode(Text As String) As String +Public Function Base64Encode(Text As String, Optional Charset As String = "Windows-1250") As String #If Mac Then Dim web_Command As String web_Command = "printf " & PrepareTextForPrintf(Text) & " | openssl base64" @@ -1072,10 +1073,22 @@ Public Function Base64Encode(Text As String) As String #Else Dim web_Bytes() As Byte - web_Bytes = VBA.StrConv(Text, vbFromUnicode) + With CreateObject("ADODB.Stream") + .Type = 2 ' adTypeText + .Open + ' For a list of the character set names that are known by a system, + ' see the subkeys of HKEY_CLASSES_ROOT\MIME\Database\Charset in the Windows Registry. + .Charset = Charset + .WriteText Text + .Position = 0 + .Type = 1 ' adTypeBinary + web_Bytes = .Read + .Close + End With + Base64Encode = web_AnsiBytesToBase64(web_Bytes) #End If - + Base64Encode = VBA.Replace$(Base64Encode, vbLf, "") End Function @@ -1083,9 +1096,10 @@ End Function ' Decode Base64-encoded text ' ' @param {Variant} Encoded Text to decode +' @param {String} [Charset=Windows-1250] - Charset for decoding (UTF-8, UTF-16, Windows-1250) ' @return {String} Decoded string '' -Public Function Base64Decode(Encoded As Variant) As String +Public Function Base64Decode(Encoded As Variant, Optional Charset As String = "Windows-1250") As String ' Add trailing padding, if necessary If (VBA.Len(Encoded) Mod 4 > 0) Then Encoded = Encoded & VBA.Left("====", 4 - (VBA.Len(Encoded) Mod 4)) @@ -1106,11 +1120,25 @@ Public Function Base64Decode(Encoded As Variant) As String web_Node.Text = Encoded Base64Decode = VBA.StrConv(web_Node.nodeTypedValue, vbUnicode) + With CreateObject("ADODB.Stream") + .Type = 1 ' adTypeBinary + .Open + .Write web_Node.nodeTypedValue + .Position = 0 + .Type = 2 ' adTypeText + ' For a list of the character set names that are known by a system, + ' see the subkeys of HKEY_CLASSES_ROOT\MIME\Database\Charset in the Windows Registry. + .Charset = Charset + Base64Decode = .ReadText + .Close + End With + Set web_Node = Nothing Set web_XmlObj = Nothing #End If End Function + '' ' Register custom converter for converting request `Body` and response `Content`. ' If the `ConvertCallback` or `ParseCallback` are object methods,