Use libtasn1 v2.4.
[gnutls.git] / lib / gnutls_num.c
blob68247d8e22affb6eee261f82273f714b786bf3b6
1 /*
2 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2010 Free
3 * Software Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
26 /* This file contains the functions needed for 64 bit integer support in
27 * TLS, and functions which ease the access to TLS vectors (data of given size).
30 #include <gnutls_int.h>
31 #include <gnutls_num.h>
32 #include <gnutls_errors.h>
34 #include <byteswap.h>
36 /* This function will add one to uint64 x.
37 * Returns 0 on success, or -1 if the uint64 max limit
38 * has been reached.
40 int
41 _gnutls_uint64pp (uint64 * x)
43 register int i, y = 0;
45 for (i = 7; i >= 0; i--)
47 y = 0;
48 if (x->i[i] == 0xff)
50 x->i[i] = 0;
51 y = 1;
53 else
54 x->i[i]++;
56 if (y == 0)
57 break;
59 if (y != 0)
60 return -1; /* over 64 bits! WOW */
62 return 0;
65 uint32_t
66 _gnutls_uint24touint32 (uint24 num)
68 uint32_t ret = 0;
70 ((uint8_t *) & ret)[1] = num.pint[0];
71 ((uint8_t *) & ret)[2] = num.pint[1];
72 ((uint8_t *) & ret)[3] = num.pint[2];
73 return ret;
76 uint24
77 _gnutls_uint32touint24 (uint32_t num)
79 uint24 ret;
81 ret.pint[0] = ((uint8_t *) & num)[1];
82 ret.pint[1] = ((uint8_t *) & num)[2];
83 ret.pint[2] = ((uint8_t *) & num)[3];
84 return ret;
88 /* data should be at least 3 bytes */
89 uint32_t
90 _gnutls_read_uint24 (const opaque * data)
92 uint32_t res;
93 uint24 num;
95 num.pint[0] = data[0];
96 num.pint[1] = data[1];
97 num.pint[2] = data[2];
99 res = _gnutls_uint24touint32 (num);
100 #ifndef WORDS_BIGENDIAN
101 res = bswap_32 (res);
102 #endif
103 return res;
106 void
107 _gnutls_write_uint24 (uint32_t num, opaque * data)
109 uint24 tmp;
111 #ifndef WORDS_BIGENDIAN
112 num = bswap_32 (num);
113 #endif
114 tmp = _gnutls_uint32touint24 (num);
116 data[0] = tmp.pint[0];
117 data[1] = tmp.pint[1];
118 data[2] = tmp.pint[2];
121 uint32_t
122 _gnutls_read_uint32 (const opaque * data)
124 uint32_t res;
126 memcpy (&res, data, sizeof (uint32_t));
127 #ifndef WORDS_BIGENDIAN
128 res = bswap_32 (res);
129 #endif
130 return res;
133 void
134 _gnutls_write_uint32 (uint32_t num, opaque * data)
137 #ifndef WORDS_BIGENDIAN
138 num = bswap_32 (num);
139 #endif
140 memcpy (data, &num, sizeof (uint32_t));
143 uint16_t
144 _gnutls_read_uint16 (const opaque * data)
146 uint16_t res;
147 memcpy (&res, data, sizeof (uint16_t));
148 #ifndef WORDS_BIGENDIAN
149 res = bswap_16 (res);
150 #endif
151 return res;
154 void
155 _gnutls_write_uint16 (uint16_t num, opaque * data)
158 #ifndef WORDS_BIGENDIAN
159 num = bswap_16 (num);
160 #endif
161 memcpy (data, &num, sizeof (uint16_t));
164 uint32_t
165 _gnutls_conv_uint32 (uint32_t data)
167 #ifndef WORDS_BIGENDIAN
168 return bswap_32 (data);
169 #else
170 return data;
171 #endif
174 uint16_t
175 _gnutls_conv_uint16 (uint16_t data)
177 #ifndef WORDS_BIGENDIAN
178 return bswap_16 (data);
179 #else
180 return data;
181 #endif
184 uint32_t
185 _gnutls_uint64touint32 (const uint64 * num)
187 uint32_t ret;
189 memcpy (&ret, &num->i[4], 4);
190 #ifndef WORDS_BIGENDIAN
191 ret = bswap_32 (ret);
192 #endif
194 return ret;