Base64 decode data: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Perl 6}}: added zkl header)
(→‎{{header|zkl}}: added code)
Line 77: Line 77:


=={{header|zkl}}==
=={{header|zkl}}==
Using shared libraries for cURL and message hashing:
<lang zkl></lang>
<lang zkl>var [const] MsgHash=Import("zklMsgHash"), Curl=Import("zklCurl");
<lang zkl></lang>
icon:=Curl().get("http://rosettacode.org/favicon.ico"); //-->(Data(4,331),693,0)
icon=icon[0][icon[1],*]; // remove header
iconEncoded:=MsgHash.base64encode(icon);
iconDecoded:=MsgHash.base64decode(iconEncoded);
File("rosettaCodeIcon.ico","wb").write(iconDecoded); # eyeball checking says good
println("Is the Rosetta Code Icon the same (byte for byte) encoded then decoded: ",
icon==iconDecoded);</lang>
{{out}}
{{out}}
<pre>
<pre>
Is the Rosetta Code icon the same (byte for byte) encoded then decoded: True
</pre>
{{out|Text based test}}
<lang zkl>msg,b64 := "Rosetta Code Base64 decode data task", MsgHash.base64encode(msg);
println("Original: %s\nEncoded: %s\nDecoded: %s\nBytes: %s"
.fmt(msg, b64.text, MsgHash.base64decode(b64).text,
b64.bytes().apply("toString",16).concat(",")));</lang>
<pre>
Original: Rosetta Code Base64 decode data task
Encoded: Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr


Decoded: Rosetta Code Base64 decode data task
Bytes: 55,6d,39,7a,5a,58,52,30,59,53,42,44,62,32,52,6c,49,45,4a,68,63,32,55,32,4e,43,42,6b,5a,57,4e,76,5a,47,55,67,5a,47,46,30,59,53,42,30,59,58,4e,72,a
</pre>
</pre>

Revision as of 19:59, 10 December 2018

Base64 decode data is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

Go

As images can no longer be uploaded to RC, I've encoded and decoded a string rather than the Rosetta Code icon. <lang go>package main

import (

   "encoding/base64"
   "fmt"

)

func main() {

   msg := "Rosetta Code Base64 decode data task"
   fmt.Println("Original :", msg)
   encoded := base64.StdEncoding.EncodeToString([]byte(msg))
   fmt.Println("\nEncoded  :", encoded)
   decoded, err := base64.StdEncoding.DecodeString(encoded)
   if err != nil {
       fmt.Println(err)
       return
   }
   fmt.Println("\nDecoded  :", string(decoded))

}</lang>

Output:
Original : Rosetta Code Base64 decode data task

Encoded  : Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr

Decoded  : Rosetta Code Base64 decode data task

Perl 6

Works with: Rakudo version 2018.11

<lang perl6>my $e64 = ' VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY2 9tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g= ';

my @base64map = flat 'A' .. 'Z', 'a' .. 'z', ^10, '+', '/'; my %base64 is default(0) = @base64map.pairs.invert;

sub base64-decode-slow ($enc) {

   my $buf = Buf.new;
   for $enc.subst(/\s/, , :g).comb(4) -> $chunck {
       $buf.append: |(sprintf "%06d%06d%06d%06d", |$chunck.comb.map:
           {%base64{$_}.base(2)}).comb(8).map: {:2($_)};
   }
   $buf

}

say 'Slow:'; say base64-decode-slow($e64).decode('utf8');


  1. Of course, the above routine is slow and is only for demonstration purposes.
  2. For real code you should use a module, which is MUCH faster and heavily tested.

say "\nFast:"; use Base64::Native; say base64-decode($e64).decode('utf8');</lang>

Output:
Slow:
To err is human, but to really foul things up you need a computer.
    -- Paul R. Ehrlich

Fast:
To err is human, but to really foul things up you need a computer.
    -- Paul R. Ehrlich

zkl

Using shared libraries for cURL and message hashing: <lang zkl>var [const] MsgHash=Import("zklMsgHash"), Curl=Import("zklCurl");

icon:=Curl().get("http://rosettacode.org/favicon.ico"); //-->(Data(4,331),693,0) icon=icon[0][icon[1],*]; // remove header iconEncoded:=MsgHash.base64encode(icon); iconDecoded:=MsgHash.base64decode(iconEncoded); File("rosettaCodeIcon.ico","wb").write(iconDecoded); # eyeball checking says good println("Is the Rosetta Code Icon the same (byte for byte) encoded then decoded: ",

  icon==iconDecoded);</lang>
Output:
Is the Rosetta Code icon the same (byte for byte) encoded then decoded: True
Text based test:

<lang zkl>msg,b64 := "Rosetta Code Base64 decode data task", MsgHash.base64encode(msg); println("Original: %s\nEncoded: %s\nDecoded: %s\nBytes: %s"

  .fmt(msg, b64.text, MsgHash.base64decode(b64).text,
       b64.bytes().apply("toString",16).concat(",")));</lang>
Original: Rosetta Code Base64 decode data task
Encoded:  Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr

Decoded:  Rosetta Code Base64 decode data task
Bytes:    55,6d,39,7a,5a,58,52,30,59,53,42,44,62,32,52,6c,49,45,4a,68,63,32,55,32,4e,43,42,6b,5a,57,4e,76,5a,47,55,67,5a,47,46,30,59,53,42,30,59,58,4e,72,a