Leaked source code of windows server 2003
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.

169 lines
3.3 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. memory.c
  5. Abstract:
  6. This module provides all the memory management functions for all spooler
  7. components
  8. Author:
  9. Krishna Ganugapati (KrishnaG) 03-Feb-1994
  10. Revision History:
  11. --*/
  12. #define UNICODE
  13. #define _UNICODE
  14. #include "dswarn.h"
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <windows.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <tchar.h>
  22. #include <wchar.h>
  23. #include <winldap.h>
  24. #include <adserr.h>
  25. #include "memory.h"
  26. #define MAPHEXTODIGIT(x) ( x >= '0' && x <= '9' ? (x-'0') : \
  27. x >= 'A' && x <= 'F' ? (x-'A'+10) : \
  28. x >= 'a' && x <= 'f' ? (x-'a'+10) : 0 )
  29. HRESULT
  30. ADsEncodeBinaryData (
  31. PBYTE pbSrcData,
  32. DWORD dwSrcLen,
  33. LPWSTR * ppszDestData
  34. )
  35. {
  36. LPWSTR pszDest = NULL;
  37. DWORD dwDestLen, dwDestSize = 0;
  38. WCHAR wch;
  39. if (!ppszDestData || (!pbSrcData && dwSrcLen))
  40. return (E_ADS_BAD_PARAMETER);
  41. *ppszDestData = NULL;
  42. //
  43. // figure out how long of a buffer we need.
  44. //
  45. dwDestLen = ldap_escape_filter_element (
  46. (char *) pbSrcData,
  47. dwSrcLen,
  48. NULL,
  49. 0
  50. );
  51. if (dwDestLen == 0) {
  52. return S_OK;
  53. }
  54. dwDestSize = dwDestLen * sizeof (WCHAR);
  55. pszDest = (LPWSTR) AllocADsMem( dwDestSize );
  56. if (pszDest == NULL)
  57. return (E_OUTOFMEMORY );
  58. dwDestLen = ldap_escape_filter_element (
  59. (char *) pbSrcData,
  60. dwSrcLen,
  61. pszDest,
  62. dwDestSize
  63. );
  64. if(dwDestLen)
  65. {
  66. if(pszDest)
  67. {
  68. FreeADsMem(pszDest);
  69. }
  70. return (E_FAIL);
  71. }
  72. *ppszDestData = pszDest;
  73. return (S_OK);
  74. }
  75. HRESULT
  76. ADsDecodeBinaryData (
  77. LPWSTR szSrcData,
  78. PBYTE *ppbDestData,
  79. ULONG *pdwDestLen
  80. )
  81. {
  82. HRESULT hr = S_OK;
  83. ULONG dwDestLen = 0;
  84. LPWSTR szSrc = NULL;
  85. PBYTE pbDestData = NULL;
  86. PBYTE pbDestDataCurrent = NULL;
  87. WCHAR ch = 0;
  88. if (szSrcData == NULL) {
  89. return E_FAIL;
  90. }
  91. //
  92. // Counting length of output binary string
  93. //
  94. szSrc = szSrcData;
  95. while (*szSrc != L'\0') {
  96. ch = *(szSrc++);
  97. if (ch == L'\\') {
  98. szSrc = szSrc + 2;
  99. }
  100. dwDestLen++;
  101. }
  102. //
  103. // Allocating return binary string
  104. //
  105. pbDestData = (PBYTE) AllocADsMem(dwDestLen);
  106. if (pbDestData == NULL) {
  107. hr = E_OUTOFMEMORY;
  108. return (hr);
  109. }
  110. //
  111. // Decoding String
  112. //
  113. szSrc = szSrcData;
  114. pbDestDataCurrent = pbDestData;
  115. while (*szSrc != L'\0') {
  116. ch = *szSrc ++;
  117. if (ch == L'\\') {
  118. *(pbDestDataCurrent++) = MAPHEXTODIGIT( *szSrc ) * 16 +
  119. MAPHEXTODIGIT( *(szSrc+1) );
  120. szSrc+=2;
  121. }
  122. else {
  123. *(pbDestDataCurrent++) = (BYTE)ch;
  124. }
  125. }
  126. *ppbDestData = pbDestData;
  127. *pdwDestLen = dwDestLen;
  128. return hr;
  129. }