3 Copyright (c) 2006 - 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
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.
25 #include "TianoCommon.h"
28 #define UTILITY_VERSION "v1.0"
29 #define UTILITY_NAME "EfiCompress"
36 typedef struct _COMPRESS_ACTION_LIST
{
37 struct _COMPRESS_ACTION_LIST
*NextAction
;
41 } COMPRESS_ACTION_LIST
;
49 COMPRESS_ACTION_LIST
**ActionListHead
55 Parse command line options
59 argc - number of arguments passed into the command line.
60 argv[] - files to compress and files to output compressed data to.
61 Options - Point to COMMAND_LINE_OPTIONS, receiving command line options.
65 BOOLEAN: TRUE for a successful parse.
82 ExeName - Application's full path
93 COMPRESS_TYPE CompressType
99 Compress InFileName to OutFileName using algorithm specified by CompressType.
103 InFileName - Input file to compress
104 OutFileName - Output file compress to
105 CompressType - Compress algorithm, can be EFI_COMPRESS or TIANO_COMPRESS
109 BOOLEAN: TRUE for compress file successfully
123 Compresses the input files
127 argc - number of arguments passed into the command line.
128 argv[] - files to compress and files to output compressed data to.
132 int: 0 for successful execution of the function.
136 COMPRESS_ACTION_LIST
*ActionList
;
137 COMPRESS_ACTION_LIST
*NextAction
;
142 ActionCount
= SuccessCount
= 0;
144 if (!ParseCommandLine (argc
, argv
, &ActionList
)) {
149 while (ActionList
!= NULL
) {
152 ActionList
->InFileName
,
153 ActionList
->OutFileName
,
154 ActionList
->CompressType
)
158 NextAction
= ActionList
;
159 ActionList
= ActionList
->NextAction
;
163 fprintf (stdout
, "\nCompressed %d files, %d succeed!\n", ActionCount
, SuccessCount
);
164 if (SuccessCount
< ActionCount
) {
176 COMPRESS_ACTION_LIST
**ActionListHead
179 COMPRESS_TYPE CurrentType
;
181 COMPRESS_ACTION_LIST
**Action
;
183 Action
= ActionListHead
;
184 CurrentType
= EFI_COMPRESS
; // default compress algorithm
191 if (strcmp (*argv
, "-h") == 0 || strcmp (*argv
, "-?") == 0) {
193 // 1. Directly return, help message will be printed.
197 } else if (strncmp (*argv
, "-t", 2) == 0) {
199 // 2. Specifying CompressType
201 if (_stricmp ((*argv
)+2, "EFI") == 0) {
202 CurrentType
= EFI_COMPRESS
;
203 } else if (_stricmp ((*argv
)+2, "Tiano") == 0) {
204 CurrentType
= TIANO_COMPRESS
;
206 fprintf (stdout
, " ERROR: CompressType %s not supported!\n", (*argv
)+2);
211 // 3. Current parameter is *FileName
213 if (*Action
== NULL
) {
215 // need to create a new action item
217 *Action
= (COMPRESS_ACTION_LIST
*) malloc (sizeof **Action
);
218 if (*Action
== NULL
) {
219 fprintf (stdout
, " ERROR: malloc failed!\n");
222 memset (*Action
, 0, sizeof **Action
);
223 (*Action
)->CompressType
= CurrentType
;
227 // Assignment to InFileName and OutFileName in order
229 if ((*Action
)->InFileName
== NULL
) {
230 (*Action
)->InFileName
= *argv
;
232 (*Action
)->OutFileName
= *argv
;
233 Action
= &(*Action
)->NextAction
;
242 if (*Action
!= NULL
) {
243 assert ((*Action
)->InFileName
!= NULL
);
244 fprintf (stdout
, " ERROR: Compress OutFileName not specified with InFileName: %s!\n", (*Action
)->InFileName
);
248 if (*ActionListHead
== NULL
) {
259 COMPRESS_TYPE CompressType
269 COMPRESS_FUNCTION CompressFunc
;
271 SrcBuffer
= DstBuffer
= NULL
;
272 InFileP
= OutFileP
= NULL
;
274 fprintf (stdout
, "%s --> %s\n", InFileName
, OutFileName
);
276 if ((OutFileP
= fopen (OutFileName
, "wb")) == NULL
) {
277 fprintf (stdout
, " ERROR: Can't open output file %s for write!\n", OutFileName
);
281 if ((InFileP
= fopen (InFileName
, "rb")) == NULL
) {
282 fprintf (stdout
, " ERROR: Can't open input file %s for read!\n", InFileName
);
287 // Get the size of source file
289 fseek (InFileP
, 0, SEEK_END
);
290 SrcSize
= ftell (InFileP
);
293 // Read in the source data
295 if ((SrcBuffer
= malloc (SrcSize
)) == NULL
) {
296 fprintf (stdout
, " ERROR: Can't allocate memory!\n");
300 if (fread (SrcBuffer
, 1, SrcSize
, InFileP
) != SrcSize
) {
301 fprintf (stdout
, " ERROR: Can't read from source!\n");
306 // Choose the right compress algorithm
308 CompressFunc
= (CompressType
== EFI_COMPRESS
) ? EfiCompress
: TianoCompress
;
311 // Get destination data size and do the compression
314 Status
= CompressFunc (SrcBuffer
, SrcSize
, DstBuffer
, &DstSize
);
315 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
316 fprintf (stdout
, " Error: Compress failed: %x!\n", Status
);
319 if ((DstBuffer
= malloc (DstSize
)) == NULL
) {
320 fprintf (stdout
, " ERROR: Can't allocate memory!\n");
324 Status
= CompressFunc (SrcBuffer
, SrcSize
, DstBuffer
, &DstSize
);
325 if (EFI_ERROR (Status
)) {
326 fprintf (stdout
, " ERROR: Compress Error!\n");
330 fprintf (stdout
, " Orig Size = %ld\tComp Size = %ld\n", SrcSize
, DstSize
);
332 if (DstBuffer
== NULL
) {
333 fprintf (stdout
, " ERROR: No destination to write to!\n");
338 // Write out the result
340 if (fwrite (DstBuffer
, 1, DstSize
, OutFileP
) != DstSize
) {
341 fprintf (stdout
, " ERROR: Can't write to destination file!\n");
372 const char *Str
[] = {
373 UTILITY_NAME
" "UTILITY_VERSION
" - Intel EFI Compress Utility",
374 " Copyright (C), 2006 - 2008 Intel Corporation",
376 #if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
377 " Built from "UTILITY_BUILD
", project of "UTILITY_VENDOR
,
381 " "UTILITY_NAME
" [OPTION] SOURCE DEST ...",
383 " Compress a list of SOURCE(s) to accordingly DEST(s) using the specified",
384 " compress algorithm.",
386 " -tCompressAlgo Optional compress algorithm (EFI | Tiano), case insensitive.",
387 " If ommitted, compress type specified ahead is used,",
389 " e.g.: EfiCompress a.in a.out -tTiano b.in b.out \\",
390 " c.in c.out -tEFI d.in d.out",
391 " a.in and d.in are compressed using EFI compress algorithm",
392 " b.in and c.in are compressed using Tiano compress algorithm",
395 for (Index
= 0; Str
[Index
] != NULL
; Index
++) {
396 fprintf (stdout
, "%s\n", Str
[Index
]);