summaryrefslogtreecommitdiff
path: root/Tools/CCode/Source/EfiCompress/EfiCompressMain.c
blob: db27a33a5bf12241750c61c3507919c06774c0d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*++

Copyright (c)  1999-2006 Intel Corporation. All rights reserved
This program and the accompanying materials are licensed and made available 
under the terms and conditions of the BSD License which accompanies this 
distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.


Module Name:
  
  EfiCompressMain.c

Abstract:

  The main function for the compression utility.
  
--*/

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>

#include <Common/UefiBaseTypes.h>
#include "Compress.h"

#define UTILITY_NAME "EfiCompress"
#define UTILITY_MAJOR_VERSION 1
#define UTILITY_MINOR_VERSION 1

void 
Version(
  void
  )
/*++

Routine Description:

  Print out version information for EfiCompress.

Arguments:

  None
  
Returns:

  None
  
--*/ 
{
  printf ("%s v%d.%d -Efi File Compress Utility\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
  printf ("Copyright (c) 2005-2006 Intel Corporation. All rights reserved.\n");
}

void 
Usage(
  void
  )
/*++

Routine Description:

  Print out usage information for EfiCompress.

Arguments:

  None
  
Returns:

  None
  
--*/ 
{
  Version();
  printf ("\nUsage: %s Inputfile Outputfile\n", UTILITY_NAME);
}


int
main (
  INT32 argc,
  CHAR8 *argv[]
  )
/*++

Routine Description:

  Compresses the input files

Arguments:

  argc   - number of arguments passed into the command line.
  argv[] - files to compress and files to output compressed data to.

Returns:

  int: 0 for successful execution of the function.

--*/
{
  EFI_STATUS  Status;
  FILE        *infile;
  FILE        *outfile;
  UINT32      SrcSize;
  UINT32      DstSize;
  UINT8       *SrcBuffer;
  UINT8       *DstBuffer;
  UINT8       Buffer[8];

  //
  //  Added for makefile debug - KCE
  //
  INT32       arg_counter;
 
  SrcBuffer             = DstBuffer = NULL;
  infile                = outfile = NULL;

  if (argc == 1) {
    Usage();
    goto Done;
  }
  
  if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||
      (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {
    Usage();
    goto Done;
  }
  
  if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {
    Version();
    goto Done;
  }
  
  if (argc != 3) {
    Usage();
    goto Done;
  }

  if ((outfile = fopen (argv[2], "wb")) == NULL) {
    printf ("Can't open output file\n");
    goto Done;
  }

  if ((infile = fopen (argv[1], "rb")) == NULL) {
    printf ("Can't open input file\n");
    goto Done;
  }
  //
  // Get the size of source file
  //
  SrcSize = 0;
  while (fread (Buffer, 1, 1, infile)) {
    SrcSize++;

  }
  //
  // Read in the source data
  //
  if ((SrcBuffer = malloc (SrcSize)) == NULL) {
    printf ("Can't allocate memory\n");
    goto Done;
  }

  rewind (infile);
  if (fread (SrcBuffer, 1, SrcSize, infile) != SrcSize) {
    printf ("Can't read from source\n");
    goto Done;
  }
  //
  // Get destination data size and do the compression
  //
  DstSize = 0;
  Status  = EfiCompress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
  if (Status == EFI_BUFFER_TOO_SMALL) {
    if ((DstBuffer = malloc (DstSize)) == NULL) {
      printf ("Can't allocate memory\n");
      goto Done;
    }

    Status = EfiCompress (SrcBuffer, SrcSize, DstBuffer, &DstSize);
  }

  if (EFI_ERROR (Status)) {
    printf ("Compress Error\n");
    goto Done;
  }

  printf ("\nOrig Size = %ld\n", SrcSize);
  printf ("Comp Size = %ld\n", DstSize);

  if (DstBuffer == NULL) {
    printf ("No destination to write to.\n");
    goto Done;
  }
  //
  // Write out the result
  //
  if (fwrite (DstBuffer, 1, DstSize, outfile) != DstSize) {
    printf ("Can't write to destination file\n");
  }

Done:
  if (SrcBuffer) {
    free (SrcBuffer);
  }

  if (DstBuffer) {
    free (DstBuffer);
  }

  if (infile) {
    fclose (infile);
  }

  if (outfile) {
    fclose (outfile);
  }

  return 0;
}