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.

238 lines
4.8 KiB

  1. #include "sfstr.h"
  2. #include "dbg.h"
  3. HRESULT _SafeStrCpyN(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest)
  4. {
  5. ASSERT(pszDest && pszSrc && (cchDest > 0));
  6. HRESULT hres = E_BUFFERTOOSMALL;
  7. // Need room for NULL terminator
  8. --cchDest;
  9. while (cchDest && *pszSrc)
  10. {
  11. *pszDest = *pszSrc;
  12. ++pszDest;
  13. ++pszSrc;
  14. --cchDest;
  15. }
  16. // Did we reach the end?
  17. if (!*pszSrc)
  18. {
  19. // Yep
  20. *pszDest = 0;
  21. hres = S_OK;
  22. }
  23. return hres;
  24. }
  25. HRESULT SafeStrCpyN(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest)
  26. {
  27. HRESULT hres = E_INVALIDARG;
  28. if (pszDest && pszSrc && (cchDest > 0))
  29. {
  30. hres = _SafeStrCpyN(pszDest, pszSrc, cchDest);
  31. }
  32. return hres;
  33. }
  34. HRESULT SafeStrCatN(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest)
  35. {
  36. HRESULT hres = E_INVALIDARG;
  37. if (pszDest && pszSrc && (cchDest > 0))
  38. {
  39. LPWSTR psz = pszDest;
  40. hres = E_BUFFERTOOSMALL;
  41. while (cchDest && *psz)
  42. {
  43. ++psz;
  44. --cchDest;
  45. }
  46. if (cchDest)
  47. {
  48. hres = _SafeStrCpyN(psz, pszSrc, cchDest);
  49. }
  50. }
  51. return hres;
  52. }
  53. HRESULT _SafeStrCpyNEx(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest,
  54. LPWSTR* ppszEnd, DWORD* pcchLeft)
  55. {
  56. ASSERT(pszDest && pszSrc && (cchDest > 0) && ppszEnd && pcchLeft);
  57. HRESULT hres = E_BUFFERTOOSMALL;
  58. // Need room for NULL terminator
  59. --cchDest;
  60. while (cchDest && *pszSrc)
  61. {
  62. *pszDest = *pszSrc;
  63. ++pszDest;
  64. ++pszSrc;
  65. --cchDest;
  66. }
  67. // Did we reach the end?
  68. if (!*pszSrc)
  69. {
  70. // Yep
  71. *pszDest = 0;
  72. *ppszEnd = pszDest;
  73. *pcchLeft = (cchDest + 1);
  74. hres = S_OK;
  75. }
  76. return hres;
  77. }
  78. HRESULT SafeStrCpyNEx(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest,
  79. LPWSTR* ppszEnd, DWORD* pcchLeft)
  80. {
  81. HRESULT hres = E_INVALIDARG;
  82. if (pszDest && pszSrc && (cchDest > 0) && ppszEnd && pcchLeft)
  83. {
  84. hres = _SafeStrCpyNEx(pszDest, pszSrc, cchDest, ppszEnd, pcchLeft);
  85. }
  86. return hres;
  87. }
  88. HRESULT SafeStrCatNEx(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest,
  89. LPWSTR* ppszEnd, DWORD* pcchLeft)
  90. {
  91. HRESULT hres = E_INVALIDARG;
  92. if (pszDest && pszSrc && (cchDest > 0) && ppszEnd && pcchLeft)
  93. {
  94. LPWSTR psz = pszDest;
  95. hres = E_BUFFERTOOSMALL;
  96. while (cchDest && *psz)
  97. {
  98. ++psz;
  99. --cchDest;
  100. }
  101. if (cchDest)
  102. {
  103. hres = _SafeStrCpyNEx(psz, pszSrc, cchDest, ppszEnd, pcchLeft);
  104. }
  105. }
  106. return hres;
  107. }
  108. HRESULT SafeStrCpyNReq(LPWSTR pszDest, LPWSTR pszSrc, DWORD cchDest,
  109. DWORD* pcchRequired)
  110. {
  111. HRESULT hres = E_INVALIDARG;
  112. if (pszDest && pszSrc && cchDest && pcchRequired)
  113. {
  114. hres = SafeStrCpyN(pszDest, pszSrc, cchDest);
  115. if (E_BUFFERTOOSMALL == hres)
  116. {
  117. *pcchRequired = lstrlen(pszSrc) + 1;
  118. }
  119. else
  120. {
  121. *pcchRequired = 0;
  122. }
  123. }
  124. return hres;
  125. }
  126. HRESULT _SafeStrCpyNExactEx(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest,
  127. DWORD cchExact, LPWSTR* ppszEnd, DWORD* pcchLeft)
  128. {
  129. HRESULT hres = E_INVALIDARG;
  130. ASSERT((ppszEnd && pcchLeft) || (!ppszEnd && !pcchLeft));
  131. if (pszDest && pszSrc && (cchDest > 0) && (cchExact > 0))
  132. {
  133. // Need room for NULL terminator
  134. --cchDest;
  135. --cchExact;
  136. while (cchDest && cchExact && *pszSrc)
  137. {
  138. *pszDest = *pszSrc;
  139. ++pszDest;
  140. ++pszSrc;
  141. --cchDest;
  142. --cchExact;
  143. }
  144. // Did we stop because we copied the Exact # of chars to copy?
  145. if (!cchExact)
  146. {
  147. // Yep, NULL terminate it!
  148. *pszDest = 0;
  149. if (ppszEnd)
  150. {
  151. *ppszEnd = pszDest;
  152. *pcchLeft = (cchDest + 1);
  153. }
  154. hres = S_OK;
  155. }
  156. else
  157. {
  158. // Did we stop because we met the end of the sources string before
  159. // copying cchExact chars?
  160. if (!*pszSrc)
  161. {
  162. // Yes
  163. hres = E_SOURCEBUFFERTOOSMALL;
  164. }
  165. else
  166. {
  167. // No
  168. hres = E_BUFFERTOOSMALL;
  169. }
  170. }
  171. }
  172. return hres;
  173. }
  174. HRESULT SafeStrCpyNExact(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest,
  175. DWORD cchExact)
  176. {
  177. return _SafeStrCpyNExactEx(pszDest, pszSrc, cchDest, cchExact, NULL, NULL);
  178. }
  179. HRESULT SafeStrCpyNExactEx(LPWSTR pszDest, LPCWSTR pszSrc, DWORD cchDest,
  180. DWORD cchExact, LPWSTR* ppszEnd, DWORD* pcchLeft)
  181. {
  182. HRESULT hres = E_INVALIDARG;
  183. if (ppszEnd && pcchLeft)
  184. {
  185. hres = _SafeStrCpyNExactEx(pszDest, pszSrc, cchDest, cchExact, ppszEnd,
  186. pcchLeft);
  187. }
  188. return hres;
  189. }