1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
10 ////////////////////////////////////////////////////////////////////////////////
14 global.getLocale = function () {
15 if (loc === null) loc = global.Cc["@mozilla.org/chrome/chrome-registry;1"].getService(global.Ci.nsIXULChromeRegistry).getSelectedLocale("global");
21 ////////////////////////////////////////////////////////////////////////////////
22 function fileReadText (file, charset) {
23 if (typeof(charset) !== "string") charset = "UTF-8";
24 charset = charset||"UTF-8";
25 let inputStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
26 inputStream.init(file, -1, -1, null);
27 let scInputStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream);
28 scInputStream.init(inputStream);
29 let output = scInputStream.read(-1);
30 scInputStream.close();
32 let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
33 converter.charset = charset;
34 let res = converter.ConvertToUnicode(output);
35 if (typeof(res) != "string") throw new Error("invalid file '"+file.path+"'");
37 if (charset == "UTF-8" && res.length >= 3 && res.substr(0, 3) == "\u00EF\u00BB\u00BF") res = res.substr(3);
42 ////////////////////////////////////////////////////////////////////////////////
43 function fileReadBinary (fl) {
44 let istream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
45 istream.init(fl, -1, -1, false);
46 let bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
47 bstream.setInputStream(istream);
48 let bytes = bstream.readBytes(bstream.available());
54 ////////////////////////////////////////////////////////////////////////////////
57 let dirSvcProps = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
58 // get profile directory
59 global.getProfileDir = function () dirSvcProps.get("ProfD", Ci.nsIFile);
63 ////////////////////////////////////////////////////////////////////////////////
64 // string aString: A string of data to be hashed.
65 // string aAlg: optional; the hash algorithm to be used; possible values are: MD2, MD5, SHA1, SHA256, SHA384, and SHA512; defaults to SHA1
66 // string aCharset: optional; the charset used by the passed string; defaults to UTF-8
67 function cryptoHash (aString, aAlg, aCharset) {
68 const PR_UINT32_MAX = 0xffffffff; // this tells updateFromStream to read the entire string
71 let alg = (""+(aAlg||"SHA1")).trim().toUpperCase();
72 let charset = (""+(aCharset||"UTF-8")).trim();
74 let chashObj = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
77 chashObj.initWithString(alg);
79 logError("invalid hash algorithm: '"+aAlg+"'");
80 throw new Error("invalid hash algorithm: '"+aAlg+"'");
83 let uniconvObj = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
85 uniconvObj.charset = charset;
87 logError("invalid charset: '"+aCharset+"'");
88 throw new Error("invalid charset: '"+aCharset+"'");
91 if (str) chashObj.updateFromStream(uniconvObj.convertToInputStream(str), PR_UINT32_MAX);
92 let hash = chashObj.finish(false); // hash as raw octets
93 return [("0"+hash.charCodeAt(i).toString(16)).slice(-2) for (i in hash)].join("");
97 ////////////////////////////////////////////////////////////////////////////////
98 function calcFileSha512 (fname) {
99 let fl = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
100 fl.initWithPath(fname);
101 let istream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
103 istream.init(fl, 0x01, 0400, istream.CLOSE_ON_EOF); // 0x01: PR_RDONLY
104 let chan = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
105 chan.init(chan.SHA512);
106 chan.updateFromStream(istream, 0xffffffff);
107 // pass false here to get binary data back
108 let hash = chan.finish(false);
109 return [("0"+hash.charCodeAt(i).toString(16)).slice(-2) for (i in hash)].join("");
113 ////////////////////////////////////////////////////////////////////////////////
114 function uri2re (uri) {
115 uri = uri.replace(/^\s+/, "").replace(/\s+$/, "");
116 if (uri == "") return null;
117 if (uri == "*") return null;
119 if (uri.length >= 2 && uri.charCodeAt(0) == 47 && uri.charCodeAt(uri.length-1) == 47) {
120 if (uri.length < 3) return null;
121 return new RegExp(uri.substring(1, uri.length-1), "i");
123 // convert glob to regexp
125 for (let f = 0; f < uri.length; ++f) {
127 case "*": re += ".*?"; break; // any, non-greedy
128 case ".": case "?": case "^": case "$": case "+":
129 case "{": case "}": case "[": case "]": case "|":
130 case "(": case ")": case "\\":
133 case " ": break; // ignore spaces
134 default: re += uri[f]; break;
138 //if (addonOptions.debugCache) conlog("uri:["+uri+"] re: ["+re+"]");
139 return new RegExp(re, "i");
143 ////////////////////////////////////////////////////////////////////////////////
144 function wild2re (wildstr) {
145 wildstr = wildstr.replace(/^\s+/, "").replace(/\s+$/, "");
146 if (wildstr == "") wildstr = "*";
147 if (wildstr == "*") return new RegExp(/./);
149 if (wildstr.length >= 2 && wildstr.charCodeAt(0) == 47 && wildstr.charCodeAt(wildstr.length-1) == 47) {
150 if (wildstr.length < 3) return new RegExp(/./);
151 return new RegExp(wildstr.substring(1, wildstr.length-1), "i");
153 // convert glob to regexp
155 for (let f = 0; f < wildstr.length; ++f) {
156 switch (wildstr[f]) {
157 case "*": re += ".*"; break; // any, greedy
158 case ".": case "?": case "^": case "$": case "+":
159 case "{": case "}": case "[": case "]": case "|":
160 case "(": case ")": case "\\":
161 re += "\\"+wildstr[f];
163 case " ": re += "\\s"; break;
164 default: re += wildstr[f]; break;
168 //if (addonOptions.debugCache) conlog("wildstr:["+wildstr+"] re: ["+re+"]");
169 return new RegExp(re, "i");
173 ////////////////////////////////////////////////////////////////////////////////
174 function buildRelFile (fl, path) {
175 if (!fl) return null;
176 let res = fl.clone();
177 for (let n of path.split("/")) {
178 if (!n || n == ".") continue;
181 if (!res) return null;
190 ////////////////////////////////////////////////////////////////////////////////
191 function newIFile (path) {
192 let fl = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
193 fl.initWithPath(path);