Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2002-2025 Free Software Foundation, Inc. |
3 | | * |
4 | | * This file is part of LIBTASN1. |
5 | | * |
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 |
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, but |
12 | | * 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, see |
18 | | * <https://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #include <config.h> |
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 | 25.1k | { |
35 | 25.1k | size_t str_size = strlen (src); |
36 | 25.1k | size_t dest_size = strlen (dest); |
37 | | |
38 | 25.1k | if (dest_tot_size - dest_size > str_size) |
39 | 24.6k | { |
40 | 24.6k | strcat (dest, src); |
41 | 24.6k | } |
42 | 483 | else |
43 | 483 | { |
44 | 483 | if (dest_tot_size > dest_size) |
45 | 483 | { |
46 | 483 | strncat (dest, src, (dest_tot_size - dest_size) - 1); |
47 | 483 | dest[dest_tot_size - 1] = 0; |
48 | 483 | } |
49 | 483 | } |
50 | 25.1k | } |
51 | | |
52 | | /* Returns the bytes copied (not including the null terminator) */ |
53 | | unsigned int |
54 | | _asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src) |
55 | 95.5k | { |
56 | 95.5k | size_t str_size = strlen (src); |
57 | | |
58 | 95.5k | if (dest_tot_size > str_size) |
59 | 95.4k | { |
60 | 95.4k | strcpy (dest, src); |
61 | 95.4k | return str_size; |
62 | 95.4k | } |
63 | 46 | else |
64 | 46 | { |
65 | 46 | if (dest_tot_size > 0) |
66 | 46 | { |
67 | 46 | str_size = dest_tot_size - 1; |
68 | 46 | memcpy (dest, src, str_size); |
69 | 46 | dest[str_size] = 0; |
70 | 46 | return str_size; |
71 | 46 | } |
72 | 0 | else |
73 | 0 | return 0; |
74 | 46 | } |
75 | 95.5k | } |