PageRenderTime 62ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/ntdll/crt.c

https://github.com/sherpya/nloader
C | 328 lines | 242 code | 57 blank | 29 comment | 43 complexity | a064a16d64f0592fd3ae4af55c211b33 MD5 | raw file
  1. /*
  2. * Copyright (c) 2010, Sherpya <sherpya@netfarm.it>, aCaB <acab@0xacab.net>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the copyright holders nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "ntdll.h"
  28. #include <ctype.h>
  29. MODULE(crt)
  30. FORWARD_FUNCTION(memcmp, rpl_memcmp);
  31. FORWARD_FUNCTION(memcpy, rpl_memcpy);
  32. FORWARD_FUNCTION(memset, rpl_memset);
  33. FORWARD_FUNCTION(memmove, rpl_memmove);
  34. FORWARD_FUNCTION(atoll, rpl__atoi64);
  35. FORWARD_FUNCTION(atoi, rpl_atoi);
  36. FORWARD_FUNCTION(qsort, rpl_qsort);
  37. FORWARD_FUNCTION(strcasecmp, rpl__stricmp);
  38. FORWARD_FUNCTION(strncasecmp, rpl__strnicmp);
  39. FORWARD_FUNCTION(strncpy, rpl_strncpy);
  40. FORWARD_FUNCTION(strpbrk, rpl_strpbrk);
  41. FORWARD_FUNCTION(strspn, rpl_strspn);
  42. // long is always 32bit on win32
  43. LONG CDECL rpl_atol(const char *nptr)
  44. {
  45. return atol(nptr);
  46. }
  47. SIZE_T CDECL rpl_wcslen(LPCWSTR str)
  48. {
  49. const WCHAR *s = str;
  50. if (!str)
  51. return 0;
  52. #ifdef DEBUG_CRT
  53. spam_wchar(str);
  54. #endif
  55. while (*s)
  56. s++;
  57. return (s - str);
  58. }
  59. LPWSTR CDECL rpl_wcscat(LPWSTR dest, LPCWSTR src)
  60. {
  61. DECLAREVARCONV(srcA);
  62. DECLAREVARCONV(destA);
  63. WCHAR *d;
  64. if (!src)
  65. return dest;
  66. WSTR2STR(dest);
  67. WSTR2STR(src);
  68. Log("ntdll.wcscat(\"%s\", \"%s\")\n", destA, srcA);
  69. d = dest;
  70. /* while (*d++); d--; */ // FIXME: why not?
  71. while ((*d++ = *src++));
  72. return dest;
  73. }
  74. int CDECL rpl_wcscmp(LPCWSTR s1, LPCWSTR s2)
  75. {
  76. DECLAREVARCONV(s1A);
  77. DECLAREVARCONV(s2A);
  78. WSTR2STR(s1);
  79. WSTR2STR(s2);
  80. Log("ntdll._wcscmp(\"%s\", \"%s\")\n", s1A, s2A);
  81. while (*s1 == *s2)
  82. {
  83. if (!*s1++) return 0;
  84. s2++;
  85. }
  86. return (*s1 < *s2) ? -1 : 1;
  87. }
  88. int CDECL rpl_wcsncmp(LPCWSTR s1, LPCWSTR s2, SIZE_T n)
  89. {
  90. DECLAREVARCONV(s1A);
  91. DECLAREVARCONV(s2A);
  92. WSTR2STR(s1);
  93. WSTR2STR(s2);
  94. Log("ntdll.wcsncmp(\"%s\", \"%s\", %d)\n", s1A, s2A, n);
  95. do
  96. {
  97. if (!(n && *s1))
  98. return 0;
  99. }
  100. while (n-- && (*s1++ == *s2++));
  101. return (*s1 < *s2) ? -1 : 1;
  102. }
  103. int CDECL rpl__wcsicmp(LPCWSTR s1, LPCWSTR s2)
  104. {
  105. DECLAREVARCONV(s1A);
  106. DECLAREVARCONV(s2A);
  107. WSTR2STR(s1);
  108. WSTR2STR(s2);
  109. Log("ntdll._wcsicmp(\"%s\", \"%s\")\n", s1A, s2A);
  110. while (wcmp(*s1, *s2, TRUE))
  111. {
  112. if (!*s1++)
  113. return 0;
  114. s2++;
  115. }
  116. return widetol(*s1) < widetol(*s2) ? -1 : 1;
  117. }
  118. int CDECL rpl__wcsnicmp(LPCWSTR s1, LPCWSTR s2, SIZE_T n)
  119. {
  120. DECLAREVARCONV(s1A);
  121. DECLAREVARCONV(s2A);
  122. WSTR2STR(s1);
  123. WSTR2STR(s2);
  124. Log("ntdll._wcsnicmp(\"%s\", \"%s\", %d)\n", s1A, s2A, n);
  125. while (wcmp(*s1, *s2, TRUE))
  126. {
  127. n--;
  128. if (!(n && *s1++))
  129. return 0;
  130. s2++;
  131. }
  132. return widetol(*s1) < widetol(*s2) ? -1 : 1;
  133. }
  134. LPWSTR CDECL rpl_wcsstr(LPCWSTR haystack, LPCWSTR needle)
  135. {
  136. char *res;
  137. DECLAREVARCONV(haystackA);
  138. DECLAREVARCONV(needleA);
  139. if (!rpl_wcslen(needle))
  140. return (LPWSTR) haystack;
  141. WSTR2STR(haystack);
  142. WSTR2STR(needle);
  143. if (!(res = strstr(haystackA, needleA)))
  144. return NULL;
  145. return (LPWSTR) (haystack + (needleA - haystackA));
  146. }
  147. LPWSTR CDECL rpl_wcscpy(LPWSTR dest, LPCWSTR src)
  148. {
  149. WCHAR *s = dest;
  150. while (*src)
  151. *s++ = *src++;
  152. *s = 0;
  153. return dest;
  154. }
  155. LPWSTR CDECL rpl_wcsrchr(LPCWSTR s, WCHAR c)
  156. {
  157. const WCHAR *p = NULL;
  158. DECLAREVARCONV(sA);
  159. WSTR2STR(s);
  160. Log("ntdll.wcsrchr(\"%s\", \"%c\")\n", sA, widetoa(c));
  161. do
  162. {
  163. if (*s == c)
  164. p = s;
  165. } while (*s++);
  166. return (WCHAR *) p;
  167. }
  168. LPWSTR CDECL rpl_wcschr(LPCWSTR str, WCHAR c)
  169. {
  170. WCHAR *s = (WCHAR *) str;
  171. DECLAREVARCONV(strA);
  172. WSTR2STR(str);
  173. Log("ntdll.wcschr(\"%s\", \"%c\")\n", strA, widetoa(c));
  174. while (*s)
  175. {
  176. if (*s == c)
  177. return s;
  178. s++;
  179. }
  180. return NULL;
  181. }
  182. LPWSTR CDECL rpl__wcsupr(LPWSTR str)
  183. {
  184. WCHAR *s = str;
  185. while (*s)
  186. {
  187. *s = widetou(*s);
  188. s++;
  189. }
  190. return str;
  191. }
  192. LPWSTR CDECL rpl__wcslwr(LPWSTR str)
  193. {
  194. WCHAR *s = str;
  195. while (*s)
  196. {
  197. *s = widetol(*s);
  198. s++;
  199. }
  200. return str;
  201. }
  202. SIZE_T rpl_wcsspn(LPCWSTR wcs, LPCWSTR accept)
  203. {
  204. SIZE_T res;
  205. DECLAREVARCONV(wcsA);
  206. DECLAREVARCONV(acceptA);
  207. WSTR2STR(wcs);
  208. WSTR2STR(accept);
  209. res = strspn(wcsA, acceptA);
  210. Log("ntdll._wcsspn(\"%s\", \"%s\") = %d\n", wcsA, acceptA, res);
  211. return res;
  212. }
  213. /* ctype */
  214. int CDECL rpl_isspace(int c)
  215. {
  216. if (((unsigned) c <= 255) && strchr(" \f\n\r\t", c))
  217. return 1;
  218. return 0;
  219. }
  220. int CDECL rpl_isprint(int c)
  221. {
  222. if (((unsigned) c <= 255) && (c >= ' ') && (c <= '~'))
  223. return 1;
  224. return 0;
  225. }
  226. ULONG CDECL rpl_wcstoul(const wchar_t *nptr, wchar_t **endptr, int base)
  227. {
  228. ULONG ret;
  229. DECLAREVARCONV(nptrA);
  230. WSTR2STR(nptr);
  231. char *endptrA;
  232. ret = strtoul(nptrA, &endptrA, base);
  233. if (endptr)
  234. *endptr = ((wchar_t *) nptr) + (endptrA - nptrA);
  235. return ret;
  236. }
  237. ULONGLONG CDECL rpl__wcstoui64(const wchar_t *nptr, wchar_t **endptr, int base)
  238. {
  239. ULONGLONG ret;
  240. DECLAREVARCONV(nptrA);
  241. WSTR2STR(nptr);
  242. char *endptrA;
  243. ret = strtoll(nptrA, &endptrA, base);
  244. if (endptr)
  245. *endptr = ((wchar_t *) nptr) + (endptrA - nptrA);
  246. return ret;
  247. }
  248. LONGLONG CDECL rpl__wtoi64(LPCWSTR str)
  249. {
  250. ULONGLONG value = 0;
  251. BOOLEAN neg = FALSE;
  252. while (rpl_isspace(widetoa(*str)))
  253. str++;
  254. if (*str == L'+')
  255. str++;
  256. else if (*str == L'-')
  257. {
  258. neg = TRUE;
  259. str++;
  260. }
  261. while (widetoa(*str) >= '0' && widetoa(*str) <= '9')
  262. {
  263. value = value * 10 + widetoa(*str) - '0';
  264. str++;
  265. }
  266. return neg ? 0 - value : value;
  267. }
  268. LONG CDECL rpl__wtol(LPCWSTR str)
  269. {
  270. return (LONG) rpl__wtoi64(str);
  271. }