author | Olivier Brunel
<jjk@jjacky.com> 2023-04-30 17:40:41 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2023-05-20 18:06:39 UTC |
parent | d6d7e4538383297b124714fc1d519337a5a9417f |
src/liblimb/fdpatrim.h/fdpatrim_add.c | +22 | -0 |
src/liblimb/fdpatrim.h/fdpatrim_addv.c | +25 | -0 |
src/liblimb/fdpatrim.h/fdpatrim_get.c | +25 | -0 |
src/liblimb/fdpatrim.h/fdpatrim_initr.c | +30 | -0 |
src/liblimb/fdpatrim.h/fdpatrim_initw.c | +28 | -0 |
src/liblimb/include/limb/fdpatrim.h | +17 | -0 |
diff --git a/src/liblimb/fdpatrim.h/fdpatrim_add.c b/src/liblimb/fdpatrim.h/fdpatrim_add.c new file mode 100644 index 0000000..6bde27f --- /dev/null +++ b/src/liblimb/fdpatrim.h/fdpatrim_add.c @@ -0,0 +1,22 @@ +/* This file is part of limb https://lila.oss/limb + * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <skalibs/allreadwrite.h> +#include <limb/fdpatrim.h> +#include <limb/patrim.h> + +int +fdpatrim_add(int fd, u64 id, u64 u, const char *data) +{ + char buf[18]; + int n; + + n = patrim_put(buf, sizeof(buf), 0, id, u); + if (fd_write(fd, buf, n) != n) + return 0; + + if (patrim_isblob(id) && fd_write(fd, data, (size_t) u) != (ssize_t) u) + return 0; + + return 1; +} diff --git a/src/liblimb/fdpatrim.h/fdpatrim_addv.c b/src/liblimb/fdpatrim.h/fdpatrim_addv.c new file mode 100644 index 0000000..fab71f1 --- /dev/null +++ b/src/liblimb/fdpatrim.h/fdpatrim_addv.c @@ -0,0 +1,25 @@ +/* This file is part of limb https://lila.oss/limb + * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <errno.h> +#include <skalibs/allreadwrite.h> +#include <limb/fdpatrim.h> +#include <limb/patrim.h> +#include <limb/siovec.h> + +int +fdpatrim_addv(int fd, u64 id, const struct iovec v[], unsigned int n) +{ + char buf[18]; + u64 l; + int r; + + if (!patrim_isblob(id)) return (errno = EINVAL, 0); + + l = siov_len(v, n); + r = patrim_put(buf, sizeof(buf), 0, id, l); + if (fd_write(fd, buf, r) != r || fd_writev(fd, v, n) != (ssize_t) l) + return 0; + + return 1; +} diff --git a/src/liblimb/fdpatrim.h/fdpatrim_get.c b/src/liblimb/fdpatrim.h/fdpatrim_get.c new file mode 100644 index 0000000..7622dc4 --- /dev/null +++ b/src/liblimb/fdpatrim.h/fdpatrim_get.c @@ -0,0 +1,25 @@ +/* This file is part of limb https://lila.oss/limb + * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <skalibs/allreadwrite.h> +#include <limb/fdpatrim.h> +#include <limb/u64.h> + +int +fdpatrim_get(int fd, u64 *id, u64 *u) +{ + char buf[9]; + int r = 0; + + for (;;) { + if (fd_read(fd, buf + r++, 1) != 1) + return 0; + if (u64_unpack_trim(id, buf, r) == r) { + if (id == u) break; + id = u; + r = 0; + } + } + + return 1; +} diff --git a/src/liblimb/fdpatrim.h/fdpatrim_initr.c b/src/liblimb/fdpatrim.h/fdpatrim_initr.c new file mode 100644 index 0000000..aaac438 --- /dev/null +++ b/src/liblimb/fdpatrim.h/fdpatrim_initr.c @@ -0,0 +1,30 @@ +/* This file is part of limb https://lila.oss/limb + * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <errno.h> +#include <skalibs/allreadwrite.h> +#include <limb/fdpatrim.h> +#include <limb/u32.h> +#include <limb/u64.h> + +int +fdpatrim_initr(int fd, u32 *magic, u64 *ver) +{ + if (fd_read(fd, (char *) magic, sizeof(u32)) != sizeof(u32)) + return 0; + u32p_be(magic); + + if (*magic & 0xf0f0f000 != 0xa0e0f000) + return (errno = EINVAL, 0); + + char buf[9]; + int r = 0; + for (;;) { + if (fd_read(fd, buf + r++, 1) != 1) + return 0; + if (u64_unpack_trim(ver, buf, r) == r) + break; + } + + return 1; +} diff --git a/src/liblimb/fdpatrim.h/fdpatrim_initw.c b/src/liblimb/fdpatrim.h/fdpatrim_initw.c new file mode 100644 index 0000000..9155498 --- /dev/null +++ b/src/liblimb/fdpatrim.h/fdpatrim_initw.c @@ -0,0 +1,28 @@ +/* This file is part of limb https://lila.oss/limb + * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include <errno.h> +#include <skalibs/allreadwrite.h> +#include <limb/fdpatrim.h> +#include <limb/siovec.h> +#include <limb/u32.h> +#include <limb/u64.h> + +int +fdpatrim_initw(int fd, u32 magic, u64 ver) +{ + char buf[9]; + struct iovec v[2]; + + if (magic & 0xf0f0f000 != 0xa0e0f000) + return (errno = EINVAL, -1); + + u32p_be(&magic); + v[0].iov_base = &magic; + v[0].iov_len = sizeof(u32); + + v[1].iov_base = buf; + v[1].iov_len = u64_pack_trim(buf, sizeof(buf), ver); + + return fd_writev(fd, v, 2) == (ssize_t) siov_len(v, 2); +} diff --git a/src/liblimb/include/limb/fdpatrim.h b/src/liblimb/include/limb/fdpatrim.h new file mode 100644 index 0000000..89fba54 --- /dev/null +++ b/src/liblimb/include/limb/fdpatrim.h @@ -0,0 +1,17 @@ +/* This file is part of limb https://lila.oss/limb + * Copyright (C) 2023 Olivier Brunel jjk@jjacky.com */ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef LIMB_FDPATRIM_H +#define LIMB_FDPATRIM_H + +#include <limb/int.h> +#include <limb/siovec.h> + +extern int fdpatrim_initw(int fd, u32 magic, u64 ver); +extern int fdpatrim_add(int fd, u64 id, u64 u, const char *data); +extern int fdpatrim_addv(int fd, u64 id, const struct iovec v[], unsigned int n); + +extern int fdpatrim_initr(int fd, u32 *magic, u64 *ver); +extern int fdpatrim_get(int fd, u64 *id, u64 *u); + +#endif /* LIMB_FDPATRIM_H */