summaryrefslogtreecommitdiffstats
path: root/FatPkg/EnhancedFatDxe/UnicodeCollation.c
blob: 9e5db7b96714abc2d80a6db95bdd03ebc4918f71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/** @file
  Unicode Collation Library that hides the trival difference of Unicode Collation
  and Unicode collation 2 Protocol.

  Copyright (c) 2007, Intel Corporation<BR>
  All rights reserved. This program and the accompanying materials
  are licensed and made available under the terms and conditions of the BSD License
  which accompanies this distribution.  The full text of the license may be found at
  http://opensource.org/licenses/bsd-license.php

  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "Fat.h"

STATIC EFI_UNICODE_COLLATION_PROTOCOL  *mUnicodeCollationInterface = NULL;

typedef
BOOLEAN
(* SEARCH_LANG_CODE) (
  IN CONST CHAR8    *Languages,
  IN CONST CHAR8    *MatchLangCode
  );

struct _UNICODE_INTERFACE {
  CHAR16            *VariableName;
  CHAR8             *DefaultLangCode;
  SEARCH_LANG_CODE  SearchLangCode;
  EFI_GUID          *UnicodeProtocolGuid;
};

typedef struct _UNICODE_INTERFACE UNICODE_INTERFACE;

STATIC
BOOLEAN
SearchIso639LangCode (
  IN CONST CHAR8    *Languages,
  IN CONST CHAR8    *MatchLangCode
  )
{
  CONST CHAR8       *LangCode;

  for (LangCode = Languages; *LangCode != '\0'; LangCode += 3) {
    if (CompareMem (LangCode, MatchLangCode, 3) == 0) {
      return TRUE;
    }
  }

  return FALSE;
}

STATIC
BOOLEAN
SearchRfc3066LangCode (
  IN CONST CHAR8    *Languages,
  IN CONST CHAR8    *MatchLangCode
  )
{
  CHAR8       *SubStr;
  CHAR8       Terminal;

  SubStr = AsciiStrStr (Languages, MatchLangCode);
  if (SubStr == NULL) {
    return FALSE;
  }

  if (SubStr != Languages && *(SubStr - 1) != ';') {
    return FALSE;
  }

  Terminal = *(SubStr + AsciiStrLen (MatchLangCode));
  if (Terminal != '\0' && Terminal != ';') {
    return FALSE;
  }

  return TRUE;
}

GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mIso639Lang = {
  L"Lang",
  (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultLang),
  SearchIso639LangCode,
  &gEfiUnicodeCollationProtocolGuid,
};

GLOBAL_REMOVE_IF_UNREFERENCED UNICODE_INTERFACE mRfc3066Lang = {
  L"PlatformLang",
  (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang),
  SearchRfc3066LangCode,
  &gEfiUnicodeCollation2ProtocolGuid,
};

STATIC
EFI_STATUS
InitializeUnicodeCollationSupportWithConfig (
  IN EFI_HANDLE         AgentHandle,
  IN UNICODE_INTERFACE  *UnicodeInterface
  )
{
  EFI_STATUS                      Status;
  CHAR8                           Buffer[100];
  UINTN                           BufferSize;
  UINTN                           Index;
  CHAR8                           *LangCode;
  UINTN                           NoHandles;
  EFI_HANDLE                      *Handles;
  EFI_UNICODE_COLLATION_PROTOCOL  *Uci;

  LangCode   = Buffer;
  BufferSize = sizeof (Buffer);
  Status = gRT->GetVariable (
                  UnicodeInterface->VariableName,
                  &gEfiGlobalVariableGuid,
                  NULL,
                  &BufferSize,
                  Buffer
                  );
  if (EFI_ERROR (Status)) {
    LangCode = UnicodeInterface->DefaultLangCode;
  }

  Status = gBS->LocateHandleBuffer (
                  ByProtocol,
                  UnicodeInterface->UnicodeProtocolGuid,
                  NULL,
                  &NoHandles,
                  &Handles
                  );
  if (EFI_ERROR (Status)) {
    return Status;
  }

  for (Index = 0; Index < NoHandles; Index++) {
    //
    // Open Unicode Collation Protocol
    //
    Status = gBS->OpenProtocol (
                    Handles[Index],
                    UnicodeInterface->UnicodeProtocolGuid,
                    (VOID **) &Uci,
                    AgentHandle,
                    NULL,
                    EFI_OPEN_PROTOCOL_GET_PROTOCOL
                    );
    if (EFI_ERROR (Status)) {
      continue;
    }

    if (UnicodeInterface->SearchLangCode (Uci->SupportedLanguages, LangCode)) {
      mUnicodeCollationInterface = Uci;
      break;
    }
  }

  FreePool (Handles);

  return (mUnicodeCollationInterface != NULL)? EFI_SUCCESS : EFI_NOT_FOUND;
}

/**
  Initialize Unicode Collation support.

  This function searches Initialized Unicode Collation support based on PCDs:
  PcdUnicodeCollation2Support and PcdUnicodeCollationSupport.
  It first tries to locate Unicode Collation 2 protocol and matches it with current
  platform language code. If for any reason the first attempt fails, it then tries to
  use Unicode Collation Protocol.

  @param  AgentHandle          The handle used to open Unicode Collation (2) protocol.

  @retval EFI_SUCCESS          The Unicode Collation (2) protocol has been successfully located.
  @retval Others               The Unicode Collation (2) protocol has not been located.

**/
EFI_STATUS
InitializeUnicodeCollationSupport (
  IN EFI_HANDLE    AgentHandle
  )
{

  EFI_STATUS       Status;

  Status = EFI_UNSUPPORTED;

  //
  // First try to use RFC 3066 Unicode Collation 2 Protocol.
  //
  if (FeaturePcdGet (PcdUnicodeCollation2Support)) {
    Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mRfc3066Lang);
  }

  //
  // If the attempt to use Unicode Collation 2 Protocol fails, then we fall back
  // on the ISO 639-2 Unicode Collation Protocol.
  //
  if (FeaturePcdGet (PcdUnicodeCollationSupport) && EFI_ERROR (Status)) {
    Status = InitializeUnicodeCollationSupportWithConfig (AgentHandle, &mIso639Lang);
  }

  return Status;
}

