X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Futils.c;h=c58f8ca8ddd9857d8c35a00c99f633dcd4624596;hb=95e1cc36d320b47408ac3ec6f89df54e55a010d4;hp=dd7e4de25c53548a709258bbf79626b35bb08d43;hpb=886a6f61a1f4cc48a77b42d10f34f9126377d904;p=tinc diff --git a/src/utils.c b/src/utils.c index dd7e4de2..c58f8ca8 100644 --- a/src/utils.c +++ b/src/utils.c @@ -23,15 +23,28 @@ #include "../src/logger.h" #include "utils.h" -const char hexadecimals[] = "0123456789ABCDEF"; +static const char hexadecimals[] = "0123456789ABCDEF"; +static const char base64imals[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -int charhex2bin(char c) { +static int charhex2bin(char c) { if(isdigit(c)) return c - '0'; else return toupper(c) - 'A' + 10; } +static int charb64decode(char c) { + if(c >= 'a') + return c - 'a' + 26; + else if(c >= 'A') + return c - 'A'; + else if(c >= '0') + return c - '0' + 52; + else if(c == '+') + return 62; + else + return 63; +} void hex2bin(char *src, char *dst, int length) { int i; @@ -47,6 +60,65 @@ void bin2hex(char *src, char *dst, int length) { } } +int b64decode(const char *src, char *dst, int length) { + uint32_t triplet = 0; + unsigned char *udst = dst; + + for(int i = 0; i < length; i++) { + triplet |= charb64decode(src[i]) << (6 * (i & 3)); + if((i & 3) == 3) { + udst[0] = triplet & 0xff; triplet >>= 8; + udst[1] = triplet & 0xff; triplet >>= 8; + udst[2] = triplet; + triplet = 0; + udst += 3; + } + } + if((length & 3) == 3) { + udst[0] = triplet & 0xff; triplet >>= 8; + udst[1] = triplet & 0xff; + return length / 4 * 3 + 2; + } else if((length & 3) == 2) { + udst[0] = triplet & 0xff; + return length / 4 * 3 + 1; + } else { + return length / 4 * 3; + } +} + +int b64encode(const char *src, char *dst, int length) { + uint32_t triplet; + const unsigned char *usrc = src; + int origlen = length; + + while(length > 0) { + if(length >= 3) { + triplet = usrc[0] | usrc[1] << 8 | usrc[2] << 16; + dst[0] = base64imals[triplet & 63]; triplet >>= 6; + dst[1] = base64imals[triplet & 63]; triplet >>= 6; + dst[2] = base64imals[triplet & 63]; triplet >>= 6; + dst[3] = base64imals[triplet]; + dst += 4; usrc += 3; length -= 3; + } else if(length >=2) { + triplet = usrc[0] | usrc[1] << 8; + dst[0] = base64imals[triplet & 63]; triplet >>= 6; + dst[1] = base64imals[triplet & 63]; triplet >>= 6; + dst[2] = base64imals[triplet]; + dst[3] = 0; + return origlen / 3 * 4 + 3; + } else { + triplet = usrc[0]; + dst[0] = base64imals[triplet & 63]; triplet >>= 6; + dst[1] = base64imals[triplet]; + dst[2] = 0; + return origlen / 3 * 4 + 2; + } + } + + *dst = 0; + return origlen / 4 * 3; +} + #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN) #ifdef HAVE_CYGWIN #include @@ -67,7 +139,7 @@ const char *winerror(int err) { } #endif -unsigned int bitfield_to_int(void *bitfield, size_t size) { +unsigned int bitfield_to_int(const void *bitfield, size_t size) { unsigned int value = 0; if(size > sizeof value) size = sizeof value;