blob: 538669833c5c561213c2da38950b6f489acd3196 [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
Meki Cherkaouib21911b2012-05-18 16:54:37 -070024DataUtils.stringtoBase64=function stringtoBase64(input) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070025 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
Meki Cherkaouib21911b2012-05-18 16:54:37 -070059DataUtils.base64toString = function base64toString(input) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070060 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 };
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700100
101//byte []
102DataUtils.prototype.unsignedLongToByteArray= function( value) {
103 if( 0 == value )
104 return [0];
105
106 if( 0 <= value && value <= 0x00FF ) {
107 //byte []
108 bb = new Array[1];
109 bb[0] = (value & 0x00FF);
110 return bb;
111 }
112
113
114 //byte []
115 out = null;
116 //int
117 var offset = -1;
118 for(var i = 7; i >=0; --i) {
119 //byte
120 b = ((value >> (i * 8)) & 0xFF);
121 if( out == null && b != 0 ) {
122 out = new byte[i+1];
123 offset = i;
124 }
125 if( out != null )
126 out[ offset - i ] = b;
127 }
128 return out;
129}
130
131
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700132var utf8 = {}
133
134utf8.toByteArray = function(str) {
135 var byteArray = [];
136 for (var i = 0; i < str.length; i++)
137 if (str.charCodeAt(i) <= 0x7F)
138 byteArray.push(str.charCodeAt(i));
139 else {
140 var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
141 for (var j = 0; j < h.length; j++)
142 byteArray.push(parseInt(h[j], 16));
143 }
144 return byteArray;
145};
146
147utf8.parse = function(byteArray) {
148 var str = '';
149 for (var i = 0; i < byteArray.length; i++)
150 str += byteArray[i] <= 0x7F?
151 byteArray[i] === 0x25 ? "%25" : // %
152 String.fromCharCode(byteArray[i]) :
153 "%" + byteArray[i].toString(16).toUpperCase();
154 return decodeURIComponent(str);
155};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700156
157
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700158//http://ejohn.org/blog/numbers-hex-and-colors/
159function toHex(arguments){
160 //console.log(arguments);
161 var ret = "";
162 for ( var i = 0; i < arguments.length; i++ )
163 ret += (arguments[i] < 16 ? "0" : "") + arguments[i].toString(16);
164 return ret.toUpperCase();
165}
166
167//DOES NOT SEEM TO WORK
168function toString(arguments){
169 //console.log(arguments);
170 var ret = "";
171 for ( var i = 0; i < arguments.length; i++ )
172 ret += String.fromCharCode(arguments[i]);
173 return ret;
174}
175
176function toNumbers( str ){
177 if(typeof str =='string'){
178 var ret = [];
179 str.replace(/(..)/g, function(str){
180 ret.push( parseInt( str, 16 ) );
181 });
182 return ret;
183 }
184}
185
186function toNumbersFromString( str ){
187 var bytes = new Array(str.length);
188 for(var i=0;i<str.length;i++)
189 bytes[i] = str.charCodeAt(i);
190 return bytes;
191}