view src/utils.c @ 10:42ac054c0231

*: huge refactoring dirtools now uses wchar (wayyy overdue) the timer doesn't have a stupid design anymore we don't use windows.h at all now ...
author Paper <paper@paper.us.eu.org>
date Sun, 11 Feb 2024 19:43:31 -0500
parents 07f0e2f43204
children e6a594f16403
line wrap: on
line source

/**
 * utils.c:
 *
 * Useful utilities for general use.
**/
#include "utils.h"

#include <sysinfoapi.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;
}