I am attempting to compress a string via use of zlib (I've tried this code with current 1.2.3 version of zlib and with zlib 1.1.3). My code works correctly, unless run on a Japanese machine. After compressing a file, I am encrypting it. The decryption is successful, but the call to uncompress returns -3 (Z_DATA_ERROR
, meaning the input data was corrupted). As there are no errors being logged, I know that no exceptions are being thrown, and that the compression function is returning 0 (Z_OK
, meaning it worked).
Thus, I suspect the problem is that the sCompressed
string is losing integrity on either the line "sCompressed = Left(sCompressed, lcompressedlen)
" or the line "encryptedData.Content = sCompressed
." Alternatively, VB6 might be doing something stupid helpful to the contents of sCompressed
during the call to compress
. I know that the return value of this function is not being corrupted later, because that would have broken decryption, which works fine.
Public Function EncryptString(ByVal Definition As String) As String
On Error GoTo ErrorHandler
Dim encryptedData As New CAPICOM.encryptedData
encryptedData.SetSecret KEY_CONST
Dim lStringLen As Long
Dim lcompressedlen As Long
Dim sCompressed As String
Dim lReturn As Long
Dim tstpost As String
lStringLen = Len(Definition)
lcompressedlen = (lStringLen * 1.01) + 13
sCompressed = Space(lcompressedlen)
lReturn = compress(sCompressed, lcompressedlen, Definition, lStringLen)
If lReturn <> 0 Then
sCompressed = "Error: " & CStr(lReturn)
'<LOG ERROR>'
Else
sCompressed = Left(sCompressed, lcompressedlen)
End If
encryptedData.Content = sCompressed
encryptedData.Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM_3DES
EncryptXmlString = encryptedData.Encrypt
Exit Function
ErrorHandler:
'<LOG ERROR>'
Resume Next
End Function
Conclusion:
I ended up making the program give an error message and quit if run on a machine with a suspicious character set. It is quite likely that this bug still exists on a few settings and also likely that it does not exist on some of the settings that trigger the error. However, since the target audience is English speakers, passing the Turkey Test is not important enough to justify actually spending more time on this.