gstr.c

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2006 Free Software Foundation
00003  *      Copyright (C) 2002 Nikos Mavroyanopoulos
00004  *
00005  * This file is part of LIBTASN1.
00006  *
00007  * The LIBTASN1 library is free software; you can redistribute it
00008  * and/or modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00020  * 02110-1301, USA
00021  */
00022 
00023 #include <int.h>
00024 
00025 /* These function are like strcat, strcpy. They only
00026  * do bounds checking (they shouldn't cause buffer overruns),
00027  * and they always produce null terminated strings.
00028  *
00029  * They should be used only with null terminated strings.
00030  */
00031 void
00032 MHD__asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
00033 {
00034   size_t str_size = strlen (src);
00035   size_t dest_size = strlen (dest);
00036 
00037   if (dest_tot_size - dest_size > str_size)
00038     {
00039       strcat (dest, src);
00040     }
00041   else
00042     {
00043       if (dest_tot_size - dest_size > 0)
00044         {
00045           strncat (dest, src, (dest_tot_size - dest_size) - 1);
00046           dest[dest_tot_size - 1] = 0;
00047         }
00048     }
00049 }
00050 
00051 void
00052 MHD__asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src)
00053 {
00054   size_t str_size = strlen (src);
00055 
00056   if (dest_tot_size > str_size)
00057     {
00058       strcpy (dest, src);
00059     }
00060   else
00061     {
00062       if (dest_tot_size > 0)
00063         {
00064           strncpy (dest, src, (dest_tot_size) - 1);
00065           dest[dest_tot_size - 1] = 0;
00066         }
00067     }
00068 }

Generated on Fri Feb 27 18:18:39 2009 for GNU libmicrohttpd by  doxygen 1.5.8