]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/EfiCompress/EfiCompressMain.c
5964eb544cbfc1febf6ab2f17ea4492979970b8d
[mirror_edk2.git] / Tools / Source / TianoTools / EfiCompress / EfiCompressMain.c
1 /*++
2
3 Copyright (c) 1999 - 2002 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
10 Intel Corporation.
11
12
13 Module Name:
14
15 EfiCompressMain.c
16
17 Abstract:
18
19 The main function for the compression utility.
20
21 --*/
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <UefiBaseTypes.h>
29 #include <Base.h>
30 #include "EfiCompress.h"
31
32 int
33 main (
34 INT32 argc,
35 CHAR8 *argv[]
36 )
37 /*++
38
39 Routine Description:
40
41 Compresses the input files
42
43 Arguments:
44
45 argc - number of arguments passed into the command line.
46 argv[] - files to compress and files to output compressed data to.
47
48 Returns:
49
50 int: 0 for successful execution of the function.
51
52 --*/
53 {
54 EFI_STATUS Status;
55 FILE *infile;
56 FILE *outfile;
57 UINT32 SrcSize;
58 UINT32 DstSize;
59 UINT8 *SrcBuffer;
60 UINT8 *DstBuffer;
61 UINT8 Buffer[8];
62
63 //
64 // Added for makefile debug - KCE
65 //
66 INT32 arg_counter;
67 printf ("\n\n");
68 for (arg_counter = 0; arg_counter < argc; arg_counter++) {
69 printf ("%s ", argv[arg_counter]);
70 }
71
72 printf ("\n\n");
73
74 SrcBuffer = DstBuffer = NULL;
75
76 infile = outfile = NULL;
77
78 if (argc != 3) {
79 printf ("Usage: EFICOMPRESS <infile> <outfile>\n");
80 goto Done;
81 }
82
83 if ((outfile = fopen (argv[2], "wb")) == NULL) {
84 printf ("Can't open output file\n");
85 goto Done;
86 }
87
88 if ((infile = fopen (argv[1], "rb")) == NULL) {
89 printf ("Can't open input file\n");
90 goto Done;
91 }
92 //
93 // Get the size of source file
94 //
95 SrcSize = 0;
96 while (fread (Buffer, 1, 1, infile)) {
97 SrcSize++;
98
99 }
100 //
101 // Read in the source data
102 //
103 if ((SrcBuffer = malloc (SrcSize)) == NULL) {
104 printf ("Can't allocate memory\n");
105 goto Done;
106 }
107
108 rewind (infile);
109 if (fread (SrcBuffer, 1, SrcSize, infile) != SrcSize) {
110 printf ("Can't read from source\n");
111 goto Done;
112 }
113 //
114 // Get destination data size and do the compression
115 //
116 DstSize = 0;
117 Status = Compress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
118 if (Status == EFI_BUFFER_TOO_SMALL) {
119 if ((DstBuffer = malloc (DstSize)) == NULL) {
120 printf ("Can't allocate memory\n");
121 goto Done;
122 }
123
124 Status = Compress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
125 }
126
127 if (EFI_ERROR (Status)) {
128 printf ("Compress Error\n");
129 goto Done;
130 }
131
132 printf ("\nOrig Size = %ld\n", SrcSize);
133 printf ("Comp Size = %ld\n", DstSize);
134
135 if (DstBuffer == NULL) {
136 printf ("No destination to write to.\n");
137 goto Done;
138 }
139 //
140 // Write out the result
141 //
142 if (fwrite (DstBuffer, 1, DstSize, outfile) != DstSize) {
143 printf ("Can't write to destination file\n");
144 }
145
146 Done:
147 if (SrcBuffer) {
148 free (SrcBuffer);
149 }
150
151 if (DstBuffer) {
152 free (DstBuffer);
153 }
154
155 if (infile) {
156 fclose (infile);
157 }
158
159 if (outfile) {
160 fclose (outfile);
161 }
162
163 return 0;
164 }