]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
Sync BaseTools Trunk (version r2387) to EDKII main trunk.
[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 - 2011, 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
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 %s -Utility to break a file into two pieces at the request offset.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);
61 printf ("Copyright (c) 1999-2010 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 UINT64 DebugLevel = 0;
233 UINT64 VerboseLevel = 0;
234
235 SetUtilityName(UTILITY_NAME);
236 if (argc == 1) {
237 Usage();
238 return STATUS_ERROR;
239 }
240
241 argc --;
242 argv ++;
243
244 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
245 Usage();
246 return STATUS_SUCCESS;
247 }
248
249 if (stricmp (argv[0], "--version") == 0) {
250 Version();
251 return STATUS_SUCCESS;
252 }
253
254 while (argc > 0) {
255 if ((stricmp (argv[0], "-p") == 0) || (stricmp (argv[0], "--prefix") == 0)) {
256 OutputDir = argv[1];
257 if (OutputDir == NULL) {
258 Warning (NULL, 0, 0, "NO output directory specified.", NULL);
259 return STATUS_ERROR;
260 }
261 argc -= 2;
262 argv += 2;
263 continue;
264 }
265
266 if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--filename") == 0)) {
267 InputFileName = argv[1];
268 if (InputFileName == NULL) {
269 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
270 return STATUS_ERROR;
271 }
272 argc -= 2;
273 argv += 2;
274 continue;
275 }
276
277 if ((stricmp (argv[0], "-s") == 0) || (stricmp (argv[0], "--split") == 0)) {
278 Status = GetSplitValue(argv[1], &SplitValue);
279 if (EFI_ERROR (Status)) {
280 Error (NULL, 0, 0x1003, "Input split value is not one valid integer.", NULL);
281 return STATUS_ERROR;
282 }
283 argc -= 2;
284 argv += 2;
285 continue;
286 }
287
288 if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--firstfile") == 0)) {
289 OutFileName1 = argv[1];
290 if (OutFileName1 == NULL) {
291 Warning (NULL, 0, 0, NULL, "No output file1 specified.");
292 }
293 argc -= 2;
294 argv += 2;
295 continue;
296 }
297
298 if ((stricmp (argv[0], "-t") == 0) || (stricmp (argv[0], "--secondfile") == 0)) {
299 OutFileName2 = argv[1];
300 if (OutFileName2 == NULL) {
301 Warning (NULL, 0, 0, NULL, "No output file2 specified.");
302 }
303 argc -= 2;
304 argv += 2;
305 continue;
306 }
307
308 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
309 argc --;
310 argv ++;
311 continue;
312 }
313
314 if ((strlen(argv[0]) >= 2 && argv[0][0] == '-' && (argv[0][1] == 'v' || argv[0][1] == 'V')) || (stricmp (argv[0], "--verbose") == 0)) {
315 VerboseLevel = 1;
316 if (strlen(argv[0]) > 2) {
317 Status = CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &VerboseLevel);
318 if (EFI_ERROR (Status)) {
319 Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);
320 return STATUS_ERROR;
321 }
322 }
323
324 argc --;
325 argv ++;
326 continue;
327 }
328
329 if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) {
330 Status = AsciiStringToUint64 (argv[1], FALSE, &DebugLevel);
331 if (EFI_ERROR (Status)) {
332 Error (NULL, 0, 0x1003, "Input debug level is not one valid integrator.", NULL);
333 return STATUS_ERROR;
334 }
335 argc -= 2;
336 argv += 2;
337 continue;
338 }
339 //
340 // Don't recognize the paramter.
341 //
342 Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);
343 return STATUS_ERROR;
344 }
345
346 if (InputFileName == NULL) {
347 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
348 return STATUS_ERROR;
349 }
350
351 In = fopen (InputFileName, "rb");
352 if (In == NULL) {
353 // ("Unable to open file \"%s\"\n", InputFileName);
354 Error (InputFileName, 0, 1, "File open failure", NULL);
355 return STATUS_ERROR;
356 }
357
358 if (OutFileName1 == NULL) {
359 OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16);
360 if (OutName1 == NULL) {
361 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
362 return STATUS_ERROR;
363 }
364 strcpy (OutName1, InputFileName);
365 strcat (OutName1, "1");
366 OutFileName1 = OutName1;
367
368 }
369 if (OutFileName2 == NULL) {
370 OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16);
371 if (OutName2 == NULL) {
372 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
373 return STATUS_ERROR;
374 }
375 strcpy (OutName2, InputFileName);
376 strcat (OutName2, "2");
377 OutFileName2 = OutName2;
378
379 }
380
381 if (OutputDir != NULL) {
382 //OutputDirSpecified = TRUE;
383 if (chdir(OutputDir) != 0) {
384 Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");
385 return STATUS_ERROR;
386 }
387 }
388
389 CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0);
390 if (EFI_ERROR(CreateDir(&OutFileName1))) {
391 Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);
392 return STATUS_ERROR;
393 }
394 chdir(CurrentDir);
395
396 if (EFI_ERROR(CreateDir(&OutFileName2))) {
397 Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);
398 return STATUS_ERROR;
399 }
400 chdir(CurrentDir);
401 free(CurrentDir);
402
403 Out1 = fopen (OutFileName1, "wb");
404 if (Out1 == NULL) {
405 // ("Unable to open file \"%s\"\n", OutFileName1);
406 Error (OutFileName1, 0, 1, "File open failure", NULL);
407 return STATUS_ERROR;
408 }
409
410 Out2 = fopen (OutFileName2, "wb");
411 if (Out2 == NULL) {
412 // ("Unable to open file \"%s\"\n", OutFileName2);
413 Error (OutFileName2, 0, 1, "File open failure", NULL);
414 return STATUS_ERROR;
415 }
416
417 for (Index = 0; Index < SplitValue; Index++) {
418 CharC = (CHAR8) fgetc (In);
419 if (feof (In)) {
420 break;
421 }
422
423 fputc (CharC, Out1);
424 }
425
426 for (;;) {
427 CharC = (CHAR8) fgetc (In);
428 if (feof (In)) {
429 break;
430 }
431
432 fputc (CharC, Out2);
433 }
434
435 if (OutName1 != NULL) {
436 free(OutName1);
437 }
438 if (OutName2 != NULL) {
439 free(OutName2);
440 }
441 fclose (In);
442 fclose (Out1);
443 fclose (Out2);
444
445 return STATUS_SUCCESS;
446 }