Base64 decode data: Difference between revisions

added RPL
(added RPL)
 
(16 intermediate revisions by 11 users not shown)
Line 126:
<pre>To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich</pre>
 
=={{header|BaCon}}==
Using the result from the [[Base64 encode data]] task as requested, but the result is abbreviated in the code below.
<syntaxhighlight lang="bacon">data$ = "AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAE.......QAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAE="
ico$ = B64DEC$(data$)
BSAVE ico$ TO "favicon.ico" SIZE LEN(ico$)</syntaxhighlight>
 
=={{header|Bash}}==
Line 190 ⟶ 184:
done
done</syntaxhighlight>
 
=={{header|BASIC}}==
==={{header|BaCon}}===
Using the result from the [[Base64 encode data]] task as requested, but the result is abbreviated in the code below.
<syntaxhighlight lang="bacon">data$ = "AAABAAIAEBAAAAAAAABoBQAAJgAAACAgAAAAAAAAqAgAAI4FAAAoAAAAE.......QAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAE="
ico$ = B64DEC$(data$)
BSAVE ico$ TO "favicon.ico" SIZE LEN(ico$)</syntaxhighlight>
 
=={{header|C}}==
Line 546 ⟶ 547:
"To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich"</pre>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
/**
* This implementation is encapsulated as Base64Format in the web.xtclang.org module,
* but the module is currently in flux, so the implementation is included "inline" here.
*/
module Base64 {
@Inject Console console;
void run() {
String orig = \|VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIH\
|lvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
;
Byte[] bytes = decode(orig);
String text = encode(bytes, pad=True);
assert text == orig;
console.print($"base64={text}, bytes={bytes}");
}
 
static Byte[] read(Iterator<Char> stream) {
Int charLen = 0;
charLen := stream.knownSize();
Byte[] byteBuf = new Byte[](charLen * 6 / 8);
Byte prevBits = 0;
Int prevCount = 0;
while (Char ch := stream.next()) {
if (Byte newBits := isBase64(ch, assertTrash=True)) {
if (prevCount == 0) {
prevBits = newBits;
prevCount = 6;
} else {
byteBuf.add((prevBits << 8-prevCount) | (newBits >> prevCount-2));
prevBits = newBits;
prevCount -= 2;
}
}
}
 
return byteBuf.freeze(True);
}
 
static void write(Byte[] value, Appender<Char> stream, Boolean pad=False, Int? lineLength=Null) {
lineLength ?:= Int.MaxValue;
 
Int lineOffset = 0;
Int totalChars = 0;
Byte prevByte = 0;
Int prevCount = 0; // number of leftover bits from the previous byte
Int byteOffset = 0;
Int byteLength = value.size;
while (True) {
// glue together the next six bits, which will create one character of output
Byte sixBits;
if (byteOffset >= byteLength) {
if (prevCount == 0) {
break;
}
sixBits = prevByte << 6 - prevCount;
prevCount = 0;
} else if (prevCount == 6) {
sixBits = prevByte << 6 - prevCount;
prevCount = 0;
} else {
Byte nextByte = value[byteOffset++];
sixBits = (prevByte << 6 - prevCount) | (nextByte >> 2 + prevCount);
prevByte = nextByte;
prevCount += 2;
}
 
if (lineOffset >= lineLength) {
stream.add('\r').add('\n');
totalChars += lineOffset;
lineOffset = 0;
}
 
stream.add(base64(sixBits & 0b111111));
++lineOffset;
}
 
if (pad) {
totalChars += lineOffset;
for (Int i = 0, Int padCount = 4 - (totalChars & 0b11) & 0b11; i < padCount; ++i) {
if (lineOffset >= lineLength) {
stream.add('\r').add('\n');
lineOffset = 0;
}
 
stream.add('=');
++lineOffset;
}
}
}
 
static String encode(Byte[] value, Boolean pad=False, Int? lineLength=Null) {
// calculate buffer size
Int byteLen = value.size;
Int charLen = (byteLen * 8 + 5) / 6;
if (pad) {
charLen += 4 - (charLen & 0b11) & 0b11;
}
if (lineLength != Null) {
charLen += ((charLen + lineLength - 1) / lineLength - 1).maxOf(0) * 2;
}
 
StringBuffer charBuf = new StringBuffer(charLen);
write(value, charBuf, pad, lineLength);
return charBuf.toString();
}
 
static Byte[] decode(String text) {
Int charLen = text.size;
Byte[] byteBuf = new Byte[](charLen * 6 / 8);
Byte prevBits = 0;
Int prevCount = 0;
for (Int offset = 0; offset < charLen; ++offset) {
if (Byte newBits := isBase64(text[offset], assertTrash=True)) {
if (prevCount == 0) {
prevBits = newBits;
prevCount = 6;
} else {
byteBuf.add((prevBits << 8-prevCount) | (newBits >> prevCount-2));
prevBits = newBits;
prevCount -= 2;
}
}
}
 
return byteBuf.freeze(True);
}
 
/**
* Translate a single Base64 character to the least significant 6 bits of a `Byte` value.
*
* @param ch the Base64 character; no pad or newlines allowed
*
* @return the value in the range `0 ..< 64`
*/
static Byte valOf(Char ch) {
return switch (ch) {
case 'A'..'Z': (ch - 'A').toUInt8();
case 'a'..'z': (ch - 'a').toUInt8() + 26;
case '0'..'9': (ch - '0').toUInt8() + 52;
case '+': 62;
case '/': 63;
 
case '=': assert as $"Unexpected padding character in Base64: {ch.quoted()}";
case '\r', '\n': assert as $"Unexpected newline character in Base64: {ch.quoted()}";
default: assert as $"Invalid Base64 character: {ch.quoted()}";
};
}
 
/**
* Translate a single Base64 character to the least significant 6 bits of a `Byte` value.
*
* @param ch the character to test if it is Base64
* @param assertTrash (optional) pass True to assert on illegal Base64 characters
*
* @return the value in the range `0 ..< 64`
*/
static conditional Byte isBase64(Char ch, Boolean assertTrash=False) {
return switch (ch) {
case 'A'..'Z': (True, (ch - 'A').toUInt8());
case 'a'..'z': (True, (ch - 'a').toUInt8() + 26);
case '0'..'9': (True, (ch - '0').toUInt8() + 52);
case '+': (True, 62);
case '/': (True, 63);
 
case '=': // "pad" sometimes allowed (or required) at end
case '\r', '\n': // newlines sometimes allowed (or required)
False;
 
default: assertTrash ? assert as $"Invalid Base64 character: {ch.quoted()}" : False;
};
}
 
/**
* Convert the passed byte value to a Base64 character.
*
* @param the byte value, which must be in the range `0..63`
*
* @return the Base64 character
*/
static Char base64(Byte byte) {
return switch (byte) {
case 0 ..< 26: 'A'+byte;
case 26 ..< 52: 'a'+(byte-26);
case 52 ..< 62: '0'+(byte-52);
case 62: '+';
case 63: '/';
default: assert:bounds as $"byte={byte}";
};
}
}
</syntaxhighlight>
 
