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.
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
#ifndef OPENSSL_NO_ENGINE
sl@0
   127
#include <openssl/engine.h>
sl@0
   128
#endif
sl@0
   129
#define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
sl@0
   130
#include "progs.h"
sl@0
   131
#include "s_apps.h"
sl@0
   132
#include <openssl/err.h>
sl@0
   133
#ifdef SYMBIAN
sl@0
   134
#include "topenssl.h"
sl@0
   135
#endif
sl@0
   136
sl@0
   137
/* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
sl@0
   138
 * base prototypes (we cast each variable inside the function to the required
sl@0
   139
 * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
sl@0
   140
 * functions. */
sl@0
   141
sl@0
   142
/* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
sl@0
   143
static unsigned long MS_CALLBACK hash(const void *a_void);
sl@0
   144
/* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
sl@0
   145
static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
sl@0
   146
static LHASH *prog_init(void );
sl@0
   147
static int do_cmd(LHASH *prog,int argc,char *argv[]);
sl@0
   148
char *default_config_file=NULL;
sl@0
   149
sl@0
   150
/* Make sure there is only one when MONOLITH is defined */
sl@0
   151
#ifdef MONOLITH
sl@0
   152
CONF *config=NULL;
sl@0
   153
BIO *bio_err=NULL;
sl@0
   154
#endif
sl@0
   155
sl@0
   156
#ifdef SYMBIAN
sl@0
   157
FILE *fp_stdout;
sl@0
   158
FILE *fp_stderr;
sl@0
   159
FILE *fp_stdin;
sl@0
   160
#endif
sl@0
   161
sl@0
   162
sl@0
   163
static void lock_dbg_cb(int mode, int type, const char *file, int line)
sl@0
   164
	{
sl@0
   165
	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
sl@0
   166
	const char *errstr = NULL;
sl@0
   167
	int rw;
sl@0
   168
sl@0
   169
	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
sl@0
   170
	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
sl@0
   171
		{
sl@0
   172
		errstr = "invalid mode";
sl@0
   173
		goto err;
sl@0
   174
		}
sl@0
   175
sl@0
   176
	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
sl@0
   177
		{
sl@0
   178
		errstr = "type out of bounds";
sl@0
   179
		goto err;
sl@0
   180
		}
sl@0
   181
sl@0
   182
	if (mode & CRYPTO_LOCK)
sl@0
   183
		{
sl@0
   184
		if (modes[type])
sl@0
   185
			{
sl@0
   186
			errstr = "already locked";
sl@0
   187
			/* must not happen in a single-threaded program
sl@0
   188
			 * (would deadlock) */
sl@0
   189
			goto err;
sl@0
   190
			}
sl@0
   191
sl@0
   192
		modes[type] = rw;
sl@0
   193
		}
sl@0
   194
	else if (mode & CRYPTO_UNLOCK)
sl@0
   195
		{
sl@0
   196
		if (!modes[type])
sl@0
   197
			{
sl@0
   198
			errstr = "not locked";
sl@0
   199
			goto err;
sl@0
   200
			}
sl@0
   201
sl@0
   202
		if (modes[type] != rw)
sl@0
   203
			{
sl@0
   204
			errstr = (rw == CRYPTO_READ) ?
sl@0
   205
				"CRYPTO_r_unlock on write lock" :
sl@0
   206
				"CRYPTO_w_unlock on read lock";
sl@0
   207
			}
sl@0
   208
sl@0
   209
		modes[type] = 0;
sl@0
   210
		}
sl@0
   211
	else
sl@0
   212
		{
sl@0
   213
		errstr = "invalid mode";
sl@0
   214
		goto err;
sl@0
   215
		}
sl@0
   216
sl@0
   217
 err:
sl@0
   218
	if (errstr)
sl@0
   219
		{
sl@0
   220
		/* we cannot use bio_err here */
sl@0
   221
#ifndef SYMBIAN
sl@0
   222
		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
sl@0
   223
			errstr, mode, type, file, line);
sl@0
   224
#else
sl@0
   225
        fprintf(fp_stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
sl@0
   226
			errstr, mode, type, file, line);
sl@0
   227
        fclose(fp_stderr)			;
sl@0
   228
#endif
sl@0
   229
		}
sl@0
   230
	}
sl@0
   231
sl@0
   232
sl@0
   233
sl@0
   234
#ifdef SYMBIAN
sl@0
   235
void testResultXml(char *filename,int retval)
sl@0
   236
{
sl@0
   237
	char time_buf[50];
sl@0
   238
sl@0
   239
	char result[10];
sl@0
   240
sl@0
   241
	char xmlfilename[256];
sl@0
   242
sl@0
   243
	time_t t = time(NULL);
sl@0
   244
sl@0
   245
	struct tm *tm1 = localtime(&t);
sl@0
   246
sl@0
   247
	char *atsinitmsg 	= 	"<test-report>\n\t<test-batch>";
sl@0
   248
sl@0
   249
	char *atsbatchinit1	=   \
sl@0
   250
							"\n\t\t<batch-init>\
sl@0
   251
							\n\t\t\t<description></description>\
sl@0
   252
							\n\t\t\t<date>";
sl@0
   253
sl@0
   254
	char *atsbatchinit2 =	"</date>\
sl@0
   255
							\n\t\t\t<factory>NA</factory>\
sl@0
   256
							\n\t\t\t<component>\
sl@0
   257
							\n\t\t\t\t<name>NA</name>\
sl@0
   258
							\n\t\t\t\t<version>NA</version>\
sl@0
   259
							\n\t\t\t</component>\
sl@0
   260
							\n\t\t</batch-init>";
sl@0
   261
sl@0
   262
	char *atsbatchresult=	\
sl@0
   263
							"\n\t\t<batch-result>\
sl@0
   264
							\n\t\t\t<run-time>00:00:00</run-time>\
sl@0
   265
							\n\t\t</batch-result>";
sl@0
   266
sl@0
   267
	char *atsclosemsg	=   \
sl@0
   268
							"\n\t</test-batch>\
sl@0
   269
							\n</test-report>\n ";
sl@0
   270
sl@0
   271
	char *atstestinit	=	"\n\t\t<test-case time-stamp=\"00:00:00\">";
sl@0
   272
sl@0
   273
sl@0
   274
	char *atscaseinit1	=	\
sl@0
   275
							"\n\t\t\t<case-init>\
sl@0
   276
							\n\t\t\t\t<version></version>\
sl@0
   277
							\n\t\t\t\t<id>";
sl@0
   278
sl@0
   279
	char *atscaseinit2 = 	"</id>\
sl@0
   280
							\n\t\t\t\t<expected-result description=\"\">0</expected-result>\
sl@0
   281
							\n\t\t\t</case-init>";
sl@0
   282
sl@0
   283
	char *atscaseresult1=	\
sl@0
   284
							"\n\t\t\t<case-result status=\"";
sl@0
   285
sl@0
   286
	char *atscaseresult2=	"\">\
sl@0
   287
							\n\t\t\t\t<actual-result>0</actual-result>\
sl@0
   288
							\n\t\t\t\t<run-time>00:00:00</run-time>\
sl@0
   289
							\n\t\t\t</case-result>";
sl@0
   290
sl@0
   291
	char *atstestclose	=	"\n\t\t</test-case>";
sl@0
   292
sl@0
   293
	DIR *dir;
sl@0
   294
	FILE *fp;
sl@0
   295
sl@0
   296
	dir = opendir("c:\\spd_logs");
sl@0
   297
	if(!dir)
sl@0
   298
		mkdir("c:\\spd_logs",0777);
sl@0
   299
sl@0
   300
	dir = opendir("c:\\spd_logs\\xml");
sl@0
   301
	if(!dir)
sl@0
   302
		mkdir("c:\\spd_logs\\xml",0777);
sl@0
   303
sl@0
   304
	// create the xml file name
sl@0
   305
	strcpy(xmlfilename,"c:/spd_logs/xml/");
sl@0
   306
	strcat(xmlfilename,filename);
sl@0
   307
	strcat(xmlfilename,".xml");
sl@0
   308
sl@0
   309
	strftime(time_buf,50,"%c",tm1);
sl@0
   310
sl@0
   311
	if(retval)
sl@0
   312
		strcpy(result,"FAILED");
sl@0
   313
	else
sl@0
   314
		strcpy(result,"PASSED");
sl@0
   315
sl@0
   316
	fp = fopen(xmlfilename,"w");
sl@0
   317
sl@0
   318
	if(fp)
sl@0
   319
	{
sl@0
   320
		fprintf(fp,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",atsinitmsg,atsbatchinit1,time_buf,atsbatchinit2,atstestinit,
sl@0
   321
			atscaseinit1,filename,atscaseinit2,atscaseresult1,result,atscaseresult2,
sl@0
   322
			atstestclose,atsbatchresult,atsclosemsg);
sl@0
   323
sl@0
   324
		fclose(fp);
sl@0
   325
	}
sl@0
   326
}
sl@0
   327
sl@0
   328
sl@0
   329
int openssl_init(void)
sl@0
   330
{
sl@0
   331
sl@0
   332
	fp_stdout = freopen(LOG_STDOUT,"w+",stdout);
sl@0
   333
	if(!fp_stdout)
sl@0
   334
	return 1;
sl@0
   335
	fp_stderr = freopen(LOG_STDERR,"w+",stderr);
sl@0
   336
	if(!fp_stderr)
sl@0
   337
	return 1;
sl@0
   338
	fp_stdin = freopen(LOG_STDIN,"w+",stdin);
sl@0
   339
	if(!fp_stdin)
sl@0
   340
	return 1;
sl@0
   341
	return 0;
sl@0
   342
}
sl@0
   343
sl@0
   344
void openssl_deinit(void)
sl@0
   345
{
sl@0
   346
    fclose(fp_stdout);
sl@0
   347
    fclose(fp_stderr);
sl@0
   348
    fclose(fp_stdin);
sl@0
   349
    fp_stdout=NULL;
sl@0
   350
    fp_stdin=NULL;
sl@0
   351
    fp_stderr=NULL;
sl@0
   352
sl@0
   353
}
sl@0
   354
sl@0
   355
