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.

355 lines
7.6 KiB

  1. #define UNICODE 1
  2. #include "shellprv.h"
  3. #pragma hdrstop
  4. /*
  5. * StrEndN - Find the end of a string, but no more than n bytes
  6. * Assumes lpStart points to start of null terminated string
  7. * nBufSize is the maximum length
  8. * returns ptr to just after the last byte to be included
  9. */
  10. LPWSTR StrEndNW(LPCWSTR lpStart, int nBufSize)
  11. {
  12. LPCWSTR lpEnd;
  13. for (lpEnd = lpStart + nBufSize; *lpStart && lpStart < lpEnd; lpStart = CharNext(lpStart))
  14. {
  15. /* just getting to the end of the string */
  16. continue;
  17. }
  18. if (lpStart > lpEnd)
  19. {
  20. /* We can only get here if the last wchar before lpEnd was a lead byte
  21. */
  22. lpStart -= 2;
  23. }
  24. return((LPWSTR)lpStart);
  25. }
  26. LPSTR StrEndNA(LPCSTR lpStart, int nBufSize)
  27. {
  28. LPCSTR lpEnd;
  29. for (lpEnd = lpStart + nBufSize; *lpStart && lpStart < lpEnd; lpStart = CharNextA(lpStart))
  30. {
  31. /* just getting to the end of the string */
  32. continue;
  33. }
  34. if (lpStart > lpEnd)
  35. {
  36. // We can only get here if the last byte before lpEnd was a lead byte
  37. lpStart -= 2;
  38. }
  39. return (LPSTR)lpStart;
  40. }
  41. /*
  42. * StrCpyN - Copy up to N chars, don't end in LeadByte char
  43. *
  44. * Assumes lpDest points to buffer of nBufSize bytes (including NULL)
  45. * lpSource points to string to be copied.
  46. * returns Number of bytes copied, NOT including NULL
  47. */
  48. int Shell32_StrCpyNW(LPWSTR lpDest, LPWSTR lpSource, int nBufSize)
  49. {
  50. LPWSTR lpEnd;
  51. WCHAR cHold;
  52. if (nBufSize < 0)
  53. return(nBufSize);
  54. lpEnd = StrEndNW(lpSource, nBufSize);
  55. cHold = *lpEnd;
  56. *lpEnd = WCHAR_NULL;
  57. lstrcpy(lpDest, lpSource);
  58. *lpEnd = cHold;
  59. return (int)(lpEnd - lpSource);
  60. }
  61. int Shell32_StrCpyNA(LPSTR lpDest, LPSTR lpSource, int nBufSize)
  62. {
  63. LPSTR lpEnd;
  64. CHAR cHold;
  65. if (nBufSize < 0)
  66. return(nBufSize);
  67. lpEnd = StrEndNA(lpSource, nBufSize);
  68. cHold = *lpEnd;
  69. *lpEnd = '\0';
  70. lstrcpyA(lpDest, lpSource);
  71. *lpEnd = cHold;
  72. return (int)(lpEnd - lpSource);
  73. }
  74. /*
  75. * StrNCmp - Compare n characters
  76. *
  77. * returns See lstrcmp return values.
  78. */
  79. int StrNCmpW(LPWSTR lpStr1, LPWSTR lpStr2, int nChar)
  80. {
  81. WCHAR cHold1, cHold2;
  82. int i;
  83. LPWSTR lpsz1 = lpStr1, lpsz2 = lpStr2;
  84. for (i = 0; i < nChar; i++)
  85. {
  86. /* If we hit the end of either string before the given number
  87. * of bytes, just return the comparison
  88. */
  89. if (!*lpsz1 || !*lpsz2)
  90. return(wcscmp(lpStr1, lpStr2));
  91. lpsz1 = CharNextW(lpsz1);
  92. lpsz2 = CharNextW(lpsz2);
  93. }
  94. cHold1 = *lpsz1;
  95. cHold2 = *lpsz2;
  96. *lpsz1 = *lpsz2 = WCHAR_NULL;
  97. i = wcscmp(lpStr1, lpStr2);
  98. *lpsz1 = cHold1;
  99. *lpsz2 = cHold2;
  100. return(i);
  101. }
  102. int StrNCmpA(LPSTR lpStr1, LPSTR lpStr2, int nChar)
  103. {
  104. CHAR cHold1, cHold2;
  105. int i;
  106. LPSTR lpsz1 = lpStr1, lpsz2 = lpStr2;
  107. for (i = 0; i < nChar; i++)
  108. {
  109. /* If we hit the end of either string before the given number
  110. * of bytes, just return the comparison
  111. */
  112. if (!*lpsz1 || !*lpsz2)
  113. return(lstrcmpA(lpStr1, lpStr2));
  114. lpsz1 = CharNextA(lpsz1);
  115. lpsz2 = CharNextA(lpsz2);
  116. }
  117. cHold1 = *lpsz1;
  118. cHold2 = *lpsz2;
  119. *lpsz1 = *lpsz2 = '\0';
  120. i = lstrcmpA(lpStr1, lpStr2);
  121. *lpsz1 = cHold1;
  122. *lpsz2 = cHold2;
  123. return i;
  124. }
  125. /*
  126. * StrNCmpI - Compare n characters, case insensitive
  127. *
  128. * returns See lstrcmpi return values.
  129. */
  130. int StrNCmpIW(LPWSTR lpStr1, LPWSTR lpStr2, int nChar)
  131. {
  132. WCHAR cHold1, cHold2;
  133. int i;
  134. LPWSTR lpsz1 = lpStr1, lpsz2 = lpStr2;
  135. for (i = 0; i < nChar; i++)
  136. {
  137. /* If we hit the end of either string before the given number
  138. * of bytes, just return the comparison
  139. */
  140. if (!*lpsz1 || !*lpsz2)
  141. return(lstrcmpi(lpStr1, lpStr2));
  142. lpsz1 = CharNext(lpsz1);
  143. lpsz2 = CharNext(lpsz2);
  144. }
  145. cHold1 = *lpsz1;
  146. cHold2 = *lpsz2;
  147. *lpsz1 = *lpsz2 = WCHAR_NULL;
  148. i = _wcsicmp(lpStr1, lpStr2);
  149. *lpsz1 = cHold1;
  150. *lpsz2 = cHold2;
  151. return i;
  152. }
  153. int StrNCmpIA(LPSTR lpStr1, LPSTR lpStr2, int nChar)
  154. {
  155. CHAR cHold1, cHold2;
  156. int i;
  157. LPSTR lpsz1 = lpStr1, lpsz2 = lpStr2;
  158. for (i = 0; i < nChar; i++)
  159. {
  160. /* If we hit the end of either string before the given number
  161. * of bytes, just return the comparison
  162. */
  163. if (!*lpsz1 || !*lpsz2)
  164. return(lstrcmpiA(lpStr1, lpStr2));
  165. lpsz1 = CharNextA(lpsz1);
  166. lpsz2 = CharNextA(lpsz2);
  167. }
  168. cHold1 = *lpsz1;
  169. cHold2 = *lpsz2;
  170. *lpsz1 = *lpsz2 = '\0';
  171. i = lstrcmpiA(lpStr1, lpStr2);
  172. *lpsz1 = cHold1;
  173. *lpsz2 = cHold2;
  174. return i;
  175. }
  176. /*
  177. * StrNCpy - Copy n characters
  178. *
  179. * returns Actual number of characters copied
  180. */
  181. int StrNCpyW(LPWSTR lpDest, LPWSTR lpSource, int nChar)
  182. {
  183. WCHAR cHold;
  184. int i;
  185. LPWSTR lpch = lpSource;
  186. if (nChar < 0)
  187. return(nChar);
  188. for (i = 0; i < nChar; i++)
  189. {
  190. if (!*lpch)
  191. break;
  192. lpch = CharNext(lpch);
  193. }
  194. cHold = *lpch;
  195. *lpch = WCHAR_NULL;
  196. wcscpy(lpDest, lpSource);
  197. *lpch = cHold;
  198. return i;
  199. }
  200. int StrNCpyA(LPSTR lpDest, LPSTR lpSource,int nChar)
  201. {
  202. CHAR cHold;
  203. int i;
  204. LPSTR lpch = lpSource;
  205. if (nChar < 0)
  206. return(nChar);
  207. for (i = 0; i < nChar; i++)
  208. {
  209. if (!*lpch)
  210. break;
  211. lpch = CharNextA(lpch);
  212. }
  213. cHold = *lpch;
  214. *lpch = '\0';
  215. lstrcpyA(lpDest, lpSource);
  216. *lpch = cHold;
  217. return i;
  218. }
  219. /*
  220. * StrRStr - Search for last occurrence of a substring
  221. *
  222. * Assumes lpSource points to the null terminated source string
  223. * lpLast points to where to search from in the source string
  224. * lpLast is not included in the search
  225. * lpSrch points to string to search for
  226. * returns last occurrence of string if successful; NULL otherwise
  227. */
  228. LPWSTR StrRStrW(LPWSTR lpSource, LPWSTR lpLast, LPWSTR lpSrch)
  229. {
  230. int iLen;
  231. iLen = lstrlen(lpSrch);
  232. if (!lpLast)
  233. {
  234. lpLast = lpSource + lstrlen(lpSource);
  235. }
  236. do
  237. {
  238. /* Return NULL if we hit the exact beginning of the string
  239. */
  240. if (lpLast == lpSource)
  241. return(NULL);
  242. --lpLast;
  243. /* Break if we hit the beginning of the string
  244. */
  245. if (!lpLast)
  246. break;
  247. /* Break if we found the string, and its first byte is not a tail byte
  248. */
  249. if (!StrCmpNW(lpLast, lpSrch, iLen) && (lpLast==StrEndNW(lpSource, (int)(lpLast-lpSource))))
  250. break;
  251. }
  252. while (1);
  253. return lpLast;
  254. }
  255. LPSTR StrRStrA(LPSTR lpSource, LPSTR lpLast, LPSTR lpSrch)
  256. {
  257. int iLen;
  258. iLen = lstrlenA(lpSrch);
  259. if (!lpLast)
  260. {
  261. lpLast = lpSource + lstrlenA(lpSource);
  262. }
  263. do
  264. {
  265. /* Return NULL if we hit the exact beginning of the string
  266. */
  267. if (lpLast == lpSource)
  268. return(NULL);
  269. --lpLast;
  270. /* Break if we hit the beginning of the string
  271. */
  272. if (!lpLast)
  273. break;
  274. /* Break if we found the string, and its first byte is not a tail byte
  275. */
  276. if (!StrCmpNA(lpLast, lpSrch, iLen) &&(lpLast==StrEndNA(lpSource, (int)(lpLast-lpSource))))
  277. {
  278. break;
  279. }
  280. }
  281. while (1);
  282. return lpLast;
  283. }