X-Git-Url: http://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Futils.c;h=3a3030d1ce3ebfcf8cd0b42e7a5193ab1185b669;hb=c8402791b82947c49ba1d04f855dab04191607ca;hp=6104fada3ab62b2007be2d5f4c835c06f963970a;hpb=b2701c7c54b11cda71461c5dbbc985476bf5b221;p=tinc diff --git a/src/utils.c b/src/utils.c index 6104fada..3a3030d1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -85,7 +85,7 @@ size_t b64decode_tinc(const char *src, void *dst, size_t length) { unsigned char *udst = (unsigned char *)dst; for(i = 0; i < length && src[i]; i++) { - triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3)); + triplet |= (uint32_t)(base64_decode[src[i] & 0xff] << (6 * (i & 3))); if((i & 3) == 3) { if(triplet & 0xff000000U) { @@ -119,6 +119,24 @@ size_t b64decode_tinc(const char *src, void *dst, size_t length) { } } +bool is_decimal(const char *str) { + if(!str) { + return false; + } + + errno = 0; + char *badchar = NULL; + strtol(str, &badchar, 10); + return !errno && badchar != str && !*badchar; +} + +// itoa() conflicts with a similarly named function under MinGW. +char *int_to_str(int num) { + char *str = NULL; + xasprintf(&str, "%d", num); + return str; +} + 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; @@ -176,7 +194,7 @@ size_t b64encode_tinc_urlsafe(const void *src, char *dst, size_t length) { return b64encode_tinc_internal(src, dst, length, base64_urlsafe); } -#ifdef HAVE_MINGW +#ifdef HAVE_WINDOWS const char *winerror(int err) { static char buf[1024], *ptr; @@ -195,17 +213,6 @@ 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; @@ -287,29 +294,7 @@ 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; +bool string_eq(const char *first, const char *second) { + return !first == !second && + !(first && second && strcmp(first, second)); } -