Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

To calculate the CRC32 hash of a string without using built-in functions, we can implement the CRC32 algorithm manually in C. The CRC32 algorithm involves polynomial division where you treat the input data as a large polynomial and divide it by a fixed polynomial (the CRC polynomial), keeping only the remainder. Here is a basic implementation for the CRC32 calculation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_for_byte(uint32_t r) {
    for(int j = 0; j < 8; ++j) {
        r = (r & 1? 0: (uint32_t)0xEDB88320L) ^ r >> 1;
    }
    return r ^ (uint32_t)0xFF000000L;
}

void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
    static uint32_t table[0x100];
    if(!*table) {
        for(size_t i = 0; i < 0x100; ++i) {
            table[i] = crc32_for_byte(i);
        }
    }
    for(size_t i = 0; i < n_bytes; ++i) {
        *crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
    }
}

int main() {
    const char *str = "correct horse battery staple";
    uint32_t crc = 0;
    crc32(str, strlen(str), &crc);
    printf("CRC32: %08X\n", crc ^ 0xFFFFFFFF);
    return 0;
}
This program defines a function `crc32_for_byte` that generates a lookup table value for a single byte. The main `crc32` function uses this lookup table to compute the CRC32 of an arbitrary block of data. The initial and final XOR operations are part of the CRC32 algorithm to invert the bits at the start and the end, ensuring that the algorithm is sensitive to leading and trailing zeros in the data. When you run this program, it calculates the CRC32 hash of the string "correct horse battery staple" and prints it. Note that this is a basic and straightforward implementation. In practice, there are optimizations and variations you can apply to the CRC32 calculation for performance improvements or to meet specific requirements of different CRC32 standards.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_for_byte(uint32_t r) {
    for(int j = 0; j < 8; ++j) {
        r = (r & 1? 0: (uint32_t)0xEDB88320L) ^ r >> 1;
    }
    return r ^ (uint32_t)0xFF000000L;
}

void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
    static uint32_t table[0x100];
    if(!*table) {
        for(size_t i = 0; i < 0x100; ++i) {
            table[i] = crc32_for_byte(i);
        }
    }
    for(size_t i = 0; i < n_bytes; ++i) {
        *crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
    }
}

int main() {
    const char *str = "correct horse battery staple";
    uint32_t crc = 0;
    crc32(str, strlen(str), &crc);
    printf("CRC32: %08X\n", crc ^ 0xFFFFFFFF);
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_for_byte(uint32_t r) {
    for(int j = 0; j < 8; ++j) {
        r = (r & 1? 0: (uint32_t)0xEDB88320L) ^ r >> 1;
    }
    return r ^ (uint32_t)0xFF000000L;
}

void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
    static uint32_t table[0x100];
    if(!*table) {
        for(size_t i = 0; i < 0x100; ++i) {
            table[i] = crc32_for_byte(i);
        }
    }
    for(size_t i = 0; i < n_bytes; ++i) {
        *crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
    }
}

int main() {
    const char *str = "correct horse battery staple";
    uint32_t crc = 0;
    crc32(str, strlen(str), &crc);
    printf("CRC32: %08X\n", crc ^ 0xFFFFFFFF);
    return 0;
}
And got the output: CRC32: 348191EF main.c: In function 'main': main.c:27:16: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration] 27 | crc32(str, strlen(str), &crc); | ^~~~~~ main.c:4:1: note: include '<string.h>' or provide a declaration of 'strlen' 3 | #include <stdint.h> +++ |+#include <string.h> 4 | main.c:27:16: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch] 27 | crc32(str, strlen(str), &crc); | ^~~~~~ main.c:27:16: note: include '<string.h>' or provide a declaration of 'strlen'

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: False

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Calculating the CRC32 hash of a string involves implementing or using a CRC32 algorithm. CRC32 is a type of checksum algorithm that uses polynomial division to calculate the checksum value of a data stream, which in this case, is the string "correct horse battery staple". Below is a simple C program that calculates the CRC32 hash of the given string without using any built-in CRC32 functions. This program uses a common CRC32 polynomial represented as `0xEDB88320`.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_for_byte(uint32_t r) {
    for (int j = 0; j < 8; ++j) {
        r = (r & 1 ? 0 : (uint32_t)0xEDB88320L) ^ r >> 1;
    }
    return r ^ (uint32_t)0xFF000000L;
}

