Add T4 bit map compression routines

Add bit map encoder and decoder functions: decoder is fully functional
while encoder is good enough for testing - no backtracking to find
the best possible compression is implemented. If somebody is willing to
implement MS side of EDGE than this has to be expanded.
Add corresponding tests.
N. B: the encoding is implemented according to ETSI TS 44.060 which is
slightly different from T4 used for fax according to CCITT G31D (RFC 804).

Ticket: OW#2407
Sponsored-by: On-Waves ehf

Signed-off-by: Max <msuraev@sysmocom.de>
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a4a6b2e..571e87b 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -12,7 +12,7 @@
 		 loggingrb/loggingrb_test strrb/strrb_test              \
 		 vty/vty_test comp128/comp128_test utils/utils_test	\
 		 smscb/gsm0341_test stats/stats_test			\
-		 bitvec/bitvec_test msgb/msgb_test
+		 bitvec/bitvec_test msgb/msgb_test bits/bitcomp_test
 
 if ENABLE_MSGFILE
 check_PROGRAMS += msgfile/msgfile_test
@@ -42,6 +42,9 @@
 bitvec_bitvec_test_SOURCES = bitvec/bitvec_test.c
 bitvec_bitvec_test_LDADD = $(top_builddir)/src/libosmocore.la
 
+bits_bitcomp_test_SOURCES = bits/bitcomp_test.c
+bits_bitcomp_test_LDADD = $(top_builddir)/src/libosmocore.la
+
 conv_conv_test_SOURCES = conv/conv_test.c
 conv_conv_test_LDADD = $(top_builddir)/src/libosmocore.la
 
@@ -136,7 +139,7 @@
              loggingrb/logging_test.err	strrb/strrb_test.ok		\
 	     vty/vty_test.ok comp128/comp128_test.ok			\
 	     utils/utils_test.ok stats/stats_test.ok			\
-	     bitvec/bitvec_test.ok msgb/msgb_test.ok
+	     bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok
 
 DISTCLEANFILES = atconfig
 
diff --git a/tests/bits/bitcomp_test.c b/tests/bits/bitcomp_test.c
new file mode 100644
index 0000000..f6895cf
--- /dev/null
+++ b/tests/bits/bitcomp_test.c
@@ -0,0 +1,66 @@
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <time.h>
+#include <stdbool.h>
+#include <errno.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/bits.h>
+#include <osmocom/core/bitcomp.h>
+
+static char lol[1024]; // for pretty-printing
+
+int main(int argc, char **argv)
+{
+	srand(time(NULL));
+
+	struct bitvec bv, out;
+	uint8_t i = 20, test[i], data[i];
+
+	bv.data_len = i;
+        bv.data = test;
+	out.data_len = i;
+	out.data = data;
+	bitvec_zero(&bv);
+	bitvec_zero(&out);
+
+	printf("\nrunning static tests...\n");
+
+	printf("\nTEST1:\n 00110111 01000111 10000001 1111\n");
+	bitvec_zero(&bv);
+	bitvec_set_uint(&bv, 0x374781F, 28); bitvec_to_string_r(&bv, lol); printf("%s", lol);
+
+	printf("\nEncoded:\n%d", osmo_t4_encode(&bv)); bitvec_to_string_r(&bv, lol); printf("%s", lol);
+	printf(" [%d]\nExpected:\n0 11011110 10001000 01110101 01100101 100 [35]\n", bv.cur_bit);
+
+	bitvec_zero(&bv);
+	bitvec_set_uint(&bv, 0xDE887565, 32);
+	bitvec_set_uint(&bv, 4, 3);
+	bitvec_to_string_r(&bv, lol);
+	printf(" %s [%d]\n", lol, bv.cur_bit);
+	int d = osmo_t4_decode(&bv, 0, &out);
+	printf("\nDecoded:\n%d", d);
+	bitvec_to_string_r(&out, lol);
+	printf("%s [%d]\n", lol, out.cur_bit);
+	printf("Expected:\n  00110111 01000111 10000001 1111 \n");
+
+	printf("\nTEST2:\n 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000000 00\n");
+	bitvec_zero(&bv);
+	bitvec_set_uint(&bv, 0xFFFFFFFF, 32);
+	bitvec_set_uint(&bv, 0xFFFFFFFF, 32);
+	bitvec_set_uint(&bv, 0xFFFFFC00, 26); bitvec_to_string_r(&bv, lol); printf("%s", lol);
+	printf("\nEncoded:\n%d", osmo_t4_encode(&bv)); bitvec_to_string_r(&bv, lol); printf("%s", lol);
+	printf(" [%d]\nExpected:\n1 11011101 01000001 00 [18]\n", bv.cur_bit);
+
+	bitvec_zero(&out);
+	d = osmo_t4_decode(&bv, 1, &out);
+	printf("\nDecoded:\n%d", d);
+	bitvec_to_string_r(&out, lol);
+	printf("%s [%d]\n", lol, out.cur_bit);
+	printf("Expected:\n  11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000000 00\n");
+
+	return 0;
+}
diff --git a/tests/bits/bitcomp_test.ok b/tests/bits/bitcomp_test.ok
new file mode 100644
index 0000000..238f3c4
--- /dev/null
+++ b/tests/bits/bitcomp_test.ok
@@ -0,0 +1,29 @@
+
+running static tests...
+
+TEST1:
+ 00110111 01000111 10000001 1111
+ 00110111 01000111 10000001 1111
+Encoded:
+-1 00110111 01000111 10000001 1111 [28]
+Expected:
+0 11011110 10001000 01110101 01100101 100 [35]
+  11011110 10001000 01110101 01100101 100 [35]
+
+Decoded:
+0 00110111 01000111 10000001 1111 [28]
+Expected:
+  00110111 01000111 10000001 1111 
+
+TEST2:
+ 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000000 00
+ 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000000 00
+Encoded:
+1 11011101 01000001 00 [18]
+Expected:
+1 11011101 01000001 00 [18]
+
+Decoded:
+0 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000000 00 [90]
+Expected:
+  11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000000 00
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 9cda1de..3d4d526 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -27,6 +27,12 @@
 AT_CHECK([$abs_top_builddir/tests/bitvec/bitvec_test], [0], [expout], [ignore])
 AT_CLEANUP
 
+AT_SETUP([bitcomp])
+AT_KEYWORDS([bitcomp])
+cat $abs_srcdir/bits/bitcomp_test.ok > expout
+AT_CHECK([$abs_top_builddir/tests/bits/bitcomp_test], [0], [expout])
+AT_CLEANUP
+
 AT_SETUP([conv])
 AT_KEYWORDS([conv])
 cat $abs_srcdir/conv/conv_test.ok > expout