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.

211 lines
5.1 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. crtlib.c
  5. Abstract:
  6. This module has support some C run Time functions which are not
  7. supported in KM.
  8. Environment:
  9. Win32 subsystem, Unidrv driver
  10. Revision History:
  11. 01/03/97 -ganeshp-
  12. Created it.
  13. dd-mm-yy -author-
  14. description
  15. --*/
  16. #include "precomp.h"
  17. /*++
  18. Routine Name:
  19. iDrvPrintfSafeA
  20. Routine Description:
  21. This is a safer version of sprintf/iDrvPrintfA.
  22. It utilizes the StringCchPrintf in the
  23. strsafe.h library. sprintf returns the number of characters copied to the
  24. destination string, but StringCchPrintf doesn't.
  25. Note: May not compile/work if WINNT_40 switch turned on. But since
  26. we are not supporting NT4 anymore, we should be ok.
  27. Arguments:
  28. pszDest : Destination string.
  29. cchDest : Number of characters in the destination string. Since this is the ANSI version
  30. number of chars = num of bytes.
  31. pszFormat : e.g. "PP%d,%d"
  32. ... : The variable list of arguments
  33. Return Value:
  34. Negative : if some error encountered.
  35. Positive : Number of characters copied.
  36. 0 : i.e. no character copied.
  37. Author:
  38. -hsingh- 2/21/2002
  39. Revision History:
  40. -hsingh- 2/21/2002 Created this function.
  41. --*/
  42. INT iDrvPrintfSafeA (
  43. IN PCHAR pszDest,
  44. IN CONST ULONG cchDest,
  45. IN CONST PCHAR pszFormat,
  46. IN ...)
  47. {
  48. va_list args;
  49. INT icchWritten = (INT)-1;
  50. va_start(args, pszFormat);
  51. icchWritten = iDrvVPrintfSafeA ( pszDest, cchDest, pszFormat, args);
  52. va_end(args);
  53. return icchWritten;
  54. }
  55. /*++
  56. Routine Name:
  57. iDrvPrintfSafeW
  58. Routine Description:
  59. This is a safer version of sprintf/iDrvPrintfW.
  60. It utilizes the StringCchPrintf in the
  61. strsafe.h library. sprintf returns the number of characters copied to the
  62. destination string, but StringCchPrintf doesn't.
  63. Note: May not compile/work if WINNT_40 switch turned on. But since
  64. we are not supporting NT4 anymore, we should be ok.
  65. Arguments:
  66. pszDest : Destination string.
  67. cchDest : Number of characters in the destination string. Since this is the UNICODE version
  68. the size of buffer is twice the number of characters.
  69. pszFormat : e.g. "PP%d,%d"
  70. ... : The variable list of arguments
  71. Return Value:
  72. Negative : if some error encountered.
  73. Positive : Number of characters copied.
  74. 0 : i.e. no character copied.
  75. Author:
  76. -hsingh- 2/21/2002
  77. Revision History:
  78. -hsingh- 2/21/2002 Created this function.
  79. --*/
  80. INT iDrvPrintfSafeW (
  81. IN PWCHAR pszDest,
  82. IN CONST ULONG cchDest,
  83. IN CONST PWCHAR pszFormat,
  84. IN ...)
  85. {
  86. va_list args;
  87. INT icchWritten = (INT)-1;
  88. va_start(args, pszFormat);
  89. icchWritten = iDrvVPrintfSafeW (pszDest, cchDest, pszFormat, args);
  90. va_end(args);
  91. return icchWritten;
  92. }
  93. INT iDrvVPrintfSafeA (
  94. IN PCHAR pszDest,
  95. IN CONST ULONG cchDest,
  96. IN CONST PCHAR pszFormat,
  97. IN va_list arglist)
  98. {
  99. HRESULT hr = S_FALSE;
  100. INT icchWritten = (INT)-1;
  101. size_t cchRemaining = cchDest;
  102. //
  103. // Since return value is a signed integer, but cchDest is unsigned.
  104. // cchDest can be atmost MAX_ULONG but return can be atmost MAX_LONG.
  105. // So make sure that input buffer is not more than MAX_LONG (LONG is
  106. // virtually same as INT)
  107. //
  108. if ( cchDest > (ULONG) MAX_LONG )
  109. {
  110. return icchWritten;
  111. }
  112. hr = StringCchVPrintfExA (pszDest, cchDest, NULL, &cchRemaining, 0, pszFormat, arglist);
  113. if (SUCCEEDED (hr) )
  114. {
  115. //
  116. // Subtracting the number of characters remaining in the string
  117. // from the number of characters originally present give us the number
  118. // of characters written.
  119. //
  120. icchWritten = (INT)(cchDest - cchRemaining);
  121. }
  122. return icchWritten;
  123. }
  124. INT iDrvVPrintfSafeW (
  125. IN PWCHAR pszDest,
  126. IN CONST ULONG cchDest,
  127. IN CONST PWCHAR pszFormat,
  128. IN va_list arglist)
  129. {
  130. HRESULT hr = S_FALSE;
  131. INT icchWritten = (INT)-1;
  132. size_t cchRemaining = cchDest;
  133. //
  134. // Since return value is a signed integer, but cchDest is unsigned.
  135. // cchDest can be atmost MAX_ULONG but return can be atmost MAX_LONG.
  136. // So make sure that input buffer is not more than MAX_LONG (LONG is
  137. // virtually same as INT)
  138. //
  139. if ( cchDest > (ULONG) MAX_LONG )
  140. {
  141. return icchWritten;
  142. }
  143. hr = StringCchVPrintfExW (pszDest, cchDest, NULL, &cchRemaining, 0, pszFormat, arglist);
  144. if (SUCCEEDED (hr) )
  145. {
  146. //
  147. // Subtracting the number of characters remaining in the string
  148. // from the number of characters originally present give us the number
  149. // of characters written.
  150. //
  151. icchWritten = (INT)(cchDest - cchRemaining);
  152. }
  153. return icchWritten;
  154. }