summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/coreutil.cpp
blob: bd10802ef5ab48901f6f20e9d3a64d1aac29646b (plain) (blame)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    coreutil.c

    Miscellaneous utility code

***************************************************************************/

#include "coreutil.h"
#include <assert.h>
#include <zlib.h>


/***************************************************************************
    BINARY CODED DECIMAL HELPERS
***************************************************************************/

int bcd_adjust(int value)
{
	if ((value & 0xf) >= 0xa)
		value = value + 0x10 - 0xa;
	if ((value & 0xf0) >= 0xa0)
		value = value - 0xa0 + 0x100;
	return value;
}


uint32_t dec_2_bcd(uint32_t a)
{
	uint32_t result = 0;
	int shift = 0;

	while (a != 0)
	{
		result |= (a % 10) << shift;
		a /= 10;
		shift += 4;
	}
	return result;
}


uint32_t bcd_2_dec(uint32_t a)
{
	uint32_t result = 0;
	uint32_t scale = 1;

	while (a != 0)
	{
		result += (a & 0x0f) * scale;
		a >>= 4;
		scale *= 10;
	}
	return result;
}



/***************************************************************************
    MISC
***************************************************************************/

/**
 * @fn  void rand_memory(void *memory, size_t length)
 *
 * @brief   Random memory.
 *
 * @param [in,out]  memory  If non-null, the memory.
 * @param   length          The length.
 */

void rand_memory(void *memory, size_t length)
{
	static uint32_t seed = 0;
	uint8_t *bytes = (uint8_t *) memory;
	size_t i;

	for (i = 0; i < length; i++)
	{
		seed = seed * 214013 + 2531011;
		bytes[i] = (uint8_t) (seed >> 16);
	}
}


uint32_t core_crc32(uint32_t crc, const uint8_t *buf, uint32_t len)
{
	return crc32(crc, buf, len);
}