I have writen the following code to Base64 encode/decode a file.
Code:
;encode
handle.l = ReadFile(#PB_Any, <original file>);
Size.l=Lof(handle)
*FileData = AllocateMemory(Size)
ReadData(handle,*FileData,Size)
CloseFile(handle)
Size2.l= Size * 1.35
*NewFileData = AllocateMemory(Size2)
Base64Encoder(*FileData, Size, *NewFileData, Size2)
handle2.l = CreateFile(#PB_Any, <encrypted file>);
WriteData(handle2, *NewFileData, Size2)
CloseFile(handle2)
FreeMemory(*FileData)
FreeMemory(*NewFileData)
Code:
;decode
handle.l = ReadFile(#PB_Any, <encrypted file>)
Size=Lof(handle)
*FileData = AllocateMemory(Size)
ReadData(handle, *FileData, Size)
CloseFile(handle)
*Decoded = AllocateMemory(Size/1.35)
Bytes.l = Base64Decoder(*FileData, Size, *Decoded, Size)
Debug Bytes
handle2.l = CreateFile(#PB_Any,<original file>)
WriteData(handle2, *Decoded, MemorySize(*Decoded))
CloseFile(handle2)
FreeMemory(*Decoded)
Some files are encoded/decoded fine, some others not. Some times the code with some files runs fine and the new file has the same CRC with the original one. Some times with other different original files the new file has the last byte 00 than the original one. Some other times the code returns "Invalid memory access" to the last line. Can somebody tell me what am I doing wrong?