view src/utils.c @ 9:07f0e2f43204

*: add the restrict keyword when necessary also fixed some typos in the README
author Paper
date Fri, 16 Dec 2022 21:55:37 -0500
parents be4835547dd0
children 42ac054c0231
line wrap: on
line source

/**
 * utils.c:
 *
 * Useful utilities for general use.
**/
#ifndef WIN32_LEAN_AND_MEAN
#  define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <stdint.h>

uint64_t get_system_time_in_milliseconds(void) {
 	FILETIME ft;
	GetSystemTimeAsFileTime(&ft);
	uint64_t ul = (uint64_t) ft.dwHighDateTime << 32 | ft.dwLowDateTime;
	return ((ul - 116444736000000000ULL)/10000000);
}

uint32_t crc32b(unsigned char* restrict message) {
	int32_t i, j;
	uint32_t byte, crc, mask;

	crc = 0xFFFFFFFF;
	for (i = 0; message[i] != 0; i++) {
		byte = message[i];            // Get next byte.
		crc = crc ^ byte;
		for (j = 7; j >= 0; j--) {    // Do eight times.
			mask = -(crc & 1);
			crc = (crc >> 1) ^ (0xEDB88320 & mask);
		}
	}
	return ~crc;
}