util.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "log.h"
  2. #include "util.h"
  3. #include <string.h>
  4. #include <tox/tox.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. void writechecksum(uint8_t *address)
  8. {
  9. uint8_t *checksum = address + 36;
  10. uint32_t i;
  11. for (i = 0; i < 36; ++i)
  12. checksum[i % 2] ^= address[i];
  13. }
  14. /* From utox/util.c */
  15. void to_hex(char_t *a, const char_t *p, int size)
  16. {
  17. char_t b, c;
  18. const char_t *end = p + size;
  19. while(p != end) {
  20. b = *p++;
  21. c = (b & 0xF);
  22. b = (b >> 4);
  23. if(b < 10) {
  24. *a++ = b + '0';
  25. } else {
  26. *a++ = b - 10 + 'A';
  27. }
  28. if(c < 10) {
  29. *a++ = c + '0';
  30. } else {
  31. *a++ = c - 10 + 'A';
  32. }
  33. }
  34. a = '\0';
  35. }
  36. /* From utox/util.c */
  37. void id_to_string(char_t *dest, const char_t *src)
  38. {
  39. to_hex(dest, src, TOX_FRIEND_ADDRESS_SIZE);
  40. }
  41. /* From utox/util.c */
  42. int string_to_id(char_t *w, char_t *a)
  43. {
  44. char_t *end = w + TOX_FRIEND_ADDRESS_SIZE;
  45. while(w != end) {
  46. char_t c, v;
  47. c = *a++;
  48. if(c >= '0' && c <= '9') {
  49. v = (c - '0') << 4;
  50. } else if(c >= 'A' && c <= 'F') {
  51. v = (c - 'A' + 10) << 4;
  52. } else if(c >= 'a' && c <= 'f') {
  53. v = (c - 'a' + 10) << 4;
  54. } else {
  55. return 0;
  56. }
  57. c = *a++;
  58. if(c >= '0' && c <= '9') {
  59. v |= (c - '0');
  60. } else if(c >= 'A' && c <= 'F') {
  61. v |= (c - 'A' + 10);
  62. } else if(c >= 'a' && c <= 'f') {
  63. v |= (c - 'a' + 10);
  64. } else {
  65. return 0;
  66. }
  67. *w++ = v;
  68. }
  69. return 1;
  70. }
  71. /* Parse the -L parameter */
  72. /* 0 = success */
  73. int parse_local_port_forward(char *string, int *local_port, char **hostname, int *remote_port)
  74. {
  75. char *lport;
  76. char *host;
  77. char *rport;
  78. lport = strtok(string, ":");
  79. host = strtok(NULL, ":");
  80. rport = strtok(NULL, ":");
  81. if(!lport || !host || !rport)
  82. {
  83. return -1;
  84. }
  85. *local_port = atoi(lport);
  86. *hostname = host;
  87. *remote_port = atoi(rport);
  88. return 0;
  89. }
  90. /* Parse the -P parameter */
  91. /* 0 = success */
  92. int parse_pipe_port_forward(char *string, char **hostname, int *remote_port)
  93. {
  94. char *host;
  95. char *rport;
  96. host = strtok(string, ":");
  97. rport = strtok(NULL, ":");
  98. if(!host || !rport)
  99. {
  100. return -1;
  101. }
  102. *hostname = host;
  103. *remote_port = atoi(rport);
  104. return 0;
  105. }
  106. void* file_raw(char *path, uint32_t *size)
  107. {
  108. FILE *file;
  109. char *data;
  110. int len;
  111. file = fopen(path, "rb");
  112. if(!file) {
  113. log_printf(L_WARNING, "File not found (%s)\n", path);
  114. return NULL;
  115. }
  116. fseek(file, 0, SEEK_END);
  117. len = ftell(file);
  118. data = malloc(len);
  119. if(!data) {
  120. fclose(file);
  121. return NULL;
  122. }
  123. fseek(file, 0, SEEK_SET);
  124. if(fread(data, len, 1, file) != 1) {
  125. log_printf(L_WARNING, "Read error (%s)\n", path);
  126. fclose(file);
  127. free(data);
  128. return NULL;
  129. }
  130. fclose(file);
  131. log_printf(L_DEBUG, "Read %u bytes (%s)\n", len, path);
  132. if(size) {
  133. *size = len;
  134. }
  135. return data;
  136. }