]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
BootSectImage.exe, EfiLdrImage.exe and Split.exe tools have incorrect output from...
[mirror_edk2.git] / BaseTools / Source / C / Split / Split.c
1 /** @file
2
3 Split a file into two pieces at the request offset.
4
5 Copyright (c) 1999 - 2014, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 // GC_TODO: fix comment to start with /*++
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #ifdef __GNUC__
21 #include <unistd.h>
22 #else
23 #include <direct.h>
24 #endif
25 #include <ctype.h>
26 #include "ParseInf.h"
27 #include "CommonLib.h"
28 #include "EfiUtilityMsgs.h"
29 //
30 // Utility Name
31 //
32 #define UTILITY_NAME "Split"
33
34 //
35 // Utility version information
36 //
37 #define UTILITY_MAJOR_VERSION 0
38 #define UTILITY_MINOR_VERSION 1
39
40 void
41 Version (
42 void
43 )
44 /*++
45
46 Routine Description:
47
48 Displays the standard utility information to SDTOUT
49
50 Arguments:
51
52 None
53
54 Returns:
55
56 None
57
58 --*/
59 {
60 printf ("%s Version %d.%d %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
61 printf ("Copyright (c) 1999-2014 Intel Corporation. All rights reserved.\n");
62 printf ("\n SplitFile creates two Binary files either in the same directory as the current working\n");
63 printf (" directory or in the specified directory.\n");
64 }
65
66 void
67 Usage (
68 void
69 )
70 /*++
71
72 Routine Description:
73
74 GC_TODO: Add function description
75
76 Arguments:
77
78
79 Returns:
80
81 GC_TODO: add return values
82
83 --*/
84 {
85 Version();
86 printf ("\nUsage: \n\
87 Split\n\
88 -f, --filename inputFile to split\n\
89 -s, --split VALUE the number of bytes in the first file\n\
90 [-p, --prefix OutputDir]\n\
91 [-o, --firstfile Filename1]\n\
92 [-t, --secondfile Filename2]\n\
93 [-v, --verbose]\n\
94 [--version]\n\
95 [-q, --quiet disable all messages except fatal errors]\n\
96 [-d, --debug[#]\n\
97 [-h, --help]\n");
98 }
99
100 EFI_STATUS
101 GetSplitValue (
102 IN CONST CHAR8* SplitValueString,
103 OUT UINT64 *ReturnValue
104 )
105 {
106 UINT64 len = strlen(SplitValueString);
107 UINT64 base = 1;
108 UINT64 index = 0;
109 UINT64 number = 0;
110 CHAR8 lastCHAR = 0;
111 EFI_STATUS Status = EFI_SUCCESS;
112
113 if (len == 0) {
114 return EFI_ABORTED;
115 }
116
117 Status = AsciiStringToUint64 (SplitValueString, FALSE, ReturnValue);
118 if (!EFI_ERROR (Status)) {
119 return Status;
120 }
121
122 if (SplitValueString[0] == '0' && (SplitValueString[1] == 'x' || SplitValueString[1] == 'X')) {
123 Status = AsciiStringToUint64 (SplitValueString, TRUE, ReturnValue);
124 if (!EFI_ERROR (Status)) {
125 return Status;
126 }
127 }
128
129 lastCHAR = (CHAR8)toupper((int)SplitValueString[len - 1]);
130
131 if (lastCHAR != 'K' && lastCHAR != 'M' && lastCHAR != 'G') {
132 return STATUS_ERROR;
133 }
134
135 for (;index < len - 1; ++index) {
136 if (!isdigit((int)SplitValueString[index])) {
137 return EFI_ABORTED;
138 }
139 }
140
141 number = atol (SplitValueString);
142 if (lastCHAR == 'K')
143 base = 1024;
144 else if (lastCHAR == 'M')
145 base = 1024*1024;
146 else
147 base = 1024*1024*1024;
148
149 *ReturnValue = number*base;
150
151 return EFI_SUCCESS;
152 }
153
154 EFI_STATUS
155 CountVerboseLevel (
156 IN CONST CHAR8* VerboseLevelString,
157 IN CONST UINT64 Length,
158 OUT UINT64 *ReturnValue
159 )
160 {
161 UINT64 i = 0;
162 for (;i < Length; ++i) {
163 if (VerboseLevelString[i] != 'v' && VerboseLevelString[i] != 'V') {
164 return EFI_ABORTED;
165 }
166 ++(*ReturnValue);
167 }
168
169 return EFI_SUCCESS;
170 }
171
172 EFI_STATUS
173 CreateDir (
174 IN OUT CHAR8** FullFileName
175 )
176 {
177 CHAR8* temp = *FullFileName;
178 CHAR8* start = temp;
179 UINT64 index = 0;
180
181 for (;index < strlen(temp); ++index) {
182 if (temp[index] == '\\' || temp[index] == '/') {
183 temp[index] = 0;
184 if (chdir(start)) {
185 if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
186 return EFI_ABORTED;
187 }
188 chdir(start);
189 }
190 start = temp + index + 1;
191 temp[index] = '/';
192 }
193 }
194
195 return EFI_SUCCESS;
196 }
197
198 int
199 main (
200 int argc,
201 char*argv[]
202 )
203 /*++
204
205 Routine Description:
206
207 GC_TODO: Add function description
208
209 Arguments:
210
211 argc - GC_TODO: add argument description
212 ] - GC_TODO: add argument description
213
214 Returns:
215
216 GC_TODO: add return values
217
218 --*/
219 {
220 EFI_STATUS Status = EFI_SUCCESS;
221 FILE *In;
222 CHAR8 *InputFileName = NULL;
223 CHAR8 *OutputDir = NULL;
224 CHAR8 *OutFileName1 = NULL;
225 CHAR8 *OutFileName2 = NULL;
226 UINT64 SplitValue = (UINT64) -1;
227 FILE *Out1;
228 FILE *Out2;
229 CHAR8 *OutName1 = NULL;
230 CHAR8 *OutName2 = NULL;
231 CHAR8 *CurrentDir = NULL;
232 UINT64 Index;
233 CHAR8 CharC;
234 UINT64 DebugLevel = 0;
235 UINT64 VerboseLevel = 0;
236
237 SetUtilityName(UTILITY_NAME);
238 if (argc == 1) {
239 Usage();
240 return STATUS_ERROR;
241 }
242
243 argc --;
244 argv ++;
245
246 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
247 Usage();
248 return STATUS_SUCCESS;
249 }
250
251 if (stricmp (argv[0], "--version") == 0) {
252 Version();
253 return STATUS_SUCCESS;
254 }
255
256 while (argc > 0) {
257 if ((stricmp (argv[0], "-p") == 0) || (stricmp (argv[0], "--prefix") == 0)) {
258 OutputDir = argv[1];
259 if (OutputDir == NULL) {
260 Warning (NULL, 0, 0, "NO output directory specified.", NULL);
261 return STATUS_ERROR;
262 }
263 argc -= 2;
264 argv += 2;
265 continue;
266 }
267
268 if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--filename") == 0)) {
269 InputFileName = argv[1];
270 if (InputFileName == NULL) {
271 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
272 return STATUS_ERROR;
273 }
274 argc -= 2;
275 argv += 2;
276 continue;
277 }
278
279 if ((stricmp (argv[0], "-s") == 0) || (stricmp (argv[0], "--split") == 0)) {
280 Status = GetSplitValue(argv[1], &SplitValue);
281 if (EFI_ERROR (Status)) {
282 Error (NULL, 0, 0x1003, "Input split value is not one valid integer.", NULL);
283 return STATUS_ERROR;
284 }
285 argc -= 2;
286 argv += 2;
287 continue;
288 }
289
290 if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--firstfile") == 0)) {
291 OutFileName1 = argv[1];
292 if (OutFileName1 == NULL) {
293 Warning (NULL, 0, 0, NULL, "No output file1 specified.");
294 }
295 argc -= 2;
296 argv += 2;
297 continue;
298 }
299
300 if ((stricmp (argv[0], "-t") == 0) || (stricmp (argv[0], "--secondfile") == 0)) {
301 OutFileName2 = argv[1];
302 if (OutFileName2 == NULL) {
303 Warning (NULL, 0, 0, NULL, "No output file2 specified.");
304 }
305 argc -= 2;
306 argv += 2;
307 continue;
308 }
309
310 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
311 argc --;
312 argv ++;
313 continue;
314 }
315
316 if ((strlen(argv[0]) >= 2 && argv[0][0] == '-' && (argv[0][1] == 'v' || argv[0][1] == 'V')) || (stricmp (argv[0], "--verbose") == 0)) {
317 VerboseLevel = 1;
318 if (strlen(argv[0]) > 2) {
319 Status = CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &VerboseLevel);
320 if (EFI_ERROR (Status)) {
321 Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);
322 return STATUS_ERROR;
323 }
324 }
325
326 argc --;
327 argv ++;
328 continue;
329 }
330
331 if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) {
332 Status = AsciiStringToUint64 (argv[1], FALSE, &DebugLevel);
333 if (EFI_ERROR (Status)) {
334 Error (NULL, 0, 0x1003, "Input debug level is not one valid integrator.", NULL);
335 return STATUS_ERROR;
336 }
337 argc -= 2;
338 argv += 2;
339 continue;
340 }
341 //
342 // Don't recognize the paramter.
343 //
344 Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);
345 return STATUS_ERROR;
346 }
347
348 if (InputFileName == NULL) {
349 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
350 return STATUS_ERROR;
351 }
352
353 In = fopen (LongFilePath (InputFileName), "rb");
354 if (In == NULL) {
355 // ("Unable to open file \"%s\"\n", InputFileName);
356 Error (InputFileName, 0, 1, "File open failure", NULL);
357 return STATUS_ERROR;
358 }
359
360 if (OutFileName1 == NULL) {
361 OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16);
362 if (OutName1 == NULL) {
363 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
364 return STATUS_ERROR;
365 }
366 strcpy (OutName1, InputFileName);
367 strcat (OutName1, "1");
368 OutFileName1 = OutName1;
369
370 }
371 if (OutFileName2 == NULL) {
372 OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16);
373 if (OutName2 == NULL) {
374 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
375 return STATUS_ERROR;
376 }
377 strcpy (OutName2, InputFileName);
378 strcat (OutName2, "2");
379 OutFileName2 = OutName2;
380
381 }
382
383 if (OutputDir != NULL) {
384 //OutputDirSpecified = TRUE;
385 if (chdir(OutputDir) != 0) {
386 Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");
387 return STATUS_ERROR;
388 }
389 }
390
391 CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0);
392 if (EFI_ERROR(CreateDir(&OutFileName1))) {
393 Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);
394 return STATUS_ERROR;
395 }
396 chdir(CurrentDir);
397
398 if (EFI_ERROR(CreateDir(&OutFileName2))) {
399 Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);
400 return STATUS_ERROR;
401 }
402 chdir(CurrentDir);
403 free(CurrentDir);
404
405 Out1 = fopen (LongFilePath (OutFileName1), "wb");
406 if (Out1 == NULL) {
407 // ("Unable to open file \"%s\"\n", OutFileName1);
408 Error (OutFileName1, 0, 1, "File open failure", NULL);
409 return STATUS_ERROR;
410 }
411
412 Out2 = fopen (LongFilePath (OutFileName2), "wb");
413 if (Out2 == NULL) {
414 // ("Unable to open file \"%s\"\n", OutFileName2);
415 Error (OutFileName2, 0, 1, "File open failure", NULL);
416 return STATUS_ERROR;
417 }
418
419 for (Index = 0; Index < SplitValue; Index++) {
420 CharC = (CHAR8) fgetc (In);
421 if (feof (In)) {
422 break;
423 }
424
425 fputc (CharC, Out1);
426 }
427
428 for (;;) {
429 CharC = (CHAR8) fgetc (In);
430 if (feof (In)) {
431 break;
432 }
433
434 fputc (CharC, Out2);
435 }
436
437 if (OutName1 != NULL) {
438 free(OutName1);
439 }
440 if (OutName2 != NULL) {
441 free(OutName2);
442 }
443 fclose (In);
444 fclose (Out1);
445 fclose (Out2);
446
447 return STATUS_SUCCESS;
448 }