5
Copyright (c) 2003, Eric B. Weddington
8
Redistribution and use in source and binary forms, with or without
9
modification, are permitted provided that the following conditions are met:
11
* Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
* Redistributions in binary form must reproduce the above copyright
14
notice, this list of conditions and the following disclaimer in
15
the documentation and/or other materials provided with the
17
* Neither the name of the copyright holders nor the names of
18
contributors may be used to endorse or promote products derived
19
from this software without specific prior written permission.
21
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
POSSIBILITY OF SUCH DAMAGE.
36
/** \ingroup avr_pgmspace
37
\fn size_t strlcpy_P (char *dst, PGM_P, size_t siz)
38
\brief Copy a string from progmem to RAM.
40
Copy src to string dst of size siz. At most siz-1 characters will be
41
copied. Always NULL terminates (unless siz == 0).
43
\returns The strlcpy_P() function returns strlen(src). If retval >= siz,
44
truncation occurred. */
65
.type _U(strlcpy_P), @function
68
LOAD_X (dst_lo, dst_hi) ; X = dst
69
LOAD_Z (src_lo, src_hi) ; Z = src
70
cp siz_lo, __zero_reg__
71
cpc siz_hi, __zero_reg__ ; size == 0 ?
72
breq .strlcpy_P_truncated
74
.strlcpy_P_copy_loop: ; copy src to dst
75
subi siz_lo, lo8(-(-1))
76
sbci siz_hi, hi8(-(-1)) ; decrement siz
77
breq 1f ; --> siz chars copied
78
LPM_R0_ZP ; get next src char
79
st X+, __tmp_reg__ ; copy char
80
tst __tmp_reg__ ; end of src string ?
81
breq .strlcpy_P_len ; --> all src chars copied
82
rjmp .strlcpy_P_copy_loop ; next char
83
1: st X, __zero_reg__ ; truncate dst string
85
.strlcpy_P_truncated: ; find Z = end of src string
86
LPM_R0_ZP ; get next char from src
87
tst __tmp_reg__ ; end of src string ?
88
brne .strlcpy_P_truncated ; next char
90
.strlcpy_P_len: ; calculate strlen(src)
92
sbc Z_hi, src_hi ; Z points past \0
103
.size _U(strlcpy_P), .strlcpy_P_end - _U(strlcpy_P)
105
#endif /* not DOXYGEN */