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.

134 lines
2.4 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. spprintf.c
  5. Abstract:
  6. safer sprintf variants
  7. Author:
  8. Jay Krell (a-JayK) November 2000
  9. Revision History:
  10. --*/
  11. #include "spprecmp.h"
  12. #pragma hdrstop
  13. #include <stdarg.h>
  14. #include <stdio.h>
  15. #include "spprintf.h"
  16. #include "spcab.h"
  17. //
  18. // _snprintf and co. do not write a terminal nul when the string just fits.
  19. // These function do.
  20. //
  21. void
  22. SpFormatStringVaA(
  23. PSTR Buffer,
  24. SIZE_T Size,
  25. PCSTR Format,
  26. va_list Args
  27. )
  28. {
  29. if (Buffer != NULL && Size != 0)
  30. {
  31. Buffer[0] = 0;
  32. Size -= 1;
  33. if (Size != 0)
  34. _vsnprintf(Buffer, Size, Format, Args);
  35. Buffer[Size] = 0;
  36. }
  37. }
  38. void
  39. __cdecl
  40. SpFormatStringA(
  41. PSTR Buffer,
  42. SIZE_T Size,
  43. PCSTR Format,
  44. ...
  45. )
  46. {
  47. va_list Args;
  48. va_start(Args, Format);
  49. SpFormatStringVaA(Buffer, Size, Format, Args);
  50. va_end(Args);
  51. }
  52. void
  53. SpFormatStringVaW(
  54. PWSTR Buffer,
  55. SIZE_T Size,
  56. PCWSTR Format,
  57. va_list Args
  58. )
  59. {
  60. if (Buffer != NULL && Size != 0)
  61. {
  62. Buffer[0] = 0;
  63. Size -= 1;
  64. if (Size != 0)
  65. _vsnwprintf(Buffer, Size, Format, Args);
  66. Buffer[Size] = 0;
  67. }
  68. }
  69. void
  70. __cdecl
  71. SpFormatStringW(
  72. PWSTR Buffer,
  73. SIZE_T Size,
  74. PCWSTR Format,
  75. ...
  76. )
  77. {
  78. va_list Args;
  79. va_start(Args, Format);
  80. SpFormatStringVaW(Buffer, Size, Format, Args);
  81. va_end(Args);
  82. }
  83. NTSTATUS
  84. __cdecl
  85. SpFormatStringWToA(
  86. PSTR Buffer,
  87. SIZE_T Size,
  88. PCWSTR Format,
  89. ...
  90. )
  91. {
  92. va_list Args;
  93. UNICODE_STRING UnicodeBuffer = { 0 };
  94. ANSI_STRING AnsiBuffer = { 0 };
  95. NTSTATUS Status = STATUS_SUCCESS;
  96. va_start(Args, Format);
  97. UnicodeBuffer.Buffer = (PWSTR)SpMemAlloc(Size * sizeof(UnicodeBuffer.Buffer[0]));
  98. if (UnicodeBuffer.Buffer == NULL) {
  99. Status = STATUS_NO_MEMORY;
  100. goto Exit;
  101. }
  102. if (Size != 0) {
  103. UnicodeBuffer.Buffer[0] = 0;
  104. }
  105. SpFormatStringVaW(UnicodeBuffer.Buffer, Size, Format, Args);
  106. UnicodeBuffer.Length = (USHORT)(wcslen(UnicodeBuffer.Buffer) + 1) * sizeof(UnicodeBuffer.Buffer[0]);
  107. AnsiBuffer.MaximumLength = (USHORT)Size * sizeof(AnsiBuffer.Buffer[0]);
  108. Status = SpUnicodeStringToAnsiString(&AnsiBuffer, &UnicodeBuffer, FALSE);
  109. Exit:
  110. va_end(Args);
  111. return Status;
  112. }