+static inline uint8_t
+read_unaligned_byte(uint8_t *ptr, unsigned int len, unsigned int offset)
+{
+ uint8_t ret = 0;
+
+ ret = ptr[offset / CHAR_BIT];
+ if (offset % CHAR_BIT) {
+ ret <<= (offset % CHAR_BIT);
+ ret |= ptr[(offset / CHAR_BIT) + 1] >>
+ (CHAR_BIT - (offset % CHAR_BIT));
+ }
+
+ return ret >> (CHAR_BIT - len);
+}
+
+static inline uint32_t
+read_unaligned_bits(uint8_t *ptr, int len, int offset)
+{
+ uint32_t ret = 0;
+
+ len = RTE_MAX(len, 0);
+ len = RTE_MIN(len, (int)(sizeof(uint32_t) * CHAR_BIT));
+
+ while (len > 0) {
+ ret <<= CHAR_BIT;
+
+ ret |= read_unaligned_byte(ptr, RTE_MIN(len, CHAR_BIT),
+ offset);
+ offset += CHAR_BIT;
+ len -= CHAR_BIT;
+ }
+
+ return ret;
+}
+
+/* returns mask for len bits with given offset inside byte */
+static inline uint8_t
+get_bits_mask(unsigned int len, unsigned int offset)
+{
+ unsigned int last_bit;
+
+ offset %= CHAR_BIT;
+ /* last bit within byte */
+ last_bit = RTE_MIN((unsigned int)CHAR_BIT, offset + len);
+
+ return ((1 << (CHAR_BIT - offset)) - 1) ^
+ ((1 << (CHAR_BIT - last_bit)) - 1);
+}
+