Mood: rushed
Now Playing: http://somafm.com/play/groovesalad
Topic: speed fuck
http://www.mediafire.com/?hngth40zzy5
###################################################################
# #
# Parameterized CRC algorithm implementation for (G)AWK #
# 20090202 themabus[at]inbox[dot]lv #
# #
# please verify thoroughly before actual use #
# #
###################################################################
function _ord_init( low, high, i, t)
#this and following two functions are taken from GAWK manual
{
low = sprintf("%c", 7) # BEL is ascii 7
if (low == "\a") { # regular ascii
low = 0
high = 127
} else if (sprintf("%c", 128 + 7) == "\a") {
# ascii, mark parity
low = 128
high = 255
} else { # ebcdic(!)
low = 0
high = 255
}
for (i = low; i <= high; i++) {
t = sprintf("%c", i)
_ord_[t] = i
}
}
function ord(str, c)
{
# only first character is of interest
c = substr(str, 1, 1)
return _ord_[c]
}
function chr(c)
{
# force c to be numeric by adding 0
return sprintf("%c", c + 0)
}
###################################################################
function filltable(poly, width, refin, bitmask, table, cnt1, cnt2, tmp1, mask)
{
bitmask = 0
for (cnt1 = 0; cnt1 < width; cnt1++) {
bitmask = xor(bitmask, lshift(1, cnt1))
}
if (refin == 0) {
mask = lshift(1, width-1)
for (cnt1 = 0; cnt1 < 256; cnt1++) {
tmp1 = lshift(cnt1, width-8)
for (cnt2 = 0; cnt2 < 8; cnt2++) {
and(tmp1, mask)==mask ? tmp1 = xor(lshift(tmp1, 1), poly) : tmp1 = lshift(tmp1, 1)
}
table[cnt1] = and(tmp1, bitmask)
# printf( "%08x\n", table[cnt1])
}
} else {
mask = 1
poly = reflect(poly, width, bitmask)
for (cnt1 = 0; cnt1 < 256; cnt1++) {
tmp1 = cnt1
for (cnt2 = 0; cnt2 < 8; cnt2++) {
and(tmp1, mask)==mask ? tmp1 = xor(rshift(tmp1, 1), poly) : tmp1 = rshift(tmp1, 1)
}
table[cnt1] = and(tmp1, bitmask)
# printf( "%08x\n", table[cnt1])
}
}
return bitmask
}
###################################################################
function reflect(input, width, bitmask, cnt1, tmp1, mask)
{
mask = lshift(1, width-1)
tmp1 = 0
for (cnt1 = 0; cnt1 < width; cnt1++) {
tmp1 = rshift(tmp1, 1)
if (and(input, mask)==mask) tmp1 = xor(tmp1, mask)
input = lshift(input, 1)
}
return and(tmp1, bitmask)
}
###################################################################
function calccrc(crc, width, refin, bitmask, table, message, type, len, cnt1, tmp1, ind)
#type - 0: text string e.g. "123456789"
# 1: string of hex values e.g. "313233343536373839"
{
cnt1 = 1
if (refin == 0) {
while (cnt1 <= len) {
type == 0 ? tmp1 = ord(substr(message, cnt1, 1)) : tmp1 = strtonum("0x" substr(message, cnt1, 2))
ind = xor(and(rshift(crc, width-8), 255), tmp1)
crc = xor(lshift(crc, 8), table[ind])
crc = and(crc, bitmask)
cnt1++
if (type == 1) cnt1++
}
} else {
while (cnt1 <= len) {
type == 0 ? tmp1 = ord(substr(message, cnt1, 1)) : tmp1 = strtonum("0x" substr(message, cnt1, 2))
ind = xor(and(crc, 255), tmp1)
crc = xor(rshift(crc, 8), table[ind])
crc = and(crc, bitmask)
cnt1++
if (type == 1) cnt1++
}
}
return crc
}
###################################################################
function selftest( poly, init, xorout, width, refin, refout, bitmask, table, message, crc)
{
bitmask = 0
split("", table)
print "CRC-32-IEEE 802.3"
print "cbf43926" # expected value
poly = strtonum("0x04c11db7")
init = strtonum("0xffffffff")
xorout = strtonum("0xffffffff")
width = 32
refin = 1 # 0 - false; 1 - true
refout = 1 # 0 - false; 1 - true
bitmask = filltable(poly, width, refin, bitmask, table)
message="123456789"
print ""
print message
crc = calccrc(init, width, refin, bitmask, table, message, 0, length(message))
if (refin!=refout) crc = reflect(crc, width, bitmask)
crc = xor(crc, xorout)
printf("%0" (int(width / 4) + ((width % 4)!=0)) "x\n", crc)
message="313233343536373839"
print ""
print message
crc = calccrc(init, width, refin, bitmask, table, message, 1, length(message))
if (refin!=refout) crc = reflect(crc, widht, bitmask)
crc = xor(crc, xorout)
printf("%0" (int(width / 4) + ((width % 4)!=0)) "x\n", crc)
print ""
print ""
print "CRC-16-CCITT"
print "29b1" # expected value
poly = strtonum("0x1021")
init = strtonum("0xffff")
xorout = strtonum("0x0000")
width = 16
refin = 0 # 0 - false; 1 - true
refout = 0 # 0 - false; 1 - true
bitmask = filltable(poly, width, refin, bitmask, table)
message="123456789"
print ""
print message
crc = calccrc(init, width, refin, bitmask, table, message, 0, length(message))
if (refin!=refout) crc = reflect(crc, width, bitmask)
crc = xor(crc, xorout)
printf("%0" (int(width / 4) + ((width % 4)!=0)) "x\n", crc)
message="313233343536373839"
print ""
print message
crc = calccrc(init, width, refin, bitmask, table, message, 1, length(message))
if (refin!=refout) crc = reflect(crc, width, bitmask)
crc = xor(crc, xorout)
printf("%0" (int(width / 4) + ((width % 4)!=0)) "x\n", crc)
}
###################################################################
BEGIN{
_ord_init()
# selftest()
bitmask = 0
split("", table)
# set up your model here or with command-line parameters '-v var=val'
poly = strtonum("0x04c11db7")
init = strtonum("0xffffffff")
xorout = strtonum("0xffffffff")
width = 32
refin = 1 # 0 - false; 1 - true
refout = 1 # 0 - false; 1 - true
bitmask = filltable(poly, width, refin, bitmask, table)
crc_file= init
}
###################################################################
{
# selftest()
crc_line = calccrc(init, width, refin, bitmask, table, $0, 0, length($0))
if (refin!=refout) crc_line = reflect(crc_line, width, bitmask)
crc_line = xor(crc_line, xorout)
printf("%0" (int(width / 4) + ((width % 4)!=0)) "x\n", crc_line)
crc_file = calccrc(crc_file, width, refin, bitmask, table, $0, 0, length($0))
# end console input with Ctrl + Z
}
###################################################################
END{
if (refin!=refout) crc_file = reflect(crc_file, width, bitmask)
crc_file = xor(crc_file, xorout)
printf("%0" (int(width / 4) + ((width % 4)!=0)) "x\n", crc_file)
}
Updated: 03/02/2009 12:45 EET
Post Comment | Permalink | Share This Post