diff options
Diffstat (limited to 'docroot/classes/decoder.module.js')
-rwxr-xr-x | docroot/classes/decoder.module.js | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/docroot/classes/decoder.module.js b/docroot/classes/decoder.module.js new file mode 100755 index 0000000..ae33059 --- /dev/null +++ b/docroot/classes/decoder.module.js | |||
@@ -0,0 +1,205 @@ | |||
1 | /* | ||
2 | * Material Experience - Encoder/Decoder Module | ||
3 | * | ||
4 | * EYEMG - Interactive Media Group | ||
5 | * Updated by Mike Crute (mcrute@eyemg.com) on 9/26/07 | ||
6 | * | ||
7 | * Encoder/Decoder module for common encoding schemes like URL and | ||
8 | * base64. Code from: http://ostermiller.org/calc/encode.html | ||
9 | */ | ||
10 | var END_OF_INPUT = -1; | ||
11 | |||
12 | var base64Chars = new Array( | ||
13 | 'A','B','C','D','E','F','G','H', | ||
14 | 'I','J','K','L','M','N','O','P', | ||
15 | 'Q','R','S','T','U','V','W','X', | ||
16 | 'Y','Z','a','b','c','d','e','f', | ||
17 | 'g','h','i','j','k','l','m','n', | ||
18 | 'o','p','q','r','s','t','u','v', | ||
19 | 'w','x','y','z','0','1','2','3', | ||
20 | '4','5','6','7','8','9','+','/' | ||
21 | ); | ||
22 | |||
23 | var reverseBase64Chars = new Array(); | ||
24 | var base64Str; | ||
25 | var base64Count; | ||
26 | |||
27 | for (var i=0; i < base64Chars.length; i++) | ||
28 | { | ||
29 | reverseBase64Chars[base64Chars[i]] = i; | ||
30 | } | ||
31 | |||
32 | // -------------------------------------------------------------------------- \\ | ||
33 | |||
34 | function urlDecode(str) | ||
35 | { | ||
36 | return unescape(str.replace(new RegExp('\\+','g'),' ')); | ||
37 | } | ||
38 | |||
39 | function urlEncode(str) | ||
40 | { | ||
41 | str = escape(str); | ||
42 | str = str.replace(new RegExp('\\+','g'),'%2B'); | ||
43 | |||
44 | return str.replace(new RegExp('%20','g'),'+'); | ||
45 | } | ||
46 | |||
47 | // -------------------------------------------------------------------------- \\ | ||
48 | |||
49 | function setBase64Str(str) | ||
50 | { | ||
51 | base64Str = str; | ||
52 | base64Count = 0; | ||
53 | } | ||
54 | |||
55 | function readBase64() | ||
56 | { | ||
57 | if (!base64Str) | ||
58 | { | ||
59 | return END_OF_INPUT; | ||
60 | } | ||
61 | |||
62 | if (base64Count >= base64Str.length) | ||
63 | { | ||
64 | return END_OF_INPUT; | ||
65 | } | ||
66 | |||
67 | var c = base64Str.charCodeAt(base64Count) & 0xff; | ||
68 | |||
69 | base64Count++; | ||
70 | |||
71 | return c; | ||
72 | } | ||
73 | |||
74 | function encodeBase64(str) | ||
75 | { | ||
76 | setBase64Str(str); | ||
77 | |||
78 | var result = ''; | ||
79 | var inBuffer = new Array(3); | ||
80 | var lineCount = 0; | ||
81 | var done = false; | ||
82 | |||
83 | while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT) | ||
84 | { | ||
85 | inBuffer[1] = readBase64(); | ||
86 | inBuffer[2] = readBase64(); | ||
87 | result += (base64Chars[ inBuffer[0] >> 2 ]); | ||
88 | |||
89 | if (inBuffer[1] != END_OF_INPUT) | ||
90 | { | ||
91 | result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]); | ||
92 | |||
93 | if (inBuffer[2] != END_OF_INPUT) | ||
94 | { | ||
95 | result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]); | ||
96 | result += (base64Chars [inBuffer[2] & 0x3F]); | ||
97 | } | ||
98 | else | ||
99 | { | ||
100 | result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]); | ||
101 | result += ('='); | ||
102 | done = true; | ||
103 | } | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]); | ||
108 | result += ('='); | ||
109 | result += ('='); | ||
110 | done = true; | ||
111 | } | ||
112 | |||
113 | lineCount += 4; | ||
114 | |||
115 | if (lineCount >= 76) | ||
116 | { | ||
117 | result += ('\n'); | ||
118 | lineCount = 0; | ||
119 | } | ||
120 | } | ||
121 | |||
122 | return result; | ||
123 | } | ||
124 | |||
125 | function readReverseBase64() | ||
126 | { | ||
127 | if (!base64Str) | ||
128 | { | ||
129 | return END_OF_INPUT; | ||
130 | } | ||
131 | |||
132 | while (true) | ||
133 | { | ||
134 | if (base64Count >= base64Str.length) | ||
135 | { | ||
136 | return END_OF_INPUT; | ||
137 | } | ||
138 | |||
139 | var nextCharacter = base64Str.charAt(base64Count); | ||
140 | base64Count++; | ||
141 | |||
142 | if (reverseBase64Chars[nextCharacter]) | ||
143 | { | ||
144 | return reverseBase64Chars[nextCharacter]; | ||
145 | } | ||
146 | |||
147 | if (nextCharacter == 'A') | ||
148 | { | ||
149 | return 0; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | return END_OF_INPUT; | ||
154 | } | ||
155 | |||
156 | function ntos(n) | ||
157 | { | ||
158 | n = n.toString(16); | ||
159 | |||
160 | if (n.length == 1) | ||
161 | { | ||
162 | n = "0" + n; | ||
163 | } | ||
164 | |||
165 | n = "%" + n; | ||
166 | |||
167 | return unescape(n); | ||
168 | } | ||
169 | |||
170 | function decodeBase64(str) | ||
171 | { | ||
172 | setBase64Str(str); | ||
173 | |||
174 | var result = ""; | ||
175 | var inBuffer = new Array(4); | ||
176 | var done = false; | ||
177 | |||
178 | while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT | ||
179 | && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT) | ||
180 | { | ||
181 | inBuffer[2] = readReverseBase64(); | ||
182 | inBuffer[3] = readReverseBase64(); | ||
183 | result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4)); | ||
184 | |||
185 | if (inBuffer[2] != END_OF_INPUT) | ||
186 | { | ||
187 | result += ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2)); | ||
188 | |||
189 | if (inBuffer[3] != END_OF_INPUT) | ||
190 | { | ||
191 | result += ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3])); | ||
192 | } | ||
193 | else | ||
194 | { | ||
195 | done = true; | ||
196 | } | ||
197 | } | ||
198 | else | ||
199 | { | ||
200 | done = true; | ||
201 | } | ||
202 | } | ||
203 | |||
204 | return result; | ||
205 | } \ No newline at end of file | ||