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