summaryrefslogtreecommitdiffhomepage
path: root/src/hash/ntapi_tt_crc32.c
blob: 2e0630a9af70c1890a326952714c93024761dd4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_crc32.h>
#include "ntapi_impl.h"

static const uint32_t crc32_table[256] = NTAPI_CRC32_TABLE;

uint32_t __ntapi_tt_buffer_crc32(
	uint32_t	prev_hash,
	const void *	buffer,
	size_t		size)
{
	const unsigned char *	ch;
	uint32_t		crc32;

	crc32	= prev_hash ^ 0xFFFFFFFF;
	ch	= buffer;

	for (; size; size--,ch++)
		crc32 = (crc32 >> 8) ^ crc32_table[(crc32 ^ *ch) & 0xFF];

	return (crc32 ^ 0xFFFFFFFF);
}


uint32_t __cdecl __ntapi_tt_mbstr_crc32(const void * str)
{
	const unsigned char *	ch;
	uint32_t		crc32;

	crc32	= 0 ^ 0xFFFFFFFF;
	ch	= str;

	while (*ch) {
		crc32 = (crc32 >> 8) ^ crc32_table[(crc32 ^ *ch) & 0xFF];
		ch++;
	}

	return (crc32 ^ 0xFFFFFFFF);
}


const uint32_t * __cdecl __ntapi_tt_crc32_table(void)
{
	return crc32_table;
}