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.

262 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. utils.c
  5. Abstract:
  6. Revision History:
  7. Jeff Sigman 05/01/00 Created
  8. Jeff Sigman 05/10/00 Version 1.5 released
  9. Jeff Sigman 10/18/00 Fix for Soft81 bug(s)
  10. --*/
  11. #include "precomp.h"
  12. //
  13. // Conditionally free's a pointer if it is non-null
  14. //
  15. VOID*
  16. RutlFree(
  17. IN VOID* pvData
  18. )
  19. {
  20. if (pvData)
  21. {
  22. FreePool(pvData);
  23. }
  24. return NULL;
  25. }
  26. //
  27. // Uses AllocateZeroPool to copy a string
  28. //
  29. char*
  30. RutlStrDup(
  31. IN char* pszSrc
  32. )
  33. {
  34. char* pszRet = NULL;
  35. UINTN dwLen = 0;
  36. if ((pszSrc == NULL) ||
  37. ((dwLen = strlena(pszSrc)) == 0)
  38. )
  39. {
  40. return NULL;
  41. }
  42. pszRet = AllocateZeroPool(dwLen + 1);
  43. if (pszRet)
  44. {
  45. CopyMem(pszRet, pszSrc, dwLen);
  46. }
  47. return pszRet;
  48. }
  49. //
  50. // Uses AllocateZeroPool to copy an ASCII string to unicode
  51. //
  52. CHAR16*
  53. RutlUniStrDup(
  54. IN char* pszSrc
  55. )
  56. {
  57. UINTN i,
  58. dwLen = 0;
  59. char* t = NULL;
  60. CHAR16* pszRet = NULL;
  61. if ((pszSrc == NULL) ||
  62. ((dwLen = strlena(pszSrc)) == 0)
  63. )
  64. {
  65. return NULL;
  66. }
  67. pszRet = AllocateZeroPool((dwLen + 1) * sizeof(CHAR16));
  68. if (pszRet != NULL)
  69. {
  70. t = (char*) pszRet;
  71. //
  72. // Convert the buffer to a hacked unicode.
  73. //
  74. for (i = 0; i < dwLen; i++)
  75. {
  76. *(t + i * 2) = *(pszSrc + i);
  77. }
  78. }
  79. return pszRet;
  80. }
  81. //
  82. // Find next token in string
  83. // Stolen from: ..\base\crts\crtw32\string\strtok.c
  84. //
  85. char* __cdecl
  86. strtok(
  87. IN char* string,
  88. IN const char* control
  89. )
  90. {
  91. unsigned char* str;
  92. const unsigned char* ctrl = control;
  93. unsigned char map[32];
  94. int count;
  95. static char* nextoken;
  96. /* Clear control map */
  97. for (count = 0; count < 32; count++)
  98. {
  99. map[count] = 0;
  100. }
  101. /* Set bits in delimiter table */
  102. do {
  103. map[*ctrl >> 3] |= (1 << (*ctrl & 7));
  104. } while (*ctrl++);
  105. /* Initialize str. If string is NULL, set str to the saved
  106. * pointer (i.e., continue breaking tokens out of the string
  107. * from the last strtok call) */
  108. if (string)
  109. {
  110. str = string;
  111. }
  112. else
  113. {
  114. str = nextoken;
  115. }
  116. /* Find beginning of token (skip over leading delimiters). Note that
  117. * there is no token iff this loop sets str to point to the terminal
  118. * null (*str == '\0') */
  119. while ((map[*str >> 3] & (1 << (*str & 7))) && *str)
  120. {
  121. str++;
  122. }
  123. string = str;
  124. /* Find the end of the token. If it is not the end of the string,
  125. * put a null there. */
  126. for (; *str; str++)
  127. {
  128. if (map[*str >> 3] & (1 << (*str & 7)))
  129. {
  130. *str++ = '\0';
  131. break;
  132. }
  133. }
  134. /* Update nextoken (or the corresponding field in the per-thread data
  135. * structure */
  136. nextoken = str;
  137. /* Determine if a token has been found. */
  138. if (string == str)
  139. {
  140. return NULL;
  141. }
  142. else
  143. {
  144. return string;
  145. }
  146. }
  147. //
  148. // Find a substring
  149. // Stolen from: ..\base\crts\crtw32\string\strstr.c
  150. //
  151. char* __cdecl
  152. strstr(
  153. IN const char* str1,
  154. IN const char* str2
  155. )
  156. {
  157. char* cp = (char*) str1;
  158. char* s1, *s2;
  159. if (!*str2)
  160. {
  161. return((char*)str1);
  162. }
  163. while (*cp)
  164. {
  165. s1 = cp;
  166. s2 = (char*) str2;
  167. while (*s1 && *s2 && !(*s1-*s2))
  168. {
  169. s1++, s2++;
  170. }
  171. if (!*s2)
  172. {
  173. return(cp);
  174. }
  175. cp++;
  176. }
  177. return(NULL);
  178. }
  179. //
  180. // Open a file, return a handle
  181. //
  182. EFI_FILE_HANDLE
  183. OpenFile(
  184. IN UINT64 OCFlags,
  185. IN EFI_LOADED_IMAGE* LoadedImage,
  186. IN EFI_FILE_HANDLE* CurDir,
  187. IN CHAR16* String
  188. )
  189. {
  190. UINTN i;
  191. CHAR16 FileName[128];
  192. CHAR16* DevicePathAsString = NULL;
  193. EFI_STATUS Status;
  194. EFI_FILE_HANDLE FileHandle = NULL;
  195. DevicePathAsString = DevicePathToStr(LoadedImage->FilePath);
  196. if (!DevicePathAsString)
  197. {
  198. return NULL;
  199. }
  200. StrCpy(FileName, DevicePathAsString);
  201. DevicePathAsString = RutlFree(DevicePathAsString);
  202. for(i = StrLen(FileName); i > 0 && FileName[i] != wackc; i--)
  203. ;
  204. FileName[i] = 0;
  205. StrCat(FileName, String);
  206. Status = (*CurDir)->Open(
  207. *CurDir,
  208. &FileHandle,
  209. FileName,
  210. OCFlags,
  211. 0);
  212. if (EFI_ERROR(Status))
  213. {
  214. return NULL;
  215. }
  216. return FileHandle;
  217. }