1 /*
2 Copyright (c) 2007 Corinna Vinschen <corinna@vinschen.de>
3  */
4 /*
5 FUNCTION
6 	<<stpncpy>>---counted copy string returning a pointer to its end
7 
8 INDEX
9 	stpncpy
10 
11 SYNOPSIS
12 	#include <string.h>
13 	char *stpncpy(char *restrict <[dst]>, const char *restrict <[src]>,
14                       size_t <[length]>);
15 
16 DESCRIPTION
17 	<<stpncpy>> copies not more than <[length]> characters from the
18 	the string pointed to by <[src]> (including the terminating
19 	null character) to the array pointed to by <[dst]>.  If the
20 	string pointed to by <[src]> is shorter than <[length]>
21 	characters, null characters are appended to the destination
22 	array until a total of <[length]> characters have been
23 	written.
24 
25 RETURNS
26 	This function returns a pointer to the end of the destination string,
27 	thus pointing to the trailing '\0', or, if the destination string is
28 	not null-terminated, pointing to dst + n.
29 
30 PORTABILITY
31 <<stpncpy>> is a GNU extension, candidate for inclusion into POSIX/SUSv4.
32 
33 <<stpncpy>> requires no supporting OS subroutines.
34 
35 QUICKREF
36 	stpncpy gnu
37 */
38 
39 #define _DEFAULT_SOURCE
40 #include <string.h>
41 #include <limits.h>
42 #include <stdint.h>
43 
44 /*SUPPRESS 560*/
45 /*SUPPRESS 530*/
46 
47 /* Nonzero if either X or Y is not aligned on a "long" boundary.  */
48 #define UNALIGNED(X, Y) \
49   (((uintptr_t)X & (sizeof (long) - 1)) | ((uintptr_t)Y & (sizeof (long) - 1)))
50 
51 #if LONG_MAX == 2147483647L
52 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
53 #else
54 #if LONG_MAX == 9223372036854775807L
55 /* Nonzero if X (a long int) contains a NULL byte. */
56 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
57 #else
58 #error long int is not a 32bit or 64bit type.
59 #endif
60 #endif
61 
62 #ifndef DETECTNULL
63 #error long int is not a 32bit or 64bit byte
64 #endif
65 
66 #define TOO_SMALL(LEN) ((LEN) < sizeof (long))
67 
68 char *
stpncpy(char * __restrict dst,const char * __restrict src,size_t count)69 stpncpy (char *__restrict dst,
70 	const char *__restrict src,
71 	size_t count)
72 {
73   char *ret = NULL;
74 
75 #if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__) && \
76     !defined(PICOLIBC_NO_OUT_OF_BOUNDS_READS)
77   long *aligned_dst;
78   const long *aligned_src;
79 
80   /* If SRC and DEST is aligned and count large enough, then copy words.  */
81   if (!UNALIGNED (src, dst) && !TOO_SMALL (count))
82     {
83       aligned_dst = (long*)dst;
84       aligned_src = (long*)src;
85 
86       /* SRC and DEST are both "long int" aligned, try to do "long int"
87 	 sized copies.  */
88       while (count >= sizeof (long int) && !DETECTNULL(*aligned_src))
89 	{
90 	  count -= sizeof (long int);
91 	  *aligned_dst++ = *aligned_src++;
92 	}
93 
94       dst = (char*)aligned_dst;
95       src = (char*)aligned_src;
96     }
97 #endif /* not PREFER_SIZE_OVER_SPEED */
98 
99   while (count > 0)
100     {
101       --count;
102       if ((*dst++ = *src++) == '\0')
103 	{
104 	  ret = dst - 1;
105 	  break;
106 	}
107     }
108 
109   while (count-- > 0)
110     *dst++ = '\0';
111 
112   return ret ? ret : dst;
113 }
114