int append(char *file1,char *file2, char *dst_file )
sl@0
   356
{
sl@0
   357
   FILE *fp1 = NULL;
sl@0
   358
   FILE *fp2 = NULL;
sl@0
   359
   FILE *fp3 = NULL;
sl@0
   360
sl@0
   361
   long file1_size;
sl@0
   362
   long file2_size;
sl@0
   363
   char *file1_data = NULL;
sl@0
   364
   char *file2_data = NULL;
sl@0
   365
sl@0
   366
   int ret;
sl@0
   367
sl@0
   368
   if(!(fp1 = fopen(file1,"r+")))
sl@0
   369
   {
sl@0
   370
	 fprintf(stdout,"Faliure opening file %s.",file1);
sl@0
   371
     ret = -1;
sl@0
   372
	 goto err;
sl@0
   373
   }
sl@0
   374
sl@0
   375
   if(!(fp2 = fopen(file2,"r+")))
sl@0
   376
   {
sl@0
   377
	 fprintf(stdout,"Faliure opening file %s.",file2);
sl@0
   378
	 ret = -1;
sl@0
   379
	 goto err;
sl@0
   380
   }
sl@0
   381
sl@0
   382
   if(!(fp3= fopen(dst_file,"w+")))
sl@0
   383
   {
sl@0
   384
	 fprintf(stdout,"Faliure opening file %s.",dst_file);
sl@0
   385
	 ret = -1;
sl@0
   386
	 goto err;
sl@0
   387
   }
sl@0
   388
sl@0
   389
   if((ret = fseek(fp1, 0, SEEK_END)) < 0)
sl@0
   390
   {
sl@0
   391
   	  ret = -1;
sl@0
   392
	  goto err;
sl@0
   393
   }
sl@0
   394
sl@0
   395
sl@0
   396
   if((ret = fseek(fp2, 0, SEEK_END)) < 0)
sl@0
   397
   {
sl@0
   398
   	 ret = -1;
sl@0
   399
	 goto err;
sl@0
   400
   }
sl@0
   401
sl@0
   402
sl@0
   403
   if((file1_size = ftell(fp1)) < 0)
sl@0
   404
   {
sl@0
   405
   	 ret = -1;
sl@0
   406
	 goto err;
sl@0
   407
   }
sl@0
   408
sl@0
   409
   if((file2_size = ftell(fp2)) < 0)
sl@0
   410
   {
sl@0
   411
     ret = -1;
sl@0
   412
	 goto err;
sl@0
   413
   }
sl@0
   414
sl@0
   415
   if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
sl@0
   416
   {
sl@0
   417
   	 ret = -1;
sl@0
   418
	 goto err;
sl@0
   419
   }
sl@0
   420
sl@0
   421
   if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
sl@0
   422
   {
sl@0
   423
   	 ret = -1;
sl@0
   424
	 goto err;
sl@0
   425
   }
sl@0
   426
sl@0
   427
   file1_data = (char *)malloc(file1_size);
sl@0
   428
   file2_data = (char *)malloc(file2_size);
sl@0
   429
sl@0
   430
   if(!file1_data||!file2_data)
sl@0
   431
   {
sl@0
   432
   	 ret = -1;
sl@0
   433
	 goto err;
sl@0
   434
   }
sl@0
   435
   memset(file1_data,0,file1_size);
sl@0
   436
sl@0
   437
   memset(file2_data,0,file2_size);
sl@0
   438
sl@0
   439
   ret = fread(file1_data,sizeof(char),file1_size,fp1);
sl@0
   440
   if(ret < file1_size)
sl@0
   441
   {
sl@0
   442
	fprintf(stdout,"Failed reading file %s.", file1);
sl@0
   443
	ret = -1;
sl@0
   444
    goto err;
sl@0
   445
   }
sl@0
   446
sl@0
   447
  ret = fread(file2_data,sizeof(char),file2_size,fp2);
sl@0
   448
  if(ret < file2_size)
sl@0
   449
  {
sl@0
   450
	fprintf(stdout,"Failed reading file %s.",file2);
sl@0
   451
	ret = -1;
sl@0
   452
	goto err;
sl@0
   453
  }
sl@0
   454
sl@0
   455
  ret = fwrite(file1_data,sizeof(char),file1_size,fp3);
sl@0
   456
  if(ret < file1_size)
sl@0
   457
  {
sl@0
   458
	fprintf(stdout,"Failed wrting from %s file to file %s.",file1,dst_file);
sl@0
   459
	ret = -1;
sl@0
   460
	goto err;
sl@0
   461
  }
sl@0
   462
  ret = fwrite(file2_data,sizeof(char),file2_size,fp3);
sl@0
   463
  if(ret < file2_size)
sl@0
   464
  {
sl@0
   465
	fprintf(stdout,"Failed wrting from %s file to file %s.",file2,dst_file);
sl@0
   466
	ret = -1;
sl@0
   467
	goto err;
sl@0
   468
  }
sl@0
   469
  ret=0;
sl@0
   470
sl@0
   471
err:  if(file1_data)
sl@0
   472
  free(file1_data);
sl@0
   473
  if(file2_data)
sl@0
   474
  free(file2_data);
sl@0
   475
  fclose(fp1);
sl@0
   476
  fclose(fp2);
sl@0
   477
  fclose(fp3);
sl@0
   478
  return ret;
sl@0
   479
sl@0
   480
}
sl@0
   481
void print_logs(int argc, char *argv[])
sl@0
   482
{
sl@0
   483
	int i=0;
sl@0
   484
	for(i=0;i<argc-3;i++)
sl@0
   485
    {
sl@0
   486
    	fprintf(stdout,"%s ",argv[i+3]);
sl@0
   487
    }
sl@0
   488
   fprintf(stdout,"\n");
sl@0
   489
sl@0
   490
}
sl@0
   491
