Initialize Tizen 2.3
[external/libtasn1.git] / lib / gstr.c
1 /*
2  * Copyright (C) 2002, 2006, 2007, 2008, 2009, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * This file is part of LIBTASN1.
6  *
7  * The LIBTASN1 library is free software; you can redistribute it
8  * and/or modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA
21  */
22
23 #include <int.h>
24 #include "gstr.h"
25
26 /* These function are like strcat, strcpy. They only
27  * do bounds checking (they shouldn't cause buffer overruns),
28  * and they always produce null terminated strings.
29  *
30  * They should be used only with null terminated strings.
31  */
32 void
33 _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
34 {
35   size_t str_size = strlen (src);
36   size_t dest_size = strlen (dest);
37
38   if (dest_tot_size - dest_size > str_size)
39     {
40       strcat (dest, src);
41     }
42   else
43     {
44       if (dest_tot_size - dest_size > 0)
45         {
46           strncat (dest, src, (dest_tot_size - dest_size) - 1);
47           dest[dest_tot_size - 1] = 0;
48         }
49     }
50 }
51
52 void
53 _asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src)
54 {
55   size_t str_size = strlen (src);
56
57   if (dest_tot_size > str_size)
58     {
59       strcpy (dest, src);
60     }
61   else
62     {
63       if (dest_tot_size > 0)
64         {
65           strncpy (dest, src, (dest_tot_size) - 1);
66           dest[dest_tot_size - 1] = 0;
67         }
68     }
69 }