utils.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. utils.c -- gathering of some stupid small functions
  3. Copyright (C) 1999-2005 Ivo Timmermans
  4. 2000-2014 Guus Sliepen <guus@tinc-vpn.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "system.h"
  18. #include "../src/logger.h"
  19. #include "utils.h"
  20. static const char hexadecimals[] = "0123456789ABCDEF";
  21. static int charhex2bin(char c) {
  22. if(isdigit(c)) {
  23. return c - '0';
  24. } else {
  25. return toupper(c) - 'A' + 10;
  26. }
  27. }
  28. bool hex2bin(char *src, char *dst, int length) {
  29. for(int i = 0; i < length; i++) {
  30. if(!isxdigit(src[i * 2]) || !isxdigit(src[i * 2 + 1])) {
  31. return false;
  32. }
  33. dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
  34. }
  35. return true;
  36. }
  37. void bin2hex(char *src, char *dst, int length) {
  38. int i;
  39. for(i = length - 1; i >= 0; i--) {
  40. dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
  41. dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
  42. }
  43. }
  44. #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
  45. #ifdef HAVE_CYGWIN
  46. #include <w32api/windows.h>
  47. #endif
  48. const char *winerror(int err) {
  49. static char buf[1024], *ptr;
  50. ptr = buf + sprintf(buf, "(%d) ", err);
  51. if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  52. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), ptr, sizeof(buf) - (ptr - buf), NULL)) {
  53. strcpy(ptr, "(unable to format errormessage)");
  54. };
  55. if((ptr = strchr(buf, '\r'))) {
  56. *ptr = '\0';
  57. }
  58. return buf;
  59. }
  60. #endif
  61. unsigned int bitfield_to_int(const void *bitfield, size_t size) {
  62. unsigned int value = 0;
  63. if(size > sizeof(value)) {
  64. size = sizeof(value);
  65. }
  66. memcpy(&value, bitfield, size);
  67. return value;
  68. }
  69. /**
  70. * As memcmp(), but constant-time.
  71. * Returns 0 when data is equal, non-zero otherwise.
  72. */
  73. int memcmp_constant_time(const void *a, const void *b, size_t size) {
  74. const uint8_t *a1 = a, *b1 = b;
  75. int ret = 0;
  76. size_t i;
  77. for(i = 0; i < size; i++) {
  78. ret |= *a1++ ^ *b1++;
  79. }
  80. return ret;
  81. }