1 /*
2  * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3  *
4  * Author: Nikos Mavrogiannopoulos
5  *
6  * This file is part of GnuTLS.
7  *
8  * The GnuTLS is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>
20  *
21  */
22 
23 #ifndef GNUTLS_LIB_NUM_H
24 #define GNUTLS_LIB_NUM_H
25 
26 #include "gnutls_int.h"
27 
28 #include <minmax.h>
29 #include <byteswap.h>
30 
31 /* data should be at least 3 bytes */
_gnutls_read_uint24(const uint8_t * data)32 inline static uint32_t _gnutls_read_uint24(const uint8_t * data)
33 {
34 	return (data[0] << 16) | (data[1] << 8) | (data[2]);
35 }
36 
_gnutls_read_uint64(const uint8_t * data)37 inline static uint64_t _gnutls_read_uint64(const uint8_t * data)
38 {
39 	uint64_t res;
40 
41 	memcpy(&res, data, sizeof(uint64_t));
42 #ifndef WORDS_BIGENDIAN
43 	res = bswap_64(res);
44 #endif
45 	return res;
46 }
47 
_gnutls_write_uint64(uint64_t num,uint8_t * data)48 inline static void _gnutls_write_uint64(uint64_t num, uint8_t * data)
49 {
50 #ifndef WORDS_BIGENDIAN
51 	num = bswap_64(num);
52 #endif
53 	memcpy(data, &num, 8);
54 }
55 
_gnutls_write_uint24(uint32_t num,uint8_t * data)56 inline static void _gnutls_write_uint24(uint32_t num, uint8_t * data)
57 {
58 	data[0] = num >> 16;
59 	data[1] = num >> 8;
60 	data[2] = num;
61 }
62 
_gnutls_read_uint32(const uint8_t * data)63 inline static uint32_t _gnutls_read_uint32(const uint8_t * data)
64 {
65 	uint32_t res;
66 
67 	memcpy(&res, data, sizeof(uint32_t));
68 #ifndef WORDS_BIGENDIAN
69 	res = bswap_32(res);
70 #endif
71 	return res;
72 }
73 
_gnutls_write_uint32(uint32_t num,uint8_t * data)74 inline static void _gnutls_write_uint32(uint32_t num, uint8_t * data)
75 {
76 
77 #ifndef WORDS_BIGENDIAN
78 	num = bswap_32(num);
79 #endif
80 	memcpy(data, &num, sizeof(uint32_t));
81 }
82 
_gnutls_read_uint16(const uint8_t * data)83 inline static uint16_t _gnutls_read_uint16(const uint8_t * data)
84 {
85 	uint16_t res;
86 	memcpy(&res, data, sizeof(uint16_t));
87 #ifndef WORDS_BIGENDIAN
88 	res = bswap_16(res);
89 #endif
90 	return res;
91 }
92 
_gnutls_write_uint16(uint16_t num,uint8_t * data)93 inline static void _gnutls_write_uint16(uint16_t num, uint8_t * data)
94 {
95 
96 #ifndef WORDS_BIGENDIAN
97 	num = bswap_16(num);
98 #endif
99 	memcpy(data, &num, sizeof(uint16_t));
100 }
101 
_gnutls_conv_uint32(uint32_t data)102 inline static uint32_t _gnutls_conv_uint32(uint32_t data)
103 {
104 #ifndef WORDS_BIGENDIAN
105 	return bswap_32(data);
106 #else
107 	return data;
108 #endif
109 }
110 
_gnutls_conv_uint16(uint16_t data)111 inline static uint16_t _gnutls_conv_uint16(uint16_t data)
112 {
113 #ifndef WORDS_BIGENDIAN
114 	return bswap_16(data);
115 #else
116 	return data;
117 #endif
118 }
119 
120 #endif /* GNUTLS_LIB_NUM_H */
121