p≡p engine fork for my own dirty testing of stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.2 KiB

8 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. /**
  2. * @file keyreset_command.c
  3. * @brief implementation of keyreset command structure and list memory,
  4. * manipulation and informational functions
  5. * @license GNU General Public License 3.0 - see LICENSE.txt
  6. */
  7. #include "pEp_internal.h"
  8. #include <stdlib.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include "keyreset_command.h"
  12. DYNAMIC_API keyreset_command * new_keyreset_command(const pEp_identity * ident, const char * new_key)
  13. {
  14. keyreset_command * command = NULL;
  15. // key and command should not be NULL, that's bad style (while legal)
  16. assert(ident);
  17. assert(new_key);
  18. command = calloc(1, sizeof(keyreset_command));
  19. assert(command);
  20. if (command == NULL)
  21. goto enomem;
  22. command->ident = ident ? identity_dup(ident) : new_identity(NULL, NULL, NULL, NULL);
  23. if (command->ident == NULL)
  24. goto enomem;
  25. if (command->ident && command->ident->fpr) {
  26. // make content uppercase
  27. for (size_t i=0; i<strlen(command->ident->fpr); i++)
  28. command->ident->fpr[i] = toupper(command->ident->fpr[i]);
  29. }
  30. command->new_key = new_key ? strdup(new_key) : strdup("");
  31. assert(command->new_key);
  32. if (command->new_key == NULL)
  33. goto enomem;
  34. // make content uppercase
  35. for (size_t i=0; i<strlen(command->new_key); i++)
  36. command->new_key[i] = toupper(command->new_key[i]);
  37. return command;
  38. enomem:
  39. free_keyreset_command(command);
  40. return NULL;
  41. }
  42. DYNAMIC_API void free_keyreset_command(keyreset_command * command)
  43. {
  44. if (command) {
  45. free_identity(command->ident);
  46. free(command->new_key);
  47. free(command);
  48. }
  49. }
  50. DYNAMIC_API keyreset_command * keyreset_command_dup(const keyreset_command * src)
  51. {
  52. assert(src);
  53. if (src == NULL)
  54. return NULL;
  55. return new_keyreset_command(src->ident, src->new_key);
  56. }
  57. DYNAMIC_API keyreset_command_list * new_keyreset_command_list(keyreset_command * command)
  58. {
  59. keyreset_command_list * result = calloc(1, sizeof(keyreset_command_list));
  60. assert(result);
  61. if (result && command)
  62. result->command = command;
  63. return result;
  64. }
  65. DYNAMIC_API keyreset_command_list * keyreset_command_list_dup(
  66. const keyreset_command_list * src
  67. )
  68. {
  69. assert(src);
  70. if (src == NULL)
  71. return NULL;
  72. keyreset_command * cpy = keyreset_command_dup(src->command);
  73. keyreset_command_list * dst = new_keyreset_command_list(cpy);
  74. if (dst == NULL)
  75. return NULL;
  76. keyreset_command_list * src_curr = src->next;
  77. keyreset_command_list ** dst_curr_ptr = &dst->next;
  78. while (src_curr) {
  79. cpy = keyreset_command_dup(src_curr->command);
  80. if (cpy == NULL) {
  81. free_keyreset_command_list(dst);
  82. return NULL;
  83. }
  84. *dst_curr_ptr = new_keyreset_command_list(cpy);
  85. if (*dst_curr_ptr == NULL) {
  86. free_keyreset_command(cpy);
  87. free_keyreset_command_list(dst);
  88. return NULL;
  89. }
  90. src_curr = src_curr->next;
  91. dst_curr_ptr = &((*dst_curr_ptr)->next);
  92. }
  93. return dst;
  94. }
  95. DYNAMIC_API keyreset_command_list * keyreset_command_list_add(
  96. keyreset_command_list * command_list,
  97. keyreset_command * command
  98. )
  99. {
  100. assert(command);
  101. // empty list (no nodes)
  102. if (command_list == NULL)
  103. return new_keyreset_command_list(command);
  104. // empty list (one node, no command)
  105. if (command_list->command == NULL) {
  106. if (command_list->next)
  107. return NULL; // invalid list
  108. command_list->command = command;
  109. assert(command_list->command);
  110. if (command_list->command == NULL)
  111. return NULL;
  112. return command_list;
  113. }
  114. keyreset_command_list * list_curr = command_list;
  115. while (list_curr->next)
  116. list_curr = list_curr->next;
  117. list_curr->next = new_keyreset_command_list(command);
  118. assert(list_curr->next);
  119. if (list_curr->next == NULL)
  120. return NULL;
  121. return list_curr->next;
  122. }
  123. DYNAMIC_API keyreset_command_list * keyreset_command_list_append(
  124. keyreset_command_list * command_list,
  125. keyreset_command_list * second
  126. )
  127. {
  128. assert(command_list);
  129. if (command_list == NULL)
  130. return NULL;
  131. // second list is empty
  132. if (second == NULL || second->command == NULL)
  133. return command_list;
  134. keyreset_command_list * _s = command_list;
  135. for (keyreset_command_list * _s2 = second; _s2 != NULL; _s2 = _s2->next) {
  136. keyreset_command * _sp = keyreset_command_dup(_s2->command);
  137. if (_sp == NULL)
  138. return NULL;
  139. _s = keyreset_command_list_add(_s, _sp);
  140. if (_s == NULL){
  141. free_keyreset_command(_sp);
  142. return NULL;
  143. }
  144. }
  145. return _s;
  146. }
  147. DYNAMIC_API int keyreset_command_list_length(
  148. const keyreset_command_list * command_list
  149. )
  150. {
  151. int len = 0;
  152. for (const keyreset_command_list * _sl = command_list; _sl && _sl->command; _sl = _sl->next)
  153. len++;
  154. return len;
  155. }
  156. DYNAMIC_API void free_keyreset_command_list(keyreset_command_list * command_list)
  157. {
  158. if (command_list) {
  159. free_keyreset_command_list(command_list->next);
  160. free_keyreset_command(command_list->command);
  161. free(command_list);
  162. }
  163. }