Search code examples
algorithmawkchecksumcrc

Calculating CRC in awk


Has anyone implemented the POSIX 1003.2 compiliant CRC algorithm (as output by cksum) in awk/gawk? I'm needing to do a checksum on an in memory string (not the whole file) and shelling out to call cksum is slow and expensive.

My overall need is to generate a numerical checksum that fits within 10 digits or less. Other hash/CRC functions could work too, anyone have any thing handy?

A Google search and a scan of awk.info turned up nothing interesting.


EDIT:
I ended up using the external cksum command, but caching the results into an awk associative array. Performance was good enough and I didn't need to reinvent the wheel.


Solution

  • Since cksum uses a large table, it's probably impractical to re-implement it in AWK. You might be able to calculate it on the fly without using a table, but that's likely to be slower than calling cksum.

    References:

    Translating it from C to AWK should be fairly trivial, however, if someone were so inclined.

    By the way, gawk has coprocesses:

    gawk 'BEGIN {
        cmd="cksum"
        print "hello" |& cmd
        close(cmd, "to")
        while (cmd |& getline a > 0)
            print a
        close(cmd)
        }'