utils.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. utils.c -- gathering of some stupid small functions
  3. Copyright (C) 1999-2005 Ivo Timmermans <zarq@iname.com>
  4. 2000-2006 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
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include "system.h"
  18. #include "../src/logger.h"
  19. #include "utils.h"
  20. #ifdef ENABLE_TRACING
  21. volatile int (cp_line[]) = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  22. volatile char (*cp_file[]) = {"?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?"};
  23. volatile int cp_index = 0;
  24. #endif
  25. char *hexadecimals = "0123456789ABCDEF";
  26. int charhex2bin(char c)
  27. {
  28. if(isdigit(c))
  29. return c - '0';
  30. else
  31. return toupper(c) - 'A' + 10;
  32. }
  33. void hex2bin(char *src, char *dst, int length)
  34. {
  35. int i;
  36. for(i = 0; i < length; i++)
  37. dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
  38. }
  39. void bin2hex(char *src, char *dst, int length)
  40. {
  41. int i;
  42. for(i = length - 1; i >= 0; i--) {
  43. dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
  44. dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
  45. }
  46. }
  47. #ifdef ENABLE_TRACING
  48. void cp_trace()
  49. {
  50. logger(LOG_DEBUG, "Checkpoint trace: %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d <- %s:%d...",
  51. cp_file[(cp_index + 15) % 16], cp_line[(cp_index + 15) % 16],
  52. cp_file[(cp_index + 14) % 16], cp_line[(cp_index + 14) % 16],
  53. cp_file[(cp_index + 13) % 16], cp_line[(cp_index + 13) % 16],
  54. cp_file[(cp_index + 12) % 16], cp_line[(cp_index + 12) % 16],
  55. cp_file[(cp_index + 11) % 16], cp_line[(cp_index + 11) % 16],
  56. cp_file[(cp_index + 10) % 16], cp_line[(cp_index + 10) % 16],
  57. cp_file[(cp_index + 9) % 16], cp_line[(cp_index + 9) % 16],
  58. cp_file[(cp_index + 8) % 16], cp_line[(cp_index + 8) % 16],
  59. cp_file[(cp_index + 7) % 16], cp_line[(cp_index + 7) % 16],
  60. cp_file[(cp_index + 6) % 16], cp_line[(cp_index + 6) % 16],
  61. cp_file[(cp_index + 5) % 16], cp_line[(cp_index + 5) % 16],
  62. cp_file[(cp_index + 4) % 16], cp_line[(cp_index + 4) % 16],
  63. cp_file[(cp_index + 3) % 16], cp_line[(cp_index + 3) % 16],
  64. cp_file[(cp_index + 2) % 16], cp_line[(cp_index + 2) % 16],
  65. cp_file[(cp_index + 1) % 16], cp_line[(cp_index + 1) % 16],
  66. cp_file[cp_index], cp_line[cp_index]
  67. );
  68. }
  69. #endif
  70. #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
  71. #ifdef HAVE_CYGWIN
  72. #include <w32api/windows.h>
  73. #endif
  74. char *winerror(int err) {
  75. static char buf[1024], *newline;
  76. if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  77. NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, sizeof(buf), NULL)) {
  78. strncpy(buf, _("(unable to format errormessage)"), sizeof(buf));
  79. };
  80. if((newline = strchr(buf, '\r')))
  81. *newline = '\0';
  82. return buf;
  83. }
  84. #endif