2 * Copyright 2015 Ketmar Dark <ketmar@ketmar.no-ip.org>
3 * Portions copyright 2010, Erik Vold
4 * Contributors: See contributors list in install.rdf and CREDITS
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * Note that this license applies only to the Greasemonkey extension source
14 * files, not to the user scripts which it runs. User scripts are licensed
15 * separately by their authors.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * The above copyright notice and this permission notice shall be included in all
26 * copies or substantial portions of the Software.
28 ////////////////////////////////////////////////////////////////////////////////
29 // string aString: A string of data to be hashed.
30 // string aAlg: optional; the hash algorithm to be used; possible values are: MD2, MD5, SHA1, SHA256, SHA384, and SHA512; defaults to SHA1
31 // string aCharset: optional; the charset used by the passed string; defaults to UTF-8
32 function cryptoHash (aString, aAlg, aCharset) {
33 const PR_UINT32_MAX = 0xffffffff; // this tells updateFromStream to read the entire string
36 let alg = (""+(aAlg||"SHA1")).trim().toUpperCase();
37 let charset = (""+(aCharset||"UTF-8")).trim();
39 let chashObj = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
42 chashObj.initWithString(alg);
44 logError("invalid hash algorithm: '"+aAlg+"'");
45 throw new Error("invalid hash algorithm: '"+aAlg+"'");
48 let uniconvObj = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
50 uniconvObj.charset = charset;
52 logError("invalid charset: '"+aCharset+"'");
53 throw new Error("invalid charset: '"+aCharset+"'");
56 if (str) chashObj.updateFromStream(uniconvObj.convertToInputStream(str), PR_UINT32_MAX);
57 let hash = chashObj.finish(false); // hash as raw octets
58 return [("0"+hash.charCodeAt(i).toString(16)).slice(-2) for (i in hash)].join("");
60 addExport("cryptoHash", cryptoHash);
63 ////////////////////////////////////////////////////////////////////////////////
64 function calcFileSha512 (fname) {
65 let fl = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
66 fl.initWithPath(fname);
67 let istream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
69 istream.init(fl, 0x01, 0400, istream.CLOSE_ON_EOF); // 0x01: PR_RDONLY
70 let chan = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
71 chan.init(chan.SHA512);
72 // this tells updateFromStream to read the entire file
73 const PR_UINT32_MAX = 0xffffffff;
74 chan.updateFromStream(istream, PR_UINT32_MAX);
75 // pass false here to get binary data back
76 let hash = chan.finish(false);
77 return [("0"+hash.charCodeAt(i).toString(16)).slice(-2) for (i in hash)].join("");
79 addExport("calcFileSha512", calcFileSha512);
82 ////////////////////////////////////////////////////////////////////////////////
83 function uri2re (uri) {
84 uri = uri.replace(/^\s+/, "").replace(/\s+$/, "");
85 if (uri == "") return null;
86 if (uri == "*") return null;
88 if (uri.length >= 2 && uri.charCodeAt(0) == 47 && uri.charCodeAt(uri.length-1) == 47) {
89 if (uri.length < 3) return null;
90 return new RegExp(uri.substring(1, uri.length-1), "i");
92 // convert glob to regexp
94 for (let f = 0; f < uri.length; ++f) {
96 case "*": re += ".*?"; break; // any, non-greedy
97 case ".": case "?": case "^": case "$": case "+":
98 case "{": case "}": case "[": case "]": case "|":
99 case "(": case ")": case "\\":
102 case " ": break; // ignore spaces
103 default: re += uri[f]; break;
107 //if (guerillaOptions.debugCache) conlog("uri:["+uri+"] re: ["+re+"]");
108 return new RegExp(re, "i");
110 addExport("uri2re", uri2re);
113 ////////////////////////////////////////////////////////////////////////////////
114 function wild2re (wildstr) {
115 wildstr = wildstr.replace(/^\s+/, "").replace(/\s+$/, "");
116 if (wildstr == "") wildstr = "*";
117 if (wildstr == "*") return new RegExp(/./);
119 if (wildstr.length >= 2 && wildstr.charCodeAt(0) == 47 && wildstr.charCodeAt(wildstr.length-1) == 47) {
120 if (wildstr.length < 3) return new RegExp(/./);
121 return new RegExp(wildstr.substring(1, wildstr.length-1), "i");
123 // convert glob to regexp
125 for (let f = 0; f < wildstr.length; ++f) {
126 switch (wildstr[f]) {
127 case "*": re += ".*"; break; // any, greedy
128 case ".": case "?": case "^": case "$": case "+":
129 case "{": case "}": case "[": case "]": case "|":
130 case "(": case ")": case "\\":
131 re += "\\"+wildstr[f];
133 case " ": re += "\\s"; break;
134 default: re += wildstr[f]; break;
138 //if (guerillaOptions.debugCache) conlog("wildstr:["+wildstr+"] re: ["+re+"]");
139 return new RegExp(re, "i");
141 addExport("wild2re", wild2re);
144 ////////////////////////////////////////////////////////////////////////////////
145 function buildRelFile (fl, path) {
146 if (!fl) return null;
147 let res = fl.clone();
148 for (let n of path.split("/")) {
149 if (!n || n == ".") continue;
152 if (!res) return null;
159 addExport("buildRelFile", buildRelFile);
162 ////////////////////////////////////////////////////////////////////////////////
163 function newIFile (path) {
164 let fl = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
165 fl.initWithPath(path);
168 addExport("newIFile", newIFile);