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.

125 lines
2.8 KiB

  1. /*++
  2. Copyright (c) 1992, 1993 Microsoft Corporation
  3. Module Name:
  4. packstr.c
  5. Abstract:
  6. Contains functions for packing strings into buffers that also contain
  7. structures.
  8. Author:
  9. From LAN Manager netlib.
  10. Rita Wong (ritaw) 2-Mar-1993
  11. Environment:
  12. User Mode - Win32
  13. Revision History:
  14. --*/
  15. #include <procs.h>
  16. BOOL
  17. NwlibCopyStringToBuffer(
  18. IN LPCWSTR SourceString OPTIONAL,
  19. IN DWORD CharacterCount,
  20. IN LPCWSTR FixedDataEnd,
  21. IN OUT LPWSTR *EndOfVariableData,
  22. OUT LPWSTR *VariableDataPointer
  23. )
  24. /*++
  25. Routine Description:
  26. This routine puts a single variable-length string into an output buffer.
  27. The string is not written if it would overwrite the last fixed structure
  28. in the buffer.
  29. Arguments:
  30. SourceString - Supplies a pointer to the source string to copy into the
  31. output buffer. If SourceString is null then a pointer to a zero terminator
  32. is inserted into output buffer.
  33. CharacterCount - Supplies the length of SourceString, not including zero
  34. terminator. (This in units of characters - not bytes).
  35. FixedDataEnd - Supplies a pointer to just after the end of the last
  36. fixed structure in the buffer.
  37. EndOfVariableData - Supplies an address to a pointer to just after the
  38. last position in the output buffer that variable data can occupy.
  39. Returns a pointer to the string written in the output buffer.
  40. VariableDataPointer - Supplies a pointer to the place in the fixed
  41. portion of the output buffer where a pointer to the variable data
  42. should be written.
  43. Return Value:
  44. Returns TRUE if string fits into output buffer, FALSE otherwise.
  45. --*/
  46. {
  47. DWORD CharsNeeded = (CharacterCount + 1);
  48. //
  49. // Determine if source string will fit, allowing for a zero terminator.
  50. // If not, just set the pointer to NULL.
  51. //
  52. if ((*EndOfVariableData - CharsNeeded) >= FixedDataEnd) {
  53. //
  54. // It fits. Move EndOfVariableData pointer up to the location where
  55. // we will write the string.
  56. //
  57. *EndOfVariableData -= CharsNeeded;
  58. //
  59. // Copy the string to the buffer if it is not null.
  60. //
  61. if (CharacterCount > 0 && SourceString != NULL) {
  62. (VOID) wcsncpy(*EndOfVariableData, SourceString, CharacterCount);
  63. }
  64. //
  65. // Set the zero terminator.
  66. //
  67. *(*EndOfVariableData + CharacterCount) = L'\0';
  68. //
  69. // Set up the pointer in the fixed data portion to point to where the
  70. // string is written.
  71. //
  72. *VariableDataPointer = *EndOfVariableData;
  73. return TRUE;
  74. }
  75. else {
  76. //
  77. // It doesn't fit. Set the offset to NULL.
  78. //
  79. *VariableDataPointer = NULL;
  80. return FALSE;
  81. }
  82. }