X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Futils.c;h=8b0cd6edbedeed17ee21c33e9c94932c11362baf;hb=7be02138580c6821145405fd55def976dfde4998;hp=da8c4331c1db8186fe936306a4c550997a334246;hpb=f6e87ab476a0faf8b124ecaaa27f967d825e6457;p=tinc diff --git a/src/utils.c b/src/utils.c index da8c4331..8b0cd6ed 100644 --- a/src/utils.c +++ b/src/utils.c @@ -28,46 +28,49 @@ static const char base64_original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmno static const char base64_urlsafe[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; static const char base64_decode[256] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, - -1, 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, -1, -1, -1, -1, 63, - -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, - 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -}; - -static int charhex2bin(char c) { - if(isdigit(c)) { - return c - '0'; + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, + -1, 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, -1, -1, -1, -1, 63, + -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, + }; + +static uint8_t charhex2bin(char c) { + uint8_t cu = (uint8_t) c; + + if(isdigit(cu)) { + return cu - '0'; } else { - return toupper(c) - 'A' + 10; + return toupper(cu) - 'A' + 10; } } -int hex2bin(const char *src, void *vdst, int length) { - char *dst = vdst; - int i; +size_t hex2bin(const char *src, void *vdst, size_t length) { + uint8_t *dst = vdst; + size_t i; - for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++) { + for(i = 0; i < length && isxdigit((uint8_t) src[i * 2]) && isxdigit((uint8_t) src[i * 2 + 1]); i++) { dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]); } return i; } -int bin2hex(const void *vsrc, char *dst, int length) { +size_t bin2hex(const void *vsrc, char *dst, size_t length) { const char *src = vsrc; - for(int i = length - 1; i >= 0; i--) { + for(size_t i = length; i > 0;) { + --i; dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15]; dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4]; } @@ -76,8 +79,8 @@ int bin2hex(const void *vsrc, char *dst, int length) { return length * 2; } -int b64decode(const char *src, void *dst, int length) { - int i; +size_t b64decode_tinc(const char *src, void *dst, size_t length) { + size_t i; uint32_t triplet = 0; unsigned char *udst = (unsigned char *)dst; @@ -116,11 +119,11 @@ int b64decode(const char *src, void *dst, int length) { } } -static int b64encode_internal(const void *src, char *dst, int length, const char *alphabet) { +static size_t b64encode_tinc_internal(const void *src, char *dst, size_t length, const char *alphabet) { uint32_t triplet; const unsigned char *usrc = (unsigned char *)src; - int si = length / 3 * 3; - int di = length / 3 * 4; + size_t si = length / 3 * 3; + size_t di = length / 3 * 4; switch(length % 3) { case 2: @@ -165,19 +168,15 @@ static int b64encode_internal(const void *src, char *dst, int length, const char return length; } -int b64encode(const void *src, char *dst, int length) { - return b64encode_internal(src, dst, length, base64_original); +size_t b64encode_tinc(const void *src, char *dst, size_t length) { + return b64encode_tinc_internal(src, dst, length, base64_original); } -int b64encode_urlsafe(const void *src, char *dst, int length) { - return b64encode_internal(src, dst, length, base64_urlsafe); +size_t b64encode_tinc_urlsafe(const void *src, char *dst, size_t length) { + return b64encode_tinc_internal(src, dst, length, base64_urlsafe); } -#if defined(HAVE_MINGW) || defined(HAVE_CYGWIN) -#ifdef HAVE_CYGWIN -#include -#endif - +#ifdef HAVE_MINGW const char *winerror(int err) { static char buf[1024], *ptr; @@ -196,24 +195,13 @@ const char *winerror(int err) { } #endif -unsigned int bitfield_to_int(const void *bitfield, size_t size) { - unsigned int value = 0; - - if(size > sizeof(value)) { - size = sizeof(value); - } - - memcpy(&value, bitfield, size); - return value; -} - bool check_id(const char *id) { if(!id || !*id) { return false; } for(; *id; id++) - if(!isalnum(*id) && *id != '_') { + if(!isalnum((uint8_t) *id) && *id != '_') { return false; } @@ -226,7 +214,7 @@ bool check_netname(const char *netname, bool strict) { } for(const char *c = netname; *c; c++) { - if(iscntrl(*c)) { + if(iscntrl((uint8_t) *c)) { return false; } @@ -272,7 +260,7 @@ char *replace_name(const char *name) { ret_name = xstrdup(envname); for(char *c = ret_name; *c; c++) - if(!isalnum(*c)) { + if(!isalnum((uint8_t) *c)) { *c = '_'; } } else { @@ -287,3 +275,30 @@ char *replace_name(const char *name) { return ret_name; } + +/* Open a file with the desired permissions, minus the umask. + Also, if we want to create an executable file, we call fchmod() + to set the executable bits. */ + +FILE *fopenmask(const char *filename, const char *mode, mode_t perms) { + mode_t mask = umask(0); + perms &= ~mask; + umask(~perms & 0777); + FILE *f = fopen(filename, mode); + + if(!f) { + fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno)); + return NULL; + } + +#ifdef HAVE_FCHMOD + + if(perms & 0444) { + fchmod(fileno(f), perms); + } + +#endif + umask(mask); + return f; +} +