]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
Sync tool code to BuildTools project r1783.
[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-2008 Intel Corporation. All rights reserved
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
10
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.
13
14 **/
15
16 // GC_TODO: fix comment to start with /*++
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #ifdef __GNUC__
21 #include <unistd.h>
22 #else
23 #include <direct.h>
24 #endif
25 #include <ctype.h>
26 #include "ParseInf.h"
27 #include "CommonLib.h"
28 #include "EfiUtilityMsgs.h"
29 //
30 // Utility Name
31 //
32 #define UTILITY_NAME "Split"
33
34 //
35 // Utility version information
36 //
37 #define UTILITY_MAJOR_VERSION 0
38 #define UTILITY_MINOR_VERSION 1
39
40 void
41 Version (
42 void
43 )
44 /*++
45
46 Routine Description:
47
48 Displays the standard utility information to SDTOUT
49
50 Arguments:
51
52 None
53
54 Returns:
55
56 None
57
58 --*/
59 {
60 printf ("%s v%d.%d -Utility to break a file into two pieces at the request offset.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
61 printf ("Copyright (c) 1999-2007 Intel Corporation. All rights reserved.\n");
62 }
63
64 void
65 Usage (
66 void
67 )
68 /*++
69
70 Routine Description:
71
72 GC_TODO: Add function description
73
74 Arguments:
75
76
77 Returns:
78
79 GC_TODO: add return values
80
81 --*/
82 {
83 Version();
84 printf ("\nUsage: \n\
85 Split\n\
86 -f, --filename inputFile to split\n\
87 -s, --split VALUE the number of bytes in the first file\n\
88 [-p, --prefix OutputDir]\n\
89 [-o, --firstfile Filename1]\n\
90 [-t, --secondfile Filename2]\n\
91 [-v, --verbose]\n\
92 [--version]\n\
93 [-q, --quiet disable all messages except fatal errors]\n\
94 [-d, --debug[#]\n\
95 [-h, --help]\n");
96 }
97
98 EFI_STATUS
99 GetSplitValue (
100 IN CONST CHAR8* SplitValueString,
101 OUT UINT64 *ReturnValue
102 )
103 {
104 UINT64 len = strlen(SplitValueString);
105 UINT64 base = 1;
106 UINT64 index = 0;
107 UINT64 number = 0;
108 CHAR8 lastCHAR = 0;
109 EFI_STATUS Status = EFI_SUCCESS;
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 UINT64 index = 0;
178
179 for (;index < strlen(temp); ++index) {
180 if (temp[index] == '\\' || temp[index] == '/') {
181 temp[index] = 0;
182 if (chdir(start)) {
183 if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
184 return EFI_ABORTED;
185 }
186 chdir(start);
187 }
188 start = temp + index + 1;
189 temp[index] = '/';
190 }
191 }
192
193 return EFI_SUCCESS;
194 }
195
196 int
197 main (
198 int argc,
199 char*argv[]
200 )
201 /*++
202
203 Routine Description:
204
205 GC_TODO: Add function description
206
207 Arguments:
208
209 argc - GC_TODO: add argument description
210 ] - GC_TODO: add argument description
211
212 Returns:
213
214 GC_TODO: add return values
215
216 --*/
217 {
218 EFI_STATUS Status = EFI_SUCCESS;
219 FILE *In;
220 CHAR8 *InputFileName = NULL;
221 CHAR8 *OutputDir = NULL;
222 CHAR8 *OutFileName1 = NULL;
223 CHAR8 *OutFileName2 = NULL;
224 UINT64 SplitValue = (UINT64) -1;
225 FILE *Out1;
226 FILE *Out2;
227 CHAR8 *OutName1 = NULL;
228 CHAR8 *OutName2 = NULL;
229 CHAR8 *CurrentDir = NULL;
230 UINT64 Index;
231 CHAR8 CharC;
232 BOOLEAN QuietFlag = TRUE;
233 UINT64 DebugLevel = 0;
234 UINT64 VerboseLevel = 0;
235
236 SetUtilityName(UTILITY_NAME);
237 if (argc == 1) {
238 Usage();
239 return STATUS_ERROR;
240 }
241
242 argc --;
243 argv ++;
244
245 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
246 Usage();
247 return STATUS_SUCCESS;
248 }
249
250 if (stricmp (argv[0], "--version") == 0) {
251 Version();
252 return STATUS_SUCCESS;
253 }
254
255 while (argc > 0) {
256 if ((stricmp (argv[0], "-p") == 0) || (stricmp (argv[0], "--prefix") == 0)) {
257 OutputDir = argv[1];
258 if (OutputDir == NULL) {
259 Warning (NULL, 0, 0, "NO output directory specified.", NULL);
260 return STATUS_ERROR;
261 }
262 argc -= 2;
263 argv += 2;
264 continue;
265 }
266
267 if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--filename") == 0)) {
268 InputFileName = argv[1];
269 if (InputFileName == NULL) {
270 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
271 return STATUS_ERROR;
272 }
273 argc -= 2;
274 argv += 2;
275 continue;
276 }
277
278 if ((stricmp (argv[0], "-s") == 0) || (stricmp (argv[0], "--split") == 0)) {
279 Status = GetSplitValue(argv[1], &SplitValue);
280 if (EFI_ERROR (Status)) {
281 Error (NULL, 0, 0x1003, "Input split value is not one valid integer.", NULL);
282 return STATUS_ERROR;
283 }
284 argc -= 2;
285 argv += 2;
286 continue;
287 }
288
289 if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--firstfile") == 0)) {
290 OutFileName1 = argv[1];
291 if (OutFileName1 == NULL) {
292 Warning (NULL, 0, 0, NULL, "No output file1 specified.");
293 }
294 argc -= 2;
295 argv += 2;
296 continue;
297 }
298
299 if ((stricmp (argv[0], "-t") == 0) || (stricmp (argv[0], "--secondfile") == 0)) {
300 OutFileName2 = argv[1];
301 if (OutFileName2 == NULL) {
302 Warning (NULL, 0, 0, NULL, "No output file2 specified.");
303 }
304 argc -= 2;
305 argv += 2;
306 continue;
307 }
308
309 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
310 QuietFlag = TRUE;
311 argc --;
312 argv ++;
313 continue;
314 }
315
316 if ((strlen(argv[0]) >= 2 && argv[0][0] == '-' && (argv[0][1] == 'v' || argv[0][1] == 'V')) || (stricmp (argv[0], "--verbose") == 0)) {
317 VerboseLevel = 1;
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]);
322 return STATUS_ERROR;
323 }
324 }
325
326 argc --;
327 argv ++;
328 continue;
329 }
330
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);
335 return STATUS_ERROR;
336 }
337 argc -= 2;
338 argv += 2;
339 continue;
340 }
341 //
342 // Don't recognize the paramter.
343 //
344 Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);
345 return STATUS_ERROR;
346 }
347
348 if (InputFileName == NULL) {
349 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
350 return STATUS_ERROR;
351 }
352
353 In = fopen (InputFileName, "rb");
354 if (In == NULL) {
355 // ("Unable to open file \"%s\"\n", InputFileName);
356 Error (InputFileName, 0, 1, "File open failure", NULL);
357 return STATUS_ERROR;
358 }
359
360 if (OutFileName1 == NULL) {
361 OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16);
362 if (OutName1 == NULL) {
363 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
364 return STATUS_ERROR;
365 }
366 strcpy (OutName1, InputFileName);
367 strcat (OutName1, "1");
368 OutFileName1 = OutName1;
369
370 }
371 if (OutFileName2 == NULL) {
372 OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16);
373 if (OutName2 == NULL) {
374 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
375 return STATUS_ERROR;
376 }
377 strcpy (OutName2, InputFileName);
378 strcat (OutName2, "2");
379 OutFileName2 = OutName2;
380
381 }
382
383 if (OutputDir != NULL) {
384 //OutputDirSpecified = TRUE;
385 if (chdir(OutputDir) != 0) {
386 Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");
387 return STATUS_ERROR;
388 }
389 }
390
391 CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0);
392 if (EFI_ERROR(CreateDir(&OutFileName1))) {
393 Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);
394 return STATUS_ERROR;
395 }
396 chdir(CurrentDir);
397
398 if (EFI_ERROR(CreateDir(&OutFileName2))) {
399 Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);
400 return STATUS_ERROR;
401 }
402 chdir(CurrentDir);
403 free(CurrentDir);
404
405 Out1 = fopen (OutFileName1, "wb");
406 if (Out1 == NULL) {
407 // ("Unable to open file \"%s\"\n", OutFileName1);
408 Error (OutFileName1, 0, 1, "File open failure", NULL);
409 return STATUS_ERROR;
410 }
411
412 Out2 = fopen (OutFileName2, "wb");
413 if (Out2 == NULL) {
414 // ("Unable to open file \"%s\"\n", OutFileName2);
415 Error (OutFileName2, 0, 1, "File open failure", NULL);
416 return STATUS_ERROR;
417 }
418
419 for (Index = 0; Index < SplitValue; Index++) {
420 CharC = (CHAR8) fgetc (In);
421 if (feof (In)) {
422 break;
423 }
424
425 fputc (CharC, Out1);
426 }
427
428 for (;;) {
429 CharC = (CHAR8) fgetc (In);
430 if (feof (In)) {
431 break;
432 }
433
434 fputc (CharC, Out2);
435 }
436
437 if (OutName1 != NULL) {
438 free(OutName1);
439 }
440 if (OutName2 != NULL) {
441 free(OutName2);
442 }
443 fclose (In);
444 fclose (Out1);
445 fclose (Out2);
446
447 return STATUS_SUCCESS;
448 }