Base64 file encoding

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Base64 file encoding

Post by rudolfo »

After some time of googling for base64.dll (with several hits, but I guess the downloading feature did not play well together with my old browser version) I went to http://libb64.sourceforge.net/ and downloaded that library code which is in the public domain. The command line tool that is included in the library is basically doing the same as the tool from Charles.
My first attempt was to make it a plain C Dll, but that was too difficult. And Chris Venter the author of the library also included a C++ wrapper that makes life a lot easier. So with a second approach I modified the command line tool into a Dll that exports two functions: base64_encode() and base64_decode(). Each one uses two parameters: the input filename and the output filename. I tested it with the following Basic code:

Code: Select all

Declare Function EncodeBase64 Lib "base64.dll" Alias "base64_encode" ( inFile, outFile ) As Integer
Declare Function DecodeBase64 Lib "base64.dll" Alias "base64_decode" ( inFile, outFile ) As Integer

Sub TestEncoder
  Dim sInputFile As String, sOutputFile As String
  Dim nRet As Integer

  sInputFile = "C:\home\rolf\staticMap.png"
  sOutputFile = "C:\home\rolf\staticMap.asc"
  nRet = EncodeBase64(sInputFile,sOutputFile)
  MsgBox nRet
End Sub
base64.dll is the name of the Dll and it needs to be in the search path. Runs at a promising speed.
The Dll is compiled with MinGW, so no special dependencies, but it is rather large (450kByte). I guess that's a tribute to the C++ classes that are used internally. If the input file cannot be found it a MessageBox will popup informing about this. But that's the only test that I have done apart from the pure encoding test. Better do some checks for correct padding and things like that.
And it is too large to upload it as an attachment. The zip archive is 135 KB, with bzip2 I get to 131KB, but that's still above 128 kByte. Sigh. But 7zip did the job to compress the file below 128k.
Attachments
base64.7z
Dll based on code from libb64.sourceforge.net
(108.07 KiB) Downloaded 273 times
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
zabolyx
Posts: 216
Joined: Fri Aug 07, 2009 7:28 pm

Re: Base64 file encoding

Post by zabolyx »

Interesting... I'll have to see about testing and integrating this into version 0.2.0

Sounds like a great way to go with this. This could make a lot of my code much faster if we could find some others that would handle some of the other tasks I have... building my EML file (not shabby using the base64 app and generating all of the text and headers in basic - come in at around 5 seconds to build an email with 5 images and a 3MB PDf attachment). As for getting it onto the end users machine I'm currently using the companies FTP server to store a few files that are used.. I simply use FileCopy to get a copy where I need it.

Another thing to consider is how this DLL method handles spaces in the path. I couldn't get the systemShellExecute to run a path with spaces even when converted to a URL Of course I was trying to pass the file input and output files path and names. It could be that I surpassed the command line maximum character limit (don't even remember what DOS' was or what Windows will handle). So I ended up using a batch file that I created with the macro and it worked then.

But to keep down the size of the SystemShellExecute or Shell command (and avoid spaces) I might have the macro copy the files needing converted into the same directory as the DLL or app (depending on what I end up changing to). The biggest issue I have with using this an application is that I have these CMD windows showing up for half a second for each encoded file. I could stop this if I could get the Shell command to work as I can set it to execute in a minimized window.

I hope to do some experimenting with this over the weekend.. and start implementing it into the codebase. I also have bug testing to complete and rebuilding of the upgrade engine in the version I have to get finished for the new year.
OOo 3.1 On Windows XP SP3 (Home)
Running portables of 2.4, 3.0, 3.1, and 3.2 on XP SP3 (Work)
OOo BASIC user

My contribution to the OOo Community code and more
https://sites.google.com/site/ooomacrolog/
Post Reply