xref: /PHP-7.4/main/strlcpy.c (revision 92ac598a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) The PHP Group                                          |
6   +----------------------------------------------------------------------+
7   | This source file is subject to version 3.01 of the PHP license,      |
8   | that is bundled with this package in the file LICENSE, and is        |
9   | available through the world-wide-web at the following url:           |
10   | http://www.php.net/license/3_01.txt                                  |
11   | If you did not receive a copy of the PHP license and are unable to   |
12   | obtain it through the world-wide-web, please send a note to          |
13   | license@php.net so we can mail you a copy immediately.               |
14   +----------------------------------------------------------------------+
15   | Author:                                                              |
16   +----------------------------------------------------------------------+
17 */
18 
19 #include "php.h"
20 
21 #ifdef USE_STRLCPY_PHP_IMPL
22 
23 /*     $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $    */
24 
25 /*
26  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  *    notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  *    notice, this list of conditions and the following disclaimer in the
36  *    documentation and/or other materials provided with the distribution.
37  * 3. The name of the author may not be used to endorse or promote products
38  *    derived from this software without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
41  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
42  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
43  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
46  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  */
51 
52 #if defined(LIBC_SCCS) && !defined(lint)
53 static char *rcsid = "$OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $";
54 #endif /* LIBC_SCCS and not lint */
55 
56 #include <sys/types.h>
57 #include <string.h>
58 
59 /*
60  * Copy src to string dst of size siz.  At most siz-1 characters
61  * will be copied.  Always NUL terminates (unless siz == 0).
62  * Returns strlen(src); if retval >= siz, truncation occurred.
63  */
php_strlcpy(dst,src,siz)64 PHPAPI size_t php_strlcpy(dst, src, siz)
65 	char *dst;
66 	const char *src;
67 	size_t siz;
68 {
69 	const char *s = src;
70 	size_t n = siz;
71 
72 	/* Copy as many bytes as will fit */
73 	if (n != 0) {
74 		while (--n != 0) {
75 			if ((*dst++ = *src++) == 0)
76 				break;
77 		}
78 	}
79 
80 	/* Not enough room in dst, add NUL and traverse rest of src */
81 	if (n == 0) {
82 		if (siz != 0)
83 			*dst = '\0';		/* NUL-terminate dst */
84 		while (*src++)
85 			;
86 	}
87 
88 	/*
89 	 * Cast pointers to unsigned type before calculation, to avoid signed
90 	 * overflow when the string ends where the MSB has changed.
91 	 * Return value does not include NUL.
92 	 */
93 	return((uintptr_t)src - (uintptr_t)s - 1);
94 }
95 
96 #endif /* !HAVE_STRLCPY */
97