From 24f880d7187d5c00a031474174bb3f1c039fa108 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Sun, 19 Apr 2015 12:38:05 +0200 Subject: [PATCH] base64: sync with libubox modifications Signed-off-by: Felix Fietkau --- base64.c | 28 +++++++++++++++++++++++++--- base64.h | 8 ++++---- main.c | 8 ++++---- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/base64.c b/base64.c index 27acf40..763e3ff 100644 --- a/base64.c +++ b/base64.c @@ -1,3 +1,21 @@ +/* + * base64 - libubox base64 functions + * + * Copyright (C) 2015 Felix Fietkau + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + /* $OpenBSD: base64.c,v 1.7 2013/12/31 02:32:56 tedu Exp $ */ /* @@ -116,8 +134,8 @@ static const char Pad64 = '='; characters followed by one "=" padding character. */ -int b64_ntop(const void *_src, size_t srclength, - void *dest, size_t targsize) +int b64_encode(const void *_src, size_t srclength, + void *dest, size_t targsize) { const unsigned char *src = _src; char *target = dest; @@ -178,7 +196,7 @@ int b64_ntop(const void *_src, size_t srclength, it returns the number of data bytes stored at the target, or -1 on error. */ -int b64_pton(const void *_src, void *dest, size_t targsize) +int b64_decode(const void *_src, void *dest, size_t targsize) { const char *src = _src; unsigned char *target = dest; @@ -301,5 +319,9 @@ int b64_pton(const void *_src, void *dest, size_t targsize) return (-1); } + /* Null-terminate if we have room left */ + if (tarindex < targsize) + target[tarindex] = 0; + return (tarindex); } diff --git a/base64.h b/base64.h index 2625721..0f49e9a 100644 --- a/base64.h +++ b/base64.h @@ -1,12 +1,12 @@ #ifndef __BASE64_H #define __BASE64_H -int b64_ntop(const void *src, size_t src_len, - void *dest, size_t dest_len); +int b64_encode(const void *src, size_t src_len, + void *dest, size_t dest_len); -int b64_pton(const void *src, void *dest, size_t dest_len); +int b64_decode(const void *src, void *dest, size_t dest_len); +#define B64_ENCODE_LEN(_len) ((((_len) + 2) / 3) * 4 + 1) #define B64_DECODE_LEN(_len) (((_len) / 4) * 3 + 1) -#define B64_ENCODE_LEN(_len) ((((_len) / 3) + 1) * 4 + 1) #endif diff --git a/main.c b/main.c index a1ed6c3..869ee08 100644 --- a/main.c +++ b/main.c @@ -135,7 +135,7 @@ static bool get_base64_file(const char *file, void *dest, int size, void *buf, int buflen) { get_file(file, buf, buflen - 1); - return b64_pton(buf, dest, size) == size; + return b64_decode(buf, dest, size) == size; } static void write_file(const char *name, const uint8_t *fingerprint, @@ -245,7 +245,7 @@ static int sign(const char *msgfile) munmap(m, mlen); close(mfd); - if (b64_ntop(&sig, sizeof(sig), buf, sizeof(buf)) < 0) + if (b64_encode(&sig, sizeof(sig), buf, sizeof(buf)) < 0) return 1; write_file(sigfile, sig.fingerprint, "signed by key", buf); @@ -309,7 +309,7 @@ static int generate(void) sha512_add(&s, skey.seckey, sizeof(skey.seckey)); memcpy(skey.checksum, sha512_final_get(&s), sizeof(skey.checksum)); - if (b64_ntop(&skey, sizeof(skey), buf, sizeof(buf)) < 0) + if (b64_encode(&skey, sizeof(skey), buf, sizeof(buf)) < 0) return 1; write_file(seckeyfile, skey.fingerprint, "public key", buf); @@ -317,7 +317,7 @@ static int generate(void) memcpy(pkey.fingerprint, skey.fingerprint, sizeof(pkey.fingerprint)); memcpy(pkey.pubkey, skey.seckey + 32, sizeof(pkey.pubkey)); - if (b64_ntop(&pkey, sizeof(pkey), buf, sizeof(buf)) < 0) + if (b64_encode(&pkey, sizeof(pkey), buf, sizeof(buf)) < 0) return 1; write_file(pubkeyfile, pkey.fingerprint, "private key", buf); -- 2.25.1