Source code of Windows XP (NT5)
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.

209 lines
3.8 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: string.cpp
  7. //
  8. // Contents: Cert Server wrapper routines
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. #define __dwFILE__ __dwFILE_CERTLIB_STRING_CPP__
  14. extern HINSTANCE g_hInstance;
  15. DWORD g_cStringAlloc;
  16. DWORD g_cStringUsed;
  17. typedef struct _RESOURCESTRING
  18. {
  19. DWORD id;
  20. WCHAR const *pwsz;
  21. } RESOURCESTRING;
  22. #define CRS_CHUNK 100
  23. RESOURCESTRING *g_rgrs = NULL;
  24. DWORD g_crsMax = 0;
  25. DWORD g_crs = 0;
  26. RESOURCESTRING *
  27. AllocStringHeader()
  28. {
  29. if (g_crs >= g_crsMax)
  30. {
  31. DWORD cb = (CRS_CHUNK + g_crsMax) * sizeof(g_rgrs[0]);
  32. RESOURCESTRING *rgrsT;
  33. if (NULL == g_rgrs)
  34. {
  35. rgrsT = (RESOURCESTRING *) LocalAlloc(LMEM_FIXED, cb);
  36. }
  37. else
  38. {
  39. rgrsT = (RESOURCESTRING *) LocalReAlloc(g_rgrs, cb, LMEM_MOVEABLE);
  40. }
  41. if (NULL == rgrsT)
  42. {
  43. DBGPRINT((
  44. DBG_SS_CERTLIB,
  45. "Error allocating resource string header\n"));
  46. return(NULL);
  47. }
  48. g_rgrs = rgrsT;
  49. g_crsMax += CRS_CHUNK;
  50. }
  51. return(&g_rgrs[g_crs++]);
  52. }
  53. #define cwcRESOURCEMIN 128
  54. #define cwcRESOURCEMAX 8192
  55. WCHAR const *
  56. myLoadResourceString(
  57. IN DWORD ResourceId)
  58. {
  59. HRESULT hr;
  60. DWORD i;
  61. WCHAR awc[cwcRESOURCEMIN];
  62. WCHAR *pwsz = NULL;
  63. DWORD cwc;
  64. WCHAR const *pwszString = NULL;
  65. WCHAR *pwszAlloc;
  66. for (i = 0; i < g_crs; i++)
  67. {
  68. if (g_rgrs[i].id == ResourceId)
  69. {
  70. pwszString = g_rgrs[i].pwsz;
  71. }
  72. }
  73. if (NULL == pwszString)
  74. {
  75. RESOURCESTRING *prs;
  76. pwsz = awc;
  77. cwc = ARRAYSIZE(awc);
  78. while (TRUE)
  79. {
  80. if (!LoadString(g_hInstance, ResourceId, pwsz, cwc))
  81. {
  82. hr = myHLastError();
  83. DBGPRINT((
  84. DBG_SS_CERTLIB,
  85. "LoadString(%d) -> %x\n",
  86. ResourceId,
  87. hr));
  88. _JumpError(hr, error, "LoadString");
  89. }
  90. #if 0
  91. DBGPRINT((
  92. DBG_SS_CERTLIBI,
  93. "myLoadResourceString(%d) %x/%x\n",
  94. ResourceId,
  95. wcslen(pwsz),
  96. cwc));
  97. #endif
  98. // if there's any room left, the resource was not truncated.
  99. // if the buffer is already at the maximum size we support, we just
  100. // live with the truncation.
  101. if (wcslen(pwsz) < cwc - 1 || cwcRESOURCEMAX <= cwc)
  102. {
  103. break;
  104. }
  105. // LoadString filled the buffer completely, so the string may have
  106. // been truncated. Double the buffer size and try again.
  107. DBGPRINT((
  108. DBG_SS_CERTLIBI,
  109. "myLoadResourceString(%d) %x/%x ==> %x\n",
  110. ResourceId,
  111. wcslen(pwsz),
  112. cwc,
  113. cwc << 1));
  114. if (pwsz != awc)
  115. {
  116. LocalFree(pwsz);
  117. pwsz = NULL;
  118. }
  119. cwc <<= 1;
  120. pwsz = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  121. if (NULL == pwsz)
  122. {
  123. hr = E_OUTOFMEMORY;
  124. _JumpError(hr, error, "LocalAlloc");
  125. }
  126. }
  127. hr = myDupString(pwsz, &pwszAlloc);
  128. _JumpIfError(hr, error, "myDupString");
  129. prs = AllocStringHeader();
  130. if (NULL != prs)
  131. {
  132. prs->id = ResourceId;
  133. prs->pwsz = pwszAlloc;
  134. }
  135. pwszString = pwszAlloc;
  136. g_cStringAlloc++;
  137. }
  138. g_cStringUsed++;
  139. error:
  140. if (NULL != pwsz && awc != pwsz)
  141. {
  142. LocalFree(pwsz);
  143. }
  144. if (NULL == pwszString)
  145. {
  146. SetLastError(hr);
  147. }
  148. return(pwszString);
  149. }
  150. VOID
  151. myFreeResourceStrings(
  152. IN char const *pszModule)
  153. {
  154. DWORD i;
  155. if (0 != g_cStringAlloc || 0 != g_crs || 0 != g_cStringUsed)
  156. {
  157. DBGPRINT((
  158. DBG_SS_CERTLIBI,
  159. "%hs Strings: alloc = %d, saved = %d, used = %d\n",
  160. pszModule,
  161. g_cStringAlloc,
  162. g_crs,
  163. g_cStringUsed));
  164. }
  165. if (NULL != g_rgrs)
  166. {
  167. for (i = 0; i < g_crs; i++)
  168. {
  169. LocalFree(const_cast<WCHAR *>(g_rgrs[i].pwsz));
  170. g_rgrs[i].pwsz = NULL;
  171. g_rgrs[i].id = 0;
  172. }
  173. LocalFree(g_rgrs);
  174. g_rgrs = NULL;
  175. }
  176. g_crsMax = 0;
  177. g_crs = 0;
  178. g_cStringAlloc = 0;
  179. g_cStringUsed = 0;
  180. }