惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Mojies

Prompt 相关 T-Lite 简介 一个 python 拆解文本文件的工具 UNIX 环境编程 Note ( UPDATING ) 航模.fs.ia6b 接收机记录 线程同步 STM32 Note Linux FrameBuffer note VIM Note Android 开发笔记(更新中....) Effective C++ (暂停更新) Linux 命令行对比二进制文件脚本 Linux Note (Updateing) 关于 HID 你可能需要知道的几件事 (更新中...) 自制树莓派 3D 模型,附 STL 文件 《重构改善既有代码的设计》Tips 翻译: buildroot 用户手册 (更新中...) Some view of engineers 地球地址
CRC 计算 C 语言例子
Mojies · 2024-09-17 · via 博客园 - Mojies
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

void uint32_2_bin(char *buf, uint32_t val ){
    uint32_t b = 0x80000000;
    while(b>0){
        if( val&b ) *buf = '1';
        else *buf = '0';
        b>>=1;
        buf++;
    }
    *buf = '\0';
}

/**
 * param:
 *      data -- 需要计算的数组
 *      data_len -- 计算数组的长度
 *      div_e -- mod2 的除数
 *          如果公式为 X^8 + X^5 + X^4 + X
 *          那么 div_e == 0x131
 *      init -- 初始值,如果没有就填写 0x00
 *
 * 注意该函数目前是按照 crc8 的方式编写,
 * 通过变更返回值类型和 div_e 等变量可以变更成 crc16 crc32
 */
uint8_t mod2_division_8bits( uint8_t data[], uint32_t data_len, uint32_t div_e, uint8_t init ){
    uint8_t *end = data + data_len;
    uint32_t a = *data^init;

    uint8_t div_bits;
    uint32_t d;

    uint8_t b = 8;
    uint8_t c = 0;
    uint8_t sub;

#if 1
    {
        int i;
        for(i=0; i<32; i++)
            if( div_e & (0x01<<i) )
                div_bits = i+1;
        d = 0x01<<(div_bits-1);
    }
#else
    // fomule = X^8 + X^5 + X^4 + X;
    d = 0x100;
    div_bits = 9;
#endif

    while(1){
        if( b < div_bits ){
            if( data != end ){
                if( c > 0 ){
                    uint8_t bit = sub&c?1:0;
                    a = (a<<1)|bit;
                    c >>= 1;
                    if( a&d ) b++;
                }else{
                    data++;
                    sub = *data;
                    c = 0x80;
                    continue;
                }
            }else{
                if( c == 0 )
                    break;
                a <<= 1;
                c >>= 1;
                if( a&d ) b++;
            }
        }else{
            if( a&d )
                a ^= div_e;

            char s[64];
            uint32_2_bin( s, a );
            printf( "%s %2x\n", s, a );
            b--;
        }
    }

    return a&0xFF;
}

int main(void){

    uint8_t a[] = { 28, 184, 245, 165, 156, 208 };
    uint16_t b = 0x131;

    mod2_division_8bits( a, 6, b, 0xFF );

    return 0;
}