/*-
* Copyright (c) 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* James W. Williams of NASA Goddard Space Flight Center.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Compute a POSIX 1003.2 checksum. This routine has been broken out so that
* other programs can use it. It takes a file descriptor to read from and
* locations to store the crc and the number of bytes read. It returns 0 on
* success and 1 on failure. Errno is set on failure.
*/
uint32_t crc_total = ~0u; /* The crc over a number of files. */
thecrc = len = crctot = 0;
if (sflag)
crctot = ~crc_total;
while ((nr = read(fd, buf, sizeof(buf))) > 0)
if (sflag) {
for (len += (uint32_t)nr, p = buf; nr--; ++p) {
COMPUTE(thecrc, *p);
COMPUTE(crctot, *p);
}
} else {
for (len += (uint32_t)nr, p = buf; nr--; ++p)
COMPUTE(thecrc, *p);
}
if (nr < 0)
return 1;
*clen = len;
/* Include the length of the file. */
if (sflag) {
for (; len != 0; len >>= 8) {
COMPUTE(thecrc, len & 0xff);
COMPUTE(crctot, len & 0xff);
}
} else {
for (; len != 0; len >>= 8)
COMPUTE(thecrc, len & 0xff);
}