]>
Commit | Line | Data |
---|---|---|
5929563f UD |
1 | /* copy no more then N bytes from SRC to DEST, returning the address of |
2 | the terminating '\0' in DEST. | |
6d52618b | 3 | For Intel 80x86, x>=3. |
a334319f | 4 | Copyright (C) 1994,1995,1996,1997,2000,2002 Free Software Foundation, Inc. |
6d52618b UD |
5 | This file is part of the GNU C Library. |
6 | Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu> | |
7 | Some bug fixes by Alan Modra <Alan@SPRI.Levels.UniSA.Edu.Au> | |
8 | - original wrote n+1 chars in some cases. | |
9 | - stpncpy() ought to behave like strncpy() ie. not null-terminate | |
10 | if limited by n. glibc-1.09 stpncpy() does this. | |
11 | ||
12 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
13 | modify it under the terms of the GNU Lesser General Public |
14 | License as published by the Free Software Foundation; either | |
15 | version 2.1 of the License, or (at your option) any later version. | |
6d52618b UD |
16 | |
17 | The GNU C Library is distributed in the hope that it will be useful, | |
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 20 | Lesser General Public License for more details. |
6d52618b | 21 | |
41bdb6e2 AJ |
22 | You should have received a copy of the GNU Lesser General Public |
23 | License along with the GNU C Library; if not, write to the Free | |
24 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
25 | 02111-1307 USA. */ | |
8f5ca04b RM |
26 | |
27 | #include <sysdep.h> | |
28 | #include "asm-syntax.h" | |
2fc08826 | 29 | #include "bp-sym.h" |
3f02f778 | 30 | #include "bp-asm.h" |
8f5ca04b | 31 | |
3f02f778 GM |
32 | #define PARMS LINKAGE+4 /* space for 1 saved reg */ |
33 | #define RTN PARMS | |
34 | #define DEST RTN+RTN_SIZE | |
35 | #define SRC DEST+PTR_SIZE | |
36 | #define LEN SRC+PTR_SIZE | |
8f5ca04b RM |
37 | |
38 | .text | |
2fc08826 | 39 | ENTRY (BP_SYM (__stpncpy)) |
3f02f778 | 40 | ENTER |
8f5ca04b RM |
41 | |
42 | pushl %esi | |
43 | ||
3f02f778 GM |
44 | movl DEST(%esp), %eax |
45 | movl SRC(%esp), %esi | |
46 | movl LEN(%esp), %ecx | |
2fc08826 GM |
47 | CHECK_BOUNDS_LOW (%eax, DEST(%esp)) |
48 | CHECK_BOUNDS_LOW (%esi, SRC(%esp)) | |
8f5ca04b RM |
49 | |
50 | subl %eax, %esi /* magic: reduce number of loop variants | |
51 | to one using addressing mode */ | |
5929563f | 52 | jmp L(1) /* jump to loop "head" */ |
8f5ca04b RM |
53 | |
54 | ALIGN(4) | |
55 | ||
56 | /* Four times unfolded loop with two loop counters. We get the | |
57 | the third value (the source address) by using the index+base | |
6d52618b | 58 | addressing mode. */ |
5929563f | 59 | L(2): movb (%eax,%esi), %dl /* load current char */ |
8f5ca04b RM |
60 | movb %dl, (%eax) /* and store it */ |
61 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 62 | jz L(7) /* yes, then exit */ |
8f5ca04b RM |
63 | |
64 | movb 1(%eax,%esi), %dl /* load current char */ | |
65 | movb %dl, 1(%eax) /* and store it */ | |
66 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 67 | jz L(6) /* yes, then exit */ |
8f5ca04b RM |
68 | |
69 | movb 2(%eax,%esi), %dl /* load current char */ | |
70 | movb %dl, 2(%eax) /* and store it */ | |
71 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 72 | jz L(5) /* yes, then exit */ |
8f5ca04b RM |
73 | |
74 | movb 3(%eax,%esi), %dl /* load current char */ | |
75 | movb %dl, 3(%eax) /* and store it */ | |
76 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 77 | jz L(4) /* yes, then exit */ |
8f5ca04b RM |
78 | |
79 | addl $4, %eax /* increment loop counter for full round */ | |
80 | ||
5929563f UD |
81 | L(1): subl $4, %ecx /* still more than 4 bytes allowed? */ |
82 | jae L(2) /* yes, then go to start of loop */ | |
8f5ca04b RM |
83 | |
84 | /* The maximal remaining 15 bytes are not processed in a loop. */ | |
85 | ||
86 | addl $4, %ecx /* correct above subtraction */ | |
5929563f | 87 | jz L(9) /* maximal allowed char reached => go to end */ |
8f5ca04b RM |
88 | |
89 | movb (%eax,%esi), %dl /* load current char */ | |
90 | movb %dl, (%eax) /* and store it */ | |
91 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 92 | jz L(3) /* yes, then exit */ |
8f5ca04b RM |
93 | |
94 | incl %eax /* increment pointer */ | |
95 | decl %ecx /* decrement length counter */ | |
5929563f | 96 | jz L(9) /* no more allowed => exit */ |
8f5ca04b RM |
97 | |
98 | movb (%eax,%esi), %dl /* load current char */ | |
99 | movb %dl, (%eax) /* and store it */ | |
100 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 101 | jz L(3) /* yes, then exit */ |
8f5ca04b RM |
102 | |
103 | incl %eax /* increment pointer */ | |
104 | decl %ecx /* decrement length counter */ | |
5929563f | 105 | jz L(9) /* no more allowed => exit */ |
8f5ca04b RM |
106 | |
107 | movb (%eax,%esi), %dl /* load current char */ | |
108 | movb %dl, (%eax) /* and store it */ | |
109 | testb %dl, %dl /* was it NUL? */ | |
5929563f | 110 | jz L(3) /* yes, then exit */ |
8f5ca04b RM |
111 | |
112 | incl %eax /* increment pointer */ | |
5929563f | 113 | jmp L(9) /* we don't have to test for counter underflow |
8f5ca04b RM |
114 | because we know we had a most 3 bytes |
115 | remaining => exit */ | |
116 | ||
117 | /* When coming from the main loop we have to adjust the pointer. */ | |
5929563f | 118 | L(4): decl %ecx /* decrement counter */ |
8f5ca04b RM |
119 | incl %eax /* increment pointer */ |
120 | ||
5929563f | 121 | L(5): decl %ecx /* increment pointer */ |
8f5ca04b RM |
122 | incl %eax /* increment pointer */ |
123 | ||
5929563f | 124 | L(6): decl %ecx /* increment pointer */ |
8f5ca04b | 125 | incl %eax /* increment pointer */ |
5929563f | 126 | L(7): |
8f5ca04b RM |
127 | |
128 | addl $3, %ecx /* correct pre-decrementation of counter | |
129 | at the beginning of the loop; but why 3 | |
130 | and not 4? Very simple, we have to count | |
131 | the NUL char we already wrote. */ | |
5929563f | 132 | jz L(9) /* counter is also 0 => exit */ |
8f5ca04b RM |
133 | |
134 | /* We now have to fill the rest of the buffer with NUL. This | |
6d52618b | 135 | is done in a tricky way. Please note that the addressing mode |
8f5ca04b RM |
136 | used below is not the same we used above. Here we use the |
137 | %ecx register. */ | |
5929563f | 138 | L(8): |
8f5ca04b | 139 | movb $0, (%ecx,%eax) /* store NUL char */ |
5929563f UD |
140 | L(3): decl %ecx /* all bytes written? */ |
141 | jnz L(8) /* no, then again */ | |
8f5ca04b | 142 | |
2fc08826 GM |
143 | L(9): |
144 | #if __BOUNDED_POINTERS__ | |
145 | addl %eax, %esi /* undo magic: %esi now points beyond end of SRC */ | |
53c06508 GM |
146 | CHECK_BOUNDS_HIGH (%esi, SRC(%esp), jbe) |
147 | CHECK_BOUNDS_HIGH (%eax, DEST(%esp), jbe) | |
2fc08826 GM |
148 | RETURN_BOUNDED_POINTER (DEST(%esp)) |
149 | #endif | |
150 | popl %esi /* restore saved register content */ | |
8f5ca04b | 151 | |
3f02f778 GM |
152 | LEAVE |
153 | RET_PTR | |
2fc08826 | 154 | END (BP_SYM (__stpncpy)) |
8f5ca04b | 155 | |
37ba7d66 | 156 | libc_hidden_def (BP_SYM (__stpncpy)) |
2fc08826 | 157 | weak_alias (BP_SYM (__stpncpy), BP_SYM (stpncpy)) |