summaryrefslogtreecommitdiff
path: root/StdLib/LibC/String/Copying.c
blob: e27519e24c0465902a32c8fb8cdae120c05a8ee6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/** @file
    Copying Functions for <string.h>.

    Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
    This program and the accompanying materials are licensed and made available under
    the terms and conditions of the BSD License that accompanies this distribution.
    The full text of the license may be found at
    http://opensource.org/licenses/bsd-license.php.

    THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
    WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include  <Uefi.h>
#include  <Library/BaseLib.h>
#include  <Library/BaseMemoryLib.h>

#include  <LibConfig.h>

#include  <stdlib.h>
#include  <string.h>

/** Do not define memcpy for IPF+GCC or ARM+GCC builds.
    For IPF, using a GCC compiler, the memcpy function is converted to
    CopyMem by objcpy during build.
    For ARM, the memcpy function is provided by the CompilerIntrinsics library.
**/
#if !((defined(MDE_CPU_IPF) || defined(MDE_CPU_ARM)) && defined(__GNUC__))
/** The memcpy function copies n characters from the object pointed to by s2
    into the object pointed to by s1.

    The implementation is reentrant and handles the case where s2 overlaps s1.

    @return   The memcpy function returns the value of s1.
**/
void *
memcpy(void * __restrict s1, const void * __restrict s2, size_t n)
{
  return CopyMem( s1, s2, n);
}
#endif  /* !(defined(MDE_CPU_IPF) && defined(__GCC)) */

/** The memmove function copies n characters from the object pointed to by s2
    into the object pointed to by s1. Copying takes place as if the n
    characters from the object pointed to by s2 are first copied into a
    temporary array of n characters that does not overlap the objects pointed
    to by s1 and s2, and then the n characters from the temporary array are
    copied into the object pointed to by s1.

    This is a version of memcpy that is guaranteed to work when s1 and s2
    overlap.  Since our implementation of memcpy already handles overlap,
    memmove can be identical to memcpy.

    @return   The memmove function returns the value of s1.
**/
void *
memmove(void *s1, const void *s2, size_t n)
{
  return CopyMem( s1, s2, n);
}

/** The strcpy function copies the string pointed to by s2 (including the
    terminating null character) into the array pointed to by s1. If copying
    takes place between objects that overlap, the behavior is undefined.

    @return   The strcpy function returns the value of s1.
**/
char *
strcpy(char * __restrict s1, const char * __restrict s2)
{
  //char *s1ret = s1;

  //while ( *s1++ = *s2++)  /* Empty Body */;
  //return(s1ret);
  return AsciiStrCpy( s1, s2);
}

/** The strncpy function copies not more than n characters (characters that
    follow a null character are not copied) from the array pointed to by s2 to
    the array pointed to by s1. If copying takes place between objects that
    overlap, the behavior is undefined.

    If the array pointed to by s2 is a string that is shorter than n
    characters, null characters are appended to the copy in the array pointed
    to by s1, until n characters in all have been written.

    @return   The strncpy function returns the value of s1.
**/
char     *strncpy(char * __restrict s1, const char * __restrict s2, size_t n)
{
  return AsciiStrnCpy( s1, s2, n);
  //char *dest = s1;

  //while(n != 0) {
  //  --n;
  //  if((*dest++ = *s2++) == '\0')  break;
  //}
  //while(n != 0) {
  //  *dest++ = '\0';
  //  --n;
  //}
  //return (s1);
}

/** The strncpyX function copies not more than n-1 characters (characters that
    follow a null character are not copied) from the array pointed to by s2 to
    the array pointed to by s1. Array s1 is guaranteed to be NULL terminated.
    If copying takes place between objects that overlap,
    the behavior is undefined.

    strncpyX exists because normal strncpy does not indicate if the copy was
    terminated because of exhausting the buffer or reaching the end of s2.

    @return   The strncpyX function returns 0 if the copy operation was
              terminated because it reached the end of s1.  Otherwise,
              a non-zero value is returned indicating how many characters
              remain in s1.
**/
int strncpyX(char * __restrict s1, const char * __restrict s2, size_t n)
{
  int NumLeft;

  for( ; n != 0; --n) {
    if((*s1++ = *s2++) == '\0')  break;
  }
  NumLeft = (int)n;

  for( --s1; n != 0; --n) {
    *s1++ = '\0';
  }

  return NumLeft;   // Zero if we ran out of buffer ( strlen(s1) < strlen(s2) )
}

/** NetBSD Compatibility Function strdup creates a duplicate copy of a string. **/
char *
strdup(const char *str)
{
  size_t len;
  char *copy;

  len = strlen(str) + 1;
  if ((copy = malloc(len)) == NULL)
    return (NULL);
  memcpy(copy, str, len);
  return (copy);
}