Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 155c93f
  • /
  • demos
  • /
  • maurice
  • /
  • example4.c
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:ce629848b74fc6617cce73ba87fc1e887ef79eeb
directory badge Iframe embedding
swh:1:dir:085bfd4fdc3373f26d2c6518586aeb48099ea1a5
example4.c
/* NOCW */
/*
        Please read the README file for condition of use, before
        using this software.

        Maurice Gittens  <mgittens@gits.nl>   January 1997

*/

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <openssl/evp.h>

#define STDIN     	0
#define STDOUT    	1
#define BUFLEN	  	512 

static const char *usage = "Usage: example4 [-d]\n";

void do_encode(void);
void do_decode(void);

int main(int argc, char *argv[])
{
	if ((argc == 1))	
	{
		do_encode();
	}	
	else if ((argc == 2) && !strcmp(argv[1],"-d"))
	{
		do_decode();
	}
	else
	{
		fprintf(stderr,"%s", usage);
		exit(1);
	}

	return 0;		
}

void do_encode()
{
	char buf[BUFLEN];
	char ebuf[BUFLEN+24];
	unsigned int ebuflen;
	EVP_ENCODE_CTX ectx;
        
	EVP_EncodeInit(&ectx);

	while(1)
	{
		int readlen = read(STDIN, buf, sizeof(buf));
	
		if (readlen <= 0)
		{
			if (!readlen)
			   break;
			else
			{
				perror("read");
				exit(1);
			}
		}

		EVP_EncodeUpdate(&ectx, ebuf, &ebuflen, buf, readlen);

		write(STDOUT, ebuf, ebuflen);
	}

        EVP_EncodeFinal(&ectx, ebuf, &ebuflen); 

	write(STDOUT, ebuf, ebuflen);
}

void do_decode()
{
 	char buf[BUFLEN];
 	char ebuf[BUFLEN+24];
	unsigned int ebuflen;
	EVP_ENCODE_CTX ectx;
        
	EVP_DecodeInit(&ectx);

	while(1)
	{
		int readlen = read(STDIN, buf, sizeof(buf));
		int rc;	
	
		if (readlen <= 0)
		{
			if (!readlen)
			   break;
			else
			{
				perror("read");
				exit(1);
			}
		}

		rc = EVP_DecodeUpdate(&ectx, ebuf, &ebuflen, buf, readlen);
		if (rc <= 0)
		{
			if (!rc)
			{
				write(STDOUT, ebuf, ebuflen);
				break;
			}

			fprintf(stderr, "Error: decoding message\n");
			return;
		}

		write(STDOUT, ebuf, ebuflen);
	}

        EVP_DecodeFinal(&ectx, ebuf, &ebuflen); 

	write(STDOUT, ebuf, ebuflen); 
}

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top