]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Library/UefiHiiLib/HiiString.c
Fix a minor issue to ensure the converted RFC 3066 language is NULL-terminated.
[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 adds the string into String Package of each language
167 supported by the package list.
168
169 If String is NULL, then ASSERT.
170 If StringId is NULL, the ASSERT.
171 If PackageList could not be found in the default HII database, then ASSERT.
172
173 @param PackageList Handle of the package list where this string will
174 be added.
175 @param StringId On return, contains the new strings id, which is
176 unique within PackageList.
177 @param String Points to the new null-terminated string.
178
179 @retval EFI_SUCCESS The new string was added successfully.
180 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
181
182 **/
183 EFI_STATUS
184 EFIAPI
185 HiiLibNewString (
186 IN EFI_HII_HANDLE PackageList,
187 OUT EFI_STRING_ID *StringId,
188 IN CONST EFI_STRING String
189 )
190 {
191 EFI_STATUS Status;
192 CHAR8 *Languages;
193 CHAR8 *LangStrings;
194 CHAR8 *Lang;
195
196 ASSERT (String != NULL);
197 ASSERT (StringId != NULL);
198
199 Status = EFI_SUCCESS;
200
201 Languages = HiiLibGetSupportedLanguages (PackageList);
202 ASSERT (Languages != NULL);
203 //
204 // Allocate working buffer to contain substring of Languages.
205 //
206 Lang = AllocatePool (AsciiStrSize (Languages));
207 ASSERT (Lang != NULL);
208
209 LangStrings = Languages;
210 while (*LangStrings != 0) {
211 HiiLibGetNextLanguage (&LangStrings, Lang);
212
213 //
214 // For each language supported by the package,
215 // a string token is created.
216 //
217 Status = mHiiStringProt->NewString (
218 mHiiStringProt,
219 PackageList,
220 StringId,
221 Lang,
222 NULL,
223 String,
224 NULL
225 );
226 if (EFI_ERROR (Status)) {
227 break;
228 }
229 }
230
231 FreePool (Lang);
232 FreePool (Languages);
233
234 return Status;
235
236 }
237
238
239 /**
240 This function update the specified string in String Package of each language
241 supported by the package list.
242
243 If String is NULL, then ASSERT.
244 If PackageList could not be found in the default HII database, then ASSERT.
245 If StringId is not found in PackageList, then ASSERT.
246
247 @param PackageList Handle of the package list where this string will
248 be added.
249 @param StringId Ths String Id to be updated.
250 @param String Points to the new null-terminated string.
251
252 @retval EFI_SUCCESS The new string was added successfully.
253 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
254
255 **/
256 EFI_STATUS
257 EFIAPI
258 HiiLibSetString (
259 IN EFI_HII_HANDLE PackageList,
260 IN EFI_STRING_ID StringId,
261 IN CONST EFI_STRING String
262 )
263 {
264 EFI_STATUS Status;
265 CHAR8 *Languages;
266 CHAR8 *LangStrings;
267 CHAR8 *Lang;
268
269 ASSERT (IsHiiHandleRegistered (PackageList));
270
271 Status = EFI_SUCCESS;
272
273 Languages = HiiLibGetSupportedLanguages (PackageList);
274 ASSERT (Languages != NULL);
275
276 //
277 // Allocate working buffer to contain substring of Languages.
278 //
279 Lang = AllocatePool (AsciiStrSize (Languages));
280 ASSERT (Lang != NULL);
281
282 LangStrings = Languages;
283 while (*LangStrings != 0) {
284 HiiLibGetNextLanguage (&LangStrings, Lang);
285
286 //
287 // For each language supported by the package,
288 // the string is updated.
289 //
290 Status = mHiiStringProt->SetString (
291 mHiiStringProt,
292 PackageList,
293 StringId,
294 Lang,
295 String,
296 NULL
297 );
298 if (EFI_ERROR (Status)) {
299 break;
300 }
301 }
302
303 FreePool (Lang);
304 FreePool (Languages);
305
306 return Status;
307 }
308
309
310 /**
311 Get the string given the StringId and String package Producer's Guid. The caller
312 is responsible to free the *String.
313
314 If PackageList with the matching ProducerGuid is not found, then ASSERT.
315 If PackageList with the matching ProducerGuid is found but no String is
316 specified by StringId is found, then ASSERT.
317
318 @param ProducerGuid The Guid of String package list.
319 @param StringId The String ID.
320 @param String The output string.
321
322 @retval EFI_SUCCESS Operation is successful.
323 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
324
325 **/
326 EFI_STATUS
327 EFIAPI
328 HiiLibGetStringFromToken (
329 IN EFI_GUID *ProducerGuid,
330 IN EFI_STRING_ID StringId,
331 OUT EFI_STRING *String
332 )
333 {
334 EFI_STATUS Status;
335 UINTN Index;
336 UINTN HandleBufferLen;
337 EFI_HII_HANDLE *HiiHandleBuffer;
338 EFI_GUID Guid;
339
340 Status = HiiLibGetHiiHandles (&HandleBufferLen, &HiiHandleBuffer);
341 if (HiiHandleBuffer == NULL) {
342 return EFI_NOT_FOUND;
343 }
344 for (Index = 0; Index < (HandleBufferLen / sizeof (EFI_HII_HANDLE)); Index++) {
345 Status = HiiLibExtractGuidFromHiiHandle (HiiHandleBuffer[Index], &Guid);
346 if (EFI_ERROR(Status)) {
347 return Status;
348 }
349 if (CompareGuid (&Guid, ProducerGuid)) {
350 break;
351 }
352 }
353
354 if (Index >= (HandleBufferLen / sizeof (EFI_HII_HANDLE))) {
355 //
356 // If PackageList with the matching ProducerGuid is not found, then ASSERT.
357 //
358 ASSERT (FALSE);
359 Status = EFI_NOT_FOUND;
360 goto Out;
361 }
362
363 Status = HiiLibGetStringFromHandle (HiiHandleBuffer[Index], StringId, String);
364
365 Out:
366 FreePool (HiiHandleBuffer);
367
368 return Status;
369 }
370
371 /**
372 This function try to retrieve string from String package of current language.
373 If fails, it try to retrieve string from String package of first language it support.
374
375 If StringSize is NULL, then ASSERT.
376 If String is NULL and *StringSize is not 0, then ASSERT.
377 If PackageList could not be found in the default HII database, then ASSERT.
378 If StringId is not found in PackageList, then ASSERT.
379
380 @param PackageList The package list in the HII database to search for
381 the specified string.
382 @param StringId The string's id, which is unique within
383 PackageList.
384 @param String Points to the new null-terminated string.
385 @param StringSize On entry, points to the size of the buffer pointed
386 to by String, in bytes. On return, points to the
387 length of the string, in bytes.
388
389 @retval EFI_SUCCESS The string was returned successfully.
390 @retval EFI_NOT_FOUND The string specified by StringId is not available.
391 @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringLength is too small
392 to hold the string.
393
394 **/
395 EFI_STATUS
396 EFIAPI
397 HiiLibGetString (
398 IN EFI_HII_HANDLE PackageList,
399 IN EFI_STRING_ID StringId,
400 OUT EFI_STRING String,
401 IN OUT UINTN *StringSize
402 )
403 {
404 EFI_STATUS Status;
405 CHAR8 *Languages;
406 CHAR8 *CurrentLang;
407 CHAR8 *BestLanguage;
408
409 ASSERT (StringSize != NULL);
410 ASSERT (!(*StringSize != 0 && String == NULL));
411 ASSERT (IsHiiHandleRegistered (PackageList));
412
413 Languages = HiiLibGetSupportedLanguages (PackageList);
414 ASSERT (Languages != NULL);
415
416 CurrentLang = GetEfiGlobalVariable (L"PlatformLang");
417
418 Status = EFI_NOT_FOUND;
419 BestLanguage = GetBestLanguage (
420 Languages,
421 FALSE,
422 (CurrentLang != NULL) ? CurrentLang : "",
423 (CHAR8 *) PcdGetPtr (PcdUefiVariableDefaultPlatformLang),
424 Languages,
425 NULL
426 );
427 if (BestLanguage != NULL ) {
428 Status = mHiiStringProt->GetString (
429 mHiiStringProt,
430 BestLanguage,
431 PackageList,
432 StringId,
433 String,
434 StringSize,
435 NULL
436 );
437 FreePool (BestLanguage);
438 }
439 if (CurrentLang != NULL) {
440 FreePool (CurrentLang);
441 }
442 FreePool (Languages);
443
444 return Status;
445 }
446
447
448 /**
449 Get string specified by StringId form the HiiHandle. The caller
450 is responsible to free the *String.
451
452 If String is NULL, then ASSERT.
453 If HiiHandle could not be found in the default HII database, then ASSERT.
454 If StringId is not found in PackageList, then ASSERT.
455
456 @param HiiHandle The HII handle of package list.
457 @param StringId The String ID.
458 @param String The output string.
459
460 @retval EFI_NOT_FOUND String is not found.
461 @retval EFI_SUCCESS Operation is successful.
462 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
463
464 **/
465 EFI_STATUS
466 EFIAPI
467 HiiLibGetStringFromHandle (
468 IN EFI_HII_HANDLE HiiHandle,
469 IN EFI_STRING_ID StringId,
470 OUT EFI_STRING *String
471 )
472 {
473 EFI_STATUS Status;
474 UINTN StringSize;
475
476 ASSERT (String != NULL);
477
478 StringSize = HII_LIB_DEFAULT_STRING_SIZE;
479 *String = AllocateZeroPool (StringSize);
480 if (*String == NULL) {
481 return EFI_OUT_OF_RESOURCES;
482 }
483
484 Status = HiiLibGetString (HiiHandle, StringId, *String, &StringSize);
485 if (Status == EFI_BUFFER_TOO_SMALL) {
486 FreePool (*String);
487 *String = AllocateZeroPool (StringSize);
488 if (*String == NULL) {
489 return EFI_OUT_OF_RESOURCES;
490 }
491 Status = HiiLibGetString (HiiHandle, StringId, *String, &StringSize);
492 }
493
494 return Status;
495 }
496
497
498
499 /**
500 Convert language code from RFC3066 to ISO639-2.
501
502 @param LanguageRfc3066 RFC3066 language code.
503 @param LanguageIso639 ISO639-2 language code.
504
505 @retval EFI_SUCCESS Language code converted.
506 @retval EFI_NOT_FOUND Language code not found.
507
508 **/
509 EFI_STATUS
510 EFIAPI
511 ConvertRfc3066LanguageToIso639Language (
512 IN CHAR8 *LanguageRfc3066,
513 OUT CHAR8 *LanguageIso639
514 )
515 {
516 UINTN Index;
517
518 if ((LanguageRfc3066[2] != '-') && (LanguageRfc3066[2] != 0)) {
519 CopyMem (LanguageIso639, LanguageRfc3066, 3);
520 return EFI_SUCCESS;
521 }
522
523 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
524 if (CompareMem (LanguageRfc3066, &Iso639ToRfc3066ConversionTable[Index + 3], 2) == 0) {
525 CopyMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3);
526 return EFI_SUCCESS;
527 }
528 }
529
530 return EFI_NOT_FOUND;
531 }
532
533
534 /**
535 Convert language code from ISO639-2 to RFC3066 and return the converted language.
536 Caller is responsible for freeing the allocated buffer.
537
538 LanguageIso639 contain a single ISO639-2 code such as
539 "eng" or "fra".
540
541 If LanguageIso639 is NULL, then ASSERT.
542 If LanguageRfc3066 is NULL, then ASSERT.
543
544 @param LanguageIso639 ISO639-2 language code.
545
546 @return the allocated buffer or NULL, if the language is not found.
547
548 **/
549 CHAR8*
550 EFIAPI
551 ConvertIso639LanguageToRfc3066Language (
552 IN CONST CHAR8 *LanguageIso639
553 )
554 {
555 UINTN Index;
556 CHAR8 *Rfc3066Language;
557
558 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
559 if (CompareMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3) == 0) {
560 Rfc3066Language = AllocateZeroPool (3);
561 if (Rfc3066Language != NULL) {
562 Rfc3066Language = CopyMem (Rfc3066Language, &Iso639ToRfc3066ConversionTable[Index + 3], 2);
563 }
564 return Rfc3066Language;
565 }
566 }
567
568 return NULL;
569 }
570
571 /**
572 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
573 be converted to "engfra".
574
575 @param SupportedLanguages The RFC3066 language list.
576
577 @return The ISO639-2 language list.
578
579 **/
580 CHAR8 *
581 EFIAPI
582 Rfc3066ToIso639 (
583 CHAR8 *SupportedLanguages
584 )
585 {
586 CHAR8 *Languages;
587 CHAR8 *ReturnValue;
588 CHAR8 *LangCodes;
589 CHAR8 *LangRfc3066;
590 CHAR8 LangIso639[ISO_639_2_ENTRY_SIZE];
591 UINTN LanguageSize;
592 EFI_STATUS Status;
593
594 LanguageSize = AsciiStrSize (SupportedLanguages);
595 ReturnValue = AllocateZeroPool (LanguageSize);
596 if (ReturnValue == NULL) {
597 return ReturnValue;
598 }
599
600 //
601 // Allocate working buffer to contain substring in SupportedLanguages;
602 //
603 LangRfc3066 = AllocatePool (LanguageSize);
604 if (LangRfc3066 == NULL) {
605 FreePool (ReturnValue);
606 return NULL;
607 }
608 Languages = ReturnValue;
609 LangCodes = SupportedLanguages;
610 while (*LangCodes != 0) {
611 HiiLibGetNextLanguage (&LangCodes, LangRfc3066);
612
613 Status = ConvertRfc3066LanguageToIso639Language (LangRfc3066, LangIso639);
614 if (!EFI_ERROR (Status)) {
615 CopyMem (Languages, LangIso639, 3);
616 Languages = Languages + 3;
617 }
618 }
619
620 FreePool (LangRfc3066);
621 return ReturnValue;
622 }
623
624