xref: /openbsd-src/sys/lib/libkern/memcmp.c (revision 249415e0143d372032bbbb017e8451c2888c674f)
1*249415e0Skettenis /*	$OpenBSD: memcmp.c,v 1.7 2017/01/24 08:09:05 kettenis Exp $	*/
26aca4c3cSmillert 
36aca4c3cSmillert /*-
46aca4c3cSmillert  * Copyright (c) 1990 The Regents of the University of California.
56aca4c3cSmillert  * All rights reserved.
66aca4c3cSmillert  *
76aca4c3cSmillert  * This code is derived from software contributed to Berkeley by
86aca4c3cSmillert  * Chris Torek.
96aca4c3cSmillert  *
106aca4c3cSmillert  * Redistribution and use in source and binary forms, with or without
116aca4c3cSmillert  * modification, are permitted provided that the following conditions
126aca4c3cSmillert  * are met:
136aca4c3cSmillert  * 1. Redistributions of source code must retain the above copyright
146aca4c3cSmillert  *    notice, this list of conditions and the following disclaimer.
156aca4c3cSmillert  * 2. Redistributions in binary form must reproduce the above copyright
166aca4c3cSmillert  *    notice, this list of conditions and the following disclaimer in the
176aca4c3cSmillert  *    documentation and/or other materials provided with the distribution.
1829295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
196aca4c3cSmillert  *    may be used to endorse or promote products derived from this software
206aca4c3cSmillert  *    without specific prior written permission.
216aca4c3cSmillert  *
226aca4c3cSmillert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
236aca4c3cSmillert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
246aca4c3cSmillert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
256aca4c3cSmillert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
266aca4c3cSmillert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
276aca4c3cSmillert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
286aca4c3cSmillert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
296aca4c3cSmillert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
306aca4c3cSmillert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
316aca4c3cSmillert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
326aca4c3cSmillert  * SUCH DAMAGE.
336aca4c3cSmillert  */
346aca4c3cSmillert 
356aca4c3cSmillert #include <lib/libkern/libkern.h>
366aca4c3cSmillert 
37*249415e0Skettenis #undef memcmp
38*249415e0Skettenis 
396aca4c3cSmillert /*
406aca4c3cSmillert  * Compare memory regions.
416aca4c3cSmillert  */
426aca4c3cSmillert int
memcmp(const void * s1,const void * s2,size_t n)43a26aa419Sderaadt memcmp(const void *s1, const void *s2, size_t n)
446aca4c3cSmillert {
456aca4c3cSmillert 	if (n != 0) {
46a26aa419Sderaadt 		const unsigned char *p1 = s1, *p2 = s2;
476aca4c3cSmillert 
486aca4c3cSmillert 		do {
496aca4c3cSmillert 			if (*p1++ != *p2++)
506aca4c3cSmillert 				return (*--p1 - *--p2);
516aca4c3cSmillert 		} while (--n != 0);
526aca4c3cSmillert 	}
536aca4c3cSmillert 	return (0);
546aca4c3cSmillert }
55