strcpy_naive.S revision 1.2.8.2 1 1.2.8.2 tls /*-
2 1.2.8.2 tls * Copyright (c) 2013 The NetBSD Foundation, Inc.
3 1.2.8.2 tls * All rights reserved.
4 1.2.8.2 tls *
5 1.2.8.2 tls * This code is derived from software contributed to The NetBSD Foundation
6 1.2.8.2 tls * by Matt Thomas of 3am Software Foundry.
7 1.2.8.2 tls *
8 1.2.8.2 tls * Redistribution and use in source and binary forms, with or without
9 1.2.8.2 tls * modification, are permitted provided that the following conditions
10 1.2.8.2 tls * are met:
11 1.2.8.2 tls * 1. Redistributions of source code must retain the above copyright
12 1.2.8.2 tls * notice, this list of conditions and the following disclaimer.
13 1.2.8.2 tls * 2. Redistributions in binary form must reproduce the above copyright
14 1.2.8.2 tls * notice, this list of conditions and the following disclaimer in the
15 1.2.8.2 tls * documentation and/or other materials provided with the distribution.
16 1.2.8.2 tls *
17 1.2.8.2 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18 1.2.8.2 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 1.2.8.2 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 1.2.8.2 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21 1.2.8.2 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 1.2.8.2 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 1.2.8.2 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 1.2.8.2 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 1.2.8.2 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 1.2.8.2 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 1.2.8.2 tls * POSSIBILITY OF SUCH DAMAGE.
28 1.2.8.2 tls */
29 1.2.8.2 tls #include <machine/asm.h>
30 1.2.8.2 tls
31 1.2.8.2 tls RCSID("$NetBSD: strcpy_naive.S,v 1.2.8.2 2013/02/25 00:23:58 tls Exp $")
32 1.2.8.2 tls
33 1.2.8.2 tls #ifdef _LIBC
34 1.2.8.2 tls #ifdef STRLCPY
35 1.2.8.2 tls WEAK_ALIAS(strlcpy, _strlcpy)
36 1.2.8.2 tls #endif
37 1.2.8.2 tls #include "namespace.h"
38 1.2.8.2 tls #endif
39 1.2.8.2 tls
40 1.2.8.2 tls /*
41 1.2.8.2 tls * These are "naive" versions of the str*cpy routines designed to be simple
42 1.2.8.2 tls * and small.
43 1.2.8.2 tls */
44 1.2.8.2 tls
45 1.2.8.2 tls #if defined(STRLCPY)
46 1.2.8.2 tls /* LINTSTUB: size_t strlcpy(char *, const char *, size_t) */
47 1.2.8.2 tls ENTRY(strlcpy)
48 1.2.8.2 tls add ip, r1, #1 /* save src pointer (+ NUL) */
49 1.2.8.2 tls subs r2, r2, #1 /* make sure there's room for a NUL */
50 1.2.8.2 tls blt 3f /* no room, do the strlen */
51 1.2.8.2 tls add r2, r2, r0 /* get end of dst */
52 1.2.8.2 tls 1: cmp r0, r2 /* room for another char? */
53 1.2.8.2 tls beq 2f /* no, write NUL and do the strlen */
54 1.2.8.2 tls ldrb r3, [r1], #1 /* read a byte */
55 1.2.8.2 tls strb r3, [r0], #1 /* write a byte */
56 1.2.8.2 tls teq r3, #0 /* was it a NUL? */
57 1.2.8.2 tls bne 1b /* no, do next byte */
58 1.2.8.2 tls b 4f /* yes, end of string, so return */
59 1.2.8.2 tls 2: mov r3, #0 /* NUL */
60 1.2.8.2 tls strb r3, [r0], #1 /* write to end of string */
61 1.2.8.2 tls 3: ldrb r3, [r1], #1 /* read a byte */
62 1.2.8.2 tls teq r3, #0 /* was it a NUL? */
63 1.2.8.2 tls bne 3b /* no, get next byte */
64 1.2.8.2 tls 4: sub r0, r1, ip /* return length of src string */
65 1.2.8.2 tls RET
66 1.2.8.2 tls END(strlcpy)
67 1.2.8.2 tls #elif defined(STRNCPY)
68 1.2.8.2 tls /* LINTSTUB: char * strncpy(char *, const char *, size_t) */
69 1.2.8.2 tls ENTRY(strncpy)
70 1.2.8.2 tls mov ip, r0 /* we want to preserve r0 */
71 1.2.8.2 tls add r2, r2, r0 /* get end of dst buffer */
72 1.2.8.2 tls 1: cmp ip, r2 /* are at the end of dst already? */
73 1.2.8.2 tls RETc(eq) /* yes, just return. */
74 1.2.8.2 tls ldrb r3, [r1], #1 /* read a byte */
75 1.2.8.2 tls strb r3, [ip], #1 /* write a byte */
76 1.2.8.2 tls teq r3, #0 /* was it a NUL? */
77 1.2.8.2 tls bne 1b /* no, try next byte */
78 1.2.8.2 tls 2: cmp ip, r2 /* are at the end of dst already? */
79 1.2.8.2 tls strltb r3, [ip], #1 /* no, write a NUL */
80 1.2.8.2 tls blt 2b /* until dst is filled */
81 1.2.8.2 tls 3: RET /* return dst pointer */
82 1.2.8.2 tls END(strncpy)
83 1.2.8.2 tls #else
84 1.2.8.2 tls /* LINTSTUB: char * strcpy(char *, const char *) */
85 1.2.8.2 tls ENTRY(strcpy)
86 1.2.8.2 tls mov ip, r0 /* we want to preserve r0 */
87 1.2.8.2 tls 1: ldrb r3, [r1], #1 /* read a byte */
88 1.2.8.2 tls strb r3, [ip], #1 /* write a byte */
89 1.2.8.2 tls teq r3, #0 /* was it a NUL? */
90 1.2.8.2 tls bne 1b /* no, try next byte */
91 1.2.8.2 tls RET /* return dst pointer */
92 1.2.8.2 tls END(strcpy)
93 1.2.8.2 tls #endif
94