/centutils/libcentutils/ctarray.c

https://github.com/BeyondTrust/pbis-open · C · 135 lines · 89 code · 16 blank · 30 comment · 11 complexity · 029d4b54f875ee8434fef7f75580a28e MD5 · raw file

  1. /* Editor Settings: expandtabs and use 4 spaces for indentation
  2. * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
  3. * -*- mode: c, c-basic-offset: 4 -*- */
  4. /*
  5. * Copyright © BeyondTrust Software 2004 - 2019
  6. * All rights reserved.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * BEYONDTRUST MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING TERMS AS
  21. * WELL. IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT WITH
  22. * BEYONDTRUST, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE TERMS OF THAT
  23. * SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE APACHE LICENSE,
  24. * NOTWITHSTANDING THE ABOVE NOTICE. IF YOU HAVE QUESTIONS, OR WISH TO REQUEST
  25. * A COPY OF THE ALTERNATE LICENSING TERMS OFFERED BY BEYONDTRUST, PLEASE CONTACT
  26. * BEYONDTRUST AT beyondtrust.com/contact
  27. */
  28. #include "config.h"
  29. #include "ctbase.h"
  30. #include "ctarray.h"
  31. DWORD CTArrayConstruct(DynamicArray* array, size_t itemSize)
  32. {
  33. DWORD ceError = ERROR_SUCCESS;
  34. array->size = 0;
  35. array->capacity = 32;
  36. BAIL_ON_CENTERIS_ERROR(ceError = CTAllocateMemory(array->capacity*itemSize,
  37. (PVOID*)&array->data));
  38. error:
  39. return ceError;
  40. }
  41. DWORD CTSetCapacity(DynamicArray *array, size_t itemSize, size_t capacity)
  42. {
  43. DWORD ceError = ERROR_SUCCESS;
  44. /* Resize the array */
  45. ceError = CTReallocMemory(array->data, &array->data, capacity * itemSize);
  46. BAIL_ON_CENTERIS_ERROR(ceError);
  47. array->capacity = capacity;
  48. if(array->size > capacity)
  49. array->size = capacity;
  50. error:
  51. return ceError;
  52. }
  53. DWORD CTArrayInsert(DynamicArray *array, int insertPos, int itemSize, const void *data, size_t dataLen)
  54. {
  55. DWORD ceError = ERROR_SUCCESS;
  56. if(array->size + dataLen > array->capacity)
  57. {
  58. /* Resize the array */
  59. ceError = CTSetCapacity(array, itemSize, array->capacity + dataLen + array->capacity);
  60. BAIL_ON_CENTERIS_ERROR(ceError);
  61. }
  62. /* Make room for the new value */
  63. memmove((char *)array->data + (insertPos + dataLen)*itemSize,
  64. (char *)array->data + insertPos*itemSize,
  65. (array->size - insertPos)*itemSize);
  66. memcpy((char *)array->data + insertPos*itemSize, data, dataLen*itemSize);
  67. array->size += dataLen;
  68. error:
  69. return ceError;
  70. }
  71. DWORD CTArrayAppend(DynamicArray *array, int itemSize, const void *data, size_t dataLen)
  72. {
  73. return CTArrayInsert(array, array->size, itemSize, data, dataLen);
  74. }
  75. DWORD CTArrayRemove(DynamicArray *array, int removePos, int itemSize, size_t dataLen)
  76. {
  77. if(dataLen + removePos > array->size)
  78. dataLen = array->size - removePos;
  79. if(dataLen == 0)
  80. return ERROR_INVALID_PARAMETER;
  81. memmove((char *)array->data + removePos*itemSize,
  82. (char *)array->data + (removePos + dataLen)*itemSize,
  83. (array->size - removePos - dataLen)*itemSize);
  84. array->size -= dataLen;
  85. return ERROR_SUCCESS;
  86. }
  87. size_t CTArrayRemoveHead(DynamicArray *array, int itemSize, void *store, size_t dataLen)
  88. {
  89. if(dataLen > array->size)
  90. dataLen = array->size;
  91. if(store != NULL)
  92. {
  93. memcpy(store, array->data, dataLen * itemSize);
  94. }
  95. CTArrayRemove(array, 0, itemSize, dataLen);
  96. return dataLen;
  97. }
  98. void CTArrayFree(DynamicArray *array)
  99. {
  100. CT_SAFE_FREE_MEMORY(array->data);
  101. array->size = 0;
  102. array->capacity = 0;
  103. }
  104. void * CTArrayGetItem(DynamicArray *array, size_t index, size_t itemSize)
  105. {
  106. if(index >= array->size)
  107. return NULL;
  108. return (char *)array->data + index*itemSize;
  109. }
  110. ssize_t CTArrayFindString(DynamicArray* array, PCSTR find)
  111. {
  112. size_t i;
  113. for(i = 0; i < array->size; i++)
  114. {
  115. if(!strcmp(*(PCSTR *)CTArrayGetItem(array, i, sizeof(PCSTR)), find))
  116. return i;
  117. }
  118. return -1;
  119. }