Advertisement
FlyFar

DoubleAgent/Path.c

Jan 2nd, 2024
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 KB | Cybersecurity | 0 0
  1. /* Includes ******************************************************************/
  2. #include <Windows.h>
  3. #include <Shlwapi.h>
  4. #include "Status.h"
  5. #include "Path.h"
  6.  
  7. /* Public Function Definitions ***********************************************/
  8. DOUBLEAGENT_STATUS PATH_GetDirectory(IN PCWSTR pcwszFilePath, OUT PWSTR *ppwszDirPath)
  9. {
  10.     DOUBLEAGENT_STATUS eStatus = DOUBLEAGENT_STATUS_INVALID_VALUE;
  11.     PWSTR pwszDirPathLocal = NULL;
  12.     SIZE_T nFilePathLenInBytes = 0;
  13.  
  14.     /* Validates the parameters */
  15.     if ((NULL == pcwszFilePath) || (NULL == ppwszDirPath))
  16.     {
  17.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_PATH_GETDIRECTORY_INVALID_PARAMS);
  18.         goto lbl_cleanup;
  19.     }
  20.  
  21.     /* Gets the file path length in bytes */
  22.     nFilePathLenInBytes = (wcslen(pcwszFilePath) + 1) * sizeof(*pcwszFilePath);
  23.  
  24.     /* Allocates the directory path */
  25.     pwszDirPathLocal = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, nFilePathLenInBytes);
  26.     if (NULL == pwszDirPathLocal)
  27.     {
  28.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_PATH_GETDIRECTORY_HEAPALLOC_FAILED);
  29.         goto lbl_cleanup;
  30.     }
  31.  
  32.     /* Copies the file path to the directory path */
  33.     CopyMemory(pwszDirPathLocal, pcwszFilePath, nFilePathLenInBytes);
  34.  
  35.     /* Creates the directory path by removing the file name */
  36.     if (FALSE == PathRemoveFileSpecW(pwszDirPathLocal))
  37.     {
  38.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_PATH_GETDIRECTORY_PATHREMOVEFILESPECW_FAILED);
  39.         goto lbl_cleanup;
  40.     }
  41.  
  42.     /* Sets the received parameters */
  43.     *ppwszDirPath = pwszDirPathLocal;
  44.     pwszDirPathLocal = NULL;
  45.  
  46.     /* Succeeded */
  47.     DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_SUCCESS);
  48.  
  49. lbl_cleanup:
  50.     /* Frees the directory path */
  51.     if (NULL != pwszDirPathLocal)
  52.     {
  53.         (VOID)HeapFree(GetProcessHeap(), 0, pwszDirPathLocal);
  54.         pwszDirPathLocal = NULL;
  55.     }
  56.  
  57.     /* Returns status */
  58.     return eStatus;
  59. }
  60.  
  61. DOUBLEAGENT_STATUS PATH_Combine(IN PCWSTR pcwszPath1, IN PCWSTR pcwszPath2, OUT PWSTR *ppwszCombined)
  62. {
  63.     DOUBLEAGENT_STATUS eStatus = DOUBLEAGENT_STATUS_INVALID_VALUE;
  64.     PWSTR pwszCombinedLocal = NULL;
  65.  
  66.     /* Validates the parameters */
  67.     if ((NULL == pcwszPath1) || (NULL == pcwszPath2) || (NULL == ppwszCombined))
  68.     {
  69.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_PATH_COMBINE_INVALID_PARAMS);
  70.         goto lbl_cleanup;
  71.     }
  72.  
  73.     /* Allocates the combined path */
  74.     pwszCombinedLocal = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, MAX_PATH);
  75.     if (NULL == pwszCombinedLocal)
  76.     {
  77.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_PATH_COMBINE_HEAPALLOC_FAILED);
  78.         goto lbl_cleanup;
  79.     }
  80.  
  81.     /* Combines the paths */
  82.     if (NULL == PathCombineW(pwszCombinedLocal, pcwszPath1, pcwszPath2))
  83.     {
  84.         DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_DOUBLEAGENT_PATH_COMBINE_PATHCOMBINEW_FAILED);
  85.         goto lbl_cleanup;
  86.     }
  87.  
  88.     /* Sets the received parameters */
  89.     *ppwszCombined = pwszCombinedLocal;
  90.     pwszCombinedLocal = NULL;
  91.  
  92.     /* Succeeded */
  93.     DOUBLEAGENT_SET(eStatus, DOUBLEAGENT_STATUS_SUCCESS);
  94.  
  95. lbl_cleanup:
  96.     /* Frees the combined path */
  97.     if (NULL != pwszCombinedLocal)
  98.     {
  99.         (VOID)HeapFree(GetProcessHeap(), 0, pwszCombinedLocal);
  100.         pwszCombinedLocal = NULL;
  101.     }
  102.  
  103.     /* Returns status */
  104.     return eStatus;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement