summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2023-10-13 16:51:56 +0200
committerLeah Neukirchen <leah@vuxu.org>2023-10-13 16:51:56 +0200
commitae9b6838c96a6194b30452fa6f4b9c0043b68ec2 (patch)
tree5a62db04bea4e2005feb537eadbcaefb586e3c99
parent084f5b70a41049da9d3a1d60e3c527ee4e4ba75c (diff)
downloadmico-ae9b6838c96a6194b30452fa6f4b9c0043b68ec2.tar.gz
mico-ae9b6838c96a6194b30452fa6f4b9c0043b68ec2.tar.xz
mico-ae9b6838c96a6194b30452fa6f4b9c0043b68ec2.zip
add mico-dump
-rw-r--r--Makefile2
-rw-r--r--mico-dump.c114
-rw-r--r--mico-store.c7
3 files changed, 122 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 4c07d17..e80b676 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 CFLAGS=-g -O2 -Wall -Wno-switch -Wextra -Wwrite-strings
 LDLIBS=-lzip
 
-all: mico-store
+all: mico-store mico-dump
 
 clean: FRC
 	rm -f mico-store
diff --git a/mico-dump.c b/mico-dump.c
new file mode 100644
index 0000000..8bf12e4
--- /dev/null
+++ b/mico-dump.c
@@ -0,0 +1,114 @@
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <zip.h>
+
+zip_t *zip;
+
+struct bitreader {
+	zip_file_t *input;
+
+        uint64_t bitbuf;        // value of bits in write buffer
+        int bitcount;   // number of bits in write buffer
+
+	int eof;
+};
+
+static uint64_t
+getbits1_msb(struct bitreader *input, int count)
+{
+        assert(count >= 1 && count <= 57);
+
+        // Keep reading extra bytes until we have enough bits buffered
+        // Big endian; our current bits are at the top of the word,
+        // new bits get added at the bottom.
+        while (input->bitcount < count) {
+		unsigned char buf;
+		int ret = zip_fread(input->input, &buf, sizeof buf);
+		if (ret < 0) {
+			printf("err: %s", zip_strerror(zip));
+		}
+		if (ret == 0) {
+			input->eof = 1;
+			return 0;
+		}
+                input->bitbuf |= (uint64_t)buf << (56 - input->bitcount);
+                input->bitcount += 8;
+        }
+
+        // We now have enough bits present; the most significant
+        // "count" bits in "bitbuf" are our result.
+        uint64_t result = input->bitbuf >> (64 - count);
+
+        // Now remove these bits from the buffer
+        input->bitbuf <<= count;
+        input->bitcount -= count;
+
+        return result;
+}
+
+static int32_t
+getsigned(struct bitreader *input, int count)
+{
+        uint64_t bits = getbits1_msb(input, count);
+        uint64_t sign = 1 << (count - 1);
+
+        if (bits & sign)
+                return -(bits ^ sign) - 1;
+        else
+                return bits;
+}
+
+static int32_t
+get1(struct bitreader *input)
+{
+        if (getbits1_msb(input, 1) == 0)
+                return 0;
+        if (getbits1_msb(input, 1) == 0)
+                return getsigned(input, 7);
+        if (getbits1_msb(input, 1) == 0)
+                return getsigned(input, 9);
+        if (getbits1_msb(input, 1) == 0)
+                return getsigned(input, 12);
+        return getsigned(input, 32);
+}
+
+int
+main()
+{
+	zip = zip_open("out.zip", ZIP_RDONLY, 0);
+
+	int entries = zip_get_num_entries(zip, 0);
+
+	for (int i = 0; i < entries; i += 2) {
+		struct bitreader ts = { 0 };
+		struct bitreader vs = { 0 };
+
+		/* XXX verify assumptions on zip file order */
+		ts.input = zip_fopen_index(zip, i, 0);
+		vs.input = zip_fopen_index(zip, i+1, 0);
+
+		char *name = strdup(zip_get_name(zip, i, ZIP_FL_ENC_RAW));
+		char *s = strchr(name, '/');
+		*s = '{';
+		s = strrchr(name, '/');
+		*s++ = '}';
+		*s = 0;
+
+		int64_t t = 0, td = 0;
+		double v = 0.0;
+
+		while (!ts.eof) {
+			int32_t tdd = get1(&ts);
+			int32_t vd = get1(&vs);
+
+			td += tdd;
+			t += td;
+			v += vd;
+
+			printf("%s %f %ld\n", name, v/10.0, t);
+		}
+	}
+}
diff --git a/mico-store.c b/mico-store.c
index 1083928..38e4b7e 100644
--- a/mico-store.c
+++ b/mico-store.c
@@ -1,5 +1,7 @@
+#include <assert.h>
 #include <stdint.h>
 #include <stdio.h>
+#include <string.h>
 
 #include <zip.h>
 
@@ -179,6 +181,11 @@ main()
 			t = metrics[m].value[i].t;
 			v = metrics[m].value[i].v;
 
+			if (t == pt) {
+				fprintf(stderr, "duplicate timestamp: %ld", t);
+				continue;
+			}
+
 			put1(&ts, (t - pt) - (pt - ppt));
 			put1(&vs, (int32_t)((v - pv) * 10));