1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 1994-2009 Red Hat, Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * 3. Neither the name of the copyright holder nor the names of its 17 * contributors may be used to endorse or promote products derived from this 18 * software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 FUNCTION 35 <<memcmp>>---compare two memory areas 36 37 INDEX 38 memcmp 39 40 ANSI_SYNOPSIS 41 #include <string.h> 42 int memcmp(const void *<[s1]>, const void *<[s2]>, size_t <[n]>); 43 44 TRAD_SYNOPSIS 45 #include <string.h> 46 int memcmp(<[s1]>, <[s2]>, <[n]>) 47 void *<[s1]>; 48 void *<[s2]>; 49 size_t <[n]>; 50 51 DESCRIPTION 52 This function compares not more than <[n]> characters of the 53 object pointed to by <[s1]> with the object pointed to by <[s2]>. 54 55 RETURNS 56 The function returns an integer greater than, equal to or 57 less than zero according to whether the object pointed to by 58 <[s1]> is greater than, equal to or less than the object 59 pointed to by <[s2]>. 60 61 PORTABILITY 62 <<memcmp>> is ANSI C. 63 64 <<memcmp>> requires no supporting OS subroutines. 65 66 QUICKREF 67 memcmp ansi pure 68 */ 69 70 #include "_ansi.h" 71 #include <string.h> 72 73 /* Nonzero if either X or Y is not aligned on a "long" boundary. */ 74 #define UNALIGNED(X, Y) \ 75 (((long)X & (sizeof(long) - 1)) | ((long)Y & (sizeof(long) - 1))) 76 77 /* How many bytes are copied each iteration of the word copy loop. */ 78 #define LBLOCKSIZE (sizeof(long)) 79 80 /* Threshhold for punting to the byte copier. */ 81 #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE) 82 83 int 84 _DEFUN(memcmp, (m1, m2, n), _CONST _PTR m1 _AND _CONST _PTR m2 _AND size_t n) 85 { 86 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) 87 unsigned char *s1 = (unsigned char *)m1; 88 unsigned char *s2 = (unsigned char *)m2; 89 90 while (n--) { 91 if (*s1 != *s2) 92 return *s1 - *s2; 93 s1++; 94 s2++; 95 } 96 return 0; 97 #else 98 unsigned char *s1 = (unsigned char *)m1; 99 unsigned char *s2 = (unsigned char *)m2; 100 unsigned long *a1; 101 unsigned long *a2; 102 103 /* If the size is too small, or either pointer is unaligned, 104 then we punt to the byte compare loop. Hopefully this will 105 not turn up in inner loops. */ 106 if (!TOO_SMALL(n) && !UNALIGNED(s1, s2)) { 107 /* 108 * Otherwise, load and compare the blocks of memory one word at 109 * a time. 110 */ 111 a1 = (unsigned long *)s1; 112 a2 = (unsigned long *)s2; 113 while (n >= LBLOCKSIZE) { 114 if (*a1 != *a2) 115 break; 116 a1++; 117 a2++; 118 n -= LBLOCKSIZE; 119 } 120 121 /* check m mod LBLOCKSIZE remaining characters */ 122 123 s1 = (unsigned char *)a1; 124 s2 = (unsigned char *)a2; 125 } 126 127 while (n--) { 128 if (*s1 != *s2) 129 return *s1 - *s2; 130 s1++; 131 s2++; 132 } 133 134 return 0; 135 #endif /* not PREFER_SIZE_OVER_SPEED */ 136 } 137