secureswitools/swisistools/source/makesis/parsecmd.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Note: This file may contain code to generate corrupt files for test purposes.
       
    16 * Such code is excluded from production builds by use of compiler defines;
       
    17 * it is recommended that such code should be removed if this code is ever published publicly.
       
    18 * handles parsing of makesis command line args
       
    19 * INCLUDES
       
    20 *
       
    21 */
       
    22 
       
    23 
       
    24 /**
       
    25  @file 
       
    26  @internalComponent
       
    27  @released
       
    28 */
       
    29 
       
    30 #include "utils.h"
       
    31 #include "utility_interface.h"
       
    32 #include "parsecmd.h"
       
    33 #include "utility.h"
       
    34 #include "siscontents.h"
       
    35 
       
    36 // ===========================================================================
       
    37 // CParseCmd
       
    38 // Responsable for processing and maintaining the command line options
       
    39 // ===========================================================================
       
    40 
       
    41 CParseCmd::CParseCmd ()
       
    42 :iOptions (0L),
       
    43 iShowSyntax (false),
       
    44 iShowSSL (false),
       
    45 iInterpretSisReport (false)
       
    46 	{
       
    47 	iDir   [0] = '\0';
       
    48 	iSource[0] = '\0';
       
    49 	iTarget[0] = '\0';
       
    50 	}
       
    51 
       
    52 BOOL CParseCmd::ParseCommandLine(int argc, _TCHAR *argv[])
       
    53 // Purpose  : Proceses the command line, and options
       
    54 // Inputs   : argc, argv - command line as passed to the process
       
    55 	{
       
    56 	// Test the number of arguments
       
    57 	if(argc == 1)
       
    58 		throw ErrInsufficientArgs;
       
    59 	
       
    60 	int wCount = 1;
       
    61 	// Parse the command line
       
    62 	// Check for options
       
    63 	while(wCount < argc)
       
    64 		{
       
    65 		if((argv[wCount][0] != '-') && ((argv[wCount][0] != '/') || (argv[wCount][0] != '\\')))
       
    66 			break;
       
    67 		else
       
    68 			{
       
    69 			if(argv[wCount][1] == '\0')
       
    70 				throw ErrBadCommandFlag;
       
    71 
       
    72 			// cope with multiple arguments following the '-' or '/'
       
    73 			int wCharacter = 1;
       
    74 			while (argv[wCount][wCharacter] !='\0')
       
    75 				{
       
    76 				switch(argv[wCount][wCharacter])
       
    77 					{
       
    78 					case 'a' : // Verbose
       
    79 					case 'A' : 
       
    80 #if defined (_DEBUG)
       
    81 						iOptions |= EOptDump;
       
    82 #endif // _DEBUG
       
    83 						break;
       
    84 #ifdef GENERATE_ERRORS
       
    85 					case 'b' :
       
    86 					case 'B' :
       
    87 						{
       
    88 						for (int index = wCharacter+1; argv [wCount] [index]; index++)
       
    89 							{
       
    90 							switch(argv [wCount] [index])
       
    91 								{
       
    92 								case 'a' :
       
    93 								case 'A' :
       
    94 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugArrayCount);
       
    95 									break;
       
    96 								case 'b' :
       
    97 								case 'B' :
       
    98 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugInsaneBlob);
       
    99 									break;
       
   100 								case 'c' :
       
   101 								case 'C' :
       
   102 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugCRCError);
       
   103 									break;
       
   104 								case 'e' :
       
   105 								case 'E' :
       
   106 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugBigEndian);
       
   107 									break;
       
   108 								case 'f' :
       
   109 								case 'F' :
       
   110 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugDuffFieldType);
       
   111 									break;
       
   112 								case 'h' :
       
   113 								case 'H' :
       
   114 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugHashError);
       
   115 									break;
       
   116 								case 'k' :
       
   117 								case 'K' :
       
   118 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugUnknownData);
       
   119 									break;
       
   120 								case 'l' :
       
   121 								case 'L' :
       
   122 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugInvalidLength);
       
   123 									break;
       
   124 								case 'm' :
       
   125 								case 'M' :
       
   126 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugMissingField);
       
   127 									break;
       
   128 								case 'n' :
       
   129 								case 'N' :
       
   130 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugNegativeLength);
       
   131 									break;
       
   132 								case 'p' :
       
   133 								case 'P' :
       
   134 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugEmptyCaps);
       
   135 									break;
       
   136 								case 's' :
       
   137 								case 'S' :
       
   138 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugInsaneString);
       
   139 									break;
       
   140 								case 't' :
       
   141 								case 'T' :
       
   142 									CSISFieldRoot::SetBug (CSISFieldRoot::EBug32As64);
       
   143 									break;
       
   144 								case 'u' :
       
   145 								case 'U' :
       
   146 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugUnexpectedField);
       
   147 									break;
       
   148 								case 'v' :
       
   149 								case 'V' :
       
   150 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugInvalidValues);
       
   151 									break;
       
   152 								case 'x' :
       
   153 								case 'X' :
       
   154 									CSISFieldRoot::SetBug (CSISFieldRoot::EBugUnknownField);
       
   155 									break;
       
   156 								}
       
   157 							wCharacter++;
       
   158 							}
       
   159 						}
       
   160 						break;
       
   161 #endif // GENERATE_ERRORS
       
   162 					case 'c' : // Report InterpretSis errors
       
   163 					case 'C' :
       
   164 						iInterpretSisReport = true;
       
   165 						break;
       
   166 					case 'd' : // Search directory
       
   167 					case 'D' : iOptions |= EOptDirectory;
       
   168 						SetDirectory(&argv[wCount][wCharacter+1]);
       
   169 						// Change next char to a NUL so we don't process the directory as if it were an option string.
       
   170 						argv[wCount][wCharacter+1] = '\0';
       
   171 						break;
       
   172 					case 'h' : // Help. i.e Show Syntax	
       
   173 					case 'H' : 
       
   174 						iShowSyntax = true;
       
   175 						return TRUE;
       
   176 					case 'i' :
       
   177 					case 'I' :
       
   178 						iShowSSL = true;
       
   179 						return TRUE;
       
   180 					case 'p' : // Use Password for Private Key Decryption	
       
   181 					case 'P' : iOptions |= EOptPassword;
       
   182 						break;
       
   183 #ifdef GENERATE_ERRORS
       
   184 					case 'q' :
       
   185 					case 'Q' :
       
   186 						CSISFieldRoot::SetBugStart (atoi (wstring2string (&argv [wCount] [wCharacter+1]).c_str ()));
       
   187 						wCharacter++;
       
   188 						break;
       
   189 					case 'r' :
       
   190 					case 'R' :
       
   191 						CSISFieldRoot::SetBugRepeat (atoi (wstring2string (&argv [wCount] [wCharacter+1]).c_str ()));
       
   192 						wCharacter++;
       
   193 						break;
       
   194 #endif // GENERATE_ERRORS
       
   195 					case 's' :
       
   196 					case 'S' :
       
   197 						iOptions |= EOptMakeStub;
       
   198 						CSISContents::SetStub (CSISContents::EStubROM);
       
   199 						break;
       
   200 					case 'v' : // Verbose
       
   201 					case 'V' : iOptions |= EOptVerbose;
       
   202 						break;
       
   203 					case 'x' :
       
   204 					case 'X' :
       
   205 						{
       
   206 						for (int index = wCharacter+1; argv [wCount][index]; index++)
       
   207 							{
       
   208 							switch(argv[wCount][index])
       
   209 								{
       
   210 								case 'c' :
       
   211 								case 'C' :
       
   212 									CSISFieldRoot::SetDebugOption (CSISFieldRoot::EDbgControllerChecksum);
       
   213 									break;
       
   214 								case 'd' :
       
   215 								case 'D' :
       
   216 									CSISFieldRoot::SetDebugOption (CSISFieldRoot::EDbgDataChecksum);
       
   217 									break;
       
   218 								case 'w' :
       
   219 								case 'W' :
       
   220 									CSISFieldRoot::SetDebugOption (CSISFieldRoot::EDbgCompress);
       
   221 									break;
       
   222 								case 'x' :
       
   223 								case 'X' :
       
   224 									CSISFieldRoot::SetDebugOption (CSISFieldRoot::EDbgNoCompress);
       
   225 									break;
       
   226 								}
       
   227 							wCharacter++;
       
   228 							}
       
   229 						}
       
   230 						break;
       
   231 					default  : // Uh-Oh...
       
   232 						throw ErrBadCommandFlag;
       
   233 					}
       
   234 				wCharacter++;
       
   235 				}
       
   236 			wCount++;
       
   237 			}
       
   238 		}
       
   239 	// Get the source and (optionally) target filenames
       
   240 	if(wCount < argc)
       
   241 		{
       
   242 		SetSource(argv[wCount++]);
       
   243 		}
       
   244 	else
       
   245 		throw ErrNoSourceFile;
       
   246 	
       
   247 	if(wCount < argc)
       
   248 		{
       
   249 		SetTarget(argv[wCount]);
       
   250 		}
       
   251 	
       
   252 	return TRUE;
       
   253 	}
       
   254 
       
   255 void CParseCmd::SetDirectory(const wchar_t* aPath)
       
   256 // Purpose  : Sets the search directory
       
   257 // Inputs   : pszCmdLine - the path
       
   258 	{
       
   259 	wcsncpy(iDir, aPath, PATHMAX - 1);
       
   260 	DWORD len=wcslen(iDir);
       
   261 	wchar_t *pBuffer = iDir;
       
   262 	wchar_t *pCurrent = pBuffer;
       
   263 
       
   264 	while (pBuffer && *pBuffer && (pCurrent = wcschr(pBuffer,L'\\')) != NULL)
       
   265 		{
       
   266 		*pCurrent = L'/';
       
   267 		pBuffer = pCurrent + 1;
       
   268 		} 
       
   269 	if(len>0 && iDir[len - 1] != '/')
       
   270 		{
       
   271 		iDir[len] = '/';
       
   272 		iDir[len+1] = '\0';
       
   273 		}
       
   274 	}
       
   275 
       
   276 void CParseCmd::SetTarget(const wchar_t* aTarget)
       
   277 // Purpose  : Sets the target filename
       
   278 // Inputs   : aTarget - The target filename
       
   279 	{
       
   280 	//Conversion of the filename consisting backward slashes with forward slashes
       
   281 	const wchar_t *pBuffer = aTarget;
       
   282 	wchar_t *pCurrent = const_cast<wchar_t*>(pBuffer);
       
   283 
       
   284 	while (pBuffer && *pBuffer && (pCurrent = wcschr(pBuffer,L'\\')) != NULL)
       
   285 		{
       
   286 		*pCurrent = L'/';
       
   287 		pBuffer = pCurrent + 1;
       
   288 		} 
       
   289 	// Check that the filename is valid
       
   290 	if (wcslen(aTarget) < wcslen(DESTFILE) + 1) // Check that we have at least one character for the filename, and 4 for the extension: ".sis"
       
   291 		throw ErrBadTargetFile;
       
   292 	else
       
   293 		{
       
   294 		if(CompareTwoString((wchar_t*)&aTarget[wcslen(aTarget) - (wcslen(DESTFILE))],DESTFILE) != 0 )
       
   295 			throw ErrBadTargetFile;
       
   296 		else
       
   297 			{
       
   298 			wcsncpy(iTarget, aTarget, PATHMAX-1);
       
   299 			iTarget [PATHMAX - 1] = 0;	
       
   300 			}
       
   301 		}
       
   302 	}
       
   303 
       
   304 void CParseCmd::SetSource(const wchar_t* aSource)
       
   305 // Purpose  : Sets the source filename
       
   306 // Inputs   : aSource - The source filename
       
   307 	{
       
   308 	// Check that the filename is valid
       
   309 	if (wcslen(aSource) <= wcslen(SOURCEFILE))
       
   310 		throw ErrBadSourceFile;
       
   311 	else
       
   312 		{
       
   313 		if(CompareTwoString((wchar_t*)&aSource[wcslen(aSource) - (wcslen(SOURCEFILE))],SOURCEFILE) != 0 )
       
   314 			throw ErrBadSourceFile;
       
   315 		else
       
   316 			{
       
   317 			wcsncpy(iSource, aSource, PATHMAX-1);
       
   318 			iTarget [PATHMAX - 1] = 0;
       
   319 			}
       
   320 		}
       
   321 	}
       
   322 
       
   323 const wchar_t* CParseCmd::TargetFile()
       
   324 // Returns the destination file. If none was set then it mugnes the source filename into
       
   325 // something acceptable
       
   326 	{
       
   327 	// Has a destination file been set ?
       
   328 	if(wcslen(iTarget) < 1)
       
   329 		{
       
   330 		wcscpy(iTarget, iSource);
       
   331 		iTarget[wcslen(iTarget) - (wcslen(SOURCEFILE))] = '\0';
       
   332 		wcscat(iTarget, DESTFILE);
       
   333 		}
       
   334 	return iTarget;
       
   335 	}