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