Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
664 B
9
Indexable
Never
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>




uint32_t crc_32_bitoriented(unsigned char* buffer, int length) {
	int i, j;

	uint32_t poly = 0b10000001010000010100000110101011;
	uint32_t rev_poly = ~poly;

	uint32_t crcreg = 0x0;
	
	for (j = 0; j < length; ++j) {
		unsigned char b = buffer[j];
		for (i = 0; i < 8; ++i) {
			if ((crcreg ^ b) & 1) {
				crcreg = (crcreg >> 1) ^ poly;
			}
			else {
				crcreg >>= 1;
			}
			b >>= 1;
		}
	}
	return crcreg ^ 0x0;
}


int main(void){
    uint32_t crc = crc_32_bitoriented("123456789", 9);
    printf("%u\n", crc);
    
    return EXIT_SUCCESS;
}