commit dcb5fb757be749d2a067b98d7135184f8de05a7f
parent c921c5fe9973eb3430fc5935e005b612588cd65a
Author: Ambroise Vincent <ambroise.vincent@arm.com>
Date: Wed, 8 May 2019 16:04:42 +0100
[src] Move serialize functions from lib9p to libk
Change-Id: I27f3afbb7ca3804a84846c321db23e12540b487f
Signed-off-by: Ambroise Vincent <ambroise.vincent@arm.com>
Diffstat:
15 files changed, 93 insertions(+), 97 deletions(-)
diff --git a/src/lib9p/convfr9p.h b/include/deserialize.h
diff --git a/include/serialize.h b/include/serialize.h
@@ -0,0 +1,8 @@
+#define CHAR(x,p) (p = tobytes(p, x, 1))
+#define SHORT(x,p) (p = tobytes(p, x, 2))
+#define LONG(x,p) (p = tobytes(p, x, 4))
+#define LLONG(x,p) (p = tobytes(p, x, 8))
+#define STRING(s,p) (p = tostring(p, s))
+
+unsigned char *tobytes(unsigned char *p, unsigned long long v, int nbytes);
+unsigned char *tostring(unsigned char *p, char *s);
diff --git a/src/lib9p/Makefile b/src/lib9p/Makefile
@@ -2,11 +2,7 @@
PROJECTDIR=../..
include $(PROJECTDIR)/scripts/rules.mk
-OBJS = tobytes.o \
- tostring.o \
- dirtop9.o \
- frombytes.o \
- fromstring.o \
+OBJS = dirtop9.o \
p9todir.o \
opendir.o \
readdir.o \
diff --git a/src/lib9p/convto9p.h b/src/lib9p/convto9p.h
@@ -1,8 +0,0 @@
-#define CHAR(x,p) (p = tobytes(p, x, 1))
-#define SHORT(x,p) (p = tobytes(p, x, 2))
-#define LONG(x,p) (p = tobytes(p, x, 4))
-#define LLONG(x,p) (p = tobytes(p, x, 8))
-#define STRING(s,p) (p = tostring(p, s))
-
-unsigned char *tobytes(unsigned char *p, unsigned long long v, int nbytes);
-unsigned char *tostring(unsigned char *p, char *s);
diff --git a/src/lib9p/dirtop9.c b/src/lib9p/dirtop9.c
@@ -1,9 +1,8 @@
#include <assert.h>
+#include <serialize.h>
#include <rcode/io.h>
-#include "convto9p.h"
-
int
dirtop9(Dir *dp, unsigned char *buf, int n)
{
diff --git a/src/lib9p/frombytes.c b/src/lib9p/frombytes.c
@@ -1,24 +0,0 @@
-#include "convfr9p.h"
-
-long long
-frombytes(unsigned char *bp, int *n, int max, int nbytes)
-{
- int i;
- unsigned long long v;
-
- if (*n == -1)
- return 0;
-
- if (*n + nbytes > max) {
- *n = -1;
- return -1;
- }
-
- bp += *n;
- *n += nbytes;
- v = 0;
- for (i = 0; i < nbytes; i++)
- v |= bp[ i] << (8 * i);
-
- return v;
-}
diff --git a/src/lib9p/fromstring.c b/src/lib9p/fromstring.c
@@ -1,34 +0,0 @@
-#include <string.h>
-
-#include "convfr9p.h"
-
-char *
-fromstring(unsigned char *p, int *n, int max, char *s, int size)
-{
- int len;
-
- if (*n == -1)
- goto error;
-
- len = frombytes(p, n, max, 2);
- if (len == -1)
- goto error;
-
- if (*n + len > max)
- goto error;
-
- if (len >= size)
- goto error;
-
- if (s) {
- memcpy(s, p + *n, len);
- s[len] = '\0';
- }
- *n += len;
-
- return s;
-
-error:
- *n = -1;
- return NULL;
-}
diff --git a/src/lib9p/p9todir.c b/src/lib9p/p9todir.c
@@ -1,11 +1,10 @@
#include <assert.h>
+#include <deserialize.h>
#include <errno.h>
#include <stddef.h>
#include <rcode/io.h>
-#include "convfr9p.h"
-
#define USED(x) ((void) x)
#define USERLEN 20
diff --git a/src/lib9p/tobytes.c b/src/lib9p/tobytes.c
@@ -1,9 +0,0 @@
-#include "convto9p.h"
-
-unsigned char *
-tobytes(unsigned char *p, unsigned long long v, int n)
-{
- for ( ; n--; p++)
- *p = v, v >>= 8;
- return p;
-}
diff --git a/src/lib9p/tostring.c b/src/lib9p/tostring.c
@@ -1,13 +0,0 @@
-#include <string.h>
-
-#include "convto9p.h"
-
-unsigned char *
-tostring(unsigned char *p, char *s)
-{
- int len = strlen(s);
-
- p = tobytes(p, len, 2);
- memcpy(p, s, len);
- return p + len;
-}
diff --git a/src/libk/Makefile b/src/libk/Makefile
@@ -14,6 +14,10 @@ OBJS = doprnt.o \
kwrite.o \
tokenize.o \
kstd.o \
+ frombytes.o \
+ fromstring.o \
+ tobytes.o \
+ tostring.o \
__assert.o \
TARGET = $(LIBDIR)/libk.a
diff --git a/src/libk/frombytes.c b/src/libk/frombytes.c
@@ -0,0 +1,24 @@
+#include <deserialize.h>
+
+long long
+frombytes(unsigned char *bp, int *n, int max, int nbytes)
+{
+ unsigned i;
+ unsigned long long v;
+
+ if (*n == -1)
+ return 0;
+
+ if (*n + nbytes > max) {
+ *n = -1;
+ return -1;
+ }
+
+ bp += *n;
+ *n += nbytes;
+ v = 0;
+ for (i = 0; i < nbytes; i++)
+ v |= (unsigned long long) bp[i] << (8 * i);
+
+ return v;
+}
diff --git a/src/libk/fromstring.c b/src/libk/fromstring.c
@@ -0,0 +1,33 @@
+#include <string.h>
+#include <deserialize.h>
+
+char *
+fromstring(unsigned char *p, int *n, int max, char *s, int size)
+{
+ int len;
+
+ if (*n == -1)
+ goto error;
+
+ len = frombytes(p, n, max, 2);
+ if (len == -1)
+ goto error;
+
+ if (*n + len > max)
+ goto error;
+
+ if (len >= size)
+ goto error;
+
+ if (s) {
+ memcpy(s, p + *n, len);
+ s[len] = '\0';
+ }
+ *n += len;
+
+ return s;
+
+error:
+ *n = -1;
+ return NULL;
+}
diff --git a/src/libk/tobytes.c b/src/libk/tobytes.c
@@ -0,0 +1,9 @@
+#include <serialize.h>
+
+unsigned char *
+tobytes(unsigned char *p, unsigned long long v, int n)
+{
+ for ( ; n--; p++)
+ *p = v, v >>= 8;
+ return p;
+}
diff --git a/src/libk/tostring.c b/src/libk/tostring.c
@@ -0,0 +1,12 @@
+#include <string.h>
+#include <serialize.h>
+
+unsigned char *
+tostring(unsigned char *p, char *s)
+{
+ int len = strlen(s);
+
+ p = tobytes(p, len, 2);
+ memcpy(p, s, len);
+ return p + len;
+}