]>
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 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
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.
16 // GC_TODO: fix comment to start with /*++
27 #include "CommonLib.h"
28 #include "EfiUtilityMsgs.h"
32 #define UTILITY_NAME "Split"
35 // Utility version information
37 #define UTILITY_MAJOR_VERSION 1
38 #define UTILITY_MINOR_VERSION 0
48 Displays the standard utility information to SDTOUT
60 printf ("%s Version %d.%d Build %s\n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
71 GC_TODO: Add function description
78 GC_TODO: add return values
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");
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\
95 [-q, --quiet disable all messages except fatal errors]\n\
102 IN CONST CHAR8
* SplitValueString
,
103 OUT UINT64
*ReturnValue
111 EFI_STATUS Status
= EFI_SUCCESS
;
113 if (SplitValueString
!= NULL
){
114 len
= strlen(SplitValueString
);
121 Status
= AsciiStringToUint64 (SplitValueString
, FALSE
, ReturnValue
);
122 if (!EFI_ERROR (Status
)) {
126 if (SplitValueString
[0] == '0' && (SplitValueString
[1] == 'x' || SplitValueString
[1] == 'X')) {
127 Status
= AsciiStringToUint64 (SplitValueString
, TRUE
, ReturnValue
);
128 if (!EFI_ERROR (Status
)) {
133 lastCHAR
= (CHAR8
)toupper((int)SplitValueString
[len
- 1]);
135 if (lastCHAR
!= 'K' && lastCHAR
!= 'M' && lastCHAR
!= 'G') {
139 for (;index
< len
- 1; ++index
) {
140 if (!isdigit((int)SplitValueString
[index
])) {
145 number
= atol (SplitValueString
);
148 else if (lastCHAR
== 'M')
151 base
= 1024*1024*1024;
153 *ReturnValue
= number
*base
;
160 IN CONST CHAR8
* VerboseLevelString
,
161 IN CONST UINT64 Length
,
162 OUT UINT64
*ReturnValue
166 for (;i
< Length
; ++i
) {
167 if (VerboseLevelString
[i
] != 'v' && VerboseLevelString
[i
] != 'V') {
178 IN OUT CHAR8
** FullFileName
181 CHAR8
* temp
= *FullFileName
;
186 for (;index
< strlen(temp
); ++index
) {
187 if (temp
[index
] == '\\' || temp
[index
] == '/') {
188 if (temp
[index
+ 1] != '\0') {
189 tempchar
= temp
[index
+ 1];
192 if (mkdir(start
, S_IRWXU
| S_IRWXG
| S_IRWXO
) != 0) {
197 start
= temp
+ index
+ 1;
199 temp
[index
+ 1] = tempchar
;
216 GC_TODO: Add function description
220 argc - GC_TODO: add argument description
221 ] - GC_TODO: add argument description
225 GC_TODO: add return values
229 EFI_STATUS Status
= EFI_SUCCESS
;
230 INTN ReturnStatus
= STATUS_SUCCESS
;
232 CHAR8
*InputFileName
= NULL
;
233 CHAR8
*OutputDir
= NULL
;
234 CHAR8
*OutFileName1
= NULL
;
235 CHAR8
*OutFileName2
= NULL
;
236 UINT64 SplitValue
= (UINT64
) -1;
239 CHAR8
*OutName1
= NULL
;
240 CHAR8
*OutName2
= NULL
;
241 CHAR8
*CurrentDir
= NULL
;
244 UINT64 DebugLevel
= 0;
245 UINT64 VerboseLevel
= 0;
247 SetUtilityName(UTILITY_NAME
);
256 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
258 return STATUS_SUCCESS
;
261 if (stricmp (argv
[0], "--version") == 0) {
263 return STATUS_SUCCESS
;
267 if ((stricmp (argv
[0], "-p") == 0) || (stricmp (argv
[0], "--prefix") == 0)) {
269 if (OutputDir
== NULL
) {
270 Warning (NULL
, 0, 0, "NO output directory specified.", NULL
);
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
);
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
);
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.");
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.");
320 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
326 if ((strlen(argv
[0]) >= 2 && argv
[0][0] == '-' && (argv
[0][1] == 'v' || argv
[0][1] == 'V')) || (stricmp (argv
[0], "--verbose") == 0)) {
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 invaild parameter!", argv
[0]);
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
);
352 // Don't recognize the parameter.
354 Error (NULL
, 0, 0x1003, NULL
, "%s is invaild parameter!", argv
[0]);
358 if (InputFileName
== NULL
) {
359 Error (NULL
, 0, 0x1001, "NO Input file specified.", NULL
);
363 In
= fopen (LongFilePath (InputFileName
), "rb");
365 // ("Unable to open file \"%s\"\n", InputFileName);
366 Error (InputFileName
, 0, 1, "File open failure", NULL
);
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
;
377 strcpy (OutName1
, InputFileName
);
378 strcat (OutName1
, "1");
379 OutFileName1
= OutName1
;
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
;
389 strcpy (OutName2
, InputFileName
);
390 strcat (OutName2
, "2");
391 OutFileName2
= OutName2
;
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
;
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
;
412 if (EFI_ERROR(CreateDir(&OutFileName2
))) {
413 Error (OutFileName2
, 0, 5, "Create Dir for File2 Fail.", NULL
);
414 ReturnStatus
= STATUS_ERROR
;
420 Out1
= fopen (LongFilePath (OutFileName1
), "wb");
422 // ("Unable to open file \"%s\"\n", OutFileName1);
423 Error (OutFileName1
, 0, 1, "File open failure", NULL
);
424 ReturnStatus
= STATUS_ERROR
;
428 Out2
= fopen (LongFilePath (OutFileName2
), "wb");
430 // ("Unable to open file \"%s\"\n", OutFileName2);
431 Error (OutFileName2
, 0, 1, "File open failure", NULL
);
432 ReturnStatus
= STATUS_ERROR
;
436 for (Index
= 0; Index
< SplitValue
; Index
++) {
437 CharC
= (CHAR8
) fgetc (In
);
446 CharC
= (CHAR8
) fgetc (In
);
455 if (OutName1
!= NULL
) {
458 if (OutName2
!= NULL
) {