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.

190 lines
4.5 KiB

  1. #include "pch.h"
  2. #include "limits.h"
  3. //Read From Stdin
  4. //Caller responsible for LocalFree(*ppBuffer)
  5. //Return Value:
  6. // Number of WCHAR read if successful
  7. // -1 in case of Failure. Call GetLastError to get the error.
  8. LONG ReadFromIn(OUT LPWSTR *ppBuffer)
  9. {
  10. LPWSTR pBuffer = NULL;
  11. DWORD dwErr = ERROR_SUCCESS;
  12. pBuffer = (LPWSTR)LocalAlloc(LPTR,INIT_SIZE*sizeof(WCHAR));
  13. if(!pBuffer)
  14. {
  15. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  16. return -1;
  17. }
  18. LONG Pos = 0;
  19. LONG MaxSize = INIT_SIZE;
  20. wint_t ch = 0;
  21. if (g_fUnicodeInput)
  22. {
  23. //Security Review: ch is widechar
  24. while(2 == fread(&ch,1,2,stdin))
  25. {
  26. if (0x000D == ch || 0xFEFF == ch) continue;
  27. if(Pos == MaxSize -1 )
  28. {
  29. if(ERROR_SUCCESS != ResizeByTwo(&pBuffer,&MaxSize))
  30. {
  31. LocalFree(pBuffer);
  32. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  33. return -1;
  34. }
  35. }
  36. pBuffer[Pos++] = (WCHAR)ch;
  37. }
  38. }
  39. else
  40. {
  41. while((ch = getwchar()) != WEOF)
  42. {
  43. if(Pos == MaxSize -1 )
  44. {
  45. if(ERROR_SUCCESS != ResizeByTwo(&pBuffer,&MaxSize))
  46. {
  47. LocalFree(pBuffer);
  48. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  49. return -1;
  50. }
  51. }
  52. pBuffer[Pos++] = (WCHAR)ch;
  53. }
  54. }
  55. pBuffer[Pos] = L'\0';
  56. *ppBuffer = pBuffer;
  57. return Pos;
  58. }
  59. //General Utility Functions
  60. DWORD ResizeByTwo( LPTSTR *ppBuffer,
  61. LONG *pSize )
  62. {
  63. if(!ppBuffer || !pSize)
  64. {
  65. return ERROR_INVALID_PARAMETER;
  66. }
  67. LPWSTR pTempBuffer = (LPWSTR)LocalAlloc(LPTR,(*pSize)*2*sizeof(WCHAR));
  68. if(!pTempBuffer)
  69. return ERROR_NOT_ENOUGH_MEMORY;
  70. //Security Review:Correct memory is allocated
  71. memcpy(pTempBuffer,*ppBuffer,*pSize*sizeof(WCHAR));
  72. LocalFree(*ppBuffer);
  73. *ppBuffer = pTempBuffer;
  74. *pSize *=2;
  75. return ERROR_SUCCESS;
  76. }
  77. BOOL StringCopy( LPWSTR *ppDest, LPWSTR pSrc)
  78. {
  79. if(!ppDest || !pSrc)
  80. {
  81. return FALSE;
  82. }
  83. *ppDest = NULL;
  84. if(!pSrc)
  85. return TRUE;
  86. //Security Review:pSrc is null terminated.
  87. *ppDest = (LPWSTR)LocalAlloc(LPTR, (wcslen(pSrc) + 1)*sizeof(WCHAR));
  88. if(!*ppDest)
  89. return FALSE;
  90. //Security Review:Buffer is correctly allocated
  91. wcscpy(*ppDest,pSrc);
  92. return TRUE;
  93. }
  94. //+----------------------------------------------------------------------------
  95. // Function: ConvertStringToInterger
  96. // Synopsis: Converts string to integer. Returns false if string is outside
  97. // the range on an integer
  98. // Arguments:pszInput: integer in string format
  99. // :pIntOutput:takes converted integer
  100. //// Returns:TRUE is successful.
  101. //-----------------------------------------------------------------------------
  102. BOOL ConvertStringToInterger(LPWSTR pszInput, int* pIntOutput)
  103. {
  104. if(!pIntOutput || !pszInput)
  105. return FALSE;
  106. //Get the Max len of integer
  107. int iMaxInt = INT_MAX;
  108. WCHAR szMaxIntBuffer[34];
  109. //Security Review:34 is maximum buffer required
  110. _itow(iMaxInt,szMaxIntBuffer,10);
  111. //Security Review:_itow returns a null terminated string
  112. UINT nMaxLen = wcslen(szMaxIntBuffer);
  113. LPWSTR pszTempInput = pszInput;
  114. if(pszInput[0] == L'-')
  115. {
  116. pszTempInput++;
  117. }
  118. //Security review:pszTempInput is null terminated
  119. UINT nInputLen = wcslen(pszTempInput);
  120. if(nInputLen > nMaxLen)
  121. return FALSE;
  122. //
  123. //Convert input to long
  124. //
  125. LONG lInput = _wtol(pszTempInput);
  126. //
  127. //RAID: 700067 - ronmart
  128. //If lInput zero, then make sure the value
  129. //is really zero and not an error from _wtol
  130. //
  131. if(lInput == 0)
  132. {
  133. //
  134. //Walk the string
  135. //
  136. for(UINT i = 0; i < nInputLen; i++)
  137. {
  138. //
  139. // If non-numeric value encountered
  140. //
  141. if(pszTempInput[i] < L'0' || pszTempInput[i] > L'9')
  142. {
  143. //
  144. // And the value isn't a space, then a char string has
  145. // been passed so fail
  146. //
  147. if(pszTempInput[i] != L' ')
  148. return FALSE;
  149. }
  150. }
  151. }
  152. //
  153. //Check its less that max integer
  154. //
  155. if(lInput > (LONG)iMaxInt)
  156. return FALSE;
  157. //
  158. //Value is good
  159. //
  160. *pIntOutput = _wtoi(pszInput);
  161. return TRUE;
  162. }