Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function(d,a,e,b,c,f){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87)))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c}
- function(d,a,e,b,c,f){c="";for(a=e=b=0;a<4*d.length/3;f=b>>2*(++a&3)&63,c+=String.fromCharCode(f+71-(f<26?6:f<52?0:f<62?75:f^63?90:87))+(75==(a-1)%76?"rn":""))a&3^3&&(b=b<<8^d[e++]);for(;a++&3;)c+="=";return c}
- $/=$;print map{$l=y///c/2%3;[A..Z,a..z,0..9,"+","/"]->[oct"0b".substr$_.0 x4,0,6],$l?"="x(3-$l):""}unpack("B*",<>)=~/.{1,6}/g
- my @x = ('A'..'Z','a'..'z',0..9,'+','/');
- my $in = join '', <>;
- my $bits = unpack 'B*', $in;
- my @six_bit_groups = $bits =~ /.{1,6}/g;
- for my $sixbits (@six_bit_groups) {
- next unless defined $sixbits;
- $l=length($sixbits)/2%3;
- my $zero_padded = $sixbits . ( "0" x 4 );
- my $padded_bits = substr( $zero_padded, 0, 6 );
- my $six_bit_int = oct "0b" . $padded_bits;
- print $x[$six_bit_int];
- print "=" x (3 - $l) if $l;
- }
- sub b{$f=(3-($#_+1)%3)%3;$_=unpack'B*',pack'C*',@_;@r=map{(A..Z,a..z,0..9,'+','/')[oct"0b$_"]}/.{1,6}/g;$"='';join"rn",("@r".'='x$f)=~/.{1,76}/g}
- Y2Fmw6kA
- sub b {
- # input array: @_
- # number of elements: $#_ + 1 ($#_ is zero-based index of last element in
- $fillbytes = (3 - ($#_ + 1) % 3) % 3;
- # calculate the number for the needed fill bytes
- print "fillbytes: $fillbytesn";
- $byte_string = pack 'C*', @_;
- # the numbers are packed as octets to a binary string
- # (binary string not printed)
- $bit_string = unpack 'B*', $byte_string;
- # the binary string is converted to its bit representation, a string wit
- print "bit string: "$bit_string"n";
- @six_bit_strings = $bit_string =~ /.{1,6}/g;
- # group in blocks of 6 bit
- print "6-bit strings: [@six_bit_strings]n";
- @index_positions = map { oct"0b$_" } @six_bit_strings;
- # convert bit string to number
- print "index positions: [@index_positions]n";
- @alphabet = (A..Z,a..z,0..9,'+','/');
- # the alphabet for base64
- @output_chars = map { $alphabet[$_] } @index_positions;
- # output characters with wrong last characters that entirely derived fro
- print "output chars: [@output_chars]n";
- local $" = ''; #"
- $output_string = "@output_chars";
- # array to string without space between elements ($")
- print "output string: "$output_string"n";
- $result = $output_string .= '=' x $fillbytes;
- # add padding with trailing '=' characters
- print "result: "$result"n";
- $formatted_result = join "rn", $result =~ /.{1,76}/g;
- # maximum line length is 76 and line ends are "rn" according to RFC 2045
- print "formatted result:n$formatted_resultn";
- return $formatted_result;
- }
- fillbytes: 1
- bit string: "0110001101100001011001101100001110101001"
- 6-bit strings: [011000 110110 000101 100110 110000 111010 1001]
- index positions: [24 54 5 38 48 58 9]
- output chars: [Y 2 F m w 6 J]
- output string: "Y2Fmw6J"
- result: "Y2Fmw6J="
- formatted result:
- Y2Fmw6J=
- TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
- IHNpbmd1bGFyIHBhc3Npb24gZnJvbQpvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
- dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodAppbiB0aGUgY29udGlu
- dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
- ZSBzaG9ydAp2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZSO=
- YW55IGNhcm5hbCBwbGVhc3VyZSO=
- YW55IGNhcm5hbCBwbGVhc3VyZB==
- YW55IGNhcm5hbCBwbGVhc3Vy
- YW55IGNhcm5hbCBwbGVhc3F=
- YW55IGNhcm5hbCBwbGVhcD==
- cGxlYXN1cmUu
- bGVhc3VyZSO=
- ZWFzdXJlLC==
- YXN1cmUu
- c3VyZSO=
- def F(s):
- R=range;A=R(65,91)+R(97,123)+R(48,58)+[43,47];n=len(s);s+=[0,0];r='';i=0
- while i<n:
- if i%57<1:r+='rn'
- for j in R(4):r+=chr(A[s[i]*65536+s[i+1]*256+s[i+2]>>18-6*j&63])
- i+=3
- k=-n%3
- if k:r=r[:-k]+'='*k
- return r[2:]
- ~.,~)3%:P[0]*+[4]3*+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>76/"r
- ":n*
- ~.,~)3%:P[0]*+[4]3*+256base 64base{'+/''A[a{:0'{,^}/=}/{;}P*'='P*]4>{13]n+}/
- $ echo '[99 97 102 195 169]' | golfscript base64.gs | cat -A
- Y2Fmw6k=^M$
- $ echo [ {0..142} ] | golfscript base64.gs | cat -A
- AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4^M$
- OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3Bx^M$
- cnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY4=^M$
- $ echo '[99 97 102 195 169]' | golfscript base64-sneaky.gs | cat -A
- Y^M$
- 2^M$
- F^M$
- m^M$
- w^M$
- 6^M$
- k^M$
- =^M$
- $
- ~ # Interpret the input string.
- .,~)3%:P # Calculate the number of bytes missing to yield a multiple of 3 and save in “P”.
- [0]*+ # Append that many zero bytes to the input array.
- [4]3*+ # Prepend 3 bytes to the input array to avoid issues with leading zeros.
- 256base # Convert the input array into an integer.
- 64base # Convert that integer to base 64.
- { # For each digit:
- '+/' # Push '+/'.
- 'A[a{:0' # Push 'A[a{:0'.
- { # For each byte in 'A[a{:0':
- , # Push the array of all bytes up to that byte.
- ^ # Take the symmetric difference with the array below it.
- }/ # Result: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
- = # Retrieve the character corresponding to the digit.
- }/ #
- {;}P*'='P* # Replace the last “P” characters with a string containing that many “=” chars.
- ] # Collect all bytes on the stack into an array.
- 4> # Remove the first four, which correspond to the 3 prepended bytes.
- 76/ # Collect all bytes on the stack into an array and split into 76-byte chunks.
- "rn":n* # Join the chunks with separator CRLF and save CRLF as the new line terminator.
- 66 B8 0D 0A 66 AB 6A 14 5A 4A 74 F4 AD 4E 45 0F C8 6A 04 59 C1 C0 06 24 3F 3C 3E 72 05 C0
- E0 02 2C 0E 2C 04 3C 30 7D 08 04 45 3C 5A 76 02 04 06 AA 4D E0 E0 75 D3 B0 3D F3 AA C3
- b64_newline:
- mov ax, 0a0dh
- stosw
- b64encode:
- push (76 shr 2) + 1
- pop edx
- b64_outer:
- dec edx
- je b64_newline
- lodsd
- dec esi
- inc ebp
- bswap eax
- push 4
- pop ecx
- b64_inner:
- rol eax, 6
- and al, 3fh
- cmp al, 3eh
- jb b64_testchar
- shl al, 2 ;'+' and '/' differ by only 1 bit
- sub al, ((3eh shl 2) + 'A' - '+') and 0ffh
- b64_testchar:
- sub al, 4
- cmp al, '0'
- jnl b64_store ;l not b because '/' is still < 0 here
- add al, 'A' + 4
- cmp al, 'Z'
- jbe b64_store
- add al, 'a' - 'Z' - 1
- b64_store:
- stosb
- dec ebp
- loopne b64_inner
- jne b64_outer
- mov al, '='
- rep stosb
- ret
Add Comment
Please, Sign In to add comment