gnutls_num.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation
00003  *
00004  * Author: Nikos Mavrogiannopoulos
00005  *
00006  * This file is part of GNUTLS.
00007  *
00008  * The GNUTLS library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00021  * USA
00022  *
00023  */
00024 
00025 /* This file contains the functions needed for 64 bit integer support in
00026  * TLS, and functions which ease the access to TLS vectors (data of given size).
00027  */
00028 
00029 #include <gnutls_int.h>
00030 #include <gnutls_num.h>
00031 #include <gnutls_errors.h>
00032 
00033 /* This function will add one to uint64 x.
00034  * Returns 0 on success, or -1 if the uint64 max limit
00035  * has been reached.
00036  */
00037 int
00038 MHD_gtls_uint64pp (uint64 * x)
00039 {
00040   register int i, y = 0;
00041 
00042   for (i = 7; i >= 0; i--)
00043     {
00044       y = 0;
00045       if (x->i[i] == 0xff)
00046         {
00047           x->i[i] = 0;
00048           y = 1;
00049         }
00050       else
00051         x->i[i]++;
00052 
00053       if (y == 0)
00054         break;
00055     }
00056   if (y != 0)
00057     return -1;                  /* over 64 bits! WOW */
00058 
00059   return 0;
00060 }
00061 
00062 uint32_t
00063 MHD_gtls_uint24touint32 (uint24 num)
00064 {
00065   uint32_t ret = 0;
00066 
00067   ((uint8_t *) & ret)[1] = num.pint[0];
00068   ((uint8_t *) & ret)[2] = num.pint[1];
00069   ((uint8_t *) & ret)[3] = num.pint[2];
00070   return ret;
00071 }
00072 
00073 uint24
00074 MHD_gtls_uint32touint24 (uint32_t num)
00075 {
00076   uint24 ret;
00077 
00078   ret.pint[0] = ((uint8_t *) & num)[1];
00079   ret.pint[1] = ((uint8_t *) & num)[2];
00080   ret.pint[2] = ((uint8_t *) & num)[3];
00081   return ret;
00082 
00083 }
00084 
00085 /* data should be at least 3 bytes */
00086 uint32_t
00087 MHD_gtls_read_uint24 (const opaque * data)
00088 {
00089   uint32_t res;
00090   uint24 num;
00091 
00092   num.pint[0] = data[0];
00093   num.pint[1] = data[1];
00094   num.pint[2] = data[2];
00095 
00096   res = MHD_gtls_uint24touint32 (num);
00097 #ifndef WORDS_BIGENDIAN
00098   res = byteswap32 (res);
00099 #endif
00100   return res;
00101 }
00102 
00103 void
00104 MHD_gtls_write_uint24 (uint32_t num, opaque * data)
00105 {
00106   uint24 tmp;
00107 
00108 #ifndef WORDS_BIGENDIAN
00109   num = byteswap32 (num);
00110 #endif
00111   tmp = MHD_gtls_uint32touint24 (num);
00112 
00113   data[0] = tmp.pint[0];
00114   data[1] = tmp.pint[1];
00115   data[2] = tmp.pint[2];
00116 }
00117 
00118 uint32_t
00119 MHD_gtls_read_uint32 (const opaque * data)
00120 {
00121   uint32_t res;
00122 
00123   memcpy (&res, data, sizeof (uint32_t));
00124 #ifndef WORDS_BIGENDIAN
00125   res = byteswap32 (res);
00126 #endif
00127   return res;
00128 }
00129 
00130 void
00131 MHD_gtls_write_uint32 (uint32_t num, opaque * data)
00132 {
00133 
00134 #ifndef WORDS_BIGENDIAN
00135   num = byteswap32 (num);
00136 #endif
00137   memcpy (data, &num, sizeof (uint32_t));
00138 }
00139 
00140 uint16_t
00141 MHD_gtls_read_uint16 (const opaque * data)
00142 {
00143   uint16_t res;
00144   memcpy (&res, data, sizeof (uint16_t));
00145 #ifndef WORDS_BIGENDIAN
00146   res = byteswap16 (res);
00147 #endif
00148   return res;
00149 }
00150 
00151 void
00152 MHD_gtls_write_uint16 (uint16_t num, opaque * data)
00153 {
00154 
00155 #ifndef WORDS_BIGENDIAN
00156   num = byteswap16 (num);
00157 #endif
00158   memcpy (data, &num, sizeof (uint16_t));
00159 }
00160 
00161 uint32_t
00162 MHD_gtls_conv_uint32 (uint32_t data)
00163 {
00164 #ifndef WORDS_BIGENDIAN
00165   return byteswap32 (data);
00166 #else
00167   return data;
00168 #endif
00169 }
00170 
00171 uint16_t
00172 MHD_gtls_conv_uint16 (uint16_t data)
00173 {
00174 #ifndef WORDS_BIGENDIAN
00175   return byteswap16 (data);
00176 #else
00177   return data;
00178 #endif
00179 }
00180 
00181 uint32_t
00182 MHD_gtls_uint64touint32 (const uint64 * num)
00183 {
00184   uint32_t ret;
00185 
00186   memcpy (&ret, &num->i[4], 4);
00187 #ifndef WORDS_BIGENDIAN
00188   ret = byteswap32 (ret);
00189 #endif
00190 
00191   return ret;
00192 }

Generated on Fri Feb 27 18:31:19 2009 for GNU libmicrohttpd by  doxygen 1.5.7.1