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 */
66 _gnutls_uint24touint32 (uint24 num)
70 ((uint8_t *) & ret)[1] = num.pint[0];
71 ((uint8_t *) & ret)[2] = num.pint[1];
72 ((uint8_t *) & ret)[3] = num.pint[2];
77 _gnutls_uint32touint24 (uint32_t num)
81 ret.pint[0] = ((uint8_t *) & num)[1];
82 ret.pint[1] = ((uint8_t *) & num)[2];
83 ret.pint[2] = ((uint8_t *) & num)[3];
88 /* data should be at least 3 bytes */
90 _gnutls_read_uint24 (const opaque * data)
95 num.pint[0] = data[0];
96 num.pint[1] = data[1];
97 num.pint[2] = data[2];
99 res = _gnutls_uint24touint32 (num);
100 #ifndef WORDS_BIGENDIAN
101 res = bswap_32 (res);
107 _gnutls_write_uint24 (uint32_t num, opaque * data)
111 #ifndef WORDS_BIGENDIAN
112 num = bswap_32 (num);
114 tmp = _gnutls_uint32touint24 (num);
116 data[0] = tmp.pint[0];
117 data[1] = tmp.pint[1];
118 data[2] = tmp.pint[2];
122 _gnutls_read_uint32 (const opaque * data)
126 memcpy (&res, data, sizeof (uint32_t));
127 #ifndef WORDS_BIGENDIAN
128 res = bswap_32 (res);
134 _gnutls_write_uint32 (uint32_t num, opaque * data)
137 #ifndef WORDS_BIGENDIAN
138 num = bswap_32 (num);
140 memcpy (data, &num, sizeof (uint32_t));
144 _gnutls_read_uint16 (const opaque * data)
147 memcpy (&res, data, sizeof (uint16_t));
148 #ifndef WORDS_BIGENDIAN
149 res = bswap_16 (res);
155 _gnutls_write_uint16 (uint16_t num, opaque * data)
158 #ifndef WORDS_BIGENDIAN
159 num = bswap_16 (num);
161 memcpy (data, &num, sizeof (uint16_t));
165 _gnutls_conv_uint32 (uint32_t data)
167 #ifndef WORDS_BIGENDIAN
168 return bswap_32 (data);
175 _gnutls_conv_uint16 (uint16_t data)
177 #ifndef WORDS_BIGENDIAN
178 return bswap_16 (data);
185 _gnutls_uint64touint32 (const uint64 * num)
189 memcpy (&ret, &num->i[4], 4);
190 #ifndef WORDS_BIGENDIAN
191 ret = bswap_32 (ret);