弗莱彻校验重拍的32位从8 [英] Remake of Fletcher checksum from 32bit to 8

查看:286
本文介绍了弗莱彻校验重拍的32位从8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是兑换权从原来的?

uint8_t fletcher8( uint8_t *data, uint8_t len )
{
    uint8_t sum1 = 0xff, sum2 = 0xff;

    while (len) {
            unsigned tlen = len > 360 ? 360 : len;
            len -= tlen;
            do {
                    sum1 += *data++;
                    sum2 += sum1;
                    tlen -= sizeof( uint8_t );
            } while (tlen);
            sum1 = (sum1 & 0xff) + (sum1 >> 4);
            sum2 = (sum2 & 0xff) + (sum2 >> 4);
    }
    /* Second reduction step to reduce sums to 4 bits */
    sum1 = (sum1 & 0xff) + (sum1 >> 4);
    sum2 = (sum2 & 0xff) + (sum2 >> 4);
    return sum2 << 4 | sum1;
    }

原文:

uint32_t fletcher32( uint16_t *data, size_t len )
{
    uint32_t sum1 = 0xffff, sum2 = 0xffff;

    while (len) {
            unsigned tlen = len > 360 ? 360 : len;
            len -= tlen;
            do {
                    sum1 += *data++;
                    sum2 += sum1;
                    tlen -= sizeof( uint16_t );
            } while (tlen);
            sum1 = (sum1 & 0xffff) + (sum1 >> 16);
            sum2 = (sum2 & 0xffff) + (sum2 >> 16);
    }
    /* Second reduction step to reduce sums to 16 bits */
    sum1 = (sum1 & 0xffff) + (sum1 >> 16);
    sum2 = (sum2 & 0xffff) + (sum2 >> 16);
    return sum2 << 16 | sum1;
    }

LEN将是8。

len will be 8.

数据将具有数据的输入[](1 - 8)

data will have an input of data[] (1 - 8)

其实我不知道用线怎么办:无符号TLEN = LEN> 360? 360:LEN;

Actually I don't know what to do with the line: unsigned tlen = len > 360 ? 360 : len;

也许 - > TLEN中int8_t LEN => 255? 255:LEN;

Maybe -> int8_t tlen = len > 255 ? 255 : len;

推荐答案

我想你需要口罩0xF的整个不0xFF的。 32位使用16位掩码,第32一半,您的8位使用一个8位掩码这不是8,4位一半是8半

I think you need 0xF masks throughout not 0xFF. The 32 bit uses a 16 bit mask, half of 32, your 8 bit uses an 8 bit mask which is not half of 8, 4 bits is half of 8.

uint8_t fletcher8( uint8_t *data, uint8_t len )
{
    uint8_t sum1 = 0xf, sum2 = 0xf;

    while (len) {
        unsigned tlen = len > 360 ? 360 : len;
        len -= tlen;
        do {
                sum1 += *data++;
                sum2 += sum1;
                tlen -= sizeof( uint8_t );
        } while (tlen);
        sum1 = (sum1 & 0xf) + (sum1 >> 4);
        sum2 = (sum2 & 0xf) + (sum2 >> 4);
    }
    /* Second reduction step to reduce sums to 4 bits */
    sum1 = (sum1 & 0xf) + (sum1 >> 4);
    sum2 = (sum2 & 0xf) + (sum2 >> 4);
    return sum2 << 4 | sum1;
}

否则,你要创建一个不同的校验,不弗莱彻。 SUM1例如正在执行,我认为所谓的那些补充校验和。基本上它是在你的情况事先和4位16位,校验那里进位的加回。在互联网协议使用使得它很容易修改数据包,而无需计算对整个数据包的校验和,可以针对存在的校验加减只有变化。

Otherwise you are creating a different checksum, not Fletcher. sum1 for example is performing what I think is called a ones complement checksum. Basically it is a 16 bit in prior and 4 bit in your case, checksum where the carry bits are added back in. used in internet protocols makes it very easy to modify a packet without having to compute the checksum on the whole packet, you can add and subtract only the changes against the existing checksum.

另外的还原步​​骤一个角落情况下,如果SUM1结果+ = *数据= 0x1F的使用四位方案,那么进位的加入为0x01 +为0x0F = 0×10,你需要添加进位回也如此循环0×01 + 0×00 = 0×01之外。否则环路外的总和加上零。根据您的架构你可能的东西执行得更快一样,如果(SUM1&安培; 0×10)= SUM1为0x01;比可能需要更多的指令缩骨添加的东西。

The additional reduction step is for a corner case, if the result of sum1 += *data = 0x1F using a four bit scheme, then the adding of the carry bit is 0x01 + 0x0F = 0x10, you need to add that carry bit back in as well so outside the loop 0x01 + 0x00 = 0x01. Otherwise that outside the loop sum is adding in zero. Depending on your architecture you might execute faster with something like if(sum1&0x10) sum1=0x01; than the shifty adding thing that may take more instructions.

它成为的东西不仅仅是一个与加进位校验更多的是最后一步,当二者合一。如果你比如只使用32位弗莱彻为16位校验,你已经浪费了你的时间,结果的低16位只是一个股票校验和进位的加回,没什么特别的。 SUM2是因为它是SUM1校验的积累的有趣的数字(SUM1是数据的积累,SUM2是校验的积累)。

Where it becomes something more than just a checksum with the carry bits added in is that last step when the two are combined. And if you for example only use the 32 bit fletcher as a 16 bit checksum, you have wasted your time, the lower 16 bits of the result are just a stock checksum with the carry bit added back in, nothing special. sum2 is the interesting number as it is an accumulation of sum1 checksums (sum1 is an accumulation of the data, sum2 is an accumulation of checksums).

这篇关于弗莱彻校验重拍的32位从8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