Update copyright notices with scripts/update-copyrights.
[glibc.git] / sysdeps / i386 / stpncpy.S
blob05fd12abc957058f884c68228dfbc1e57af94889
1 /* copy no more then N bytes from SRC to DEST, returning the address of
2    the terminating '\0' in DEST.
3    For Intel 80x86, x>=3.
4    Copyright (C) 1994-2013 Free Software Foundation, Inc.
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.
12    The GNU C Library is free software; you can redistribute it and/or
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.
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
20    Lesser General Public License for more details.
22    You should have received a copy of the GNU Lesser General Public
23    License along with the GNU C Library; if not, see
24    <http://www.gnu.org/licenses/>.  */
26 #include <sysdep.h>
27 #include "asm-syntax.h"
28 #include "bp-sym.h"
29 #include "bp-asm.h"
31 #define PARMS   LINKAGE+4       /* space for 1 saved reg */
32 #define RTN     PARMS
33 #define DEST    RTN+RTN_SIZE
34 #define SRC     DEST+PTR_SIZE
35 #define LEN     SRC+PTR_SIZE
37         .text
38 ENTRY (BP_SYM (__stpncpy))
39         ENTER
41         pushl %esi
42         cfi_adjust_cfa_offset (4)
44         movl DEST(%esp), %eax
45         movl SRC(%esp), %esi
46         cfi_rel_offset (esi, 0)
47         movl LEN(%esp), %ecx
48         CHECK_BOUNDS_LOW (%eax, DEST(%esp))
49         CHECK_BOUNDS_LOW (%esi, SRC(%esp))
51         subl %eax, %esi         /* magic: reduce number of loop variants
52                                    to one using addressing mode */
53         jmp L(1)                /* jump to loop "head" */
55         ALIGN(4)
57         /* Four times unfolded loop with two loop counters.  We get the
58            third value (the source address) by using the index+base
59            addressing mode.  */
60 L(2):   movb (%eax,%esi), %dl   /* load current char */
61         movb %dl, (%eax)        /* and store it */
62         testb %dl, %dl          /* was it NUL? */
63         jz L(7)                 /* yes, then exit */
65         movb 1(%eax,%esi), %dl  /* load current char */
66         movb %dl, 1(%eax)       /* and store it */
67         testb %dl, %dl          /* was it NUL? */
68         jz L(6)                 /* yes, then exit */
70         movb 2(%eax,%esi), %dl  /* load current char */
71         movb %dl, 2(%eax)       /* and store it */
72         testb %dl, %dl          /* was it NUL? */
73         jz L(5)                 /* yes, then exit */
75         movb 3(%eax,%esi), %dl  /* load current char */
76         movb %dl, 3(%eax)       /* and store it */
77         testb %dl, %dl          /* was it NUL? */
78         jz L(4)                 /* yes, then exit */
80         addl $4, %eax           /* increment loop counter for full round */
82 L(1):   subl $4, %ecx           /* still more than 4 bytes allowed? */
83         jae L(2)                /* yes, then go to start of loop */
85         /* The maximal remaining 15 bytes are not processed in a loop.  */
87         addl $4, %ecx           /* correct above subtraction */
88         jz L(9)                 /* maximal allowed char reached => go to end */
90         movb (%eax,%esi), %dl   /* load current char */
91         movb %dl, (%eax)        /* and store it */
92         testb %dl, %dl          /* was it NUL? */
93         jz L(3)                 /* yes, then exit */
95         incl %eax               /* increment pointer */
96         decl %ecx               /* decrement length counter */
97         jz L(9)                 /* no more allowed => exit */
99         movb (%eax,%esi), %dl   /* load current char */
100         movb %dl, (%eax)        /* and store it */
101         testb %dl, %dl          /* was it NUL? */
102         jz L(3)                 /* yes, then exit */
104         incl %eax               /* increment pointer */
105         decl %ecx               /* decrement length counter */
106         jz L(9)                 /* no more allowed => exit */
108         movb (%eax,%esi), %dl   /* load current char */
109         movb %dl, (%eax)        /* and store it */
110         testb %dl, %dl          /* was it NUL? */
111         jz L(3)                 /* yes, then exit */
113         incl %eax               /* increment pointer */
114         jmp L(9)                /* we don't have to test for counter underflow
115                                    because we know we had a most 3 bytes
116                                    remaining => exit */
118         /* When coming from the main loop we have to adjust the pointer.  */
119 L(4):   decl %ecx               /* decrement counter */
120         incl %eax               /* increment pointer */
122 L(5):   decl %ecx               /* increment pointer */
123         incl %eax               /* increment pointer */
125 L(6):   decl %ecx               /* increment pointer */
126         incl %eax               /* increment pointer */
127 L(7):
129         addl $3, %ecx           /* correct pre-decrementation of counter
130                                    at the beginning of the loop; but why 3
131                                    and not 4?  Very simple, we have to count
132                                    the NUL char we already wrote.  */
133         jz L(9)                 /* counter is also 0 => exit */
135         /* We now have to fill the rest of the buffer with NUL.  This
136            is done in a tricky way.  Please note that the addressing mode
137            used below is not the same we used above.  Here we use the
138            %ecx register.  */
139 L(8):
140         movb $0, (%ecx,%eax)    /* store NUL char */
141 L(3):   decl %ecx               /* all bytes written? */
142         jnz L(8)                /* no, then again */
144 L(9):
145 #if __BOUNDED_POINTERS__
146         addl %eax, %esi         /* undo magic: %esi now points beyond end of SRC */
147         CHECK_BOUNDS_HIGH (%esi, SRC(%esp), jbe)
148         CHECK_BOUNDS_HIGH (%eax, DEST(%esp), jbe)
149         RETURN_BOUNDED_POINTER (DEST(%esp))
150 #endif
151         popl %esi               /* restore saved register content */
152         cfi_adjust_cfa_offset (-4)
153         cfi_restore (esi)
155         LEAVE
156         RET_PTR
157 END (BP_SYM (__stpncpy))
159 libc_hidden_def (BP_SYM (__stpncpy))
160 weak_alias (BP_SYM (__stpncpy), BP_SYM (stpncpy))