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

推荐订阅源

N
News | PayPal Newsroom
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
SecWiki News
SecWiki News
Know Your Adversary
Know Your Adversary
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
NISL@THU
NISL@THU
WordPress大学
WordPress大学
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threat Research - Cisco Blogs
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
Security Archives - TechRepublic
Security Archives - TechRepublic
有赞技术团队
有赞技术团队
L
LINUX DO - 热门话题
Hacker News: Ask HN
Hacker News: Ask HN
V
V2EX
G
GRAHAM CLULEY
TaoSecurity Blog
TaoSecurity Blog
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
F
Fortinet All Blogs
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
Recorded Future
Recorded Future
Latest news
Latest news
The Hacker News
The Hacker News
aimingoo的专栏
aimingoo的专栏
T
Troy Hunt's Blog
S
Schneier on Security
I
Intezer
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threatpost
爱范儿
爱范儿
The Register - Security
The Register - Security
S
SegmentFault 最新的问题
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
宝玉的分享
宝玉的分享
Recent Commits to openclaw:main
Recent Commits to openclaw:main
美团技术团队
B
Blog RSS Feed

博客园 - 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;
}