sl@0
   492
char** create_params(char *ip_str,int *argc)
sl@0
   493
{
sl@0
   494
   char *paramets[50];
sl@0
   495
   char **cmd_line=NULL;
sl@0
   496
   char *str=NULL,*ptr=NULL;
sl@0
   497
   int len=0,cnt=0,i=0;
sl@0
   498
sl@0
   499
sl@0
   500
sl@0
   501
   str=ip_str;
sl@0
   502
sl@0
   503
   while(str)
sl@0
   504
   {
sl@0
   505
      ptr=strchr(str,' ');
sl@0
   506
      if(ptr)
sl@0
   507
      {
sl@0
   508
        len = ptr-str;
sl@0
   509
        paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
sl@0
   510
        if(paramets[cnt])
sl@0
   511
        {
sl@0
   512
          strncpy(paramets[cnt],str,len);
sl@0
   513
      	  paramets[cnt][len]='\0';
sl@0
   514
      	}
sl@0
   515
      	cnt++;
sl@0
   516
      	str+= len + 1;
sl@0
   517
      }
sl@0
   518
      else
sl@0
   519
      {
sl@0
   520
        len = strlen(str);
sl@0
   521
        paramets[cnt]=(char *)malloc(sizeof(char)*len+1);
sl@0
   522
        if(paramets[cnt])
sl@0
   523
       	strcpy(paramets[cnt],str);
sl@0
   524
        cnt++;
sl@0
   525
      	break;
sl@0
   526
      }
sl@0
   527
sl@0
   528
   }
sl@0
   529
sl@0
   530
   if(cnt)
sl@0
   531
   cmd_line=(char **)malloc(cnt*sizeof(char *));
sl@0
   532
   for(i=0;i<cnt;i++)
sl@0
   533
   cmd_line[i]=paramets[i];
sl@0
   534
   *argc=cnt;
sl@0
   535
   return cmd_line;
sl@0
   536
sl@0
   537
sl@0
   538
}
sl@0
   539
sl@0
   540
sl@0
   541
sl@0
   542
int compare_files(const char *file1, const char *file2)
sl@0
   543
{
sl@0
   544
sl@0
   545
   			FILE *fp1 = NULL;
sl@0
   546
			FILE *fp2 = NULL;
sl@0
   547
sl@0
   548
				long file1_size;
sl@0
   549
				long file2_size;
sl@0
   550
sl@0
   551
				char *file1_data = NULL;
sl@0
   552
				char *file2_data = NULL;
sl@0
   553
sl@0
   554
				int ret;
sl@0
   555
				int argc=0;
sl@0
   556
   			char** argv = NULL;
sl@0
   557
sl@0
   558
sl@0
   559
				if(!(fp1 = fopen(file1,"r+"))){
sl@0
   560
					fprintf(stdout,"Faliure opening file %s.",file1);
sl@0
   561
					ret = -1;
sl@0
   562
					goto err;
sl@0
   563
				}
sl@0
   564
sl@0
   565
				if(!(fp2 = fopen(file2,"r+"))){
sl@0
   566
					fprintf(stdout,"Faliure opening file %s.",file2);
sl@0
   567
					ret = -1;
sl@0
   568
					goto err;
sl@0
   569
				}
sl@0
   570
sl@0
   571
				if((ret = fseek(fp1, 0, SEEK_END)) < 0)
sl@0
   572
					goto err;
sl@0
   573
sl@0
   574
				if((ret = fseek(fp2, 0, SEEK_END)) < 0)
sl@0
   575
					goto err;
sl@0
   576
sl@0
   577
 				if((file1_size = ftell(fp1)) < 0)
sl@0
   578
				{
sl@0
   579
					ret = file1_size;
sl@0
   580
					goto err;
sl@0
   581
				}
sl@0
   582
				if((file2_size = ftell(fp1)) < 0)
sl@0
   583
					{
sl@0
   584
						ret = file2_size;
sl@0
   585
						goto err;
sl@0
   586
					}
sl@0
   587
sl@0
   588
				if(file1_size != file2_size)
sl@0
   589
				{
sl@0
   590
					fprintf(stdout,"File size of  %s is not same as that of %s.",file1,file2);
sl@0
   591
					ret = -1;
sl@0
   592
					goto err;
sl@0
   593
				}
sl@0
   594
sl@0
   595
                if((ret = fseek(fp1, 0, SEEK_SET)) < 0)
sl@0
   596
					goto err;
sl@0
   597
sl@0
   598
				if((ret = fseek(fp2, 0, SEEK_SET)) < 0)
sl@0
   599
					goto err;
sl@0
   600
sl@0
   601
				file1_data = (char *)malloc(file1_size);
sl@0
   602
				file2_data = (char *)malloc(file2_size);
sl@0
   603
sl@0
   604
				memset(file1_data,0,file1_size);
sl@0
   605
				memset(file2_data,0,file2_size);
sl@0
   606
sl@0
   607
				ret = fread(file1_data,sizeof(char),file1_size,fp1);
sl@0
   608
				if(ret < file1_size)
sl@0
   609
				{
sl@0
   610
				    ret = ferror(fp1);
sl@0
   611
					fprintf(stdout,"Failed reading file %s.", file1);
sl@0
   612
					ret = -1;
sl@0
   613
					goto err1;
sl@0
   614
				}
sl@0
   615
sl@0
   616
				ret = fread(file2_data,sizeof(char),file2_size,fp2);
sl@0
   617
				if(ret < file2_size)
sl@0
   618
				{
sl@0
   619
					fprintf(stdout,"Failed reading file %s.",file2);
sl@0
   620
					ret = -1;
sl@0
   621
					goto err2;
sl@0
   622
				}
sl@0
   623
sl@0
   624
				if(memcmp(file1_data,file2_data,file1_size))
sl@0
   625
				{
sl@0
   626
sl@0
   627
				   fprintf(stdout,"File file1 is not similar to file2.");
sl@0
   628
				   ret = -1;
sl@0
   629
				   goto err2;
sl@0
   630
				}
sl@0
   631
				else
sl@0
   632
				ret=0; //file are smae
sl@0
   633
sl@0
   634
err2:
sl@0
   635
				fclose(fp2);
sl@0
   636
err1:
sl@0
   637
				fclose(fp1);
sl@0
   638
err:
sl@0
   639
sl@0
   640
				if(file1_data)
sl@0
   641
					free(file1_data);
sl@0
   642
sl@0
   643
				if(file2_data)
sl@0
   644
					free(file2_data);
sl@0
   645
sl@0
   646
				return ret;
sl@0
   647
}
sl@0
   648
