-/* $OpenBSD: encoding.c,v 1.5 2021/10/26 16:12:54 claudio Exp $ */
+/* $OpenBSD: encoding.c,v 1.6 2021/10/26 16:59:19 claudio Exp $ */
/*
* Copyright (c) 2020 Claudio Jeker <claudio@openbsd.org>
*
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <sys/stat.h>
+
#include <err.h>
+#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <openssl/evp.h>
#include "extern.h"
+/*
+ * Load file from disk and return the buffer and size.
+ */
+unsigned char *
+load_file(const char *name, size_t *len)
+{
+ unsigned char *buf = NULL;
+ struct stat st;
+ ssize_t n;
+ size_t size;
+ int fd;
+
+ *len = 0;
+
+ if ((fd = open(name, O_RDONLY)) == -1)
+ return NULL;
+ if (fstat(fd, &st) != 0)
+ goto err;
+ if (st.st_size < 0)
+ goto err;
+ size = (size_t)st.st_size;
+ if ((buf = malloc(size)) == NULL)
+ goto err;
+ n = read(fd, buf, size);
+ if (n < 0 || (size_t)n != size)
+ goto err;
+ close(fd);
+ *len = size;
+ return buf;
+
+err:
+ close(fd);
+ free(buf);
+ return NULL;
+}
+
/*
* Decode base64 encoded string into binary buffer returned in out.
* The out buffer size is stored in outlen.
-/* $OpenBSD: extern.h,v 1.80 2021/10/26 16:12:54 claudio Exp $ */
+/* $OpenBSD: extern.h,v 1.81 2021/10/26 16:59:19 claudio Exp $ */
/*
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
*
/* Encoding functions for hex and base64. */
+unsigned char *load_file(const char *, size_t *);
int base64_decode(const unsigned char *, size_t,
unsigned char **, size_t *);
int base64_encode(const unsigned char *, size_t, char **);
void logx(const char *fmt, ...)
__attribute__((format(printf, 1, 2)));
-unsigned char *load_file(const char *, size_t *);
int mkpath(const char *);
-/* $OpenBSD: main.c,v 1.155 2021/10/26 16:12:54 claudio Exp $ */
+/* $OpenBSD: main.c,v 1.156 2021/10/26 16:59:19 claudio Exp $ */
/*
* Copyright (c) 2021 Claudio Jeker <claudio@openbsd.org>
* Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/resource.h>
-#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/tree.h>
#include <sys/wait.h>
}
}
-unsigned char *
-load_file(const char *name, size_t *len)
-{
- unsigned char *buf = NULL;
- struct stat st;
- ssize_t n;
- size_t size;
- int fd;
-
- *len = 0;
-
- if ((fd = open(name, O_RDONLY)) == -1)
- return NULL;
- if (fstat(fd, &st) != 0)
- goto err;
- if (st.st_size < 0)
- goto err;
- size = (size_t)st.st_size;
- if ((buf = malloc(size)) == NULL)
- goto err;
- n = read(fd, buf, size);
- if (n < 0 || (size_t)n != size)
- goto err;
- close(fd);
- *len = size;
- return buf;
-
-err:
- close(fd);
- free(buf);
- return NULL;
-}
-
void
entity_free(struct entity *ent)
{