UnicodeCollation.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /** @file
  2. Unicode Collation Library that hides the trival difference of Unicode Collation
  3. and Unicode collation 2 Protocol.
  4. Copyright (c) 2007, Intel Corporation<BR>
  5. All rights reserved. This program and the accompanying materials
  6. are licensed and made available under the terms and conditions of the BSD License
  7. which accompanies this 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 <Uefi.h>
  13. #include <Guid/GlobalVariable.h>
  14. #include <Protocol/UnicodeCollation.h>
  15. #include <Library/BaseLib.h>
  16. #include <Library/BaseMemoryLib.h>
  17. #include <Library/DebugLib.h>
  18. #include <Library/PcdLib.h>
  19. #include <Library/MemoryAllocationLib.h>
  20. #include <Library/UefiBootServicesTableLib.h>
  21. #include <Library/UefiRuntimeServicesTableLib.h>
  22. STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollationInterface = NULL;
  23. typedef
  24. BOOLEAN
  25. (* SEARCH_LANG_CODE) (
  26. IN CONST CHAR8 *Languages,
  27. IN CONST CHAR8 *MatchLangCode
  28. );
  29. struct _UNICODE_INTERFACE {
  30. CHAR16 *VariableName;
  31. CHAR8 *DefaultLangCode;
  32. SEARCH_LANG_CODE SearchLangCode;
  33. EFI_GUID *UnicodeProtocolGuid;
  34. };
  35. typedef struct _UNICODE_INTERFACE UNICODE_INTERFACE;
  36. STATIC
  37. BOOLEAN
  38. SearchIso639LangCode (
  39. IN CONST CHAR8 *Languages,
  40. IN CONST CHAR8 *MatchLangCode
  41. )
  42. {
  43. CONST CHAR8 *LangCode;
  44. for (LangCode = Languages; *LangCode != '\0'; LangCode += 3) {
  45. if (CompareMem (LangCode, MatchLangCode, 3) == 0) {
  46. return TRUE;
  47. }
  48. }
  49. return FALSE;
  50. }
  51. STATIC
  52. BOOLEAN
  53. SearchRfc3066LangCode (
  54. IN CONST CHAR8 *Languages,
  55. IN CONST CHAR8 *MatchLangCode
  56. )
  57. {
  58. CHAR8 *SubStr;
  59. CHAR8 Terminal;
  60. SubStr = AsciiStrStr (Languages, MatchLangCode);
  61. if (SubStr == NULL) {
  62. return FALSE;
  63. }
  64. if (SubStr != Languages && *(SubStr - 1) != ';') {
  65. return FALSE;
  66. }
  67. Terminal = *(SubStr + AsciiStrLen (MatchLangCode));
  68. if (Terminal != '\0' && Terminal != ';') {
  69. return FALSE;
  70. }
  71. return TRUE;
  72. }
  73. GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mIso639Lang = {
  74. L"Lang",
  75. (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang),
  76. SearchIso639LangCode,
  77. &gEfiUnicodeCollationProtocolGuid,
  78. };
  79. GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mRfc3066Lang = {
  80. L"PlatformLang",
  81. (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang),
  82. SearchRfc3066LangCode,
  83. &gEfiUnicodeCollation2ProtocolGuid,
  84. };
  85. STATIC
  86. EFI_STATUS
  87. InitializeUnicodeCollationSupportWithConfig (
  88. IN EFI_HANDLE AgentHandle,
  89. IN UNICODE_INTERFACE *UnicodeInterface
  90. )
  91. {
  92. EFI_STATUS Status;
  93. CHAR8 Buffer[100];
  94. UINTN BufferSize;
  95. UINTN Index;
  96. CHAR8 *LangCode;
  97. UINTN NoHandles;
  98. EFI_HANDLE *Handles;
  99. EFI_UNICODE_COLLATION_PROTOCOL *Uci;
  100. LangCode = Buffer;
  101. BufferSize = sizeof (Buffer);
  102. Status = gRT->GetVariable (
  103. UnicodeInterface->VariableName,
  104. &gEfiGlobalVariableGuid,
  105. NULL,
  106. &BufferSize,
  107. Buffer
  108. );
  109. if (EFI_ERROR (Status)) {
  110. LangCode = UnicodeInterface->DefaultLangCode;
  111. }
  112. Status = gBS->LocateHandleBuffer (
  113. ByProtocol,
  114. UnicodeInterface->UnicodeProtocolGuid,
  115. NULL,
  116. &NoHandles,
  117. &Handles
  118. );
  119. if (EFI_ERROR (Status)) {
  120. return Status;
  121. }
  122. for (Index = 0; Index < NoHandles; Index++) {
  123. //
  124. // Open Unicode Collation Protocol
  125. //
  126. Status = gBS->OpenProtocol (
  127. Handles[Index],
  128. UnicodeInterface->UnicodeProtocolGuid,
  129. (VOID **) &Uci,
  130. AgentHandle,
  131. NULL,
  132. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  133. );
  134. if (EFI_ERROR (Status)) {
  135. continue;
  136. }
  137. if (UnicodeInterface->SearchLangCode (Uci->SupportedLanguages, LangCode)) {
  138. mUnicodeCollationInterface = Uci;
  139. break;
  140. }
  141. }
  142. FreePool (Handles);
  143. return (mUnicodeCollationInterface != NULL)? EFI_SUCCESS : EFI_NOT_FOUND;
  144. }
  145. /**
  146. Initialize Unicode Collation support.
  147. @param AgentHandle The handle used to open Unicode Collation (2) protocol.
  148. @retval EFI_SUCCESS The Unicode Collation (2) protocol has been successfully located.
  149. @retval Others The Unicode Collation (2) protocol has not been located.
  150. **/
  151. EFI_STATUS
  152. InitializeUnicodeCollationSupport (
  153. IN EFI_HANDLE AgentHandle
  154. )
  155. {
  156. EFI_STATUS Status;
  157. Status = EFI_UNSUPPORTED;
  158. if (FeaturePcdGet (PcdUnicodeCollation2Support)) {
  159. Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mRfc3066Lang);
  160. }
  161. if (FeaturePcdGet (PcdUnicodeCollationSupport) && EFI_ERROR (Status)) {
  162. Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mIso639Lang);
  163. }
  164. return Status;
  165. }
  166. /**
  167. Performs a case-insensitive comparison of two Null-terminated Unicode strings.
  168. @param S1 A pointer to a Null-terminated Unicode string.
  169. @param S2 A pointer to a Null-terminated Unicode string.
  170. @retval 0 S1 is equivalent to S2.
  171. @retval >0 S1 is lexically greater than S2.
  172. @retval <0 S1 is lexically less than S2.
  173. **/
  174. INTN
  175. FatStriCmp (
  176. IN CHAR16 *S1,
  177. IN CHAR16 *S2
  178. )
  179. {
  180. ASSERT (StrSize (S1) != 0);
  181. ASSERT (StrSize (S2) != 0);
  182. ASSERT (mUnicodeCollationInterface != NULL);
  183. return mUnicodeCollationInterface->StriColl (
  184. mUnicodeCollationInterface,
  185. S1,
  186. S2
  187. );
  188. }
  189. /**
  190. Uppercase a string.
  191. @param Str The string which will be upper-cased.
  192. @return None.
  193. **/
  194. VOID
  195. FatStrUpr (
  196. IN OUT CHAR16 *String
  197. )
  198. {
  199. ASSERT (StrSize (String) != 0);
  200. ASSERT (mUnicodeCollationInterface != NULL);
  201. mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);
  202. }
  203. /**
  204. Lowercase a string
  205. @param Str The string which will be lower-cased.
  206. @return None
  207. **/
  208. VOID
  209. FatStrLwr (
  210. IN OUT CHAR16 *String
  211. )
  212. {
  213. ASSERT (StrSize (String) != 0);
  214. ASSERT (mUnicodeCollationInterface != NULL);
  215. mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);
  216. }
  217. /**
  218. Convert FAT string to unicode string.
  219. @param FatSize The size of FAT string.
  220. @param Fat The FAT string.
  221. @param String The unicode string.
  222. @return None.
  223. **/
  224. VOID
  225. FatFatToStr (
  226. IN UINTN FatSize,
  227. IN CHAR8 *Fat,
  228. OUT CHAR16 *String
  229. )
  230. {
  231. ASSERT (Fat != NULL);
  232. ASSERT (String != NULL);
  233. ASSERT (((UINTN) String & 0x01) != 0);
  234. ASSERT (mUnicodeCollationInterface != NULL);
  235. mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);
  236. }
  237. /**
  238. Convert unicode string to Fat string.
  239. @param String The unicode string.
  240. @param FatSize The size of the FAT string.
  241. @param Fat The FAT string.
  242. @retval TRUE Convert successfully.
  243. @retval FALSE Convert error.
  244. **/
  245. BOOLEAN
  246. FatStrToFat (
  247. IN CHAR16 *String,
  248. IN UINTN FatSize,
  249. OUT CHAR8 *Fat
  250. )
  251. {
  252. ASSERT (Fat != NULL);
  253. ASSERT (StrSize (String) != 0);
  254. ASSERT (mUnicodeCollationInterface != NULL);
  255. return mUnicodeCollationInterface->StrToFat (
  256. mUnicodeCollationInterface,
  257. String,
  258. FatSize,
  259. Fat
  260. );
  261. }