Anonymous user
Base64 decode data: Difference between revisions
Added solution for Action!
m (→{{header|Phix}}: added syntax colouring, marked p2js compatible) |
(Added solution for Action!) |
||
Line 6:
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.
=={{header|Action!}}==
<lang Action!>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</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Base64_decode_data.png Screenshot from Atari 8-bit computer]
<pre>
Encoded:
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo
Decoded:
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich
</pre>
=={{header|Ada}}==
|