crc.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. extern "C" unsigned long crc32_le(unsigned long crc_in, unsigned char const* data, unsigned int length)
  16. {
  17. uint32_t i;
  18. bool bit;
  19. uint8_t c;
  20. uint32_t crc = (uint32_t) crc_in;
  21. while (length--) {
  22. c = *data++;
  23. for (i = 0x80; i > 0; i >>= 1) {
  24. bit = crc & 0x80000000;
  25. if (c & i) {
  26. bit = !bit;
  27. }
  28. crc <<= 1;
  29. if (bit) {
  30. crc ^= 0x04c11db7;
  31. }
  32. }
  33. }
  34. return crc;
  35. }