PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/edk2/EdkCompatibilityPkg/Sample/Tools/Source/EfiCompress/EfiCompressMain.c

https://gitlab.com/envieidoc/Clover
C | 399 lines | 245 code | 46 blank | 108 comment | 55 complexity | b272e5a40e7775e1f6cb6b77dbbd9e37 MD5 | raw file
  1. /*++
  2. Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials
  4. are licensed and made available under the terms and conditions of the BSD License
  5. which accompanies this distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. EfiCompressMain.c
  11. Abstract:
  12. --*/
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include <assert.h>
  17. #include <stdarg.h>
  18. #include <stdio.h>
  19. #include "TianoCommon.h"
  20. #include "Compress.h"
  21. #define UTILITY_VERSION "v1.0"
  22. #define UTILITY_NAME "EfiCompress"
  23. typedef enum {
  24. EFI_COMPRESS = 1,
  25. TIANO_COMPRESS = 2
  26. } COMPRESS_TYPE;
  27. typedef struct _COMPRESS_ACTION_LIST {
  28. struct _COMPRESS_ACTION_LIST *NextAction;
  29. INT32 CompressType;
  30. CHAR8 *InFileName;
  31. CHAR8 *OutFileName;
  32. } COMPRESS_ACTION_LIST;
  33. STATIC
  34. BOOLEAN
  35. ParseCommandLine (
  36. INT32 argc,
  37. CHAR8 *argv[],
  38. COMPRESS_ACTION_LIST **ActionListHead
  39. )
  40. /*++
  41. Routine Description:
  42. Parse command line options
  43. Arguments:
  44. argc - number of arguments passed into the command line.
  45. argv[] - files to compress and files to output compressed data to.
  46. Options - Point to COMMAND_LINE_OPTIONS, receiving command line options.
  47. Returns:
  48. BOOLEAN: TRUE for a successful parse.
  49. --*/
  50. ;
  51. STATIC
  52. VOID
  53. Usage (
  54. CHAR8 *ExeName
  55. )
  56. /*++
  57. Routine Description:
  58. Print usage.
  59. Arguments:
  60. ExeName - Application's full path
  61. --*/
  62. ;
  63. STATIC
  64. BOOLEAN
  65. ProcessFile (
  66. CHAR8 *InFileName,
  67. CHAR8 *OutFileName,
  68. COMPRESS_TYPE CompressType
  69. )
  70. /*++
  71. Routine Description:
  72. Compress InFileName to OutFileName using algorithm specified by CompressType.
  73. Arguments:
  74. InFileName - Input file to compress
  75. OutFileName - Output file compress to
  76. CompressType - Compress algorithm, can be EFI_COMPRESS or TIANO_COMPRESS
  77. Returns:
  78. BOOLEAN: TRUE for compress file successfully
  79. --*/
  80. ;
  81. int
  82. main (
  83. INT32 argc,
  84. CHAR8 *argv[]
  85. )
  86. /*++
  87. Routine Description:
  88. Compresses the input files
  89. Arguments:
  90. argc - number of arguments passed into the command line.
  91. argv[] - files to compress and files to output compressed data to.
  92. Returns:
  93. int: 0 for successful execution of the function.
  94. --*/
  95. {
  96. COMPRESS_ACTION_LIST *ActionList;
  97. COMPRESS_ACTION_LIST *NextAction;
  98. UINT32 ActionCount;
  99. UINT32 SuccessCount;
  100. ActionList = NULL;
  101. ActionCount = SuccessCount = 0;
  102. if (!ParseCommandLine (argc, argv, &ActionList)) {
  103. Usage (*argv);
  104. return 1;
  105. }
  106. while (ActionList != NULL) {
  107. ++ActionCount;
  108. if (ProcessFile (
  109. ActionList->InFileName,
  110. ActionList->OutFileName,
  111. ActionList->CompressType)
  112. ) {
  113. ++SuccessCount;
  114. }
  115. NextAction = ActionList;
  116. ActionList = ActionList->NextAction;
  117. free (NextAction);
  118. }
  119. fprintf (stdout, "\nCompressed %d files, %d succeed!\n", ActionCount, SuccessCount);
  120. if (SuccessCount < ActionCount) {
  121. return 1;
  122. }
  123. return 0;
  124. }
  125. STATIC
  126. BOOLEAN
  127. ParseCommandLine (
  128. INT32 argc,
  129. CHAR8 *argv[],
  130. COMPRESS_ACTION_LIST **ActionListHead
  131. )
  132. {
  133. COMPRESS_TYPE CurrentType;
  134. COMPRESS_ACTION_LIST **Action;
  135. Action = ActionListHead;
  136. CurrentType = EFI_COMPRESS; // default compress algorithm
  137. // Skip Exe Name
  138. --argc;
  139. ++argv;
  140. while (argc > 0) {
  141. if (strcmp (*argv, "-h") == 0 || strcmp (*argv, "-?") == 0) {
  142. //
  143. // 1. Directly return, help message will be printed.
  144. //
  145. return FALSE;
  146. } else if (strncmp (*argv, "-t", 2) == 0) {
  147. //
  148. // 2. Specifying CompressType
  149. //
  150. if (_stricmp ((*argv)+2, "EFI") == 0) {
  151. CurrentType = EFI_COMPRESS;
  152. } else if (_stricmp ((*argv)+2, "Tiano") == 0) {
  153. CurrentType = TIANO_COMPRESS;
  154. } else {
  155. fprintf (stdout, " ERROR: CompressType %s not supported!\n", (*argv)+2);
  156. return FALSE;
  157. }
  158. } else {
  159. //
  160. // 3. Current parameter is *FileName
  161. //
  162. if (*Action == NULL) {
  163. //
  164. // need to create a new action item
  165. //
  166. *Action = (COMPRESS_ACTION_LIST*) malloc (sizeof **Action);
  167. if (*Action == NULL) {
  168. fprintf (stdout, " ERROR: malloc failed!\n");
  169. return FALSE;
  170. }
  171. memset (*Action, 0, sizeof **Action);
  172. (*Action)->CompressType = CurrentType;
  173. }
  174. //
  175. // Assignment to InFileName and OutFileName in order
  176. //
  177. if ((*Action)->InFileName == NULL) {
  178. (*Action)->InFileName = *argv;
  179. } else {
  180. (*Action)->OutFileName = *argv;
  181. Action = &(*Action)->NextAction;
  182. }
  183. }
  184. --argc;
  185. ++argv;
  186. }
  187. if (*Action != NULL) {
  188. assert ((*Action)->InFileName != NULL);
  189. fprintf (stdout, " ERROR: Compress OutFileName not specified with InFileName: %s!\n", (*Action)->InFileName);
  190. return FALSE;
  191. }
  192. if (*ActionListHead == NULL) {
  193. return FALSE;
  194. }
  195. return TRUE;
  196. }
  197. STATIC
  198. BOOLEAN
  199. ProcessFile (
  200. CHAR8 *InFileName,
  201. CHAR8 *OutFileName,
  202. COMPRESS_TYPE CompressType
  203. )
  204. {
  205. EFI_STATUS Status;
  206. FILE *InFileP;
  207. FILE *OutFileP;
  208. UINT32 SrcSize;
  209. UINT32 DstSize;
  210. UINT8 *SrcBuffer;
  211. UINT8 *DstBuffer;
  212. COMPRESS_FUNCTION CompressFunc;
  213. SrcBuffer = DstBuffer = NULL;
  214. InFileP = OutFileP = NULL;
  215. fprintf (stdout, "%s --> %s\n", InFileName, OutFileName);
  216. if ((OutFileP = fopen (OutFileName, "wb")) == NULL) {
  217. fprintf (stdout, " ERROR: Can't open output file %s for write!\n", OutFileName);
  218. goto ErrorHandle;
  219. }
  220. if ((InFileP = fopen (InFileName, "rb")) == NULL) {
  221. fprintf (stdout, " ERROR: Can't open input file %s for read!\n", InFileName);
  222. goto ErrorHandle;
  223. }
  224. //
  225. // Get the size of source file
  226. //
  227. fseek (InFileP, 0, SEEK_END);
  228. SrcSize = ftell (InFileP);
  229. rewind (InFileP);
  230. //
  231. // Read in the source data
  232. //
  233. if ((SrcBuffer = malloc (SrcSize)) == NULL) {
  234. fprintf (stdout, " ERROR: Can't allocate memory!\n");
  235. goto ErrorHandle;
  236. }
  237. if (fread (SrcBuffer, 1, SrcSize, InFileP) != SrcSize) {
  238. fprintf (stdout, " ERROR: Can't read from source!\n");
  239. goto ErrorHandle;
  240. }
  241. //
  242. // Choose the right compress algorithm
  243. //
  244. CompressFunc = (CompressType == EFI_COMPRESS) ? EfiCompress : TianoCompress;
  245. //
  246. // Get destination data size and do the compression
  247. //
  248. DstSize = 0;
  249. Status = CompressFunc (SrcBuffer, SrcSize, DstBuffer, &DstSize);
  250. if (Status != EFI_BUFFER_TOO_SMALL) {
  251. fprintf (stdout, " Error: Compress failed: %x!\n", Status);
  252. goto ErrorHandle;
  253. }
  254. if ((DstBuffer = malloc (DstSize)) == NULL) {
  255. fprintf (stdout, " ERROR: Can't allocate memory!\n");
  256. goto ErrorHandle;
  257. }
  258. Status = CompressFunc (SrcBuffer, SrcSize, DstBuffer, &DstSize);
  259. if (EFI_ERROR (Status)) {
  260. fprintf (stdout, " ERROR: Compress Error!\n");
  261. goto ErrorHandle;
  262. }
  263. fprintf (stdout, " Orig Size = %ld\tComp Size = %ld\n", SrcSize, DstSize);
  264. if (DstBuffer == NULL) {
  265. fprintf (stdout, " ERROR: No destination to write to!\n");
  266. goto ErrorHandle;
  267. }
  268. //
  269. // Write out the result
  270. //
  271. if (fwrite (DstBuffer, 1, DstSize, OutFileP) != DstSize) {
  272. fprintf (stdout, " ERROR: Can't write to destination file!\n");
  273. goto ErrorHandle;
  274. }
  275. return TRUE;
  276. ErrorHandle:
  277. if (SrcBuffer) {
  278. free (SrcBuffer);
  279. }
  280. if (DstBuffer) {
  281. free (DstBuffer);
  282. }
  283. if (InFileP) {
  284. fclose (InFileP);
  285. }
  286. if (OutFileP) {
  287. fclose (OutFileP);
  288. }
  289. return FALSE;
  290. }
  291. VOID
  292. Usage (
  293. CHAR8 *ExeName
  294. )
  295. {
  296. int Index;
  297. const char *Str[] = {
  298. UTILITY_NAME" "UTILITY_VERSION" - Intel EFI Compress Utility",
  299. " Copyright (C), 2006 - 2008 Intel Corporation",
  300. #if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
  301. " Built from "UTILITY_BUILD", project of "UTILITY_VENDOR,
  302. #endif
  303. "",
  304. "Usage:",
  305. " "UTILITY_NAME" [OPTION] SOURCE DEST ...",
  306. "Description:",
  307. " Compress a list of SOURCE(s) to accordingly DEST(s) using the specified",
  308. " compress algorithm.",
  309. "Options:",
  310. " -tCompressAlgo Optional compress algorithm (EFI | Tiano), case insensitive.",
  311. " If ommitted, compress type specified ahead is used,",
  312. " default is EFI\n"
  313. " e.g.: EfiCompress a.in a.out -tTiano b.in b.out \\",
  314. " c.in c.out -tEFI d.in d.out",
  315. " a.in and d.in are compressed using EFI compress algorithm",
  316. " b.in and c.in are compressed using Tiano compress algorithm",
  317. NULL
  318. };
  319. for (Index = 0; Str[Index] != NULL; Index++) {
  320. fprintf (stdout, "%s\n", Str[Index]);
  321. }
  322. }