summaryrefslogtreecommitdiffstats
path: root/lib/num.h
blob: dd2ee414648eed7707be3d5197da59181e8615fd (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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 *
 */

#ifndef GNUTLS_LIB_NUM_H
#define GNUTLS_LIB_NUM_H

#include "gnutls_int.h"

#include <minmax.h>
#include <byteswap.h>

/* data should be at least 3 bytes */
inline static uint32_t _gnutls_read_uint24(const uint8_t * data)
{
	return (data[0] << 16) | (data[1] << 8) | (data[2]);
}

inline static uint64_t _gnutls_read_uint64(const uint8_t * data)
{
	uint64_t res;

	memcpy(&res, data, sizeof(uint64_t));
#ifndef WORDS_BIGENDIAN
	res = bswap_64(res);
#endif
	return res;
}

inline static void _gnutls_write_uint64(uint64_t num, uint8_t * data)
{
#ifndef WORDS_BIGENDIAN
	num = bswap_64(num);
#endif
	memcpy(data, &num, 8);
}

inline static void _gnutls_write_uint24(uint32_t num, uint8_t * data)
{
	data[0] = num >> 16;
	data[1] = num >> 8;
	data[2] = num;
}

inline static uint32_t _gnutls_read_uint32(const uint8_t * data)
{
	uint32_t res;

	memcpy(&res, data, sizeof(uint32_t));
#ifndef WORDS_BIGENDIAN
	res = bswap_32(res);
#endif
	return res;
}

inline static void _gnutls_write_uint32(uint32_t num, uint8_t * data)
{

#ifndef WORDS_BIGENDIAN
	num = bswap_32(num);
#endif
	memcpy(data, &num, sizeof(uint32_t));
}

inline static uint16_t _gnutls_read_uint16(const uint8_t * data)
{
	uint16_t res;
	memcpy(&res, data, sizeof(uint16_t));
#ifndef WORDS_BIGENDIAN
	res = bswap_16(res);
#endif
	return res;
}

inline static void _gnutls_write_uint16(uint16_t num, uint8_t * data)
{

#ifndef WORDS_BIGENDIAN
	num = bswap_16(num);
#endif
	memcpy(data, &num, sizeof(uint16_t));
}

inline static uint32_t _gnutls_conv_uint32(uint32_t data)
{
#ifndef WORDS_BIGENDIAN
	return bswap_32(data);
#else
	return data;
#endif
}

inline static uint16_t _gnutls_conv_uint16(uint16_t data)
{
#ifndef WORDS_BIGENDIAN
	return bswap_16(data);
#else
	return data;
#endif
}

#endif /* GNUTLS_LIB_NUM_H */