]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/EfiCompress/EfiCompressMain.c
Adding Additional Tools that are needed for Platform Image creation.
[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 "TianoCommon.h"
29 #include "EfiCompress.h"
30
31 int
32 main (
33 INT32 argc,
34 CHAR8 *argv[]
35 )
36 /*++
37
38 Routine Description:
39
40 Compresses the input files
41
42 Arguments:
43
44 argc - number of arguments passed into the command line.
45 argv[] - files to compress and files to output compressed data to.
46
47 Returns:
48
49 int: 0 for successful execution of the function.
50
51 --*/
52 {
53 EFI_STATUS Status;
54 FILE *infile;
55 FILE *outfile;
56 UINT32 SrcSize;
57 UINT32 DstSize;
58 UINT8 *SrcBuffer;
59 UINT8 *DstBuffer;
60 UINT8 Buffer[8];
61
62 //
63 // Added for makefile debug - KCE
64 //
65 INT32 arg_counter;
66 printf ("\n\n");
67 for (arg_counter = 0; arg_counter < argc; arg_counter++) {
68 printf ("%s ", argv[arg_counter]);
69 }
70
71 printf ("\n\n");
72
73 SrcBuffer = DstBuffer = NULL;
74
75 infile = outfile = NULL;
76
77 if (argc != 3) {
78 printf ("Usage: EFICOMPRESS <infile> <outfile>\n");
79 goto Done;
80 }
81
82 if ((outfile = fopen (argv[2], "wb")) == NULL) {
83 printf ("Can't open output file\n");
84 goto Done;
85 }
86
87 if ((infile = fopen (argv[1], "rb")) == NULL) {
88 printf ("Can't open input file\n");
89 goto Done;
90 }
91 //
92 // Get the size of source file
93 //
94 SrcSize = 0;
95 while (fread (Buffer, 1, 1, infile)) {
96 SrcSize++;
97
98 }
99 //
100 // Read in the source data
101 //
102 if ((SrcBuffer = malloc (SrcSize)) == NULL) {
103 printf ("Can't allocate memory\n");
104 goto Done;
105 }
106
107 rewind (infile);
108 if (fread (SrcBuffer, 1, SrcSize, infile) != SrcSize) {
109 printf ("Can't read from source\n");
110 goto Done;
111 }
112 //
113 // Get destination data size and do the compression
114 //
115 DstSize = 0;
116 Status = Compress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
117 if (Status == EFI_BUFFER_TOO_SMALL) {
118 if ((DstBuffer = malloc (DstSize)) == NULL) {
119 printf ("Can't allocate memory\n");
120 goto Done;
121 }
122
123 Status = Compress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
124 }
125
126 if (EFI_ERROR (Status)) {
127 printf ("Compress Error\n");
128 goto Done;
129 }
130
131 printf ("\nOrig Size = %ld\n", SrcSize);
132 printf ("Comp Size = %ld\n", DstSize);
133
134 if (DstBuffer == NULL) {
135 printf ("No destination to write to.\n");
136 goto Done;
137 }
138 //
139 // Write out the result
140 //
141 if (fwrite (DstBuffer, 1, DstSize, outfile) != DstSize) {
142 printf ("Can't write to destination file\n");
143 }
144
145 Done:
146 if (SrcBuffer) {
147 free (SrcBuffer);
148 }
149
150 if (DstBuffer) {
151 free (DstBuffer);
152 }
153
154 if (infile) {
155 fclose (infile);
156 }
157
158 if (outfile) {
159 fclose (outfile);
160 }
161
162 return 0;
163 }