1 /*
2 Copyright (c) 1994 Cygnus Support.
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms are permitted
6 provided that the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 at Cygnus Support, Inc.  Cygnus Support, Inc. may not be used to
11 endorse or promote products derived from this software without
12 specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 	<<strcpy>>---copy string
20 
21 INDEX
22 	strcpy
23 
24 SYNOPSIS
25 	#include <string.h>
26 	char *strcpy(char *<[dst]>, const char *<[src]>);
27 
28 DESCRIPTION
29 	<<strcpy>> copies the string pointed to by <[src]>
30 	(including the terminating null character) to the array
31 	pointed to by <[dst]>.
32 
33 RETURNS
34 	This function returns the initial value of <[dst]>.
35 
36 PORTABILITY
37 <<strcpy>> is ANSI C.
38 
39 <<strcpy>> requires no supporting OS subroutines.
40 
41 QUICKREF
42 	strcpy ansi pure
43 */
44 
45 #include <string.h>
46 #include <limits.h>
47 #include <stdint.h>
48 
49 /*SUPPRESS 560*/
50 /*SUPPRESS 530*/
51 
52 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
53 #define UNALIGNED(X, Y) \
54   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
55 
56 #if LONG_MAX == 2147483647L
57 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
58 #else
59 #if LONG_MAX == 9223372036854775807L
60 /* Nonzero if X (a long int) contains a NULL byte. */
61 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
62 #else
63 #error long int is not a 32bit or 64bit type.
64 #endif
65 #endif
66 
67 #ifndef DETECTNULL
68 #error long int is not a 32bit or 64bit byte
69 #endif
70 
71 #undef strcpy
72 
73 char*
strcpy(char * dst0,const char * src0)74 strcpy (char *dst0,
75 	const char *src0)
76 {
77 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) || \
78     defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
79   char *s = dst0;
80 
81   while ((*dst0++ = *src0++))
82     ;
83 
84   return s;
85 #else
86   char *dst = dst0;
87   const char *src = src0;
88   long *aligned_dst;
89   const long *aligned_src;
90 
91   /* If SRC or DEST is unaligned, then copy bytes.  */
92   if (!UNALIGNED (src, dst))
93     {
94       aligned_dst = (long*)dst;
95       aligned_src = (long*)src;
96 
97       /* SRC and DEST are both "long int" aligned, try to do "long int"
98          sized copies.  */
99       while (!DETECTNULL(*aligned_src))
100         {
101           *aligned_dst++ = *aligned_src++;
102         }
103 
104       dst = (char*)aligned_dst;
105       src = (char*)aligned_src;
106     }
107 
108   while ((*dst++ = *src++))
109     ;
110   return dst0;
111 #endif /* not PREFER_SIZE_OVER_SPEED */
112 }
113