]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Sample/Tools/Source/HiiPack/StringParse.c
8dd26e1529c32d4124343c484ff74fd437218ebb
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / HiiPack / StringParse.c
1 /*++
2
3 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
8
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11
12 Module Name:
13
14 StringParse.c
15
16 Abstract:
17
18 Routines for parsing HII string packs
19
20 --*/
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "Tiano.h"
27 #include "EfiUtilityMsgs.h"
28 #include "EfiInternalFormRepresentation.h"
29 #include "Hii.h"
30 #include "StringParse.h"
31 #include "HiiPack.h"
32
33 typedef struct _STRING_PACK_RECORD {
34 struct _STRING_PACK_RECORD *Next;
35 int Handle;
36 EFI_GUID PackageGuid;
37 EFI_GUID FormsetGuid;
38 EFI_HII_STRING_PACK *StringPack;
39 int StringPackSize;
40 int NumStringPacks;
41 } STRING_PACK_RECORD;
42
43 static STRING_PACK_RECORD *mStringPacks = NULL;
44
45 STATUS
46 StringGetPack (
47 int Handle, // matches handle passed in with StringParsePack()
48 EFI_HII_STRING_PACK **StringPack, // returned pointer to string pack
49 int *StringPackSize, // sizeof buffer pointed to by StringPack
50 int *NumStringPacks, // in the array pointed to by StringPack
51 EFI_GUID *FormsetGuid,
52 EFI_GUID *PackageGuid
53 )
54 /*++
55
56 Routine Description:
57
58 Get a string pack given to us previously
59
60 Arguments:
61 Handle - handle of string pack to get
62 StringPack - outgoing pointer to string pack on the given handle
63 StringPackSize - outgoing size of string pack pointed to by StringPack
64 NumStringPacks - outgoing number of string packs in StringPack[] array
65 FormsetGuid - outgoing GUID passed in with the string pack when it was parsed
66 PackageGuid - outgoing GUID passed in with the string pack when it was parsed
67
68 Returns:
69
70 STATUS_SUCCESS - string pack with matching handle was found
71 STATUS_ERROR - otherwise
72
73 --*/
74 {
75 STRING_PACK_RECORD *Rec;
76
77 for (Rec = mStringPacks; Rec != NULL; Rec = Rec->Next) {
78 if (Rec->Handle == Handle) {
79 *StringPack = Rec->StringPack;
80 *StringPackSize = Rec->StringPackSize;
81 *NumStringPacks = Rec->NumStringPacks;
82 return STATUS_SUCCESS;
83 }
84 }
85
86 return STATUS_ERROR;
87 }
88
89 STATUS
90 StringParsePack (
91 int Handle,
92 EFI_HII_STRING_PACK *StringPack,
93 EFI_GUID *FormsetGuid,
94 EFI_GUID *PackageGuid
95 )
96 /*++
97
98 Routine Description:
99
100 Parse a string pack, saving the information for later retrieval by the caller
101
102 Arguments:
103 Handle - handle of string pack
104 StringPack - pointer to string pack array to parse
105 FormsetGuid - GUID of the string pack
106 PackageGuid - package GUID from the HII data table from which this string pack orginated
107
108 Returns:
109
110 STATUS_SUCCESS - Stringpack processed successfully
111 STATUS_ERROR - otherwise
112
113 --*/
114 {
115 STRING_PACK_RECORD *Rec;
116
117 STRING_PACK_RECORD *TempRec;
118 int PackSize;
119 EFI_HII_STRING_PACK *TempPack;
120 //
121 // Allocate a new string pack record
122 //
123 Rec = (STRING_PACK_RECORD *) malloc (sizeof (STRING_PACK_RECORD));
124 if (Rec == NULL) {
125 Error (NULL, 0, 0, "memory allocation failure", NULL);
126 return STATUS_ERROR;
127 }
128
129 memset (Rec, 0, sizeof (STRING_PACK_RECORD));
130 Rec->Handle = Handle;
131 if (PackageGuid != NULL) {
132 memcpy (&Rec->PackageGuid, PackageGuid, sizeof (EFI_GUID));
133 }
134
135 if (FormsetGuid != NULL) {
136 memcpy (&Rec->FormsetGuid, FormsetGuid, sizeof (EFI_GUID));
137 }
138 //
139 // Walk the string packs to find the terminator
140 //
141 TempPack = StringPack;
142 PackSize = 0;
143 while (TempPack->Header.Length > 0) {
144 if (TempPack->Header.Type != EFI_HII_STRING) {
145 Error (NULL, 0, 0, "found a non-string pack in the string pack array", NULL);
146 free (Rec);
147 return STATUS_ERROR;
148 }
149
150 PackSize += TempPack->Header.Length;
151 Rec->NumStringPacks++;
152 TempPack = (EFI_HII_STRING_PACK *) ((char *) TempPack + TempPack->Header.Length);
153 }
154 //
155 // Add space for the terminator
156 //
157 PackSize += sizeof (EFI_HII_STRING_PACK);
158 Rec->StringPackSize = PackSize;
159 //
160 // Make a copy of the incoming string pack
161 //
162 Rec->StringPack = (EFI_HII_STRING_PACK *) malloc (PackSize);
163 if (Rec->StringPack == NULL) {
164 Error (NULL, 0, 0, "memory allocation failure", NULL);
165 free (Rec);
166 return STATUS_ERROR;
167 }
168
169 memcpy ((void *) Rec->StringPack, StringPack, PackSize);
170 //
171 // Add this record to our list
172 //
173 if (mStringPacks == NULL) {
174 mStringPacks = Rec;
175 } else {
176 for (TempRec = mStringPacks; TempRec->Next != NULL; TempRec = TempRec->Next)
177 ;
178 TempRec->Next = Rec;
179 }
180 free (Rec->StringPack);
181 free (Rec);
182 return STATUS_SUCCESS;
183 }
184
185 STATUS
186 StringInit (
187 VOID
188 )
189 /*++
190
191 Routine Description:
192
193 GC_TODO: Add function description
194
195 Arguments:
196
197 None
198
199 Returns:
200
201 GC_TODO: add return values
202
203 --*/
204 {
205 StringEnd ();
206 return STATUS_SUCCESS;
207 }
208
209 STATUS
210 StringEnd (
211 VOID
212 )
213 /*++
214
215 Routine Description:
216
217 GC_TODO: Add function description
218
219 Arguments:
220
221 None
222
223 Returns:
224
225 GC_TODO: add return values
226
227 --*/
228 {
229 STRING_PACK_RECORD *Next;
230 //
231 // Free up all the memory we've allocated
232 //
233 while (mStringPacks != NULL) {
234 if (mStringPacks->StringPack != NULL) {
235 free (mStringPacks->StringPack);
236 }
237
238 Next = mStringPacks->Next;
239 free (mStringPacks);
240 mStringPacks = Next;
241 }
242
243 return STATUS_SUCCESS;
244 }