utils: Add various bit manipulation related utilites

Signed-off-by: Sylvain Munaut <tnt@246tNt.com>
diff --git a/configure.ac b/configure.ac
index a53c71d..fbddb34 100644
--- a/configure.ac
+++ b/configure.ac
@@ -13,6 +13,7 @@
 	Makefile
 	src/Makefile
 	include/Makefile
+	include/gapk/Makefile
 ])
 
 # Checks for programs.
diff --git a/include/Makefile.am b/include/Makefile.am
index e69de29..b16ce5c 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = gapk
diff --git a/include/gapk/Makefile.am b/include/gapk/Makefile.am
new file mode 100644
index 0000000..ef8800a
--- /dev/null
+++ b/include/gapk/Makefile.am
@@ -0,0 +1 @@
+noinst_HEADERS =	utils.h
diff --git a/include/gapk/utils.h b/include/gapk/utils.h
new file mode 100644
index 0000000..3b02049
--- /dev/null
+++ b/include/gapk/utils.h
@@ -0,0 +1,104 @@
+/* Various helpers */
+
+/*
+ * This file is part of gapk (GSM Audio Pocket Knife).
+ *
+ * gapk is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * gapk is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with gapk.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GAPK_UTILS_H__
+#define __GAPK_UTILS_H__
+
+#include <stdint.h>
+
+static inline int
+msb_get_bit(const uint8_t *buf, int bn)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = 7 - (bn & 7);
+
+	return (buf[pos_byte] >> pos_bit) & 1;
+}
+
+static inline void
+msb_put_bit(uint8_t *buf, int bn, int bit)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = 7 - (bn & 7);
+
+	if (bit)
+		buf[pos_byte] |=  (1 << pos_bit);
+	else
+		buf[pos_byte] &= ~(1 << pos_bit);
+}
+
+static inline void
+msb_set_bit(uint8_t *buf, int bn)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = 7 - (bn & 7);
+
+	buf[pos_byte] |=  (1 << pos_bit);
+}
+
+static inline void
+msb_clr_bit(uint8_t *buf, int bn)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = 7 - (bn & 7);
+
+	buf[pos_byte] &= ~(1 << pos_bit);
+}
+
+
+static inline int
+lsb_get_bit(const uint8_t *buf, int bn)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = bn & 7;
+
+	return (buf[pos_byte] >> pos_bit) & 1;
+}
+
+static inline void
+lsb_put_bit(uint8_t *buf, int bn, int bit)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = bn & 7;
+
+	if (bit)
+		buf[pos_byte] |=  (1 << pos_bit);
+	else
+		buf[pos_byte] &= ~(1 << pos_bit);
+}
+
+static inline void
+lsb_set_bit(uint8_t *buf, int bn)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = bn & 7;
+
+	buf[pos_byte] |=  (1 << pos_bit);
+}
+
+static inline void
+lsb_clr_bit(uint8_t *buf, int bn)
+{
+	int pos_byte = bn >> 3;
+	int pos_bit  = bn & 7;
+
+	buf[pos_byte] &= ~(1 << pos_bit);
+}
+
+#endif /* __GAPK_UTILS_H__ */