summaryrefslogtreecommitdiff
path: root/lib/gnutls_num.c
blob: 1abd35aabf72452ccaa4f8525800799c1c5911dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 *      Copyright (C) 2000,2001 Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 *  The GNUTLS library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public   
 *  License as published by the Free Software Foundation; either 
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 */

#include <gnutls_int.h>
#include <gnutls_num.h>
#include <gnutls_errors.h>


#ifndef HAVE_UINT64

/* This function will set the uint64 x to zero 
 */
int uint64zero( uint64 *x) {

	memset( x->i, 0, 8);
	return 0;
}

/* This function will add one to uint64 x.
 * Returns 0 on success, or -1 if the uint64 max limit
 * has been reached.
 */
int uint64pp( uint64 *x) {
register int i, y=0;

	for (i=7;i>=0;i--) {
		y = 0;
		if ( x->i[i] == 0xff) {
			x->i[i] = 0;
			y = 1;
		} else x->i[i]++;

		if (y==0) break;
	}
	if (y != 0) return -1; /* over 64 bits! WOW */

	return 0;
}

#endif /* HAVE_UINT64 */

uint32 uint24touint32( uint24 num) {
uint32 ret=0;

	((uint8*)&ret)[1] = num.pint[0];
	((uint8*)&ret)[2] = num.pint[1];
	((uint8*)&ret)[3] = num.pint[2];
	return ret;
}

uint24 uint32touint24( uint32 num) {
uint24 ret;

	ret.pint[0] = ((uint8*)&num)[1];
	ret.pint[1] = ((uint8*)&num)[2];
	ret.pint[2] = ((uint8*)&num)[3];
	return ret;

}

/* data should be at least 3 bytes */
uint32 READuint24( const opaque* data) {
uint32 res;
uint24 num;
	
	num.pint[0] = data[0];
	num.pint[1] = data[1];
	num.pint[2] = data[2];

	res = uint24touint32( num);
#ifndef WORDS_BIGENDIAN
	res = byteswap32( res);
#endif
return res;
}

void WRITEuint24( uint32 num, opaque* data) {
uint24 tmp;
	
#ifndef WORDS_BIGENDIAN
	num = byteswap32( num);
#endif
	tmp = uint32touint24( num);

	data[0] = tmp.pint[0];
	data[1] = tmp.pint[1];
	data[2] = tmp.pint[2];
	return;
}

uint32 READuint32( const opaque* data) {
uint32 res;

	memcpy( &res, data, sizeof(uint32));
#ifndef WORDS_BIGENDIAN
	res = byteswap32( res);
#endif
return res;
}

void WRITEuint32( uint32 num, opaque* data) {

#ifndef WORDS_BIGENDIAN
	num = byteswap32( num);
#endif
	memcpy( data, &num, sizeof(uint32));
	return;
}

uint16 READuint16( const opaque* data) {
uint16 res;
	memcpy( &res, data, sizeof(uint16));
#ifndef WORDS_BIGENDIAN
	res = byteswap16( res);
#endif
return res;
}

void WRITEuint16( uint16 num, opaque* data) {

#ifndef WORDS_BIGENDIAN
	num = byteswap16( num);
#endif
	memcpy( data, &num, sizeof(uint16));
	return;
}

uint32 CONVuint32( uint32 data) {
#ifndef WORDS_BIGENDIAN
	return byteswap32( data);
#else
	return data;
#endif
}

uint16 CONVuint16( uint16 data) {
#ifndef WORDS_BIGENDIAN
	return byteswap16( data);
#else
	return data;
#endif
}

uint64 CONVuint64( const uint64* data) {
#ifdef HAVE_UINT64
# ifndef WORDS_BIGENDIAN
	return byteswap64(*data);
# else
	return *data;
# endif /* WORDS_BIGENDIAN */
#else
	uint64 ret;

	memcpy( ret.i, data->i, 8);
	return ret;
#endif /* HAVE_UINT64 */
}

uint32 uint64touint32( const uint64* num) {
uint32 ret;

#ifdef HAVE_UINT64
	ret = (uint32) *num;

#else /* no native uint64 */

	memcpy( &ret, &num->i[4], 4);
# ifndef WORDS_BIGENDIAN
	ret = byteswap32(ret);
# endif
#endif /* HAVE_UINT64 */

 

 return ret;
}