]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiHiiLib/HiiString.c
2c317e5de355c1501ba2ac1b4eab0f282396fe45
[mirror_edk2.git] / MdeModulePkg / Library / UefiHiiLib / HiiString.c
1 /** @file
2 HII Library implementation that uses DXE protocols and services.
3
4 Copyright (c) 2006 - 2008, 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
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15
16 #include "InternalHiiLib.h"
17
18
19 //
20 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
21 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
22 // The last 2 CHAR8 values are the ISO 639-1 code.
23 //
24 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 Iso639ToRfc3066ConversionTable[] =
25 "\
26 aaraa\
27 abkab\
28 afraf\
29 amham\
30 araar\
31 asmas\
32 aymay\
33 azeaz\
34 bakba\
35 belbe\
36 benbn\
37 bihbh\
38 bisbi\
39 bodbo\
40 brebr\
41 bulbg\
42 catca\
43 cescs\
44 corkw\
45 cosco\
46 cymcy\
47 danda\
48 deude\
49 dzodz\
50 ellel\
51 engen\
52 epoeo\
53 estet\
54 euseu\
55 faofo\
56 fasfa\
57 fijfj\
58 finfi\
59 frafr\
60 fryfy\
61 gaiga\
62 gdhgd\
63 glggl\
64 grngn\
65 gujgu\
66 hauha\
67 hebhe\
68 hinhi\
69 hrvhr\
70 hunhu\
71 hyehy\
72 ikuiu\
73 ileie\
74 inaia\
75 indid\
76 ipkik\
77 islis\
78 itait\
79 jawjw\
80 jpnja\
81 kalkl\
82 kankn\
83 kasks\
84 katka\
85 kazkk\
86 khmkm\
87 kinrw\
88 kirky\
89 korko\
90 kurku\
91 laolo\
92 latla\
93 lavlv\
94 linln\
95 litlt\
96 ltzlb\
97 malml\
98 marmr\
99 mkdmk\
100 mlgmg\
101 mltmt\
102 molmo\
103 monmn\
104 mrimi\
105 msams\
106 myamy\
107 nauna\
108 nepne\
109 nldnl\
110 norno\
111 ocioc\
112 ormom\
113 panpa\
114 polpl\
115 porpt\
116 pusps\
117 quequ\
118 rohrm\
119 ronro\
120 runrn\
121 rusru\
122 sagsg\
123 sansa\
124 sinsi\
125 slksk\
126 slvsl\
127 smise\
128 smosm\
129 snasn\
130 sndsd\
131 somso\
132 sotst\
133 spaes\
134 sqisq\
135 srpsr\
136 sswss\
137 sunsu\
138 swasw\
139 swesv\
140 tamta\
141 tattt\
142 telte\
143 tgktg\
144 tgltl\
145 thath\
146 tsnts\
147 tuktk\
148 twitw\
149 uigug\
150 ukruk\
151 urdur\
152 uzbuz\
153 vievi\
154 volvo\
155 wolwo\
156 xhoxh\
157 yidyi\
158 zhaza\
159 zhozh\
160 zulzu\
161 ";
162
163
164
165 /**
166 This function create a new string in String Package or updates an existing
167 string in a String Package. If StringId is 0, then a new string is added to
168 a String Package. If StringId is not zero, then a string in String Package is
169 updated. If SupportedLanguages is NULL, then the string is added or updated
170 for all the languages that the String Package supports. If SupportedLanguages
171 is not NULL, then the string is added or updated for the set of languages
172 specified by SupportedLanguages.
173
174 If HiiHandle is NULL, then ASSERT().
175 If String is NULL, then ASSERT().
176
177 @param[in] HiiHandle A handle that was previously registered in the
178 HII Database.
179 @param[in] StringId If zero, then a new string is created in the
180 String Package associated with HiiHandle. If
181 non-zero, then the string specified by StringId
182 is updated in the String Package associated
183 with HiiHandle.
184 @param[in] String A pointer to the Null-terminated Unicode string
185 to add or update in the String Package associated
186 with HiiHandle.
187 @param[in] SupportedLanguages A pointer to a Null-terminated ASCII string of
188 language codes. If this parameter is NULL, then
189 String is added or updated in the String Package
190 associated with HiiHandle for all the languages
191 that the String Package supports. If this
192 parameter is not NULL, then then String is added
193 or updated in the String Package associated with
194 HiiHandle for the set oflanguages specified by
195 SupportedLanguages. The format of
196 SupportedLanguages must follow the language
197 format assumed the HII Database.
198
199 @retval 0 The string could not be added or updated in the String Package.
200 @retval Other The EFI_STRING_ID of the newly added or updated string.
201
202 **/
203 EFI_STRING_ID
204 EFIAPI
205 HiiSetString (
206 IN EFI_HII_HANDLE HiiHandle,
207 IN EFI_STRING_ID StringId, OPTIONAL
208 IN CONST EFI_STRING String,
209 IN CONST CHAR8 *SupportedLanguages OPTIONAL
210 )
211 {
212 EFI_STATUS Status;
213 CHAR8 *AllocatedLanguages;
214 CHAR8 *Supported;
215 CHAR8 *Language;
216 EFI_STRING_ID NewStringId;
217
218 ASSERT (HiiHandle != NULL);
219 ASSERT (String != NULL);
220
221 if (SupportedLanguages == NULL) {
222 //
223 // Retrieve the languages that the package specified by HiiHandle supports
224 //
225 AllocatedLanguages = HiiGetSupportedLanguages (HiiHandle);
226 } else {
227 //
228 // Allocate a copy of the SupportLanguages string that passed in
229 //
230 AllocatedLanguages = AllocateCopyPool (AsciiStrLen (SupportedLanguages), SupportedLanguages);
231 }
232
233 //
234 // If there are not enough resources for the supported languages string, then return a StringId of 0
235 //
236 if (AllocatedLanguages == NULL) {
237 return (EFI_STRING_ID)(0);
238 }
239
240 NewStringId = 0;
241 Status = EFI_INVALID_PARAMETER;
242 //
243 // Loop through each language that the string supports
244 //
245 for (Supported = AllocatedLanguages; *Supported != '\0'; ) {
246 //
247 // Cache a pointer to the beginning of the current language in the list of languages
248 //
249 Language = Supported;
250
251 //
252 // Search for the next language seperator and replace it with a Null-terminator
253 //
254 for (; *Supported != 0 && *Supported != ';'; Supported++);
255 if (*Supported != 0) {
256 *(Supported++) = '\0';
257 }
258
259 //
260 // If StringId is 0, then call NewString(). Otherwise, call SetString()
261 //
262 if (StringId == (EFI_STRING_ID)(0)) {
263 Status = gHiiString->NewString (gHiiString, HiiHandle, &NewStringId, Language, NULL, String, NULL);
264 } else {
265 Status = gHiiString->SetString (gHiiString, HiiHandle, StringId, Language, String, NULL);
266 }
267
268 //
269 // If there was an error, then break out of the loop and return a StringId of 0
270 //
271 if (EFI_ERROR (Status)) {
272 break;
273 }
274 }
275
276 //
277 // Free the buffer of supported languages
278 //
279 FreePool (AllocatedLanguages);
280
281 if (EFI_ERROR (Status)) {
282 return (EFI_STRING_ID)(0);
283 } else if (StringId == (EFI_STRING_ID)(0)) {
284 return NewStringId;
285 } else {
286 return StringId;
287 }
288 }
289
290
291 /**
292 Retrieves a string from a string package names by GUID in a specific language.
293 If the language is not specified, then a string from a string package in the
294 current platform language is retrieved. If the string can not be retrieved
295 using the specified language or the current platform language, then the string
296 is retrieved from the string package in the first language the string package
297 supports. The returned string is allocated using AllocatePool(). The caller
298 is responsible for freeing the allocated buffer using FreePool().
299
300 If PackageListGuid is NULL, then ASSERT().
301 If StringId is 0, then ASSERT.
302
303 @param[in] PackageListGuid The GUID of a package list that was previously
304 registered in the HII Database.
305 @param[in] StringId The identifier of the string to retrieved from the
306 string package associated with PackageListGuid.
307 @param[in] Language The language of the string to retrieve. If this
308 parameter is NULL, then the current platform
309 language is used. The format of Language must
310 follow the language format assumed the HII Database.
311
312 @retval NULL The package list specified by PackageListGuid is not present in the
313 HII Database.
314 @retval NULL The string specified by StringId is not present in the string package.
315 @retval Other The string was returned.
316
317 **/
318 EFI_STRING
319 EFIAPI
320 HiiGetPackageString (
321 IN CONST EFI_GUID *PackageListGuid,
322 IN EFI_STRING_ID StringId,
323 IN CONST CHAR8 *Language OPTIONAL
324 )
325 {
326 EFI_HANDLE *HiiHandleBuffer;
327 EFI_HANDLE HiiHandle;
328
329 ASSERT (PackageListGuid != NULL);
330
331 HiiHandleBuffer = HiiGetHiiHandles (PackageListGuid);
332 if (HiiHandleBuffer == NULL) {
333 return NULL;
334 }
335
336 HiiHandle = HiiHandleBuffer[0];
337 FreePool (HiiHandleBuffer);
338
339 return HiiGetString (HiiHandle, StringId, Language);
340 }
341
342 /**
343 Retrieves a string from a string package in a specific language. If the language
344 is not specified, then a string from a string package in the current platform
345 language is retrieved. If the string can not be retrieved using the specified
346 language or the current platform language, then the string is retrieved from
347 the string package in the first language the string package supports. The
348 returned string is allocated using AllocatePool(). The caller is responsible
349 for freeing the allocated buffer using FreePool().
350
351 If HiiHandle is NULL, then ASSERT().
352 If StringId is 0, then ASSET.
353
354 @param[in] HiiHandle A handle that was previously registered in the HII Database.
355 @param[in] StringId The identifier of the string to retrieved from the string
356 package associated with HiiHandle.
357 @param[in] Language The language of the string to retrieve. If this parameter
358 is NULL, then the current platform language is used. The
359 format of Language must follow the language format assumed
360 the HII Database.
361
362 @retval NULL The string specified by StringId is not present in the string package.
363 @retval Other The string was returned.
364
365 **/
366 EFI_STRING
367 EFIAPI
368 HiiGetString (
369 IN EFI_HII_HANDLE HiiHandle,
370 IN EFI_STRING_ID StringId,
371 IN CONST CHAR8 *Language OPTIONAL
372 )
373 {
374 EFI_STATUS Status;
375 UINTN StringSize;
376 CHAR16 TempString;
377 EFI_STRING String;
378 CHAR8 *SupportedLanguages;
379 CHAR8 *PlatformLanguage;
380 CHAR8 *BestLanguage;
381
382 ASSERT (HiiHandle != NULL);
383 ASSERT (StringId != 0);
384
385 //
386 // Initialize all allocated buffers to NULL
387 //
388 SupportedLanguages = NULL;
389 PlatformLanguage = NULL;
390 BestLanguage = NULL;
391 String = NULL;
392
393 //
394 // Get the languages that the package specified by HiiHandle supports
395 //
396 SupportedLanguages = HiiGetSupportedLanguages (HiiHandle);
397 if (SupportedLanguages == NULL) {
398 goto Error;
399 }
400
401 //
402 // Get the current platform language setting
403 //
404 PlatformLanguage = GetEfiGlobalVariable (L"PlatformLang");
405
406 //
407 // If Languag is NULL, then set it to an empty string, so it will be
408 // skipped by GetBestLanguage()
409 //
410 if (Language == NULL) {
411 Language = "";
412 }
413
414 //
415 // Get the best matching language from SupportedLanguages
416 //
417 BestLanguage = GetBestLanguage (
418 SupportedLanguages,
419 FALSE, // RFC 4646 mode
420 Language, // Highest priority
421 PlatformLanguage != NULL ? PlatformLanguage : "", // Next highest priority
422 SupportedLanguages, // Lowest priority
423 NULL
424 );
425 if (BestLanguage == NULL) {
426 goto Error;
427 }
428
429 //
430 // Retrieve the size of the string in the string package for the BestLanguage
431 //
432 StringSize = 0;
433 Status = gHiiString->GetString (
434 gHiiString,
435 BestLanguage,
436 HiiHandle,
437 StringId,
438 &TempString,
439 &StringSize,
440 NULL
441 );
442 //
443 // If GetString() returns EFI_SUCCESS for a zero size,
444 // then there are no supported languages registered for HiiHandle. If GetString()
445 // returns an error other than EFI_BUFFER_TOO_SMALL, then HiiHandle is not present
446 // in the HII Database
447 //
448 if (Status != EFI_BUFFER_TOO_SMALL) {
449 goto Error;
450 }
451
452 //
453 // Allocate a buffer for the return string
454 //
455 String = AllocateZeroPool (StringSize);
456 if (String == NULL) {
457 goto Error;
458 }
459
460 //
461 // Retrieve the string from the string package
462 //
463 Status = gHiiString->GetString (
464 gHiiString,
465 BestLanguage,
466 HiiHandle,
467 StringId,
468 String,
469 &StringSize,
470 NULL
471 );
472 if (EFI_ERROR (Status)) {
473 //
474 // Free the buffer and return NULL if the supported languages can not be retrieved.
475 //
476 FreePool (String);
477 String = NULL;
478 }
479
480 Error:
481 //
482 // Free allocated buffers
483 //
484 if (SupportedLanguages != NULL) {
485 FreePool (SupportedLanguages);
486 }
487 if (PlatformLanguage != NULL) {
488 FreePool (PlatformLanguage);
489 }
490 if (BestLanguage != NULL) {
491 FreePool (BestLanguage);
492 }
493
494 //
495 // Return the Null-terminated Unicode string
496 //
497 return String;
498 }
499
500 /**
501 Convert language code from RFC3066 to ISO639-2.
502
503 @param LanguageRfc3066 RFC3066 language code.
504 @param LanguageIso639 ISO639-2 language code.
505
506 @retval EFI_SUCCESS Language code converted.
507 @retval EFI_NOT_FOUND Language code not found.
508
509 **/
510 EFI_STATUS
511 EFIAPI
512 ConvertRfc3066LanguageToIso639Language (
513 IN CHAR8 *LanguageRfc3066,
514 OUT CHAR8 *LanguageIso639
515 )
516 {
517 UINTN Index;
518
519 if ((LanguageRfc3066[2] != '-') && (LanguageRfc3066[2] != 0)) {
520 CopyMem (LanguageIso639, LanguageRfc3066, 3);
521 return EFI_SUCCESS;
522 }
523
524 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
525 if (CompareMem (LanguageRfc3066, &Iso639ToRfc3066ConversionTable[Index + 3], 2) == 0) {
526 CopyMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3);
527 return EFI_SUCCESS;
528 }
529 }
530
531 return EFI_NOT_FOUND;
532 }
533
534
535 /**
536 Convert language code from ISO639-2 to RFC3066 and return the converted language.
537 Caller is responsible for freeing the allocated buffer.
538
539 LanguageIso639 contain a single ISO639-2 code such as
540 "eng" or "fra".
541
542 If LanguageIso639 is NULL, then ASSERT.
543 If LanguageRfc3066 is NULL, then ASSERT.
544
545 @param LanguageIso639 ISO639-2 language code.
546
547 @return the allocated buffer or NULL, if the language is not found.
548
549 **/
550 CHAR8*
551 EFIAPI
552 ConvertIso639LanguageToRfc3066Language (
553 IN CONST CHAR8 *LanguageIso639
554 )
555 {
556 UINTN Index;
557 CHAR8 *Rfc3066Language;
558
559 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
560 if (CompareMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3) == 0) {
561 Rfc3066Language = AllocateZeroPool (3);
562 if (Rfc3066Language != NULL) {
563 Rfc3066Language = CopyMem (Rfc3066Language, &Iso639ToRfc3066ConversionTable[Index + 3], 2);
564 }
565 return Rfc3066Language;
566 }
567 }
568
569 return NULL;
570 }
571
572 /**
573 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
574 be converted to "engfra".
575
576 @param SupportedLanguages The RFC3066 language list.
577
578 @return The ISO639-2 language list.
579
580 **/
581 CHAR8 *
582 EFIAPI
583 Rfc3066ToIso639 (
584 CHAR8 *SupportedLanguages
585 )
586 {
587 CHAR8 *Languages;
588 CHAR8 *ReturnValue;
589 CHAR8 *LangCodes;
590 CHAR8 *LangRfc3066;
591 CHAR8 LangIso639[ISO_639_2_ENTRY_SIZE];
592 UINTN LanguageSize;
593 EFI_STATUS Status;
594
595 LanguageSize = AsciiStrSize (SupportedLanguages);
596 ReturnValue = AllocateZeroPool (LanguageSize);
597 if (ReturnValue == NULL) {
598 return ReturnValue;
599 }
600
601 //
602 // Allocate working buffer to contain substring in SupportedLanguages;
603 //
604 LangRfc3066 = AllocatePool (LanguageSize);
605 if (LangRfc3066 == NULL) {
606 FreePool (ReturnValue);
607 return NULL;
608 }
609 Languages = ReturnValue;
610 LangCodes = SupportedLanguages;
611 while (*LangCodes != 0) {
612 HiiLibGetNextLanguage (&LangCodes, LangRfc3066);
613
614 Status = ConvertRfc3066LanguageToIso639Language (LangRfc3066, LangIso639);
615 if (!EFI_ERROR (Status)) {
616 CopyMem (Languages, LangIso639, 3);
617 Languages = Languages + 3;
618 }
619 }
620
621 FreePool (LangRfc3066);
622 return ReturnValue;
623 }
624
625