blob: b51bf1ed852da209678aabd95e4524de63b54379 [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
Meki Cherkaoui8f173612012-06-06 01:05:40 -070018DataUtils.keyStr = "ABCDEFGHIJKLMNOP" +
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070019 "QRSTUVWXYZabcdef" +
20 "ghijklmnopqrstuv" +
21 "wxyz0123456789+/" +
22 "=";
23
Meki Cherkaoui8f173612012-06-06 01:05:40 -070024
25/**
26 * Raw String to Base 64
27 */
Meki Cherkaouib21911b2012-05-18 16:54:37 -070028DataUtils.stringtoBase64=function stringtoBase64(input) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070029 input = escape(input);
30 var output = "";
31 var chr1, chr2, chr3 = "";
32 var enc1, enc2, enc3, enc4 = "";
33 var i = 0;
34
35 do {
36 chr1 = input.charCodeAt(i++);
37 chr2 = input.charCodeAt(i++);
38 chr3 = input.charCodeAt(i++);
39
40 enc1 = chr1 >> 2;
41 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
42 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
43 enc4 = chr3 & 63;
44
45 if (isNaN(chr2)) {
46 enc3 = enc4 = 64;
47 } else if (isNaN(chr3)) {
48 enc4 = 64;
49 }
50
51 output = output +
Meki Cherkaoui8f173612012-06-06 01:05:40 -070052 DataUtils.keyStr.charAt(enc1) +
53 DataUtils.keyStr.charAt(enc2) +
54 DataUtils.keyStr.charAt(enc3) +
55 DataUtils.keyStr.charAt(enc4);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070056 chr1 = chr2 = chr3 = "";
57 enc1 = enc2 = enc3 = enc4 = "";
58 } while (i < input.length);
59
60 return output;
61 }
62
Meki Cherkaoui8f173612012-06-06 01:05:40 -070063/**
64 * Base 64 to Raw String
65 */
Meki Cherkaouib21911b2012-05-18 16:54:37 -070066DataUtils.base64toString = function base64toString(input) {
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070067 var output = "";
68 var chr1, chr2, chr3 = "";
69 var enc1, enc2, enc3, enc4 = "";
70 var i = 0;
71
72 // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
73 var base64test = /[^A-Za-z0-9\+\/\=]/g;
74 if (base64test.exec(input)) {
75 alert("There were invalid base64 characters in the input text.\n" +
76 "Valid base64 characters are A-Z, a-z, 0-9, '+', '/',and '='\n" +
77 "Expect errors in decoding.");
78 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -070079
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070080 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
81
82 do {
Meki Cherkaoui8f173612012-06-06 01:05:40 -070083 enc1 = DataUtils.keyStr.indexOf(input.charAt(i++));
84 enc2 = DataUtils.keyStr.indexOf(input.charAt(i++));
85 enc3 = DataUtils.keyStr.indexOf(input.charAt(i++));
86 enc4 = DataUtils.keyStr.indexOf(input.charAt(i++));
Meki Cherkaouif441d3a2012-04-22 15:17:52 -070087
88 chr1 = (enc1 << 2) | (enc2 >> 4);
89 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
90 chr3 = ((enc3 & 3) << 6) | enc4;
91
92 output = output + String.fromCharCode(chr1);
93
94 if (enc3 != 64) {
95 output = output + String.fromCharCode(chr2);
96 }
97 if (enc4 != 64) {
98 output = output + String.fromCharCode(chr3);
99 }
100
101 chr1 = chr2 = chr3 = "";
102 enc1 = enc2 = enc3 = enc4 = "";
103
104 } while (i < input.length);
105
106 return unescape(output);
107 };
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700108
109//byte []
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700110
111/**
112 * NOT WORKING!!!!!
113 *
114 * Unsiged Long Number to Byte Array
115 */
116
117 /*
118DataUtils.unsignedLongToByteArray= function( value) {
119
120 if(LOG>4)console.log('INPUT IS '+value);
121
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700122 if( 0 == value )
123 return [0];
124
125 if( 0 <= value && value <= 0x00FF ) {
126 //byte []
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700127 var bb = new Array(1);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700128 bb[0] = (value & 0x00FF);
129 return bb;
130 }
131
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700132 if(LOG>4) console.log('type of value is '+typeof value);
133 if(LOG>4) console.log('value is '+value);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700134 //byte []
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700135 var out = null;
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700136 //int
137 var offset = -1;
138 for(var i = 7; i >=0; --i) {
139 //byte
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700140 console.log(i);
141 console.log('value is '+value);
142 console.log('(value >> (i * 8)) '+ (value >> (i * 8)) );
143 console.log(' ((value >> (i * 8)) & 0xFF) '+ ((value >> (i * 8)) & 0xFF) );
144
145 var b = ((value >> (i * 8)) & 0xFF) ;
146
147 if(LOG>4) console.log('b is '+b);
148
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700149 if( out == null && b != 0 ) {
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700150 //out = new byte[i+1];
151 out = new Array(i+1);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700152 offset = i;
153 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700154
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700155 if( out != null )
156 out[ offset - i ] = b;
157 }
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700158 if(LOG>4)console.log('OUTPUT IS ');
159 if(LOG>4)console.log(out);
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700160 return out;
161}
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700162*/
163
164/**
165 * NOT WORKING!!!!!
166 *
167 * Unsiged Long Number to Byte Array
168 *//*
169DataUtils.byteArrayToUnsignedLong = function(//final byte []
170 src) {
171 if(LOG>4) console.log('INPUT IS ');
172 if(LOG>4) console.log(src);
173
174 var value = 0;
175 for(var i = 0; i < src.length; i++) {
176 value = value << 8;
177 // Java will assume the byte is signed, so extend it and trim it.
178
179
180 var b = ((src[i]) & 0xFF );
181 value |= b;
182 }
183
184 if(LOG>4) console.log('OUTPUT IS ');
185
186 if(LOG>4) console.log(value);
187
188 return value;
189 }*/
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700190
191
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700192/**
193 * Hex String to Byte Array
194 */
195 //THIS IS NOT WORKING
196/*
197DataUtils.HexStringtoByteArray = function(str) {
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700198 var byteArray = [];
199 for (var i = 0; i < str.length; i++)
200 if (str.charCodeAt(i) <= 0x7F)
201 byteArray.push(str.charCodeAt(i));
202 else {
203 var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
204 for (var j = 0; j < h.length; j++)
205 byteArray.push(parseInt(h[j], 16));
206 }
207 return byteArray;
208};
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700209*/
210
211/**
212 * Byte Array to Hex String
213 */
214DataUtils.byteArrayToHexString = function(byteArray) {
Meki Cherkaouiabb973b2012-05-09 14:25:57 -0700215 var str = '';
216 for (var i = 0; i < byteArray.length; i++)
217 str += byteArray[i] <= 0x7F?
218 byteArray[i] === 0x25 ? "%25" : // %
219 String.fromCharCode(byteArray[i]) :
220 "%" + byteArray[i].toString(16).toUpperCase();
221 return decodeURIComponent(str);
222};
Meki Cherkaouif441d3a2012-04-22 15:17:52 -0700223
224
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700225/**
226 * Byte array to Hex String
227 */
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700228//http://ejohn.org/blog/numbers-hex-and-colors/
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700229DataUtils.toHex = function(arguments){
230 if(LOG>4) console.log('ABOUT TO CONVERT '+ arguments);
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700231 //console.log(arguments);
232 var ret = "";
233 for ( var i = 0; i < arguments.length; i++ )
234 ret += (arguments[i] < 16 ? "0" : "") + arguments[i].toString(16);
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700235 return ret; //.toUpperCase();
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700236}
237
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700238/**
239 * Byte array to raw string
240 */
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700241//DOES NOT SEEM TO WORK
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700242DataUtils.toString = function(arguments){
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700243 //console.log(arguments);
244 var ret = "";
245 for ( var i = 0; i < arguments.length; i++ )
246 ret += String.fromCharCode(arguments[i]);
247 return ret;
248}
249
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700250/**
251 * Hex String to byte array
252 */
253DataUtils.toNumbers=function( str ){
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700254 if(typeof str =='string'){
255 var ret = [];
256 str.replace(/(..)/g, function(str){
257 ret.push( parseInt( str, 16 ) );
258 });
259 return ret;
260 }
261}
262
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700263/**
264 * Raw String to Byte Array
265 */
266DataUtils.toNumbersFromString = function( str ){
Meki Cherkaouib21911b2012-05-18 16:54:37 -0700267 var bytes = new Array(str.length);
268 for(var i=0;i<str.length;i++)
269 bytes[i] = str.charCodeAt(i);
270 return bytes;
Meki Cherkaoui8f173612012-06-06 01:05:40 -0700271}
272
273DataUtils.encodeUtf8 = function (string) {
274 string = string.replace(/\r\n/g,"\n");
275 var utftext = "";
276
277 for (var n = 0; n < string.length; n++) {
278
279 var c = string.charCodeAt(n);
280
281 if (c < 128) {
282 utftext += String.fromCharCode(c);
283 }
284 else if((c > 127) && (c < 2048)) {
285 utftext += String.fromCharCode((c >> 6) | 192);
286 utftext += String.fromCharCode((c & 63) | 128);
287 }
288 else {
289 utftext += String.fromCharCode((c >> 12) | 224);
290 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
291 utftext += String.fromCharCode((c & 63) | 128);
292 }
293
294 }
295
296 return utftext;
297 };
298
299 // public method for url decoding
300DataUtils.decodeUtf8 = function (utftext) {
301 var string = "";
302 var i = 0;
303 var c = c1 = c2 = 0;
304
305 while ( i < utftext.length ) {
306
307 c = utftext.charCodeAt(i);
308
309 if (c < 128) {
310 string += String.fromCharCode(c);
311 i++;
312 }
313 else if((c > 191) && (c < 224)) {
314 c2 = utftext.charCodeAt(i+1);
315 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
316 i += 2;
317 }
318 else {
319 c2 = utftext.charCodeAt(i+1);
320 c3 = utftext.charCodeAt(i+2);
321 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
322 i += 3;
323 }
324
325 }
326
327 return string;
328 };
329
330 test = function(){
331 console.log(DataUtils.decodeUtf8("HELLO.~"));
332 return DataUtils.decodeUtf8("HELLO.~");
333 }
334
335//NOT WORKING
336/*
337DataUtils.getUTF8StringFromBytes = function(bytes) {
338
339 bytes = toString(bytes);
340
341 var ix = 0;
342
343 if( bytes.slice(0,3) == "\xEF\xBB\xBF") {
344 ix = 3;
345 }
346
347 var string = "";
348 for( ; ix < bytes.length; ix++ ) {
349 var byte1 = bytes[ix].charCodeAt(0);
350 if( byte1 < 0x80 ) {
351 string += String.fromCharCode(byte1);
352 } else if( byte1 >= 0xC2 && byte1 < 0xE0 ) {
353 var byte2 = bytes[++ix].charCodeAt(0);
354 string += String.fromCharCode(((byte1&0x1F)<<6) + (byte2&0x3F));
355 } else if( byte1 >= 0xE0 && byte1 < 0xF0 ) {
356 var byte2 = bytes[++ix].charCodeAt(0);
357 var byte3 = bytes[++ix].charCodeAt(0);
358 string += String.fromCharCode(((byte1&0xFF)<<12) + ((byte2&0x3F)<<6) + (byte3&0x3F));
359 } else if( byte1 >= 0xF0 && byte1 < 0xF5) {
360 var byte2 = bytes[++ix].charCodeAt(0);
361 var byte3 = bytes[++ix].charCodeAt(0);
362 var byte4 = bytes[++ix].charCodeAt(0);
363 var codepoint = ((byte1&0x07)<<18) + ((byte2&0x3F)<<12)+ ((byte3&0x3F)<<6) + (byte4&0x3F);
364 codepoint -= 0x10000;
365 string += String.fromCharCode(
366 (codepoint>>10) + 0xD800,
367 (codepoint&0x3FF) + 0xDC00
368 );
369 }
370 }
371
372 return string;
373}*/
374
Jeff Thompsonbb976092012-09-29 17:01:38 -0700375/**
376 * Return true if a1 and a2 are the same length with equal elements.
377 */
378DataUtils.arraysEqual = function(a1, a2){
379 if (a1.length != a2.length)
380 return false;
381
382 for (var i = 0; i < a1.length; ++i) {
383 if (a1[i] != a2[i])
384 return false;
385 }
386
387 return true;
388}