efi_unicode_collation.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI Unicode collation protocol
  4. *
  5. * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. */
  7. #include <common.h>
  8. #include <charset.h>
  9. #include <cp1250.h>
  10. #include <cp437.h>
  11. #include <efi_loader.h>
  12. /* Characters that may not be used in file names */
  13. static const char illegal[] = "<>:\"/\\|?*";
  14. /*
  15. * EDK2 assumes codepage 1250 when creating FAT 8.3 file names.
  16. * Linux defaults to codepage 437 for FAT 8.3 file names.
  17. */
  18. #if CONFIG_FAT_DEFAULT_CODEPAGE == 1250
  19. /* Unicode code points for code page 1250 characters 0x80 - 0xff */
  20. static const u16 codepage[] = CP1250;
  21. #else
  22. /* Unicode code points for code page 437 characters 0x80 - 0xff */
  23. static const u16 codepage[] = CP437;
  24. #endif
  25. /* GUID of the EFI_UNICODE_COLLATION_PROTOCOL */
  26. const efi_guid_t efi_guid_unicode_collation_protocol =
  27. EFI_UNICODE_COLLATION_PROTOCOL2_GUID;
  28. /**
  29. * efi_stri_coll() - compare utf-16 strings case-insenitively
  30. *
  31. * @this: unicode collation protocol instance
  32. * @s1: first string
  33. * @s2: second string
  34. *
  35. * This function implements the StriColl() service of the
  36. * EFI_UNICODE_COLLATION_PROTOCOL.
  37. *
  38. * See the Unified Extensible Firmware Interface (UEFI) specification for
  39. * details.
  40. *
  41. * TODO:
  42. * The implementation does not follow the Unicode collation algorithm.
  43. * For ASCII characters it results in the same sort order as EDK2.
  44. * We could use table UNICODE_CAPITALIZATION_TABLE for better results.
  45. *
  46. * Return: 0: s1 == s2, > 0: s1 > s2, < 0: s1 < s2
  47. */
  48. static efi_intn_t EFIAPI efi_stri_coll(
  49. struct efi_unicode_collation_protocol *this, u16 *s1, u16 *s2)
  50. {
  51. s32 c1, c2;
  52. efi_intn_t ret = 0;
  53. EFI_ENTRY("%p, %ls, %ls", this, s1, s2);
  54. for (; *s1 | *s2; ++s1, ++s2) {
  55. c1 = utf_to_upper(*s1);
  56. c2 = utf_to_upper(*s2);
  57. if (c1 < c2) {
  58. ret = -1;
  59. goto out;
  60. } else if (c1 > c2) {
  61. ret = 1;
  62. goto out;
  63. }
  64. }
  65. out:
  66. EFI_EXIT(EFI_SUCCESS);
  67. return ret;
  68. }
  69. /**
  70. * metai_match() - compare utf-16 string with a pattern string case-insenitively
  71. *
  72. * @s: string to compare
  73. * @p: pattern string
  74. *
  75. * The pattern string may use these:
  76. * - * matches >= 0 characters
  77. * - ? matches 1 character
  78. * - [<char1><char2>...<charN>] match any character in the set
  79. * - [<char1>-<char2>] matches any character in the range
  80. *
  81. * This function is called my efi_metai_match().
  82. *
  83. * For '*' pattern searches this function calls itself recursively.
  84. * Performance-wise this is suboptimal, especially for multiple '*' wildcards.
  85. * But it results in simple code.
  86. *
  87. * Return: true if the string is matched.
  88. */
  89. static bool metai_match(const u16 *s, const u16 *p)
  90. {
  91. u16 first;
  92. for (; *s && *p; ++s, ++p) {
  93. switch (*p) {
  94. case '*':
  95. /* Match 0 or more characters */
  96. ++p;
  97. for (;; ++s) {
  98. if (metai_match(s, p))
  99. return true;
  100. if (!*s)
  101. return false;
  102. }
  103. case '?':
  104. /* Match any one character */
  105. break;
  106. case '[':
  107. /* Match any character in the set */
  108. ++p;
  109. first = *p;
  110. if (first == ']')
  111. /* Empty set */
  112. return false;
  113. ++p;
  114. if (*p == '-') {
  115. /* Range */
  116. ++p;
  117. if (*s < first || *s > *p)
  118. return false;
  119. ++p;
  120. if (*p != ']')
  121. return false;
  122. } else {
  123. /* Set */
  124. bool hit = false;
  125. if (*s == first)
  126. hit = true;
  127. for (; *p && *p != ']'; ++p) {
  128. if (*p == *s)
  129. hit = true;
  130. }
  131. if (!hit || *p != ']')
  132. return false;
  133. }
  134. break;
  135. default:
  136. /* Match one character */
  137. if (*p != *s)
  138. return false;
  139. }
  140. }
  141. if (!*p && !*s)
  142. return true;
  143. return false;
  144. }
  145. /**
  146. * efi_metai_match() - compare utf-16 string with a pattern string
  147. * case-insenitively
  148. *
  149. * @this: unicode collation protocol instance
  150. * @s: string to compare
  151. * @p: pattern string
  152. *
  153. * The pattern string may use these:
  154. * - * matches >= 0 characters
  155. * - ? matches 1 character
  156. * - [<char1><char2>...<charN>] match any character in the set
  157. * - [<char1>-<char2>] matches any character in the range
  158. *
  159. * This function implements the MetaMatch() service of the
  160. * EFI_UNICODE_COLLATION_PROTOCOL.
  161. *
  162. * Return: true if the string is matched.
  163. */
  164. static bool EFIAPI efi_metai_match(struct efi_unicode_collation_protocol *this,
  165. const u16 *string, const u16 *pattern)
  166. {
  167. bool ret;
  168. EFI_ENTRY("%p, %ls, %ls", this, string, pattern);
  169. ret = metai_match(string, pattern);
  170. EFI_EXIT(EFI_SUCCESS);
  171. return ret;
  172. }
  173. /**
  174. * efi_str_lwr() - convert to lower case
  175. *
  176. * @this: unicode collation protocol instance
  177. * @string: string to convert
  178. * @p: pattern string
  179. *
  180. * The conversion is done in place. As long as upper and lower letters use the
  181. * same number of words this does not pose a problem.
  182. *
  183. * This function implements the StrLwr() service of the
  184. * EFI_UNICODE_COLLATION_PROTOCOL.
  185. */
  186. static void EFIAPI efi_str_lwr(struct efi_unicode_collation_protocol *this,
  187. u16 *string)
  188. {
  189. EFI_ENTRY("%p, %ls", this, string);
  190. for (; *string; ++string)
  191. *string = utf_to_lower(*string);
  192. EFI_EXIT(EFI_SUCCESS);
  193. }
  194. /**
  195. * efi_str_upr() - convert to upper case
  196. *
  197. * @this: unicode collation protocol instance
  198. * @string: string to convert
  199. * @p: pattern string
  200. *
  201. * The conversion is done in place. As long as upper and lower letters use the
  202. * same number of words this does not pose a problem.
  203. *
  204. * This function implements the StrUpr() service of the
  205. * EFI_UNICODE_COLLATION_PROTOCOL.
  206. */
  207. static void EFIAPI efi_str_upr(struct efi_unicode_collation_protocol *this,
  208. u16 *string)
  209. {
  210. EFI_ENTRY("%p, %ls", this, string);
  211. for (; *string; ++string)
  212. *string = utf_to_upper(*string);
  213. EFI_EXIT(EFI_SUCCESS);
  214. }
  215. /**
  216. * efi_fat_to_str() - convert an 8.3 file name from an OEM codepage to Unicode
  217. *
  218. * @this: unicode collation protocol instance
  219. * @fat_size: size of the string to convert
  220. * @fat: string to convert
  221. * @string: converted string
  222. *
  223. * This function implements the FatToStr() service of the
  224. * EFI_UNICODE_COLLATION_PROTOCOL.
  225. */
  226. static void EFIAPI efi_fat_to_str(struct efi_unicode_collation_protocol *this,
  227. efi_uintn_t fat_size, char *fat, u16 *string)
  228. {
  229. efi_uintn_t i;
  230. u16 c;
  231. EFI_ENTRY("%p, %zu, %s, %p", this, fat_size, fat, string);
  232. for (i = 0; i < fat_size; ++i) {
  233. c = (unsigned char)fat[i];
  234. if (c > 0x80)
  235. c = codepage[i - 0x80];
  236. string[i] = c;
  237. if (!c)
  238. break;
  239. }
  240. string[i] = 0;
  241. EFI_EXIT(EFI_SUCCESS);
  242. }
  243. /**
  244. * efi_fat_to_str() - convert a utf-16 string to legal characters for a FAT
  245. * file name in an OEM code page
  246. *
  247. * @this: unicode collation protocol instance
  248. * @string: Unicode string to convert
  249. * @fat_size: size of the target buffer
  250. * @fat: converted string
  251. *
  252. * This function implements the StrToFat() service of the
  253. * EFI_UNICODE_COLLATION_PROTOCOL.
  254. *
  255. * Return: true if an illegal character was substituted by '_'.
  256. */
  257. static bool EFIAPI efi_str_to_fat(struct efi_unicode_collation_protocol *this,
  258. const u16 *string, efi_uintn_t fat_size,
  259. char *fat)
  260. {
  261. efi_uintn_t i;
  262. s32 c;
  263. bool ret = false;
  264. EFI_ENTRY("%p, %ls, %zu, %p", this, string, fat_size, fat);
  265. for (i = 0; i < fat_size;) {
  266. c = utf16_get(&string);
  267. switch (c) {
  268. /* Ignore period and space */
  269. case '.':
  270. case ' ':
  271. continue;
  272. case 0:
  273. break;
  274. }
  275. c = utf_to_upper(c);
  276. if (c >= 0x80) {
  277. int j;
  278. /* Look for codepage translation */
  279. for (j = 0; j < 0x80; ++j) {
  280. if (c == codepage[j]) {
  281. c = j + 0x80;
  282. break;
  283. }
  284. }
  285. if (j >= 0x80) {
  286. c = '_';
  287. ret = true;
  288. }
  289. } else if (c && (c < 0x20 || strchr(illegal, c))) {
  290. c = '_';
  291. ret = true;
  292. }
  293. fat[i] = c;
  294. if (!c)
  295. break;
  296. ++i;
  297. }
  298. EFI_EXIT(EFI_SUCCESS);
  299. return ret;
  300. }
  301. const struct efi_unicode_collation_protocol efi_unicode_collation_protocol = {
  302. .stri_coll = efi_stri_coll,
  303. .metai_match = efi_metai_match,
  304. .str_lwr = efi_str_lwr,
  305. .str_upr = efi_str_upr,
  306. .fat_to_str = efi_fat_to_str,
  307. .str_to_fat = efi_str_to_fat,
  308. .supported_languages = "en",
  309. };