/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/20#include "SDL_internal.h"2122/* Public domain CRC implementation adapted from:23http://home.thep.lu.se/~bjorn/crc/crc32_simple.c2425This algorithm is compatible with the 16-bit CRC described here:26https://www.lammertbies.nl/comm/info/crc-calculation27*/28/* NOTE: DO NOT CHANGE THIS ALGORITHM29There is code that relies on this in the joystick code30*/3132static Uint16 crc16_for_byte(Uint8 r)33{34Uint16 crc = 0;35int i;36for (i = 0; i < 8; ++i) {37crc = ((crc ^ r) & 1 ? 0xA001 : 0) ^ crc >> 1;38r >>= 1;39}40return crc;41}4243Uint16 SDL_crc16(Uint16 crc, const void *data, size_t len)44{45// As an optimization we can precalculate a 256 entry table for each byte46size_t i;47for (i = 0; i < len; ++i) {48crc = crc16_for_byte((Uint8)crc ^ ((const Uint8 *)data)[i]) ^ crc >> 8;49}50return crc;51}525354