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.

255 lines
7.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: strings.cpp
  8. //
  9. // Useful string manipulation functions.
  10. //
  11. //--------------------------------------------------------------------------
  12. #include "pch.h"
  13. #pragma hdrstop
  14. #define StringByteSize(sz) \
  15. ((lstrlen(sz)+1)*sizeof(TCHAR))
  16. /*-----------------------------------------------------------------------------
  17. / LocalAllocString
  18. / ------------------
  19. / Allocate a string, and initialize it with the specified contents.
  20. /
  21. / In:
  22. / ppResult -> recieves pointer to the new string
  23. / pString -> string to initialize with
  24. /
  25. / Out:
  26. / HRESULT
  27. /----------------------------------------------------------------------------*/
  28. HRESULT LocalAllocString(LPTSTR* ppResult, LPCTSTR pString)
  29. {
  30. if ( !ppResult || !pString )
  31. return E_INVALIDARG;
  32. *ppResult = (LPTSTR)LocalAlloc(LPTR, StringByteSize(pString) );
  33. if ( !*ppResult )
  34. return E_OUTOFMEMORY;
  35. //Security Review:Buffer is correctly allocated
  36. lstrcpy(*ppResult, pString);
  37. return S_OK; // success
  38. }
  39. /*----------------------------------------------------------------------------
  40. / LocalAllocStringLen
  41. / ---------------------
  42. / Given a length return a buffer of that size.
  43. /
  44. / In:
  45. / ppResult -> receives the pointer to the string
  46. / cLen = length in characters to allocate
  47. /
  48. / Out:
  49. / HRESULT
  50. /----------------------------------------------------------------------------*/
  51. HRESULT LocalAllocStringLen(LPTSTR* ppResult, UINT cLen)
  52. {
  53. if ( !ppResult || cLen == 0 )
  54. return E_INVALIDARG;
  55. *ppResult = (LPTSTR)LocalAlloc(LPTR, (cLen+1) * sizeof(TCHAR));
  56. return *ppResult ? S_OK:E_OUTOFMEMORY;
  57. }
  58. /*-----------------------------------------------------------------------------
  59. / LocalFreeString
  60. / -----------------
  61. / Release the string pointed to be *ppString (which can be null) and
  62. / then reset the pointer back to NULL.
  63. /
  64. / In:
  65. / ppString -> pointer to string pointer to be free'd
  66. /
  67. / Out:
  68. / -
  69. /----------------------------------------------------------------------------*/
  70. void LocalFreeString(LPTSTR* ppString)
  71. {
  72. if ( ppString )
  73. {
  74. if ( *ppString )
  75. LocalFree((HLOCAL)*ppString);
  76. *ppString = NULL;
  77. }
  78. }
  79. //*************************************************************
  80. //
  81. // SizeofStringResource
  82. //
  83. // Purpose: Find the length (in chars) of a string resource
  84. //
  85. // Parameters: HINSTANCE hInstance - module containing the string
  86. // UINT idStr - ID of string
  87. //
  88. //
  89. // Return: UINT - # of chars in string, not including NULL
  90. //
  91. // Notes: Based on code from user32.
  92. //
  93. //*************************************************************
  94. UINT
  95. SizeofStringResource(HINSTANCE hInstance,
  96. UINT idStr)
  97. {
  98. UINT cch = 0;
  99. HRSRC hRes = FindResource(hInstance, (LPTSTR)((LONG_PTR)(((USHORT)idStr >> 4) + 1)), RT_STRING);
  100. if (NULL != hRes)
  101. {
  102. HGLOBAL hStringSeg = LoadResource(hInstance, hRes);
  103. if (NULL != hStringSeg)
  104. {
  105. LPWSTR psz = (LPWSTR)LockResource(hStringSeg);
  106. if (NULL != psz)
  107. {
  108. idStr &= 0x0F;
  109. while(true)
  110. {
  111. cch = *psz++;
  112. if (idStr-- == 0)
  113. break;
  114. psz += cch;
  115. }
  116. }
  117. }
  118. }
  119. return cch;
  120. }
  121. //*************************************************************
  122. //
  123. // LoadStringAlloc
  124. //
  125. // Purpose: Loads a string resource into an alloc'd buffer
  126. //
  127. // Parameters: ppszResult - string resource returned here
  128. // hInstance - module to load string from
  129. // idStr - string resource ID
  130. //
  131. // Return: same as LoadString
  132. //
  133. // Notes: On successful return, the caller must
  134. // LocalFree *ppszResult
  135. //
  136. //*************************************************************
  137. int
  138. LoadStringAlloc(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr)
  139. {
  140. int nResult = 0;
  141. UINT cch = SizeofStringResource(hInstance, idStr);
  142. if (cch)
  143. {
  144. cch++; // for NULL
  145. *ppszResult = (LPTSTR)LocalAlloc(LPTR, cch * sizeof(TCHAR));
  146. if (*ppszResult)
  147. nResult = LoadString(hInstance, idStr, *ppszResult, cch);
  148. }
  149. return nResult;
  150. }
  151. //*************************************************************
  152. //
  153. // String formatting functions
  154. //
  155. //*************************************************************
  156. DWORD
  157. FormatStringID(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr, ...)
  158. {
  159. DWORD dwResult;
  160. va_list args;
  161. va_start(args, idStr);
  162. dwResult = vFormatStringID(ppszResult, hInstance, idStr, &args);
  163. va_end(args);
  164. return dwResult;
  165. }
  166. DWORD
  167. FormatString(LPTSTR *ppszResult, LPCTSTR pszFormat, ...)
  168. {
  169. DWORD dwResult;
  170. va_list args;
  171. va_start(args, pszFormat);
  172. dwResult = vFormatString(ppszResult, pszFormat, &args);
  173. va_end(args);
  174. return dwResult;
  175. }
  176. DWORD
  177. vFormatStringID(LPTSTR *ppszResult, HINSTANCE hInstance, UINT idStr, va_list *pargs)
  178. {
  179. DWORD dwResult = 0;
  180. LPTSTR pszFormat = NULL;
  181. if (LoadStringAlloc(&pszFormat, hInstance, idStr))
  182. {
  183. dwResult = vFormatString(ppszResult, pszFormat, pargs);
  184. LocalFree(pszFormat);
  185. }
  186. return dwResult;
  187. }
  188. DWORD
  189. vFormatString(LPTSTR *ppszResult, LPCTSTR pszFormat, va_list *pargs)
  190. {
  191. //Security Review:FORMAT_MESSAGE_ALLOCATE_BUFFER is used so function
  192. //will correctly allocate the buffer.
  193. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING,
  194. pszFormat,
  195. 0,
  196. 0,
  197. (LPTSTR)ppszResult,
  198. 1,
  199. pargs);
  200. }
  201. //*************************************************************
  202. //
  203. // GetSystemErrorText
  204. //
  205. // Purpose: Retrieve error text for a win32 error value
  206. //
  207. // Parameters: ppszResult - string resource returned here
  208. // dwErr - error ID
  209. //
  210. // Return: same as FormatMessage
  211. //
  212. // Notes: On successful return, the caller must
  213. // LocalFree *ppszResult
  214. //
  215. //*************************************************************
  216. DWORD
  217. GetSystemErrorText(LPTSTR *ppszResult, DWORD dwErr)
  218. {
  219. //Security Review:FORMAT_MESSAGE_ALLOCATE_BUFFER is used so function
  220. //will correctly allocate the buffer.
  221. return FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  222. NULL,
  223. dwErr,
  224. 0,
  225. (LPTSTR)ppszResult,
  226. 0,
  227. NULL);
  228. }