Home | History | Annotate | Download | only in 2lib
      1 /* Copyright (c) 2014 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 "2sysincludes.h"
      7 #include "2crc8.h"
      8 
      9 uint8_t vb2_crc8(const void *vptr, uint32_t size)
     10 {
     11 	const uint8_t *data = vptr;
     12 	unsigned crc = 0;
     13 	uint32_t i, j;
     14 
     15 	/*
     16 	 * Calculate CRC-8 directly.  A table-based algorithm would be faster,
     17 	 * but for only a few bytes it isn't worth the code size.
     18 	 */
     19 	for (j = size; j; j--, data++) {
     20 		crc ^= (*data << 8);
     21 		for(i = 8; i; i--) {
     22 			if (crc & 0x8000)
     23 				crc ^= (0x1070 << 3);
     24 			crc <<= 1;
     25 		}
     26 	}
     27 
     28 	return (uint8_t)(crc >> 8);
     29 }
     30