*** empty log message ***
[gnutls.git] / lib / gnutls_num.c
blob8f165353d399742bfea1a4af93d961d2d7fa0b10
1 /*
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>
27 #ifndef HAVE_UINT64
29 /* This function will set the uint64 x to zero
31 int _gnutls_uint64zero( uint64 *x) {
33 memset( x->i, 0, 8);
34 return 0;
37 /* This function will add one to uint64 x.
38 * Returns 0 on success, or -1 if the uint64 max limit
39 * has been reached.
41 int _gnutls_uint64pp( uint64 *x) {
42 register int i, y=0;
44 for (i=7;i>=0;i--) {
45 y = 0;
46 if ( x->i[i] == 0xff) {
47 x->i[i] = 0;
48 y = 1;
49 } else x->i[i]++;
51 if (y==0) break;
53 if (y != 0) return -1; /* over 64 bits! WOW */
55 return 0;
58 #endif /* HAVE_UINT64 */
60 uint32 _gnutls_uint24touint32( uint24 num) {
61 uint32 ret=0;
63 ((uint8*)&ret)[1] = num.pint[0];
64 ((uint8*)&ret)[2] = num.pint[1];
65 ((uint8*)&ret)[3] = num.pint[2];
66 return ret;
69 uint24 _gnutls_uint32touint24( uint32 num) {
70 uint24 ret;
72 ret.pint[0] = ((uint8*)&num)[1];
73 ret.pint[1] = ((uint8*)&num)[2];
74 ret.pint[2] = ((uint8*)&num)[3];
75 return ret;
79 /* data should be at least 3 bytes */
80 uint32 _gnutls_read_uint24( const opaque* data) {
81 uint32 res;
82 uint24 num;
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);
91 #endif
92 return res;
95 void _gnutls_write_uint24( uint32 num, opaque* data) {
96 uint24 tmp;
98 #ifndef WORDS_BIGENDIAN
99 num = byteswap32( num);
100 #endif
101 tmp = _gnutls_uint32touint24( num);
103 data[0] = tmp.pint[0];
104 data[1] = tmp.pint[1];
105 data[2] = tmp.pint[2];
106 return;
109 uint32 _gnutls_read_uint32( const opaque* data) {
110 uint32 res;
112 memcpy( &res, data, sizeof(uint32));
113 #ifndef WORDS_BIGENDIAN
114 res = byteswap32( res);
115 #endif
116 return res;
119 void _gnutls_write_uint32( uint32 num, opaque* data) {
121 #ifndef WORDS_BIGENDIAN
122 num = byteswap32( num);
123 #endif
124 memcpy( data, &num, sizeof(uint32));
125 return;
128 uint16 _gnutls_read_uint16( const opaque* data) {
129 uint16 res;
130 memcpy( &res, data, sizeof(uint16));
131 #ifndef WORDS_BIGENDIAN
132 res = byteswap16( res);
133 #endif
134 return res;
137 void _gnutls_write_uint16( uint16 num, opaque* data) {
139 #ifndef WORDS_BIGENDIAN
140 num = byteswap16( num);
141 #endif
142 memcpy( data, &num, sizeof(uint16));
143 return;
146 uint32 _gnutls_conv_uint32( uint32 data) {
147 #ifndef WORDS_BIGENDIAN
148 return byteswap32( data);
149 #else
150 return data;
151 #endif
154 uint16 _gnutls_conv_uint16( uint16 data) {
155 #ifndef WORDS_BIGENDIAN
156 return byteswap16( data);
157 #else
158 return data;
159 #endif
162 uint64 _gnutls_conv_uint64( const uint64* data) {
163 #ifdef HAVE_UINT64
164 # ifndef WORDS_BIGENDIAN
165 return byteswap64(*data);
166 # else
167 return *data;
168 # endif /* WORDS_BIGENDIAN */
169 #else
170 uint64 ret;
172 memcpy( ret.i, data->i, 8);
173 return ret;
174 #endif /* HAVE_UINT64 */
177 uint32 _gnutls_uint64touint32( const uint64* num) {
178 uint32 ret;
180 #ifdef HAVE_UINT64
181 ret = (uint32) *num;
183 #else /* no native uint64 */
185 memcpy( &ret, &num->i[4], 4);
186 # ifndef WORDS_BIGENDIAN
187 ret = byteswap32(ret);
188 # endif
189 #endif /* HAVE_UINT64 */
191 return ret;