PageRenderTime 89ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/base/src/utils/crc.cpp

http://github.com/gamman/MRPT
C++ | 80 lines | 35 code | 12 blank | 33 comment | 1 complexity | 0dbbcd37f60c33482650695b04fc6769 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. /* +---------------------------------------------------------------------------+
  2. | The Mobile Robot Programming Toolkit (MRPT) C++ library |
  3. | |
  4. | http://www.mrpt.org/ |
  5. | |
  6. | Copyright (C) 2005-2011 University of Malaga |
  7. | |
  8. | This software was written by the Machine Perception and Intelligent |
  9. | Robotics Lab, University of Malaga (Spain). |
  10. | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
  11. | |
  12. | This file is part of the MRPT project. |
  13. | |
  14. | MRPT is free software: you can redistribute it and/or modify |
  15. | it under the terms of the GNU General Public License as published by |
  16. | the Free Software Foundation, either version 3 of the License, or |
  17. | (at your option) any later version. |
  18. | |
  19. | MRPT is distributed in the hope that it will be useful, |
  20. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  21. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  22. | GNU General Public License for more details. |
  23. | |
  24. | You should have received a copy of the GNU General Public License |
  25. | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
  26. | |
  27. +---------------------------------------------------------------------------+ */
  28. #include <mrpt/base.h> // Precompiled headers
  29. #include <mrpt/utils/crc.h>
  30. using namespace mrpt::utils;
  31. using namespace std;
  32. /*---------------------------------------------------------------
  33. CRC16
  34. ---------------------------------------------------------------*/
  35. uint16_t mrpt::utils::compute_CRC16( const std::vector<uint8_t> &data, const uint16_t gen_pol )
  36. {
  37. return compute_CRC16(&data[0],data.size(),gen_pol);
  38. }
  39. /*---------------------------------------------------------------
  40. CRC16
  41. ---------------------------------------------------------------*/
  42. uint16_t mrpt::utils::compute_CRC16(
  43. const uint8_t *data,
  44. const size_t len_,
  45. const uint16_t gen_pol)
  46. {
  47. uint16_t uCrc16;
  48. uint8_t abData[2];
  49. size_t len = len_;
  50. uCrc16 = 0;
  51. abData[0] = 0;
  52. while(len-- )
  53. {
  54. abData[1] = abData[0];
  55. abData[0] = *data++;
  56. if( uCrc16 & 0x8000 )
  57. {
  58. uCrc16 = (uCrc16 & 0x7fff) << 1;
  59. uCrc16 ^= gen_pol;
  60. }
  61. else
  62. {
  63. uCrc16 <<= 1;
  64. }
  65. uCrc16 ^= (abData[0] | (abData[1]<<8));
  66. }
  67. return uCrc16;
  68. }