/src/CMake/Utilities/cmcurl/lib/curlx/strdup.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #ifdef _WIN32 |
27 | | #include <wchar.h> |
28 | | #endif |
29 | | |
30 | | #include "curlx/strdup.h" |
31 | | |
32 | | #ifdef _WIN32 |
33 | | /*************************************************************************** |
34 | | * |
35 | | * curlx_wcsdup(source) |
36 | | * |
37 | | * Copies the 'source' wchar string to a newly allocated buffer (that is |
38 | | * returned). Used by macro curlx_tcsdup(). |
39 | | * |
40 | | * Returns the new pointer or NULL on failure. |
41 | | * |
42 | | ***************************************************************************/ |
43 | | wchar_t *curlx_wcsdup(const wchar_t *src) |
44 | | { |
45 | | size_t length = wcslen(src); |
46 | | |
47 | | if(length > (SIZE_MAX / sizeof(wchar_t)) - 1) |
48 | | return (wchar_t *)NULL; /* integer overflow */ |
49 | | |
50 | | return (wchar_t *)curlx_memdup(src, (length + 1) * sizeof(wchar_t)); |
51 | | } |
52 | | #endif |
53 | | |
54 | | /*************************************************************************** |
55 | | * |
56 | | * curlx_memdup(source, length) |
57 | | * |
58 | | * Copies the 'source' data to a newly allocated buffer (that is |
59 | | * returned). Copies 'length' bytes. |
60 | | * |
61 | | * Returns the new pointer or NULL on failure. |
62 | | * |
63 | | ***************************************************************************/ |
64 | | void *curlx_memdup(const void *src, size_t length) |
65 | 0 | { |
66 | 0 | void *buffer = curlx_malloc(length); |
67 | 0 | if(!buffer) |
68 | 0 | return NULL; /* fail */ |
69 | | |
70 | 0 | memcpy(buffer, src, length); |
71 | |
|
72 | 0 | return buffer; |
73 | 0 | } |
74 | | |
75 | | /*************************************************************************** |
76 | | * |
77 | | * curlx_memdup0(source, length) |
78 | | * |
79 | | * Copies the 'source' string to a newly allocated buffer (that is returned). |
80 | | * Copies 'length' bytes then adds a null-terminator. |
81 | | * |
82 | | * Returns the new pointer or NULL on failure. |
83 | | * |
84 | | ***************************************************************************/ |
85 | | void *curlx_memdup0(const char *src, size_t length) |
86 | 0 | { |
87 | 0 | char *buf = (length < SIZE_MAX) ? curlx_malloc(length + 1) : NULL; |
88 | 0 | if(!buf) |
89 | 0 | return NULL; |
90 | 0 | if(length) { |
91 | 0 | DEBUGASSERT(src); /* must never be NULL */ |
92 | 0 | memcpy(buf, src, length); |
93 | 0 | } |
94 | 0 | buf[length] = 0; |
95 | 0 | return buf; |
96 | 0 | } |
97 | | |
98 | | #ifdef USE_CURLX_MEMZERO |
99 | | static void *(* const volatile p_curlx_memset)(void *buf, int val, |
100 | | size_t size) = memset; |
101 | | |
102 | | /* Local fallback in case there is no system function to securely zero a memory |
103 | | buffer. */ |
104 | | void curlx_memzero(void *buf, size_t size) |
105 | | { |
106 | | if(buf) |
107 | | p_curlx_memset(buf, 0, size); |
108 | | } |
109 | | #endif |
110 | | |
111 | | /* Free 'buf' after zeroing its content. */ |
112 | | void curlx_freezero(void *buf, size_t size) |
113 | 0 | { |
114 | 0 | if(buf) |
115 | 0 | curlx_memzero(buf, size); |
116 | 0 | curlx_free(buf); |
117 | 0 | } |
118 | | |
119 | | /* Free 'buf' after zeroing its content, where 'buf' is null-terminated. */ |
120 | | void curlx_freezeroz(void *buf) |
121 | 0 | { |
122 | 0 | if(buf) |
123 | 0 | curlx_memzero(buf, strlen(buf)); |
124 | 0 | curlx_free(buf); |
125 | 0 | } |