Source code of Windows XP (NT5)
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.

161 lines
2.9 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. ldap_escape_filter_element (
  59. (char *) pbSrcData,
  60. dwSrcLen,
  61. pszDest,
  62. dwDestSize
  63. );
  64. *ppszDestData = pszDest;
  65. return (S_OK);
  66. }
  67. HRESULT
  68. ADsDecodeBinaryData (
  69. LPWSTR szSrcData,
  70. PBYTE *ppbDestData,
  71. ULONG *pdwDestLen
  72. )
  73. {
  74. HRESULT hr = S_OK;
  75. ULONG dwDestLen = 0;
  76. LPWSTR szSrc = NULL;
  77. PBYTE pbDestData = NULL;
  78. PBYTE pbDestDataCurrent = NULL;
  79. WCHAR ch = 0;
  80. if (szSrcData == NULL) {
  81. return E_FAIL;
  82. }
  83. //
  84. // Counting length of output binary string
  85. //
  86. szSrc = szSrcData;
  87. while (*szSrc != L'\0') {
  88. ch = *(szSrc++);
  89. if (ch == L'\\') {
  90. szSrc = szSrc + 2;
  91. }
  92. dwDestLen++;
  93. }
  94. //
  95. // Allocating return binary string
  96. //
  97. pbDestData = (PBYTE) AllocADsMem(dwDestLen);
  98. if (pbDestData == NULL) {
  99. hr = E_OUTOFMEMORY;
  100. return (hr);
  101. }
  102. //
  103. // Decoding String
  104. //
  105. szSrc = szSrcData;
  106. pbDestDataCurrent = pbDestData;
  107. while (*szSrc != L'\0') {
  108. ch = *szSrc ++;
  109. if (ch == L'\\') {
  110. *(pbDestDataCurrent++) = MAPHEXTODIGIT( *szSrc ) * 16 +
  111. MAPHEXTODIGIT( *(szSrc+1) );
  112. szSrc+=2;
  113. }
  114. else {
  115. *(pbDestDataCurrent++) = (BYTE)ch;
  116. }
  117. }
  118. *ppbDestData = pbDestData;
  119. *pdwDestLen = dwDestLen;
  120. return hr;
  121. }