blob: 92a4257af4601e2761bdeaf4130ecad8bcb062a0 [file] [log] [blame]
Meki Cherkaouif441d3a2012-04-22 15:17:52 -07001
2/*
3 * This class contains utilities to help parse the data
4 * author: ucla-cs
5 */
6
7var DataUtils = function DataUtils(){
8
9
10};
11
12
13/*
14 * NOTE THIS IS CURRENTLY NOT BEHING USED
15 *
16 */
17
18 var keyStr = "ABCDEFGHIJKLMNOP" +
19 "QRSTUVWXYZabcdef" +
20 "ghijklmnopqrstuv" +
21 "wxyz0123456789+/" +
22 "=";
23
24DataUtils.encode64=function encode64(input) {
25 input = escape(input);
26 var output = "";
27 var chr1, chr2, chr3 = "";
28 var enc1, enc2, enc3, enc4 = "";
29 var i = 0;
30
31 do {
32 chr1 = input.charCodeAt(i++);
33 chr2 = input.charCodeAt(i++);
34 chr3 = input.charCodeAt(i++);
35
36 enc1 = chr1 >> 2;
37 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
38 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
39 enc4 = chr3 & 63;
40
41 if (isNaN(chr2)) {
42 enc3 = enc4 = 64;
43 } else if (isNaN(chr3)) {
44 enc4 = 64;
45 }
46
47 output = output +
48 keyStr.charAt(enc1) +
49 keyStr.charAt(enc2) +
50 keyStr.charAt(enc3) +
51 keyStr.charAt(enc4);
52 chr1 = chr2 = chr3 = "";
53 enc1 = enc2 = enc3 = enc4 = "";
54 } while (i < input.length);
55
56 return output;
57 }
58
59DataUtils.decode64 = function decode64(input) {
60 var output = "";
61 var chr1, chr2, chr3 = "";
62 var enc1, enc2, enc3, enc4 = "";
63 var i = 0;
64
65 // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
66 var base64test = /[^A-Za-z0-9\+\/\=]/g;
67 if (base64test.exec(input)) {
68 alert("There were invalid base64 characters in the input text.\n" +
69 "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
70 "Expect errors in decoding.");
71 }
72 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
73
74 do {
75 enc1 = keyStr.indexOf(input.charAt(i++));
76 enc2 = keyStr.indexOf(input.charAt(i++));
77 enc3 = keyStr.indexOf(input.charAt(i++));
78 enc4 = keyStr.indexOf(input.charAt(i++));
79
80 chr1 = (enc1 << 2) | (enc2 >> 4);
81 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
82 chr3 = ((enc3 & 3) << 6) | enc4;
83
84 output = output + String.fromCharCode(chr1);
85
86 if (enc3 != 64) {
87 output = output + String.fromCharCode(chr2);
88 }
89 if (enc4 != 64) {
90 output = output + String.fromCharCode(chr3);
91 }
92
93 chr1 = chr2 = chr3 = "";
94 enc1 = enc2 = enc3 = enc4 = "";
95
96 } while (i < input.length);
97
98 return unescape(output);
99 };
100
101
102
103
104
105//byte []
106DataUtils.prototype.unsignedLongToByteArray= function( value) {
107 if( 0 == value )
108 return [0];
109
110 if( 0 <= value && value <= 0x00FF ) {
111 //byte []
112 bb = new Array[1];
113 bb[0] = (value & 0x00FF);
114 return bb;
115 }
116
117
118 //byte []
119 out = null;
120 //int
121 var offset = -1;
122 for(var i = 7; i >=0; --i) {
123 //byte
124 b = ((value >> (i * 8)) & 0xFF);
125 if( out == null && b != 0 ) {
126 out = new byte[i+1];
127 offset = i;
128 }
129 if( out != null )
130 out[ offset - i ] = b;
131 }
132 return out;
133}
134
135
136
137