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.

159 lines
3.7 KiB

1 year ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /**
  2. * @file identity_list.c
  3. * @brief implementation of identity list functions
  4. * @license GNU General Public License 3.0 - see LICENSE.txt
  5. */
  6. #include "pEp_internal.h"
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include "pEpEngine.h"
  10. #include "identity_list.h"
  11. DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
  12. {
  13. identity_list *id_list = calloc(1, sizeof(identity_list));
  14. assert(id_list);
  15. if (id_list == NULL)
  16. return NULL;
  17. id_list->ident = ident;
  18. return id_list;
  19. }
  20. DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
  21. {
  22. assert(src);
  23. if (src == NULL)
  24. return NULL;
  25. if (src->ident == NULL)
  26. return new_identity_list(NULL);
  27. pEp_identity *_ident = identity_dup(src->ident);
  28. if (_ident == NULL)
  29. return NULL;
  30. identity_list *id_list = new_identity_list(_ident);
  31. if (id_list == NULL) {
  32. free_identity(_ident);
  33. return NULL;
  34. }
  35. identity_list* src_curr = src->next;
  36. identity_list** dst_curr_ptr = &id_list->next;
  37. while (src_curr) {
  38. _ident = identity_dup(src_curr->ident);
  39. if (_ident == NULL) {
  40. free_identity_list(id_list);
  41. return NULL;
  42. }
  43. *dst_curr_ptr = new_identity_list(_ident);
  44. if (*dst_curr_ptr == NULL) {
  45. free_identity(_ident);
  46. free_identity_list(id_list);
  47. return NULL;
  48. }
  49. src_curr = src_curr->next;
  50. dst_curr_ptr = &((*dst_curr_ptr)->next);
  51. }
  52. return id_list;
  53. }
  54. DYNAMIC_API void free_identity_list(identity_list *id_list)
  55. {
  56. identity_list *curr = id_list;
  57. while (curr) {
  58. identity_list *next = curr->next;
  59. free_identity(curr->ident);
  60. free(curr);
  61. curr = next;
  62. }
  63. }
  64. DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
  65. {
  66. assert(ident);
  67. if (ident == NULL)
  68. return NULL;
  69. if (id_list == NULL)
  70. return new_identity_list(ident);
  71. // empty list
  72. if (id_list->ident == NULL) {
  73. if (id_list->next)
  74. return NULL; // invalid list
  75. id_list->ident = ident;
  76. if (id_list->ident == NULL)
  77. return NULL;
  78. return id_list;
  79. }
  80. identity_list* list_curr = id_list;
  81. while (list_curr->next)
  82. list_curr = list_curr->next;
  83. list_curr->next = new_identity_list(ident);
  84. return list_curr->next;
  85. }
  86. // returns *head* of list
  87. DYNAMIC_API identity_list* identity_list_join(identity_list *first_list, identity_list *second_list) {
  88. if (!first_list) {
  89. if (!second_list)
  90. return NULL;
  91. return second_list;
  92. }
  93. if (second_list) {
  94. identity_list* list_curr = first_list;
  95. while (list_curr->next)
  96. list_curr = list_curr->next;
  97. list_curr->next = second_list;
  98. }
  99. return first_list;
  100. }
  101. DYNAMIC_API int identity_list_length(const identity_list *id_list)
  102. {
  103. int len = 0;
  104. for (; id_list && id_list->ident; id_list = id_list->next)
  105. ++len;
  106. return len;
  107. }
  108. PEP_STATUS set_all_userids_in_list(identity_list* id_list, const char* user_id) {
  109. if (!user_id || user_id[0] == '\0')
  110. return PEP_ILLEGAL_VALUE;
  111. identity_list* curr_list = id_list;
  112. while (curr_list) {
  113. if (curr_list->ident) {
  114. free(curr_list->ident->user_id);
  115. curr_list->ident->user_id = NULL;
  116. char* dup_userid = strdup(user_id);
  117. if (dup_userid == NULL)
  118. return PEP_OUT_OF_MEMORY;
  119. curr_list->ident->user_id = dup_userid;
  120. }
  121. curr_list = curr_list->next;
  122. }
  123. return PEP_STATUS_OK;
  124. }