Author | Jamozed <jake@omkov.net> |
Date | 2021-02-21 10:42:46 |
Commit | d079bad38b52cef50f07d8944000f69ccefc9a7f |
Parent | 5b04688b57c4425a6fb43f608b3523fb9815d276 |
lib: Add base32 version 1.0.2
Diffstat
A | src/lib/base32.c | | | 148 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/lib/base32.h | | | 45 | +++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 193 insertions, 0 deletions
diff --git a/src/lib/base32.c b/src/lib/base32.c new file mode 100644 index 0000000..76ed533 --- /dev/null +++ b/src/lib/base32.c @@ -0,0 +1,148 @@ +// base32.c, version 1.0.2 +// Base32 source file for OMKOV lib +// Copyright (C) 2021, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +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: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE. +*/ + +#include "base32.h" + +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +static const uint8_t B32E[]; +static const uint8_t B32D[]; + +/* Encode Base32 */ +size_t b32encode(uint8_t *dst, uint8_t *src, size_t len) { + register uint8_t *i = src, *o = dst; + + for (; len >= 5; len -= 5) { + *o++ = B32E[i[0] >> 3]; + *o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)]; + *o++ = B32E[(i[1] >> 1) & 0x1F]; + *o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)]; + *o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)]; + *o++ = B32E[(i[3] >> 2) & 0x1F]; + *o++ = B32E[((i[3] & 0x03) << 3) | (i[4] >> 5)]; + *o++ = B32E[i[4] & 0x1F]; i += 5; + } + + switch (len) { + case 1: { + *o++ = B32E[i[0] >> 3]; + *o++ = B32E[(i[0] & 0x07) << 2]; + memset(o, '=', 6); o += 6; break; + } + case 2: { + *o++ = B32E[i[0] >> 3]; + *o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)]; + *o++ = B32E[(i[1] >> 1) & 0x1F]; + *o++ = B32E[(i[1] & 0x01) << 4]; + memset(o, '=', 4); o += 4; break; + } + case 3: { + *o++ = B32E[i[0] >> 3]; + *o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)]; + *o++ = B32E[(i[1] >> 1) & 0x1F]; + *o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)]; + *o++ = B32E[(i[2] & 0x0F) << 1]; + memset(o, '=', 3); o += 3; break; + } + case 4: { + *o++ = B32E[i[0] >> 3]; + *o++ = B32E[((i[0] & 0x07) << 2) | (i[1] >> 6)]; + *o++ = B32E[(i[1] >> 1) & 0x1F]; + *o++ = B32E[((i[1] & 0x01) << 4) | (i[2] >> 4)]; + *o++ = B32E[((i[2] & 0x0F) << 1) | (i[3] >> 7)]; + *o++ = B32E[(i[3] >> 2) & 0x1F]; + *o++ = B32E[(i[3] & 0x03) << 3]; + *o++ = '='; break; + } + } + + return o - dst; +} + +/* Decode Base32 */ +size_t b32decode(uint8_t *dst, uint8_t *src, size_t len) { + register uint8_t *i = src, *o = dst; + + for (; src[len - 1] == '='; --len); + + for (; len >= 8; len -= 8) { + *o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2); + *o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4); + *o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1); + *o++ = (B32D[i[4]] << 7) | (B32D[i[5]] << 2) | (B32D[i[6]] >> 3); + *o++ = (B32D[i[6]] << 5) | (B32D[i[7]]); i += 8; + } + + switch (len) { + case 2: { + *o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2); + *o++ = (B32D[i[1]] << 6); break; + } + case 4: { + *o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2); + *o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4); + *o++ = (B32D[i[3]] << 4); break; + } + case 5: { + *o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2); + *o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4); + *o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1); + *o++ = (B32D[i[4]] << 7); break; + } + case 7: { + *o++ = (B32D[i[0]] << 3) | (B32D[i[1]] >> 2); + *o++ = (B32D[i[1]] << 6) | (B32D[i[2]] << 1) | (B32D[i[3]] >> 4); + *o++ = (B32D[i[3]] << 4) | (B32D[i[4]] >> 1); + *o++ = (B32D[i[4]] << 7) | (B32D[i[5]] << 2) | (B32D[i[6]] >> 3); + *o++ = (B32D[i[6]] << 5); break; + } + } + + return o - dst; +} + +static const uint8_t B32E[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', + 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', + '2', '3', '4', '5', '6', '7' +}; + +static const uint8_t B32D[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 +}; diff --git a/src/lib/base32.h b/src/lib/base32.h new file mode 100644 index 0000000..3cc2f08 --- /dev/null +++ b/src/lib/base32.h @@ -0,0 +1,45 @@ +// base32.h, version 1.0.2 +// Base32 header file for OMKOV lib +// Copyright (C) 2021, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +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: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS 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 WITH THE SOFTWARE. +*/ + +#ifndef OMKOV_LIB_BASE32_H_2PSMZTB0 +#define OMKOV_LIB_BASE32_H_2PSMZTB0 + +#include <stdint.h> +#include <stdlib.h> + +#define B32ELEN(x) (((x + 4) / 5) * 8) +#define B32DLEN(x) (((x + 7) / 8) * 5) + +extern size_t b32encode(uint8_t *dst, uint8_t *src, size_t len); +extern size_t b32decode(uint8_t *dst, uint8_t *src, size_t len); + +#endif // OMKOV_LIB_BASE32_H_2PSMZTB0