此 CRC32 方法的大端兼容版本会是什么样子?
我正在开发一个项目,需要对正在传输的数据进行 CRC32 检查。我想让我的代码不仅兼容 Intel 架构(“Little Endian”),而且还兼容 Solaris 架构(“Big Endian”)。我发现这个“CCRC32”对于两台小端机器来说工作得很好,但完全失败了任何跨平台测试:
代码:
CCRC32.h & CCRC32.cpp (摘自维基百科的“外部链接”)
http://en.wikipedia.org/wiki/Cyclic_redundancy_check
这是代码的方法示例:
void CCRC32::PartialCRC(unsigned long *ulCRC, const unsigned char *sData, unsigned long ulDataLength) {
while(ulDataLength--) {
//If your compiler complains about the following line, try changing each
//occurrence of *ulCRC with "((unsigned long)*ulCRC)" or "*(unsigned long *)ulCRC".
*(unsigned long *)ulCRC =
((*(unsigned long *)ulCRC) >> 8)
^ this->ulTable[((*(unsigned long *)ulCRC) & 0xFF) ^ *sData++];
}
unsigned long CCRC32::FullCRC(const unsigned char *sData, unsigned long ulDataLength) {
unsigned long ulCRC = 0xffffffff; //Initilaize the CRC.
this->PartialCRC(&ulCRC, sData, ulDataLength);
return(ulCRC ^ 0xffffffff); //Finalize the CRC and return.
}
所以我的问题是:你们中的大端大师知道如何调整上述方法以与大端机器一起使用吗?或者有人知道现有的源代码吗那可以实现我的目标吗?到目前为止我的搜索一直没有成功。
谢谢你的宝贵时间,
詹姆斯
I'm working on a project that requires a CRC32 check to be done on data that is being transmitted. I would like to make my code compatible for not only Intel architecture ("Little Endian"), but for Solaris architecture ("Big Endian") as well. I've found this "CCRC32" that works spiffy for two little endian machines, but utterly fails any cross platform tests:
Code:
CCRC32.h & CCRC32.cpp
(taken off of wikipedia's "external links")
http://en.wikipedia.org/wiki/Cyclic_redundancy_check
Here is a method sample of the code:
void CCRC32::PartialCRC(unsigned long *ulCRC, const unsigned char *sData, unsigned long ulDataLength) {
while(ulDataLength--) {
//If your compiler complains about the following line, try changing each
//occurrence of *ulCRC with "((unsigned long)*ulCRC)" or "*(unsigned long *)ulCRC".
*(unsigned long *)ulCRC =
((*(unsigned long *)ulCRC) >> 8)
^ this->ulTable[((*(unsigned long *)ulCRC) & 0xFF) ^ *sData++];
}
unsigned long CCRC32::FullCRC(const unsigned char *sData, unsigned long ulDataLength) {
unsigned long ulCRC = 0xffffffff; //Initilaize the CRC.
this->PartialCRC(&ulCRC, sData, ulDataLength);
return(ulCRC ^ 0xffffffff); //Finalize the CRC and return.
}
So my question is this: Do you any of you big endian gurus know how to tweak the above methods to work with big endian machines, or does anyone know of an existing piece of source code that could accomplish my goal? I've been unsuccessful in my search thus far.
Thank you for your time,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定它是否有帮助,但是这段C代码有大小端版本。
Not sure if it helps, but this piece of C code has big and small endian versions.