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 or ARM+GCC builds.
|
---|
23 | For IPF, using a GCC compiler, the memcpy function is converted to
|
---|
24 | CopyMem by objcpy during build.
|
---|
25 | For ARM, the memcpy function is provided by the CompilerIntrinsics library.
|
---|
26 | **/
|
---|
27 | #if !((defined(MDE_CPU_IPF) || defined(MDE_CPU_ARM)) && defined(__GNUC__))
|
---|
28 | /** The memcpy function copies n characters from the object pointed to by s2
|
---|
29 | into the object pointed to by s1.
|
---|
30 |
|
---|
31 | The implementation is reentrant and handles the case where s2 overlaps s1.
|
---|
32 |
|
---|
33 | @return The memcpy function returns the value of s1.
|
---|
34 | **/
|
---|
35 | void *
|
---|
36 | memcpy(void * __restrict s1, const void * __restrict s2, size_t n)
|
---|
37 | {
|
---|
38 | return CopyMem( s1, s2, n);
|
---|
39 | }
|
---|
40 | #endif /* !(defined(MDE_CPU_IPF) && defined(__GCC)) */
|
---|
41 |
|
---|
42 | /** The memmove function copies n characters from the object pointed to by s2
|
---|
43 | into the object pointed to by s1. Copying takes place as if the n
|
---|
44 | characters from the object pointed to by s2 are first copied into a
|
---|
45 | temporary array of n characters that does not overlap the objects pointed
|
---|
46 | to by s1 and s2, and then the n characters from the temporary array are
|
---|
47 | copied into the object pointed to by s1.
|
---|
48 |
|
---|
49 | This is a version of memcpy that is guaranteed to work when s1 and s2
|
---|
50 | overlap. Since our implementation of memcpy already handles overlap,
|
---|
51 | memmove can be identical to memcpy.
|
---|
52 |
|
---|
53 | @return The memmove function returns the value of s1.
|
---|
54 | **/
|
---|
55 | void *
|
---|
56 | memmove(void *s1, const void *s2, size_t n)
|
---|
57 | {
|
---|
58 | return CopyMem( s1, s2, n);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /** The strcpy function copies the string pointed to by s2 (including the
|
---|
62 | terminating null character) into the array pointed to by s1. If copying
|
---|
63 | takes place between objects that overlap, the behavior is undefined.
|
---|
64 |
|
---|
65 | @return The strcpy function returns the value of s1.
|
---|
66 | **/
|
---|
67 | char *
|
---|
68 | strcpy(char * __restrict s1, const char * __restrict s2)
|
---|
69 | {
|
---|
70 | //char *s1ret = s1;
|
---|
71 |
|
---|
72 | //while ( *s1++ = *s2++) /* Empty Body */;
|
---|
73 | //return(s1ret);
|
---|
74 | return AsciiStrCpy( s1, s2);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /** The strncpy function copies not more than n characters (characters that
|
---|
78 | follow a null character are not copied) from the array pointed to by s2 to
|
---|
79 | the array pointed to by s1. If copying takes place between objects that
|
---|
80 | overlap, the behavior is undefined.
|
---|
81 |
|
---|
82 | If the array pointed to by s2 is a string that is shorter than n
|
---|
83 | characters, null characters are appended to the copy in the array pointed
|
---|
84 | to by s1, until n characters in all have been written.
|
---|
85 |
|
---|
86 | @return The strncpy function returns the value of s1.
|
---|
87 | **/
|
---|
88 | char *strncpy(char * __restrict s1, const char * __restrict s2, size_t n)
|
---|
89 | {
|
---|
90 | return AsciiStrnCpy( s1, s2, n);
|
---|
91 | //char *dest = s1;
|
---|
92 |
|
---|
93 | //while(n != 0) {
|
---|
94 | // --n;
|
---|
95 | // if((*dest++ = *s2++) == '\0') break;
|
---|
96 | //}
|
---|
97 | //while(n != 0) {
|
---|
98 | // *dest++ = '\0';
|
---|
99 | // --n;
|
---|
100 | //}
|
---|
101 | //return (s1);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /** The strncpyX function copies not more than n-1 characters (characters that
|
---|
105 | follow a null character are not copied) from the array pointed to by s2 to
|
---|
106 | the array pointed to by s1. Array s1 is guaranteed to be NULL terminated.
|
---|
107 | If copying takes place between objects that overlap,
|
---|
108 | the behavior is undefined.
|
---|
109 |
|
---|
110 | strncpyX exists because normal strncpy does not indicate if the copy was
|
---|
111 | terminated because of exhausting the buffer or reaching the end of s2.
|
---|
112 |
|
---|
113 | @return The strncpyX function returns 0 if the copy operation was
|
---|
114 | terminated because it reached the end of s1. Otherwise,
|
---|
115 | a non-zero value is returned indicating how many characters
|
---|
116 | remain in s1.
|
---|
117 | **/
|
---|
118 | int strncpyX(char * __restrict s1, const char * __restrict s2, size_t n)
|
---|
119 | {
|
---|
120 | int NumLeft;
|
---|
121 |
|
---|
122 | for( ; n != 0; --n) {
|
---|
123 | if((*s1++ = *s2++) == '\0') break;
|
---|
124 | }
|
---|
125 | NumLeft = (int)n;
|
---|
126 |
|
---|
127 | for( --s1; n != 0; --n) {
|
---|
128 | *s1++ = '\0';
|
---|
129 | }
|
---|
130 |
|
---|
131 | return NumLeft; // Zero if we ran out of buffer ( strlen(s1) < strlen(s2) )
|
---|
132 | }
|
---|
133 |
|
---|
134 | /** NetBSD Compatibility Function strdup creates a duplicate copy of a string. **/
|
---|
135 | char *
|
---|
136 | strdup(const char *str)
|
---|
137 | {
|
---|
138 | size_t len;
|
---|
139 | char *copy;
|
---|
140 |
|
---|
141 | len = strlen(str) + 1;
|
---|
142 | if ((copy = malloc(len)) == NULL)
|
---|
143 | return (NULL);
|
---|
144 | memcpy(copy, str, len);
|
---|
145 | return (copy);
|
---|
146 | }
|
---|