/// Name: CBase32.cs
/// Module: CBase32
/// Author: Mittermeyer
/// Comment:
///
/// History:
/// 2004-08-31 Mittermeyer Create
///
/// Copyright 1995-2004 by Mittermeyer.All rights reserved.
/// *******************************************************************************
using System.Text;
{
/// <summary>
/// Summary description for CBase32.
/// </summary>
public class CBase32
{
private const String DefaultBase32Map = "ABCDEFGHIJKLMNPQRSTUVWXYZ3456789";
private const Int32 Base32MapLength = 32;
private static Char[] m_acBase32Map = null;
{
m_acBase32Map = DefaultBase32Map.ToCharArray();
}
public CBase32()
{
}
{
get
{
return m_acBase32Map.ToString();
}
set
{
if (value != null && value.Length >= Base32MapLength)
{
m_acBase32Map = value.ToCharArray();
}
else
{
m_acBase32Map = DefaultBase32Map.ToCharArray();
}
}
}
{
Char cMappingData = '/0';
{
cMappingData = m_acBase32Map[dwIndex];
}
return cMappingData;
}
public static Int32 GetCharIndex(Char cData)
{
Int32 dwIndex = -1,dwLoop = 0;
{
for (dwLoop = 0;dwLoop < m_acBase32Map.Length;dwLoop++)
{
if (m_acBase32Map[dwLoop] == cData)
{
dwIndex = dwLoop;
break;
}
}
}
return dwIndex;
}
{
Int32 dwLoop = 0,dwCharIndex = 0,dwCharCount;
Char[] acPart = null;
StringBuilder sbOutput = null;
return null;
{
dwCharCount = (Int32) (abData.Length / 5f * 8f) + 1;
sbOutput = new StringBuilder(dwCharCount);
acPart = new Char[8];
}
catch (Exception)
{
}
if (acPart == null || sbOutput == null)
return null;
for(dwLoop = 0;dwLoop < abData.Length;dwLoop += 5)
{
// every 5 bytes is a unit,can convert to 8 chars
// data format:
// AAAAABBB BBCCCCCD DDDDEEEE EFFFFFGG GGGHHHHH
switch (abData.Length - dwLoop)
{
case 1:
dwCharIndex = abData[dwLoop] >> 3; // AAAAA
acPart[0] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop] & 0x7) << 2; // BBB00
acPart[1] = m_acBase32Map[dwCharIndex];
dwCharCount = 2;
break;
dwCharIndex = abData[dwLoop] >> 3; // AAAAA
acPart[0] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop] & 0x7) << 2) + (abData[dwLoop + 1] >> 6); // BBBBB
acPart[1] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = m_acBase32Map[dwCharIndex];
dwCharIndex = abData[dwLoop + 1] & 0x1; // D0000
acPart[3] = m_acBase32Map[dwCharIndex];
dwCharCount = 4;
break;
dwCharIndex = abData[dwLoop] >> 3; // AAAAA
acPart[0] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop] & 0x7) << 2) + (abData[dwLoop + 1] >> 6); // BBBBB
acPart[1] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop + 1] & 0x1) << 4) + (abData[dwLoop + 2] >> 4);// DDDDD
acPart[3] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 2] & 0xF) << 1; // EEEE0
acPart[4] = m_acBase32Map[dwCharIndex];
dwCharCount = 5;
break;
dwCharIndex = abData[dwLoop] >> 3; // AAAAA
acPart[0] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop] & 0x7) << 2) + (abData[dwLoop + 1] >> 6); // BBBBB
acPart[1] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop + 1] & 0x1) << 4) + (abData[dwLoop + 2] >> 4);// DDDDD
acPart[3] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop + 2] & 0xF) << 1) + (abData[dwLoop + 3] >> 7);// EEEEE
acPart[4] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 3] & 0x3) << 3; // GG000
acPart[6] = m_acBase32Map[dwCharIndex];
dwCharCount = 7;
break;
dwCharIndex = abData[dwLoop] >> 3; // AAAAA
acPart[0] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop] & 0x7) << 2) + (abData[dwLoop + 1] >> 6); // BBBBB
acPart[1] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 1] & 0x3F) >> 1; // CCCCC
acPart[2] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop + 1] & 0x1) << 4) + (abData[dwLoop + 2] >> 4);// DDDDD
acPart[3] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop + 2] & 0xF) << 1) + (abData[dwLoop + 3] >> 7);// EEEEE
acPart[4] = m_acBase32Map[dwCharIndex];
dwCharIndex = (abData[dwLoop + 3] & 0x7F) >> 2; // FFFFF
acPart[5] = m_acBase32Map[dwCharIndex];
dwCharIndex = ((abData[dwLoop + 3] & 0x3) << 3) + (abData[dwLoop + 4] >> 5);// GGGGG
acPart[6] = m_acBase32Map[dwCharIndex];
dwCharIndex = abData[dwLoop + 4] & 0x1F; // HHHHH
acPart[7] = m_acBase32Map[dwCharIndex];
dwCharCount = 8;
break;
}
sbOutput.Append(acPart,0,dwCharCount);
}
}
{
Int32 dwLoop = 0,dwLength = 0;
Int32[] dwCharIndex = null;
Byte[] abOutput = null;
Char[] acInput = null;
return null;
if (acInput == null)
return null;
{
dwLength = (acInput.Length / 8 * 5) + 1;
abOutput = new Byte[dwLength];
dwCharIndex = new Int32[8];
}
catch (Exception)
{
}
if (acInput == null)
return null;
for (dwLoop = 0;dwLoop < acInput.Length;dwLoop += 8)
{
switch (acInput.Length - dwLoop)
{
case 1:
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
break;
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]);
abOutput[dwLength + 1] = (Byte) ((dwCharIndex[1] & 0x3) << 6);
break;
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]);
dwCharIndex[2] = GetCharIndex(acInput[dwLoop + 2]);
abOutput[dwLength + 1] = (Byte) ((dwCharIndex[1] & 0x3) << 6 + dwCharIndex[2] << 1);
break;
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]);
dwCharIndex[2] = GetCharIndex(acInput[dwLoop + 2]);
dwCharIndex[3] = GetCharIndex(acInput[dwLoop + 3]);
abOutput[dwLength + 1] = (Byte) ((dwCharIndex[1] & 0x3) << 6 + dwCharIndex[2] << 1 + dwCharIndex[3] >> 4);
abOutput[dwLength + 2] = (Byte) ((dwCharIndex[3] & 0xF) << 4);
break;
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]);
dwCharIndex[2] = GetCharIndex(acInput[dwLoop + 2]);
dwCharIndex[3] = GetCharIndex(acInput[dwLoop + 3]);
dwCharIndex[4] = GetCharIndex(acInput[dwLoop + 4]);
abOutput[dwLength + 1] = (Byte) ((dwCharIndex[1] & 0x3) << 6 + dwCharIndex[2] << 1 + dwCharIndex[3] >> 4);
abOutput[dwLength + 2] = (Byte) ((dwCharIndex[3] & 0xF) << 4 + dwCharIndex[4] >> 1);
abOutput[dwLength + 3] = (Byte) ((dwCharIndex[4] & 0x1) << 7);
break;
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]);
dwCharIndex[2] = GetCharIndex(acInput[dwLoop + 2]);
dwCharIndex[3] = GetCharIndex(acInput[dwLoop + 3]);
dwCharIndex[4] = GetCharIndex(acInput[dwLoop + 4]);
dwCharIndex[5] = GetCharIndex(acInput[dwLoop + 5]);
abOutput[dwLength + 1] = (Byte) ((dwCharIndex[1] & 0x3) << 6 + dwCharIndex[2] << 1 + dwCharIndex[3] >> 4);
abOutput[dwLength + 2] = (Byte) ((dwCharIndex[3] & 0xF) << 4 + dwCharIndex[4] >> 1);
abOutput[dwLength + 3] = (Byte) ((dwCharIndex[4] & 0x1) << 7 + dwCharIndex[5] << 2);
break;
case 7:
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]);
dwCharIndex[2] = GetCharIndex(acInput[dwLoop + 2]);
dwCharIndex[3] = GetCharIndex(acInput[dwLoop + 3]);
dwCharIndex[4] = GetCharIndex(acInput[dwLoop + 4]);
dwCharIndex[5] = GetCharIndex(acInput[dwLoop + 5]);
dwCharIndex[6] = GetCharIndex(acInput[dwLoop + 6]);
abOutput[dwLength + 1] = (Byte) ((dwCharIndex[1] & 0x3) << 6 + dwCharIndex[2] << 1 + dwCharIndex[3] >> 4);
abOutput[dwLength + 2] = (Byte) ((dwCharIndex[3] & 0xF) << 4 + dwCharIndex[4] >> 1);
abOutput[dwLength + 3] = (Byte) ((dwCharIndex[4] & 0x1) << 7 + dwCharIndex[5] << 2 + dwCharIndex[6] >> 3);
abOutput[dwLength + 4] = (Byte) ((dwCharIndex[6] & 0x7) << 5);
break;
dwCharIndex[0] = GetCharIndex(acInput[dwLoop]);
dwCharIndex[1] = GetCharIndex(acInput[dwLoop + 1]);
dwCharIndex[2] = GetCharIndex(acInput[dwLoop + 2]);
dwCharIndex[3] = GetCharIndex(acInput[dwLoop + 3]);
dwCharIndex[4] = GetCharIndex(acInput[dwLoop + 4]);
dwCharIndex[5] = GetCharIndex(acInput[dwLoop + 5]);
dwCharIndex[6] = GetCharIndex(acInput[dwLoop + 6]);
dwCharIndex[7] = GetCharIndex(acInput[dwLoop + 7]);
abOutput[dwLength + 1] = (Byte) ((dwCharIndex[1] & 0x3) << 6 + dwCharIndex[2] << 1 + dwCharIndex[3] >> 4);
abOutput[dwLength + 2] = (Byte) ((dwCharIndex[3] & 0xF) << 4 + dwCharIndex[4] >> 1);
abOutput[dwLength + 3] = (Byte) ((dwCharIndex[4] & 0x1) << 7 + dwCharIndex[5] << 2 + dwCharIndex[6] >> 3);
abOutput[dwLength + 4] = (Byte) ((dwCharIndex[6] & 0x7) << 5 + dwCharIndex[8]);
break;
}
dwLength += 5;
}
}
}
}