Wed, 28 Dec 2011 09:49:19 +0100, Jean-Paul Iribarren did cat :
and if you want some fun (and have no bladi nor blada at hand) here's
some base64 code for playing with ;-)
---------
cat base64_in_awk.sh
### not yet complete nor compliant ;-)
###
### this is a simple base64 enc/dec in awk
### mostly made for fun but actually used in a few awk scripts I use
### in some other tools I wrote for fine grain analysis of
### texts, mainly emails, mainly spams to try and generate some
### synthetic regexps (or ideas of) regarding false positives or reinforcements.
### (and yes I know some tools exist in perl and I even use some of
### them which is another reason why I also do it in awk ;-)
### the wrapping is set at 72 like the 'mimencode' usage
### to avoid wrapping set ORS to nil.
###
Aargh(){
r=$1
shift
printf "\n%s\nThats all, folks...\n\n" "${@}"
exit $r
}
[ $# -gt 1 ] || Aargh 1 "something is direly in the unseen world"
### most used way by default, anyway as 2 parms are mandatory this is only belting the suspenders
WOT=${1:-d}
shift
### gawk -v wot=$WOT -v ORS='' '
### gawk -v wot=$WOT -v ORS='µ' '
gawk -v wot=$WOT '
function _ba64dec(_b64str,_BASE64,_wrap,_res,_ba,_by,_len,_i,_j)
{
_len=split(_b64str,_ba,"")
while (_i<=_len){
if( 0==(++_wrap) %72){++_i;continue}
### get the 4 _bytes values and find their position in BASE64 base
for(_j=1;_j<5;_j++){
_by[_j] = index(_BASE64, _ba[++_i])
_by[_j]--
}
### Reconstruct ASCII string
_res = _res sprintf( "%c", lshift(and(_by[1], 63), 2) + rshift(and(_by[2], 48), 4) )
_res = _res sprintf( "%c", lshift(and(_by[2], 15), 4) + rshift(and(_by[3], 60), 2) )
_res = _res sprintf( "%c", lshift(and(_by[3], 3), 6) + _by[4] )
gsub(/[\x00\xff\xbf\x0f]/,"",_res)
}
return _res
}
function _ord(_char, i)
{
while(++i<256) if (sprintf("%c", i) == _char) return i
}
function _ba64enc(_b64str,_BASE64,_wrap, _ba1,_ba2,_ba3,_ba4,_by1,_by2,_by3,_by4, _res)
{
while (length(_b64str) > 0){
### find the values
_by1 = _ord(substr(_b64str, 1, 1))
if (length(_b64str) == 1){
_by2 = 0
_by3 = 0
}
if (length(_b64str) == 2){
_by2 = _ord(substr(_b64str, 2, 1))
_by3 = 0
}
if (length(_b64str) >= 3){
_by2 = _ord(substr(_b64str, 2, 1))
_by3 = _ord(substr(_b64str, 3, 1))
}
### transform to BASE64 values
_ba1 = rshift(_by1, 2)
_ba2 = lshift(and(_by1, 3), 4) + rshift(and(_by2, 240), 4)
_ba3 = lshift(and(_by2, 15), 2) + rshift(and(_by3, 192), 6)
_ba4 = and(_by3, 63)
### transmute values to BASE64 string
_res = _res substr(_BASE64, _ba1 + 1, 1)
_res = _res substr(_BASE64, _ba2 + 1, 1)
if (length(_b64str) == 1){
_res = _res "=="
_b64str = ""
}
if (length(_b64str) == 2){
_res = _res substr(_BASE64, _ba3 + 1, 1)
_res = _res "="
_b64str = ""
}
if (length(_b64str) >= 3){
_res = _res substr(_BASE64, _ba3 + 1, 1)
_res = _res substr(_BASE64, _ba4 + 1, 1)
_b64str = substr(_b64str, 4)
}
if( 0==(++_wrap) %18) _res=_res ORS
}
return _res
}
BEGIN{_w=0}
{
### Base64 for filenames given as alternate example, see RFC4648
### _BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
_BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
print wot=="d"?_ba64dec($0,_BASE64,_w):_ba64enc($0,_BASE64,_w)
}
' ${@}
---------