xref: /PHP-7.2/main/strlcpy.c (revision 7a7ec01a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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 /* $Id$ */
20 
21 #include "php.h"
22 
23 #ifdef USE_STRLCPY_PHP_IMPL
24 
25 /*     $OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $    */
26 
27 /*
28  * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. The name of the author may not be used to endorse or promote products
40  *    derived from this software without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
43  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
44  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
45  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
46  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
47  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
48  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
49  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
50  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
51  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52  */
53 
54 #if defined(LIBC_SCCS) && !defined(lint)
55 static char *rcsid = "$OpenBSD: strlcpy.c,v 1.15 2016/10/16 17:37:39 dtucker Exp $";
56 #endif /* LIBC_SCCS and not lint */
57 
58 #include <sys/types.h>
59 #include <string.h>
60 
61 /*
62  * Copy src to string dst of size siz.  At most siz-1 characters
63  * will be copied.  Always NUL terminates (unless siz == 0).
64  * Returns strlen(src); if retval >= siz, truncation occurred.
65  */
php_strlcpy(dst,src,siz)66 PHPAPI size_t php_strlcpy(dst, src, siz)
67 	char *dst;
68 	const char *src;
69 	size_t siz;
70 {
71 	const char *s = src;
72 	size_t n = siz;
73 
74 	/* Copy as many bytes as will fit */
75 	if (n != 0) {
76 		while (--n != 0) {
77 			if ((*dst++ = *src++) == 0)
78 				break;
79 		}
80 	}
81 
82 	/* Not enough room in dst, add NUL and traverse rest of src */
83 	if (n == 0) {
84 		if (siz != 0)
85 			*dst = '\0';		/* NUL-terminate dst */
86 		while (*src++)
87 			;
88 	}
89 
90 	/*
91 	 * Cast pointers to unsigned type before calculation, to avoid signed
92 	 * overflow when the string ends where the MSB has changed.
93 	 * Return value does not include NUL.
94 	 */
95 	return((uintptr_t)src - (uintptr_t)s - 1);
96 }
97 
98 #endif /* !HAVE_STRLCPY */
99 
100 /*
101  * Local variables:
102  * tab-width: 4
103  * c-basic-offset: 4
104  * End:
105  * vim600: sw=4 ts=4 fdm=marker
106  * vim<600: sw=4 ts=4
107  */
108