crc32-main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin
  4. * cleaned up code to current version of sparse and added the slicing-by-8
  5. * algorithm to the closely similar existing slicing-by-4 algorithm.
  6. *
  7. * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com>
  8. * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks!
  9. * Code was from the public domain, copyright abandoned. Code was
  10. * subsequently included in the kernel, thus was re-licensed under the
  11. * GNU GPL v2.
  12. *
  13. * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com>
  14. * Same crc32 function was used in 5 other places in the kernel.
  15. * I made one version, and deleted the others.
  16. * There are various incantations of crc32(). Some use a seed of 0 or ~0.
  17. * Some xor at the end with ~0. The generic crc32() function takes
  18. * seed as an argument, and doesn't xor at the end. Then individual
  19. * users can do whatever they need.
  20. * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0.
  21. * fs/jffs2 uses seed 0, doesn't xor with ~0.
  22. * fs/partitions/efi.c uses seed ~0, xor's with ~0.
  23. */
  24. /* see: Documentation/staging/crc32.rst for a description of algorithms */
  25. #include <linux/crc32.h>
  26. #include <linux/export.h>
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include "crc32table.h"
  30. static inline u32 __maybe_unused
  31. crc32_le_base(u32 crc, const u8 *p, size_t len)
  32. {
  33. while (len--)
  34. crc = (crc >> 8) ^ crc32table_le[(crc & 255) ^ *p++];
  35. return crc;
  36. }
  37. static inline u32 __maybe_unused
  38. crc32_be_base(u32 crc, const u8 *p, size_t len)
  39. {
  40. while (len--)
  41. crc = (crc << 8) ^ crc32table_be[(crc >> 24) ^ *p++];
  42. return crc;
  43. }
  44. static inline u32 __maybe_unused
  45. crc32c_base(u32 crc, const u8 *p, size_t len)
  46. {
  47. while (len--)
  48. crc = (crc >> 8) ^ crc32ctable_le[(crc & 255) ^ *p++];
  49. return crc;
  50. }
  51. #ifdef CONFIG_CRC32_ARCH
  52. #include "crc32.h" /* $(SRCARCH)/crc32.h */
  53. u32 crc32_optimizations(void)
  54. {
  55. return crc32_optimizations_arch();
  56. }
  57. EXPORT_SYMBOL(crc32_optimizations);
  58. #else
  59. #define crc32_le_arch crc32_le_base
  60. #define crc32_be_arch crc32_be_base
  61. #define crc32c_arch crc32c_base
  62. #endif
  63. u32 crc32_le(u32 crc, const void *p, size_t len)
  64. {
  65. return crc32_le_arch(crc, p, len);
  66. }
  67. EXPORT_SYMBOL(crc32_le);
  68. u32 crc32_be(u32 crc, const void *p, size_t len)
  69. {
  70. return crc32_be_arch(crc, p, len);
  71. }
  72. EXPORT_SYMBOL(crc32_be);
  73. u32 crc32c(u32 crc, const void *p, size_t len)
  74. {
  75. return crc32c_arch(crc, p, len);
  76. }
  77. EXPORT_SYMBOL(crc32c);
  78. #ifdef crc32_mod_init_arch
  79. static int __init crc32_mod_init(void)
  80. {
  81. crc32_mod_init_arch();
  82. return 0;
  83. }
  84. subsys_initcall(crc32_mod_init);
  85. static void __exit crc32_mod_exit(void)
  86. {
  87. }
  88. module_exit(crc32_mod_exit);
  89. #endif
  90. MODULE_DESCRIPTION("CRC32 library functions");
  91. MODULE_LICENSE("GPL");