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.

93 lines
1.8 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. encode.cxx
  5. Abstract:
  6. This module provides encoing functions to convert a binary blob of data so
  7. that it can be embedded in a search filter.
  8. Author:
  9. Shankara Shastry (ShankSh) 12-Mar-1997
  10. Revision History:
  11. --*/
  12. #include "procs.hxx"
  13. #pragma hdrstop
  14. #include "oleds.hxx"
  15. WCHAR HexToWCharTable[17] = L"0123456789ABCDEF";
  16. //
  17. // Helper function to support allowing opaque blobs of data in search filters.
  18. // This API takes any filter element and adds necessary escape characters
  19. // such that when the element hits the wire in the search request, it will
  20. // be equal to the opaque blob past in here as source.
  21. //
  22. //
  23. // The destination string ppszDestData needs to be freed using FreeAdsMem after
  24. // using it.
  25. //
  26. HRESULT
  27. ADsEncodeBinaryData (
  28. PBYTE pbSrcData,
  29. DWORD dwSrcLen,
  30. LPWSTR * ppszDestData
  31. )
  32. {
  33. LPWSTR pszDest = NULL, pszDestPtr = NULL;
  34. DWORD lengthRequired = 0;
  35. DWORD dwSrcCount = 0;
  36. WCHAR wch;
  37. if (!ppszDestData || (!pbSrcData && dwSrcLen))
  38. RRETURN(E_ADS_BAD_PARAMETER);
  39. *ppszDestData = NULL;
  40. //
  41. // figure out how long of a buffer we need.
  42. //
  43. lengthRequired = ((dwSrcLen * 2) + 1) * sizeof(WCHAR);
  44. pszDest = (LPWSTR) AllocADsMem( lengthRequired );
  45. if (pszDest == NULL)
  46. RRETURN (E_OUTOFMEMORY );
  47. pszDestPtr = pszDest;
  48. //
  49. // For each byte in source string, copy it to dest string but we first
  50. // expand it out such that each nibble is it's own character (UNICODE)
  51. //
  52. while (++dwSrcCount <= dwSrcLen) {
  53. wch = ( (*pbSrcData) & 0xF0 ) >> 4;
  54. *(pszDestPtr++) = HexToWCharTable[wch];
  55. wch = (*(pbSrcData++)) & 0x0F;
  56. *(pszDestPtr++) = HexToWCharTable[wch];
  57. }
  58. *pszDestPtr = '\0';
  59. *ppszDestData = pszDest;
  60. RRETURN (S_OK);
  61. }