aboutsummaryrefslogtreecommitdiffstats
path: root/tests/nicrealtek.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/nicrealtek.c')
0 files changed, 0 insertions, 0 deletions
n2' href='#n2'>2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
package com.trilead.ssh2.crypto;

import java.io.CharArrayWriter;
import java.io.IOException;

/**
 * Basic Base64 Support.
 * 
 * @author Christian Plattner, plattner@trilead.com
 * @version $Id: Base64.java,v 1.1 2007/10/15 12:49:56 cplattne Exp $
 */
public class Base64
{
	static final char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
	
	public static char[] encode(byte[] content)
	{
		CharArrayWriter cw = new CharArrayWriter((4 * content.length) / 3);

		int idx = 0;
					
		int x = 0;

		for (int i = 0; i < content.length; i++)
		{
			if (idx == 0)
				x = (content[i] & 0xff) << 16;
			else if (idx == 1)
				x = x | ((content[i] & 0xff) << 8);
			else
				x = x | (content[i] & 0xff);

			idx++;

			if (idx == 3)
			{
				cw.write(alphabet[x >> 18]);
				cw.write(alphabet[(x >> 12) & 0x3f]);
				cw.write(alphabet[(x >> 6) & 0x3f]);
				cw.write(alphabet[x & 0x3f]);

				idx = 0;
			}
		}

		if (idx == 1)
		{
			cw.write(alphabet[x >> 18]);
			cw.write(alphabet[(x >> 12) & 0x3f]);
			cw.write('=');
			cw.write('=');
		}

		if (idx == 2)
		{
			cw.write(alphabet[x >> 18]);
			cw.write(alphabet[(x >> 12) & 0x3f]);
			cw.write(alphabet[(x >> 6) & 0x3f]);
			cw.write('=');
		}

		return cw.toCharArray();
	}

	public static byte[] decode(char[] message) throws IOException
	{
		byte buff[] = new byte[4];
		byte dest[] = new byte[message.length];

		int bpos = 0;
		int destpos = 0;

		for (int i = 0; i < message.length; i++)
		{
			int c = message[i];

			if ((c == '\n') || (c == '\r') || (c == ' ') || (c == '\t'))
				continue;

			if ((c >= 'A') && (c <= 'Z'))
			{
				buff[bpos++] = (byte) (c - 'A');
			}
			else if ((c >= 'a') && (c <= 'z'))
			{
				buff[bpos++] = (byte) ((c - 'a') + 26);
			}
			else if ((c >= '0') && (c <= '9'))
			{
				buff[bpos++] = (byte) ((c - '0') + 52);
			}
			else if (c == '+')
			{
				buff[bpos++] = 62;
			}
			else if (c == '/')
			{
				buff[bpos++] = 63;
			}
			else if (c == '=')
			{
				buff[bpos++] = 64;
			}
			else
			{
				throw new IOException("Illegal char in base64 code.");
			}

			if (bpos == 4)
			{
				bpos = 0;

				if (buff[0] == 64)
					break;

				if (buff[1] == 64)
					throw new IOException("Unexpected '=' in base64 code.");

				if (buff[2] == 64)
				{
					int v = (((buff[0] & 0x3f) << 6) | ((buff[1] & 0x3f)));
					dest[destpos++] = (byte) (v >> 4);
					break;
				}
				else if (buff[3] == 64)
				{
					int v = (((buff[0] & 0x3f) << 12) | ((buff[1] & 0x3f) << 6) | ((buff[2] & 0x3f)));
					dest[destpos++] = (byte) (v >> 10);
					dest[destpos++] = (byte) (v >> 2);
					break;
				}
				else
				{
					int v = (((buff[0] & 0x3f) << 18) | ((buff[1] & 0x3f) << 12) | ((buff[2] & 0x3f) << 6) | ((buff[3] & 0x3f)));
					dest[destpos++] = (byte) (v >> 16);
					dest[destpos++] = (byte) (v >> 8);
					dest[destpos++] = (byte) (v);
				}
			}
		}

		byte[] res = new byte[destpos];
		System.arraycopy(dest, 0, res, 0, destpos);

		return res;
	}
}