Android Question Base64 decoding problem

cxbs

Active Member
Licensed User
Longtime User
Hello everyone!
Using the following two different decoding methods will get different results
But in b4i, the result extracted by using stringutils.decodebase64 is correct
Is the function of stringutils.decodebase64 in b4a and b4i different?

(The above English is from translation software)

B4X:
    Dim B64 As Base64
    Dim su As StringUtils
    Dim compress As CompressedStreams
    Dim a As String="H4sIAP8sDWEA/4uuVjJQslIKcTIwMFTSUTKEsA2NgOyg5LwSJStDHSU3MMNIRykkMzcVKFKrA9EU6QHVYaRUGwsAcgHeqUkAAAA="

    Dim d() As Byte
    d=B64.DecodeStoB(a)
    Log("OK Array Size:" & d.Length & "=" & d(0) & "," & d(1) & "," & d(2))
    'OK Array Size:74=31,-117,8
    d=compress.DecompressBytes(d,"gzip") '<- Normal decompression   
    
    Dim d() As Byte
    d=su.DecodeBase64(a)
    Log("Error Array Size:" & d.Length & "," & d(0) & "," & d(1) & "," & d(2))
    'Error Array Size:73=91,123,34
    
    d=compress.DecompressBytes(d,"gzip") '<-- Unable to unzip
 

Attachments

  • DecodeBase64.zip
    9.2 KB · Views: 208

cxbs

Active Member
Licensed User
Longtime User
After many tests, it is found that the stringutils.decodebase64 in b4a already contains the function of compress.decompressbytes (D, "gzip"), but the stringutils.decodebase64 in b4i does not contain the function of compress.decompressbytes! Therefore, pay attention to the conversion between b4i and b4a codes!
 
Upvote 0

cxbs

Active Member
Licensed User
Longtime User
But a strange problem arose again
The result of the following code is the same
Does stringutils.decodebase64 check whether it is gzip compression during decoding, and if so, do gzip decompression along the way!
(the above English is from translation software)

B4X:
    Dim B64 As Base64
    Dim su As StringUtils
    Dim a As String="DQIAAAAAAAB4nPNxtArJL1dABrzBIVYQlgmvsz+QacCrHQESMTXj1QUzDIAikSCGuQmvLphhZMLrGGGlDdIDZEVa6ULEfCFiQBN8I2GsABcrLTDglVIYBQMNGBiYgzrVVwgJMzB8lgIAoJhJig=="
    Dim d() As Byte
    d=B64.DecodeStoB(a)
    Log("Array1 Size:" & d.Length & "=" & d(0) & "," & d(1) & "," & d(2))
    'Array1 Size:109=13,2,0 
    
    Dim d2() As Byte
    d2=su.DecodeBase64(a)
    Log("Array2 Size:" & d2.Length & "," & d2(0) & "," & d2(1) & "," & d2(2))
    'Array2 Size:109,13,2,0
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
That's true. It will automatically gunzip the data when it detects the gzip header. This is true in B4J and B4A.

You can use this sub instead:
B4X:
Private Sub DecodeWithoutGUnzip (s As String) As Byte()
    Dim jo As JavaObject
    Return jo.InitializeStatic("anywheresoftware.b4a.objects.Base64").RunMethod("decode", Array(s, 4))
End Sub
 
Upvote 0

cxbs

Active Member
Licensed User
Longtime User
That's true. It will automatically gunzip the data when it detects the gzip header. This is true in B4J and B4A.

You can use this sub instead:
B4X:
Private Sub DecodeWithoutGUnzip (s As String) As Byte()
    Dim jo As JavaObject
    Return jo.InitializeStatic("anywheresoftware.b4a.objects.Base64").RunMethod("decode", Array(s, 4))
End Sub

Thank you, Mr. Erel!
In addition, I'm worried that this will happen to other class library functions
 
Upvote 0
Top