void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
    static uint32_t table[0x100];
    if (!*table) {
        for (size_t i = 0; i < 0x100; ++i) {
            table[i] = crc32_for_byte(i);
        }
    }
    for (size_t i = 0; i < n_bytes; ++i) {
        *crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
    }
}

int main() {
    const char *s = "correct horse battery staple";
    uint32_t crc = 0;
    crc32(s, strlen(s), &crc);
    printf("CRC32: %08X\n", crc ^ 0xFFFFFFFF);
    return 0;
}
### Explanation: - The `crc32_for_byte` function calculates the CRC32 value for a single byte. It's used to generate a lookup table that contains the CRC32 result for every possible byte (0-255). This table is used to speed up the calculation by avoiding the need to calculate the CRC32 byte by byte every time. - The `crc32` function calculates the CRC32 checksum for a given block of data. It initializes a lookup table (if not already initialized) and then processes each byte of the input data, updating the CRC32 checksum as it goes. - In the `main` function, the string "correct horse battery staple" is passed to the `crc32` function, which calculates its CRC32 checksum. The final checksum is XORed with `0xFFFFFFFF` before being printed to stdout. This XOR operation is a common step in CRC32 algorithms to invert the bits of the final checksum, providing a form of error detection capability. Compile and run this program to get the CRC32 hash of the specified string. Remember that the output is in hexadecimal format.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_for_byte(uint32_t r) {
    for (int j = 0; j < 8; ++j) {
        r = (r & 1 ? 0 : (uint32_t)0xEDB88320L) ^ r >> 1;
    }
    return r ^ (uint32_t)0xFF000000L;
}

void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
    static uint32_t table[0x100];
    if (!*table) {
        for (size_t i = 0; i < 0x100; ++i) {
            table[i] = crc32_for_byte(i);
        }
    }
    for (size_t i = 0; i < n_bytes; ++i) {
        *crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
    }
}

int main() {
    const char *s = "correct horse battery staple";
    uint32_t crc = 0;
    crc32(s, strlen(s), &crc);
    printf("CRC32: %08X\n", crc ^ 0xFFFFFFFF);
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <stdint.h>

uint32_t crc32_for_byte(uint32_t r) {
    for (int j = 0; j < 8; ++j) {
        r = (r & 1 ? 0 : (uint32_t)0xEDB88320L) ^ r >> 1;
    }
    return r ^ (uint32_t)0xFF000000L;
}

void crc32(const void *data, size_t n_bytes, uint32_t* crc) {
    static uint32_t table[0x100];
    if (!*table) {
        for (size_t i = 0; i < 0x100; ++i) {
            table[i] = crc32_for_byte(i);
        }
    }
    for (size_t i = 0; i < n_bytes; ++i) {
        *crc = table[(uint8_t)*crc ^ ((uint8_t*)data)[i]] ^ *crc >> 8;
    }
}

int main() {
    const char *s = "correct horse battery staple";
    uint32_t crc = 0;
    crc32(s, strlen(s), &crc);
    printf("CRC32: %08X\n", crc ^ 0xFFFFFFFF);
    return 0;
}
And got the output: CRC32: 348191EF main.c: In function 'main': main.c:27:14: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration] 27 | crc32(s, strlen(s), &crc); | ^~~~~~ main.c:4:1: note: include '<string.h>' or provide a declaration of 'strlen' 3 | #include <stdint.h> +++ |+#include <string.h> 4 | main.c:27:14: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch] 27 | crc32(s, strlen(s), &crc); | ^~~~~~ main.c:27:14: note: include '<string.h>' or provide a declaration of 'strlen'

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: False

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

