Home | History | Annotate | Download | only in lib
      1 /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  */
      5 
      6 #include "sysincludes.h"
      7 
      8 #include "crc8.h"
      9 
     10 /**
     11  * Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial.  A table-based
     12  * algorithm would be faster, but for only a few bytes it isn't worth the code
     13  * size. */
     14 uint8_t Crc8(const void *vptr, int len)
     15 {
     16 	const uint8_t *data = vptr;
     17 	unsigned crc = 0;
     18 	int i, j;
     19 
     20 	for (j = len; j; j--, data++) {
     21 		crc ^= (*data << 8);
     22 		for(i = 8; i; i--) {
     23 			if (crc & 0x8000)
     24 				crc ^= (0x1070 << 3);
     25 			crc <<= 1;
     26 		}
     27 	}
     28 
     29 	return (uint8_t)(crc >> 8);
     30 }
     31