Wikis / Unreal Wiki / Legacy:Base64

Wikipedia:Base64 is a data encoding scheme whereby binary-encoded data is converted to printable ASCII characters. It is defined as a MIME content transfer encoding for use in internet e-mail. The only characters used are the upper- and lower-case Roman alphabet characters (A-Z, a-z), the numerals (0-9), and the "+" and "/" symbols, with the "=" symbol as a special suffix code.

Base64 is used in emails but also for basic HTTP authentication. However, base64 is not even close to anything secure. In other words, don't use it for securing information.

Code

/**
	base64 encode an input array
*/
static function array<string> Base64Encode(array<string> indata, out array<string> B64Lookup)
{
	local array<string> result;
	local int i, dl, n;
	local string res;
	local array<byte> inp;
	local array<string> outp;
 
	if (B64Lookup.length != 64) Base64EncodeLookupTable(B64Lookup);
 
	// convert string to byte array
	for (n = 0; n < indata.length; n++)
	{
		res = indata[n];
		outp.length = 0;
		inp.length = 0;
		for (i = 0; i < len(res); i++)
		{
			inp[inp.length] = Asc(Mid(res, i, 1));
		}
 
		dl = inp.length;
		// fix byte array
		if ((dl%3) == 1)
		{
			inp[inp.length] = 0;
			inp[inp.length] = 0;
		}
		if ((dl%3) == 2)
		{
			inp[inp.length] = 0;
		}
		i = 0;
		while (i < dl)
		{
			outp[outp.length] = B64Lookup[(inp[i] >> 2)];
			outp[outp.length] = B64Lookup[((inp[i]&3)<<4) | (inp[i+1]>>4)];
			outp[outp.length] = B64Lookup[((inp[i+1]&15)<<2) | (inp[i+2]>>6)];
			outp[outp.length] = B64Lookup[(inp[i+2]&63)];
			i += 3;
		}
		// pad result
		if ((dl%3) == 1)
		{
			outp[outp.length-1] = "=";
			outp[outp.length-2] = "=";
		}
		if ((dl%3) == 2)
		{
			outp[outp.length-1] = "=";
		}
 
		res = "";
		for (i = 0; i < outp.length; i++)
		{
			res = res$outp[i];
		}
		result[result.length] = res;
	}
 
	return result;
}
 
/**
	Decode a base64 encoded string
*/
static function array<string> Base64Decode(array<string> indata)
{
	local array<string> result;
	local int i, dl, n, padded;
	local string res;
	local array<byte> inp;
	local array<string> outp;
 
	// convert string to byte array
	for (n = 0; n < indata.length; n++)
	{
		res = indata[n];
		outp.length = 0;
		inp.length = 0;
		padded = 0;
		for (i = 0; i < len(res); i++)
		{
			dl = Asc(Mid(res, i, 1));
			// convert base64 ascii to base64 index
			if ((dl >= 65) && (dl <= 90)) dl -= 65; // cap alpha
			else if ((dl >= 97) && (dl <= 122)) dl -= 71; // low alpha
			else if ((dl >= 48) && (dl <= 57)) dl += 4; // digits
			else if (dl == 43) dl = 62;
			else if (dl == 47) dl = 63;
			else if (dl == 61) padded++;
			inp[inp.length] = dl;
		}
 
		dl = inp.length;
		i = 0;
		while (i < dl)
		{
			outp[outp.length] = Chr((inp[i] << 2) | (inp[i+1] >> 4));
			outp[outp.length] = Chr(((inp[i+1]&15)<<4) | (inp[i+2]>>2));
			outp[outp.length] = Chr(((inp[i+2]&3)<<6) | (inp[i+3]));
			i += 4;
		}
		outp.length = outp.length-padded;
 
		res = "";
		for (i = 0; i < outp.length; i++)
		{
			res = res$outp[i];
		}
		result[result.length] = res;
	}
 
	return result;
}
 
/**
	Generate the base 64 encode lookup table
*/
static function Base64EncodeLookupTable(out array<string> LookupTable)
{
	local int i;
	for (i = 0; i < 26; i++)
	{
		LookupTable[i] = Chr(i+65);
	}
	for (i = 0; i < 26; i++)
	{
		LookupTable[i+26] = Chr(i+97);
	}
	for (i = 0; i < 10; i++)
	{
		LookupTable[i+52] = Chr(i+48);
	}
	LookupTable[62] = "+";
	LookupTable[63] = "/";
}

License

This code is part of the LibHTTP package which is released under the LesserOpenUnrealModLicense

Copyright 2003/2004 Michiel "El Muerte" Hendriks

Related Articles

  • RFC 1341 → the MIME rfc explains the base64 encoding algorithm

Page Categories

Page Information

2022-11-20T20:27:12.814016Z 2004-09-21T16:19:17Z El Muerte * https://wiki.beyondunreal.com/Legacy:Base64 Attribution-NonCommercial-ShareAlike 3.0