#include "internal.h" #include #include #include void cepis_buf_init(cepis_buf_t *buf) { buf->data = NULL; buf->len = 0; buf->cap = 0; } bool cepis_buf_append(cepis_buf_t *buf, const char *data, size_t len) { if (buf->len + len + 1 > buf->cap) { size_t cap = buf->cap ? buf->cap : 256; char *grown; while (buf->len + len + 1 > cap) { if (cap > (size_t)-1 / 2) return false; cap *= 2; } grown = realloc(buf->data, cap); if (grown == NULL) return false; buf->data = grown; buf->cap = cap; } if (len > 0) memcpy(buf->data + buf->len, data, len); buf->len += len; buf->data[buf->len] = '\0'; return true; } bool cepis_buf_append_str(cepis_buf_t *buf, const char *text) { return cepis_buf_append(buf, text, strlen(text)); } void cepis_buf_free(cepis_buf_t *buf) { free(buf->data); cepis_buf_init(buf); } char *cepis_strdup(const char *text) { size_t len; char *copy; if (text == NULL) text = ""; len = strlen(text); copy = malloc(len + 1); if (copy == NULL) return NULL; memcpy(copy, text, len + 1); return copy; } cepis_status_t cepis_set_error(cepis_error_t *err, cepis_status_t status, long http_status, double retry_after, const char *fmt, ...) { va_list args; if (err == NULL) return status; err->status = status; err->http_status = http_status; err->retry_after = retry_after; /* * Cleared here, not by the caller: an error is declared uninitialised * on the stack and reused across calls, so anything left set would be * read as a municipality that this failure never named. */ err->has_faixa = false; va_start(args, fmt); vsnprintf(err->message, sizeof(err->message), fmt, args); va_end(args); return status; }