package cql

import (
	"hash/crc32"
)

var (
	// Initial CRC32 bytes: 0xFA, 0x2D, 0x55, 0xCA
	initialCRC32Bytes = []byte{0xfa, 0x2d, 0x55, 0xca}
)

// Crc32 calculates the CRC32 checksum of the given byte slice.
// Used for protocol v5+ segment payload integrity.
func Crc32(b []byte) uint32 {
	crc := crc32.NewIEEE()
	crc.Write(initialCRC32Bytes) // Include initial CRC32 bytes
	crc.Write(b)
	return crc.Sum32()
}

const (
	crc24Init = 0x875060  // Initial value for CRC24 calculation
	crc24Poly = 0x1974F0B // Polynomial for CRC24 calculation (Koopman polynomial)
)

// Crc24 calculates the CRC24 checksum using the Koopman polynomial.
// Used for protocol v5+ segment header integrity.
func Crc24(buf []byte) uint32 {
	crc := crc24Init
	for _, b := range buf {
		crc ^= int(b) << 16

		for i := 0; i < 8; i++ {
			crc <<= 1
			if crc&0x1000000 != 0 {
				crc ^= crc24Poly
			}
		}
	}

	return uint32(crc)
}
