Blame ptl_ips/ips_crc32.c

Packit 961e70
/* The code in this file was derived from crc32.c in zlib 1.2.3, and
Packit 961e70
   modified from its original form to suit our requirements. The zlib
Packit 961e70
   license and crc32.c copyright and credits are preserved below. */
Packit 961e70
Packit 961e70
/* zlib.h -- interface of the 'zlib' general purpose compression library
Packit 961e70
  version 1.2.3, July 18th, 2005
Packit 961e70
Packit 961e70
  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
Packit 961e70
Packit 961e70
  This software is provided 'as-is', without any express or implied
Packit 961e70
  warranty.  In no event will the authors be held liable for any damages
Packit 961e70
  arising from the use of this software.
Packit 961e70
Packit 961e70
  Permission is granted to anyone to use this software for any purpose,
Packit 961e70
  including commercial applications, and to alter it and redistribute it
Packit 961e70
  freely, subject to the following restrictions:
Packit 961e70
Packit 961e70
  1. The origin of this software must not be misrepresented; you must not
Packit 961e70
     claim that you wrote the original software. If you use this software
Packit 961e70
     in a product, an acknowledgment in the product documentation would be
Packit 961e70
     appreciated but is not required.
Packit 961e70
  2. Altered source versions must be plainly marked as such, and must not be
Packit 961e70
     misrepresented as being the original software.
Packit 961e70
  3. This notice may not be removed or altered from any source distribution.
Packit 961e70
Packit 961e70
  Jean-loup Gailly        Mark Adler
Packit 961e70
  jloup@gzip.org          madler@alumni.caltech.edu
Packit 961e70
Packit 961e70
  The data format used by the zlib library is described by RFCs (Request for
Packit 961e70
  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
Packit 961e70
  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
Packit 961e70
*/
Packit 961e70
Packit 961e70
/* crc32.c -- compute the CRC-32 of a data stream
Packit 961e70
 * Copyright (C) 1995-2005 Mark Adler
Packit 961e70
 * For conditions of distribution and use, see copyright notice in zlib.h
Packit 961e70
 *
Packit 961e70
 * Thanks to Rodney Brown <rbrown64@csc.com.au> for his contribution of faster
Packit 961e70
 * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing
Packit 961e70
 * tables for updating the shift register in one step with three exclusive-ors
Packit 961e70
 * instead of four steps with four exclusive-ors.  This results in about a
Packit 961e70
 * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3.
Packit 961e70
 */
Packit 961e70
Packit 961e70
#include "psm_user.h"
Packit 961e70
#include "psm2_hal.h"
Packit 961e70
#include "ips_proto.h"
Packit 961e70
#include "ips_proto_internal.h"
Packit 961e70
Packit 961e70
/* Table of CRCs of all 8-bit messages. */
Packit 961e70
static uint32_t crc_table[256];
Packit 961e70
Packit 961e70
/* Flag: has the table been computed? Initially false. */
Packit 961e70
static int crc_table_computed;
Packit 961e70
Packit 961e70
/* Make the table for a fast CRC. */
Packit 961e70
static void make_crc_table(void)
Packit 961e70
{
Packit 961e70
	uint32_t c;
Packit 961e70
	int n, k;
Packit 961e70
Packit 961e70
	for (n = 0; n < 256; n++) {
Packit 961e70
		c = (uint32_t) n;
Packit 961e70
		for (k = 0; k < 8; k++) {
Packit 961e70
			if (c & 1)
Packit 961e70
				c = 0xedb88320 ^ (c >> 1);
Packit 961e70
			else
Packit 961e70
				c = c >> 1;
Packit 961e70
		}
Packit 961e70
		crc_table[n] = c;
Packit 961e70
	}
Packit 961e70
	crc_table_computed = 1;
Packit 961e70
}
Packit 961e70
Packit 961e70
/* Update a running CRC with the bytes buf[0..len-1]--the CRC
Packit 961e70
 * should be initialized to all 1's, and the transmitted value
Packit 961e70
 * is the 1's complement of the final running CRC (see the
Packit 961e70
 * crc() routine below)).
Packit 961e70
 */
Packit 961e70
Packit 961e70
uint32_t ips_crc_calculate(uint32_t len, uint8_t *data, uint32_t crc)
Packit 961e70
{
Packit 961e70
	uint32_t c = crc;
Packit 961e70
	uint32_t n;
Packit 961e70
Packit 961e70
	if (!crc_table_computed) {
Packit 961e70
		make_crc_table();
Packit 961e70
	}
Packit 961e70
	for (n = 0; n < len; n++) {
Packit 961e70
		c = crc_table[(c ^ data[n]) & 0xff] ^ (c >> 8);
Packit 961e70
	}
Packit 961e70
	return c;
Packit 961e70
}