#include "game_types.h"

/* Sub_08010088 @ 0x08010088 (real 88B) -- CRC-16/CCITT-FALSE over len bytes, inverted. Mirrors src/matched/Sub_08010088.c. */
/* CRC-16 (poly 0x1021, init 0xFFFF) over len bytes, bit-banged with the
   17-bit verbatim shift on clear bits, returned inverted and masked. */
unsigned Sub_08010088(int unused, unsigned char *buf, int len)
{
    unsigned crc = 0xFFFF;
    int i, k;
    (void)unused;
    for (i = 0; i < len; i++) {
        crc ^= (unsigned)buf[i] << 8;
        for (k = 0; k < 8; k++) {
            if ((crc & 0x8000) != 0)
                crc = (((crc << 1) ^ 0x1021) << 16) >> 16;
            else
                crc = (crc << 17) >> 16;
        }
    }
    return (~crc) & 0xFFFF;
}
