2 * Copyright (C) 2000,2001,2002 Nikos Mavroyanopoulos
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <gnutls_int.h>
23 #include <gnutls_num.h>
24 #include <gnutls_errors.h>
29 /* This function will set the uint64 x to zero
31 int _gnutls_uint64zero( uint64
*x
) {
37 /* This function will add one to uint64 x.
38 * Returns 0 on success, or -1 if the uint64 max limit
41 int _gnutls_uint64pp( uint64
*x
) {
46 if ( x
->i
[i
] == 0xff) {
53 if (y
!= 0) return -1; /* over 64 bits! WOW */
58 #endif /* HAVE_UINT64 */
60 uint32
_gnutls_uint24touint32( uint24 num
) {
63 ((uint8
*)&ret
)[1] = num
.pint
[0];
64 ((uint8
*)&ret
)[2] = num
.pint
[1];
65 ((uint8
*)&ret
)[3] = num
.pint
[2];
69 uint24
_gnutls_uint32touint24( uint32 num
) {
72 ret
.pint
[0] = ((uint8
*)&num
)[1];
73 ret
.pint
[1] = ((uint8
*)&num
)[2];
74 ret
.pint
[2] = ((uint8
*)&num
)[3];
79 /* data should be at least 3 bytes */
80 uint32
_gnutls_read_uint24( const opaque
* data
) {
84 num
.pint
[0] = data
[0];
85 num
.pint
[1] = data
[1];
86 num
.pint
[2] = data
[2];
88 res
= _gnutls_uint24touint32( num
);
89 #ifndef WORDS_BIGENDIAN
90 res
= byteswap32( res
);
95 void _gnutls_write_uint24( uint32 num
, opaque
* data
) {
98 #ifndef WORDS_BIGENDIAN
99 num
= byteswap32( num
);
101 tmp
= _gnutls_uint32touint24( num
);
103 data
[0] = tmp
.pint
[0];
104 data
[1] = tmp
.pint
[1];
105 data
[2] = tmp
.pint
[2];
109 uint32
_gnutls_read_uint32( const opaque
* data
) {
112 memcpy( &res
, data
, sizeof(uint32
));
113 #ifndef WORDS_BIGENDIAN
114 res
= byteswap32( res
);
119 void _gnutls_write_uint32( uint32 num
, opaque
* data
) {
121 #ifndef WORDS_BIGENDIAN
122 num
= byteswap32( num
);
124 memcpy( data
, &num
, sizeof(uint32
));
128 uint16
_gnutls_read_uint16( const opaque
* data
) {
130 memcpy( &res
, data
, sizeof(uint16
));
131 #ifndef WORDS_BIGENDIAN
132 res
= byteswap16( res
);
137 void _gnutls_write_uint16( uint16 num
, opaque
* data
) {
139 #ifndef WORDS_BIGENDIAN
140 num
= byteswap16( num
);
142 memcpy( data
, &num
, sizeof(uint16
));
146 uint32
_gnutls_conv_uint32( uint32 data
) {
147 #ifndef WORDS_BIGENDIAN
148 return byteswap32( data
);
154 uint16
_gnutls_conv_uint16( uint16 data
) {
155 #ifndef WORDS_BIGENDIAN
156 return byteswap16( data
);
162 uint64
_gnutls_conv_uint64( const uint64
* data
) {
164 # ifndef WORDS_BIGENDIAN
165 return byteswap64(*data
);
168 # endif /* WORDS_BIGENDIAN */
172 memcpy( ret
.i
, data
->i
, 8);
174 #endif /* HAVE_UINT64 */
177 uint32
_gnutls_uint64touint32( const uint64
* num
) {
183 #else /* no native uint64 */
185 memcpy( &ret
, &num
->i
[4], 4);
186 # ifndef WORDS_BIGENDIAN
187 ret
= byteswap32(ret
);
189 #endif /* HAVE_UINT64 */