os/ossrv/ssl/tsrc/BC/libcrypto/topenssl/src/openssl.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* apps/openssl.c */
     2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
     3  * All rights reserved.
     4  *
     5  * This package is an SSL implementation written
     6  * by Eric Young (eay@cryptsoft.com).
     7  * The implementation was written so as to conform with Netscapes SSL.
     8  *
     9  * This library is free for commercial and non-commercial use as long as
    10  * the following conditions are aheared to.  The following conditions
    11  * apply to all code found in this distribution, be it the RC4, RSA,
    12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
    13  * included with this distribution is covered by the same copyright terms
    14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
    15  *
    16  * Copyright remains Eric Young's, and as such any Copyright notices in
    17  * the code are not to be removed.
    18  * If this package is used in a product, Eric Young should be given attribution
    19  * as the author of the parts of the library used.
    20  * This can be in the form of a textual message at program startup or
    21  * in documentation (online or textual) provided with the package.
    22  *
    23  * Redistribution and use in source and binary forms, with or without
    24  * modification, are permitted provided that the following conditions
    25  * are met:
    26  * 1. Redistributions of source code must retain the copyright
    27  *    notice, this list of conditions and the following disclaimer.
    28  * 2. Redistributions in binary form must reproduce the above copyright
    29  *    notice, this list of conditions and the following disclaimer in the
    30  *    documentation and/or other materials provided with the distribution.
    31  * 3. All advertising materials mentioning features or use of this software
    32  *    must display the following acknowledgement:
    33  *    "This product includes cryptographic software written by
    34  *     Eric Young (eay@cryptsoft.com)"
    35  *    The word 'cryptographic' can be left out if the rouines from the library
    36  *    being used are not cryptographic related :-).
    37  * 4. If you include any Windows specific code (or a derivative thereof) from
    38  *    the apps directory (application code) you must include an acknowledgement:
    39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
    40  *
    41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
    42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
    45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    51  * SUCH DAMAGE.
    52  *
    53  * The licence and distribution terms for any publically available version or
    54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
    55  * copied and put under another distribution licence
    56  * [including the GNU Public Licence.]
    57  */
    58 /* ====================================================================
    59  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
    60  *
    61  * Redistribution and use in source and binary forms, with or without
    62  * modification, are permitted provided that the following conditions
    63  * are met:
    64  *
    65  * 1. Redistributions of source code must retain the above copyright
    66  *    notice, this list of conditions and the following disclaimer.
    67  *
    68  * 2. Redistributions in binary form must reproduce the above copyright
    69  *    notice, this list of conditions and the following disclaimer in
    70  *    the documentation and/or other materials provided with the
    71  *    distribution.
    72  *
    73  * 3. All advertising materials mentioning features or use of this
    74  *    software must display the following acknowledgment:
    75  *    "This product includes software developed by the OpenSSL Project
    76  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    77  *
    78  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    79  *    endorse or promote products derived from this software without
    80  *    prior written permission. For written permission, please contact
    81  *    openssl-core@openssl.org.
    82  *
    83  * 5. Products derived from this software may not be called "OpenSSL"
    84  *    nor may "OpenSSL" appear in their names without prior written
    85  *    permission of the OpenSSL Project.
    86  *
    87  * 6. Redistributions of any form whatsoever must retain the following
    88  *    acknowledgment:
    89  *    "This product includes software developed by the OpenSSL Project
    90  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    91  *
    92  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    93  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    94  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    95  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    96  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    97  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    98  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    99  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   100  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   101  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   102  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   103  * OF THE POSSIBILITY OF SUCH DAMAGE.
   104  * ====================================================================
   105  *
   106  * This product includes cryptographic software written by Eric Young
   107  * (eay@cryptsoft.com).  This product includes software written by Tim
   108  * Hudson (tjh@cryptsoft.com).
   109  *
   110  */
   111 
   112 
   113 #include <stdio.h>
   114 #include <string.h>
   115 #include <stdlib.h>
   116 #include <dirent.h>
   117 #define OPENSSL_C /* tells apps.h to use complete apps_startup() */
   118 #include "apps.h"
   119 #include <openssl/bio.h>
   120 #include <openssl/crypto.h>
   121 #include <openssl/lhash.h>
   122 #include <openssl/conf.h>
   123 #include <openssl/x509.h>
   124 #include <openssl/pem.h>
   125 #include <openssl/ssl.h>
   126 #ifndef OPENSSL_NO_ENGINE
   127 #include <openssl/engine.h>
   128 #endif
   129 #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
   130 #include "progs.h"
   131 #include "s_apps.h"
   132 #include <openssl/err.h>
   133 #ifdef SYMBIAN
   134 #include "topenssl.h"
   135 #endif
   136 
   137 /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
   138  * base prototypes (we cast each variable inside the function to the required
   139  * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
   140  * functions. */
   141 
   142 /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
   143 static unsigned long MS_CALLBACK hash(const void *a_void);
   144 /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
   145 static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
   146 static LHASH *prog_init(void );
   147 static int do_cmd(LHASH *prog,int argc,char *argv[]);
   148 char *default_config_file=NULL;
   149 
   150 /* Make sure there is only one when MONOLITH is defined */
   151 #ifdef MONOLITH
   152 CONF *config=NULL;
   153 BIO *bio_err=NULL;
   154 #endif
   155 
   156 #ifdef SYMBIAN
   157 FILE *fp_stdout;
   158 FILE *fp_stderr;
   159 FILE *fp_stdin;
   160 #endif
   161 
   162 
   163 static void lock_dbg_cb(int mode, int type, const char *file, int line)
   164 	{
   165 	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
   166 	const char *errstr = NULL;
   167 	int rw;
   168 
   169 	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
   170 	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
   171 		{
   172 		errstr = "invalid mode";
   173 		goto err;
   174 		}
   175 
   176 	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
   177 		{
   178 		errstr = "type out of bounds";
   179 		goto err;
   180 		}
   181 
   182 	if (mode & CRYPTO_LOCK)
   183 		{
   184 		if (modes[type])
   185 			{
   186 			errstr = "already locked";
   187 			/* must not happen in a single-threaded program
   188 			 * (would deadlock) */
   189 			goto err;
   190 			}
   191 
   192 		modes[type] = rw;
   193 		}
   194 	else if (mode & CRYPTO_UNLOCK)
   195 		{
   196 		if (!modes[type])
   197 			{
   198 			errstr = "not locked";
   199 			goto err;
   200 			}
   201 
   202 		if (modes[type] != rw)
   203 			{
   204 			errstr = (rw == CRYPTO_READ) ?
   205 				"CRYPTO_r_unlock on write lock" :
   206 				"CRYPTO_w_unlock on read lock";
   207 			}
   208 
   209 		modes[type] = 0;
   210 		}
   211 	else
   212 		{
   213 		errstr = "invalid mode";
   214 		goto err;
   215 		}
   216 
   217  err:
   218 	if (errstr)
   219 		{
   220 		/* we cannot use bio_err here */
   221 #ifndef SYMBIAN
   222 		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
   223 			errstr, mode, type, file, line);
   224 #else
   225         fprintf(fp_stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
   226 			errstr, mode, type, file, line);
   227         fclose(fp_stderr)			;
   228 #endif
   229 		}
   230 	}
   231 
   232 
   233 
   234 #ifdef SYMBIAN
   235 void testResultXml(char *filename,int retval)
   236 {
   237 	char time_buf[50];
   238 
   239 	char result[10];
   240 
   241 	char xmlfilename[256];
   242 
   243 	time_t t = time(NULL);
   244 
   245 	struct tm *tm1 = localtime(&t);
   246 
   247 	char *atsinitmsg 	= 	"<test-report>\n\t<test-batch>";
   248 
   249 	char *atsbatchinit1	=   \
   250 							"\n\t\t<batch-init>\
   251 							\n\t\t\t<description></description>\
   252 							\n\t\t\t<date>";
   253 
   254 	char *atsbatchinit2 =	"</date>\
   255 							\n\t\t\t<factory>NA</factory>\
   256 							\n\t\t\t<component>\
   257 							\n\t\t\t\t<name>NA</name>\
   258 							\n\t\t\t\t<version>NA</version>\
   259 							\n\t\t\t</component>\
   260 							\n\t\t</batch-init>";
   261 
   262 	char *atsbatchresult=	\
   263 							"\n\t\t<batch-result>\
   264 							\n\t\t\t<run-time>00:00:00</run-time>\
   265 							\n\t\t</batch-result>";
   266 
   267 	char *atsclosemsg	=   \
   268 							"\n\t</test-batch>\
   269 							\n</test-report>\n ";
   270 
   271 	char *atstestinit	=	"\n\t\t<test-case time-stamp=\"00:00:00\">";
   272 
   273 
   274 	char *atscaseinit1	=	\
   275 							"\n\t\t\t<case-init>\
   276 							\n\t\t\t\t<version></version>\
   277 							\n\t\t\t\t<id>";
   278 
   279 	char *atscaseinit2 = 	"</id>\
   280 							\n\t\t\t\t<expected-result description=\"\">0</expected-result>\
   281 							\n\t\t\t</case-init>";
   282 
   283 	char *atscaseresult1=	\
   284 							"\n\t\t\t<case-result status=\"";
   285 
   286 	char *atscaseresult2=	"\">\
   287 							\n\t\t\t\t<actual-result>0</actual-result>\
   288 							\n\t\t\t\t<run-time>00:00:00</run-time>\
   289 							\n\t\t\t</case-result>";
   290 
   291 	char *atstestclose	=	"\n\t\t</test-case>";
   292 
   293 	DIR *dir;
   294 	FILE *fp;
   295 
   296 	dir = opendir("c:\\spd_logs");
   297 	if(!dir)
   298 		mkdir("c:\\spd_logs",0777);
   299 
   300 	dir = opendir("c:\\spd_logs\\xml");
   301 	if(!dir)
   302 		mkdir("c:\\spd_logs\\xml",0777);
   303 
   304 	// create the xml file name
   305 	strcpy(xmlfilename,"c:/spd_logs/xml/");
   306 	strcat(xmlfilename,filename);
   307 	strcat(xmlfilename,".xml");
   308 
   309 	strftime(time_buf,50,"%c",tm1);
   310 
   311 	if(retval)
   312 		strcpy(result,"FAILED");
   313 	else
   314 		strcpy(result,"PASSED");
   315 
   316 	fp = fopen(xmlfilename,"w");
   317 
   318 	if(fp)
   319 	{
   320 		fprintf(fp,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",atsinitmsg,atsbatchinit1,time_buf,atsbatchinit2,atstestinit,
   321 			atscaseinit1,filename,atscaseinit2,atscaseresult1,result,atscaseresult2,
   322 			atstestclose,atsbatchresult,atsclosemsg);
   323 
   324 		fclose(fp);
   325 	}
   326 }
   327 
   328 
   329 int openssl_init(void)
   330 {
   331 
   332 	fp_stdout = freopen(LOG_STDOUT,"w+",stdout);
   333 	if(!fp_stdout)
   334 	return 1;
   335 	fp_stderr = freopen(LOG_STDERR,"w+",stderr);
   336 	if(!fp_stderr)
   337 	return 1;
   338 	fp_stdin = freopen(LOG_STDIN,"w+",stdin);
   339 	if(!fp_stdin)
   340 	return 1;
   341 	return 0;
   342 }
   343 
   344 void openssl_deinit(void)
   345 {
   346     fclose(fp_stdout);
   347     fclose(fp_stderr);
   348     fclose(fp_stdin);
   349     fp_stdout=NULL;
   350     fp_stdin=NULL;
   351     fp_stderr=NULL;
   352 
   353 }
   354 
   355 int append(char *file1,char *file2, char *dst_file )
   356 {
   357    FILE *fp1 = NULL;
   358    FILE *fp2 = NULL;
   359    FILE *fp3 = NULL;
   360 
   361    long file1_size;
   362    long file2_size;
   363    char *file1_data = NULL;
   364    char *file2_data = NULL;
   365 
   366    int ret;
   367 
   368    if(!(fp1 = fopen(file1,"r+")))
   369    {
   370 	 fprintf(stdout,"Faliure opening file %s.",file1);
   371      ret = -1;
   372 	 goto err;
   373    }
   374 
   375    if(!(fp2 = fopen(file2,"r+")))
   376    {
   377 	 fprintf(stdout,"Faliure opening file %s.",file2);
   378 	 ret = -1;
   379 	 goto err;
   380    }
   381 
   382    if(!(fp3= fopen(dst_file,"w+")))
   383    {
   384 	 fprintf(stdout,"Faliure opening file %s.",dst_file);
   385 	 ret = -1;
   386 	 goto err;
   387    }
   388 
   389    if((ret = fseek(fp1, 0, SEEK_END)) < 0)
   390    {
   391    	  ret = -1;
   392 	  goto err;
   393    }
   394 
   395 
   396    if((ret = fseek(fp2, 0, SEEK_END)) < 0)
   397    {
   398    	 ret = -1;
   399 	 goto err;
   400    }
   401 
   402 
   403    if((file1_size = ftell(fp1)) < 0)
   404    {
   405    	 ret = -1;
   406 	 goto err;
   407    }
   408 
   409    if((file2_size = ftell(fp2)) < 0)
   410    {
   411      ret = -1;
   412 	 goto err;
   413    }
   414 
   415    if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
   416    {
   417    	 ret = -1;
   418 	 goto err;
   419    }
   420 
   421    if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
   422    {
   423    	 ret = -1;
   424 	 goto err;
   425    }
   426 
   427    file1_data = (char *)malloc(file1_size);
   428    file2_data = (char *)malloc(file2_size);
   429 
   430    if(!file1_data||!file2_data)
   431    {
   432    	 ret = -1;
   433 	 goto err;
   434    }
   435    memset(file1_data,0,file1_size);
   436 
   437    memset(file2_data,0,file2_size);
   438 
   439    ret = fread(file1_data,sizeof(char),file1_size,fp1);
   440    if(ret < file1_size)
   441    {
   442 	fprintf(stdout,"Failed reading file %s.", file1);
   443 	ret = -1;
   444     goto err;
   445    }
   446 
   447   ret = fread(file2_data,sizeof(char),file2_size,fp2);
   448   if(ret < file2_size)
   449   {
   450 	fprintf(stdout,"Failed reading file %s.",file2);
   451 	ret = -1;
   452 	goto err;
   453   }
   454 
   455   ret = fwrite(file1_data,sizeof(char),file1_size,fp3);
   456   if(ret < file1_size)
   457   {
   458 	fprintf(stdout,"Failed wrting from %s file to file %s.",file1,dst_file);
   459 	ret = -1;
   460 	goto err;
   461   }
   462   ret = fwrite(file2_data,sizeof(char),file2_size,fp3);
   463   if(ret < file2_size)
   464   {
   465 	fprintf(stdout,"Failed wrting from %s file to file %s.",file2,dst_file);
   466 	ret = -1;
   467 	goto err;
   468   }
   469   ret=0;
   470 
   471 err:  if(file1_data)
   472   free(file1_data);
   473   if(file2_data)
   474   free(file2_data);
   475   fclose(fp1);
   476   fclose(fp2);
   477   fclose(fp3);
   478   return ret;
   479 
   480 }
   481 void print_logs(int argc, char *argv[])
   482 {
   483 	int i=0;
   484 	for(i=0;i<argc-3;i++)
   485     {
   486     	fprintf(stdout,"%s ",argv[i+3]);
   487     }
   488    fprintf(stdout,"\n");
   489 
   490 }
   491 
   492 char** create_params(char *ip_str,int *argc)
   493 {
   494    char *paramets[50];
   495    char **cmd_line=NULL;
   496    char *str=NULL,*ptr=NULL;
   497    int len=0,cnt=0,i=0;
   498 
   499 
   500 
   501    str=ip_str;
   502 
   503    while(str)
   504    {
   505       ptr=strchr(str,' ');
   506       if(ptr)
   507       {
   508         len = ptr-str;
   509         paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
   510         if(paramets[cnt])
   511         {
   512           strncpy(paramets[cnt],str,len);
   513       	  paramets[cnt][len]='\0';
   514       	}
   515       	cnt++;
   516       	str+= len + 1;
   517       }
   518       else
   519       {
   520         len = strlen(str);
   521         paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
   522         if(paramets[cnt])
   523        	strcpy(paramets[cnt],str);
   524         cnt++;
   525       	break;
   526       }
   527 
   528    }
   529 
   530    if(cnt)
   531    cmd_line=(char **)malloc(cnt*sizeof(char *));
   532    for(i=0;i<cnt;i++)
   533    cmd_line[i]=paramets[i];
   534    *argc=cnt;
   535    return cmd_line;
   536 
   537 
   538 }
   539 
   540 
   541 
   542 int compare_files(const char *file1, const char *file2)
   543 {
   544 
   545    			FILE *fp1 = NULL;
   546 			FILE *fp2 = NULL;
   547 
   548 				long file1_size;
   549 				long file2_size;
   550 
   551 				char *file1_data = NULL;
   552 				char *file2_data = NULL;
   553 
   554 				int ret;
   555 				int argc=0;
   556    			char** argv = NULL;
   557 
   558 
   559 				if(!(fp1 = fopen(file1,"r+"))){
   560 					fprintf(stdout,"Faliure opening file %s.",file1);
   561 					ret = -1;
   562 					goto err;
   563 				}
   564 
   565 				if(!(fp2 = fopen(file2,"r+"))){
   566 					fprintf(stdout,"Faliure opening file %s.",file2);
   567 					ret = -1;
   568 					goto err;
   569 				}
   570 
   571 				if((ret = fseek(fp1, 0, SEEK_END)) < 0)
   572 					goto err;
   573 
   574 				if((ret = fseek(fp2, 0, SEEK_END)) < 0)
   575 					goto err;
   576 
   577  				if((file1_size = ftell(fp1)) < 0)
   578 				{
   579 					ret = file1_size;
   580 					goto err;
   581 				}
   582 				if((file2_size = ftell(fp1)) < 0)
   583 					{
   584 						ret = file2_size;
   585 						goto err;
   586 					}
   587 
   588 				if(file1_size != file2_size)
   589 				{
   590 					fprintf(stdout,"File size of  %s is not same as that of %s.",file1,file2);
   591 					ret = -1;
   592 					goto err;
   593 				}
   594 
   595                 if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
   596 					goto err;
   597 
   598 				if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
   599 					goto err;
   600 
   601 				file1_data = (char *)malloc(file1_size);
   602 				file2_data = (char *)malloc(file2_size);
   603 
   604 				memset(file1_data,0,file1_size);
   605 				memset(file2_data,0,file2_size);
   606 
   607 				ret = fread(file1_data,sizeof(char),file1_size,fp1);
   608 				if(ret < file1_size)
   609 				{
   610 				    ret = ferror(fp1);
   611 					fprintf(stdout,"Failed reading file %s.", file1);
   612 					ret = -1;
   613 					goto err1;
   614 				}
   615 
   616 				ret = fread(file2_data,sizeof(char),file2_size,fp2);
   617 				if(ret < file2_size)
   618 				{
   619 					fprintf(stdout,"Failed reading file %s.",file2);
   620 					ret = -1;
   621 					goto err2;
   622 				}
   623 
   624 				if(memcmp(file1_data,file2_data,file1_size))
   625 				{
   626 
   627 				   fprintf(stdout,"File file1 is not similar to file2.");
   628 				   ret = -1;
   629 				   goto err2;
   630 				}
   631 				else
   632 				ret=0; //file are smae
   633 
   634 err2:
   635 				fclose(fp2);
   636 err1:
   637 				fclose(fp1);
   638 err:
   639 
   640 				if(file1_data)
   641 					free(file1_data);
   642 
   643 				if(file2_data)
   644 					free(file2_data);
   645 
   646 				return ret;
   647 }
   648 
   649 void delete_file(char *file)
   650 {
   651 	unlink(file);
   652 }
   653 
   654 void delete_params(char ** cmd_line,int argc)
   655 {
   656 	int i;
   657 	for(i=0;i<argc;i++)
   658 	if(cmd_line[i])	free(cmd_line[i]);
   659 	if(cmd_line)
   660 	free(cmd_line);
   661 	cmd_line=NULL;
   662 }
   663 
   664 void strip(char *str)
   665 {
   666 	int len=0;
   667 	char *ptr=NULL;
   668 	char *ptr1=NULL;
   669 	len=strlen(str);
   670 	ptr=strchr(str,'\r');
   671 	ptr1=strchr(str,'\n');
   672 	if((ptr) || (ptr1))
   673 	{
   674 		if(ptr)
   675 		*ptr='\0'; //strip \r and \n
   676 		else
   677 		*ptr1='\0';
   678 	}
   679 	return;
   680 }
   681 
   682 
   683 int main(int argc_m, char *argv_m[])
   684 {
   685 
   686    int ret,argc=0;
   687    char argstr[200];
   688    int total_cases,cur_case=0,fail=0;
   689    char **argv=NULL;
   690    char openssltestnum[20];
   691    FILE *arg_fp;
   692 
   693 
   694   if(openssl_init())
   695   {
   696     return 1;
   697   }
   698 
   699 
   700 
   701     if(argc_m > 1)
   702     	arg_fp = fopen(argv_m[1],"r+");
   703     else
   704     	return 1; // filename needed as argument.
   705 
   706 
   707     if(!arg_fp)
   708     {
   709       return 1;
   710     }
   711 
   712     while(!feof(arg_fp)&&!fail)
   713     {
   714   	  memset(argstr,0,200);
   715       fgets(argstr, 200, arg_fp);
   716       strip(argstr);
   717       argv = create_params(argstr,&argc);
   718 	  strcpy(openssltestnum,argv[1]);
   719 
   720 	  if(!strcmp(argv[2],"test_log"))
   721       {
   722         print_logs(argc,argv);
   723       }
   724       else if(!strcmp(argv[2],"compare"))
   725       {
   726         ret = compare_files(argv[3],argv[4]);
   727         if(ret)
   728         {
   729         testResultXml(openssltestnum, ret);
   730         fail=1;
   731         }
   732 
   733       }
   734       else if(!strcmp(argv[2],"append"))
   735       {
   736         ret = append(argv[3],argv[4],argv[5]);
   737         if(ret)
   738         {
   739          testResultXml(openssltestnum, ret);
   740          fail=1;
   741         }
   742 
   743       }
   744        else if(!strcmp(argv[2],"delete"))
   745       {
   746         delete_file(argv[3]);
   747 
   748       }
   749       else
   750       {
   751         ret = openssl_main(argc-2,argv+2);
   752         if(ret)
   753         {
   754         testResultXml(openssltestnum, ret);
   755         fail=1;
   756         }
   757 
   758       }
   759 
   760      //delete_params(argv,argc);
   761    }//while
   762 
   763    if(!fail&&!ret)
   764    testResultXml(openssltestnum, fail);
   765    fclose(arg_fp);
   766 
   767 
   768  openssl_deinit();
   769  return ret;
   770 }
   771 
   772 #endif  /* IF_DEF SYMBIAN */
   773 #ifndef SYMBIAN
   774 int main(int Argc, char *Argv[])
   775 #else
   776 int openssl_main(int Argc, char *Argv[])
   777 #endif
   778 	{
   779 	ARGS arg;
   780 #define PROG_NAME_SIZE	39
   781 	char pname[PROG_NAME_SIZE+1];
   782 	FUNCTION f,*fp;
   783 	MS_STATIC const char *prompt;
   784 	MS_STATIC char buf[1024];
   785 	char *to_free=NULL;
   786 	int n,i,ret=0;
   787 	int argc;
   788 	char **argv,*p;
   789 	LHASH *prog=NULL;
   790 	long errline;
   791 
   792 	arg.data=NULL;
   793 	arg.count=0;
   794 
   795 	if (bio_err == NULL)
   796 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
   797 #ifdef SYMBIAN
   798         BIO_set_fp(bio_err,fp_stderr,BIO_NOCLOSE|BIO_FP_TEXT);
   799 #else
   800         BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
   801 #endif
   802 	if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
   803 		{
   804 		if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
   805 			{
   806 			CRYPTO_malloc_debug_init();
   807 			CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
   808 			}
   809 		else
   810 			{
   811 			/* OPENSSL_DEBUG_MEMORY=off */
   812 			CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
   813 			}
   814 		}
   815 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
   816 
   817 #if 0
   818 	if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
   819 #endif
   820 		{
   821 		CRYPTO_set_locking_callback(lock_dbg_cb);
   822 		}
   823 
   824 	apps_startup();
   825 
   826 	/* Lets load up our environment a little */
   827 	p=getenv("OPENSSL_CONF");
   828 	if (p == NULL)
   829 		p=getenv("SSLEAY_CONF");
   830 	if (p == NULL)
   831 		p=to_free=make_config_name();
   832 
   833 	default_config_file=p;
   834 
   835 	config=NCONF_new(NULL);
   836 	i=NCONF_load(config,p,&errline);
   837 	if (i == 0)
   838 		{
   839 		NCONF_free(config);
   840 		config = NULL;
   841 		ERR_clear_error();
   842 		}
   843 
   844 	prog=prog_init();
   845 
   846 	/* first check the program name */
   847 	program_name(Argv[0],pname,sizeof pname);
   848 
   849 	f.name=pname;
   850 	fp=(FUNCTION *)lh_retrieve(prog,&f);
   851 	if (fp != NULL)
   852 		{
   853 		Argv[0]=pname;
   854 		ret=fp->func(Argc,Argv);
   855 		goto end;
   856 		}
   857 
   858 	/* ok, now check that there are not arguments, if there are,
   859 	 * run with them, shifting the ssleay off the front */
   860 	if (Argc != 1)
   861 		{
   862 		Argc--;
   863 		Argv++;
   864 		ret=do_cmd(prog,Argc,Argv);
   865 		if (ret < 0) ret=0;
   866 		goto end;
   867 		}
   868 
   869 	/* ok, lets enter the old 'OpenSSL>' mode */
   870 
   871 	for (;;)
   872 		{
   873 		ret=0;
   874 		p=buf;
   875 		n=sizeof buf;
   876 		i=0;
   877 		for (;;)
   878 			{
   879 			p[0]='\0';
   880 			if (i++)
   881 				prompt=">";
   882 			else	prompt="OpenSSL> ";
   883 #ifndef SYMBIAN
   884 			fputs(prompt,stdout);
   885 			fflush(stdout);
   886 			fgets(p,n,stdin);
   887 #else
   888             fputs(prompt,stdout);
   889 			fflush(stdout);
   890 			fgets(p,n,stdin);
   891 
   892 #endif
   893 			if (p[0] == '\0') goto end;
   894 			i=strlen(p);
   895 			if (i <= 1) break;
   896 			if (p[i-2] != '\\') break;
   897 			i-=2;
   898 			p+=i;
   899 			n-=i;
   900 			}
   901 		if (!chopup_args(&arg,buf,&argc,&argv)) break;
   902 
   903 		ret=do_cmd(prog,argc,argv);
   904 		if (ret < 0)
   905 			{
   906 			ret=0;
   907 			goto end;
   908 			}
   909 		if (ret != 0)
   910 			BIO_printf(bio_err,"error in %s\n",argv[0]);
   911 		(void)BIO_flush(bio_err);
   912 		}
   913 	BIO_printf(bio_err,"bad exit\n");
   914 	ret=1;
   915 end:
   916 	if (to_free)
   917 		OPENSSL_free(to_free);
   918 	if (config != NULL)
   919 		{
   920 		NCONF_free(config);
   921 		config=NULL;
   922 		}
   923 	if (prog != NULL) lh_free(prog);
   924 	if (arg.data != NULL) OPENSSL_free(arg.data);
   925 
   926 	apps_shutdown();
   927 
   928 	CRYPTO_mem_leaks(bio_err);
   929 	if (bio_err != NULL)
   930 		{
   931 		BIO_free(bio_err);
   932 		bio_err=NULL;
   933 		}
   934 
   935 	return ret;
   936 //	OPENSSL_EXIT(ret);
   937 	}
   938 
   939 #define LIST_STANDARD_COMMANDS "list-standard-commands"
   940 #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
   941 #define LIST_CIPHER_COMMANDS "list-cipher-commands"
   942 
   943 static int do_cmd(LHASH *prog, int argc, char *argv[])
   944 	{
   945 	FUNCTION f,*fp;
   946 	int i,ret=1,tp,nl;
   947 
   948 	if ((argc <= 0) || (argv[0] == NULL))
   949 		{ ret=0; goto end; }
   950 	f.name=argv[0];
   951 	fp=(FUNCTION *)lh_retrieve(prog,&f);
   952 	if (fp != NULL)
   953 		{
   954 		ret=fp->func(argc,argv);
   955 		}
   956 	else if ((strncmp(argv[0],"no-",3)) == 0)
   957 		{
   958 
   959 		BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
   960 #ifdef OPENSSL_SYS_VMS
   961 		{
   962 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
   963 		bio_stdout = BIO_push(tmpbio, bio_stdout);
   964 		}
   965 #endif
   966 		f.name=argv[0]+3;
   967 		ret = (lh_retrieve(prog,&f) != NULL);
   968 		if (!ret)
   969 			BIO_printf(bio_stdout, "%s\n", argv[0]);
   970 		else
   971 			BIO_printf(bio_stdout, "%s\n", argv[0]+3);
   972 		BIO_free_all(bio_stdout);
   973 		goto end;
   974 		}
   975 	else if ((strcmp(argv[0],"quit") == 0) ||
   976 		(strcmp(argv[0],"q") == 0) ||
   977 		(strcmp(argv[0],"exit") == 0) ||
   978 		(strcmp(argv[0],"bye") == 0))
   979 		{
   980 		ret= -1;
   981 		goto end;
   982 		}
   983 	else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
   984 		(strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
   985 		(strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
   986 		{
   987 		int list_type;
   988 		BIO *bio_stdout;
   989 
   990 		if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
   991 			list_type = FUNC_TYPE_GENERAL;
   992 		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
   993 			list_type = FUNC_TYPE_MD;
   994 		else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
   995 			list_type = FUNC_TYPE_CIPHER;
   996 
   997 		bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
   998 #ifdef OPENSSL_SYS_VMS
   999 		{
  1000 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  1001 		bio_stdout = BIO_push(tmpbio, bio_stdout);
  1002 		}
  1003 #endif
  1004 
  1005 		for (fp=functions; fp->name != NULL; fp++)
  1006 			if (fp->type == list_type)
  1007 				BIO_printf(bio_stdout, "%s\n", fp->name);
  1008 		BIO_free_all(bio_stdout);
  1009 		ret=0;
  1010 		goto end;
  1011 		}
  1012 	else
  1013 		{
  1014 		BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
  1015 			argv[0]);
  1016 		BIO_printf(bio_err, "\nStandard commands");
  1017 		i=0;
  1018 		tp=0;
  1019 		for (fp=functions; fp->name != NULL; fp++)
  1020 			{
  1021 			nl=0;
  1022 			if (((i++) % 5) == 0)
  1023 				{
  1024 				BIO_printf(bio_err,"\n");
  1025 				nl=1;
  1026 				}
  1027 			if (fp->type != tp)
  1028 				{
  1029 				tp=fp->type;
  1030 				if (!nl) BIO_printf(bio_err,"\n");
  1031 				if (tp == FUNC_TYPE_MD)
  1032 					{
  1033 					i=1;
  1034 					BIO_printf(bio_err,
  1035 						"\nMessage Digest commands (see the `dgst' command for more details)\n");
  1036 					}
  1037 				else if (tp == FUNC_TYPE_CIPHER)
  1038 					{
  1039 					i=1;
  1040 					BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
  1041 					}
  1042 				}
  1043 			BIO_printf(bio_err,"%-15s",fp->name);
  1044 			}
  1045 		BIO_printf(bio_err,"\n\n");
  1046 		ret=0;
  1047 		}
  1048 end:
  1049 	return(ret);
  1050 	}
  1051 
  1052 static int SortFnByName(const void *_f1,const void *_f2)
  1053     {
  1054     const FUNCTION *f1=_f1;
  1055     const FUNCTION *f2=_f2;
  1056 
  1057     if(f1->type != f2->type)
  1058 	return f1->type-f2->type;
  1059     return strcmp(f1->name,f2->name);
  1060     }
  1061 
  1062 static LHASH *prog_init(void)
  1063 	{
  1064 	LHASH *ret;
  1065 	FUNCTION *f;
  1066 	size_t i;
  1067 
  1068 	/* Purely so it looks nice when the user hits ? */
  1069 	for(i=0,f=functions ; f->name != NULL ; ++f,++i)
  1070 	    ;
  1071 	qsort(functions,i,sizeof *functions,SortFnByName);
  1072 
  1073 	if ((ret=lh_new(hash, cmp)) == NULL)
  1074 		return(NULL);
  1075 
  1076 	for (f=functions; f->name != NULL; f++)
  1077 		lh_insert(ret,f);
  1078 	return(ret);
  1079 	}
  1080 
  1081 /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
  1082 static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
  1083 	{
  1084 	return(strncmp(((const FUNCTION *)a_void)->name,
  1085 			((const FUNCTION *)b_void)->name,8));
  1086 	}
  1087 
  1088 /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
  1089 static unsigned long MS_CALLBACK hash(const void *a_void)
  1090 	{
  1091 	return(lh_strhash(((const FUNCTION *)a_void)->name));
  1092 	}