/src/curl/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 | 560 | { |
87 | 560 | char *buf = (length < SIZE_MAX) ? curlx_malloc(length + 1) : NULL; |
88 | 560 | if(!buf) |
89 | 0 | return NULL; |
90 | 560 | if(length) { |
91 | 522 | DEBUGASSERT(src); /* must never be NULL */ |
92 | 522 | memcpy(buf, src, length); |
93 | 522 | } |
94 | 560 | buf[length] = 0; |
95 | 560 | return buf; |
96 | 560 | } |