sl@0
   649
void delete_file(char *file)
sl@0
   650
{
sl@0
   651
	unlink(file);
sl@0
   652
}
sl@0
   653
sl@0
   654
void delete_params(char ** cmd_line,int argc)
sl@0
   655
{
sl@0
   656
	int i;
sl@0
   657
	for(i=0;i<argc;i++)
sl@0
   658
	if(cmd_line[i])	free(cmd_line[i]);
sl@0
   659
	if(cmd_line)
sl@0
   660
	free(cmd_line);
sl@0
   661
	cmd_line=NULL;
sl@0
   662
}
sl@0
   663
sl@0
   664
void strip(char *str)
sl@0
   665
{
sl@0
   666
	int len=0;
sl@0
   667
	char *ptr=NULL;
sl@0
   668
	char *ptr1=NULL;
sl@0
   669
	len=strlen(str);
sl@0
   670
	ptr=strchr(str,'\r');
sl@0
   671
	ptr1=strchr(str,'\n');
sl@0
   672
	if((ptr) || (ptr1))
sl@0
   673
	{
sl@0
   674
		if(ptr)
sl@0
   675
		*ptr='\0'; //strip \r and \n
sl@0
   676
		else
sl@0
   677
		*ptr1='\0';
sl@0
   678
	}
sl@0
   679
	return;
sl@0
   680
}
sl@0
   681
sl@0
   682
sl@0
   683
int main(int argc_m, char *argv_m[])
sl@0
   684
{
sl@0
   685
sl@0
   686
   int ret,argc=0;
sl@0
   687
   char argstr[200];
sl@0
   688
   int total_cases,cur_case=0,fail=0;
sl@0
   689
   char **argv=NULL;
sl@0
   690
   char openssltestnum[20];
sl@0
   691
   FILE *arg_fp;
sl@0
   692
sl@0
   693
sl@0
   694
  if(openssl_init())
sl@0
   695
  {
sl@0
   696
    return 1;
sl@0
   697
  }
sl@0
   698
sl@0
   699
sl@0
   700
sl@0
   701
    if(argc_m > 1)
sl@0
   702
    	arg_fp = fopen(argv_m[1],"r+");
sl@0
   703
    else
sl@0
   704
    	return 1; // filename needed as argument.
sl@0
   705
sl@0
   706
sl@0
   707
    if(!arg_fp)
sl@0
   708
    {
sl@0
   709
      return 1;
sl@0
   710
    }
sl@0
   711
sl@0
   712
    while(!feof(arg_fp)&&!fail)
sl@0
   713
    {
sl@0
   714
  	  memset(argstr,0,200);
sl@0
   715
      fgets(argstr, 200, arg_fp);
sl@0
   716
      strip(argstr);
sl@0
   717
      argv = create_params(argstr,&argc);
sl@0
   718
	  strcpy(openssltestnum,argv[1]);
sl@0
   719
sl@0
   720
	  if(!strcmp(argv[2],"test_log"))
sl@0
   721
      {
sl@0
   722
        print_logs(argc,argv);
sl@0
   723
      }
sl@0
   724
      else if(!strcmp(argv[2],"compare"))
sl@0
   725
      {
sl@0
   726
        ret = compare_files(argv[3],argv[4]);
sl@0
   727
        if(ret)
sl@0
   728
        {
sl@0
   729
        testResultXml(openssltestnum, ret);
sl@0
   730
        fail=1;
sl@0
   731
        }
sl@0
   732
sl@0
   733
      }
sl@0
   734
      else if(!strcmp(argv[2],"append"))
sl@0
   735
      {
sl@0
   736
        ret = append(argv[3],argv[4],argv[5]);
sl@0
   737
        if(ret)
sl@0
   738
        {
sl@0
   739
         testResultXml(openssltestnum, ret);
sl@0
   740
         fail=1;
sl@0
   741
        }
sl@0
   742
sl@0
   743
      }
sl@0
   744
       else if(!strcmp(argv[2],"delete"))
sl@0
   745
      {
sl@0
   746
        delete_file(argv[3]);
sl@0
   747
sl@0
   748
      }
sl@0
   749
      else
sl@0
   750
      {
sl@0
   751
        ret = openssl_main(argc-2,argv+2);
sl@0
   752
        if(ret)
sl@0
   753
        {
sl@0
   754
        testResultXml(openssltestnum, ret);
sl@0
   755
        fail=1;
sl@0
   756
        }
sl@0
   757
sl@0
   758
      }
sl@0
   759
sl@0
   760
     //delete_params(argv,argc);
sl@0
   761
   }//while
sl@0
   762
sl@0
   763
   if(!fail&&!ret)
sl@0
   764
   testResultXml(openssltestnum, fail);
sl@0
   765
   fclose(arg_fp);
sl@0
   766
sl@0
   767
sl@0
   768
 openssl_deinit();
sl@0
   769
 return ret;
sl@0
   770
}
sl@0
   771
