3 Copyright 2006 - 2007, Intel Corporation
4 All rights reserved. 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"
33 typedef struct _COMPRESS_ACTION_LIST
{
34 struct _COMPRESS_ACTION_LIST
*NextAction
;
38 } COMPRESS_ACTION_LIST
;
46 COMPRESS_ACTION_LIST
**ActionListHead
52 Parse command line options
56 argc - number of arguments passed into the command line.
57 argv[] - files to compress and files to output compressed data to.
58 Options - Point to COMMAND_LINE_OPTIONS, receiving command line options.
62 BOOLEAN: TRUE for a successful parse.
79 ExeName - Application's full path
90 COMPRESS_TYPE CompressType
96 Compress InFileName to OutFileName using algorithm specified by CompressType.
100 InFileName - Input file to compress
101 OutFileName - Output file compress to
102 CompressType - Compress algorithm, can be EFI_COMPRESS or TIANO_COMPRESS
106 BOOLEAN: TRUE for compress file successfully
120 Compresses the input files
124 argc - number of arguments passed into the command line.
125 argv[] - files to compress and files to output compressed data to.
129 int: 0 for successful execution of the function.
133 COMPRESS_ACTION_LIST
*ActionList
;
134 COMPRESS_ACTION_LIST
*NextAction
;
139 ActionCount
= SuccessCount
= 0;
141 if (!ParseCommandLine (argc
, argv
, &ActionList
)) {
146 while (ActionList
!= NULL
) {
149 ActionList
->InFileName
,
150 ActionList
->OutFileName
,
151 ActionList
->CompressType
)
155 NextAction
= ActionList
;
156 ActionList
= ActionList
->NextAction
;
160 fprintf (stdout
, "\nCompressed %d files, %d succeed!\n", ActionCount
, SuccessCount
);
161 if (SuccessCount
< ActionCount
) {
173 COMPRESS_ACTION_LIST
**ActionListHead
176 COMPRESS_TYPE CurrentType
;
178 COMPRESS_ACTION_LIST
**Action
;
180 Action
= ActionListHead
;
181 CurrentType
= EFI_COMPRESS
; // default compress algorithm
188 if (strcmp (*argv
, "-h") == 0 || strcmp (*argv
, "-?") == 0) {
190 // 1. Directly return, help message will be printed.
194 } else if (strncmp (*argv
, "-t", 2) == 0) {
196 // 2. Specifying CompressType
198 if (_stricmp ((*argv
)+2, "EFI") == 0) {
199 CurrentType
= EFI_COMPRESS
;
200 } else if (_stricmp ((*argv
)+2, "Tiano") == 0) {
201 CurrentType
= TIANO_COMPRESS
;
203 fprintf (stdout
, " ERROR: CompressType %s not supported!\n", (*argv
)+2);
208 // 3. Current parameter is *FileName
210 if (*Action
== NULL
) {
212 // need to create a new action item
214 *Action
= (COMPRESS_ACTION_LIST
*) malloc (sizeof **Action
);
215 if (*Action
== NULL
) {
216 fprintf (stdout
, " ERROR: malloc failed!\n");
219 memset (*Action
, 0, sizeof **Action
);
220 (*Action
)->CompressType
= CurrentType
;
224 // Assignment to InFileName and OutFileName in order
226 if ((*Action
)->InFileName
== NULL
) {
227 (*Action
)->InFileName
= *argv
;
229 (*Action
)->OutFileName
= *argv
;
230 Action
= &(*Action
)->NextAction
;
239 if (*Action
!= NULL
) {
240 assert ((*Action
)->InFileName
!= NULL
);
241 fprintf (stdout
, " ERROR: Compress OutFileName not specified with InFileName: %s!\n", (*Action
)->InFileName
);
245 if (*ActionListHead
== NULL
) {
256 COMPRESS_TYPE CompressType
266 COMPRESS_FUNCTION CompressFunc
;
268 SrcBuffer
= DstBuffer
= NULL
;
269 InFileP
= OutFileP
= NULL
;
271 fprintf (stdout
, "%s --> %s\n", InFileName
, OutFileName
);
273 if ((OutFileP
= fopen (OutFileName
, "wb")) == NULL
) {
274 fprintf (stdout
, " ERROR: Can't open output file %s for write!\n", OutFileName
);
278 if ((InFileP
= fopen (InFileName
, "rb")) == NULL
) {
279 fprintf (stdout
, " ERROR: Can't open input file %s for read!\n", InFileName
);
284 // Get the size of source file
286 fseek (InFileP
, 0, SEEK_END
);
287 SrcSize
= ftell (InFileP
);
290 // Read in the source data
292 if ((SrcBuffer
= malloc (SrcSize
)) == NULL
) {
293 fprintf (stdout
, " ERROR: Can't allocate memory!\n");
297 if (fread (SrcBuffer
, 1, SrcSize
, InFileP
) != SrcSize
) {
298 fprintf (stdout
, " ERROR: Can't read from source!\n");
303 // Choose the right compress algorithm
305 CompressFunc
= (CompressType
== EFI_COMPRESS
) ? EfiCompress
: TianoCompress
;
308 // Get destination data size and do the compression
311 Status
= CompressFunc (SrcBuffer
, SrcSize
, DstBuffer
, &DstSize
);
312 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
313 fprintf (stdout
, " Error: Compress failed: %x!\n", Status
);
316 if ((DstBuffer
= malloc (DstSize
)) == NULL
) {
317 fprintf (stdout
, " ERROR: Can't allocate memory!\n");
321 Status
= CompressFunc (SrcBuffer
, SrcSize
, DstBuffer
, &DstSize
);
322 if (EFI_ERROR (Status
)) {
323 fprintf (stdout
, " ERROR: Compress Error!\n");
327 fprintf (stdout
, " Orig Size = %ld\tComp Size = %ld\n", SrcSize
, DstSize
);
329 if (DstBuffer
== NULL
) {
330 fprintf (stdout
, " ERROR: No destination to write to!\n");
335 // Write out the result
337 if (fwrite (DstBuffer
, 1, DstSize
, OutFileP
) != DstSize
) {
338 fprintf (stdout
, " ERROR: Can't write to destination file!\n");
371 "Usage: %s [-tCompressType] InFileName OutFileName\n"
372 " %*c [[-tCompressType] InFileName OutFileName ...]\n"
375 " CompressType - optional compress algorithm (EFI | Tiano), case insensitive.\n"
376 " If ommitted, compress type specified ahead is used, \n"
378 " e.g.: EfiCompress a.in a.out -tTiano b.in b.out \\ \n"
379 " c.in c.out -tEFI d.in d.out\n"
380 " a.in and d.in are compressed using EFI compress algorithm\n"
381 " b.in and c.in are compressed using Tiano compress algorithm\n"
382 " InFileName - input file path\n"
383 " OutFileName - output file path\n",
384 ExeName
, strlen(ExeName
), ' '