Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equivalent DecompressString in python? #1

Open
acote5 opened this issue Mar 9, 2022 · 1 comment
Open

Equivalent DecompressString in python? #1

acote5 opened this issue Mar 9, 2022 · 1 comment

Comments

@acote5
Copy link

acote5 commented Mar 9, 2022

I'm attempting to create a script that is inspired by your post (and this project). My ultimate objective is to extract, modify, and inject the BICS_VIEW_HEADER in an Excel file. I'd like to use Python, and I've managed to do every step except I can't seem to replicate the DecompressString function correctly. Are you able to help out?

If I understand correctly, the operation that decompresses the Base64 string is Convert.FromBase64String(compressedText), and then to return it as UTF8, you use Encoding.UTF8.GetString. The other operations within the function are to write the values to a buffer stream of the right size (not required in python). Is this correct? I assume I am missing something, because the equivalent in Python is returning me gibrish... I've had to ignore errors to get it working altogether for the content of the BICS_VIEW_HEADER tag.

Here's the function in Python:

def string_decoder(string):
base64_bytes = string.encode('ascii')
message_bytes = base64.b64decode(base64_bytes)
message = message_bytes.decode('utf-8', errors='ignore')
return message

@Masterchen09
Copy link

I had the same issue when I tried to extract the BICS_VIEW_HEADER using Python and came across this repository. The solution is that you have to ignore the first four bytes of the BICS_VIEW_HEADER - it seems this is done in the DecompressString function using the Write method of the MemoryStream:

memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4)

Using Python the decompression of the string is quite easy (assuming bics_view_header is the text between the BICS_VIEW_HEADER tags of the xml; you do not have to encode it using ascii characters first, because b64decode can also work with strings):

bics_view_header = base64.b64decode(bics_view_header)
bics_view_header = gzip.decompress(bics_view_header[4:]).decode('utf-8')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants