Check whether `-fgnu89-inline' is supported before using it.
[gnutls.git] / lib / gnutls_num.c
blobda7be4b283b49d93691325feb38fd2a527e5679f
1 /*
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,
21 * USA
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
35 * has been reached.
37 int
38 _gnutls_uint64pp (uint64 * x)
40 register int i, y = 0;
42 for (i = 7; i >= 0; i--)
44 y = 0;
45 if (x->i[i] == 0xff)
47 x->i[i] = 0;
48 y = 1;
50 else
51 x->i[i]++;
53 if (y == 0)
54 break;
56 if (y != 0)
57 return -1; /* over 64 bits! WOW */
59 return 0;
62 uint32_t
63 _gnutls_uint24touint32 (uint24 num)
65 uint32_t ret = 0;
67 ((uint8_t *) & ret)[1] = num.pint[0];
68 ((uint8_t *) & ret)[2] = num.pint[1];
69 ((uint8_t *) & ret)[3] = num.pint[2];
70 return ret;
73 uint24
74 _gnutls_uint32touint24 (uint32_t num)
76 uint24 ret;
78 ret.pint[0] = ((uint8_t *) & num)[1];
79 ret.pint[1] = ((uint8_t *) & num)[2];
80 ret.pint[2] = ((uint8_t *) & num)[3];
81 return ret;
85 /* data should be at least 3 bytes */
86 uint32_t
87 _gnutls_read_uint24 (const opaque * data)
89 uint32_t res;
90 uint24 num;
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);
99 #endif
100 return res;
103 void
104 _gnutls_write_uint24 (uint32_t num, opaque * data)
106 uint24 tmp;
108 #ifndef WORDS_BIGENDIAN
109 num = byteswap32 (num);
110 #endif
111 tmp = _gnutls_uint32touint24 (num);
113 data[0] = tmp.pint[0];
114 data[1] = tmp.pint[1];
115 data[2] = tmp.pint[2];
118 uint32_t
119 _gnutls_read_uint32 (const opaque * data)
121 uint32_t res;
123 memcpy (&res, data, sizeof (uint32_t));
124 #ifndef WORDS_BIGENDIAN
125 res = byteswap32 (res);
126 #endif
127 return res;
130 void
131 _gnutls_write_uint32 (uint32_t num, opaque * data)
134 #ifndef WORDS_BIGENDIAN
135 num = byteswap32 (num);
136 #endif
137 memcpy (data, &num, sizeof (uint32_t));
140 uint16_t
141 _gnutls_read_uint16 (const opaque * data)
143 uint16_t res;
144 memcpy (&res, data, sizeof (uint16_t));
145 #ifndef WORDS_BIGENDIAN
146 res = byteswap16 (res);
147 #endif
148 return res;
151 void
152 _gnutls_write_uint16 (uint16_t num, opaque * data)
155 #ifndef WORDS_BIGENDIAN
156 num = byteswap16 (num);
157 #endif
158 memcpy (data, &num, sizeof (uint16_t));
161 uint32_t
162 _gnutls_conv_uint32 (uint32_t data)
164 #ifndef WORDS_BIGENDIAN
165 return byteswap32 (data);
166 #else
167 return data;
168 #endif
171 uint16_t
172 _gnutls_conv_uint16 (uint16_t data)
174 #ifndef WORDS_BIGENDIAN
175 return byteswap16 (data);
176 #else
177 return data;
178 #endif
181 uint32_t
182 _gnutls_uint64touint32 (const uint64 * num)
184 uint32_t ret;
186 memcpy (&ret, &num->i[4], 4);
187 #ifndef WORDS_BIGENDIAN
188 ret = byteswap32 (ret);
189 #endif
191 return ret;