xref: /aosp_15_r20/external/flac/src/flac/local_string_utils.c (revision 600f14f40d737144c998e2ec7a483122d3776fbc)
1 /* flac - Command-line FLAC encoder/decoder
2  */
3 
4 #ifdef HAVE_CONFIG_H
5 #  include <config.h>
6 #endif
7 
8 #include <string.h>
9 
10 #include "utils.h"
11 #include "local_string_utils.h"
12 
13 /*	$OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $
14  *
15  * Copyright (c) 1998 Todd C. Miller <[email protected]>
16  *
17  * Permission to use, copy, modify, and distribute this software for any
18  * purpose with or without fee is hereby granted, provided that the above
19  * copyright notice and this permission notice appear in all copies.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
22  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
24  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
27  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28  *
29  *
30  * Copy src to string dst of size siz.  At most siz-1 characters
31  * will be copied.  Always NUL terminates (unless siz == 0).
32  * Returns strlen(src); if retval >= siz, truncation occurred.
33  */
34 size_t
flac__strlcpy(char * dst,const char * src,size_t siz)35 flac__strlcpy(char *dst, const char *src, size_t siz)
36 {
37 	register char *d = dst;
38 	register const char *s = src;
39 	register size_t n = siz;
40 
41 	/* Copy as many bytes as will fit */
42 	if (n != 0 && --n != 0) {
43 		do {
44 			if ((*d++ = *s++) == 0)
45 				break;
46 		} while (--n != 0);
47 	}
48 
49 	/* Not enough room in dst, add NUL and traverse rest of src */
50 	if (n == 0) {
51 		if (siz != 0)
52 			*d = '\0';		/* NUL-terminate dst */
53 		while (*s++)
54 			;
55 	}
56 
57 	return(s - src - 1);	/* count does not include NUL */
58 }
59 
60 /*	$OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $
61  *
62  * Copyright (c) 1998 Todd C. Miller <[email protected]>
63  *
64  * Permission to use, copy, modify, and distribute this software for any
65  * purpose with or without fee is hereby granted, provided that the above
66  * copyright notice and this permission notice appear in all copies.
67  *
68  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
69  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
70  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
71  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
72  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
73  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
74  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
75  *
76  *
77  * Appends src to string dst of size siz (unlike strncat, siz is the
78  * full size of dst, not space left).  At most siz-1 characters
79  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
80  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
81  * If retval >= siz, truncation occurred.
82  */
83 size_t
flac__strlcat(char * dst,const char * src,size_t siz)84 flac__strlcat(char *dst, const char *src, size_t siz)
85 {
86 	register char *d = dst;
87 	register const char *s = src;
88 	register size_t n = siz;
89 	size_t dlen;
90 
91 	/* Find the end of dst and adjust bytes left but don't go past end */
92 	while (n-- != 0 && *d != '\0')
93 		d++;
94 	dlen = d - dst;
95 	n = siz - dlen;
96 
97 	if (n == 0)
98 		return(dlen + strlen(s));
99 	while (*s != '\0') {
100 		if (n != 1) {
101 			*d++ = *s;
102 			n--;
103 		}
104 		s++;
105 	}
106 	*d = '\0';
107 
108 	return(dlen + (s - src));	/* count does not include NUL */
109 }
110