From: Aidan Van Dyk Date: Tue, 9 Jan 2007 14:11:50 +0000 (+0000) Subject: AWK based encoders for base64 and quoted-printable. X-Git-Tag: HYLAFAX-4_3_2BETA1~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d3e4ce2b7be3cd4abb6ee9c8af7bc65a87bb71d5;p=thirdparty%2FHylaFAX.git AWK based encoders for base64 and quoted-printable. --- diff --git a/util/Makefile.in b/util/Makefile.in index d2cac1cf..11b11c53 100644 --- a/util/Makefile.in +++ b/util/Makefile.in @@ -187,6 +187,8 @@ install: installClient ${PUTSUPD} -m 755 -src notify-4.1.sh -O notify-4.1 ${PUTSUPD} -m 755 -src notify-4.2.sh -O notify-4.2 ${PUTSUPD} -m 444 -src ${SRCDIR}/notify.awk -O notify.awk + ${PUTSUPD} -m 444 -src ${SRCDIR}/qp-encode.awk -O qp-encode.awk + ${PUTSUPD} -m 444 -src ${SRCDIR}/b64-encode.awk -O b64-encode.awk ${PUTSUPD} -m 755 -src archive.sh -O archive ${PUTSUPD} -m 755 -src common-functions.sh -O common-functions ${PUTSUPD} -m 755 -src dictionary.sh -O dictionary diff --git a/util/b64-encode.awk b/util/b64-encode.awk new file mode 100755 index 00000000..ac7034b1 --- /dev/null +++ b/util/b64-encode.awk @@ -0,0 +1,153 @@ +#!/usr/bin/awk -f +# Copyright 2006 Aidan Van Dyk +# Copyright 2006 iFAX Solutions Inc. + +function asc(char, l_found) +{ + l_found = 0 + for (i=0; i < 256; i++) + { + if (sprintf("%c", i) == char) + l_found = i; + } + return l_found; +} + +function private_and (a, b, l_res, l_i) +{ + l_res = 0; + for (l_i = 0; l_i < 8; l_i++) + { + if (a%2 == 1 && b%2 == 1) + l_res = l_res/2 + 128; + else + l_res /= 2; + a=int(a/2); + b=int(b/2); + } + return l_res; +} + +function private_lshift(x, n) +{ + while (n > 0) + { + x *= 2; + n--; + } + return x; +} + +function private_rshift(x,n) +{ + while (n > 0) + { + x =int(x/2); + n--; + }; + return x; +} + + +function readbytes(n, m, s, line, str, __RS) { +# RS = "\x00"; + + m = n; + while (m > 0) { + s = length(__readbuffer); + if (s == 0) { + getline __readbuffer; + if (RT != "") + __readbuffer = __readbuffer RT; + + if ((s = length(__readbuffer)) == 0) + break; + + } + + if (s > 0) { + if (m > s) { + str = str __readbuffer; + m = m - s; + __readbuffer = ""; + } + else { + str = str substr(__readbuffer, 1, m); + __readbuffer = substr(__readbuffer, m+1); + break; + } + } + } + + return (str); +} + +function base64_write(b1, b2, b3, n, r1,r2,r3,r4) +{ + r1 = private_rshift(b1,2) + r2 = private_lshift(private_and(b1,3),4) + private_rshift(b2,4); + printf "%c", substr(BASE64,r1+1,1); + printf "%c", substr(BASE64,r2+1,1); + + if (n > 1) + { + r3 = private_lshift(private_and(b2,15),2) + private_rshift(b3,6); + printf "%c", substr(BASE64,r3+1,1); + } else + printf "=" + if (n > 2) + { + r4 = private_and(b3,63); + printf "%c", substr(BASE64,r4+1,1); + } else + printf "=" +} + +function base64_encode(input) +{ + while (length(input) > 0) + { + if (length(input) == 1) + { + byte1=asc(substr(input,1,1)); + byte2=0; + byte3=0; + base64_write(byte1,byte2,byte3, 1); + } + if (length(input) == 2) + { + byte1=asc(substr(input,1,1)); + byte2=asc(substr(input,2,1)); + byte3=0; + base64_write(byte1,byte2,byte3, 2); + } + if (length(input) >= 3) + { + byte1=asc(substr(input,1,1)); + byte2=asc(substr(input,2,1)); + byte3=asc(substr(input,3,1)); + base64_write(byte1,byte2,byte3, 3); + } + input=substr(input,4); + } +} + +function base64( __RS, data) +{ + __RS = RS; + RS = "\xF1\xF2\x00"; + data = readbytes(54); + while (length(data) > 0) + { + base64_encode(data); + printf "\n"; + data = readbytes(54); + } + RS = __RS; +} + +BEGIN { + BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + base64() + exit (0); +} diff --git a/util/qp-encode.awk b/util/qp-encode.awk new file mode 100755 index 00000000..7d6d5ad0 --- /dev/null +++ b/util/qp-encode.awk @@ -0,0 +1,57 @@ +#!/usr/bin/awk -f +# Copyright 2006 Patrice Fournier +# Copyright 2006 iFAX Solutions Inc. + +function qp_lc(c) +{ + # Space (32) and tab (9) must be encoded at the end of line. + if (c == " ") + return "=20"; + else if (c == "\t") + return "=09"; + else + return _qp[c]; +} + +function qp(c) +{ + return _qp[c]; +} + +BEGIN { + FS = "\n"; + + # Space (32) and tab (9) are only encoded at the end of line. + for (i = 0; i < 256; i++) { + c = sprintf("%c", i); + if (i == 9 || (i >= 32 && i <= 60) || (i >= 62 && i <= 126)) + _qp[c] = c; + else + _qp[c] = sprintf("=%02X", i); + } +} + +{ + l=0; + out=""; + len=length(); + if (len > 0) + { + for (i=1; i 75) + { + end = 75; + while (substr(out, end, 1) == "=" || substr(out, end-1, 1) == "=") + end--; + print substr(out, 1, end) "="; + out = substr(out, end+1); + } + } + print out; +} +