💾 Archived View for thrig.me › blog › 2024 › 09 › 24 › hex3.pl captured on 2024-09-29 at 03:05:33.
-=-=-=-=-=-=-
#!/usr/bin/env perl # hex3 - hexagram (base64) encode input bytes # https://en.wikipedia.org/wiki/Base64 # https://www.unicode.org/charts/PDF/U4DC0.pdf use 5.36.0; binmode STDIN, ':raw'; binmode STDOUT, ':encoding(UTF-8)'; local $/ = \32768; sub b64 ($i) { my @t = ( 'A' .. 'Z', 'a' .. 'z', 0 .. 9, '+', '/' ); $t[$i]; } sub emit ( $n, $l = 3 ) { my $shift = 26; # where the sextets start in the 32-bit value for ( 0 .. $l ) { #print b64( ($n >> $shift) & 0b111111 ); print chr( 0x4DC0 + ( ( $n >> $shift ) & 0b111111 ) ); $shift -= 6; } # padding character for when the string is short #print '=' x ( 3 - $l ); print "\x{00B7}" x ( 3 - $l ); } my ( $length, $value, $shift ) = ( 0, 0, 24 ); while (readline) { while (/(.)/gs) { $value |= ord($1) << $shift; $shift -= 8; if ( ++$length == 3 ) { emit $value; ( $length, $value, $shift ) = ( 0, 0, 24 ); } } } emit $value, $length if $length; print "\n"; # POSIX compliance