UnicodeCollation.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /** @file
  2. Unicode Collation Support component that hides the trivial difference of Unicode Collation
  3. and Unicode collation 2 Protocol.
  4. Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
  5. SPDX-License-Identifier: BSD-2-Clause-Patent
  6. **/
  7. #include "Fat.h"
  8. EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;
  9. /**
  10. Worker function to initialize Unicode Collation support.
  11. It tries to locate Unicode Collation (2) protocol and matches it with current
  12. platform language code.
  13. @param AgentHandle The handle used to open Unicode Collation (2) protocol.
  14. @param ProtocolGuid The pointer to Unicode Collation (2) protocol GUID.
  15. @param VariableName The name of the RFC 4646 or ISO 639-2 language variable.
  16. @param DefaultLanguage The default language in case the RFC 4646 or ISO 639-2 language is absent.
  17. @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.
  18. @retval Others The Unicode Collation (2) protocol has not been located.
  19. **/
  20. EFI_STATUS
  21. InitializeUnicodeCollationSupportWorker (
  22. IN EFI_HANDLE AgentHandle,
  23. IN EFI_GUID *ProtocolGuid,
  24. IN CONST CHAR16 *VariableName,
  25. IN CONST CHAR8 *DefaultLanguage
  26. )
  27. {
  28. EFI_STATUS ReturnStatus;
  29. EFI_STATUS Status;
  30. UINTN NumHandles;
  31. UINTN Index;
  32. EFI_HANDLE *Handles;
  33. EFI_UNICODE_COLLATION_PROTOCOL *Uci;
  34. BOOLEAN Iso639Language;
  35. CHAR8 *Language;
  36. CHAR8 *BestLanguage;
  37. Status = gBS->LocateHandleBuffer (
  38. ByProtocol,
  39. ProtocolGuid,
  40. NULL,
  41. &NumHandles,
  42. &Handles
  43. );
  44. if (EFI_ERROR (Status)) {
  45. return Status;
  46. }
  47. Iso639Language = (BOOLEAN) (ProtocolGuid == &gEfiUnicodeCollationProtocolGuid);
  48. GetEfiGlobalVariable2 (VariableName, (VOID**) &Language, NULL);
  49. ReturnStatus = EFI_UNSUPPORTED;
  50. for (Index = 0; Index < NumHandles; Index++) {
  51. //
  52. // Open Unicode Collation Protocol
  53. //
  54. Status = gBS->OpenProtocol (
  55. Handles[Index],
  56. ProtocolGuid,
  57. (VOID **) &Uci,
  58. AgentHandle,
  59. NULL,
  60. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  61. );
  62. if (EFI_ERROR (Status)) {
  63. continue;
  64. }
  65. //
  66. // Find the best matching matching language from the supported languages
  67. // of Unicode Collation (2) protocol.
  68. //
  69. BestLanguage = GetBestLanguage (
  70. Uci->SupportedLanguages,
  71. Iso639Language,
  72. (Language == NULL) ? "" : Language,
  73. DefaultLanguage,
  74. NULL
  75. );
  76. if (BestLanguage != NULL) {
  77. FreePool (BestLanguage);
  78. mUnicodeCollationInterface = Uci;
  79. ReturnStatus = EFI_SUCCESS;
  80. break;
  81. }
  82. }
  83. if (Language != NULL) {
  84. FreePool (Language);
  85. }
  86. FreePool (Handles);
  87. return ReturnStatus;
  88. }
  89. /**
  90. Initialize Unicode Collation support.
  91. It tries to locate Unicode Collation 2 protocol and matches it with current
  92. platform language code. If for any reason the first attempt fails, it then tries to
  93. use Unicode Collation Protocol.
  94. @param AgentHandle The handle used to open Unicode Collation (2) protocol.
  95. @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.
  96. @retval Others The Unicode Collation (2) protocol has not been located.
  97. **/
  98. EFI_STATUS
  99. InitializeUnicodeCollationSupport (
  100. IN EFI_HANDLE AgentHandle
  101. )
  102. {
  103. EFI_STATUS Status;
  104. Status = EFI_UNSUPPORTED;
  105. //
  106. // First try to use RFC 4646 Unicode Collation 2 Protocol.
  107. //
  108. Status = InitializeUnicodeCollationSupportWorker (
  109. AgentHandle,
  110. &gEfiUnicodeCollation2ProtocolGuid,
  111. L"PlatformLang",
  112. (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang)
  113. );
  114. //
  115. // If the attempt to use Unicode Collation 2 Protocol fails, then we fall back
  116. // on the ISO 639-2 Unicode Collation Protocol.
  117. //
  118. if (EFI_ERROR (Status)) {
  119. Status = InitializeUnicodeCollationSupportWorker (
  120. AgentHandle,
  121. &gEfiUnicodeCollationProtocolGuid,
  122. L"Lang",
  123. (CONST CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang)
  124. );
  125. }
  126. return Status;
  127. }
  128. /**
  129. Performs a case-insensitive comparison of two Null-terminated Unicode strings.
  130. @param S1 A pointer to a Null-terminated Unicode string.
  131. @param S2 A pointer to a Null-terminated Unicode string.
  132. @retval 0 S1 is equivalent to S2.
  133. @retval >0 S1 is lexically greater than S2.
  134. @retval <0 S1 is lexically less than S2.
  135. **/
  136. INTN
  137. FatStriCmp (
  138. IN CHAR16 *S1,
  139. IN CHAR16 *S2
  140. )
  141. {
  142. ASSERT (StrSize (S1) != 0);
  143. ASSERT (StrSize (S2) != 0);
  144. ASSERT (mUnicodeCollationInterface != NULL);
  145. return mUnicodeCollationInterface->StriColl (
  146. mUnicodeCollationInterface,
  147. S1,
  148. S2
  149. );
  150. }
  151. /**
  152. Uppercase a string.
  153. @param String The string which will be upper-cased.
  154. **/
  155. VOID
  156. FatStrUpr (
  157. IN OUT CHAR16 *String
  158. )
  159. {
  160. ASSERT (StrSize (String) != 0);
  161. ASSERT (mUnicodeCollationInterface != NULL);
  162. mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);
  163. }
  164. /**
  165. Lowercase a string
  166. @param String The string which will be lower-cased.
  167. **/
  168. VOID
  169. FatStrLwr (
  170. IN OUT CHAR16 *String
  171. )
  172. {
  173. ASSERT (StrSize (String) != 0);
  174. ASSERT (mUnicodeCollationInterface != NULL);
  175. mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);
  176. }
  177. /**
  178. Convert FAT string to unicode string.
  179. @param FatSize The size of FAT string.
  180. @param Fat The FAT string.
  181. @param String The unicode string.
  182. @return None.
  183. **/
  184. VOID
  185. FatFatToStr (
  186. IN UINTN FatSize,
  187. IN CHAR8 *Fat,
  188. OUT CHAR16 *String
  189. )
  190. {
  191. ASSERT (Fat != NULL);
  192. ASSERT (String != NULL);
  193. ASSERT (((UINTN) String & 0x01) == 0);
  194. ASSERT (mUnicodeCollationInterface != NULL);
  195. mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);
  196. }
  197. /**
  198. Convert unicode string to Fat string.
  199. @param String The unicode string.
  200. @param FatSize The size of the FAT string.
  201. @param Fat The FAT string.
  202. @retval TRUE Convert successfully.
  203. @retval FALSE Convert error.
  204. **/
  205. BOOLEAN
  206. FatStrToFat (
  207. IN CHAR16 *String,
  208. IN UINTN FatSize,
  209. OUT CHAR8 *Fat
  210. )
  211. {
  212. ASSERT (Fat != NULL);
  213. ASSERT (StrSize (String) != 0);
  214. ASSERT (mUnicodeCollationInterface != NULL);
  215. return mUnicodeCollationInterface->StrToFat (
  216. mUnicodeCollationInterface,
  217. String,
  218. FatSize,
  219. Fat
  220. );
  221. }