bit_lib.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #pragma once
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. typedef enum {
  9. BitLibParityEven,
  10. BitLibParityOdd,
  11. BitLibParityAlways0,
  12. BitLibParityAlways1,
  13. } BitLibParity;
  14. /** @brief Increment and wrap around a value.
  15. * @param index value to increment
  16. * @param length wrap-around range
  17. */
  18. #define bit_lib_increment_index(index, length) (index = (((index) + 1) % (length)))
  19. /** @brief Test if a bit is set.
  20. * @param data value to test
  21. * @param index bit index to test
  22. */
  23. #define bit_lib_bit_is_set(data, index) ((data & (1 << (index))) != 0)
  24. /** @brief Test if a bit is not set.
  25. * @param data value to test
  26. * @param index bit index to test
  27. */
  28. #define bit_lib_bit_is_not_set(data, index) ((data & (1 << (index))) == 0)
  29. /** @brief Push a bit into a byte array.
  30. * @param data array to push bit into
  31. * @param data_size array size
  32. * @param bit bit to push
  33. */
  34. void bit_lib_push_bit(uint8_t* data, size_t data_size, bool bit);
  35. /** @brief Set a bit in a byte array.
  36. * @param data array to set bit in
  37. * @param position The position of the bit to set.
  38. * @param bit bit value to set
  39. */
  40. void bit_lib_set_bit(uint8_t* data, size_t position, bool bit);
  41. /** @brief Set the bit at the given position to the given value.
  42. * @param data The data to set the bit in.
  43. * @param position The position of the bit to set.
  44. * @param byte The data to set the bit to.
  45. * @param length The length of the data.
  46. */
  47. void bit_lib_set_bits(uint8_t* data, size_t position, uint8_t byte, uint8_t length);
  48. /** @brief Get the bit of a byte.
  49. * @param data The byte to get the bits from.
  50. * @param position The position of the bit.
  51. * @return The bit.
  52. */
  53. bool bit_lib_get_bit(const uint8_t* data, size_t position);
  54. /**
  55. * @brief Get the bits of a data, as uint8_t.
  56. * @param data The data to get the bits from.
  57. * @param position The position of the first bit.
  58. * @param length The length of the bits.
  59. * @return The bits.
  60. */
  61. uint8_t bit_lib_get_bits(const uint8_t* data, size_t position, uint8_t length);
  62. /**
  63. * @brief Get the bits of a data, as uint16_t.
  64. * @param data The data to get the bits from.
  65. * @param position The position of the first bit.
  66. * @param length The length of the bits.
  67. * @return The bits.
  68. */
  69. uint16_t bit_lib_get_bits_16(const uint8_t* data, size_t position, uint8_t length);
  70. /**
  71. * @brief Get the bits of a data, as uint32_t.
  72. * @param data The data to get the bits from.
  73. * @param position The position of the first bit.
  74. * @param length The length of the bits.
  75. * @return The bits.
  76. */
  77. uint32_t bit_lib_get_bits_32(const uint8_t* data, size_t position, uint8_t length);
  78. /**
  79. * @brief Test parity of given bits
  80. * @param bits Bits to test parity of
  81. * @param parity Parity to test against
  82. * @return true if parity is correct, false otherwise
  83. */
  84. bool bit_lib_test_parity_32(uint32_t bits, BitLibParity parity);
  85. /**
  86. * @brief Test parity of bit array, check parity for every parity_length block from start
  87. *
  88. * @param data Bit array
  89. * @param position Start position
  90. * @param length Bit count
  91. * @param parity Parity to test against
  92. * @param parity_length Parity block length
  93. * @return true
  94. * @return false
  95. */
  96. bool bit_lib_test_parity(
  97. const uint8_t* data,
  98. size_t position,
  99. uint8_t length,
  100. BitLibParity parity,
  101. uint8_t parity_length);
  102. /**
  103. * @brief Remove bit every n in array and shift array left. Useful to remove parity.
  104. *
  105. * @param data Bit array
  106. * @param position Start position
  107. * @param length Bit count
  108. * @param n every n bit will be removed
  109. * @return size_t
  110. */
  111. size_t bit_lib_remove_bit_every_nth(uint8_t* data, size_t position, uint8_t length, uint8_t n);
  112. /**
  113. * @brief Copy bits from source to destination.
  114. *
  115. * @param data destination array
  116. * @param position position in destination array
  117. * @param length length of bits to copy
  118. * @param source source array
  119. * @param source_position position in source array
  120. */
  121. void bit_lib_copy_bits(
  122. uint8_t* data,
  123. size_t position,
  124. size_t length,
  125. const uint8_t* source,
  126. size_t source_position);
  127. /**
  128. * @brief Reverse bits in bit array
  129. *
  130. * @param data Bit array
  131. * @param position start position
  132. * @param length length of bits to reverse
  133. */
  134. void bit_lib_reverse_bits(uint8_t* data, size_t position, uint8_t length);
  135. /**
  136. * @brief Count 1 bits in data
  137. *
  138. * @param data
  139. * @return uint8_t set bit count
  140. */
  141. uint8_t bit_lib_get_bit_count(uint32_t data);
  142. /**
  143. * @brief Print data as bit array
  144. *
  145. * @param data
  146. * @param length
  147. */
  148. void bit_lib_print_bits(const uint8_t* data, size_t length);
  149. typedef struct {
  150. const char mark;
  151. const size_t start;
  152. const size_t length;
  153. } BitLibRegion;
  154. /**
  155. * @brief Print data as bit array and mark regions. Regions needs to be sorted by start position.
  156. *
  157. * @param regions
  158. * @param region_count
  159. * @param data
  160. * @param length
  161. */
  162. void bit_lib_print_regions(
  163. const BitLibRegion* regions,
  164. size_t region_count,
  165. const uint8_t* data,
  166. size_t length);
  167. /**
  168. * @brief Reverse bits in uint16_t, faster than generic bit_lib_reverse_bits.
  169. *
  170. * @param data
  171. * @return uint16_t
  172. */
  173. uint16_t bit_lib_reverse_16_fast(uint16_t data);
  174. /**
  175. * @brief Slow, but generic CRC16 implementation
  176. *
  177. * @param data
  178. * @param data_size
  179. * @param polynom CRC polynom
  180. * @param init init value
  181. * @param ref_in true if the right bit is older
  182. * @param ref_out true to reverse output
  183. * @param xor_out xor output with this value
  184. * @return uint16_t
  185. */
  186. uint16_t bit_lib_crc16(
  187. uint8_t const* data,
  188. size_t data_size,
  189. uint16_t polynom,
  190. uint16_t init,
  191. bool ref_in,
  192. bool ref_out,
  193. uint16_t xor_out);
  194. #ifdef __cplusplus
  195. }
  196. #endif