crc32.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJLIB_UTIL_CRC32_H__
  20. #define __PJLIB_UTIL_CRC32_H__
  21. /**
  22. * @file crc32.h
  23. * @brief CRC32 implementation
  24. */
  25. #include <pjlib-util/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJLIB_UTIL_CRC32 CRC32 (Cyclic Redundancy Check)
  29. * @ingroup PJLIB_UTIL_ENCRYPTION
  30. * @{
  31. * This implements CRC32 algorithm. See ITU-T V.42 for the formal
  32. * specification.
  33. */
  34. /** CRC32 context. */
  35. typedef struct pj_crc32_context
  36. {
  37. pj_uint32_t crc_state; /**< Current state. */
  38. } pj_crc32_context;
  39. /**
  40. * Initialize CRC32 context.
  41. *
  42. * @param ctx CRC32 context.
  43. */
  44. PJ_DECL(void) pj_crc32_init(pj_crc32_context *ctx);
  45. /**
  46. * Feed data incrementally to the CRC32 algorithm.
  47. *
  48. * @param ctx CRC32 context.
  49. * @param data Input data.
  50. * @param nbytes Length of the input data.
  51. *
  52. * @return The current CRC32 value.
  53. */
  54. PJ_DECL(pj_uint32_t) pj_crc32_update(pj_crc32_context *ctx,
  55. const pj_uint8_t *data,
  56. pj_size_t nbytes);
  57. /**
  58. * Finalize CRC32 calculation and retrieve the CRC32 value.
  59. *
  60. * @param ctx CRC32 context.
  61. *
  62. * @return The current CRC value.
  63. */
  64. PJ_DECL(pj_uint32_t) pj_crc32_final(pj_crc32_context *ctx);
  65. /**
  66. * Perform one-off CRC32 calculation to the specified data.
  67. *
  68. * @param data Input data.
  69. * @param nbytes Length of input data.
  70. *
  71. * @return CRC value of the data.
  72. */
  73. PJ_DECL(pj_uint32_t) pj_crc32_calc(const pj_uint8_t *data,
  74. pj_size_t nbytes);
  75. /**
  76. * @}
  77. */
  78. PJ_END_DECL
  79. #endif /* __PJLIB_UTIL_CRC32_H__ */