sl@0
   772
#endif  /* IF_DEF SYMBIAN */
sl@0
   773
#ifndef SYMBIAN
sl@0
   774
int main(int Argc, char *Argv[])
sl@0
   775
#else
sl@0
   776
int openssl_main(int Argc, char *Argv[])
sl@0
   777
#endif
sl@0
   778
	{
sl@0
   779
	ARGS arg;
sl@0
   780
#define PROG_NAME_SIZE	39
sl@0
   781
	char pname[PROG_NAME_SIZE+1];
sl@0
   782
	FUNCTION f,*fp;
sl@0
   783
	MS_STATIC const char *prompt;
sl@0
   784
	MS_STATIC char buf[1024];
sl@0
   785
	char *to_free=NULL;
sl@0
   786
	int n,i,ret=0;
sl@0
   787
	int argc;
sl@0
   788
	char **argv,*p;
sl@0
   789
	LHASH *prog=NULL;
sl@0
   790
	long errline;
sl@0
   791
sl@0
   792
	arg.data=NULL;
sl@0
   793
	arg.count=0;
sl@0
   794
sl@0
   795
	if (bio_err == NULL)
sl@0
   796
		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
sl@0
   797
#ifdef SYMBIAN
sl@0
   798
        BIO_set_fp(bio_err,fp_stderr,BIO_NOCLOSE|BIO_FP_TEXT);
sl@0
   799
#else
sl@0
   800
        BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
sl@0
   801
#endif
sl@0
   802
	if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
sl@0
   803
		{
sl@0
   804
		if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
sl@0
   805
			{
sl@0
   806
			CRYPTO_malloc_debug_init();
sl@0
   807
			CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
sl@0
   808
			}
sl@0
   809
		else
sl@0
   810
			{
sl@0
   811
			/* OPENSSL_DEBUG_MEMORY=off */
sl@0
   812
			CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
sl@0
   813
			}
sl@0
   814
		}
sl@0
   815
	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
sl@0
   816
sl@0
   817
#if 0
sl@0
   818
	if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
sl@0
   819
#endif
sl@0
   820
		{
sl@0
   821
		CRYPTO_set_locking_callback(lock_dbg_cb);
sl@0
   822
		}
sl@0
   823
sl@0
   824
	apps_startup();
sl@0
   825
sl@0
   826
	/* Lets load up our environment a little */
sl@0
   827
	p=getenv("OPENSSL_CONF");
sl@0
   828
	if (p == NULL)
sl@0
   829
		p=getenv("SSLEAY_CONF");
sl@0
   830
	if (p == NULL)
sl@0
   831
		p=to_free=make_config_name();
sl@0
   832
sl@0
   833
	default_config_file=p;
sl@0
   834
sl@0
   835
	config=NCONF_new(NULL);
sl@0
   836
	i=NCONF_load(config,p,&errline);
sl@0
   837
	if (i == 0)
sl@0
   838
		{
sl@0
   839
		NCONF_free(config);
sl@0
   840
		config = NULL;
sl@0
   841
		ERR_clear_error();
sl@0
   842
		}
sl@0
   843
sl@0
   844
	prog=prog_init();
sl@0
   845
sl@0
   846
	/* first check the program name */
sl@0
   847
	program_name(Argv[0],pname,sizeof pname);
sl@0
   848
sl@0
   849
	f.name=pname;
sl@0
   850
	fp=(FUNCTION *)lh_retrieve(prog,&f);
sl@0
   851
	if (fp != NULL)
sl@0
   852
		{
sl@0
   853
		Argv[0]=pname;
sl@0
   854
		ret=fp->func(Argc,Argv);
sl@0
   855
		goto end;
sl@0
   856
		}
sl@0
   857
sl@0
   858
	/* ok, now check that there are not arguments, if there are,
sl@0
   859
	 * run with them, shifting the ssleay off the front */
sl@0
   860
	if (Argc != 1)
sl@0
   861
		{
sl@0
   862
		Argc--;
sl@0
   863
		Argv++;
sl@0
   864
		ret=do_cmd(prog,Argc,Argv);
sl@0
   865
		if (ret < 0) ret=0;
sl@0
   866
		goto end;
sl@0
   867
		}
sl@0
   868
sl@0
   869
	/* ok, lets enter the old 'OpenSSL>' mode */
sl@0
   870
sl@0
   871
	for (;;)
sl@0
   872
		{
sl@0
   873
		ret=0;
sl@0
   874
		p=buf;
sl@0
   875
		n=sizeof buf;
sl@0
   876
		i=0;
sl@0
   877
		for (;;)
sl@0
   878
			{
sl@0
   879
			p[0]='\0';
sl@0
   880
			if (i++)
sl@0
   881
				prompt=">";
sl@0
   882
			else	prompt="OpenSSL> ";
sl@0
   883
#ifndef SYMBIAN
sl@0
   884
			fputs(prompt,stdout);
sl@0
   885
			fflush(stdout);
sl@0
   886
			fgets(p,n,stdin);
sl@0
   887
#else
sl@0
   888
            fputs(prompt,stdout);
sl@0
   889
			fflush(stdout);
sl@0
   890
			fgets(p,n,stdin);
sl@0
   891
sl@0
   892
#endif
sl@0
   893
			if (p[0] == '\0') goto end;
sl@0
   894
			i=strlen(p);
sl@0
   895
			if (i <= 1) break;
sl@0
   896
			if (p[i-2] != '\\') break;
sl@0
   897
			i-=2;
sl@0
   898
			p+=i;
sl@0
   899
			n-=i;
sl@0
   900
			}
sl@0
   901
		if (!chopup_args(&arg,buf,&argc,&argv)) break;
sl@0
   902
sl@0
   903
		ret=do_cmd(prog,argc,argv);
sl@0
   904
		if (ret < 0)
sl@0
   905
			{
sl@0
   906
			ret=0;
sl@0
   907
			goto end;
sl@0
   908
			}
sl@0
   909
		if (ret != 0)
sl@0
   910
			BIO_printf(bio_err,"error in %s\n",argv[0]);
sl@0
   911
		(void)BIO_flush(bio_err);
sl@0
   912
		}
sl@0
   913
	BIO_printf(bio_err,"bad exit\n");
sl@0
   914
	ret=1;
sl@0
   915
end:
sl@0
   916
	if (to_free)
sl@0
   917
		OPENSSL_free(to_free);
sl@0
   918
	if (config != NULL)
sl@0
   919
		{
sl@0
   920
		NCONF_free(config);
sl@0
   921
		config=NULL;
sl@0
   922
		}
sl@0
   923
	if (prog != NULL) lh_free(prog);
sl@0
   924
	if (arg.data != NULL) OPENSSL_free(arg.data);
sl@0
   925
sl@0
   926
	apps_shutdown();
sl@0
   927
sl@0
   928
	CRYPTO_mem_leaks(bio_err);
sl@0
   929
	if (bio_err != NULL)
sl@0
   930
		{
sl@0
   931
		BIO_free(bio_err);
sl@0
   932
		bio_err=NULL;
sl@0
   933
		}
sl@0
   934
sl@0
   935
	return ret;
sl@0
   936
//	OPENSSL_EXIT(ret);
sl@0
   937
	}
