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.

268 lines
7.3 KiB

  1. #include "precomp.hxx"
  2. #include <wchar.h> // wcsncpy
  3. #include <tstring.h>
  4. BOOL
  5. NetpCopyStringToBufferW (
  6. IN LPWSTR String OPTIONAL,
  7. IN DWORD CharacterCount,
  8. IN LPWSTR FixedDataEnd,
  9. IN OUT LPWSTR *EndOfVariableData,
  10. OUT LPWSTR *VariableDataPointer
  11. )
  12. /*++
  13. Routine Description:
  14. This routine puts a single variable-length string into an output buffer.
  15. The string is not written if it would overwrite the last fixed structure
  16. in the buffer.
  17. The code is swiped from svcsupp.c written by DavidTr.
  18. Sample usage:
  19. LPBYTE FixedDataEnd = OutputBuffer + sizeof(WKSTA_INFO_202);
  20. LPTSTR EndOfVariableData = OutputBuffer + OutputBufferSize;
  21. //
  22. // Copy user name
  23. //
  24. NetpCopyStringToBuffer(
  25. UserInfo->UserName.Buffer;
  26. UserInfo->UserName.Length;
  27. FixedDataEnd,
  28. &EndOfVariableData,
  29. &WkstaInfo->wki202_username
  30. );
  31. Arguments:
  32. String - Supplies a pointer to the source string to copy into the
  33. output buffer. If String is null then a pointer to a zero terminator
  34. is inserted into output buffer.
  35. CharacterCount - Supplies the length of String, not including zero
  36. terminator. (This in units of characters - not bytes).
  37. FixedDataEnd - Supplies a pointer to just after the end of the last
  38. fixed structure in the buffer.
  39. EndOfVariableData - Supplies an address to a pointer to just after the
  40. last position in the output buffer that variable data can occupy.
  41. Returns a pointer to the string written in the output buffer.
  42. VariableDataPointer - Supplies a pointer to the place in the fixed
  43. portion of the output buffer where a pointer to the variable data
  44. should be written.
  45. Return Value:
  46. Returns TRUE if string fits into output buffer, FALSE otherwise.
  47. --*/
  48. {
  49. // The following line has been removed. Since we are manipulating
  50. // either LPSTR or LPWSTR, the pointer arithmetic automatically deals with
  51. // either bytes or words. It is therefore only proper to deal in terms
  52. // of characters needed. Not Bytes needed. Therefore, the symbol has
  53. // been changed from BytesNeeded to CharsNeeded.
  54. //
  55. // DWORD BytesNeeded = (CharacterCount + 1) * sizeof(WCHAR);
  56. //
  57. //
  58. DWORD CharsNeeded = (CharacterCount + 1);
  59. #ifdef remove
  60. DbgPrint("NetpStringToBuffer: String at " FORMAT_POINTER
  61. ", CharacterCount=" FORMAT_DWORD
  62. ",\n FixedDataEnd at " FORMAT_POINTER
  63. ", *EndOfVariableData at " FORMAT_POINTER
  64. ",\n VariableDataPointer at " FORMAT_POINTER
  65. ", CharsNeeded=" FORMAT_DWORD ".\n",
  66. String, CharacterCount, FixedDataEnd, *EndOfVariableData,
  67. VariableDataPointer, CharsNeeded);
  68. #endif
  69. //
  70. // Determine if string will fit, allowing for a zero terminator. If no,
  71. // just set the pointer to NULL.
  72. //
  73. if ((*EndOfVariableData - CharsNeeded) >= FixedDataEnd) {
  74. //
  75. // It fits. Move EndOfVariableData pointer up to the location where
  76. // we will write the string.
  77. //
  78. *EndOfVariableData -= CharsNeeded;
  79. //
  80. // Copy the string to the buffer if it is not null.
  81. //
  82. if (CharacterCount > 0 && String != NULL) {
  83. wcsncpy(*EndOfVariableData, String, CharacterCount);
  84. }
  85. //
  86. // Set the zero terminator.
  87. //
  88. *(*EndOfVariableData + CharacterCount) = (WCHAR) '\0';
  89. //
  90. // Set up the pointer in the fixed data portion to point to where the
  91. // string is written.
  92. //
  93. *VariableDataPointer = *EndOfVariableData;
  94. return TRUE;
  95. }
  96. else {
  97. //
  98. // It doesn't fit. Set the offset to NULL.
  99. //
  100. *VariableDataPointer = NULL;
  101. return FALSE;
  102. }
  103. } // NetpCopyStringToBuffer
  104. BOOL
  105. NetpCopyStringToBufferA (
  106. IN LPTSTR String OPTIONAL,
  107. IN DWORD CharacterCount,
  108. IN LPBYTE FixedDataEnd,
  109. IN OUT LPTSTR *EndOfVariableData,
  110. OUT LPTSTR *VariableDataPointer
  111. )
  112. /*++
  113. Routine Description:
  114. This routine puts a single variable-length string into an output buffer.
  115. The string is not written if it would overwrite the last fixed structure
  116. in the buffer.
  117. The code is swiped from svcsupp.c written by DavidTr.
  118. Sample usage:
  119. LPBYTE FixedDataEnd = OutputBuffer + sizeof(WKSTA_INFO_202);
  120. LPTSTR EndOfVariableData = OutputBuffer + OutputBufferSize;
  121. //
  122. // Copy user name
  123. //
  124. NetpCopyStringToBuffer(
  125. UserInfo->UserName.Buffer;
  126. UserInfo->UserName.Length;
  127. FixedDataEnd,
  128. &EndOfVariableData,
  129. &WkstaInfo->wki202_username
  130. );
  131. Arguments:
  132. String - Supplies a pointer to the source string to copy into the
  133. output buffer. If String is null then a pointer to a zero terminator
  134. is inserted into output buffer.
  135. CharacterCount - Supplies the length of String, not including zero
  136. terminator.
  137. FixedDataEnd - Supplies a pointer to just after the end of the last
  138. fixed structure in the buffer.
  139. EndOfVariableData - Supplies an address to a pointer to just after the
  140. last position in the output buffer that variable data can occupy.
  141. Returns a pointer to the string written in the output buffer.
  142. VariableDataPointer - Supplies a pointer to the place in the fixed
  143. portion of the output buffer where a pointer to the variable data
  144. should be written.
  145. Return Value:
  146. Returns TRUE if string fits into output buffer, FALSE otherwise.
  147. --*/
  148. {
  149. DWORD BytesNeeded = (CharacterCount + 1) * sizeof(TCHAR);
  150. #ifdef remove
  151. DbgPrint("NetpStringToBuffer: String at " FORMAT_POINTER
  152. ", CharacterCount=" FORMAT_DWORD
  153. ",\n FixedDataEnd at " FORMAT_POINTER
  154. ", *EndOfVariableData at " FORMAT_POINTER
  155. ",\n VariableDataPointer at " FORMAT_POINTER
  156. ", BytesNeeded=" FORMAT_DWORD ".\n",
  157. String, CharacterCount, FixedDataEnd, *EndOfVariableData,
  158. VariableDataPointer, BytesNeeded);
  159. #endif
  160. //
  161. // Determine if string will fit, allowing for a zero terminator. If no,
  162. // just set the pointer to NULL.
  163. //
  164. if ((*EndOfVariableData - BytesNeeded) >= (LPTSTR)FixedDataEnd) {
  165. //
  166. // It fits. Move EndOfVariableData pointer up to the location where
  167. // we will write the string.
  168. //
  169. *EndOfVariableData -= BytesNeeded;
  170. //
  171. // Copy the string to the buffer if it is not null.
  172. //
  173. if (CharacterCount > 0 && String != NULL) {
  174. STRNCPY(*EndOfVariableData, String, CharacterCount);
  175. }
  176. //
  177. // Set the zero terminator.
  178. //
  179. *(*EndOfVariableData + CharacterCount) = (TCHAR) '\0';
  180. //
  181. // Set up the pointer in the fixed data portion to point to where the
  182. // string is written.
  183. //
  184. *VariableDataPointer = *EndOfVariableData;
  185. return TRUE;
  186. }
  187. else {
  188. //
  189. // It doesn't fit. Set the offset to NULL.
  190. //
  191. *VariableDataPointer = NULL;
  192. return FALSE;
  193. }
  194. } // NetpCopyStringToBufferA