Wandbox
SettingsLog
SettingsLog
Language
GitHubLogin
Ran/Viewed Log

Viewed less than a minute ago

W3CX6UhYjgfGkJWI

C gcc 13.2.0

Created at less than a minute ago

Created by anonymous

Author

anonymous

less than a minute ago

Language

C

Compiler

gcc 13.2.0

Options
Warnings
Compiler Default
no pedantic

Author

anonymous

less than a minute ago

›
99
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
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

uint32_t crc32_inv(uint32_t status_after, uint8_t byte_value) {
static const uint32_t MAGIC = UINT32_C(0xEDB88320);
uint32_t status = status_after;

// シフトとマジックナンバーのXORの処理を逆回しする
for (int i = 0; i < 8; i++) {
uint32_t msb = status & UINT32_C(0x80000000);
if (msb) {
status ^= MAGIC;
status <<= 1;
status |= 1;
} else {
status <<= 1;
}
}

// 更新に用いたバイトの値をXORする
status ^= byte_value;

return status;
}

int main(void) {
const char *data = "IEND";
uint32_t status = UINT32_C(0xAE426082);
printf("CRC32: 0x%08" PRIx32 "\n", status);
status = ~status; // 最後に行ったビット反転を打ち消す
for (int i = (int)strlen(data) - 1; i >= 0; i--) {
printf("after data[%d] = 0x%02x : 0x%08" PRIx32 "\n", i, data[i], status);
status = crc32_inv(status, data[i]);
}
printf("initial value: 0x%08" PRIx32 "\n", status);
return 0;
}

$ gcc prog.c -Wall -Wextra
CRC32: 0xae426082
after data[3] = 0x44 : 0x51bd9f7d
after data[2] = 0x4e : 0x639f4775
after data[1] = 0x45 : 0x992bac53
after data[0] = 0x49 : 0x22fde946
initial value: 0xffffffff
Exit Code:
0