EtherLib/utils.h

57 lines
1.6 KiB
C

//
// Created by epagris on 2022.10.31..
//
#ifndef ETHERLIB_UTILS_H
#define ETHERLIB_UTILS_H
#include <stdio.h>
#include <stdint.h>
#include <memory.h>
#ifndef htonl
#define htonl(a) \
((((a) >> 24) & 0x000000ff) | \
(((a) >> 8) & 0x0000ff00) | \
(((a) << 8) & 0x00ff0000) | \
(((a) << 24) & 0xff000000))
#endif
#ifndef ntohl
#define ntohl(a) htonl((a))
#endif
#ifndef htons
#define htons(a) \
((((a) >> 8) & 0x00ff) | \
(((a) << 8) & 0xff00))
#endif
#ifndef ntohs
#define ntohs(a) htons((a))
#endif
#define ERROR(...) printf(__VA_ARGS__)
#define INFO(...) printf(__VA_ARGS__)
#define ASSERT_BAD_ALIGN(p) if ((size_t)(p) & 0b11) ERROR("Bad memory alignment in function '%s' in file '%s' on line %d!\n", __func__, __FILE__, __LINE__)
#define ASSERT_NULL(p) if ((p) == NULL) ERROR("NULL in function '%s' in file '%s' on line %d!\n", __func__, __FILE__, __LINE__)
#define ALIGN(p,t) (((size_t)(p) + (sizeof(t) - 1)) & ~(sizeof(t) - 1))
#define COPY_ADVANCE(dst,src,n) memcpy((dst), (src), n), src += n
#define COPY_WORD_H2N_ADVANCE(dst,w) { uint16_t u; memcpy(&u, w, 2); u = htons(u); memcpy((dst), &u, 2); w += 2; }
#define COPY_DWORD_H2N_ADVANCE(dst,dw) { uint32_t du; memcpy(&du, dw, 4); du = htonl(du); memcpy((dst), &du, 4); dw += 4; }
// ------------------
/**
* Compute CRC-32 checksum.
* @param data Data from which the checksum is being calculated.
* @param size Size of data byte array.
* @return checksum
*/
uint32_t crc32(const uint8_t * data, uint32_t size);
#endif //ETHERLIB_UTILS_H