xref: /openbsd-src/sys/lib/libsa/memcmp.c (revision d5e051809668e6956874358e7339a26836bff6e6)
1*d5e05180Sreyk /*	$OpenBSD: memcmp.c,v 1.8 2016/11/27 13:57:32 reyk Exp $	*/
2*d5e05180Sreyk 
3e31e80fdSmickey /*-
4584c0368Sderaadt  * Copyright (c) 1990 The Regents of the University of California.
5e31e80fdSmickey  * All rights reserved.
6e31e80fdSmickey  *
7584c0368Sderaadt  * This code is derived from software contributed to Berkeley by
8584c0368Sderaadt  * Chris Torek.
9584c0368Sderaadt  *
10e31e80fdSmickey  * Redistribution and use in source and binary forms, with or without
11e31e80fdSmickey  * modification, are permitted provided that the following conditions
12e31e80fdSmickey  * are met:
13e31e80fdSmickey  * 1. Redistributions of source code must retain the above copyright
14e31e80fdSmickey  *    notice, this list of conditions and the following disclaimer.
15e31e80fdSmickey  * 2. Redistributions in binary form must reproduce the above copyright
16e31e80fdSmickey  *    notice, this list of conditions and the following disclaimer in the
17e31e80fdSmickey  *    documentation and/or other materials provided with the distribution.
1829295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
19584c0368Sderaadt  *    may be used to endorse or promote products derived from this software
20584c0368Sderaadt  *    without specific prior written permission.
21e31e80fdSmickey  *
22584c0368Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23584c0368Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24584c0368Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25e31e80fdSmickey  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26e31e80fdSmickey  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27e31e80fdSmickey  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28e31e80fdSmickey  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29e31e80fdSmickey  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30e31e80fdSmickey  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31e31e80fdSmickey  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e31e80fdSmickey  * SUCH DAMAGE.
33e31e80fdSmickey  */
34e31e80fdSmickey 
35d425be43Smillert #include "stand.h"
36584c0368Sderaadt 
37584c0368Sderaadt /*
38584c0368Sderaadt  * Compare memory regions.
39584c0368Sderaadt  */
40e31e80fdSmickey int
memcmp(const void * s1,const void * s2,size_t n)4153c346fbSderaadt memcmp(const void *s1, const void *s2, size_t n)
42e31e80fdSmickey {
43584c0368Sderaadt 	if (n != 0) {
4453c346fbSderaadt 		const unsigned char *p1 = s1, *p2 = s2;
45584c0368Sderaadt 
46584c0368Sderaadt 		do {
47584c0368Sderaadt 			if (*p1++ != *p2++)
48584c0368Sderaadt 				return (*--p1 - *--p2);
49584c0368Sderaadt 		} while (--n != 0);
50584c0368Sderaadt 	}
51584c0368Sderaadt 	return (0);
52e31e80fdSmickey }
53