ip4sum 0.1.0

Highly optimized IPv4 checksum calculation, no-std compatible
Documentation
/* SPDX-License-Identifier: MIT */
/* Copyright (c) 2026 Khashayar Fereidani */

#ifndef IP4SUM_CHECKSUM_H
#define IP4SUM_CHECKSUM_H

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Incremental checksum state. */
typedef struct {
    uint64_t acc;
} ip4sum_checksum;

/* Initialize a new checksum calculator. */
static inline ip4sum_checksum ip4sum_checksum_new(void) {
    ip4sum_checksum c;
    c.acc = 0;
    return c;
}

/* Feed a byte slice into the running checksum. */
void ip4sum_checksum_update(ip4sum_checksum *c, const unsigned char *data,
                            size_t len);

/* Finalize and return the 16-bit one's-complement checksum
   in network byte order. */
uint16_t ip4sum_checksum_finalize(ip4sum_checksum c);

/* Reset the calculator to its initial state. */
static inline void ip4sum_checksum_reset(ip4sum_checksum *c) {
    c->acc = 0;
}

/* One-shot: compute the Internet checksum of a byte slice. */
uint16_t ip4sum_checksum_oneshot(const unsigned char *data, size_t len);

#ifdef __cplusplus
}
#endif

#endif /* IP4SUM_CHECKSUM_H */