commit 009a69939e6ce193b6bf6ab287aa007a4b8ab11e
parent 2ddf3b5d77e2dc2b1586359d720a0c502b7a8c0d
Author: sin <sin@2f30.org>
Date: Thu, 21 Feb 2019 08:54:12 +0000
Add pack/unpack functions
Diffstat:
M | Makefile | | | 8 | ++++---- |
M | dedup.c | | | 1 | + |
A | dedup.h | | | 2 | ++ |
A | pack.c | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | unpack.c | | | 97 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 196 insertions(+), 4 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,9 +2,9 @@ VERSION = 0.3
PREFIX = /usr/local
MANPREFIX = $(PREFIX)/man
BIN = dedup
-SRC = $(BIN).c
-OBJ = $(BIN).o
-DISTFILES = $(SRC) LICENSE Makefile README arg.h $(BIN).1 tree.h
+SRC = $(BIN).c pack.c unpack.c
+OBJ = $(BIN).o pack.o unpack.o
+DISTFILES = $(SRC) LICENSE Makefile README arg.h $(BIN).1 dedup.h tree.h
CFLAGS = -g -Wall
CPPFLAGS = -I/usr/local/include -D_FILE_OFFSET_BITS=64
@@ -13,7 +13,7 @@ LDLIBS = -lcrypto -llz4
all: $(BIN)
-$(OBJ): arg.h tree.h
+$(OBJ): arg.h tree.h dedup.h
clean:
rm -f $(OBJ) $(BIN) $(BIN)-$(VERSION).tar.gz
diff --git a/dedup.c b/dedup.c
@@ -12,6 +12,7 @@
#include <openssl/sha.h>
#include "arg.h"
+#include "dedup.h"
#include "tree.h"
#define SNAPSF ".snapshots"
diff --git a/dedup.h b/dedup.h
@@ -0,0 +1,2 @@
+int pack(unsigned char *dst, char *fmt, ...);
+int unpack(unsigned char *src, char *fmt, ...);
diff --git a/pack.c b/pack.c
@@ -0,0 +1,92 @@
+/*
+ * ISC License
+ *
+ * (c) 2019 Roberto E. Vargas Caballero <k0ga@shike2.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int
+lpack(unsigned char *dst, char *fmt, va_list va)
+{
+ unsigned char *bp, *cp;
+ unsigned s;
+ unsigned long l;
+ unsigned long long q;
+ int n;
+
+ bp = dst;
+ while (*fmt) {
+ switch (*fmt++) {
+ case '\'':
+ n = atoi(fmt);
+ while (isdigit(*fmt))
+ fmt++;
+ cp = va_arg(va, unsigned char *);
+ while (n--)
+ *bp++ = *cp++;
+ break;
+ case 'c':
+ *bp++ = va_arg(va, unsigned);
+ break;
+ case 's':
+ s = va_arg(va, unsigned);
+ *bp++ = s;
+ *bp++ = s >> 8;
+ break;
+ case 'l':
+ l = va_arg(va, unsigned long);
+ *bp++ = l;
+ *bp++ = l >> 8;
+ *bp++ = l >> 16;
+ *bp++ = l >> 24;
+ break;
+ case 'q':
+ q = va_arg(va, unsigned long long);
+ *bp++ = q;
+ *bp++ = q >> 8;
+ *bp++ = q >> 16;
+ *bp++ = q >> 24;
+ *bp++ = q >> 32;
+ *bp++ = q >> 40;
+ *bp++ = q >> 48;
+ *bp++ = q >> 56;
+ break;
+ default:
+ va_end(va);
+ return -1;
+ }
+ }
+
+ return bp - dst;
+}
+
+int
+pack(unsigned char *dst, char *fmt, ...)
+{
+ int r;
+ int (*fn)(unsigned char *dst, char *fmt, va_list va);
+ va_list va;
+
+ va_start(va, fmt);
+ fn = lpack;
+ r = (*fn)(dst, fmt, va);
+ va_end(va);
+
+ return r;
+}
diff --git a/unpack.c b/unpack.c
@@ -0,0 +1,97 @@
+/*
+ * ISC License
+ *
+ * (c) 2019 Roberto E. Vargas Caballero <k0ga@shike2.com>
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static int
+lunpack(unsigned char *src, char *fmt, va_list va)
+{
+ unsigned char *bp, *cp;
+ unsigned short *sp;
+ unsigned s;
+ unsigned long *lp, l;
+ unsigned long long *qp, q;
+ int n;
+
+ bp = src;
+ while (*fmt) {
+ switch (*fmt++) {
+ case '\'':
+ n = atoi(fmt);
+ while (isdigit(*fmt))
+ fmt++;
+ cp = va_arg(va, unsigned char *);
+ while (n--)
+ *cp++ = *bp++;
+ break;
+ case 'c':
+ cp = va_arg(va, unsigned char *);
+ *cp = *bp++;
+ break;
+ case 's':
+ sp = va_arg(va, unsigned short *);
+ s = (unsigned) *bp++;
+ s |= (unsigned) *bp++ << 8;
+ *sp = s;
+ break;
+ case 'l':
+ lp = va_arg(va, unsigned long *);
+ l = (unsigned long) *bp++;
+ l |= (unsigned long) *bp++ << 8;
+ l |= (unsigned long) *bp++ << 16;
+ l |= (unsigned long) *bp++ << 24;
+ *lp = l;
+ break;
+ case 'q':
+ qp = va_arg(va, unsigned long long *);
+ q = (unsigned long long) *bp++;
+ q |= (unsigned long long) *bp++ << 8;
+ q |= (unsigned long long) *bp++ << 16;
+ q |= (unsigned long long) *bp++ << 24;
+ q |= (unsigned long long) *bp++ << 32;
+ q |= (unsigned long long) *bp++ << 40;
+ q |= (unsigned long long) *bp++ << 48;
+ q |= (unsigned long long) *bp++ << 56;
+ *qp = q;
+ break;
+ default:
+ va_end(va);
+ return -1;
+ }
+ }
+
+ return bp - src;
+}
+
+int
+unpack(unsigned char *src, char *fmt, ...)
+{
+ int r;
+ int (*fn)(unsigned char *dst, char *fmt, va_list va);
+ va_list va;
+
+ va_start(va, fmt);
+ fn = lunpack;
+ r = (*fn)(src, fmt, va);
+ va_end(va);
+
+ return r;
+}