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