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