]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
3 Split a file into two pieces at the request offset.
5 Copyright (c) 1999 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 // GC_TODO: fix comment to start with /*++
21 #include "CommonLib.h"
22 #include "EfiUtilityMsgs.h"
26 #define UTILITY_NAME "Split"
29 // Utility version information
31 #define UTILITY_MAJOR_VERSION 1
32 #define UTILITY_MINOR_VERSION 0
42 Displays the standard utility information to SDTOUT
54 printf ("%s Version %d.%d Build %s\n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
65 GC_TODO: Add function description
72 GC_TODO: add return values
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");
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\
89 [-q, --quiet disable all messages except fatal errors]\n\
96 IN CONST CHAR8
* SplitValueString
,
97 OUT UINT64
*ReturnValue
105 EFI_STATUS Status
= EFI_SUCCESS
;
107 if (SplitValueString
!= NULL
){
108 len
= strlen(SplitValueString
);
115 Status
= AsciiStringToUint64 (SplitValueString
, FALSE
, ReturnValue
);
116 if (!EFI_ERROR (Status
)) {
120 if (SplitValueString
[0] == '0' && (SplitValueString
[1] == 'x' || SplitValueString
[1] == 'X')) {
121 Status
= AsciiStringToUint64 (SplitValueString
, TRUE
, ReturnValue
);
122 if (!EFI_ERROR (Status
)) {
127 lastCHAR
= (CHAR8
)toupper((int)SplitValueString
[len
- 1]);
129 if (lastCHAR
!= 'K' && lastCHAR
!= 'M' && lastCHAR
!= 'G') {
133 for (;index
< len
- 1; ++index
) {
134 if (!isdigit((int)SplitValueString
[index
])) {
139 number
= atol (SplitValueString
);
142 else if (lastCHAR
== 'M')
145 base
= 1024*1024*1024;
147 *ReturnValue
= number
*base
;
154 IN CONST CHAR8
* VerboseLevelString
,
155 IN CONST UINT64 Length
,
156 OUT UINT64
*ReturnValue
160 for (;i
< Length
; ++i
) {
161 if (VerboseLevelString
[i
] != 'v' && VerboseLevelString
[i
] != 'V') {
172 IN OUT CHAR8
** FullFileName
175 CHAR8
* temp
= *FullFileName
;
180 for (;index
< strlen(temp
); ++index
) {
181 if (temp
[index
] == '\\' || temp
[index
] == '/') {
182 if (temp
[index
+ 1] != '\0') {
183 tempchar
= temp
[index
+ 1];
186 if (mkdir(start
, S_IRWXU
| S_IRWXG
| S_IRWXO
) != 0) {
191 start
= temp
+ index
+ 1;
193 temp
[index
+ 1] = tempchar
;
210 GC_TODO: Add function description
214 argc - GC_TODO: add argument description
215 ] - GC_TODO: add argument description
219 GC_TODO: add return values
223 EFI_STATUS Status
= EFI_SUCCESS
;
224 INTN ReturnStatus
= STATUS_SUCCESS
;
226 CHAR8
*InputFileName
= NULL
;
227 CHAR8
*OutputDir
= NULL
;
228 CHAR8
*OutFileName1
= NULL
;
229 CHAR8
*OutFileName2
= NULL
;
230 UINT64 SplitValue
= (UINT64
) -1;
233 CHAR8
*OutName1
= NULL
;
234 CHAR8
*OutName2
= NULL
;
235 CHAR8
*CurrentDir
= NULL
;
238 UINT64 DebugLevel
= 0;
239 UINT64 VerboseLevel
= 0;
241 SetUtilityName(UTILITY_NAME
);
250 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
252 return STATUS_SUCCESS
;
255 if (stricmp (argv
[0], "--version") == 0) {
257 return STATUS_SUCCESS
;
261 if ((stricmp (argv
[0], "-p") == 0) || (stricmp (argv
[0], "--prefix") == 0)) {
263 if (OutputDir
== NULL
) {
264 Warning (NULL
, 0, 0, "NO output directory specified.", NULL
);
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
);
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
);
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.");
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.");
314 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
320 if ((strlen(argv
[0]) >= 2 && argv
[0][0] == '-' && (argv
[0][1] == 'v' || argv
[0][1] == 'V')) || (stricmp (argv
[0], "--verbose") == 0)) {
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]);
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
);
346 // Don't recognize the parameter.
348 Error (NULL
, 0, 0x1003, NULL
, "%s is invalid parameter!", argv
[0]);
352 if (InputFileName
== NULL
) {
353 Error (NULL
, 0, 0x1001, "NO Input file specified.", NULL
);
357 In
= fopen (LongFilePath (InputFileName
), "rb");
359 // ("Unable to open file \"%s\"\n", InputFileName);
360 Error (InputFileName
, 0, 1, "File open failure", NULL
);
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
;
371 strcpy (OutName1
, InputFileName
);
372 strcat (OutName1
, "1");
373 OutFileName1
= OutName1
;
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
;
383 strcpy (OutName2
, InputFileName
);
384 strcat (OutName2
, "2");
385 OutFileName2
= OutName2
;
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
;
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
;
406 if (EFI_ERROR(CreateDir(&OutFileName2
))) {
407 Error (OutFileName2
, 0, 5, "Create Dir for File2 Fail.", NULL
);
408 ReturnStatus
= STATUS_ERROR
;
414 Out1
= fopen (LongFilePath (OutFileName1
), "wb");
416 // ("Unable to open file \"%s\"\n", OutFileName1);
417 Error (OutFileName1
, 0, 1, "File open failure", NULL
);
418 ReturnStatus
= STATUS_ERROR
;
422 Out2
= fopen (LongFilePath (OutFileName2
), "wb");
424 // ("Unable to open file \"%s\"\n", OutFileName2);
425 Error (OutFileName2
, 0, 1, "File open failure", NULL
);
426 ReturnStatus
= STATUS_ERROR
;
430 for (Index
= 0; Index
< SplitValue
; Index
++) {
431 CharC
= (CHAR8
) fgetc (In
);
440 CharC
= (CHAR8
) fgetc (In
);
449 if (OutName1
!= NULL
) {
452 if (OutName2
!= NULL
) {