#! /bin/nawk -f # # Converts a stream of text numbers [0-255], into the uuencode # text stream that would correspond to a binary file of the same # numerical sequence. # # The output of this script, when piped through uudecode, will # produce a binary file containing the stream of bytes. # # for example: # # makeimage | tuencode.awk filename=image.bmp | uudecode # # will produce a binary file called image.bmp that contains # the byte stream (output as decimal numbers) from makeimage # # To put it another way: # # od -v -t d1 file1 | nawk '{for(i=2;i<=NF;++i)print $i}' |\ # tuencode | uudecode file2 # # is equivalent to: # # cp file1 file2 # ############################################################################### BEGIN{ if(file) filename = file if(! filename) filename = "binout.bin" print "begin 644", filename; } # # The uuencode formula used here is: # byte1 byte2 byte3 # |------| |------| |------| # ........ ........ ........ bitfield # |----||-----||-----||----| # char1 char2 char3 char4 # +32 +32 +32 +32 # || || || || # [!-`] [!-`] [!-`] [!-`] # # so: # char1 = byte1/4 + 32 # char2 = (byte1%4)*16 +byte2/16 +32 # char3 = (byte2%16)*4 +byte3/64 +32 # char4 = byte3%64 +32 # # # all values should be a number in range 0 - 255 { for(i=1;i<=NF;++i) { ++register; # make 24-bit bitfield three_bytes = three_bytes*256 +($i%256) if(register == 3) { # convert three bytes to four, six-bit characters char1 = int(three_bytes/262144)%64 +32; char2 = int(three_bytes/ 4096)%64 +32; char3 = int(three_bytes/ 64)%64 +32; char4 = three_bytes %64 +32; # wrap zero around to 64 if(char1 == 32) char1 = 96; if(char2 == 32) char2 = 96; if(char3 == 32) char3 = 96; if(char4 == 32) char4 = 96; # save up string we will, eventually print out printout = printout sprintf("%c%c%c%c", char1, char2, char3, char4); ++pcounter; # print when the line has filled up if(pcounter == 15) { pcounter = 0; # "M" = 3*15 +32 print "M" printout; printout = "" } register = three_bytes = 0; } } } END{ # remember bytes left over extra = register; # finish off last line if(register) { while(register != 3) { # zero-fill three_bytes *= 256; ++register; } # convert three bytes to four, six-bit characters char1 = int(three_bytes/262144)%64 +32; char2 = int(three_bytes/ 4096)%64 +32; char3 = int(three_bytes/ 64)%64 +32; char4 = three_bytes %64 +32; # wrap zero around to 64 if(char1 == 32) char1 = 96; if(char2 == 32) char2 = 96; if(char3 == 32) char3 = 96; if(char4 == 32) char4 = 96; # finish off printout string printout = printout sprintf("%c%c%c%c", char1, char2, char3, char4); } # last line contains length in bytes printf "%c%s\n", 3*pcounter+extra+32, printout; print "`" print "end" }