]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/CCode/Source/EfiCompress/EfiCompressMain.c
The main issue want to be resolve is that some tools need EfiCompress and other tools...
[mirror_edk2.git] / Tools / CCode / Source / EfiCompress / EfiCompressMain.c
1 /*++
2
3 Copyright (c) 1999-2006 Intel Corporation. All rights reserved
4 This program and the accompanying materials are licensed and made available
5 under the terms and conditions of the BSD License which accompanies this
6 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
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
29 #include <Common/UefiBaseTypes.h>
30 #include "Compress.h"
31
32 #define UTILITY_NAME "EfiCompress"
33 #define UTILITY_MAJOR_VERSION 1
34 #define UTILITY_MINOR_VERSION 1
35
36 void
37 ECVersion(
38 void
39 )
40 /*++
41
42 Routine Description:
43
44 Print out version information for EfiCompress.
45
46 Arguments:
47
48 None
49
50 Returns:
51
52 None
53
54 --*/
55 {
56 printf ("%s v%d.%d -EDK Efi Compress Utility\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
57 printf ("Copyright (c) 2005-2006 Intel Corporation. All rights reserved.\n");
58 }
59
60 void
61 ECUsage(
62 void
63 )
64 /*++
65
66 Routine Description:
67
68 Print out usage information for EfiCompress.
69
70 Arguments:
71
72 None
73
74 Returns:
75
76 None
77
78 --*/
79 {
80 ECVersion();
81 printf ("\n Usage: %s Inputfile Outputfile\n", UTILITY_NAME);
82 }
83
84
85 int
86 main (
87 INT32 argc,
88 CHAR8 *argv[]
89 )
90 /*++
91
92 Routine Description:
93
94 Compresses the input files
95
96 Arguments:
97
98 argc - number of arguments passed into the command line.
99 argv[] - files to compress and files to output compressed data to.
100
101 Returns:
102
103 int: 0 for successful execution of the function.
104
105 --*/
106 {
107 EFI_STATUS Status;
108 FILE *infile;
109 FILE *outfile;
110 UINT32 SrcSize;
111 UINT32 DstSize;
112 UINT8 *SrcBuffer;
113 UINT8 *DstBuffer;
114 UINT8 Buffer[8];
115
116 //
117 // Added for makefile debug - KCE
118 //
119 INT32 arg_counter;
120
121 SrcBuffer = DstBuffer = NULL;
122 infile = outfile = NULL;
123
124 if (argc < 1) {
125 ECUsage();
126 goto Done;
127 }
128
129 if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||
130 (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {
131 ECUsage();
132 goto Done;
133 }
134
135 if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {
136 ECVersion();
137 goto Done;
138 }
139
140 if (argc != 3) {
141 ECUsage();
142 goto Done;
143 }
144
145 if ((outfile = fopen (argv[2], "wb")) == NULL) {
146 printf ("Can't open output file\n");
147 goto Done;
148 }
149
150 if ((infile = fopen (argv[1], "rb")) == NULL) {
151 printf ("Can't open input file\n");
152 goto Done;
153 }
154 //
155 // Get the size of source file
156 //
157 SrcSize = 0;
158 while (fread (Buffer, 1, 1, infile)) {
159 SrcSize++;
160
161 }
162 //
163 // Read in the source data
164 //
165 if ((SrcBuffer = malloc (SrcSize)) == NULL) {
166 printf ("Can't allocate memory\n");
167 goto Done;
168 }
169
170 rewind (infile);
171 if (fread (SrcBuffer, 1, SrcSize, infile) != SrcSize) {
172 printf ("Can't read from source\n");
173 goto Done;
174 }
175 //
176 // Get destination data size and do the compression
177 //
178 DstSize = 0;
179 Status = EfiCompress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
180 if (Status == EFI_BUFFER_TOO_SMALL) {
181 if ((DstBuffer = malloc (DstSize)) == NULL) {
182 printf ("Can't allocate memory\n");
183 goto Done;
184 }
185
186 Status = EfiCompress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
187 }
188
189 if (EFI_ERROR (Status)) {
190 printf ("Compress Error\n");
191 goto Done;
192 }
193
194 printf ("\nOrig Size = %ld\n", SrcSize);
195 printf ("Comp Size = %ld\n", DstSize);
196
197 if (DstBuffer == NULL) {
198 printf ("No destination to write to.\n");
199 goto Done;
200 }
201 //
202 // Write out the result
203 //
204 if (fwrite (DstBuffer, 1, DstSize, outfile) != DstSize) {
205 printf ("Can't write to destination file\n");
206 }
207
208 Done:
209 if (SrcBuffer) {
210 free (SrcBuffer);
211 }
212
213 if (DstBuffer) {
214 free (DstBuffer);
215 }
216
217 if (infile) {
218 fclose (infile);
219 }
220
221 if (outfile) {
222 fclose (outfile);
223 }
224
225 return 0;
226 }