blob: e91a3a151c0d6dbe7447748bbca8b9d6aa9914f7 [file] [log] [blame]
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +00001/*
Simon Josefssonbaed41e2014-04-22 21:02:23 +02002 * Copyright (C) 2002-2014 Free Software Foundation, Inc.
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +00003 *
Simon Josefssonfb915d62006-02-09 12:53:24 +00004 * This file is part of LIBTASN1.
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +00005 *
Simon Josefssonfb915d62006-02-09 12:53:24 +00006 * 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 Mavrogiannopoulose5a12ed2002-06-14 18:03:23 +00009 * version 2.1 of the License, or (at your option) any later version.
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000010 *
Simon Josefssonfb915d62006-02-09 12:53:24 +000011 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
Nikos Mavrogiannopoulose5a12ed2002-06-14 18:03:23 +000013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000015 *
Nikos Mavrogiannopoulose5a12ed2002-06-14 18:03:23 +000016 * 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 Josefssonfb915d62006-02-09 12:53:24 +000018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000020 */
21
Nikos Mavrogiannopoulos02477c22002-12-28 17:22:22 +000022#include <int.h>
Simon Josefsson7a345f92008-11-17 19:03:30 +010023#include "gstr.h"
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000024
25/* These function are like strcat, strcpy. They only
Simon Josefssonb8e7b4e2004-10-29 21:43:58 +000026 * do bounds checking (they shouldn't cause buffer overruns),
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000027 * and they always produce null terminated strings.
28 *
29 * They should be used only with null terminated strings.
30 */
Nikos Mavrogiannopoulosfbaec182006-04-26 19:32:00 +000031void
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 Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000036
Nikos Mavrogiannopoulosfbaec182006-04-26 19:32:00 +000037 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 Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000047 }
Nikos Mavrogiannopoulosfbaec182006-04-26 19:32:00 +000048 }
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000049}
50
Nikos Mavrogiannopoulos80863f72012-09-23 12:55:40 +020051/* Returns the bytes copied (not including the null terminator) */
52unsigned int
Nikos Mavrogiannopoulosfbaec182006-04-26 19:32:00 +000053_asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src)
54{
55 size_t str_size = strlen (src);
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000056
Nikos Mavrogiannopoulosfbaec182006-04-26 19:32:00 +000057 if (dest_tot_size > str_size)
58 {
59 strcpy (dest, src);
Nikos Mavrogiannopoulos80863f72012-09-23 12:55:40 +020060 return str_size;
Nikos Mavrogiannopoulosfbaec182006-04-26 19:32:00 +000061 }
62 else
63 {
64 if (dest_tot_size > 0)
65 {
Nikos Mavrogiannopoulos80863f72012-09-23 12:55:40 +020066 str_size = dest_tot_size - 1;
67 memcpy (dest, src, str_size);
68 dest[str_size] = 0;
69 return str_size;
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000070 }
Simon Josefsson36f03da2013-03-24 10:28:25 +010071 else
72 return 0;
Nikos Mavrogiannopoulosfbaec182006-04-26 19:32:00 +000073 }
Nikos Mavrogiannopoulosc379ce72002-04-05 19:57:55 +000074}