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