BASE64 toolkit

A toolkit to encode/decode BASE64 data.

Free to use, I’d just like a copy of the result and please give some credit.

©2006 Marcel Kilgus

B64_ENCODE

Syntax: output_size = B64_ENCODE(source_addr, length, dest_addr)

Encode the text at the given source address into BASE64. Writes result to the destination address and returns the length of the data written. By not using strings the data is not limited by the 32kb string limit.

The given destination buffer must be big enough for the encoded data. By reserving “length * 1.5” bytes one is on the safe side.

B64_DECODE

Syntax: output_size = B64_DECODE(source_addr, length, dest_addr)

Decode the given BASE64 data, pointed to by source_addr, into the memory located at dest_addr. Returns the size of the decoded data in bytes. By not using strings the data is not limited by the 32kb string limit.

The given destination buffer must be big enough for the decoded data. Decoded data is always smaller than encoded data.

Example

Encodes and decodes the Wikipedia moto.

a$="Man is distinguished, not only by his reason, but by this singular "
a$=a$&"passion from other animals, which is a lust of the mind, that by "
a$=a$&"a perseverance of delight in the continued and indefatigable "
a$=a$&"generation of knowledge, exceeds the short vehemence of any "
a$=a$&"carnal pleasure."
:
r$="TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0"&chr$(10)
r$=r$&"aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1"&chr$(10)
r$=r$&"c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0"&chr$(10)
r$=r$&"aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdl"&chr$(10)
r$=r$&"LCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4="&chr$(10)
:
mem=ALCHP($1000)
POKES mem,a$
el = B64_ENCODE(mem, LEN(a$),mem + $200)
res$ = PEEKS$(mem + $200, el)
PRINT res$
:
IF res$ = r$ THEN
  PRINT "Encoding okay!"
ELSE
  PRINT "Encoding failed!"
END IF
:
POKES mem,r$
el = B64_DECODE(mem, LEN(r$), mem + $200)
res$ = PEEKS$(mem + $200, el)
PRINT res$
:
IF res$ = a$ THEN
  PRINT "Decoding okay!"
ELSE
  PRINT "Decoding failed!"
END IF
PAUSE