]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/HiiLib/HiiString.c
05e9aa8b6829b27bb9ed6ea575d7dabad9213d70
[mirror_edk2.git] / MdePkg / Library / HiiLib / 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 This function adds the string into String Package of each language
20 supported by the package list.
21
22 If String is NULL, then ASSERT.
23 If StringId is NULL, the ASSERT.
24 If PackageList could not be found in the default HII database, then ASSERT.
25
26 @param PackageList Handle of the package list where this string will
27 be added.
28 @param StringId On return, contains the new strings id, which is
29 unique within PackageList.
30 @param String Points to the new null-terminated string.
31
32 @retval EFI_SUCCESS The new string was added successfully.
33 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
34
35 **/
36 EFI_STATUS
37 EFIAPI
38 HiiLibNewString (
39 IN EFI_HII_HANDLE PackageList,
40 OUT EFI_STRING_ID *StringId,
41 IN CONST EFI_STRING String
42 )
43 {
44 EFI_STATUS Status;
45 CHAR8 *Languages;
46 CHAR8 *LangStrings;
47 CHAR8 Lang[RFC_3066_ENTRY_SIZE];
48
49 ASSERT (String != NULL);
50 ASSERT (StringId != NULL);
51
52 Status = EFI_SUCCESS;
53
54 Languages = HiiLibGetSupportedLanguages (PackageList);
55
56 LangStrings = Languages;
57 while (*LangStrings != 0) {
58 HiiLibGetNextLanguage (&LangStrings, Lang);
59
60 Status = mHiiStringProt->NewString (
61 mHiiStringProt,
62 PackageList,
63 StringId,
64 Lang,
65 NULL,
66 String,
67 NULL
68 );
69 if (EFI_ERROR (Status)) {
70 break;
71 }
72 }
73
74 FreePool (Languages);
75
76 return Status;
77
78 }
79
80
81 /**
82 This function update the specified string in String Package of each language
83 supported by the package list.
84
85 If String is NULL, then ASSERT.
86 If PackageList could not be found in the default HII database, then ASSERT.
87 If StringId is not found in PackageList, then ASSERT.
88
89 @param PackageList Handle of the package list where this string will
90 be added.
91 @param StringId Ths String Id to be updated.
92 @param String Points to the new null-terminated string.
93
94 @retval EFI_SUCCESS The new string was added successfully.
95 @retval EFI_OUT_OF_RESOURCES Could not add the string due to lack of resources.
96
97 **/
98 EFI_STATUS
99 EFIAPI
100 HiiLibSetString (
101 IN EFI_HII_HANDLE PackageList,
102 IN EFI_STRING_ID StringId,
103 IN CONST EFI_STRING String
104 )
105 {
106 EFI_STATUS Status;
107 CHAR8 *Languages;
108 CHAR8 *LangStrings;
109 CHAR8 Lang[RFC_3066_ENTRY_SIZE];
110
111 ASSERT (IsHiiHandleRegistered (PackageList));
112
113 Status = EFI_SUCCESS;
114
115 Languages = HiiLibGetSupportedLanguages (PackageList);
116 ASSERT (Languages != NULL);
117
118 LangStrings = Languages;
119 while (*LangStrings != 0) {
120 HiiLibGetNextLanguage (&LangStrings, Lang);
121
122 Status = mHiiStringProt->SetString (
123 mHiiStringProt,
124 PackageList,
125 StringId,
126 Lang,
127 String,
128 NULL
129 );
130 if (EFI_ERROR (Status)) {
131 break;
132 }
133 }
134
135 FreePool (Languages);
136
137 return Status;
138 }
139
140
141 /**
142 Get the string given the StringId and String package Producer's Guid. The caller
143 is responsible to free the *String.
144
145 If PackageList with the matching ProducerGuid is not found, then ASSERT.
146 If PackageList with the matching ProducerGuid is found but no String is
147 specified by StringId is found, then ASSERT.
148
149 @param ProducerGuid The Guid of String package list.
150 @param StringId The String ID.
151 @param String The output string.
152
153 @retval EFI_SUCCESS Operation is successful.
154 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
155
156 **/
157 EFI_STATUS
158 EFIAPI
159 HiiLibGetStringFromToken (
160 IN EFI_GUID *ProducerGuid,
161 IN EFI_STRING_ID StringId,
162 OUT EFI_STRING *String
163 )
164 {
165 EFI_STATUS Status;
166 UINTN Index;
167 UINTN HandleBufferLen;
168 EFI_HII_HANDLE *HiiHandleBuffer;
169 EFI_GUID Guid;
170
171 Status = HiiLibGetHiiHandles (&HandleBufferLen, &HiiHandleBuffer);
172 if (EFI_ERROR(Status)) {
173 return Status;
174 }
175 for (Index = 0; Index < (HandleBufferLen / sizeof (EFI_HII_HANDLE)); Index++) {
176 Status = HiiLibExtractGuidFromHiiHandle (HiiHandleBuffer[Index], &Guid);
177 if (EFI_ERROR(Status)) {
178 return Status;
179 }
180 if (CompareGuid (&Guid, ProducerGuid)) {
181 break;
182 }
183 }
184
185 if (Index >= (HandleBufferLen / sizeof (EFI_HII_HANDLE))) {
186 //
187 // If PackageList with the matching ProducerGuid is not found, then ASSERT.
188 //
189 ASSERT (FALSE);
190 Status = EFI_NOT_FOUND;
191 goto Out;
192 }
193
194 Status = HiiLibGetStringFromHandle (HiiHandleBuffer[Index], StringId, String);
195
196 Out:
197 if (HiiHandleBuffer != NULL) {
198 gBS->FreePool (HiiHandleBuffer);
199 }
200 return Status;
201 }
202
203 /**
204 This function try to retrieve string from String package of current language.
205 If fails, it try to retrieve string from String package of first language it support.
206
207 If String is NULL, then ASSERT.
208 If StringSize is NULL, then ASSERT.
209 If PackageList could not be found in the default HII database, then ASSERT.
210 If StringId is not found in PackageList, then ASSERT.
211
212 @param PackageList The package list in the HII database to search for
213 the specified string.
214 @param StringId The string's id, which is unique within
215 PackageList.
216 @param String Points to the new null-terminated string.
217 @param StringSize On entry, points to the size of the buffer pointed
218 to by String, in bytes. On return, points to the
219 length of the string, in bytes.
220
221 @retval EFI_SUCCESS The string was returned successfully.
222 @retval EFI_NOT_FOUND The string specified by StringId is not available.
223 @retval EFI_BUFFER_TOO_SMALL The buffer specified by StringLength is too small
224 to hold the string.
225 @retval EFI_INVALID_PARAMETER The String or StringSize was NULL.
226
227 **/
228 EFI_STATUS
229 EFIAPI
230 HiiLibGetString (
231 IN EFI_HII_HANDLE PackageList,
232 IN EFI_STRING_ID StringId,
233 OUT EFI_STRING String,
234 IN OUT UINTN *StringSize
235 )
236 {
237 EFI_STATUS Status;
238 CHAR8 *Languages;
239 CHAR8 *LangStrings;
240 CHAR8 Lang[RFC_3066_ENTRY_SIZE];
241 CHAR8 CurrentLang[RFC_3066_ENTRY_SIZE];
242
243 ASSERT (String != NULL);
244 ASSERT (StringSize != NULL);
245 ASSERT (IsHiiHandleRegistered (PackageList));
246
247 HiiLibGetCurrentLanguage (CurrentLang);
248
249 Status = mHiiStringProt->GetString (
250 mHiiStringProt,
251 CurrentLang,
252 PackageList,
253 StringId,
254 String,
255 StringSize,
256 NULL
257 );
258
259 if (EFI_ERROR (Status) && (Status != EFI_BUFFER_TOO_SMALL)) {
260 Languages = HiiLibGetSupportedLanguages (PackageList);
261 ASSERT (Languages != NULL);
262
263 LangStrings = Languages;
264 HiiLibGetNextLanguage (&LangStrings, Lang);
265 gBS->FreePool (Languages);
266
267 Status = mHiiStringProt->GetString (
268 mHiiStringProt,
269 Lang,
270 PackageList,
271 StringId,
272 String,
273 StringSize,
274 NULL
275 );
276 }
277
278 return Status;
279 }
280
281
282 /**
283 Get string specified by StringId form the HiiHandle. The caller
284 is responsible to free the *String.
285
286 If String is NULL, then ASSERT.
287 If HiiHandle could not be found in the default HII database, then ASSERT.
288 If StringId is not found in PackageList, then ASSERT.
289
290 @param HiiHandle The HII handle of package list.
291 @param StringId The String ID.
292 @param String The output string.
293
294 @retval EFI_NOT_FOUND String is not found.
295 @retval EFI_SUCCESS Operation is successful.
296 @retval EFI_OUT_OF_RESOURCES There is not enought memory in the system.
297 @retval EFI_INVALID_PARAMETER The String is NULL.
298
299 **/
300 EFI_STATUS
301 EFIAPI
302 HiiLibGetStringFromHandle (
303 IN EFI_HII_HANDLE HiiHandle,
304 IN EFI_STRING_ID StringId,
305 OUT EFI_STRING *String
306 )
307 {
308 EFI_STATUS Status;
309 UINTN StringSize;
310
311 ASSERT (String != NULL);
312
313 StringSize = HII_LIB_DEFAULT_STRING_SIZE;
314 *String = AllocateZeroPool (StringSize);
315 if (*String == NULL) {
316 return EFI_OUT_OF_RESOURCES;
317 }
318
319 Status = HiiLibGetString (HiiHandle, StringId, *String, &StringSize);
320 if (Status == EFI_BUFFER_TOO_SMALL) {
321 gBS->FreePool (*String);
322 *String = AllocateZeroPool (StringSize);
323 if (*String == NULL) {
324 return EFI_OUT_OF_RESOURCES;
325 }
326 Status = HiiLibGetString (HiiHandle, StringId, *String, &StringSize);
327 }
328
329 return Status;
330 }
331
332
333
334 //
335 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
336 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
337 // The last 2 CHAR8 values are the ISO 639-1 code.
338 //
339 CHAR8 Iso639ToRfc3066ConversionTable[] =
340 "\
341 aaraa\
342 abkab\
343 afraf\
344 amham\
345 araar\
346 asmas\
347 aymay\
348 azeaz\
349 bakba\
350 belbe\
351 benbn\
352 bihbh\
353 bisbi\
354 bodbo\
355 brebr\
356 bulbg\
357 catca\
358 cescs\
359 corkw\
360 cosco\
361 cymcy\
362 danda\
363 deude\
364 dzodz\
365 ellel\
366 engen\
367 epoeo\
368 estet\
369 euseu\
370 faofo\
371 fasfa\
372 fijfj\
373 finfi\
374 frafr\
375 fryfy\
376 gaiga\
377 gdhgd\
378 glggl\
379 grngn\
380 gujgu\
381 hauha\
382 hebhe\
383 hinhi\
384 hrvhr\
385 hunhu\
386 hyehy\
387 ikuiu\
388 ileie\
389 inaia\
390 indid\
391 ipkik\
392 islis\
393 itait\
394 jawjw\
395 jpnja\
396 kalkl\
397 kankn\
398 kasks\
399 katka\
400 kazkk\
401 khmkm\
402 kinrw\
403 kirky\
404 korko\
405 kurku\
406 laolo\
407 latla\
408 lavlv\
409 linln\
410 litlt\
411 ltzlb\
412 malml\
413 marmr\
414 mkdmk\
415 mlgmg\
416 mltmt\
417 molmo\
418 monmn\
419 mrimi\
420 msams\
421 myamy\
422 nauna\
423 nepne\
424 nldnl\
425 norno\
426 ocioc\
427 ormom\
428 panpa\
429 polpl\
430 porpt\
431 pusps\
432 quequ\
433 rohrm\
434 ronro\
435 runrn\
436 rusru\
437 sagsg\
438 sansa\
439 sinsi\
440 slksk\
441 slvsl\
442 smise\
443 smosm\
444 snasn\
445 sndsd\
446 somso\
447 sotst\
448 spaes\
449 sqisq\
450 srpsr\
451 sswss\
452 sunsu\
453 swasw\
454 swesv\
455 tamta\
456 tattt\
457 telte\
458 tgktg\
459 tgltl\
460 thath\
461 tsnts\
462 tuktk\
463 twitw\
464 uigug\
465 ukruk\
466 urdur\
467 uzbuz\
468 vievi\
469 volvo\
470 wolwo\
471 xhoxh\
472 yidyi\
473 zhaza\
474 zhozh\
475 zulzu\
476 ";
477
478
479 /**
480 Convert language code from RFC3066 to ISO639-2.
481
482 @param LanguageRfc3066 RFC3066 language code.
483 @param LanguageIso639 ISO639-2 language code.
484
485 @retval EFI_SUCCESS Language code converted.
486 @retval EFI_NOT_FOUND Language code not found.
487
488 **/
489 EFI_STATUS
490 EFIAPI
491 ConvertRfc3066LanguageToIso639Language (
492 CHAR8 *LanguageRfc3066,
493 CHAR8 *LanguageIso639
494 )
495 {
496 UINTN Index;
497
498 if ((LanguageRfc3066[2] != '-') && (LanguageRfc3066[2] != 0)) {
499 CopyMem (LanguageIso639, LanguageRfc3066, 3);
500 return EFI_SUCCESS;
501 }
502
503 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
504 if (CompareMem (LanguageRfc3066, &Iso639ToRfc3066ConversionTable[Index + 3], 2) == 0) {
505 CopyMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3);
506 return EFI_SUCCESS;
507 }
508 }
509
510 return EFI_NOT_FOUND;
511 }
512
513
514 /**
515 Convert language code from ISO639-2 to RFC3066.
516
517 LanguageIso639 contain a single ISO639-2 code such as
518 "eng" or "fra".
519
520 The LanguageRfc3066 must be a buffer large enough
521 for RFC_3066_ENTRY_SIZE characters.
522
523 If LanguageIso639 is NULL, then ASSERT.
524 If LanguageRfc3066 is NULL, then ASSERT.
525
526 @param LanguageIso639 ISO639-2 language code.
527 @param LanguageRfc3066 RFC3066 language code.
528
529 @retval EFI_SUCCESS Language code converted.
530 @retval EFI_NOT_FOUND Language code not found.
531
532 **/
533 EFI_STATUS
534 EFIAPI
535 ConvertIso639LanguageToRfc3066Language (
536 IN CONST CHAR8 *LanguageIso639,
537 OUT CHAR8 *LanguageRfc3066
538 )
539 {
540 UINTN Index;
541
542 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
543 if (CompareMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3) == 0) {
544 CopyMem (LanguageRfc3066, &Iso639ToRfc3066ConversionTable[Index + 3], 2);
545 return EFI_SUCCESS;
546 }
547 }
548
549 return EFI_NOT_FOUND;
550 }
551
552 /**
553 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
554 be converted to "engfra".
555
556 @param SupportedLanguages The RFC3066 language list.
557
558 @return The ISO639-2 language list.
559
560 **/
561 CHAR8 *
562 EFIAPI
563 Rfc3066ToIso639 (
564 CHAR8 *SupportedLanguages
565 )
566 {
567 CHAR8 *Languages;
568 CHAR8 *ReturnValue;
569 CHAR8 *LangCodes;
570 CHAR8 LangRfc3066[RFC_3066_ENTRY_SIZE];
571 CHAR8 LangIso639[ISO_639_2_ENTRY_SIZE];
572 EFI_STATUS Status;
573
574 ReturnValue = AllocateZeroPool (AsciiStrSize (SupportedLanguages));
575 if (ReturnValue == NULL) {
576 return ReturnValue;
577 }
578
579 Languages = ReturnValue;
580 LangCodes = SupportedLanguages;
581 while (*LangCodes != 0) {
582 HiiLibGetNextLanguage (&LangCodes, LangRfc3066);
583
584 Status = ConvertRfc3066LanguageToIso639Language (LangRfc3066, LangIso639);
585 if (!EFI_ERROR (Status)) {
586 CopyMem (Languages, LangIso639, 3);
587 Languages = Languages + 3;
588 }
589 }
590
591 return ReturnValue;
592 }
593
594