Calculating the CRC32 hash of a string in C without using any built-in functions requires implementing the CRC32 algorithm from scratch. Below is a simple C program that demonstrates this by calculating the CRC32 hash of the string "correct horse battery staple".

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdint.h>

uint32_t crc_table[256];

// Function to initialize the CRC table. This should be called before calculating any CRC values.
void initialize_crc_table(void) {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ 0xEDB88320;
            } else {
                crc = crc >> 1;
            }
        }
        crc_table[i] = crc;
    }
}

// Function to calculate the CRC32 of a given string.
uint32_t crc32(const char *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        crc = (crc >> 8) ^ crc_table[(crc ^ byte) & 0xFF];
    }
    return ~crc;
}

int main() {
    const char *input = "correct horse battery staple";
    initialize_crc_table(); // Initialize the CRC table first
    uint32_t crc_result = crc32(input, strlen(input));
    printf("CRC32: %08X\n", crc_result);
    return 0;
}
This program first initializes a table (`crc_table`) used for fast CRC calculation. The `initialize_crc_table` function fills this table based on the polynomial `0xEDB88320`, which is standard for CRC32. The `crc32` function calculates the CRC32 hash for a given string by iterating over each byte, updating the CRC value using the precomputed table, and finally inverting its bits. Compile this program using a C compiler, for example, `gcc`:
1
gcc -o crc32_program crc32_program.c
Run the compiled program:
1
./crc32_program
This should output the CRC32 value for the string "correct horse battery staple". Note that the CRC32 value is printed in hexadecimal.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdint.h>
#include <string.h>

uint32_t crc_table[256];

void initialize_crc_table(void) {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ 0xEDB88320;
            } else {
                crc = crc >> 1;
            }
        }
        crc_table[i] = crc;
    }
}

uint32_t crc32(const char *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        crc = (crc >> 8) ^ crc_table[(crc ^ byte) & 0xFF];
    }
    return ~crc;
}

int main() {
    const char *input = "correct horse battery staple";
    initialize_crc_table();
    uint32_t crc_result = crc32(input, strlen(input));
    printf("CRC32: %08X\n", crc_result);
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdint.h>
#include <string.h>

uint32_t crc_table[256];

void initialize_crc_table(void) {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ 0xEDB88320;
            } else {
                crc = crc >> 1;
            }
        }
        crc_table[i] = crc;
    }
}

uint32_t crc32(const char *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        crc = (crc >> 8) ^ crc_table[(crc ^ byte) & 0xFF];
    }
    return ~crc;
}

int main() {
    const char *input = "correct horse battery staple";
    initialize_crc_table();
    uint32_t crc_result = crc32(input, strlen(input));
    printf("CRC32: %08X\n", crc_result);
    return 0;
}
And got the output: CRC32: CB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

To calculate the CRC32 hash of a string without using built-in functions, we will manually implement the CRC32 algorithm. The CRC32 algorithm is a popular method for generating checksums to detect errors in data storage or transmission. This implementation will be a straightforward version, using a commonly used polynomial. The polynomial often used for CRC32 is `0xEDB88320`. A CRC32 calculation involves several steps, including initializing a CRC value, iterating over each byte of the input data, and performing bitwise operations that reflect the data off the current CRC value. The CRC value is typically initialized to `0xFFFFFFFF` and is negated (bitwise NOT) at the end of the calculation. Here is a basic implementation of the CRC32 calculation in C:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdint.h>

// Table for faster CRC calculations
uint32_t crc_table[256];

// Function to initialize the CRC table
void generate_crc_table() {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ 0xEDB88320;
            } else {
                crc >>= 1;
            }
        }
        crc_table[i] = crc;
    }
}

// Function to calculate the CRC32 of a string
uint32_t crc32(const char *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        uint32_t lookupIndex = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc_table[lookupIndex];
    }
    return ~crc; // Finalize the CRC value by negating it
}

