2crc8.c 660 B

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