gnutls_str.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2002, 2004, 2005, 2007  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 #include <gnutls_int.h>
00026 #include <gnutls_errors.h>
00027 #include <gnutls_num.h>
00028 #include <gnutls_str.h>
00029 
00030 /* These function are like strcat, strcpy. They only
00031  * do bound checking (they shouldn't cause buffer overruns),
00032  * and they always produce null terminated strings.
00033  *
00034  * They should be used only with null terminated strings.
00035  */
00036 void
00037 MHD_gtls_str_cat (char *dest, size_t dest_tot_size, const char *src)
00038 {
00039   size_t str_size = strlen (src);
00040   size_t dest_size = strlen (dest);
00041 
00042   if (dest_tot_size - dest_size > str_size)
00043     {
00044       strcat (dest, src);
00045     }
00046   else
00047     {
00048       if (dest_tot_size - dest_size > 0)
00049         {
00050           strncat (dest, src, (dest_tot_size - dest_size) - 1);
00051           dest[dest_tot_size - 1] = 0;
00052         }
00053     }
00054 }
00055 
00056 void
00057 MHD_gtls_str_cpy (char *dest, size_t dest_tot_size, const char *src)
00058 {
00059   size_t str_size = strlen (src);
00060 
00061   if (dest_tot_size > str_size)
00062     {
00063       strcpy (dest, src);
00064     }
00065   else
00066     {
00067       if (dest_tot_size > 0)
00068         {
00069           strncpy (dest, src, (dest_tot_size) - 1);
00070           dest[dest_tot_size - 1] = 0;
00071         }
00072     }
00073 }
00074 
00075 void
00076 MHD_gtls_string_init (MHD_gtls_string * str,
00077                       MHD_gnutls_alloc_function alloc_func,
00078                       MHD_gnutls_realloc_function realloc_func,
00079                       MHD_gnutls_free_function free_func)
00080 {
00081   str->data = NULL;
00082   str->max_length = 0;
00083   str->length = 0;
00084 
00085   str->alloc_func = alloc_func;
00086   str->free_func = free_func;
00087   str->realloc_func = realloc_func;
00088 }
00089 
00090 void
00091 MHD_gtls_string_clear (MHD_gtls_string * str)
00092 {
00093   if (str == NULL || str->data == NULL)
00094     return;
00095   str->free_func (str->data);
00096 
00097   str->data = NULL;
00098   str->max_length = 0;
00099   str->length = 0;
00100 }
00101 
00102 #define MIN_CHUNK 256
00103 
00104 
00105 int
00106 MHD_gtls_string_append_data (MHD_gtls_string * dest,
00107                              const void *data, size_t data_size)
00108 {
00109   size_t tot_len = data_size + dest->length;
00110 
00111   if (dest->max_length >= tot_len)
00112     {
00113       memcpy (&dest->data[dest->length], data, data_size);
00114       dest->length = tot_len;
00115 
00116       return tot_len;
00117     }
00118   else
00119     {
00120       size_t new_len =
00121         MAX (data_size, MIN_CHUNK) + MAX (dest->max_length, MIN_CHUNK);
00122       dest->data = dest->realloc_func (dest->data, new_len);
00123       if (dest->data == NULL)
00124         {
00125           MHD_gnutls_assert ();
00126           return GNUTLS_E_MEMORY_ERROR;
00127         }
00128       dest->max_length = new_len;
00129 
00130       memcpy (&dest->data[dest->length], data, data_size);
00131       dest->length = tot_len;
00132 
00133       return tot_len;
00134     }
00135 }
00136 
00137 /* Converts the given string (old) to hex. A buffer must be provided
00138  * to hold the new hex string. The new string will be null terminated.
00139  * If the buffer does not have enough space to hold the string, a
00140  * truncated hex string is returned (always null terminated).
00141  */
00142 char *
00143 MHD_gtls_bin2hex (const void *_old,
00144                   size_t oldlen, char *buffer, size_t buffer_size)
00145 {
00146   unsigned int i, j;
00147   const opaque *old = _old;
00148 
00149   for (i = j = 0; i < oldlen && j + 2 < buffer_size; j += 2)
00150     {
00151       sprintf (&buffer[j], "%.2x", old[i]);
00152       i++;
00153     }
00154   buffer[j] = '\0';
00155 
00156   return buffer;
00157 }
Generated by  doxygen 1.6.2-20100208