]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
b63aef78f9a2ef5c0855aa68535a7b668830b007
3 Split a file into two pieces at the request offset.
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
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 0
38 #define UTILITY_MINOR_VERSION 1
48 Displays the standard utility information to SDTOUT
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");
74 GC_TODO: Add function description
81 GC_TODO: add return values
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
106 UINT64 len
= strlen(SplitValueString
);
111 EFI_STATUS Status
= EFI_SUCCESS
;
117 Status
= AsciiStringToUint64 (SplitValueString
, FALSE
, ReturnValue
);
118 if (!EFI_ERROR (Status
)) {
122 if (SplitValueString
[0] == '0' && (SplitValueString
[1] == 'x' || SplitValueString
[1] == 'X')) {
123 Status
= AsciiStringToUint64 (SplitValueString
, TRUE
, ReturnValue
);
124 if (!EFI_ERROR (Status
)) {
129 lastCHAR
= (CHAR8
)toupper((int)SplitValueString
[len
- 1]);
131 if (lastCHAR
!= 'K' && lastCHAR
!= 'M' && lastCHAR
!= 'G') {
135 for (;index
< len
- 1; ++index
) {
136 if (!isdigit((int)SplitValueString
[index
])) {
141 number
= atol (SplitValueString
);
144 else if (lastCHAR
== 'M')
147 base
= 1024*1024*1024;
149 *ReturnValue
= number
*base
;
156 IN CONST CHAR8
* VerboseLevelString
,
157 IN CONST UINT64 Length
,
158 OUT UINT64
*ReturnValue
162 for (;i
< Length
; ++i
) {
163 if (VerboseLevelString
[i
] != 'v' && VerboseLevelString
[i
] != 'V') {
174 IN OUT CHAR8
** FullFileName
177 CHAR8
* temp
= *FullFileName
;
181 for (;index
< strlen(temp
); ++index
) {
182 if (temp
[index
] == '\\' || temp
[index
] == '/') {
185 if (mkdir(start
, S_IRWXU
| S_IRWXG
| S_IRWXO
) != 0) {
190 start
= temp
+ index
+ 1;
207 GC_TODO: Add function description
211 argc - GC_TODO: add argument description
212 ] - GC_TODO: add argument description
216 GC_TODO: add return values
220 EFI_STATUS Status
= EFI_SUCCESS
;
222 CHAR8
*InputFileName
= NULL
;
223 CHAR8
*OutputDir
= NULL
;
224 CHAR8
*OutFileName1
= NULL
;
225 CHAR8
*OutFileName2
= NULL
;
226 UINT64 SplitValue
= (UINT64
) -1;
229 CHAR8
*OutName1
= NULL
;
230 CHAR8
*OutName2
= NULL
;
231 CHAR8
*CurrentDir
= NULL
;
234 UINT64 DebugLevel
= 0;
235 UINT64 VerboseLevel
= 0;
237 SetUtilityName(UTILITY_NAME
);
246 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
248 return STATUS_SUCCESS
;
251 if (stricmp (argv
[0], "--version") == 0) {
253 return STATUS_SUCCESS
;
257 if ((stricmp (argv
[0], "-p") == 0) || (stricmp (argv
[0], "--prefix") == 0)) {
259 if (OutputDir
== NULL
) {
260 Warning (NULL
, 0, 0, "NO output directory specified.", NULL
);
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
);
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
);
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.");
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.");
310 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
316 if ((strlen(argv
[0]) >= 2 && argv
[0][0] == '-' && (argv
[0][1] == 'v' || argv
[0][1] == 'V')) || (stricmp (argv
[0], "--verbose") == 0)) {
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]);
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
);
342 // Don't recognize the paramter.
344 Error (NULL
, 0, 0x1003, NULL
, "%s is invaild paramter!", argv
[0]);
348 if (InputFileName
== NULL
) {
349 Error (NULL
, 0, 0x1001, "NO Input file specified.", NULL
);
353 In
= fopen (LongFilePath (InputFileName
), "rb");
355 // ("Unable to open file \"%s\"\n", InputFileName);
356 Error (InputFileName
, 0, 1, "File open failure", NULL
);
360 if (OutFileName1
== NULL
) {
361 OutName1
= (CHAR8
*)malloc(strlen(InputFileName
) + 16);
362 if (OutName1
== NULL
) {
363 Warning (NULL
, 0, 0, NULL
, "Memory Allocation Fail.");
366 strcpy (OutName1
, InputFileName
);
367 strcat (OutName1
, "1");
368 OutFileName1
= OutName1
;
371 if (OutFileName2
== NULL
) {
372 OutName2
= (CHAR8
*)malloc(strlen(InputFileName
) + 16);
373 if (OutName2
== NULL
) {
374 Warning (NULL
, 0, 0, NULL
, "Memory Allocation Fail.");
377 strcpy (OutName2
, InputFileName
);
378 strcat (OutName2
, "2");
379 OutFileName2
= OutName2
;
383 if (OutputDir
!= NULL
) {
384 //OutputDirSpecified = TRUE;
385 if (chdir(OutputDir
) != 0) {
386 Warning (NULL
, 0, 0, NULL
, "Change dir to OutputDir Fail.");
391 CurrentDir
= (CHAR8
*)getcwd((CHAR8
*)0, 0);
392 if (EFI_ERROR(CreateDir(&OutFileName1
))) {
393 Error (OutFileName1
, 0, 5, "Create Dir for File1 Fail.", NULL
);
398 if (EFI_ERROR(CreateDir(&OutFileName2
))) {
399 Error (OutFileName2
, 0, 5, "Create Dir for File2 Fail.", NULL
);
405 Out1
= fopen (LongFilePath (OutFileName1
), "wb");
407 // ("Unable to open file \"%s\"\n", OutFileName1);
408 Error (OutFileName1
, 0, 1, "File open failure", NULL
);
412 Out2
= fopen (LongFilePath (OutFileName2
), "wb");
414 // ("Unable to open file \"%s\"\n", OutFileName2);
415 Error (OutFileName2
, 0, 1, "File open failure", NULL
);
419 for (Index
= 0; Index
< SplitValue
; Index
++) {
420 CharC
= (CHAR8
) fgetc (In
);
429 CharC
= (CHAR8
) fgetc (In
);
437 if (OutName1
!= NULL
) {
440 if (OutName2
!= NULL
) {
447 return STATUS_SUCCESS
;