2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2010 Free
3 * Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GnuTLS.
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 /* This file contains the functions needed for 64 bit integer support in
27 * TLS, and functions which ease the access to TLS vectors (data of given size).
30 #include <gnutls_int.h>
31 #include <gnutls_num.h>
32 #include <gnutls_errors.h>
36 /* This function will add one to uint64 x.
37 * Returns 0 on success, or -1 if the uint64 max limit
41 _gnutls_uint64pp (uint64
* x
)
43 register int i
, y
= 0;
45 for (i
= 7; i
>= 0; i
--)
60 return -1; /* over 64 bits! WOW */
65 /* This function will add one to uint48 x.
66 * Returns 0 on success, or -1 if the uint48 max limit
70 _gnutls_uint48pp (uint64
* x
)
72 register int i
, y
= 0;
74 for (i
= 7; i
>= 3; i
--)
89 return -1; /* over 48 bits */
95 _gnutls_uint24touint32 (uint24 num
)
99 ((uint8_t *) & ret
)[1] = num
.pint
[0];
100 ((uint8_t *) & ret
)[2] = num
.pint
[1];
101 ((uint8_t *) & ret
)[3] = num
.pint
[2];
106 _gnutls_uint32touint24 (uint32_t num
)
110 ret
.pint
[0] = ((uint8_t *) & num
)[1];
111 ret
.pint
[1] = ((uint8_t *) & num
)[2];
112 ret
.pint
[2] = ((uint8_t *) & num
)[3];
117 /* data should be at least 3 bytes */
119 _gnutls_read_uint24 (const opaque
* data
)
124 num
.pint
[0] = data
[0];
125 num
.pint
[1] = data
[1];
126 num
.pint
[2] = data
[2];
128 res
= _gnutls_uint24touint32 (num
);
129 #ifndef WORDS_BIGENDIAN
130 res
= bswap_32 (res
);
136 _gnutls_write_uint24 (uint32_t num
, opaque
* data
)
140 #ifndef WORDS_BIGENDIAN
141 num
= bswap_32 (num
);
143 tmp
= _gnutls_uint32touint24 (num
);
145 data
[0] = tmp
.pint
[0];
146 data
[1] = tmp
.pint
[1];
147 data
[2] = tmp
.pint
[2];
151 _gnutls_read_uint32 (const opaque
* data
)
155 memcpy (&res
, data
, sizeof (uint32_t));
156 #ifndef WORDS_BIGENDIAN
157 res
= bswap_32 (res
);
163 _gnutls_write_uint32 (uint32_t num
, opaque
* data
)
166 #ifndef WORDS_BIGENDIAN
167 num
= bswap_32 (num
);
169 memcpy (data
, &num
, sizeof (uint32_t));
173 _gnutls_read_uint16 (const opaque
* data
)
176 memcpy (&res
, data
, sizeof (uint16_t));
177 #ifndef WORDS_BIGENDIAN
178 res
= bswap_16 (res
);
184 _gnutls_write_uint16 (uint16_t num
, opaque
* data
)
187 #ifndef WORDS_BIGENDIAN
188 num
= bswap_16 (num
);
190 memcpy (data
, &num
, sizeof (uint16_t));
194 _gnutls_conv_uint32 (uint32_t data
)
196 #ifndef WORDS_BIGENDIAN
197 return bswap_32 (data
);
204 _gnutls_conv_uint16 (uint16_t data
)
206 #ifndef WORDS_BIGENDIAN
207 return bswap_16 (data
);
214 _gnutls_uint64touint32 (const uint64
* num
)
218 memcpy (&ret
, &num
->i
[4], 4);
219 #ifndef WORDS_BIGENDIAN
220 ret
= bswap_32 (ret
);