int main() {
    const char *input = "correct horse battery staple";
    generate_crc_table(); // Initialize the CRC table
    uint32_t crc = crc32(input, strlen(input));
    printf("The CRC32 of \"%s\" is: 0x%X\n", input, crc);
    return 0;
}
In this program: - We first create a CRC table with `generate_crc_table()`, which pre-calculates all possible 8-bit values for faster CRC calculations. - The `crc32` function calculates the CRC32 hash of a given string. It iterates through each byte of the input, using the CRC table to speed up the calculation. - In the `main` function, we calculate and print the CRC32 hash of the string "correct horse battery staple". Compile and run this program with a C compiler, and it will output the CRC32 checksum of the input string.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdint.h>

// Table for faster CRC calculations
uint32_t crc_table[256];

// Function to initialize the CRC table
void generate_crc_table() {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ 0xEDB88320;
            } else {
                crc >>= 1;
            }
        }
        crc_table[i] = crc;
    }
}

// Function to calculate the CRC32 of a string
uint32_t crc32(const char *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        uint32_t lookupIndex = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc_table[lookupIndex];
    }
    return ~crc; // Finalize the CRC value by negating it
}

int main() {
    const char *input = "correct horse battery staple";
    generate_crc_table(); // Initialize the CRC table
    uint32_t crc = crc32(input, strlen(input));
    printf("The CRC32 of \"%s\" is: 0x%X\n", input, crc);
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdint.h>

// Table for faster CRC calculations
uint32_t crc_table[256];

// Function to initialize the CRC table
void generate_crc_table() {
    uint32_t crc;
    for (uint32_t i = 0; i < 256; i++) {
        crc = i;
        for (uint32_t j = 0; j < 8; j++) {
            if (crc & 1) {
                crc = (crc >> 1) ^ 0xEDB88320;
            } else {
                crc >>= 1;
            }
        }
        crc_table[i] = crc;
    }
}

// Function to calculate the CRC32 of a string
uint32_t crc32(const char *data, size_t length) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        uint32_t lookupIndex = (crc ^ byte) & 0xFF;
        crc = (crc >> 8) ^ crc_table[lookupIndex];
    }
    return ~crc; // Finalize the CRC value by negating it
}

int main() {
    const char *input = "correct horse battery staple";
    generate_crc_table(); // Initialize the CRC table
    uint32_t crc = crc32(input, strlen(input));
    printf("The CRC32 of \"%s\" is: 0x%X\n", input, crc);
    return 0;
}
And got the output: The CRC32 of "correct horse battery staple" is: 0xCB7E6E10 main.c: In function 'main': main.c:38:33: warning: implicit declaration of function 'strlen' [-Wimplicit-function-declaration] 38 | uint32_t crc = crc32(input, strlen(input)); | ^~~~~~ main.c:4:1: note: include '<string.h>' or provide a declaration of 'strlen' 3 | #include <stdint.h> +++ |+#include <string.h> 4 | main.c:38:33: warning: incompatible implicit declaration of built-in function 'strlen' [-Wbuiltin-declaration-mismatch] 38 | uint32_t crc = crc32(input, strlen(input)); | ^~~~~~ main.c:38:33: note: include '<string.h>' or provide a declaration of 'strlen'

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True

Initial Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

LLM Generation

Query

Write a c program that calculates the crc32 hash of the string "correct horse battery staple" and prints it to stdout. Dont call any builtin crc32 functions.

Output

To calculate the CRC32 hash of a string in C without using any built-in CRC32 functions, you can implement the CRC32 algorithm from scratch. The CRC32 algorithm is a popular way to produce a checksum based on the contents of a block of data. The following C program demonstrates how to calculate the CRC32 hash for the string "correct horse battery staple". This program uses the standard CRC32 polynomial (0xEDB88320) and employs a common technique of pre-generating a table of CRC32 values for all single-byte values, which significantly speeds up the computation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define CRC32_POLYNOMIAL 0xEDB88320

