xref: /netbsd-src/sbin/disklabel/dkcksum.c (revision af8f0546ca619cdb5607560c9088cfe62df6bb1d)
1*af8f0546Smatt /*	$NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp $	*/
20114e805Scgd 
361f28255Scgd /*-
47322d6fbSmycroft  * Copyright (c) 1991, 1993
57322d6fbSmycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15bf07c871Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
321726c417Sdyoung #if HAVE_NBTOOL_CONFIG_H
331726c417Sdyoung #include "nbtool_config.h"
341726c417Sdyoung #endif
351726c417Sdyoung 
365ec7cc78Schristos #include <sys/cdefs.h>
3761f28255Scgd #ifndef lint
380114e805Scgd #if 0
390114e805Scgd static char sccsid[] = "@(#)dkcksum.c	8.1 (Berkeley) 6/5/93";
400114e805Scgd #else
41*af8f0546Smatt __RCSID("$NetBSD: dkcksum.c,v 1.14 2013/05/03 16:05:12 matt Exp $");
420114e805Scgd #endif
4361f28255Scgd #endif /* not lint */
4461f28255Scgd 
4561f28255Scgd #include <sys/types.h>
461726c417Sdyoung #if HAVE_NBTOOL_CONFIG_H
471726c417Sdyoung #include <nbinclude/sys/disklabel.h>
481726c417Sdyoung #else
4961f28255Scgd #include <sys/disklabel.h>
501726c417Sdyoung #endif /* HAVE_NBTOOL_CONFIG_H */
515ec7cc78Schristos #include "dkcksum.h"
5261f28255Scgd 
53ab4082e0Stsutsui uint16_t
dkcksum(const struct disklabel * lp)54*af8f0546Smatt dkcksum(const struct disklabel *lp)
5561f28255Scgd {
56d3724ba4Stsutsui 
57d3724ba4Stsutsui 	return dkcksum_sized(lp, lp->d_npartitions);
58d3724ba4Stsutsui }
59d3724ba4Stsutsui 
60d3724ba4Stsutsui uint16_t
dkcksum_sized(const struct disklabel * lp,size_t npartitions)61*af8f0546Smatt dkcksum_sized(const struct disklabel *lp, size_t npartitions)
62d3724ba4Stsutsui {
63*af8f0546Smatt 	const uint16_t *start, *end;
64ab4082e0Stsutsui 	uint16_t sum;
6561f28255Scgd 
6635ccd223Slukem 	sum = 0;
67*af8f0546Smatt 	start = (const uint16_t *)lp;
68*af8f0546Smatt 	end = (const uint16_t *)&lp->d_partitions[npartitions];
69*af8f0546Smatt 	while (start < end) {
7061f28255Scgd 		sum ^= *start++;
71*af8f0546Smatt 	}
72ab4082e0Stsutsui 	return sum;
7361f28255Scgd }
74