2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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.
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
25 /* This file contains the functions needed for 64 bit integer support in
26 * TLS, and functions which ease the access to TLS vectors (data of given size).
29 #include <gnutls_int.h>
30 #include <gnutls_num.h>
31 #include <gnutls_errors.h>
33 /* This function will add one to uint64 x.
34 * Returns 0 on success, or -1 if the uint64 max limit
38 _gnutls_uint64pp (uint64
* x
)
40 register int i
, y
= 0;
42 for (i
= 7; i
>= 0; i
--)
57 return -1; /* over 64 bits! WOW */
63 _gnutls_uint24touint32 (uint24 num
)
67 ((uint8_t *) & ret
)[1] = num
.pint
[0];
68 ((uint8_t *) & ret
)[2] = num
.pint
[1];
69 ((uint8_t *) & ret
)[3] = num
.pint
[2];
74 _gnutls_uint32touint24 (uint32_t num
)
78 ret
.pint
[0] = ((uint8_t *) & num
)[1];
79 ret
.pint
[1] = ((uint8_t *) & num
)[2];
80 ret
.pint
[2] = ((uint8_t *) & num
)[3];
85 /* data should be at least 3 bytes */
87 _gnutls_read_uint24 (const opaque
* data
)
92 num
.pint
[0] = data
[0];
93 num
.pint
[1] = data
[1];
94 num
.pint
[2] = data
[2];
96 res
= _gnutls_uint24touint32 (num
);
97 #ifndef WORDS_BIGENDIAN
98 res
= byteswap32 (res
);
104 _gnutls_write_uint24 (uint32_t num
, opaque
* data
)
108 #ifndef WORDS_BIGENDIAN
109 num
= byteswap32 (num
);
111 tmp
= _gnutls_uint32touint24 (num
);
113 data
[0] = tmp
.pint
[0];
114 data
[1] = tmp
.pint
[1];
115 data
[2] = tmp
.pint
[2];
119 _gnutls_read_uint32 (const opaque
* data
)
123 memcpy (&res
, data
, sizeof (uint32_t));
124 #ifndef WORDS_BIGENDIAN
125 res
= byteswap32 (res
);
131 _gnutls_write_uint32 (uint32_t num
, opaque
* data
)
134 #ifndef WORDS_BIGENDIAN
135 num
= byteswap32 (num
);
137 memcpy (data
, &num
, sizeof (uint32_t));
141 _gnutls_read_uint16 (const opaque
* data
)
144 memcpy (&res
, data
, sizeof (uint16_t));
145 #ifndef WORDS_BIGENDIAN
146 res
= byteswap16 (res
);
152 _gnutls_write_uint16 (uint16_t num
, opaque
* data
)
155 #ifndef WORDS_BIGENDIAN
156 num
= byteswap16 (num
);
158 memcpy (data
, &num
, sizeof (uint16_t));
162 _gnutls_conv_uint32 (uint32_t data
)
164 #ifndef WORDS_BIGENDIAN
165 return byteswap32 (data
);
172 _gnutls_conv_uint16 (uint16_t data
)
174 #ifndef WORDS_BIGENDIAN
175 return byteswap16 (data
);
182 _gnutls_uint64touint32 (const uint64
* num
)
186 memcpy (&ret
, &num
->i
[4], 4);
187 #ifndef WORDS_BIGENDIAN
188 ret
= byteswap32 (ret
);