summaryrefslogtreecommitdiff
path: root/docroot/classes/decoder.module.js
blob: ae33059e635200234ebede1719ac85b42b7c1d7b (plain)
1
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Material Experience - Encoder/Decoder Module
 * 
 * EYEMG - Interactive Media Group
 * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07
 * 
 * Encoder/Decoder module for common encoding schemes like URL and 
 * base64. Code from: http://ostermiller.org/calc/encode.html
 */
var END_OF_INPUT = -1;

var base64Chars = new Array(
	'A','B','C','D','E','F','G','H',
	'I','J','K','L','M','N','O','P',
	'Q','R','S','T','U','V','W','X',
	'Y','Z','a','b','c','d','e','f',
	'g','h','i','j','k','l','m','n',
	'o','p','q','r','s','t','u','v',
	'w','x','y','z','0','1','2','3',
	'4','5','6','7','8','9','+','/'
);

var reverseBase64Chars = new Array();
var base64Str;
var base64Count;

for (var i=0; i < base64Chars.length; i++)
{
	reverseBase64Chars[base64Chars[i]] = i;
}

// -------------------------------------------------------------------------- \\

function urlDecode(str)
{
	return unescape(str.replace(new RegExp('\\+','g'),' '));
}

function urlEncode(str)
{
	str = escape(str);
	str = str.replace(new RegExp('\\+','g'),'%2B');
	
	return str.replace(new RegExp('%20','g'),'+');
}

// -------------------------------------------------------------------------- \\

function setBase64Str(str)
{
	base64Str   = str;
	base64Count = 0;
}

function readBase64()
{
	if (!base64Str) 
	{
		return END_OF_INPUT;
	}
	
	if (base64Count >= base64Str.length)
	{
		return END_OF_INPUT;
	}
	
	var c = base64Str.charCodeAt(base64Count) & 0xff;
	
	base64Count++;
	
	return c;
}

function encodeBase64(str)
{
	setBase64Str(str);
	
	var result    = '';
	var inBuffer  = new Array(3);
	var lineCount = 0;
	var done      = false;
    
	while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT)
	{
		inBuffer[1]  = readBase64();
		inBuffer[2]  = readBase64();
		result      += (base64Chars[ inBuffer[0] >> 2 ]);
		
		if (inBuffer[1] != END_OF_INPUT)
		{
			result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
	
			if (inBuffer[2] != END_OF_INPUT)
			{
				result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
				result += (base64Chars [inBuffer[2] & 0x3F]);
			}
			else 
			{
				result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
				result += ('=');
				done    = true;
			}
		}
		else 
		{
			result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
			result += ('=');
			result += ('=');
			done    = true;
		}
		
		lineCount += 4;
		
		if (lineCount >= 76)
		{
			result   += ('\n');
			lineCount = 0;
		}
	}

	return result;
}

function readReverseBase64()
{
	if (!base64Str)
	{
		return END_OF_INPUT;
	}
	
	while (true)
	{
		if (base64Count >= base64Str.length)
		{
			return END_OF_INPUT;
		}
		
		var nextCharacter = base64Str.charAt(base64Count);
		base64Count++;
		
		if (reverseBase64Chars[nextCharacter])
		{
			return reverseBase64Chars[nextCharacter];
		}
		
		if (nextCharacter == 'A') 
		{
			return 0;
		}
	}
	
	return END_OF_INPUT;
}

function ntos(n)
{
	n = n.toString(16);
	
	if (n.length == 1) 
	{
		n = "0" + n;
	}
	
	n = "%" + n;
	
	return unescape(n);
}

function decodeBase64(str)
{
	setBase64Str(str);
	
	var result   = "";
	var inBuffer = new Array(4);
	var done     = false;

	while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
		&& (inBuffer[1] = readReverseBase64()) != END_OF_INPUT)
        {
		inBuffer[2]  = readReverseBase64();
		inBuffer[3]  = readReverseBase64();
		result      += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
		
		if (inBuffer[2] != END_OF_INPUT)
		{
			result +=  ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));

			if (inBuffer[3] != END_OF_INPUT)
			{
				result +=  ntos((((inBuffer[2] << 6)  & 0xff) | inBuffer[3]));
			}
			else 
			{
				done = true;
			}
		} 
		else 
		{
			done = true;
		}
	}
	
	return result;
}