JS⼯具函数⽅法(其中js的crc32和php的crc32区别)var util = {};typeof array
util.indexOf = function (array, item) {
for (var i = 0; i < array.length; i++) {
if (array[i] === item) {
return i;
}
}
return -1;
};
util.isFunction = function (source) {
return '[object Function]' === String.call(source);
};
util.isIE = function () {
var myNav = LowerCase();
return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
};
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
dst[i] = obj[i];
}
}
};
return prefix + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
};
var script = ateElement_x('script');
script.setAttribute('type', 'text/;
charset && script.setAttribute('charset', charset);
script.setAttribute('src', url);
script.async = true;
return script;
};
util.jsonp = function (url, onsuccess, onerror, charset) {
var callbackName = Name('tt_player');
window[callbackName] = function () {
if (onsuccess && util.isFunction(onsuccess)) {
onsuccess(arguments[0]);
}
};
var script = ateScript(url + '&callback=' + callbackName, charset);
if (!adyState || /loaded|complete/.adyState)) {
// 移除该script的 DOM 对象
if (script.parentNode) {
veChild(script);
}
// 删除函数或变量
window[callbackName] = null;
}
};
if (onerror && util.isFunction(onerror)) {
onerror();
}
};
};
util.json = function (options) {
var opt = {
url: '',
type: 'get',
data: {},
success: function () {},
error: function () {},
};
if (opt.url) {
var xhr = XMLHttpRequest
new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
var data = opt.data,
url = opt.url,
type = UpperCase(),
dataArr = [];
for (var k in data) {
dataArr.push(k + '=' + data[k]);
}
if (type === 'GET') {
url = url + '?' + dataArr.join('&');
xhr.open(type, place(/\?$/g, ''), true);
xhr.send();
}
if (type === 'POST') {
xhr.open(type, url, true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(dataArr.join('&'));
}
if (xhr.status === 200 || xhr.status === 304) {
var res;
if (opt.success && opt.success instanceof Function) {
res = sponseText;
if (typeof res ==== 'string') {
res = JSON.parse(res);
opt.success.call(xhr, res);
}
}
} else {
if ( && instanceof Function) {
}
}
};
}
};
<32 = function (url) {
var a = ateElement_x('a');
a.href = url;
var T = (function () {
var c = 0,
table = new Array(256);
for (var n = 0; n != 256; ++n) {
c = n;
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}
return typeof Int32Array !=== 'undefined' ? new Int32Array(table) : table;
})();
var crc32_str = function (str) {
var C = -1;
for (var i = 0, L = str.length, c, d; i < L;) {
c = str.charCodeAt(i++);
if (c < 0x80) {
C = (C >>> 8) ^ T[(C ^ c) & 0xFF];
} else if (c < 0x800) {
C = (C >>> 8) ^ T[(C ^ (192 | ((c >> 6) & 31))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (c & 63))) & 0xFF];
} else if (c >= 0xD800 && c < 0xE000) {
c = (c & 1023) + 64;
d = str.charCodeAt(i++) & 1023;
C = (C >>> 8) ^ T[(C ^ (240 | ((c >> 8) & 7))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((c >> 2) & 63))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((d >> 6) & 15) | ((c & 3) << 4))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (d & 63))) & 0xFF];
} else {
C = (C >>> 8) ^ T[(C ^ (224 | ((c >> 12) & 15))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((c >> 6) & 63))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (c & 63))) & 0xFF];
}
}
return C ^ -1;
};
var r = a.pathname + '?r=' + Math.random().toString(10).substring(2);
if (r[0] != '/') {
r = '/' + r;
}
var s = crc32_str(r) >>> 0;
var is_web = location.protocol.indexOf('http') > -1;
return (is_web ? [location.protocol, a.hostname] : ['http:', a.hostname]).join('//') + r + '&s=' + s;
};
export default util;
我的上⼏篇⽂章,还傻不拉⼏的把js⽣成crc32的算法⽤php翻译:
其实完全不⽤: (Warning由于 PHP 的整数是带符号的,所以在 32 位系统上许多 crc32 校验码将返回负整数。尽管在 64 位上所有 crc32() 的结果将都是正整数。) $g = "www.baidu";
printf("%u", crc32($g));
我的硬翻译:
<?php
function uright($a, $n)
{
$c = 2147483647>>($n-1);
return$c&($a>>$n);
}
function JS_charCodeAt($str, $index){
//not working!
$char = mb_substr($str, $index, 1, 'UTF-8');
if (mb_check_encoding($char, 'UTF-8'))
{
$ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
return hexdec(bin2hex($ret));
} else {
return null;
}
}
function modulo($a, $b) {
return$a - floor($a/$b)*$b;
}
function ToUint32($x) {
return modulo(intval($x), pow(2, 32));
}
function pcrc32()
{
for ($d = 0, $f = array(256), $g = 0; 256 != $g; ++$g) {
$d = $g;
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$f[$g] = $d;
}
//return "undefined" != typeof Int32Array ? new Int32Array($f) : $f;
return$f;
}
function pcrc32_str($g,$c)
{
for ($j = -1, $k = -1, $h = -1, $f = 0, $d =strlen($g); $f < $d;)
{
/
/开始的代码⑴ j = g.charCodeAt(f++),
//$j = $g.charCodeAt($f++); //js代码
$j = JS_charCodeAt($g, $f++); //php代码
//开始的代码⑵ j < 128 ? h = h >>> 8 ^ c[255 & (h ^ j)] :
if($j < 128)
{
$h = uright($h, 8) ^ $c[255 & ($h ^ $j)];
}
else
{
//开始的代码⑶ j < 2048 ? (h = h >>> 8 ^ c[255 & (h ^ (192 | j >> 6 & 31))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))]) :
if($j < 2048)
{
$h = uright($h, 8) ^ $c[255 & ($h ^ (192 | $j >> 6 & 31))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | 63 & $j))];
}
else
{
//开始的代码⑷ j >= 55296 && j < 57344 ? (j = (1023 & j) + 64, k = 1023 & g.charCodeAt(f++), h = h >>> 8 ^ c[255 & (h ^ (240 | j >> 8 & 7))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 2 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | k >> 6 & 1 if($j >= 55296 && $j < 57344)
{
$j = (1023 & $j) + 64;
//$k = 1023 & $g.charCodeAt($f++); //js代码
$k = 1023 & JS_charCodeAt($g, $f++); //php代码
$h = uright($h, 8) ^ $c[255 & ($h ^ (240 | $j >> 8 & 7))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | $j >> 2 & 63))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | $k >> 6 & 15 | (3 & $j) << 4))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | 63 & $k))];
}
else
{
$h = uright($h, 8) ^ $c[255 & ($h ^ (224 | $j >> 12 & 15))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | $j >> 6 & 63))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | 63 & $j))];
}
}
}
}
return$h ^ -1;
}
$g = "www.baidu";
$c = pcrc32();
$res = pcrc32_str($g,$c);
echo ToUint32($res); //js 的代码res >>> 0 翻译过来的 (很深奥) wwwblogs/fps2tao/p/9983508.html
>
//下⾯就是js的crc32,不过代码被格式化了,和上⾯的js⼯具包,⼀致
<script>
function test01(a) {
var c = function() {
for (var d = 0, f = new Array(256), g = 0; 256 != g; ++g) {
d = g, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = }
return "undefined" != typeof Int32Array ? new Int32Array(f) : f
}(),
b = function(g) {
for (var j, k, h = -1, f = 0, d = g.length; f < d;) {
j = g.charCodeAt(f++),j < 128 ? h = h >>> 8 ^ c[255 & (h ^ j)] : j < 2048 ? (h = h >>> 8 ^ c[255 & (h ^ (192 | j >> 6 & 31))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))]) : j >= 55296 && j < 57344 ? (j = (1023 & j) + 64, k = 1023 & g.charCode }
return h ^ -1
};
return b(a) >>> 0
}
var g = "www.baidu";
ss = test01(g);
console.log(ss);
</script>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论