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