btoa.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. This file is part of OmnomIRC. It is not created by OmnomIRC authors. It is believed that this is in agreement with
  3. all applicable licenses and restrictions.
  4. */
  5. /*
  6. * Text replace stuff
  7. *
  8. * found in omnimaga themes js
  9. */
  10. function replaceText(text,textarea)
  11. {if(typeof(textarea.caretPos)!="undefined"&&textarea.createTextRange)
  12. {var caretPos=textarea.caretPos;caretPos.text=caretPos.text.charAt(caretPos.text.length- 1)==' '?text+' ':text;caretPos.select();}
  13. else if(typeof(textarea.selectionStart)!="undefined")
  14. {var begin=textarea.value.substr(0,textarea.selectionStart);var end=textarea.value.substr(textarea.selectionEnd);var scrollPos=textarea.scrollTop;textarea.value=begin+ text+ end;if(textarea.setSelectionRange)
  15. {textarea.focus();textarea.setSelectionRange(begin.length+ text.length,begin.length+ text.length);}
  16. textarea.scrollTop=scrollPos;}
  17. else
  18. {textarea.value+=text;textarea.focus(textarea.value.length- 1);}}
  19. function surroundText(text1,text2,textarea)
  20. {if(typeof(textarea.caretPos)!="undefined"&&textarea.createTextRange)
  21. {var caretPos=textarea.caretPos,temp_length=caretPos.text.length;caretPos.text=caretPos.text.charAt(caretPos.text.length- 1)==' '?text1+ caretPos.text+ text2+' ':text1+ caretPos.text+ text2;if(temp_length==0)
  22. {caretPos.moveStart("character",-text2.length);caretPos.moveEnd("character",-text2.length);caretPos.select();}
  23. else
  24. textarea.focus(caretPos);}
  25. else if(typeof(textarea.selectionStart)!="undefined")
  26. {var begin=textarea.value.substr(0,textarea.selectionStart);var selection=textarea.value.substr(textarea.selectionStart,textarea.selectionEnd- textarea.selectionStart);var end=textarea.value.substr(textarea.selectionEnd);var newCursorPos=textarea.selectionStart;var scrollPos=textarea.scrollTop;textarea.value=begin+ text1+ selection+ text2+ end;if(textarea.setSelectionRange)
  27. {if(selection.length==0)
  28. textarea.setSelectionRange(newCursorPos+ text1.length,newCursorPos+ text1.length);else
  29. textarea.setSelectionRange(newCursorPos,newCursorPos+ text1.length+ selection.length+ text2.length);textarea.focus();}
  30. textarea.scrollTop=scrollPos;}
  31. else
  32. {textarea.value+=text1+ text2;textarea.focus(textarea.value.length- 1);}}
  33. /* Cookie functions
  34. * Copyright (c) by w3schools.com
  35. * yeah, i am a loser that i look there >.<
  36. *
  37. *
  38. */
  39. function setCookie(c_name,value,exdays) {
  40. var exdate=new Date();
  41. exdate.setDate(exdate.getDate() + exdays);
  42. var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  43. document.cookie=c_name + "=" + c_value;
  44. }
  45. function getCookie(c_name) {
  46. var i,x,y,ARRcookies=document.cookie.split(";");
  47. for (i=0;i<ARRcookies.length;i++) {
  48. x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  49. y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  50. x=x.replace(/^\s+|\s+$/g,"");
  51. if (x==c_name) {
  52. return unescape(y);
  53. }
  54. }
  55. }
  56. /* UTF8 encoding/decoding functions
  57. * Copyright (c) 2006 by Ali Farhadi.
  58. * released under the terms of the Gnu Public License.
  59. * see the GPL for details.
  60. *
  61. * Email: ali[at]farhadi[dot]ir
  62. * Website: http://farhadi.ir/
  63. */
  64. //an alias of String.fromCharCode
  65. function chr(code)
  66. {
  67. return String.fromCharCode(code);
  68. }
  69. //returns utf8 encoded charachter of a unicode value.
  70. //code must be a number indicating the Unicode value.
  71. //returned value is a string between 1 and 4 charachters.
  72. function code2utf(code)
  73. {
  74. if (code < 128) return chr(code);
  75. if (code < 2048) return chr(192+(code>>6)) + chr(128+(code&63));
  76. if (code < 65536) return chr(224+(code>>12)) + chr(128+((code>>6)&63)) + chr(128+(code&63));
  77. if (code < 2097152) return chr(240+(code>>18)) + chr(128+((code>>12)&63)) + chr(128+((code>>6)&63)) + chr(128+(code&63));
  78. }
  79. //it is a private function for internal use in utf8Encode function
  80. function _utf8Encode(str)
  81. {
  82. var utf8str = new Array();
  83. for (var i=0; i<str.length; i++) {
  84. utf8str[i] = code2utf(str.charCodeAt(i));
  85. }
  86. return utf8str.join('');
  87. }
  88. //Encodes a unicode string to UTF8 format.
  89. function utf8Encode(str)
  90. {
  91. var utf8str = new Array();
  92. var pos,j = 0;
  93. var tmpStr = '';
  94. while ((pos = str.search(/[^\x00-\x7F]/)) != -1) {
  95. tmpStr = str.match(/([^\x00-\x7F]+[\x00-\x7F]{0,10})+/)[0];
  96. utf8str[j++] = str.substr(0, pos);
  97. utf8str[j++] = _utf8Encode(tmpStr);
  98. str = str.substr(pos + tmpStr.length);
  99. }
  100. utf8str[j++] = str;
  101. return utf8str.join('');
  102. }
  103. //it is a private function for internal use in utf8Decode function
  104. function _utf8Decode(utf8str)
  105. {
  106. var str = new Array();
  107. var code,code2,code3,code4,j = 0;
  108. for (var i=0; i<utf8str.length; ) {
  109. code = utf8str.charCodeAt(i++);
  110. if (code > 127) code2 = utf8str.charCodeAt(i++);
  111. if (code > 223) code3 = utf8str.charCodeAt(i++);
  112. if (code > 239) code4 = utf8str.charCodeAt(i++);
  113. if (code < 128) str[j++]= chr(code);
  114. else if (code < 224) str[j++] = chr(((code-192)<<6) + (code2-128));
  115. else if (code < 240) str[j++] = chr(((code-224)<<12) + ((code2-128)<<6) + (code3-128));
  116. else str[j++] = chr(((code-240)<<18) + ((code2-128)<<12) + ((code3-128)<<6) + (code4-128));
  117. }
  118. return str.join('');
  119. }
  120. //Decodes a UTF8 formated string
  121. function utf8Decode(utf8str)
  122. {
  123. var str = new Array();
  124. var pos = 0;
  125. var tmpStr = '';
  126. var j=0;
  127. while ((pos = utf8str.search(/[^\x00-\x7F]/)) != -1) {
  128. tmpStr = utf8str.match(/([^\x00-\x7F]+[\x00-\x7F]{0,10})+/)[0];
  129. str[j++]= utf8str.substr(0, pos) + _utf8Decode(tmpStr);
  130. utf8str = utf8str.substr(pos + tmpStr.length);
  131. }
  132. str[j++] = utf8str;
  133. return str.join('');
  134. }
  135. /*
  136. * Copyright (c) 2010 Nick Galbreath
  137. * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
  138. *
  139. * Permission is hereby granted, free of charge, to any person
  140. * obtaining a copy of this software and associated documentation
  141. * files (the "Software"), to deal in the Software without
  142. * restriction, including without limitation the rights to use,
  143. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  144. * copies of the Software, and to permit persons to whom the
  145. * Software is furnished to do so, subject to the following
  146. * conditions:
  147. *
  148. * The above copyright notice and this permission notice shall be
  149. * included in all copies or substantial portions of the Software.
  150. *
  151. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  152. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  153. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  154. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  155. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  156. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  157. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  158. * OTHER DEALINGS IN THE SOFTWARE.
  159. */
  160. /* base64 encode/decode compatible with window.btoa/atob
  161. *
  162. * window.atob/btoa is a Firefox extension to convert binary data (the "b")
  163. * to base64 (ascii, the "a").
  164. *
  165. * It is also found in Safari and Chrome. It is not available in IE.
  166. *
  167. * if (!window.btoa) window.btoa = base64.encode
  168. * if (!window.atob) window.atob = base64.decode
  169. *
  170. * The original spec's for atob/btoa are a bit lacking
  171. * https://developer.mozilla.org/en/DOM/window.atob
  172. * https://developer.mozilla.org/en/DOM/window.btoa
  173. *
  174. * window.btoa and base64.encode takes a string where charCodeAt is [0,255]
  175. * If any character is not [0,255], then an DOMException(5) is thrown.
  176. *
  177. * window.atob and base64.decode take a base64-encoded string
  178. * If the input length is not a multiple of 4, or contains invalid characters
  179. * then an DOMException(5) is thrown.
  180. */
  181. var base64 = {};
  182. base64.PADCHAR = ',';
  183. base64.ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
  184. base64.makeDOMException = function() {
  185. // sadly in FF,Safari,Chrome you can't make a DOMException
  186. var e, tmp;
  187. try {
  188. return new DOMException(DOMException.INVALID_CHARACTER_ERR);
  189. } catch (tmp) {
  190. // not available, just passback a duck-typed equiv
  191. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error
  192. // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/prototype
  193. var ex = new Error("DOM Exception 5");
  194. // ex.number and ex.description is IE-specific.
  195. ex.code = ex.number = 5;
  196. ex.name = ex.description = "INVALID_CHARACTER_ERR";
  197. // Safari/Chrome output format
  198. ex.toString = function() { return 'Error: ' + ex.name + ': ' + ex.message; };
  199. return ex;
  200. }
  201. }
  202. base64.getbyte64 = function(s,i) {
  203. // This is oddly fast, except on Chrome/V8.
  204. // Minimal or no improvement in performance by using a
  205. // object with properties mapping chars to value (eg. 'A': 0)
  206. var idx = base64.ALPHA.indexOf(s.charAt(i));
  207. if (idx === -1) {
  208. //throw base64.makeDOMException();
  209. }
  210. return idx;
  211. }
  212. base64.decode = function(s) {
  213. // convert to string
  214. s = '' + s;
  215. s = s.replace("+","-");
  216. s = s.replace("/","_");
  217. s = s.replace("=",",");
  218. var getbyte64 = base64.getbyte64;
  219. var pads, i, b10;
  220. var imax = s.length
  221. if (imax === 0) {
  222. return s;
  223. }
  224. if (imax % 4 !== 0) {
  225. //throw base64.makeDOMException();
  226. }
  227. pads = 0
  228. if (s.charAt(imax - 1) === base64.PADCHAR) {
  229. pads = 1;
  230. if (s.charAt(imax - 2) === base64.PADCHAR) {
  231. pads = 2;
  232. }
  233. // either way, we want to ignore this last block
  234. imax -= 4;
  235. }
  236. var x = [];
  237. for (i = 0; i < imax; i += 4) {
  238. b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) |
  239. (getbyte64(s,i+2) << 6) | getbyte64(s,i+3);
  240. x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff, b10 & 0xff));
  241. }
  242. switch (pads) {
  243. case 1:
  244. b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | (getbyte64(s,i+2) << 6);
  245. x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff));
  246. break;
  247. case 2:
  248. b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12);
  249. x.push(String.fromCharCode(b10 >> 16));
  250. break;
  251. }
  252. return s=utf8Decode(x.join(''));
  253. }
  254. base64.getbyte = function(s,i) {
  255. var x = s.charCodeAt(i);
  256. if (x > 255) {
  257. //throw base64.makeDOMException();
  258. }
  259. return x;
  260. }
  261. base64.encode = function(s) {
  262. s=unescape(encodeURIComponent(s));
  263. if (arguments.length !== 1) {
  264. throw new SyntaxError("Not enough arguments");
  265. }
  266. var padchar = base64.PADCHAR;
  267. var alpha = base64.ALPHA;
  268. var getbyte = base64.getbyte;
  269. var i, b10;
  270. var x = [];
  271. // convert to string
  272. s = '' + s;
  273. var imax = s.length - s.length % 3;
  274. if (s.length === 0) {
  275. return s;
  276. }
  277. for (i = 0; i < imax; i += 3) {
  278. b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8) | getbyte(s,i+2);
  279. x.push(alpha.charAt(b10 >> 18));
  280. x.push(alpha.charAt((b10 >> 12) & 0x3F));
  281. x.push(alpha.charAt((b10 >> 6) & 0x3f));
  282. x.push(alpha.charAt(b10 & 0x3f));
  283. }
  284. switch (s.length - imax) {
  285. case 1:
  286. b10 = getbyte(s,i) << 16;
  287. x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
  288. padchar + padchar);
  289. break;
  290. case 2:
  291. b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8);
  292. x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
  293. alpha.charAt((b10 >> 6) & 0x3f) + padchar);
  294. break;
  295. }
  296. s = x.join('');
  297. s = s.replace("+","-");
  298. s = s.replace("/","_");
  299. s = s.replace("=",",");
  300. return s;
  301. }