UnicodeCollation.c 8.0 KB

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