Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 1 | /* |
Simon Josefsson | baed41e | 2014-04-22 21:02:23 +0200 | [diff] [blame^] | 2 | * Copyright (C) 2002-2014 Free Software Foundation, Inc. |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 3 | * |
Simon Josefsson | fb915d6 | 2006-02-09 12:53:24 +0000 | [diff] [blame] | 4 | * This file is part of LIBTASN1. |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 5 | * |
Simon Josefsson | fb915d6 | 2006-02-09 12:53:24 +0000 | [diff] [blame] | 6 | * The LIBTASN1 library is free software; you can redistribute it |
| 7 | * and/or modify it under the terms of the GNU Lesser General Public |
| 8 | * License as published by the Free Software Foundation; either |
Nikos Mavrogiannopoulos | e5a12ed | 2002-06-14 18:03:23 +0000 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 10 | * |
Simon Josefsson | fb915d6 | 2006-02-09 12:53:24 +0000 | [diff] [blame] | 11 | * This library is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
Nikos Mavrogiannopoulos | e5a12ed | 2002-06-14 18:03:23 +0000 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * Lesser General Public License for more details. |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 15 | * |
Nikos Mavrogiannopoulos | e5a12ed | 2002-06-14 18:03:23 +0000 | [diff] [blame] | 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 |
Simon Josefsson | fb915d6 | 2006-02-09 12:53:24 +0000 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 | * 02110-1301, USA |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Nikos Mavrogiannopoulos | 02477c2 | 2002-12-28 17:22:22 +0000 | [diff] [blame] | 22 | #include <int.h> |
Simon Josefsson | 7a345f9 | 2008-11-17 19:03:30 +0100 | [diff] [blame] | 23 | #include "gstr.h" |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 24 | |
| 25 | /* These function are like strcat, strcpy. They only |
Simon Josefsson | b8e7b4e | 2004-10-29 21:43:58 +0000 | [diff] [blame] | 26 | * do bounds checking (they shouldn't cause buffer overruns), |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 27 | * and they always produce null terminated strings. |
| 28 | * |
| 29 | * They should be used only with null terminated strings. |
| 30 | */ |
Nikos Mavrogiannopoulos | fbaec18 | 2006-04-26 19:32:00 +0000 | [diff] [blame] | 31 | void |
| 32 | _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src) |
| 33 | { |
| 34 | size_t str_size = strlen (src); |
| 35 | size_t dest_size = strlen (dest); |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 36 | |
Nikos Mavrogiannopoulos | fbaec18 | 2006-04-26 19:32:00 +0000 | [diff] [blame] | 37 | if (dest_tot_size - dest_size > str_size) |
| 38 | { |
| 39 | strcat (dest, src); |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | if (dest_tot_size - dest_size > 0) |
| 44 | { |
| 45 | strncat (dest, src, (dest_tot_size - dest_size) - 1); |
| 46 | dest[dest_tot_size - 1] = 0; |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 47 | } |
Nikos Mavrogiannopoulos | fbaec18 | 2006-04-26 19:32:00 +0000 | [diff] [blame] | 48 | } |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Nikos Mavrogiannopoulos | 80863f7 | 2012-09-23 12:55:40 +0200 | [diff] [blame] | 51 | /* Returns the bytes copied (not including the null terminator) */ |
| 52 | unsigned int |
Nikos Mavrogiannopoulos | fbaec18 | 2006-04-26 19:32:00 +0000 | [diff] [blame] | 53 | _asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src) |
| 54 | { |
| 55 | size_t str_size = strlen (src); |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 56 | |
Nikos Mavrogiannopoulos | fbaec18 | 2006-04-26 19:32:00 +0000 | [diff] [blame] | 57 | if (dest_tot_size > str_size) |
| 58 | { |
| 59 | strcpy (dest, src); |
Nikos Mavrogiannopoulos | 80863f7 | 2012-09-23 12:55:40 +0200 | [diff] [blame] | 60 | return str_size; |
Nikos Mavrogiannopoulos | fbaec18 | 2006-04-26 19:32:00 +0000 | [diff] [blame] | 61 | } |
| 62 | else |
| 63 | { |
| 64 | if (dest_tot_size > 0) |
| 65 | { |
Nikos Mavrogiannopoulos | 80863f7 | 2012-09-23 12:55:40 +0200 | [diff] [blame] | 66 | str_size = dest_tot_size - 1; |
| 67 | memcpy (dest, src, str_size); |
| 68 | dest[str_size] = 0; |
| 69 | return str_size; |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 70 | } |
Simon Josefsson | 36f03da | 2013-03-24 10:28:25 +0100 | [diff] [blame] | 71 | else |
| 72 | return 0; |
Nikos Mavrogiannopoulos | fbaec18 | 2006-04-26 19:32:00 +0000 | [diff] [blame] | 73 | } |
Nikos Mavrogiannopoulos | c379ce7 | 2002-04-05 19:57:55 +0000 | [diff] [blame] | 74 | } |