See Base64 encode data.

Now write a program that takes the output of the Base64 encode data task as input and regenerate the original file.

When working on the VBA implementation I found several 'solutions' on the net, including one from the software maker himself, that showed output with incorrect padding. Obviously with incorrect padding in the output you can not decode correctly to the original file again.

BYTE FUNC FindIndex(BYTE b)
  IF b>='A AND b<='Z THEN
    RETURN (b-'A)
  ELSEIF b>='a AND b<='z THEN
    RETURN (b-'a+26)
  ELSEIF b>='0 AND b<='9 THEN
    RETURN (b-'0+52)
  ELSEIF b='+ THEN
    RETURN (62)
  ELSEIF b='/ THEN
    RETURN (63)
  FI
RETURN (-1)
 
PROC PrintChar(CHAR c)
  IF c=10 THEN
    PutE()
  ELSE
    Put(c)
  FI
RETURN
 
PROC Decode(CHAR ARRAY s)
  BYTE i,b1,b2,b3,b4,i1,i2,i3,i4
  CHAR c
 
  IF s(0) MOD 4#0 THEN
    PrintE("Invalid length of string!!!")
    Break()
  FI
 
  i=1
  WHILE i<=s(0)
  DO
    b1=s(i) i==+1
    b2=s(i) i==+1
    b3=s(i) i==+1
    b4=s(i) i==+1
 
    i1=FindIndex(b1)
    i2=FindIndex(b2)
 
    c=i1 LSH 2
    c==%i2 RSH 4
    PrintChar(c)
 
    IF b3#'= THEN
      i3=FindIndex(b3)
      c=(i2&$0F) LSH 4
      c==%i3 RSH 2
      PrintChar(c)
 
      IF b4#'= THEN
        i4=FindIndex(b4)
        c=(i3&$03) LSH 6
        c==%i4
        PrintChar(c)
      FI
    FI
  OD
RETURN
 
PROC Test(CHAR ARRAY s)
  PrintE("Encoded:")
  PrintE(s)
  PutE()
  PrintE("Decoded:")
  Decode(s)
  PutE()
RETURN
 
PROC Main()
  Test("VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo")
RETURN