1/** @file
2    Comparison Functions for <string.h>.
3
4  Copyright (c) 2010, 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  <ctype.h>
20#include  <string.h>
21
22/** The memcmp function compares the first n characters of the object pointed
23    to by s1 to the first n characters of the object pointed to by s2.
24
25    @return   The memcmp function returns an integer greater than, equal to, or
26              less than zero, accordingly as the object pointed to by s1 is
27              greater than, equal to, or less than the object pointed to by s2.
28**/
29int       memcmp(const void *s1, const void *s2, size_t n)
30{
31  return (int)CompareMem( s1, s2, n);
32}
33
34/** The strcmp function compares the string pointed to by s1 to the string
35    pointed to by s2.
36
37    @return   The strcmp function returns an integer greater than, equal to, or
38              less than zero, accordingly as the string pointed to by s1 is
39              greater than, equal to, or less than the string pointed to by s2.
40**/
41int       strcmp(const char *s1, const char *s2)
42{
43  return (int)AsciiStrCmp( s1, s2);
44}
45
46/** The strcoll function compares the string pointed to by s1 to the string
47    pointed to by s2, both interpreted as appropriate to the LC_COLLATE
48    category of the current locale.
49
50    @return   The strcoll function returns an integer greater than, equal to,
51              or less than zero, accordingly as the string pointed to by s1 is
52              greater than, equal to, or less than the string pointed to by s2
53              when both are interpreted as appropriate to the current locale.
54**/
55int       strcoll(const char *s1, const char *s2)
56{
57  /* LC_COLLATE is unimplemented, hence always "C" */
58  return (strcmp(s1, s2));
59}
60
61/** The strncmp function compares not more than n characters (characters that
62    follow a null character are not compared) from the array pointed to by s1
63    to the array pointed to by s2.
64
65    @return   The strncmp function returns an integer greater than, equal to,
66              or less than zero, accordingly as the possibly null-terminated
67              array pointed to by s1 is greater than, equal to, or less than
68              the possibly null-terminated array pointed to by s2.
69**/
70int       strncmp(const char *s1, const char *s2, size_t n)
71{
72  return (int)AsciiStrnCmp( s1, s2, n);
73}
74
75/** The strxfrm function transforms the string pointed to by Src and places the
76    resulting string into the array pointed to by Dest. The transformation is
77    such that if the strcmp function is applied to two transformed strings, it
78    returns a value greater than, equal to, or less than zero, corresponding to
79    the result of the strcoll function applied to the same two original
80    strings. No more than Len characters are placed into the resulting array
81    pointed to by Dest, including the terminating null character. If Len is zero,
82    Dest is permitted to be a null pointer. If copying takes place between
83    objects that overlap, the behavior is undefined.
84
85    @return   The strxfrm function returns the length of the transformed string
86              (not including the terminating null character). If the value
87              returned is Len or more, the contents of the array pointed to by Dest
88              are indeterminate.
89**/
90size_t    strxfrm(char * __restrict Dest, const char * __restrict Src, size_t Len)
91{
92  size_t srclen, copysize;
93
94  /*
95  * Since locales are unimplemented, this is just a copy.
96  */
97  srclen = strlen(Src);
98  if (Len != 0) {
99    copysize = srclen < Len ? srclen : Len - 1;
100    (void)memcpy(Dest, Src, copysize);
101    Dest[copysize] = 0;
102  }
103  return (srclen);
104}
105
106/** Case agnostic string comparison for NetBSD compatibility. **/
107int
108strcasecmp(const char *s1, const char *s2)
109{
110  return (int)AsciiStriCmp( s1, s2);
111}
112