Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / buffer-shldata.h / buffer_shldata_putfinal.c

/* 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 <limb/buffer-shldata.h>
#include <limb/shldata.h>
#include <limb/siovec.h>

ssize_t
buffer_shldata_putfinal(buffer *b, buffer_shldata_ctx *ctx)
{
    char buf[28];
    struct iovec v[3];
    int n = 0;

    /* did we store encrypted data? i.e. length wasn't given to init */
    if (ctx->sa.len) {
        int r;
        r = shldata_predata(buf, sizeof(buf), &ctx->sd);
        if (r < 0) return -1;

        v[n].iov_base = buf;
        v[n].iov_len = r;
        ++n;

        v[n].iov_base = ctx->sa.s;
        v[n].iov_len = ctx->sa.len;
        ++n;
    }

    ssize_t l;
    l = shldata_finalw(buf + 10, sizeof(buf) - 10, &ctx->sd);
    if (l < 0) return -1;

    v[n].iov_base = buf + 10;
    v[n].iov_len = l;
    ++n;

    ctx->sa.len = 0;
    return buffer_putv(b, v, n);
}