2004-04-05-0846Z


Just got an email from my customer allowing me to share the code you see in the previous entry; that's why it wasn't uploaded till just a few minutes ago, as it would not have been kosher to give away what someone else paid for without their permission. Thanks, JT! My handful of faithful readers appreciates it!

I forgot to post this base64 decoder. While I was waiting to find out which Perl modules weren't on the customer's server (he's in England, so the time gap meant there was about 8 hours a day with no communication), I was attempting to guess which ones I'd have to code myself; a basic rule of Perl programming is that at least one critical module won't be on the customer's server, and if they don't own it (few do), it ranges from difficult to impossible to get the owner to install it. On Linux it wouldn't matter too much, if you have enough space you can install it without any special privileges beneath your own home directory. I don't know how to do that with Windows, if it's even possible.

So anyway, I guessed the base64 decoder might not be on the system. I was wrong, it was there, so I didn't need this:

jcomeau@notebook ~/rentacoder/perlpop
$ cat base64decode
#!/usr/pkg/bin/perl
$base64 = join('', ('A'..'Z', 'a'..'z', '0'..'9', '+', '/', '='));
foreach $line (<>) {
 $line =~ tr/$base64//c;
 while($line =~ /(\S{4})/gc) {
  $code = $1;
  $size = 3;
  $size -= $code =~ tr/=/=/;
  $bits = 0;
  foreach (split('', $code)) {
   $bits = ($bits << 6);
   if (($index = index($base64, $_)) < 64) {
    $bits += $index;
   }
  }
  print substr(pack('N', $bits), 1, $size);
 }
}

Don't know how bug-free it might be; try it out if you like and let me know if it fails. It accepts input from stdin or from files, and outputs to stdout, so you can use it with pipes. Water pipes, corncob, briar, it doesn't matter. Unlike many decoders, it doesn't complain about whatever you feed it; if a line isn't a multiple of 4 bytes, it quietly discards the remainder.

Back to blog or home page

last updated 2013-01-10 20:16:19. served from tektonic.jcomeau.com