]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
BaseTools: Various typo
[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 - 2017, 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 1
38 #define UTILITY_MINOR_VERSION 0
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 Build %s\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
61 }
62
63 void
64 Usage (
65 void
66 )
67 /*++
68
69 Routine Description:
70
71 GC_TODO: Add function description
72
73 Arguments:
74
75
76 Returns:
77
78 GC_TODO: add return values
79
80 --*/
81 {
82 Version();
83 printf ("Copyright (c) 1999-2017 Intel Corporation. All rights reserved.\n");
84 printf ("\n SplitFile creates two Binary files either in the same directory as the current working\n");
85 printf (" directory or in the specified directory.\n");
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 = 0;
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 (SplitValueString != NULL){
114 len = strlen(SplitValueString);
115 }
116
117 if (len == 0) {
118 return EFI_ABORTED;
119 }
120
121 Status = AsciiStringToUint64 (SplitValueString, FALSE, ReturnValue);
122 if (!EFI_ERROR (Status)) {
123 return Status;
124 }
125
126 if (SplitValueString[0] == '0' && (SplitValueString[1] == 'x' || SplitValueString[1] == 'X')) {
127 Status = AsciiStringToUint64 (SplitValueString, TRUE, ReturnValue);
128 if (!EFI_ERROR (Status)) {
129 return Status;
130 }
131 }
132
133 lastCHAR = (CHAR8)toupper((int)SplitValueString[len - 1]);
134
135 if (lastCHAR != 'K' && lastCHAR != 'M' && lastCHAR != 'G') {
136 return STATUS_ERROR;
137 }
138
139 for (;index < len - 1; ++index) {
140 if (!isdigit((int)SplitValueString[index])) {
141 return EFI_ABORTED;
142 }
143 }
144
145 number = atol (SplitValueString);
146 if (lastCHAR == 'K')
147 base = 1024;
148 else if (lastCHAR == 'M')
149 base = 1024*1024;
150 else
151 base = 1024*1024*1024;
152
153 *ReturnValue = number*base;
154
155 return EFI_SUCCESS;
156 }
157
158 EFI_STATUS
159 CountVerboseLevel (
160 IN CONST CHAR8* VerboseLevelString,
161 IN CONST UINT64 Length,
162 OUT UINT64 *ReturnValue
163 )
164 {
165 UINT64 i = 0;
166 for (;i < Length; ++i) {
167 if (VerboseLevelString[i] != 'v' && VerboseLevelString[i] != 'V') {
168 return EFI_ABORTED;
169 }
170 ++(*ReturnValue);
171 }
172
173 return EFI_SUCCESS;
174 }
175
176 EFI_STATUS
177 CreateDir (
178 IN OUT CHAR8** FullFileName
179 )
180 {
181 CHAR8* temp = *FullFileName;
182 CHAR8* start = temp;
183 CHAR8 tempchar;
184 UINT64 index = 0;
185
186 for (;index < strlen(temp); ++index) {
187 if (temp[index] == '\\' || temp[index] == '/') {
188 if (temp[index + 1] != '\0') {
189 tempchar = temp[index + 1];
190 temp[index + 1] = 0;
191 if (chdir(start)) {
192 if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
193 return EFI_ABORTED;
194 }
195 chdir(start);
196 }
197 start = temp + index + 1;
198 temp[index] = '/';
199 temp[index + 1] = tempchar;
200 }
201 }
202 }
203
204 return EFI_SUCCESS;
205 }
206
207 int
208 main (
209 int argc,
210 char*argv[]
211 )
212 /*++
213
214 Routine Description:
215
216 GC_TODO: Add function description
217
218 Arguments:
219
220 argc - GC_TODO: add argument description
221 ] - GC_TODO: add argument description
222
223 Returns:
224
225 GC_TODO: add return values
226
227 --*/
228 {
229 EFI_STATUS Status = EFI_SUCCESS;
230 INTN ReturnStatus = STATUS_SUCCESS;
231 FILE *In;
232 CHAR8 *InputFileName = NULL;
233 CHAR8 *OutputDir = NULL;
234 CHAR8 *OutFileName1 = NULL;
235 CHAR8 *OutFileName2 = NULL;
236 UINT64 SplitValue = (UINT64) -1;
237 FILE *Out1 = NULL;
238 FILE *Out2 = NULL;
239 CHAR8 *OutName1 = NULL;
240 CHAR8 *OutName2 = NULL;
241 CHAR8 *CurrentDir = NULL;
242 UINT64 Index;
243 CHAR8 CharC;
244 UINT64 DebugLevel = 0;
245 UINT64 VerboseLevel = 0;
246
247 SetUtilityName(UTILITY_NAME);
248 if (argc == 1) {
249 Usage();
250 return STATUS_ERROR;
251 }
252
253 argc --;
254 argv ++;
255
256 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
257 Usage();
258 return STATUS_SUCCESS;
259 }
260
261 if (stricmp (argv[0], "--version") == 0) {
262 Version();
263 return STATUS_SUCCESS;
264 }
265
266 while (argc > 0) {
267 if ((stricmp (argv[0], "-p") == 0) || (stricmp (argv[0], "--prefix") == 0)) {
268 OutputDir = argv[1];
269 if (OutputDir == NULL) {
270 Warning (NULL, 0, 0, "NO output directory specified.", NULL);
271 return STATUS_ERROR;
272 }
273 argc -= 2;
274 argv += 2;
275 continue;
276 }
277
278 if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--filename") == 0)) {
279 InputFileName = argv[1];
280 if (InputFileName == NULL) {
281 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
282 return STATUS_ERROR;
283 }
284 argc -= 2;
285 argv += 2;
286 continue;
287 }
288
289 if ((stricmp (argv[0], "-s") == 0) || (stricmp (argv[0], "--split") == 0)) {
290 Status = GetSplitValue(argv[1], &SplitValue);
291 if (EFI_ERROR (Status)) {
292 Error (NULL, 0, 0x1003, "Input split value is not one valid integer.", NULL);
293 return STATUS_ERROR;
294 }
295 argc -= 2;
296 argv += 2;
297 continue;
298 }
299
300 if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--firstfile") == 0)) {
301 OutFileName1 = argv[1];
302 if (OutFileName1 == NULL) {
303 Warning (NULL, 0, 0, NULL, "No output file1 specified.");
304 }
305 argc -= 2;
306 argv += 2;
307 continue;
308 }
309
310 if ((stricmp (argv[0], "-t") == 0) || (stricmp (argv[0], "--secondfile") == 0)) {
311 OutFileName2 = argv[1];
312 if (OutFileName2 == NULL) {
313 Warning (NULL, 0, 0, NULL, "No output file2 specified.");
314 }
315 argc -= 2;
316 argv += 2;
317 continue;
318 }
319
320 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
321 argc --;
322 argv ++;
323 continue;
324 }
325
326 if ((strlen(argv[0]) >= 2 && argv[0][0] == '-' && (argv[0][1] == 'v' || argv[0][1] == 'V')) || (stricmp (argv[0], "--verbose") == 0)) {
327 VerboseLevel = 1;
328 if (strlen(argv[0]) > 2) {
329 Status = CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &VerboseLevel);
330 if (EFI_ERROR (Status)) {
331 Error (NULL, 0, 0x1003, NULL, "%s is invalid parameter!", argv[0]);
332 return STATUS_ERROR;
333 }
334 }
335
336 argc --;
337 argv ++;
338 continue;
339 }
340
341 if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) {
342 Status = AsciiStringToUint64 (argv[1], FALSE, &DebugLevel);
343 if (EFI_ERROR (Status)) {
344 Error (NULL, 0, 0x1003, "Input debug level is not one valid integrator.", NULL);
345 return STATUS_ERROR;
346 }
347 argc -= 2;
348 argv += 2;
349 continue;
350 }
351 //
352 // Don't recognize the parameter.
353 //
354 Error (NULL, 0, 0x1003, NULL, "%s is invalid parameter!", argv[0]);
355 return STATUS_ERROR;
356 }
357
358 if (InputFileName == NULL) {
359 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
360 return STATUS_ERROR;
361 }
362
363 In = fopen (LongFilePath (InputFileName), "rb");
364 if (In == NULL) {
365 // ("Unable to open file \"%s\"\n", InputFileName);
366 Error (InputFileName, 0, 1, "File open failure", NULL);
367 return STATUS_ERROR;
368 }
369
370 if (OutFileName1 == NULL) {
371 OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16);
372 if (OutName1 == NULL) {
373 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
374 ReturnStatus = STATUS_ERROR;
375 goto Finish;
376 }
377 strcpy (OutName1, InputFileName);
378 strcat (OutName1, "1");
379 OutFileName1 = OutName1;
380
381 }
382 if (OutFileName2 == NULL) {
383 OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16);
384 if (OutName2 == NULL) {
385 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
386 ReturnStatus = STATUS_ERROR;
387 goto Finish;
388 }
389 strcpy (OutName2, InputFileName);
390 strcat (OutName2, "2");
391 OutFileName2 = OutName2;
392
393 }
394
395 if (OutputDir != NULL) {
396 //OutputDirSpecified = TRUE;
397 if (chdir(OutputDir) != 0) {
398 Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");
399 ReturnStatus = STATUS_ERROR;
400 goto Finish;
401 }
402 }
403
404 CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0);
405 if (EFI_ERROR(CreateDir(&OutFileName1))) {
406 Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);
407 ReturnStatus = STATUS_ERROR;
408 goto Finish;
409 }
410 chdir(CurrentDir);
411
412 if (EFI_ERROR(CreateDir(&OutFileName2))) {
413 Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);
414 ReturnStatus = STATUS_ERROR;
415 goto Finish;
416 }
417 chdir(CurrentDir);
418 free(CurrentDir);
419
420 Out1 = fopen (LongFilePath (OutFileName1), "wb");
421 if (Out1 == NULL) {
422 // ("Unable to open file \"%s\"\n", OutFileName1);
423 Error (OutFileName1, 0, 1, "File open failure", NULL);
424 ReturnStatus = STATUS_ERROR;
425 goto Finish;
426 }
427
428 Out2 = fopen (LongFilePath (OutFileName2), "wb");
429 if (Out2 == NULL) {
430 // ("Unable to open file \"%s\"\n", OutFileName2);
431 Error (OutFileName2, 0, 1, "File open failure", NULL);
432 ReturnStatus = STATUS_ERROR;
433 goto Finish;
434 }
435
436 for (Index = 0; Index < SplitValue; Index++) {
437 CharC = (CHAR8) fgetc (In);
438 if (feof (In)) {
439 break;
440 }
441
442 fputc (CharC, Out1);
443 }
444
445 for (;;) {
446 CharC = (CHAR8) fgetc (In);
447 if (feof (In)) {
448 break;
449 }
450
451 fputc (CharC, Out2);
452 }
453
454 Finish:
455 if (OutName1 != NULL) {
456 free(OutName1);
457 }
458 if (OutName2 != NULL) {
459 free(OutName2);
460 }
461 if (In != NULL) {
462 fclose (In);
463 }
464 if (Out1 != NULL) {
465 fclose (Out1);
466 }
467 if (Out2 != NULL) {
468 fclose (Out2);
469 }
470
471 return ReturnStatus;
472 }