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