util.c 3.1 KB

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