Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

How to write a simple but effective TCP/IP port scanner for Win32

, 27 Oct 2001
An article on how to write a TCP/IP port scanner with a GUI, based on the MFC's property sheet paradigm
/*
	CBase64.cpp
	Classe base per codifica/decodifica base64 (CRT).
	Luca Piergentili, 14/09/96
	lpiergentili@yahoo.com
	http://www.geocities.com/lpiergentili/
*/
#include "env.h"
#include "pragma.h"
#include <stdio.h>
#include <stdlib.h>
#include "CBase64.h"

/*
	Encode()
*/
int CBase64::Encode(const char *lpcszPlainFile,const char *lpcszEncodedFile)
{
	char szBuffer[128];
	FILE* pInputFile;
	FILE* pOutputFile;
	unsigned char cChar = 0;
	unsigned long nShifted = 0L;
	unsigned long lValue = 0L;
	int nIndex = 0;
	int nShift = 0;
	int nSaveShift = 0;
	int bDone = 0;
	
	static const char base64table[] = {
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
	};
     
	if((pInputFile = fopen(lpcszPlainFile,"rb"))==(FILE*)NULL)
		return(-1);
	
	if((pOutputFile = fopen(lpcszEncodedFile,"w"))==(FILE*)NULL)
	{
		fclose(pInputFile);
		return(-2);
	}

	do
	{
		bDone = 0;
		nShift = 0;
		nShifted = 0L;
		nIndex = 0;

		while(!feof(pInputFile) || nShift!=0)
		{
			if(!feof(pInputFile) && !bDone)
			{
				cChar = (char)fgetc(pInputFile);

				if(feof(pInputFile))
				{
					bDone = 1;
					nSaveShift = nShift;
					cChar = 0;
				}
			}
			else
			{
				bDone = 1;
				nSaveShift = nShift;
				cChar = 0;
			}

			if(!bDone || nShift!=0)
			{
				lValue = (unsigned long)cChar;
				nShifted <<= 8;
				nShift += 8;
				nShifted |= lValue;
			}

			while(nShift >= 6)
			{
				nShift -= 6;
				lValue = (nShifted >> nShift) & 0x3fL;
				cChar = (unsigned char)base64table[(int)lValue];

				szBuffer[nIndex++] = cChar;
				
				if(nIndex >= 60)
				{
					szBuffer[nIndex] = '\0';
					fprintf(pOutputFile,"%s\n",szBuffer);
					nIndex = 0;
				}

				if(bDone)
					nShift = 0;
			}
		}

		if(nSaveShift==2)
		{
			szBuffer[nIndex++] = '=';
			
			if(nIndex >= 60)
			{
				szBuffer[nIndex] = '\0';
				fprintf(pOutputFile,"%s\n",szBuffer);
				nIndex = 0;
			}

			szBuffer[nIndex++] = '=';
			
			if(nIndex >= 60)
			{
				szBuffer[nIndex] = '\0';
				fprintf(pOutputFile,"%s\n",szBuffer);
				nIndex = 0;
			}
		}
		else if(nSaveShift==4)
		{
			szBuffer[nIndex++] = '=';
			
			if(nIndex >= 60)
			{
				szBuffer[nIndex] = '\0';
				fprintf(pOutputFile,"%s\n",szBuffer);
				nIndex = 0;
			}
		}

		if(nIndex!=0)
		{
			szBuffer[nIndex] = '\0';
			fprintf(pOutputFile,"%s\n",szBuffer);
		}
	}
	while(!feof(pInputFile));

	fclose(pInputFile);
	fclose(pOutputFile);

	return(0);
}

/*
	Decode()
*/
int CBase64::Decode(const char *lpcszEncodedFile,const char *lpcszPlainFile)
{
	char szBuffer[128];
	FILE* pInputFile;
	FILE* pOutputFile;
	unsigned char cChar = 0;
	unsigned long nShifted = 0L;
	unsigned long lValue = 0L;
	int nIndex = 0;
	int nShift = 0;
	int bDone = 0;
     
	if((pInputFile = fopen(lpcszEncodedFile,"r"))==(FILE*)NULL)
		return(-1);

	if((pOutputFile = fopen(lpcszPlainFile,"wb"))==(FILE*)NULL)
	{
		fclose(pInputFile);
		return(-1);
	}
     
	do
	{
		bDone = 0;
		nShift = 0;
		nShifted = 0L;
		
		while(!feof(pInputFile) && !bDone)
		{
			if(fgets(szBuffer,sizeof(szBuffer),pInputFile))
			{
				for(nIndex = 0; szBuffer[nIndex] && szBuffer[nIndex]!='\n'; nIndex++)
				{
					lValue = ConvertToAscii(szBuffer[nIndex]);
	
					if(lValue < 64)
					{
						nShifted <<= 6;
						nShift += 6;
						nShifted |= lValue;
						
						if(nShift >= 8)
						{
							nShift -= 8;
							lValue = nShifted >> nShift;
							cChar = (unsigned char)(lValue & 0xffL);
							fputc(cChar,pOutputFile);
						}
					}
					else
					{
						bDone = 1;
						break;
					}
				}
			}
		}
	}
	while(!feof(pInputFile));

	fclose(pInputFile);
	fclose(pOutputFile);

	return(0);
}

/*
	ConvertToAscii()
*/
int CBase64::ConvertToAscii(unsigned char cChar)
{
	if((cChar >= 'A') && (cChar <= 'Z'))
	{
		return((int)(cChar - 'A'));
	}
	else if((cChar >= 'a') && (cChar <= 'z'))
	{
		return(26 + (int)(cChar - 'a'));
	}
	else if((cChar >= '0') && (cChar <= '9'))
	{
		return(52 + (int)(cChar - '0'));
	}
	else if(cChar=='+')
	{
		return(62);
	}
	else if(cChar=='/')
	{
		return(63);
	}
	else if(cChar=='=')
	{
		return(-2);
	}
	else
		return(-1);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Luca Piergentili
Web Developer
Italy Italy
I like C and C++, Acid Jazz, James Brown, gli Spaghetti Aglio e Olio, alla Bolognesa, alla Puttanesca e le Fettuccine alla Matriciana ('Maccaroni' over the world). Of course I like beautiful big tits girls too, my little car, Frank Zappa, the art of Zen, italian coffee and much more...

| Advertise | Privacy | Mobile
Web04 | 2.8.140905.1 | Last Updated 28 Oct 2001
Article Copyright 2001 by Luca Piergentili
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid