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.

139 lines
4.0 KiB

  1. #include "precomp.hxx"
  2. #include <wchar.h> // wcsncpy
  3. BOOL
  4. NetpCopyStringToBuffer (
  5. IN LPWSTR String OPTIONAL,
  6. IN DWORD CharacterCount,
  7. IN LPWSTR FixedDataEnd,
  8. IN OUT LPWSTR *EndOfVariableData,
  9. OUT LPWSTR *VariableDataPointer
  10. )
  11. /*++
  12. Routine Description:
  13. This routine puts a single variable-length string into an output buffer.
  14. The string is not written if it would overwrite the last fixed structure
  15. in the buffer.
  16. The code is swiped from svcsupp.c written by DavidTr.
  17. Sample usage:
  18. LPBYTE FixedDataEnd = OutputBuffer + sizeof(WKSTA_INFO_202);
  19. LPTSTR EndOfVariableData = OutputBuffer + OutputBufferSize;
  20. //
  21. // Copy user name
  22. //
  23. NetpCopyStringToBuffer(
  24. UserInfo->UserName.Buffer;
  25. UserInfo->UserName.Length;
  26. FixedDataEnd,
  27. &EndOfVariableData,
  28. &WkstaInfo->wki202_username
  29. );
  30. Arguments:
  31. String - Supplies a pointer to the source string to copy into the
  32. output buffer. If String is null then a pointer to a zero terminator
  33. is inserted into output buffer.
  34. CharacterCount - Supplies the length of String, not including zero
  35. terminator. (This in units of characters - not bytes).
  36. FixedDataEnd - Supplies a pointer to just after the end of the last
  37. fixed structure in the buffer.
  38. EndOfVariableData - Supplies an address to a pointer to just after the
  39. last position in the output buffer that variable data can occupy.
  40. Returns a pointer to the string written in the output buffer.
  41. VariableDataPointer - Supplies a pointer to the place in the fixed
  42. portion of the output buffer where a pointer to the variable data
  43. should be written.
  44. Return Value:
  45. Returns TRUE if string fits into output buffer, FALSE otherwise.
  46. --*/
  47. {
  48. // The following line has been removed. Since we are manipulating
  49. // either LPSTR or LPWSTR, the pointer arithmetic automatically deals with
  50. // either bytes or words. It is therefore only proper to deal in terms
  51. // of characters needed. Not Bytes needed. Therefore, the symbol has
  52. // been changed from BytesNeeded to CharsNeeded.
  53. //
  54. // DWORD BytesNeeded = (CharacterCount + 1) * sizeof(WCHAR);
  55. //
  56. //
  57. DWORD CharsNeeded = (CharacterCount + 1);
  58. #ifdef remove
  59. DbgPrint("NetpStringToBuffer: String at " FORMAT_POINTER
  60. ", CharacterCount=" FORMAT_DWORD
  61. ",\n FixedDataEnd at " FORMAT_POINTER
  62. ", *EndOfVariableData at " FORMAT_POINTER
  63. ",\n VariableDataPointer at " FORMAT_POINTER
  64. ", CharsNeeded=" FORMAT_DWORD ".\n",
  65. String, CharacterCount, FixedDataEnd, *EndOfVariableData,
  66. VariableDataPointer, CharsNeeded);
  67. #endif
  68. //
  69. // Determine if string will fit, allowing for a zero terminator. If no,
  70. // just set the pointer to NULL.
  71. //
  72. if ((*EndOfVariableData - CharsNeeded) >= FixedDataEnd) {
  73. //
  74. // It fits. Move EndOfVariableData pointer up to the location where
  75. // we will write the string.
  76. //
  77. *EndOfVariableData -= CharsNeeded;
  78. //
  79. // Copy the string to the buffer if it is not null.
  80. //
  81. if (CharacterCount > 0 && String != NULL) {
  82. wcsncpy(*EndOfVariableData, String, CharacterCount);
  83. }
  84. //
  85. // Set the zero terminator.
  86. //
  87. *(*EndOfVariableData + CharacterCount) = (WCHAR) '\0';
  88. //
  89. // Set up the pointer in the fixed data portion to point to where the
  90. // string is written.
  91. //
  92. *VariableDataPointer = *EndOfVariableData;
  93. return TRUE;
  94. }
  95. else {
  96. //
  97. // It doesn't fit. Set the offset to NULL.
  98. //
  99. *VariableDataPointer = NULL;
  100. return FALSE;
  101. }
  102. } // NetpCopyStringToBuffer