// File: OpenFodder[P]SOS_Unpacker/capsimg/Core/CRC.cpp
// Function: MakeCRCTable()
void MakeCRCTable()
{
	UDWORD poly=0;
	int i;

	if (crcinit)
		return;

	// crc32
	for (i=0; i < sizeof(crcpoly)/sizeof(UBYTE); i++)
		poly|=1UL<<(31-crcpoly[i]);

	for (i=0; i < 256; i++) {
		UDWORD crc=i;
		int bit;

		for (bit=0; bit < 8; bit++)
			crc=crc & 1 ? poly^(crc>>1) : crc>>1;

		crctab[i]=crc;
	}

	// crc-ccitt
	for (i=0, poly=0; i < sizeof(crcpoly_ccitt)/sizeof(UBYTE); i++)
		poly|=1UL<<crcpoly_ccitt[i];

	for (i=0; i < 256; i++) {
		UWORD crc=i<<8;
		int bit;

		for (bit=0; bit < 8; bit++)
			crc=crc & 0x8000 ? (UWORD)poly^(crc<<1) : crc<<1;

		crctab_ccitt[i]=crc;
	}

	// crc-ansi
	for (i=0, poly=0; i < sizeof(crcpoly_ansi)/sizeof(UBYTE); i++)
		poly|=1UL<<crcpoly_ansi[i];

	for (i=0; i < 256; i++) {
		UWORD crc=i<<8;
		int bit;

		for (bit=0; bit < 8; bit++)
			crc=crc & 0x8000 ? (UWORD)poly^(crc<<1) : crc<<1;

		crctab_ansi[i]=crc;
	}

	crcinit=1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
movl $0x0, -0x4(%rbp)
cmpl $0x0, 0x22702(%rip)     # 0x27d24
je 0x5629
jmp 0x5885
movl $0x0, -0x8(%rbp)
movslq -0x8(%rbp), %rax
cmpq $0xe, %rax
jae 0x5673
movslq -0x8(%rbp), %rcx
leaq 0x21c0b(%rip), %rax     # 0x27250
movzbl (%rax,%rcx), %ecx
movl $0x1f, %eax
subl %ecx, %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x4(%rbp), %eax
orq %rcx, %rax
movl %eax, -0x4(%rbp)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x5630
movl $0x0, -0x8(%rbp)
cmpl $0x100, -0x8(%rbp)      # imm = 0x100
jge 0x56e5
movl -0x8(%rbp), %eax
movl %eax, -0xc(%rbp)
movl $0x0, -0x10(%rbp)
cmpl $0x8, -0x10(%rbp)
jge 0x56c9
movl -0xc(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x56b0
movl -0x4(%rbp), %eax
movl -0xc(%rbp), %ecx
shrl %ecx
xorl %ecx, %eax
movl %eax, -0x24(%rbp)
jmp 0x56b8
movl -0xc(%rbp), %eax
shrl %eax
movl %eax, -0x24(%rbp)
movl -0x24(%rbp), %eax
movl %eax, -0xc(%rbp)
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0x5690
movl -0xc(%rbp), %edx
movslq -0x8(%rbp), %rcx
leaq 0x22659(%rip), %rax     # 0x27d30
movl %edx, (%rax,%rcx,4)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x567a
movl $0x0, -0x8(%rbp)
movl $0x0, -0x4(%rbp)
movslq -0x8(%rbp), %rax
cmpq $0x3, %rax
jae 0x572f
movslq -0x8(%rbp), %rcx
leaq 0x21b56(%rip), %rax     # 0x2725e
movzbl (%rax,%rcx), %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x4(%rbp), %eax
orq %rcx, %rax
movl %eax, -0x4(%rbp)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x56f3
movl $0x0, -0x8(%rbp)
cmpl $0x100, -0x8(%rbp)      # imm = 0x100
jge 0x57b0
movl -0x8(%rbp), %eax
shll $0x8, %eax
movw %ax, -0x12(%rbp)
movl $0x0, -0x18(%rbp)
cmpl $0x8, -0x18(%rbp)
jge 0x5792
movzwl -0x12(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x5777
movl -0x4(%rbp), %eax
movzwl %ax, %eax
movzwl -0x12(%rbp), %ecx
shll %ecx
xorl %ecx, %eax
movl %eax, -0x28(%rbp)
jmp 0x5780
movzwl -0x12(%rbp), %eax
shll %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
movw %ax, -0x12(%rbp)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x5750
movw -0x12(%rbp), %dx
movslq -0x8(%rbp), %rcx
leaq 0x2298f(%rip), %rax     # 0x28130
movw %dx, (%rax,%rcx,2)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x5736
movl $0x0, -0x8(%rbp)
movl $0x0, -0x4(%rbp)
movslq -0x8(%rbp), %rax
cmpq $0x3, %rax
jae 0x57fa
movslq -0x8(%rbp), %rcx
leaq 0x21a8e(%rip), %rax     # 0x27261
movzbl (%rax,%rcx), %eax
movl %eax, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl -0x4(%rbp), %eax
orq %rcx, %rax
movl %eax, -0x4(%rbp)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x57be
movl $0x0, -0x8(%rbp)
cmpl $0x100, -0x8(%rbp)      # imm = 0x100
jge 0x587b
movl -0x8(%rbp), %eax
shll $0x8, %eax
movw %ax, -0x1a(%rbp)
movl $0x0, -0x20(%rbp)
cmpl $0x8, -0x20(%rbp)
jge 0x585d
movzwl -0x1a(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x5842
movl -0x4(%rbp), %eax
movzwl %ax, %eax
movzwl -0x1a(%rbp), %ecx
shll %ecx
xorl %ecx, %eax
movl %eax, -0x2c(%rbp)
jmp 0x584b
movzwl -0x1a(%rbp), %eax
shll %eax
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movw %ax, -0x1a(%rbp)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x581b
movw -0x1a(%rbp), %dx
movslq -0x8(%rbp), %rcx
leaq 0x22ac4(%rip), %rax     # 0x28330
movw %dx, (%rax,%rcx,2)
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x5801
movl $0x1, 0x2249f(%rip)     # 0x27d24
popq %rbp
retq
nopw (%rax,%rax)