// Function to initialize the CRC table.
void initialize_crc_table(uint32_t *table) {
    for (uint32_t byte = 0; byte <= 0xFF; byte++) {
        uint32_t crc = byte;
        for (uint8_t bit = 8; bit > 0; bit--) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
        table[byte] = crc;
    }
}

// Function to calculate the CRC32 hash.
uint32_t calculate_crc32(const uint8_t *data, size_t length, uint32_t *table) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        uint8_t index = (uint8_t)(crc ^ byte);
        crc = (crc >> 8) ^ table[index];
    }
    return ~crc;
}

int main() {
    uint32_t crc_table[256];
    initialize_crc_table(crc_table);

    const char *input = "correct horse battery staple";
    uint32_t crc32_hash = calculate_crc32((const uint8_t *)input, strlen(input), crc_table);

    printf("CRC32 hash: %08X\n", crc32_hash);
    return 0;
}
This program defines two key functions besides `main`: `initialize_crc_table`, which fills in a table of CRC values for rapid lookup, and `calculate_crc32`, which computes the CRC32 hash of a given input string using the precomputed table. The `main` function initializes the CRC table, computes the CRC32 hash of the string "correct horse battery staple", and then prints the hash in hexadecimal format. This implementation is a straightforward example of how to calculate CRC32 in C without relying on built-in functions. Remember, CRC32 is widely used for checking the integrity of data, but it is not suitable for cryptographic purposes due to its susceptibility to intentional tampering.

Extract Code

I extracted the following code from that output:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define CRC32_POLYNOMIAL 0xEDB88320

// Function to initialize the CRC table.
void initialize_crc_table(uint32_t *table) {
    for (uint32_t byte = 0; byte <= 0xFF; byte++) {
        uint32_t crc = byte;
        for (uint8_t bit = 8; bit > 0; bit--) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
        table[byte] = crc;
    }
}

// Function to calculate the CRC32 hash.
uint32_t calculate_crc32(const uint8_t *data, size_t length, uint32_t *table) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        uint8_t index = (uint8_t)(crc ^ byte);
        crc = (crc >> 8) ^ table[index];
    }
    return ~crc;
}

int main() {
    uint32_t crc_table[256];
    initialize_crc_table(crc_table);

    const char *input = "correct horse battery staple";
    uint32_t crc32_hash = calculate_crc32((const uint8_t *)input, strlen(input), crc_table);

    printf("CRC32 hash: %08X\n", crc32_hash);
    return 0;
}

Run Code Interpreter

Running the following program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#define CRC32_POLYNOMIAL 0xEDB88320

// Function to initialize the CRC table.
void initialize_crc_table(uint32_t *table) {
    for (uint32_t byte = 0; byte <= 0xFF; byte++) {
        uint32_t crc = byte;
        for (uint8_t bit = 8; bit > 0; bit--) {
            if (crc & 1) {
                crc = (crc >> 1) ^ CRC32_POLYNOMIAL;
            } else {
                crc >>= 1;
            }
        }
        table[byte] = crc;
    }
}

// Function to calculate the CRC32 hash.
uint32_t calculate_crc32(const uint8_t *data, size_t length, uint32_t *table) {
    uint32_t crc = 0xFFFFFFFF;
    for (size_t i = 0; i < length; i++) {
        uint8_t byte = data[i];
        uint8_t index = (uint8_t)(crc ^ byte);
        crc = (crc >> 8) ^ table[index];
    }
    return ~crc;
}

int main() {
    uint32_t crc_table[256];
    initialize_crc_table(crc_table);

    const char *input = "correct horse battery staple";
    uint32_t crc32_hash = calculate_crc32((const uint8_t *)input, strlen(input), crc_table);

    printf("CRC32 hash: %08X\n", crc32_hash);
    return 0;
}
And got the output: CRC32 hash: CB7E6E10

Substring Evaluation

Testing if the previous output contains the string cb7e6e10: True