2017-05-25 18:16:14 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2017 Saleem Rashid
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, E1PRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
|
|
|
|
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
|
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
|
* OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "base32.h"
|
|
|
|
|
2017-05-26 18:58:40 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-05-25 19:50:13 +00:00
|
|
|
const char *BASE32_ALPHABET_RFC4648 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789";
|
|
|
|
|
2017-05-25 18:16:14 +00:00
|
|
|
static inline void base32_5to8(const uint8_t *in, uint8_t length, uint8_t *out);
|
2019-03-29 14:44:55 +00:00
|
|
|
static inline bool base32_8to5(const uint8_t *in, uint8_t length, uint8_t *out,
|
|
|
|
const char *alphabet);
|
|
|
|
static inline void base32_8to5_raw(const uint8_t *in, uint8_t length,
|
|
|
|
uint8_t *out);
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
static inline int base32_encode_character(uint8_t decoded,
|
|
|
|
const char *alphabet);
|
2017-05-26 18:58:40 +00:00
|
|
|
static inline int base32_decode_character(char encoded, const char *alphabet);
|
2017-05-25 19:50:13 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
char *base32_encode(const uint8_t *in, size_t inlen, char *out, size_t outlen,
|
|
|
|
const char *alphabet) {
|
|
|
|
size_t length = base32_encoded_length(inlen);
|
|
|
|
if (outlen <= length) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-25 19:50:13 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
base32_encode_unsafe(in, inlen, (uint8_t *)out);
|
2017-05-25 19:50:13 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
int ret = base32_encode_character(out[i], alphabet);
|
2017-05-29 08:49:38 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
out[i] = ret;
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 19:50:13 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
out[length] = '\0';
|
|
|
|
return &out[length];
|
2017-05-25 19:50:13 +00:00
|
|
|
}
|
2017-05-25 18:16:14 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
uint8_t *base32_decode(const char *in, size_t inlen, uint8_t *out,
|
|
|
|
size_t outlen, const char *alphabet) {
|
|
|
|
size_t length = base32_decoded_length(inlen);
|
|
|
|
if (outlen < length) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (!base32_decode_unsafe((uint8_t *)in, inlen, (uint8_t *)out, alphabet)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return &out[length];
|
2017-05-26 18:58:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 19:21:53 +00:00
|
|
|
void base32_encode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out) {
|
2019-03-29 14:44:55 +00:00
|
|
|
uint8_t remainder = inlen % 5;
|
|
|
|
size_t limit = inlen - remainder;
|
2017-05-25 19:21:53 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
size_t i, j;
|
|
|
|
for (i = 0, j = 0; i < limit; i += 5, j += 8) {
|
|
|
|
base32_5to8(&in[i], 5, &out[j]);
|
|
|
|
}
|
2017-05-25 19:21:53 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (remainder) base32_5to8(&in[i], remainder, &out[j]);
|
2017-05-25 19:21:53 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
bool base32_decode_unsafe(const uint8_t *in, size_t inlen, uint8_t *out,
|
|
|
|
const char *alphabet) {
|
|
|
|
uint8_t remainder = inlen % 8;
|
|
|
|
size_t limit = inlen - remainder;
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
size_t i, j;
|
|
|
|
for (i = 0, j = 0; i < limit; i += 8, j += 5) {
|
|
|
|
if (!base32_8to5(&in[i], 8, &out[j], alphabet)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (remainder && !base32_8to5(&in[i], remainder, &out[j], alphabet)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return true;
|
2017-05-26 18:58:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 19:50:13 +00:00
|
|
|
size_t base32_encoded_length(size_t inlen) {
|
2019-03-29 14:44:55 +00:00
|
|
|
uint8_t remainder = inlen % 5;
|
2017-05-25 19:50:13 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return (inlen / 5) * 8 + (remainder * 8 + 4) / 5;
|
2017-05-25 19:50:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 17:48:32 +00:00
|
|
|
size_t base32_decoded_length(size_t inlen) {
|
2019-03-29 14:44:55 +00:00
|
|
|
uint8_t remainder = inlen % 8;
|
2017-05-26 17:48:32 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return (inlen / 8) * 5 + (remainder * 5) / 8;
|
2017-05-26 17:48:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 18:16:14 +00:00
|
|
|
void base32_5to8(const uint8_t *in, uint8_t length, uint8_t *out) {
|
2019-03-29 14:44:55 +00:00
|
|
|
if (length >= 1) {
|
|
|
|
out[0] = (in[0] >> 3);
|
|
|
|
out[1] = (in[0] & 7) << 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 2) {
|
|
|
|
out[1] |= (in[1] >> 6);
|
|
|
|
out[2] = (in[1] >> 1) & 31;
|
|
|
|
out[3] = (in[1] & 1) << 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 3) {
|
|
|
|
out[3] |= (in[2] >> 4);
|
|
|
|
out[4] = (in[2] & 15) << 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 4) {
|
|
|
|
out[4] |= (in[3] >> 7);
|
|
|
|
out[5] = (in[3] >> 2) & 31;
|
|
|
|
out[6] = (in[3] & 3) << 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 5) {
|
|
|
|
out[6] |= (in[4] >> 5);
|
|
|
|
out[7] = (in[4] & 31);
|
|
|
|
}
|
2017-05-25 18:16:14 +00:00
|
|
|
}
|
2017-05-25 19:50:13 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
bool base32_8to5(const uint8_t *in, uint8_t length, uint8_t *out,
|
|
|
|
const char *alphabet) {
|
|
|
|
if (length == 1 || length == 3 || length == 6 || length > 8) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (alphabet) {
|
|
|
|
uint8_t decoded[length];
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
int ret = base32_decode_character(in[i], alphabet);
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
decoded[i] = ret;
|
|
|
|
}
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
base32_8to5_raw(decoded, length, out);
|
|
|
|
} else {
|
|
|
|
base32_8to5_raw(in, length, out);
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
2019-03-29 14:44:55 +00:00
|
|
|
return true;
|
2017-05-26 18:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void base32_8to5_raw(const uint8_t *in, uint8_t length, uint8_t *out) {
|
2019-03-29 14:44:55 +00:00
|
|
|
if (length >= 2) {
|
|
|
|
out[0] = (in[0] << 3);
|
|
|
|
out[0] |= (in[1] >> 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 4) {
|
|
|
|
out[1] = (in[1] & 3) << 6;
|
|
|
|
out[1] |= (in[2] << 1);
|
|
|
|
out[1] |= (in[3] >> 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 5) {
|
|
|
|
out[2] = (in[3] & 15) << 4;
|
|
|
|
out[2] |= (in[4] >> 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 7) {
|
|
|
|
out[3] = (in[4] & 1) << 7;
|
|
|
|
out[3] |= (in[5] << 2);
|
|
|
|
out[3] |= (in[6] >> 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length >= 8) {
|
|
|
|
out[4] = (in[6] & 7) << 5;
|
|
|
|
out[4] |= (in[7] & 31);
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 16:47:56 +00:00
|
|
|
int base32_encode_character(uint8_t decoded, const char *alphabet) {
|
2019-03-29 14:44:55 +00:00
|
|
|
if (decoded >> 5) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alphabet == BASE32_ALPHABET_RFC4648) {
|
|
|
|
if (decoded < 26) {
|
|
|
|
return 'A' + decoded;
|
|
|
|
} else {
|
|
|
|
return '2' - 26 + decoded;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return alphabet[decoded];
|
2017-05-25 19:50:13 +00:00
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
|
|
|
|
int base32_decode_character(char encoded, const char *alphabet) {
|
2019-03-29 14:44:55 +00:00
|
|
|
if (alphabet == BASE32_ALPHABET_RFC4648) {
|
|
|
|
if (encoded >= 'A' && encoded <= 'Z') {
|
|
|
|
return encoded - 'A';
|
|
|
|
} else if (encoded >= 'a' && encoded <= 'z') {
|
|
|
|
return encoded - 'a';
|
|
|
|
} else if (encoded >= '2' && encoded <= '7') {
|
|
|
|
return encoded - '2' + 26;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *occurrence = strchr(alphabet, encoded);
|
|
|
|
|
|
|
|
if (occurrence) {
|
|
|
|
return occurrence - alphabet;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-05-26 18:58:40 +00:00
|
|
|
}
|