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.

262 lines
6.7 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // strbuf.c - string buffer functions
  24. ////
  25. #include "winlocal.h"
  26. #include <stdarg.h>
  27. #include "strbuf.h"
  28. #include "mem.h"
  29. #include "trace.h"
  30. ////
  31. // private definitions
  32. ////
  33. #define CBUF_DEFAULT 8
  34. #define SIZBUF_DEFAULT 512
  35. // string buffer control struct
  36. //
  37. typedef struct STRBUF
  38. {
  39. DWORD dwVersion;
  40. HINSTANCE hInst;
  41. HTASK hTask;
  42. int cBuf;
  43. int sizBuf;
  44. int iBuf;
  45. LPTSTR lpszBuf;
  46. } STRBUF, FAR *LPSTRBUF;
  47. // helper functions
  48. //
  49. static LPSTRBUF StrBufGetPtr(HSTRBUF hStrBuf);
  50. static HSTRBUF StrBufGetHandle(LPSTRBUF lpStrBuf);
  51. ////
  52. // public functions
  53. ////
  54. // StrBufInit - initialize str buffer engine
  55. // <dwVersion> (i) must be STRBUF_VERSION
  56. // <hInst> (i) instance handle of calling module
  57. // <cBuf> (i) number of string buffers to create
  58. // 0 use default number
  59. // <sizBuf> (i) size of each string buffer, in characters
  60. // 0 use default size
  61. // return str buffer engine handle (NULL if error)
  62. //
  63. HSTRBUF DLLEXPORT WINAPI StrBufInit(DWORD dwVersion, HINSTANCE hInst, int cBuf, int sizBuf)
  64. {
  65. BOOL fSuccess = TRUE;
  66. LPSTRBUF lpStrBuf = NULL;
  67. if (dwVersion != STRBUF_VERSION)
  68. fSuccess = TraceFALSE(NULL);
  69. else if (hInst == NULL)
  70. fSuccess = TraceFALSE(NULL);
  71. else if ((lpStrBuf = (LPSTRBUF) MemAlloc(NULL, sizeof(STRBUF), 0)) == NULL)
  72. fSuccess = TraceFALSE(NULL);
  73. else
  74. {
  75. lpStrBuf->dwVersion = dwVersion;
  76. lpStrBuf->hInst = hInst;
  77. lpStrBuf->hTask = GetCurrentTask();
  78. lpStrBuf->cBuf = cBuf == 0 ? CBUF_DEFAULT : cBuf;
  79. lpStrBuf->sizBuf = sizBuf == 0 ? SIZBUF_DEFAULT : sizBuf;
  80. lpStrBuf->iBuf = -1;
  81. lpStrBuf->lpszBuf = NULL;
  82. if ((lpStrBuf->lpszBuf = (LPTSTR) MemAlloc(NULL,
  83. lpStrBuf->cBuf * lpStrBuf->sizBuf * sizeof(TCHAR), 0)) == NULL)
  84. {
  85. fSuccess = TraceFALSE(NULL);
  86. }
  87. }
  88. if (!fSuccess)
  89. {
  90. StrBufTerm(StrBufGetHandle(lpStrBuf));
  91. lpStrBuf = NULL;
  92. }
  93. return fSuccess ? StrBufGetHandle(lpStrBuf) : NULL;
  94. }
  95. // StrBufTerm - shut down str buffer engine
  96. // <hStrBuf> (i) handle returned by StrBufInit
  97. // return 0 if success
  98. //
  99. int DLLEXPORT WINAPI StrBufTerm(HSTRBUF hStrBuf)
  100. {
  101. BOOL fSuccess = TRUE;
  102. LPSTRBUF lpStrBuf;
  103. if ((lpStrBuf = StrBufGetPtr(hStrBuf)) == NULL)
  104. fSuccess = TraceFALSE(NULL);
  105. else
  106. {
  107. if (lpStrBuf->lpszBuf != NULL &&
  108. (lpStrBuf->lpszBuf = MemFree(NULL, lpStrBuf->lpszBuf)) != NULL)
  109. {
  110. fSuccess = TraceFALSE(NULL);
  111. }
  112. if ((lpStrBuf = MemFree(NULL, lpStrBuf)) != NULL)
  113. fSuccess = TraceFALSE(NULL);
  114. }
  115. return fSuccess ? 0 : -1;
  116. }
  117. // StrBufLoad - load string with specified id from resource file
  118. // <hStrBuf> (i) handle returned by StrBufInit
  119. // <idString> (i) resource id of string to load
  120. // return ptr to string in next available string buffer (NULL if error)
  121. //
  122. LPTSTR DLLEXPORT WINAPI StrBufLoad(HSTRBUF hStrBuf, UINT idString)
  123. {
  124. BOOL fSuccess = TRUE;
  125. LPSTRBUF lpStrBuf;
  126. LPTSTR lpsz;
  127. if ((lpStrBuf = StrBufGetPtr(hStrBuf)) == NULL)
  128. fSuccess = TraceFALSE(NULL);
  129. else if ((lpsz = StrBufGetNext(hStrBuf)) == NULL)
  130. fSuccess = TraceFALSE(NULL);
  131. else if (idString != 0
  132. && LoadString(lpStrBuf->hInst, idString, lpsz, (int) lpStrBuf->sizBuf) <= 0)
  133. {
  134. // specified string not found, construct a dummy string instead
  135. //
  136. wsprintf(lpsz, TEXT("String #%u"), idString);
  137. }
  138. return fSuccess ? lpsz : NULL;
  139. }
  140. // StrBufSprintf - modified version of wsprintf
  141. // <hStrBuf> (i) handle returned by StrBufInit
  142. // <lpszOutput> (o) buffer to hold formatted string result
  143. // NULL do not copy; return string buffer pointer
  144. // <lpszFormat,...> (i) format string and arguments
  145. // returns pointer to resultant string (NULL if error)
  146. //
  147. LPTSTR DLLEXPORT FAR CDECL StrBufSprintf(HSTRBUF hStrBuf, LPTSTR lpszOutput, LPCTSTR lpszFormat, ...)
  148. {
  149. BOOL fSuccess = TRUE;
  150. LPTSTR lpszTemp = lpszOutput;
  151. if (lpszOutput == NULL &&
  152. (lpszTemp = StrBufGetNext(hStrBuf)) == NULL)
  153. fSuccess = TraceFALSE(NULL);
  154. else
  155. {
  156. va_list args;
  157. va_start(args, lpszFormat);
  158. wvsprintf(lpszTemp, lpszFormat, args);
  159. va_end(args);
  160. }
  161. return fSuccess ? lpszTemp : NULL;
  162. }
  163. // StrBufGetNext - get next available static string buffer
  164. // <hStrBuf> (i) handle returned by StrBufInit
  165. // return string buffer pointer (NULL if error)
  166. // NOTE: buffers are recycled every <cBuf> times function is called
  167. //
  168. LPTSTR DLLEXPORT WINAPI StrBufGetNext(HSTRBUF hStrBuf)
  169. {
  170. BOOL fSuccess = TRUE;
  171. LPSTRBUF lpStrBuf;
  172. LPTSTR lpszBuf;
  173. if ((lpStrBuf = StrBufGetPtr(hStrBuf)) == NULL)
  174. fSuccess = TraceFALSE(NULL);
  175. else
  176. {
  177. if (++lpStrBuf->iBuf >= lpStrBuf->cBuf)
  178. lpStrBuf->iBuf = 0;
  179. lpszBuf = lpStrBuf->lpszBuf + (lpStrBuf->iBuf * lpStrBuf->sizBuf);
  180. }
  181. return fSuccess ? lpszBuf : NULL;
  182. }
  183. ////
  184. // helper functions
  185. ////
  186. // verify that str buffer engine handle is valid,
  187. // <hStrBuf> (i) handle returned by StrBufInit
  188. // return corresponding str buffer engine pointer (NULL if error)
  189. //
  190. static LPSTRBUF StrBufGetPtr(HSTRBUF hStrBuf)
  191. {
  192. BOOL fSuccess = TRUE;
  193. LPSTRBUF lpStrBuf;
  194. if ((lpStrBuf = (LPSTRBUF) hStrBuf) == NULL)
  195. fSuccess = TraceFALSE(NULL);
  196. else if (IsBadWritePtr(lpStrBuf, sizeof(STRBUF)))
  197. fSuccess = TraceFALSE(NULL);
  198. #ifdef CHECKTASK
  199. // make sure current task owns the str buffer engine handle
  200. //
  201. else if (lpStrBuf->hTask != GetCurrentTask())
  202. fSuccess = TraceFALSE(NULL);
  203. #endif
  204. return fSuccess ? lpStrBuf : NULL;
  205. }
  206. // verify that str buffer engine pointer is valid,
  207. // <lpStrBuf> (i) pointer to STRBUF struct
  208. // return corresponding str buffer engine handle (NULL if error)
  209. //
  210. static HSTRBUF StrBufGetHandle(LPSTRBUF lpStrBuf)
  211. {
  212. BOOL fSuccess = TRUE;
  213. HSTRBUF hStrBuf;
  214. if ((hStrBuf = (HSTRBUF) lpStrBuf) == NULL)
  215. fSuccess = TraceFALSE(NULL);
  216. return fSuccess ? hStrBuf : NULL;
  217. }