summaryrefslogtreecommitdiffstats
path: root/libsyscalls_section_value.c
blob: 6cdb787ccf8d8f85198a3585bb1f0173e42fbb81 (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
/* See LICENSE file for copyright and license details. */
#include "common.h"


enum libsyscalls_error
libsyscalls_section_value(unsigned long long int value_in, size_t bits,
                          enum libsyscalls_datatype_section section,
                          unsigned long long int *value_out)
{
	unsigned long long int value = value_in, mask, shift;
	size_t hbits;

	if (bits > sizeof(value) / sizeof(char) * CHAR_BIT ||
	    bits & (size_t)(LIBSYSCALLS_GET_SECTION_FRACTION(section) - 1U))
		return LIBSYSCALLS_E_INVAL;

	bits /= (size_t)LIBSYSCALLS_GET_SECTION_FRACTION(section);
	if (bits == sizeof(mask) / sizeof(char) * CHAR_BIT)
		mask = ~0ULL;
	else
		mask = (1ULL << bits) - 1ULL;

	switch (section) {
	case LIBSYSCALLS_SECTION_UNDETERMINED:
	case LIBSYSCALLS_SECTION_WHOLE: /* 0xFFFFFFFFFFFFFFFF */
	case LIBSYSCALLS_SECTION_LOWER_HALF: /* 0x00000000FFFFFFFF */
	case LIBSYSCALLS_SECTION_LOWER_QUARTER: /* 0x000000000000FFFF */
		break;

	case LIBSYSCALLS_SECTION_UPPER_HALF: /* 0xFFFFFFFF00000000 */
	case LIBSYSCALLS_SECTION_LOWER_MID_QUARTER: /* 0x00000000FFFF0000 */
		value >>= bits;
		break;

	case LIBSYSCALLS_SECTION_INNER_HALF: /* 0x0000FFFFFFFF0000 */
		value >>= bits / 2;
		break;

	case LIBSYSCALLS_SECTION_UPPER_MID_QUARTER: /* 0x0000FFFF00000000 */
		value >>= bits * 2;
		break;

	case LIBSYSCALLS_SECTION_UPPER_QUARTER: /* 0xFFFF000000000000 */
		value >>= bits * 3;
		break;

	case LIBSYSCALLS_SECTION_OUTER_HALF: /* 0xFFFF00000000FFFF */
		hbits = bits / 2;
		value = ((value & (mask << (bits + hbits))) >> bits)
		      | (value & (mask >> hbits));
		break;

	case LIBSYSCALLS_SECTION_ODD_QUARTERS_AS_HALF: /* 0xFFFF0000FFFF0000 */
		value >>= bits / 2;
		/* fall through */
	case LIBSYSCALLS_SECTION_EVEN_QUARTERS_AS_HALF: /* 0x0000FFFF0000FFFF */
		hbits = bits / 2;
		value = ((value & (mask << hbits) & ~mask) >> hbits)
		      | (value & (mask >> hbits));
		break;

	case LIBSYSCALLS_SECTION_ODD_BYTES_AS_HALF: /* 0xFF00FF00FF00FF00 */
		value_in >>= 8;
		/* fall through */
	case LIBSYSCALLS_SECTION_EVEN_BYTES_AS_HALF: /* 0x00FF00FF00FF00FF */
		value = 0;
		for (shift = 0; value_in; value_in >>= 16, shift += 8)
			value |= (value_in & 0x00FFU) << shift;
		break;

	default:
		return LIBSYSCALLS_E_INVAL;
	}

	value &= mask;
masked:
	if (value_out)
		*value_out = value;
	return LIBSYSCALLS_E_OK;
}