awk中计算CRC
有人在 awk/gawk 中实现了 POSIX 1003.2 兼容的 CRC 算法(由 cksum
输出)吗?我需要对内存字符串(不是整个文件)进行校验和,并且调用cksum
既慢又昂贵。
我的总体需求是生成一个 10 位或更少的数字校验和。其他哈希/CRC 函数也可以工作,有人有方便的东西吗?
Google 搜索和扫描 awk.info 没有发现任何有趣的结果。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
crc32 的 gawk/awk 实现(与 POSIX cksum 命令兼容)
这是 crc32 的 awk (gawk) 实现。
请注意,我们使用 T 和 X 作为查找表。
T
用于 crc32_table,X
用于查找数据到 int 值。如果您愿意,您可以在运行时计算 crc32_table,但是启动时有点慢,因此您需要在
小代码大小和慢启动
之间进行权衡,或者< code>crc32 计算速度合理,代码大小较大。我会推荐带有 crc_table 的版本,因为在完成速度比较后,代码大小的增加是合理的。如果您的 awk 版本不支持 and(),xor(),compl(),lshift(),rshift() 那么请不要忘记加载按位运算库。
然后计算 crc32
结果
所有 crc32 值都匹配,就像
cksum
命令的做法一样:-)gawk/awk implimentation of crc32 (compatible with the POSIX cksum command)
Here is a awk (gawk) implimentation of crc32.
Notice that we use T and X as our lookup table.
T
is used for the crc32_table, andX
is used for lookup of data to int value.If you wish, you can compute the crc32_table on runtime, however it was a bit slow on startup, so you would have a tradeoff between
small codesize and slow tartup
, orReasonable speed crc32 calculation and large code size
. I would recomend version with a crc_table, as the code size increace was well justifyable when compareson of speed was done.If you have a version of awk that does not support
and(),xor(),compl(),lshift(),rshift()
then do not forget to load the bitwise operation libs.Then calculate the crc32
Result
All crc32 values match, exactly like how
cksum
command does it :-)由于 cksum 使用大型表,因此在 AWK 中重新实现它可能不切实际。您也许可以在不使用表格的情况下即时计算它,但这可能比调用 cksum 慢。
参考文献:
cksum
源将其从 C 转换为 AWK 应该相当简单,然而,如果有人有这样的倾向。
顺便说一句,
gawk
有协进程: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 callingcksum
.References:
cksum
sourceTranslating it from C to AWK should be fairly trivial, however, if someone were so inclined.
By the way,
gawk
has coprocesses: