cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
redis_frame_internal.h
Go to the documentation of this file.
1/* Private RESP encoder shared by the client and focused bounds tests. */
2#ifndef CWIST_REDIS_FRAME_INTERNAL_H
3#define CWIST_REDIS_FRAME_INTERNAL_H
4#include <errno.h>
5#include <stddef.h>
6#include <stdio.h>
7#include <string.h>
8
9static inline int redis_frame_fill(char *frame, size_t cap, size_t argc, const void *const *argv,
10 const size_t *lens) {
11 if (!frame || cap == 0 || argc == 0 || !argv || !lens) return -EINVAL;
12 int n = snprintf(frame, cap, "*%zu\r\n", argc);
13 if (n < 0 || (size_t)n >= cap) return -EINVAL;
14 size_t used = (size_t)n;
15 for (size_t i = 0; i < argc; ++i) {
16 size_t len = lens[i];
17 size_t remaining = cap - used;
18 n = snprintf(frame + used, remaining, "$%zu\r\n", len);
19 if (n < 0 || (size_t)n >= remaining) return -EINVAL;
20 used += (size_t)n;
21 remaining = cap - used;
22 if (remaining < 3 || len > remaining - 3 || (len && !argv[i])) return -EINVAL;
23 if (len) memcpy(frame + used, argv[i], len);
24 used += len;
25 frame[used++] = '\r';
26 frame[used++] = '\n';
27 }
28 if (used != cap - 1) return -EINVAL;
29 frame[used] = '\0';
30 return 0;
31}
32#endif
size_t cap
Definition otel.c:56