xref: /netbsd-src/lib/libc/arch/hppa/string/strlcpy.S (revision 2af01421ec58ee0b13f04872508d2ab824742d69)
1*2af01421Sskrll/*	$NetBSD: strlcpy.S,v 1.5 2020/05/05 06:20:55 skrll Exp $	*/
202ac1ae2Sfredette
302ac1ae2Sfredette/*
402ac1ae2Sfredette * Copyright (c) 2002 The NetBSD Foundation, Inc.
502ac1ae2Sfredette * All rights reserved.
602ac1ae2Sfredette *
702ac1ae2Sfredette * This code is derived from software contributed to The NetBSD Foundation
802ac1ae2Sfredette * by Matthew Fredette.
902ac1ae2Sfredette *
1002ac1ae2Sfredette * Redistribution and use in source and binary forms, with or without
1102ac1ae2Sfredette * modification, are permitted provided that the following conditions
1202ac1ae2Sfredette * are met:
1302ac1ae2Sfredette * 1. Redistributions of source code must retain the above copyright
1402ac1ae2Sfredette *    notice, this list of conditions and the following disclaimer.
1502ac1ae2Sfredette * 2. Redistributions in binary form must reproduce the above copyright
1602ac1ae2Sfredette *    notice, this list of conditions and the following disclaimer in the
1702ac1ae2Sfredette *    documentation and/or other materials provided with the distribution.
1802ac1ae2Sfredette *
1902ac1ae2Sfredette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2002ac1ae2Sfredette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2102ac1ae2Sfredette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2202ac1ae2Sfredette * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2302ac1ae2Sfredette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2402ac1ae2Sfredette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2502ac1ae2Sfredette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2602ac1ae2Sfredette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2702ac1ae2Sfredette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2802ac1ae2Sfredette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2902ac1ae2Sfredette * POSSIBILITY OF SUCH DAMAGE.
3002ac1ae2Sfredette */
3102ac1ae2Sfredette
3202ac1ae2Sfredette#include <machine/asm.h>
3302ac1ae2Sfredette
3402ac1ae2Sfredette#if defined(LIBC_SCCS) && !defined(lint)
35*2af01421Sskrll        RCSID("$NetBSD: strlcpy.S,v 1.5 2020/05/05 06:20:55 skrll Exp $")
3602ac1ae2Sfredette#endif /* LIBC_SCCS and not lint */
3702ac1ae2Sfredette
3802ac1ae2Sfredette/*
3902ac1ae2Sfredette * size_t strlcpy(char *dst, const char *src, size_t size);
4002ac1ae2Sfredette */
41ed9d4336SmattLEAF_ENTRY(_strlcpy)
42ed9d4336Smatt	.weak strlcpy
43ed9d4336Smatt	strlcpy = _strlcpy
4402ac1ae2Sfredette
4502ac1ae2Sfredette	/* Always load the first byte of the source string. */
4602ac1ae2Sfredette	ldbs,ma		1(%arg1), %r1
4702ac1ae2Sfredette
4802ac1ae2Sfredette	/*
4902ac1ae2Sfredette	 * If our dst buffer is zero bytes, branch immediately
5002ac1ae2Sfredette	 * to the code that counts the (remaining) length
5102ac1ae2Sfredette	 * of the src string.
5202ac1ae2Sfredette	 */
5302ac1ae2Sfredette	comb,=		%r0, %arg2, $strlcpy_dst_done
5402ac1ae2Sfredette	 copy		%arg1, %ret0
5502ac1ae2Sfredette
5602ac1ae2Sfredette	/*
5702ac1ae2Sfredette	 * If our dst buffer is one byte, branch immediately
5802ac1ae2Sfredette	 * to the code that NUL-terminates the dst buffer and
5902ac1ae2Sfredette	 * counts the (remaining) length of the src string.
6002ac1ae2Sfredette	 */
6102ac1ae2Sfredette	addib,=,n	-1, %arg2, $strlcpy_dst_full
6202ac1ae2Sfredette
6302ac1ae2Sfredette	/*
6402ac1ae2Sfredette	 * Loop copying bytes.  At the top of this loop,
6502ac1ae2Sfredette	 * %arg2 is always the number of bytes we can still
6602ac1ae2Sfredette	 * store, and %r1 holds the next byte to store.
6702ac1ae2Sfredette	 */
68ed9d4336Smatt$strlcpy_loop:
6902ac1ae2Sfredette	comb,=		%r0, %r1, $strlcpy_exit
7002ac1ae2Sfredette	 stbs,ma	%r1, 1(%arg0)
7102ac1ae2Sfredette	addib,<>	-1, %arg2, $strlcpy_loop
7202ac1ae2Sfredette	 ldbs,ma	1(%arg1), %r1
7302ac1ae2Sfredette
74ed9d4336Smatt$strlcpy_dst_full:
7502ac1ae2Sfredette	stbs,ma		%r0, 1(%arg0)
7602ac1ae2Sfredette
77ed9d4336Smatt$strlcpy_dst_done:
78ed9d4336Smatt	comb,<>,n	%r0, %r1, $strlcpy_dst_done
7902ac1ae2Sfredette	ldbs,ma		1(%arg1), %r1
8002ac1ae2Sfredette
81ed9d4336Smatt$strlcpy_exit:
8202ac1ae2Sfredette	bv		%r0(%rp)
8302ac1ae2Sfredette	 sub		%arg1, %ret0, %ret0
8402ac1ae2SfredetteEXIT(strlcpy)
85