str.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*++
  2. Copyright (c) 1998 Intel Corporation
  3. Module Name:
  4. str.c
  5. Abstract:
  6. Revision History
  7. --*/
  8. #include "lib.h"
  9. INTN
  10. StrCmp (
  11. IN CONST CHAR16 *s1,
  12. IN CONST CHAR16 *s2
  13. )
  14. // compare strings
  15. {
  16. return RtStrCmp(s1, s2);
  17. }
  18. INTN
  19. StrnCmp (
  20. IN CONST CHAR16 *s1,
  21. IN CONST CHAR16 *s2,
  22. IN UINTN len
  23. )
  24. // compare strings
  25. {
  26. while (*s1 && len) {
  27. if (*s1 != *s2) {
  28. break;
  29. }
  30. s1 += 1;
  31. s2 += 1;
  32. len -= 1;
  33. }
  34. return len ? *s1 - *s2 : 0;
  35. }
  36. INTN EFIAPI
  37. LibStubStriCmp (
  38. IN EFI_UNICODE_COLLATION_INTERFACE *This
  39. __attribute__((__unused__)),
  40. IN CHAR16 *s1,
  41. IN CHAR16 *s2
  42. )
  43. {
  44. return StrCmp (s1, s2);
  45. }
  46. VOID EFIAPI
  47. LibStubStrLwrUpr (
  48. IN EFI_UNICODE_COLLATION_INTERFACE *This __attribute__((__unused__)),
  49. IN CHAR16 *Str __attribute__((__unused__))
  50. )
  51. {
  52. }
  53. INTN
  54. StriCmp (
  55. IN CONST CHAR16 *s1,
  56. IN CONST CHAR16 *s2
  57. )
  58. // compare strings
  59. {
  60. if (UnicodeInterface == &LibStubUnicodeInterface)
  61. return UnicodeInterface->StriColl(UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
  62. else
  63. return uefi_call_wrapper(UnicodeInterface->StriColl, 3, UnicodeInterface, (CHAR16 *)s1, (CHAR16 *)s2);
  64. }
  65. VOID
  66. StrLwr (
  67. IN CHAR16 *Str
  68. )
  69. // lwoer case string
  70. {
  71. if (UnicodeInterface == &LibStubUnicodeInterface)
  72. UnicodeInterface->StrLwr(UnicodeInterface, Str);
  73. else uefi_call_wrapper(UnicodeInterface->StrLwr, 2, UnicodeInterface, Str);
  74. }
  75. VOID
  76. StrUpr (
  77. IN CHAR16 *Str
  78. )
  79. // upper case string
  80. {
  81. if (UnicodeInterface == &LibStubUnicodeInterface)
  82. UnicodeInterface->StrUpr(UnicodeInterface, Str);
  83. else uefi_call_wrapper(UnicodeInterface->StrUpr, 2, UnicodeInterface, Str);
  84. }
  85. VOID
  86. StrCpy (
  87. IN CHAR16 *Dest,
  88. IN CONST CHAR16 *Src
  89. )
  90. // copy strings
  91. {
  92. RtStrCpy (Dest, Src);
  93. }
  94. VOID
  95. StrCat (
  96. IN CHAR16 *Dest,
  97. IN CONST CHAR16 *Src
  98. )
  99. {
  100. RtStrCat(Dest, Src);
  101. }
  102. UINTN
  103. StrLen (
  104. IN CONST CHAR16 *s1
  105. )
  106. // string length
  107. {
  108. return RtStrLen(s1);
  109. }
  110. UINTN
  111. StrSize (
  112. IN CONST CHAR16 *s1
  113. )
  114. // string size
  115. {
  116. return RtStrSize(s1);
  117. }
  118. CHAR16 *
  119. StrDuplicate (
  120. IN CONST CHAR16 *Src
  121. )
  122. // duplicate a string
  123. {
  124. CHAR16 *Dest;
  125. UINTN Size;
  126. Size = StrSize(Src);
  127. Dest = AllocatePool (Size);
  128. if (Dest) {
  129. CopyMem (Dest, Src, Size);
  130. }
  131. return Dest;
  132. }
  133. UINTN
  134. strlena (
  135. IN CONST CHAR8 *s1
  136. )
  137. // string length
  138. {
  139. UINTN len;
  140. for (len=0; *s1; s1+=1, len+=1) ;
  141. return len;
  142. }
  143. UINTN
  144. strcmpa (
  145. IN CONST CHAR8 *s1,
  146. IN CONST CHAR8 *s2
  147. )
  148. // compare strings
  149. {
  150. while (*s1) {
  151. if (*s1 != *s2) {
  152. break;
  153. }
  154. s1 += 1;
  155. s2 += 1;
  156. }
  157. return *s1 - *s2;
  158. }
  159. UINTN
  160. strncmpa (
  161. IN CONST CHAR8 *s1,
  162. IN CONST CHAR8 *s2,
  163. IN UINTN len
  164. )
  165. // compare strings
  166. {
  167. while (*s1 && len) {
  168. if (*s1 != *s2) {
  169. break;
  170. }
  171. s1 += 1;
  172. s2 += 1;
  173. len -= 1;
  174. }
  175. return len ? *s1 - *s2 : 0;
  176. }
  177. UINTN
  178. xtoi (
  179. CONST CHAR16 *str
  180. )
  181. // convert hex string to uint
  182. {
  183. UINTN u;
  184. CHAR16 c;
  185. // skip preceeding white space
  186. while (*str && *str == ' ') {
  187. str += 1;
  188. }
  189. // convert hex digits
  190. u = 0;
  191. while ((c = *(str++))) {
  192. if (c >= 'a' && c <= 'f') {
  193. c -= 'a' - 'A';
  194. }
  195. if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F')) {
  196. u = (u << 4) | (c - (c >= 'A' ? 'A'-10 : '0'));
  197. } else {
  198. break;
  199. }
  200. }
  201. return u;
  202. }
  203. UINTN
  204. Atoi (
  205. CONST CHAR16 *str
  206. )
  207. // convert hex string to uint
  208. {
  209. UINTN u;
  210. CHAR16 c;
  211. // skip preceeding white space
  212. while (*str && *str == ' ') {
  213. str += 1;
  214. }
  215. // convert digits
  216. u = 0;
  217. while ((c = *(str++))) {
  218. if (c >= '0' && c <= '9') {
  219. u = (u * 10) + c - '0';
  220. } else {
  221. break;
  222. }
  223. }
  224. return u;
  225. }
  226. BOOLEAN
  227. MetaMatch (
  228. IN CHAR16 *String,
  229. IN CHAR16 *Pattern
  230. )
  231. {
  232. CHAR16 c, p, l;
  233. for (; ;) {
  234. p = *Pattern;
  235. Pattern += 1;
  236. switch (p) {
  237. case 0:
  238. // End of pattern. If end of string, TRUE match
  239. return *String ? FALSE : TRUE;
  240. case '*':
  241. // Match zero or more chars
  242. while (*String) {
  243. if (MetaMatch (String, Pattern)) {
  244. return TRUE;
  245. }
  246. String += 1;
  247. }
  248. return MetaMatch (String, Pattern);
  249. case '?':
  250. // Match any one char
  251. if (!*String) {
  252. return FALSE;
  253. }
  254. String += 1;
  255. break;
  256. case '[':
  257. // Match char set
  258. c = *String;
  259. if (!c) {
  260. return FALSE; // syntax problem
  261. }
  262. l = 0;
  263. while ((p = *Pattern++)) {
  264. if (p == ']') {
  265. return FALSE;
  266. }
  267. if (p == '-') { // if range of chars,
  268. p = *Pattern; // get high range
  269. if (p == 0 || p == ']') {
  270. return FALSE; // syntax problem
  271. }
  272. if (c >= l && c <= p) { // if in range,
  273. break; // it's a match
  274. }
  275. }
  276. l = p;
  277. if (c == p) { // if char matches
  278. break; // move on
  279. }
  280. }
  281. // skip to end of match char set
  282. while (p && p != ']') {
  283. p = *Pattern;
  284. Pattern += 1;
  285. }
  286. String += 1;
  287. break;
  288. default:
  289. c = *String;
  290. if (c != p) {
  291. return FALSE;
  292. }
  293. String += 1;
  294. break;
  295. }
  296. }
  297. }
  298. BOOLEAN EFIAPI
  299. LibStubMetaiMatch (
  300. IN EFI_UNICODE_COLLATION_INTERFACE *This
  301. __attribute__((__unused__)),
  302. IN CHAR16 *String,
  303. IN CHAR16 *Pattern
  304. )
  305. {
  306. return MetaMatch (String, Pattern);
  307. }
  308. BOOLEAN
  309. MetaiMatch (
  310. IN CHAR16 *String,
  311. IN CHAR16 *Pattern
  312. )
  313. {
  314. if (UnicodeInterface == &LibStubUnicodeInterface)
  315. return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
  316. else return uefi_call_wrapper(UnicodeInterface->MetaiMatch, 3, UnicodeInterface, String, Pattern);
  317. }