sl@0
   938
sl@0
   939
#define LIST_STANDARD_COMMANDS "list-standard-commands"
sl@0
   940
#define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
sl@0
   941
#define LIST_CIPHER_COMMANDS "list-cipher-commands"
sl@0
   942
sl@0
   943
static int do_cmd(LHASH *prog, int argc, char *argv[])
sl@0
   944
	{
sl@0
   945
	FUNCTION f,*fp;
sl@0
   946
	int i,ret=1,tp,nl;
sl@0
   947
sl@0
   948
	if ((argc <= 0) || (argv[0] == NULL))
sl@0
   949
		{ ret=0; goto end; }
sl@0
   950
	f.name=argv[0];
sl@0
   951
	fp=(FUNCTION *)lh_retrieve(prog,&f);
sl@0
   952
	if (fp != NULL)
sl@0
   953
		{
sl@0
   954
		ret=fp->func(argc,argv);
sl@0
   955
		}
sl@0
   956
	else if ((strncmp(argv[0],"no-",3)) == 0)
sl@0
   957
		{
sl@0
   958
sl@0
   959
		BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
sl@0
   960
#ifdef OPENSSL_SYS_VMS
sl@0
   961
		{
sl@0
   962
		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
sl@0
   963
		bio_stdout = BIO_push(tmpbio, bio_stdout);
sl@0
   964
		}
sl@0
   965
#endif
sl@0
   966
		f.name=argv[0]+3;
sl@0
   967
		ret = (lh_retrieve(prog,&f) != NULL);
sl@0
   968
		if (!ret)
sl@0
   969
			BIO_printf(bio_stdout, "%s\n", argv[0]);
sl@0
   970
		else
sl@0
   971
			BIO_printf(bio_stdout, "%s\n", argv[0]+3);
sl@0
   972
		BIO_free_all(bio_stdout);
sl@0
   973
		goto end;
sl@0
   974
		}
sl@0
   975
	else if ((strcmp(argv[0],"quit") == 0) ||
sl@0
   976
		(strcmp(argv[0],"q") == 0) ||
sl@0
   977
		(strcmp(argv[0],"exit") == 0) ||
sl@0
   978
		(strcmp(argv[0],"bye") == 0))
sl@0
   979
		{
sl@0
   980
		ret= -1;
sl@0
   981
		goto end;
sl@0
   982
		}
sl@0
   983
	else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
sl@0
   984
		(strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
sl@0
   985
		(strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
sl@0
   986
		{
sl@0
   987
		int list_type;
sl@0
   988
		BIO *bio_stdout;
sl@0
   989
sl@0
   990
		if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
sl@0
   991
			list_type = FUNC_TYPE_GENERAL;
sl@0
   992
		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
sl@0
   993
			list_type = FUNC_TYPE_MD;
sl@0
   994
		else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
sl@0
   995
			list_type = FUNC_TYPE_CIPHER;
sl@0
   996
sl@0
   997
		bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
sl@0
   998
#ifdef OPENSSL_SYS_VMS
sl@0
   999
		{
sl@0
  1000
		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
sl@0
  1001
		bio_stdout = BIO_push(tmpbio, bio_stdout);
sl@0
  1002
		}
sl@0
  1003
#endif
sl@0
  1004
sl@0
  1005
		for (fp=functions; fp->name != NULL; fp++)
sl@0
  1006
			if (fp->type == list_type)
sl@0
  1007
				BIO_printf(bio_stdout, "%s\n", fp->name);
sl@0
  1008
		BIO_free_all(bio_stdout);
sl@0
  1009
		ret=0;
sl@0
  1010
		goto end;
sl@0
  1011
		}
sl@0
  1012
	else
sl@0
  1013
		{
sl@0
  1014
		BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
sl@0
  1015
			argv[0]);
sl@0
  1016
		BIO_printf(bio_err, "\nStandard commands");
sl@0
  1017
		i=0;
sl@0
  1018
		tp=0;
sl@0
  1019
		for (fp=functions; fp->name != NULL; fp++)
sl@0
  1020
			{
sl@0
  1021
			nl=0;
sl@0
  1022
			if (((i++) % 5) == 0)
sl@0
  1023
				{
sl@0
  1024
				BIO_printf(bio_err,"\n");
sl@0
  1025
				nl=1;
sl@0
  1026
				}
sl@0
  1027
			if (fp->type != tp)
sl@0
  1028
				{
sl@0
  1029
				tp=fp->type;
sl@0
  1030
				if (!nl) BIO_printf(bio_err,"\n");
sl@0
  1031
				if (tp == FUNC_TYPE_MD)
sl@0
  1032
					{
sl@0
  1033
					i=1;
sl@0
  1034
					BIO_printf(bio_err,
sl@0
  1035
						"\nMessage Digest commands (see the `dgst' command for more details)\n");
sl@0
  1036
					}
sl@0
  1037
				else if (tp == FUNC_TYPE_CIPHER)
sl@0
  1038
					{
sl@0
  1039
					i=1;
sl@0
  1040
					BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
sl@0
  1041
					}
sl@0
  1042
				}
sl@0
  1043
			BIO_printf(bio_err,"%-15s",fp->name);
sl@0
  1044
			}
sl@0
  1045
		BIO_printf(bio_err,"\n\n");
sl@0
  1046
		ret=0;
sl@0
  1047
		}
sl@0
  1048
end:
sl@0
  1049
	return(ret);
sl@0
  1050
	}
sl@0
  1051
sl@0
  1052
static int SortFnByName(const void *_f1,const void *_f2)
sl@0
  1053
    {
sl@0
  1054
    const FUNCTION *f1=_f1;
sl@0
  1055
    const FUNCTION *f2=_f2;
sl@0
  1056
sl@0
  1057
    if(f1->type != f2->type)
sl@0
  1058
	return f1->type-f2->type;
sl@0
  1059
    return strcmp(f1->name,f2->name);
sl@0
  1060
    }
sl@0
  1061
sl@0
  1062
static LHASH *prog_init(void)
sl@0
  1063
	{
sl@0
  1064
	LHASH *ret;
sl@0
  1065
	FUNCTION *f;
sl@0
  1066
	size_t i;
sl@0
  1067
sl@0
  1068
	/* Purely so it looks nice when the user hits ? */
sl@0
  1069
	for(i=0,f=functions ; f->name != NULL ; ++f,++i)
sl@0
  1070
	    ;
sl@0
  1071
	qsort(functions,i,sizeof *functions,SortFnByName);
sl@0
  1072
sl@0
  1073
	if ((ret=lh_new(hash, cmp)) == NULL)
sl@0
  1074
		return(NULL);
sl@0
  1075
sl@0
  1076
	for (f=functions; f->name != NULL; f++)
sl@0
  1077
		lh_insert(ret,f);
sl@0
  1078
	return(ret);
sl@0
  1079
	}
sl@0
  1080
sl@0
  1081
/* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
sl@0
  1082
static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
sl@0
  1083
	{
sl@0
  1084
	return(strncmp(((const FUNCTION *)a_void)->name,
sl@0
  1085
			((const FUNCTION *)b_void)->name,8));
sl@0
  1086
	}
sl@0
  1087
sl@0
  1088
/* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
sl@0
  1089
static unsigned long MS_CALLBACK hash(const void *a_void)
sl@0
  1090
	{
sl@0
  1091
	return(lh_strhash(((const FUNCTION *)a_void)->name));
sl@0
  1092
	}