]> git.proxmox.com Git - mirror_edk2.git/blob - StdLib/LibC/String/Copying.c
18498d6f8783c2efd9e075b99d45777de30b2d4c
[mirror_edk2.git] / StdLib / LibC / String / Copying.c
1 /** @file
2 Copying Functions for <string.h>.
3
4 Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made available under
6 the terms and conditions of the BSD License that accompanies this distribution.
7 The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 **/
13 #include <Uefi.h>
14 #include <Library/BaseLib.h>
15 #include <Library/BaseMemoryLib.h>
16
17 #include <LibConfig.h>
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 /** Do not define memcpy for IPF+GCC builds.
23 For IPF, using a GCC compiler, the memcpy function is converted to
24 CopyMem by objcpy during build.
25 **/
26 #if !(defined(MDE_CPU_IPF) && defined(__GNUC__))
27 /** The memcpy function copies n characters from the object pointed to by s2
28 into the object pointed to by s1.
29
30 The implementation is reentrant and handles the case where s2 overlaps s1.
31
32 @return The memcpy function returns the value of s1.
33 **/
34 void *
35 memcpy(void * __restrict s1, const void * __restrict s2, size_t n)
36 {
37 return CopyMem( s1, s2, n);
38 }
39 #endif /* !(defined(MDE_CPU_IPF) && defined(__GCC)) */
40
41 /** The memmove function copies n characters from the object pointed to by s2
42 into the object pointed to by s1. Copying takes place as if the n
43 characters from the object pointed to by s2 are first copied into a
44 temporary array of n characters that does not overlap the objects pointed
45 to by s1 and s2, and then the n characters from the temporary array are
46 copied into the object pointed to by s1.
47
48 This is a version of memcpy that is guaranteed to work when s1 and s2
49 overlap. Since our implementation of memcpy already handles overlap,
50 memmove can be identical to memcpy.
51
52 @return The memmove function returns the value of s1.
53 **/
54 void *
55 memmove(void *s1, const void *s2, size_t n)
56 {
57 return CopyMem( s1, s2, n);
58 }
59
60 /** The strcpy function copies the string pointed to by s2 (including the
61 terminating null character) into the array pointed to by s1. If copying
62 takes place between objects that overlap, the behavior is undefined.
63
64 @return The strcpy function returns the value of s1.
65 **/
66 char *
67 strcpy(char * __restrict s1, const char * __restrict s2)
68 {
69 //char *s1ret = s1;
70
71 //while ( *s1++ = *s2++) /* Empty Body */;
72 //return(s1ret);
73 return AsciiStrCpy( s1, s2);
74 }
75
76 /** The strncpy function copies not more than n characters (characters that
77 follow a null character are not copied) from the array pointed to by s2 to
78 the array pointed to by s1. If copying takes place between objects that
79 overlap, the behavior is undefined.
80
81 If the array pointed to by s2 is a string that is shorter than n
82 characters, null characters are appended to the copy in the array pointed
83 to by s1, until n characters in all have been written.
84
85 @return The strncpy function returns the value of s1.
86 **/
87 char *strncpy(char * __restrict s1, const char * __restrict s2, size_t n)
88 {
89 return AsciiStrnCpy( s1, s2, n);
90 //char *dest = s1;
91
92 //while(n != 0) {
93 // --n;
94 // if((*dest++ = *s2++) == '\0') break;
95 //}
96 //while(n != 0) {
97 // *dest++ = '\0';
98 // --n;
99 //}
100 //return (s1);
101 }
102
103 /** The strncpyX function copies not more than n-1 characters (characters that
104 follow a null character are not copied) from the array pointed to by s2 to
105 the array pointed to by s1. Array s1 is guaranteed to be NULL terminated.
106 If copying takes place between objects that overlap,
107 the behavior is undefined.
108
109 strncpyX exists because normal strncpy does not indicate if the copy was
110 terminated because of exhausting the buffer or reaching the end of s2.
111
112 @return The strncpyX function returns 0 if the copy operation was
113 terminated because it reached the end of s1. Otherwise,
114 a non-zero value is returned indicating how many characters
115 remain in s1.
116 **/
117 int strncpyX(char * __restrict s1, const char * __restrict s2, size_t n)
118 {
119 int NumLeft;
120
121 for( ; n != 0; --n) {
122 if((*s1++ = *s2++) == '\0') break;
123 }
124 NumLeft = (int)n;
125
126 for( --s1; n != 0; --n) {
127 *s1++ = '\0';
128 }
129
130 return NumLeft; // Zero if we ran out of buffer ( strlen(s1) < strlen(s2) )
131 }
132
133 /** NetBSD Compatibility Function strdup creates a duplicate copy of a string. **/
134 char *
135 strdup(const char *str)
136 {
137 size_t len;
138 char *copy;
139
140 len = strlen(str) + 1;
141 if ((copy = malloc(len)) == NULL)
142 return (NULL);
143 memcpy(copy, str, len);
144 return (copy);
145 }