aboutsummaryrefslogtreecommitdiff
path: root/src/util_rt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util_rt.c')
-rw-r--r--src/util_rt.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/util_rt.c b/src/util_rt.c
index e0ebafc..21f1ff3 100644
--- a/src/util_rt.c
+++ b/src/util_rt.c
@@ -1,5 +1,6 @@
#include "util_rt.h"
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
@@ -83,6 +84,17 @@ void prne_free (void *ptr) {
free(ptr);
}
+size_t prne_getpagesize (void) {
+ long ret;
+
+ ret = sysconf(_SC_PAGESIZE);
+ if (ret > 0) {
+ return ret;
+ }
+
+ return 4096;
+}
+
void prne_rnd_anum_str (mbedtls_ctr_drbg_context *rnd, char *str, const size_t len) {
static const char SET[] = "qwertyuiopasdfghjklzxcvbnm0123456789";
size_t i = 0;
@@ -227,6 +239,57 @@ bool prne_dec_base64_mem (const char *str, const size_t str_len, uint8_t **data,
return true;
}
+void prne_init_stdin_base64_rf_ctx (prne_stdin_base64_rf_ctx_t *ctx) {
+ ctx->line_len = 0;
+ ctx->out_len = 0;
+}
+
+void prne_free_stdin_base64_rf_ctx (prne_stdin_base64_rf_ctx_t *ctx) {
+ ctx->line_len = 0;
+ ctx->out_len = 0;
+}
+
+prne_pack_ret_t prne_stdin_base64_rf (void *in_ctx, const size_t req, uint8_t *out, size_t *out_len) {
+ prne_stdin_base64_rf_ctx_t *ctx = (prne_stdin_base64_rf_ctx_t*)in_ctx;
+ size_t rem = req, have;
+ prne_pack_ret_t ret;
+
+ ret.rc = PRNE_PACK_RC_OK;
+ ret.err = 0;
+ *out_len = 0;
+
+ while (true) {
+ have = prne_op_min(rem, ctx->out_len);
+ memcpy(out, ctx->out_buf, have);
+ memmove(ctx->out_buf, ctx->out_buf + have, ctx->out_len - have);
+ rem -= have;
+ ctx->out_len -= have;
+ out += have;
+ *out_len += have;
+
+ if (rem == 0) {
+ break;
+ }
+
+ if (fgets(ctx->line_buf, sizeof(ctx->line_buf), stdin) == NULL) {
+ if (feof(stdin)) {
+ break;
+ }
+ ret.rc = PRNE_PACK_RC_ERRNO;
+ ret.err = errno;
+ break;
+ }
+ ctx->line_len = prne_str_shift_spaces(ctx->line_buf, strlen(ctx->line_buf));
+
+ if ((ret.err = mbedtls_base64_decode(ctx->out_buf, sizeof(ctx->out_buf), &ctx->out_len, (unsigned char*)ctx->line_buf, ctx->line_len)) != 0) {
+ ret.rc = PRNE_PACK_RC_MBEDTLS_ERR;
+ break;
+ }
+ }
+
+ return ret;
+}
+
bool prne_set_pipe_size (const int fd, const int size) {
return
#if defined(F_SETPIPE_SZ)