util.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_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_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. /* First replace all @ with :, ':' is forbidden in some environments */
  79. char *p = string;
  80. while(*p)
  81. {
  82. if(*p == '@') *p = ':';
  83. p++;
  84. }
  85. lport = strtok(string, ":");
  86. host = strtok(NULL, ":");
  87. rport = strtok(NULL, ":");
  88. if(!lport || !host || !rport)
  89. {
  90. return -1;
  91. }
  92. *local_port = atoi(lport);
  93. *hostname = host;
  94. *remote_port = atoi(rport);
  95. return 0;
  96. }
  97. /* Parse the -P parameter */
  98. /* 0 = success */
  99. int parse_pipe_port_forward(char *string, char **hostname, int *remote_port)
  100. {
  101. char *host;
  102. char *rport;
  103. /* First replace all @ with :, ':' is forbidden in some environments */
  104. char *p = string;
  105. while(*p)
  106. {
  107. if(*p == '@') *p = ':';
  108. p++;
  109. }
  110. host = strtok(string, ":");
  111. rport = strtok(NULL, ":");
  112. if(!host || !rport)
  113. {
  114. return -1;
  115. }
  116. *hostname = host;
  117. *remote_port = atoi(rport);
  118. return 0;
  119. }
  120. void* file_raw(char *path, uint32_t *size)
  121. {
  122. FILE *file;
  123. char *data;
  124. int len;
  125. file = fopen(path, "rb");
  126. if(!file) {
  127. log_printf(L_WARNING, "File not found (%s)\n", path);
  128. return NULL;
  129. }
  130. fseek(file, 0, SEEK_END);
  131. len = ftell(file);
  132. data = malloc(len);
  133. if(!data) {
  134. fclose(file);
  135. return NULL;
  136. }
  137. fseek(file, 0, SEEK_SET);
  138. if(fread(data, len, 1, file) != 1) {
  139. log_printf(L_WARNING, "Read error (%s)\n", path);
  140. fclose(file);
  141. free(data);
  142. return NULL;
  143. }
  144. fclose(file);
  145. log_printf(L_DEBUG, "Read %u bytes (%s)\n", len, path);
  146. if(size) {
  147. *size = len;
  148. }
  149. return data;
  150. }
  151. const char *readable_connection_status(TOX_CONNECTION status)
  152. {
  153. switch(status)
  154. {
  155. case TOX_CONNECTION_NONE:
  156. return "There is no connection";
  157. case TOX_CONNECTION_TCP:
  158. return "A TCP connection has been established (via TCP relay)";
  159. case TOX_CONNECTION_UDP:
  160. return "An UDP connection has been established";
  161. default:
  162. return "Unknown connection status";
  163. }
  164. }