{{out}}
<pre>
base64=VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=, bytes=0x546F206572722069732068756D616E2C2062757420746F207265616C6C7920666F756C207468696E677320757020796F75206E656564206120636F6D70757465722E0A202020202D2D205061756C20522E204568726C696368
</pre>
 
=={{header|F_Sharp|F#}}==
Line 712 ⟶ 912:
 
=={{header|Frink}}==
This reproduces the Rosetta Code icon as specified in the [[Base64 encode data]] task. The Wiki software incorrectly wraps the long string which should be preserved in a single line.
<syntaxhighlight lang="frink">
<syntaxhighlight lang="frink">s = """iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAMAAAAoLQ9TAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAWJQTFRFAAAA/8IA/8EA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8IA/8UA/8UA/8QA/8IA/8IA/8IA/8IA/8IA/8IA/8MAdWVhiHJUiHJUc2Rj/8MA/8IA/8IA/8IA/8IA/8IA06QfjXZQjnZQjXVR3qwX/8IA/8IA/8IA/8IA/8IA/8kAjHVRjnZQjnZQjHVR/8gA/8IA/8IA/8IAjHVRjHVR/8gA/8IA/8IA1aYejXZQjnZQjXVR3qwX/8IA/8IA/8IA/8MAdGRjh3FVcmNj/8MA/8IA/8QA/8UA/8UA/8UA/8IA/8IA/8IA/8IA/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utputStream", ["favicon.ico"]]
f.write[base64DecodeToBytes[s]]
Line 917 ⟶ 1,147:
 
=={{header|Java}}==
<p>
Java offers the <code>Base64</code> class, which includes both the <code>Encoder</code> and <code>Decoder</code> classes.<br />
The implementation supports RFC 4648 and RFC 2045.
</p>
<p>
Similar to the encoding process, the usage is very simple, supply a <code>byte</code> array, and it will return an encoded <code>byte</code> array.
</p>
<p>
The class uses a static construct, so instead of instantiation via a constructor, you use one of the <kbd>static</kbd> methods.<br />
In this case we'll use the <code>Base64.getDecoder</code> method to acquire our instance.
</p>
<syntaxhighlight lang="java">
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;
</syntaxhighlight>
<syntaxhighlight lang="java">
void decodeToFile(String path, byte[] bytes) throws IOException {
try (FileOutputStream stream = new FileOutputStream(path)) {
byte[] decoded = Base64.getDecoder().decode(bytes);
stream.write(decoded, 0, decoded.length);
}
}
</syntaxhighlight>
The operation is successful, creating a 48 by 48-pixel, 32-bit color graphic, at 15,086 bytes.
<br /><br />
An alternate demonstration
{{trans|Kotlin}}
<syntaxhighlight lang="java">import java.nio.charset.StandardCharsets;
Line 1,029 ⟶ 1,286:
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich</pre>
 
=={{header|M2000 Interpreter}}==
 
=====Using a file and a Document object=====
Load.doc handle automatic UTF16LE UTF16BE UTF8 and ANSI. Also automatic find the type of new line (here is LF)
 
Tempname$ retuen a unique path+filename at temporary folder. The temporary file automatic deleted when the M2000 environment closed.
 
<syntaxhighlight lang="m2000 interpreter">
inp$={VG8gZXJyIGlzIGh1bWFuLCBidXQ
gdG8gcmVhbGx5IGZvdWwgdGhpbmdzI
HVwIHlvdSBuZWVkIGEgY29tcHV0ZXI
uCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
}
tmp$=tempname$
open tmp$ for wide output as #f
print #f, string$(inp$ as decode64);
close #f
document a$
load.doc a$, tmp$
report a$
clipboard a$
</syntaxhighlight>
 
=====Without File=====
If we know the encoding and type of line feed we can do this:
 
<syntaxhighlight lang="m2000 interpreter">
inp$={VG8gZXJyIGlzIGh1bWFuLCBidXQ
gdG8gcmVhbGx5IGZvdWwgdGhpbmdzI
HVwIHlvdSBuZWVkIGEgY29tcHV0ZXI
uCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
}
// decode inp$ (is 8-bit ANSI coded using 0xA for paragraph end/new line)
a$=string$(inp$ as Decode64)
locale 1033
// convert ansi to utf16le using Locale 1033
a$=chr$(a$) // convert to utf16LE
// expand LF to CRLF for M2000 console
a$=replace$(chr$(0xA), chr$(0XD)+chr$(0XA), a$)
// report handle cr and lf using proportional spacing for text and auto word wrapping
report a$
// also Print channel #-2 handle cr and lf (using non proportional spacing)
print #-2, a$
</syntaxhighlight>
 
{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 1,368 ⟶ 1,676:
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich
</pre>
 
=={{header|Racket}}==
<syntaxhighlight lang="racket">
#lang racket
(require net/url net/base64)
 
;; decode a string
(displayln (base64-decode #"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g="))
 
;; (encode and) decode the favicon
(displayln (base64-decode (base64-encode (call/input-url (string->url "https://rosettacode.org/favicon.ico")
get-pure-port port->bytes))))
</syntaxhighlight>
{{out}}
(string output only)
<pre>
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
Line 1,442 ⟶ 1,769:
Um9zZXR0YSBDb2RlIEJhc2U2NCBkZWNvZGUgZGF0YSB0YXNr
Rosetta Code Base64 decode data task
</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP-49C}}
« "" DUP DUP2 → s a b c d
« ""
1 s SIZE '''FOR''' j
0 3 '''FOR''' k
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
s j k + DUP SUB POS
'''NEXT'''
4 →LIST 1 - { a b c d } STO
a 4 * b 16 / IP + CHR +
'''IF''' c -1 ≠ '''THEN''' b 16 MOD 16 * c 4 / IP + CHR + '''END'''
'''IF''' d -1 ≠ '''THEN''' c 4 MOD 64 * d + CHR + '''END'''
4 '''STEP'''
» » '<span style="color:blue">B64→</span>' STO
 
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=" <span style="color:blue">B64→</span>
{{out}}
<pre>
1: "To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich"
</pre>
 
Line 1,580 ⟶ 1,930:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standard mlsml">val debase64 = fn input =>
let
 
Line 1,618 ⟶ 1,968:
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich </pre>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">
import Foundation
 
let input = """
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLSBQYXVsIFIuIEVocmxpY2g=
"""
 
if let decoded = Data(base64Encoded: input),
let str = String(data: decoded, encoding: .utf8) {
print( str )
}
</syntaxhighlight>
<pre>
To err is human, but to really foul things up you need a computer.
-- Paul R. Ehrlich
</pre>
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">package require tcl 8.6
Line 1,649 ⟶ 2,018:
-- Paul R. Ehrlich</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">import encoding.base64
 
fn main() {
Line 1,674 ⟶ 2,043:
{{libheader|Wren-str}}
From first principles using string manipulation. Quick enough here.
<syntaxhighlight lang="ecmascriptwren">import "io" for Stdout
import "./fmt" for Conv, Fmt
import "./str" for Str
 
var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Line 1,710 ⟶ 2,079:
-- Paul R. Ehrlich
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">func Decode(Ch);
int Ch;
[if Ch>=^A & Ch<=^Z then return Ch - ^A;
if Ch>=^a & Ch<=^z then return Ch - ^a + 26;
if Ch>=^0 & Ch<=^9 then return Ch - ^0 + 52;
if Ch = ^+ then return 62;
if Ch = ^/ then return 63;
return -1;
];
 
char Str;
int Count, N, Ch, Code, Acc;
[Str:=
"VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEg
Y29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo ";
loop [Count:= 0;
for N:= 1 to 4 do \read in four 6-bit chars
[repeat Ch:= Str(0); \strip out whitespace
Str:= Str+1;
if Ch = $A0 then quit; \terminating space
until Ch > $20;
Code:= Decode(Ch);
Acc:= Acc<<6;
if Code >= 0 then \don't count possible "="
[Acc:= Acc + Code;
Count:= Count+1;
];
];
ChOut(0, Acc>>16); \write out three 8-bit chars
if Count >= 3 then ChOut(0, Acc>>8);
if Count >= 4 then ChOut(0, Acc);
];
]</syntaxhighlight>
{{out}}
<pre>
To err is human, but to really foul things up you need a computer.
--Paul R.Ehrlich</pre>
 
=={{header|zkl}}==
1,150

edits