/**
  Performs a case-insensitive comparison of two Null-terminated Unicode strings.

  @param  S1                   A pointer to a Null-terminated Unicode string.
  @param  S2                   A pointer to a Null-terminated Unicode string.

  @retval 0                    S1 is equivalent to S2.
  @retval >0                   S1 is lexically greater than S2.
  @retval <0                   S1 is lexically less than S2.
**/
INTN
FatStriCmp (
  IN CHAR16       *S1,
  IN CHAR16       *S2
  )
{
  ASSERT (StrSize (S1) != 0);
  ASSERT (StrSize (S2) != 0);
  ASSERT (mUnicodeCollationInterface != NULL);

  return mUnicodeCollationInterface->StriColl (
                                       mUnicodeCollationInterface,
                                       S1,
                                       S2
                                       );
}


/**
  Uppercase a string.

  @param  Str                   The string which will be upper-cased.

  @return None.

**/
VOID
FatStrUpr (
  IN OUT CHAR16   *String
  )
{
  ASSERT (StrSize (String) != 0);
  ASSERT (mUnicodeCollationInterface != NULL);

  mUnicodeCollationInterface->StrUpr (mUnicodeCollationInterface, String);
}


/**
  Lowercase a string

  @param  Str                   The string which will be lower-cased.

  @return None

**/
VOID
FatStrLwr (
  IN OUT CHAR16   *String
  )
{
  ASSERT (StrSize (String) != 0);
  ASSERT (mUnicodeCollationInterface != NULL);

  mUnicodeCollationInterface->StrLwr (mUnicodeCollationInterface, String);
}


/**
  Convert FAT string to unicode string.

  @param  FatSize               The size of FAT string.
  @param  Fat                   The FAT string.
  @param  String                The unicode string.

  @return None.

**/
VOID
FatFatToStr (
  IN  UINTN                            FatSize,
  IN  CHAR8                            *Fat,
  OUT CHAR16                           *String
  )
{
  ASSERT (Fat != NULL);
  ASSERT (String != NULL);
  ASSERT (((UINTN) String & 0x01) == 0);
  ASSERT (mUnicodeCollationInterface != NULL);

  mUnicodeCollationInterface->FatToStr (mUnicodeCollationInterface, FatSize, Fat, String);
}


/**
  Convert unicode string to Fat string.

  @param  String                The unicode string.
  @param  FatSize               The size of the FAT string.
  @param  Fat                   The FAT string.

  @retval TRUE                  Convert successfully.
  @retval FALSE                 Convert error.

**/
BOOLEAN
FatStrToFat (
  IN  CHAR16                          *String,
  IN  UINTN                           FatSize,
  OUT CHAR8                           *Fat
  )
{
  ASSERT (Fat != NULL);
  ASSERT (StrSize (String) != 0);
  ASSERT (mUnicodeCollationInterface != NULL);

  return mUnicodeCollationInterface->StrToFat (
                                       mUnicodeCollationInterface,
                                       String,
                                       FatSize,
                                       Fat
                                       );
}