]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/HiiLib/HiiString.c
Add more comment for library instance and remove unused internal header file.
[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 HiiLibGetCurrentLanguage (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 @retval EFI_INVALID_PARAMETER The String is NULL.
305
306 **/
307 EFI_STATUS
308 EFIAPI
309 HiiLibGetStringFromHandle (
310 IN EFI_HII_HANDLE HiiHandle,
311 IN EFI_STRING_ID StringId,
312 OUT EFI_STRING *String
313 )
314 {
315 EFI_STATUS Status;
316 UINTN StringSize;
317
318 ASSERT (String != NULL);
319
320 StringSize = HII_LIB_DEFAULT_STRING_SIZE;
321 *String = AllocateZeroPool (StringSize);
322 if (*String == NULL) {
323 return EFI_OUT_OF_RESOURCES;
324 }
325
326 Status = HiiLibGetString (HiiHandle, StringId, *String, &StringSize);
327 if (Status == EFI_BUFFER_TOO_SMALL) {
328 FreePool (*String);
329 *String = AllocateZeroPool (StringSize);
330 if (*String == NULL) {
331 return EFI_OUT_OF_RESOURCES;
332 }
333 Status = HiiLibGetString (HiiHandle, StringId, *String, &StringSize);
334 }
335
336 return Status;
337 }
338
339
340
341 //
342 // Lookup table of ISO639-2 3 character language codes to ISO 639-1 2 character language codes
343 // Each entry is 5 CHAR8 values long. The first 3 CHAR8 values are the ISO 639-2 code.
344 // The last 2 CHAR8 values are the ISO 639-1 code.
345 //
346 GLOBAL_REMOVE_IF_UNREFERENCED CONST CHAR8 Iso639ToRfc3066ConversionTable[] =
347 "\
348 aaraa\
349 abkab\
350 afraf\
351 amham\
352 araar\
353 asmas\
354 aymay\
355 azeaz\
356 bakba\
357 belbe\
358 benbn\
359 bihbh\
360 bisbi\
361 bodbo\
362 brebr\
363 bulbg\
364 catca\
365 cescs\
366 corkw\
367 cosco\
368 cymcy\
369 danda\
370 deude\
371 dzodz\
372 ellel\
373 engen\
374 epoeo\
375 estet\
376 euseu\
377 faofo\
378 fasfa\
379 fijfj\
380 finfi\
381 frafr\
382 fryfy\
383 gaiga\
384 gdhgd\
385 glggl\
386 grngn\
387 gujgu\
388 hauha\
389 hebhe\
390 hinhi\
391 hrvhr\
392 hunhu\
393 hyehy\
394 ikuiu\
395 ileie\
396 inaia\
397 indid\
398 ipkik\
399 islis\
400 itait\
401 jawjw\
402 jpnja\
403 kalkl\
404 kankn\
405 kasks\
406 katka\
407 kazkk\
408 khmkm\
409 kinrw\
410 kirky\
411 korko\
412 kurku\
413 laolo\
414 latla\
415 lavlv\
416 linln\
417 litlt\
418 ltzlb\
419 malml\
420 marmr\
421 mkdmk\
422 mlgmg\
423 mltmt\
424 molmo\
425 monmn\
426 mrimi\
427 msams\
428 myamy\
429 nauna\
430 nepne\
431 nldnl\
432 norno\
433 ocioc\
434 ormom\
435 panpa\
436 polpl\
437 porpt\
438 pusps\
439 quequ\
440 rohrm\
441 ronro\
442 runrn\
443 rusru\
444 sagsg\
445 sansa\
446 sinsi\
447 slksk\
448 slvsl\
449 smise\
450 smosm\
451 snasn\
452 sndsd\
453 somso\
454 sotst\
455 spaes\
456 sqisq\
457 srpsr\
458 sswss\
459 sunsu\
460 swasw\
461 swesv\
462 tamta\
463 tattt\
464 telte\
465 tgktg\
466 tgltl\
467 thath\
468 tsnts\
469 tuktk\
470 twitw\
471 uigug\
472 ukruk\
473 urdur\
474 uzbuz\
475 vievi\
476 volvo\
477 wolwo\
478 xhoxh\
479 yidyi\
480 zhaza\
481 zhozh\
482 zulzu\
483 ";
484
485
486 /**
487 Convert language code from RFC3066 to ISO639-2.
488
489 @param LanguageRfc3066 RFC3066 language code.
490 @param LanguageIso639 ISO639-2 language code.
491
492 @retval EFI_SUCCESS Language code converted.
493 @retval EFI_NOT_FOUND Language code not found.
494
495 **/
496 EFI_STATUS
497 EFIAPI
498 ConvertRfc3066LanguageToIso639Language (
499 CHAR8 *LanguageRfc3066,
500 CHAR8 *LanguageIso639
501 )
502 {
503 UINTN Index;
504
505 if ((LanguageRfc3066[2] != '-') && (LanguageRfc3066[2] != 0)) {
506 CopyMem (LanguageIso639, LanguageRfc3066, 3);
507 return EFI_SUCCESS;
508 }
509
510 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
511 if (CompareMem (LanguageRfc3066, &Iso639ToRfc3066ConversionTable[Index + 3], 2) == 0) {
512 CopyMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3);
513 return EFI_SUCCESS;
514 }
515 }
516
517 return EFI_NOT_FOUND;
518 }
519
520
521 /**
522 Convert language code from ISO639-2 to RFC3066.
523
524 LanguageIso639 contain a single ISO639-2 code such as
525 "eng" or "fra".
526
527 The LanguageRfc3066 must be a buffer large enough
528 for RFC_3066_ENTRY_SIZE characters.
529
530 If LanguageIso639 is NULL, then ASSERT.
531 If LanguageRfc3066 is NULL, then ASSERT.
532
533 @param LanguageIso639 ISO639-2 language code.
534 @param LanguageRfc3066 RFC3066 language code.
535
536 @retval EFI_SUCCESS Language code converted.
537 @retval EFI_NOT_FOUND Language code not found.
538
539 **/
540 EFI_STATUS
541 EFIAPI
542 ConvertIso639LanguageToRfc3066Language (
543 IN CONST CHAR8 *LanguageIso639,
544 OUT CHAR8 *LanguageRfc3066
545 )
546 {
547 UINTN Index;
548
549 for (Index = 0; Iso639ToRfc3066ConversionTable[Index] != 0; Index += 5) {
550 if (CompareMem (LanguageIso639, &Iso639ToRfc3066ConversionTable[Index], 3) == 0) {
551 CopyMem (LanguageRfc3066, &Iso639ToRfc3066ConversionTable[Index + 3], 2);
552 return EFI_SUCCESS;
553 }
554 }
555
556 return EFI_NOT_FOUND;
557 }
558
559 /**
560 Convert language code list from RFC3066 to ISO639-2, e.g. "en-US;fr-FR" will
561 be converted to "engfra".
562
563 @param SupportedLanguages The RFC3066 language list.
564
565 @return The ISO639-2 language list.
566
567 **/
568 CHAR8 *
569 EFIAPI
570 Rfc3066ToIso639 (
571 CHAR8 *SupportedLanguages
572 )
573 {
574 CHAR8 *Languages;
575 CHAR8 *ReturnValue;
576 CHAR8 *LangCodes;
577 CHAR8 LangRfc3066[RFC_3066_ENTRY_SIZE];
578 CHAR8 LangIso639[ISO_639_2_ENTRY_SIZE];
579 EFI_STATUS Status;
580
581 ReturnValue = AllocateZeroPool (AsciiStrSize (SupportedLanguages));
582 if (ReturnValue == NULL) {
583 return ReturnValue;
584 }
585
586 Languages = ReturnValue;
587 LangCodes = SupportedLanguages;
588 while (*LangCodes != 0) {
589 HiiLibGetNextLanguage (&LangCodes, LangRfc3066);
590
591 Status = ConvertRfc3066LanguageToIso639Language (LangRfc3066, LangIso639);
592 if (!EFI_ERROR (Status)) {
593 CopyMem (Languages, LangIso639, 3);
594 Languages = Languages + 3;
595 }
596 }
597
598 return ReturnValue;
599 }
600
601