1/*
2   AVR LIBC
3   strlcpy_P()
4
5   Copyright (c) 2003, 2005, 2006, 2007 Eric B. Weddington
6   Copyright (c) 2009  Dmitry Xmelkov
7   All rights reserved.
8
9   Redistribution and use in source and binary forms, with or without
10   modification, are permitted provided that the following conditions are met:
11
12   * Redistributions of source code must retain the above copyright
13     notice, this list of conditions and the following disclaimer.
14   * Redistributions in binary form must reproduce the above copyright
15     notice, this list of conditions and the following disclaimer in
16     the documentation and/or other materials provided with the
17     distribution.
18   * Neither the name of the copyright holders nor the names of
19     contributors may be used to endorse or promote products derived
20     from this software without specific prior written permission.
21
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36/* $Id: strlcpy_P.S 2503 2016-02-07 22:59:47Z joerg_wunsch $ */
37
38#if !defined(__AVR_TINY__)
39
40#if !defined(__DOXYGEN__)
41
42#include "asmdef.h"
43
44#define dst_lo		r24
45#define src_hi		r23
46#define src_lo		r22
47#define siz_hi		r21
48#define siz_lo		r20
49#define rslt_lo		r24
50
51
52ENTRY strlcpy_P
53	X_movw	XL, dst_lo		; X = dst
54	X_movw	ZL, src_lo		; Z = src
55
56  ; copy loop
571:	subi	siz_lo, lo8(1)
58	sbci	siz_hi, hi8(1)
59	brcs	4f			; is possible with siz == 0
60	breq	3f			; --> siz chars copied
61	X_lpm	__tmp_reg__, Z+
62	st	X+, __tmp_reg__
63	tst	__tmp_reg__
64	brne	1b
65
66  ; calculate result (Z - 1 - src) and return
672:	sub	ZL, src_lo
68	sbc	ZH, src_hi
69	sbiw	ZL, 1
70	X_movw	rslt_lo, ZL
71	ret
72
73  ; terminate dst
743:	st	X, __zero_reg__
75
76  ; find src end
774:	X_lpm	__tmp_reg__, Z+
78	tst	__tmp_reg__
79	brne	4b
80	rjmp	2b
81
82ENDFUNC
83
84#endif /* not __DOXYGEN__ */
85
86#endif /* !defined(__AVR_TINY__) */
87