/lwtools/net/common/memory.c

https://github.com/BeyondTrust/pbis-open · C · 216 lines · 139 code · 34 blank · 43 comment · 9 complexity · 08c79b65c54fb89704e1d28fbb49d381 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. /*
  29. * Copyright (C) BeyondTrust Software. All rights reserved.
  30. *
  31. * Module Name:
  32. *
  33. * memory.c
  34. *
  35. * Abstract:
  36. *
  37. * BeyondTrust System NET Utilities
  38. *
  39. * Memory Utilities
  40. *
  41. * Authors: Krishna Ganugapati (krishnag@likewisesoftware.com)
  42. * Wei Fu (wfu@likewise.com)
  43. */
  44. #include "includes.h"
  45. DWORD
  46. LwNetAllocateMemory(
  47. size_t Size,
  48. LW_PVOID * ppMemory
  49. )
  50. {
  51. return LwAllocateMemory(Size, ppMemory);
  52. }
  53. DWORD
  54. LwNetReallocMemory(
  55. LW_PVOID pMemory,
  56. LW_PVOID * ppNewMemory,
  57. size_t Size
  58. )
  59. {
  60. return LwReallocMemory(pMemory,
  61. ppNewMemory,
  62. Size);
  63. }
  64. VOID
  65. LwNetFreeMemory(
  66. PVOID pMemory
  67. )
  68. {
  69. LwFreeMemory(pMemory);
  70. }
  71. DWORD
  72. LwNetAllocateString(
  73. PCSTR pszInputString,
  74. PSTR* ppszOutputString
  75. )
  76. {
  77. DWORD dwError = 0;
  78. DWORD dwLen = 0;
  79. PSTR pszOutputString = NULL;
  80. if (!pszInputString) {
  81. dwError = LW_ERROR_INVALID_PARAMETER;
  82. BAIL_ON_LTNET_ERROR(dwError);
  83. }
  84. dwLen = strlen(pszInputString);
  85. dwError = LwNetAllocateMemory(dwLen+1, (PVOID *)&pszOutputString);
  86. BAIL_ON_LTNET_ERROR(dwError);
  87. if (dwLen) {
  88. memcpy(pszOutputString, pszInputString, dwLen);
  89. }
  90. *ppszOutputString = pszOutputString;
  91. cleanup:
  92. return dwError;
  93. error:
  94. LTNET_SAFE_FREE_STRING(pszOutputString);
  95. *ppszOutputString = NULL;
  96. goto cleanup;
  97. }
  98. VOID
  99. LwNetFreeString(
  100. PSTR pszString
  101. )
  102. {
  103. LwNetFreeMemory(pszString);
  104. }
  105. VOID
  106. LwNetFreeStringArray(
  107. DWORD dwCount,
  108. PSTR * ppStringArray
  109. )
  110. {
  111. DWORD i;
  112. if ( ppStringArray ) {
  113. for(i = 0; i < dwCount; i++)
  114. {
  115. if (ppStringArray[i]) {
  116. LwNetFreeString(ppStringArray[i]);
  117. }
  118. }
  119. LwNetFreeMemory(ppStringArray);
  120. }
  121. return;
  122. }
  123. VOID
  124. LwNetFreeNullTerminatedStringArray(
  125. PSTR * ppStringArray
  126. )
  127. {
  128. PSTR* ppTmp = ppStringArray;
  129. while (ppTmp && *ppTmp) {
  130. LwNetFreeString(*ppTmp);
  131. ppTmp++;
  132. }
  133. LwNetFreeMemory(ppStringArray);
  134. }
  135. DWORD
  136. LwNetWC16StringAllocateFromCString(
  137. OUT PWSTR* ppszNewString,
  138. IN PCSTR pszOriginalString
  139. )
  140. {
  141. return LwNtStatusToWin32Error(
  142. LwRtlWC16StringAllocateFromCString(
  143. ppszNewString,
  144. pszOriginalString)
  145. );
  146. }
  147. VOID
  148. LwNetWC16StringFree(
  149. PWSTR pwszString
  150. )
  151. {
  152. LTNET_SAFE_FREE_MEMORY(pwszString);
  153. }
  154. VOID
  155. LwNetFreeWC16StringArray(
  156. DWORD dwCount,
  157. PWSTR* ppwszArray
  158. )
  159. {
  160. DWORD dwIndex = 0;
  161. if (ppwszArray)
  162. {
  163. for (dwIndex = 0; dwIndex < dwCount; dwIndex++)
  164. {
  165. LwNetWC16StringFree(ppwszArray[dwIndex]);
  166. }
  167. LTNET_SAFE_FREE_MEMORY(ppwszArray);
  168. }
  169. }
  170. DWORD
  171. LwNetAllocateSidFromCString(
  172. OUT PSID* Sid,
  173. IN PCSTR StringSid
  174. )
  175. {
  176. return LwNtStatusToWin32Error(
  177. RtlAllocateSidFromCString(
  178. Sid,
  179. StringSid
  180. )
  181. );
  182. }