]>
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 - 2016, 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-2016 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
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
;
182 for (;index
< strlen(temp
); ++index
) {
183 if (temp
[index
] == '\\' || temp
[index
] == '/') {
184 if (temp
[index
+ 1] != '\0') {
185 tempchar
= temp
[index
+ 1];
188 if (mkdir(start
, S_IRWXU
| S_IRWXG
| S_IRWXO
) != 0) {
193 start
= temp
+ index
+ 1;
195 temp
[index
+ 1] = tempchar
;
212 GC_TODO: Add function description
216 argc - GC_TODO: add argument description
217 ] - GC_TODO: add argument description
221 GC_TODO: add return values
225 EFI_STATUS Status
= EFI_SUCCESS
;
227 CHAR8
*InputFileName
= NULL
;
228 CHAR8
*OutputDir
= NULL
;
229 CHAR8
*OutFileName1
= NULL
;
230 CHAR8
*OutFileName2
= NULL
;
231 UINT64 SplitValue
= (UINT64
) -1;
234 CHAR8
*OutName1
= NULL
;
235 CHAR8
*OutName2
= NULL
;
236 CHAR8
*CurrentDir
= NULL
;
239 UINT64 DebugLevel
= 0;
240 UINT64 VerboseLevel
= 0;
242 SetUtilityName(UTILITY_NAME
);
251 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
253 return STATUS_SUCCESS
;
256 if (stricmp (argv
[0], "--version") == 0) {
258 return STATUS_SUCCESS
;
262 if ((stricmp (argv
[0], "-p") == 0) || (stricmp (argv
[0], "--prefix") == 0)) {
264 if (OutputDir
== NULL
) {
265 Warning (NULL
, 0, 0, "NO output directory specified.", NULL
);
273 if ((stricmp (argv
[0], "-f") == 0) || (stricmp (argv
[0], "--filename") == 0)) {
274 InputFileName
= argv
[1];
275 if (InputFileName
== NULL
) {
276 Error (NULL
, 0, 0x1001, "NO Input file specified.", NULL
);
284 if ((stricmp (argv
[0], "-s") == 0) || (stricmp (argv
[0], "--split") == 0)) {
285 Status
= GetSplitValue(argv
[1], &SplitValue
);
286 if (EFI_ERROR (Status
)) {
287 Error (NULL
, 0, 0x1003, "Input split value is not one valid integer.", NULL
);
295 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--firstfile") == 0)) {
296 OutFileName1
= argv
[1];
297 if (OutFileName1
== NULL
) {
298 Warning (NULL
, 0, 0, NULL
, "No output file1 specified.");
305 if ((stricmp (argv
[0], "-t") == 0) || (stricmp (argv
[0], "--secondfile") == 0)) {
306 OutFileName2
= argv
[1];
307 if (OutFileName2
== NULL
) {
308 Warning (NULL
, 0, 0, NULL
, "No output file2 specified.");
315 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
321 if ((strlen(argv
[0]) >= 2 && argv
[0][0] == '-' && (argv
[0][1] == 'v' || argv
[0][1] == 'V')) || (stricmp (argv
[0], "--verbose") == 0)) {
323 if (strlen(argv
[0]) > 2) {
324 Status
= CountVerboseLevel (&argv
[0][2], strlen(argv
[0]) - 2, &VerboseLevel
);
325 if (EFI_ERROR (Status
)) {
326 Error (NULL
, 0, 0x1003, NULL
, "%s is invaild paramter!", argv
[0]);
336 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--debug") == 0)) {
337 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &DebugLevel
);
338 if (EFI_ERROR (Status
)) {
339 Error (NULL
, 0, 0x1003, "Input debug level is not one valid integrator.", NULL
);
347 // Don't recognize the paramter.
349 Error (NULL
, 0, 0x1003, NULL
, "%s is invaild paramter!", argv
[0]);
353 if (InputFileName
== NULL
) {
354 Error (NULL
, 0, 0x1001, "NO Input file specified.", NULL
);
358 In
= fopen (LongFilePath (InputFileName
), "rb");
360 // ("Unable to open file \"%s\"\n", InputFileName);
361 Error (InputFileName
, 0, 1, "File open failure", NULL
);
365 if (OutFileName1
== NULL
) {
366 OutName1
= (CHAR8
*)malloc(strlen(InputFileName
) + 16);
367 if (OutName1
== NULL
) {
368 Warning (NULL
, 0, 0, NULL
, "Memory Allocation Fail.");
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.");
382 strcpy (OutName2
, InputFileName
);
383 strcat (OutName2
, "2");
384 OutFileName2
= OutName2
;
388 if (OutputDir
!= NULL
) {
389 //OutputDirSpecified = TRUE;
390 if (chdir(OutputDir
) != 0) {
391 Warning (NULL
, 0, 0, NULL
, "Change dir to OutputDir Fail.");
396 CurrentDir
= (CHAR8
*)getcwd((CHAR8
*)0, 0);
397 if (EFI_ERROR(CreateDir(&OutFileName1
))) {
398 Error (OutFileName1
, 0, 5, "Create Dir for File1 Fail.", NULL
);
403 if (EFI_ERROR(CreateDir(&OutFileName2
))) {
404 Error (OutFileName2
, 0, 5, "Create Dir for File2 Fail.", NULL
);
410 Out1
= fopen (LongFilePath (OutFileName1
), "wb");
412 // ("Unable to open file \"%s\"\n", OutFileName1);
413 Error (OutFileName1
, 0, 1, "File open failure", NULL
);
417 Out2
= fopen (LongFilePath (OutFileName2
), "wb");
419 // ("Unable to open file \"%s\"\n", OutFileName2);
420 Error (OutFileName2
, 0, 1, "File open failure", NULL
);
424 for (Index
= 0; Index
< SplitValue
; Index
++) {
425 CharC
= (CHAR8
) fgetc (In
);
434 CharC
= (CHAR8
) fgetc (In
);
442 if (OutName1
!= NULL
) {
445 if (OutName2
!= NULL
) {
452 return STATUS_SUCCESS
;