crcdnp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Library: libcrc
  3. * File: src/crcdnp.c
  4. * Author: Lammert Bies
  5. *
  6. * This file is licensed under the MIT License as stated below
  7. *
  8. * Copyright (c) 2005-2016 Lammert Bies
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all
  18. * copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. * SOFTWARE.
  27. *
  28. * Description
  29. * -----------
  30. * The source file src/crcdnp.c contains routines which are used to calculate
  31. * the CRC value in DNP messages.
  32. */
  33. #include <stdbool.h>
  34. #include <stdlib.h>
  35. #include "checksum.h"
  36. static void init_crcdnp_tab( void );
  37. static bool crc_tabdnp_init = false;
  38. static uint16_t crc_tabdnp[256];
  39. /*
  40. * uint16_t crc_dnp( const unsigned char* input_str, size_t num_bytes );
  41. *
  42. * The function crc_dnp() calculates the DNP CRC checksum of a provided byte
  43. * string in one pass.
  44. */
  45. uint16_t crc_dnp( const unsigned char *input_str, size_t num_bytes ) {
  46. uint16_t crc;
  47. uint16_t tmp;
  48. uint16_t short_c;
  49. uint16_t low_byte;
  50. uint16_t high_byte;
  51. const unsigned char *ptr;
  52. size_t a;
  53. if ( ! crc_tabdnp_init ) init_crcdnp_tab();
  54. crc = CRC_START_DNP;
  55. ptr = input_str;
  56. if ( ptr != NULL ) for (a=0; a<num_bytes; a++) {
  57. short_c = 0x00ff & (uint16_t) *ptr;
  58. tmp = crc ^ short_c;
  59. crc = (crc >> 8) ^ crc_tabdnp[ tmp & 0xff ];
  60. ptr++;
  61. }
  62. crc = ~crc;
  63. low_byte = (crc & 0xff00) >> 8;
  64. high_byte = (crc & 0x00ff) << 8;
  65. crc = low_byte | high_byte;
  66. return crc;
  67. } /* crc_dnp */
  68. /*
  69. * uint16_t update_crc_dnp( uint16_t crc, unsigned char c );
  70. *
  71. * The function update_crc_dnp() is called for every new byte in a row that
  72. * must be feeded tot the CRC-DNP routine to calculate the DNP CRC.
  73. */
  74. uint16_t update_crc_dnp( uint16_t crc, unsigned char c ) {
  75. uint16_t tmp;
  76. uint16_t short_c;
  77. short_c = 0x00ff & (uint16_t) c;
  78. if ( ! crc_tabdnp_init ) init_crcdnp_tab();
  79. tmp = crc ^ short_c;
  80. crc = (crc >> 8) ^ crc_tabdnp[ tmp & 0xff ];
  81. return crc;
  82. } /* update_crc_dnp */
  83. /*
  84. * static void init_crcdnp_tab( void );
  85. *
  86. * For better performance, the DNP CRC calculation uses a precompiled list with
  87. * bit patterns that are used in the XOR operation in the main routine. This
  88. * table is calculated once at the start of the program by the
  89. * init_crcdnp_tab() routine.
  90. */
  91. static void init_crcdnp_tab( void ) {
  92. int i;
  93. int j;
  94. uint16_t crc;
  95. uint16_t c;
  96. for (i=0; i<256; i++) {
  97. crc = 0;
  98. c = (uint16_t) i;
  99. for (j=0; j<8; j++) {
  100. if ( (crc ^ c) & 0x0001 ) crc = ( crc >> 1 ) ^ CRC_POLY_DNP;
  101. else crc = crc >> 1;
  102. c = c >> 1;
  103. }
  104. crc_tabdnp[i] = crc;
  105. }
  106. crc_tabdnp_init = true;
  107. } /* init_crcdnp_tab */