skil

a

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/470536/1218045/skil.js

  1. // ==UserScript==
  2. // @name skil
  3. // @description a
  4. // @author Nezart
  5. // @license MIT
  6. // ==/UserScript==
  7.  
  8. (function() {
  9. 'use strict';
  10.  
  11. function serialize(data) {
  12. const pow32 = 0x100000000;
  13. let floatBuffer, floatView;
  14. let array = new Uint8Array(128);
  15. let length = 0;
  16. append(data);
  17. return array.subarray(0, length);
  18.  
  19. function append(data) {
  20. switch (typeof data) {
  21. case "undefined":
  22. appendNull(data);
  23. break;
  24. case "boolean":
  25. appendBoolean(data);
  26. break;
  27. case "number":
  28. appendNumber(data);
  29. break;
  30. case "string":
  31. appendString(data);
  32. break;
  33. case "object":
  34. if (data === null) {
  35. appendNull(data);
  36. } else if (data instanceof Date) {
  37. appendDate(data);
  38. } else if (Array.isArray(data)) {
  39. appendArray(data);
  40. } else if (data instanceof Uint8Array || data instanceof Uint8ClampedArray) {
  41. appendBinArray(data);
  42. } else if (data instanceof Int8Array || data instanceof Int16Array || data instanceof Uint16Array ||
  43. data instanceof Int32Array || data instanceof Uint32Array ||
  44. data instanceof Float32Array || data instanceof Float64Array) {
  45. appendArray(data);
  46. } else {
  47. appendObject(data);
  48. }
  49. break;
  50. }
  51. }
  52.  
  53. function appendNull(data) {
  54. appendByte(0xc0);
  55. }
  56.  
  57. function appendBoolean(data) {
  58. appendByte(data ? 0xc3 : 0xc2);
  59. }
  60.  
  61. function appendNumber(data) {
  62. if (isFinite(data) && Math.floor(data) === data) {
  63. if (data >= 0 && data <= 0x7f) {
  64. appendByte(data);
  65. } else if (data < 0 && data >= -0x20) {
  66. appendByte(data);
  67. } else if (data > 0 && data <= 0xff) { // uint8
  68. appendBytes([0xcc, data]);
  69. } else if (data >= -0x80 && data <= 0x7f) { // int8
  70. appendBytes([0xd0, data]);
  71. } else if (data > 0 && data <= 0xffff) { // uint16
  72. appendBytes([0xcd, data >>> 8, data]);
  73. } else if (data >= -0x8000 && data <= 0x7fff) { // int16
  74. appendBytes([0xd1, data >>> 8, data]);
  75. } else if (data > 0 && data <= 0xffffffff) { // uint32
  76. appendBytes([0xce, data >>> 24, data >>> 16, data >>> 8, data]);
  77. } else if (data >= -0x80000000 && data <= 0x7fffffff) { // int32
  78. appendBytes([0xd2, data >>> 24, data >>> 16, data >>> 8, data]);
  79. } else if (data > 0 && data <= 0xffffffffffffffff) { // uint64
  80. let hi = data / pow32;
  81. let lo = data % pow32;
  82. appendBytes([0xd3, hi >>> 24, hi >>> 16, hi >>> 8, hi, lo >>> 24, lo >>> 16, lo >>> 8, lo]);
  83. } else if (data >= -0x8000000000000000 && data <= 0x7fffffffffffffff) { // int64
  84. appendByte(0xd3);
  85. appendInt64(data);
  86. } else if (data < 0) { // below int64
  87. appendBytes([0xd3, 0x80, 0, 0, 0, 0, 0, 0, 0]);
  88. } else { // above uint64
  89. appendBytes([0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
  90. }
  91. } else {
  92. if (!floatView) {
  93. floatBuffer = new ArrayBuffer(8);
  94. floatView = new DataView(floatBuffer);
  95. }
  96. floatView.setFloat64(0, data);
  97. appendByte(0xcb);
  98. appendBytes(new Uint8Array(floatBuffer));
  99. }
  100. }
  101.  
  102. function appendString(data) {
  103. let bytes = encodeUtf8(data);
  104. let length = bytes.length;
  105.  
  106. if (length <= 0x1f) {
  107. appendByte(0xa0 + length);
  108. } else if (length <= 0xff) {
  109. appendBytes([0xd9, length]);
  110. } else if (length <= 0xffff) {
  111. appendBytes([0xda, length >>> 8, length]);
  112. } else {
  113. appendBytes([0xdb, length >>> 24, length >>> 16, length >>> 8, length]);
  114. }
  115.  
  116. appendBytes(bytes);
  117. }
  118.  
  119. function appendArray(data) {
  120. let length = data.length;
  121.  
  122. if (length <= 0xf) {
  123. appendByte(0x90 + length);
  124. } else if (length <= 0xffff) {
  125. appendBytes([0xdc, length >>> 8, length]);
  126. } else {
  127. appendBytes([0xdd, length >>> 24, length >>> 16, length >>> 8, length]);
  128. }
  129.  
  130. for (let index = 0; index < length; index++) {
  131. append(data[index]);
  132. }
  133. }
  134.  
  135. function appendBinArray(data) {
  136. let length = data.length;
  137.  
  138. if (length <= 0xf) {
  139. appendBytes([0xc4, length]);
  140. } else if (length <= 0xffff) {
  141. appendBytes([0xc5, length >>> 8, length]);
  142. } else {
  143. appendBytes([0xc6, length >>> 24, length >>> 16, length >>> 8, length]);
  144. }
  145.  
  146. appendBytes(data);
  147. }
  148.  
  149. function appendObject(data) {
  150. let length = 0;
  151. for (let key in data) length++;
  152.  
  153. if (length <= 0xf) {
  154. appendByte(0x80 + length);
  155. } else if (length <= 0xffff) {
  156. appendBytes([0xde, length >>> 8, length]);
  157. } else {
  158. appendBytes([0xdf, length >>> 24, length >>> 16, length >>> 8, length]);
  159. }
  160.  
  161. for (let key in data) {
  162. append(key);
  163. append(data[key]);
  164. }
  165. }
  166.  
  167. function appendDate(data) {
  168. let sec = data.getTime() / 1000;
  169. if (data.getMilliseconds() === 0 && sec >= 0 && sec < 0x100000000) { // 32 bit seconds
  170. appendBytes([0xd6, 0xff, sec >>> 24, sec >>> 16, sec >>> 8, sec]);
  171. }
  172. else if (sec >= 0 && sec < 0x400000000) { // 30 bit nanoseconds, 34 bit seconds
  173. let ns = data.getMilliseconds() * 1000000;
  174. appendBytes([0xd7, 0xff, ns >>> 22, ns >>> 14, ns >>> 6, ((ns << 2) >>> 0) | (sec / pow32), sec >>> 24, sec >>> 16, sec >>> 8, sec]);
  175. }
  176. else { // 32 bit nanoseconds, 64 bit seconds, negative values allowed
  177. let ns = data.getMilliseconds() * 1000000;
  178. appendBytes([0xc7, 12, 0xff, ns >>> 24, ns >>> 16, ns >>> 8, ns]);
  179. appendInt64(sec);
  180. }
  181. }
  182.  
  183. function appendByte(byte) {
  184. if (array.length < length + 1) {
  185. let newLength = array.length * 2;
  186. while (newLength < length + 1)
  187. newLength *= 2;
  188. let newArray = new Uint8Array(newLength);
  189. newArray.set(array);
  190. array = newArray;
  191. }
  192. array[length] = byte;
  193. length++;
  194. }
  195.  
  196. function appendBytes(bytes) {
  197. if (array.length < length + bytes.length) {
  198. let newLength = array.length * 2;
  199. while (newLength < length + bytes.length)
  200. newLength *= 2;
  201. let newArray = new Uint8Array(newLength);
  202. newArray.set(array);
  203. array = newArray;
  204. }
  205. array.set(bytes, length);
  206. length += bytes.length;
  207. }
  208.  
  209. function appendInt64(value) {
  210. let hi, lo;
  211. if (value >= 0) {
  212. hi = value / pow32;
  213. lo = value % pow32;
  214. }
  215. else {
  216. value++;
  217. hi = Math.abs(value) / pow32;
  218. lo = Math.abs(value) % pow32;
  219. hi = ~hi;
  220. lo = ~lo;
  221. }
  222. appendBytes([hi >>> 24, hi >>> 16, hi >>> 8, hi, lo >>> 24, lo >>> 16, lo >>> 8, lo]);
  223. }
  224. }
  225.  
  226. function deserialize(array) {
  227. const pow32 = 0x100000000; // 2^32
  228. let pos = 0;
  229. if (array instanceof ArrayBuffer) {
  230. array = new Uint8Array(array);
  231. }
  232. if (typeof array !== "object" || typeof array.length === "undefined") {
  233. throw new Error("Invalid argument type: Expected a byte array (Array or Uint8Array) to deserialize.");
  234. }
  235. if (!array.length) {
  236. throw new Error("Invalid argument: The byte array to deserialize is empty.");
  237. }
  238. if (!(array instanceof Uint8Array)) {
  239. array = new Uint8Array(array);
  240. }
  241. let data = read();
  242. if (pos < array.length) {
  243. }
  244. return data;
  245.  
  246. function read() {
  247. const byte = array[pos++];
  248. if (byte >= 0x00 && byte <= 0x7f) return byte; // positive fixint
  249. if (byte >= 0x80 && byte <= 0x8f) return readMap(byte - 0x80); // fixmap
  250. if (byte >= 0x90 && byte <= 0x9f) return readArray(byte - 0x90); // fixarray
  251. if (byte >= 0xa0 && byte <= 0xbf) return readStr(byte - 0xa0); // fixstr
  252. if (byte === 0xc0) return null; // nil
  253. if (byte === 0xc1) throw new Error("Invalid byte code 0xc1 found."); // never used
  254. if (byte === 0xc2) return false // false
  255. if (byte === 0xc3) return true; // true
  256. if (byte === 0xc4) return readBin(-1, 1); // bin 8
  257. if (byte === 0xc5) return readBin(-1, 2); // bin 16
  258. if (byte === 0xc6) return readBin(-1, 4); // bin 32
  259. if (byte === 0xc7) return readExt(-1, 1); // ext 8
  260. if (byte === 0xc8) return readExt(-1, 2); // ext 16
  261. if (byte === 0xc9) return readExt(-1, 4) // ext 32
  262. if (byte === 0xca) return readFloat(4); // float 32
  263. if (byte === 0xcb) return readFloat(8); // float 64
  264. if (byte === 0xcc) return readUInt(1); // uint 8
  265. if (byte === 0xcd) return readUInt(2); // uint 16
  266. if (byte === 0xce) return readUInt(4); // uint 32
  267. if (byte === 0xcf) return readUInt(8) // uint 64
  268. if (byte === 0xd0) return readInt(1); // int 8
  269. if (byte === 0xd1) return readInt(2); // int 16
  270. if (byte === 0xd2) return readInt(4); // int 32
  271. if (byte === 0xd3) return readInt(8); // int 64
  272. if (byte === 0xd4) return readExt(1); // fixext 1
  273. if (byte === 0xd5) return readExt(2); // fixext 2
  274. if (byte === 0xd6) return readExt(4); // fixext 4
  275. if (byte === 0xd7) return readExt(8); // fixext 8
  276. if (byte === 0xd8) return readExt(16); // fixext 16
  277. if (byte === 0xd9) return readStr(-1, 1); // str 8
  278. if (byte === 0xda) return readStr(-1, 2); // str 16
  279. if (byte === 0xdb) return readStr(-1, 4); // str 32
  280. if (byte === 0xdc) return readArray(-1, 2); // array 16
  281. if (byte === 0xdd) return readArray(-1, 4); // array 32
  282. if (byte === 0xde) return readMap(-1, 2); // map 16
  283. if (byte === 0xdf) return readMap(-1, 4); // map 32
  284. if (byte >= 0xe0 && byte <= 0xff) return byte - 256; // negative fixint
  285. console.debug("msgpack array:", array);
  286. throw new Error("Invalid byte value '" + byte + "' at index " + (pos - 1) + " in the MessagePack binary data (length " + array.length + "): Expecting a range of 0 to 255. This is not a byte array.");
  287. }
  288.  
  289. function readInt(size) {
  290. let value = 0;
  291. let first = true;
  292. while (size-- > 0) {
  293. if (first) {
  294. let byte = array[pos++];
  295. value += byte & 0x7f;
  296. if (byte & 0x80) {
  297. value -= 0x80;
  298. }
  299. first = false;
  300. }
  301. else {
  302. value *= 256;
  303. value += array[pos++];
  304. }
  305. }
  306. return value;
  307. }
  308.  
  309. function readUInt(size) {
  310. let value = 0;
  311. while (size-- > 0) {
  312. value *= 256;
  313. value += array[pos++];
  314. }
  315. return value;
  316. }
  317.  
  318. function readFloat(size) {
  319. let view = new DataView(array.buffer, pos, size);
  320. pos += size;
  321. if (size === 4) {
  322. return view.getFloat32(0, false);
  323. }
  324. if (size === 8) {
  325. return view.getFloat64(0, false);
  326. }
  327. }
  328.  
  329. function readBin(size, lengthSize) {
  330. if (size < 0) size = readUInt(lengthSize);
  331. let data = array.subarray(pos, pos + size);
  332. pos += size;
  333. return data;
  334. }
  335.  
  336. function readMap(size, lengthSize) {
  337. if (size < 0) size = readUInt(lengthSize);
  338. let data = {};
  339. while (size-- > 0) {
  340. let key = read();
  341. data[key] = read();
  342. }
  343. return data;
  344. }
  345.  
  346. function readArray(size, lengthSize) {
  347. if (size < 0) size = readUInt(lengthSize);
  348. let data = [];
  349. while (size-- > 0) {
  350. data.push(read());
  351. }
  352. return data;
  353. }
  354.  
  355. function readStr(size, lengthSize) {
  356. if (size < 0) size = readUInt(lengthSize);
  357. let start = pos;
  358. pos += size;
  359. return decodeUtf8(array, start, size);
  360. }
  361.  
  362. function readExt(size, lengthSize) {
  363. if (size < 0) size = readUInt(lengthSize);
  364. let type = readUInt(1);
  365. let data = readBin(size);
  366. switch (type) {
  367. case 255:
  368. return readExtDate(data);
  369. }
  370. return { type: type, data: data };
  371. }
  372.  
  373. function readExtDate(data) {
  374. if (data.length === 4) {
  375. let sec = ((data[0] << 24) >>> 0) +
  376. ((data[1] << 16) >>> 0) +
  377. ((data[2] << 8) >>> 0) +
  378. data[3];
  379. return new Date(sec * 1000);
  380. }
  381. if (data.length === 8) {
  382. let ns = ((data[0] << 22) >>> 0) +
  383. ((data[1] << 14) >>> 0) +
  384. ((data[2] << 6) >>> 0) +
  385. (data[3] >>> 2);
  386. let sec = ((data[3] & 0x3) * pow32) +
  387. ((data[4] << 24) >>> 0) +
  388. ((data[5] << 16) >>> 0) +
  389. ((data[6] << 8) >>> 0) +
  390. data[7];
  391. return new Date(sec * 1000 + ns / 1000000);
  392. }
  393. if (data.length === 12) {
  394. let ns = ((data[0] << 24) >>> 0) +
  395. ((data[1] << 16) >>> 0) +
  396. ((data[2] << 8) >>> 0) +
  397. data[3];
  398. pos -= 8;
  399. let sec = readInt(8);
  400. return new Date(sec * 1000 + ns / 1000000);
  401. }
  402. throw new Error("Invalid data length for a date value.");
  403. }
  404. }
  405.  
  406. function encodeUtf8(str) {
  407. let ascii = true, length = str.length;
  408. for (let x = 0; x < length; x++) {
  409. if (str.charCodeAt(x) > 127) {
  410. ascii = false;
  411. break;
  412. }
  413. }
  414.  
  415. let i = 0, bytes = new Uint8Array(str.length * (ascii ? 1 : 4));
  416. for (let ci = 0; ci !== length; ci++) {
  417. let c = str.charCodeAt(ci);
  418. if (c < 128) {
  419. bytes[i++] = c;
  420. continue;
  421. }
  422. if (c < 2048) {
  423. bytes[i++] = c >> 6 | 192;
  424. }
  425. else {
  426. if (c > 0xd7ff && c < 0xdc00) {
  427. if (++ci >= length)
  428. throw new Error("UTF-8 encode: incomplete surrogate pair");
  429. let c2 = str.charCodeAt(ci);
  430. if (c2 < 0xdc00 || c2 > 0xdfff)
  431. throw new Error("UTF-8 encode: second surrogate character 0x" + c2.toString(16) + " at index " + ci + " out of range");
  432. c = 0x10000 + ((c & 0x03ff) << 10) + (c2 & 0x03ff);
  433. bytes[i++] = c >> 18 | 240;
  434. bytes[i++] = c >> 12 & 63 | 128;
  435. }
  436. else bytes[i++] = c >> 12 | 224;
  437. bytes[i++] = c >> 6 & 63 | 128;
  438. }
  439. bytes[i++] = c & 63 | 128;
  440. }
  441. return ascii ? bytes : bytes.subarray(0, i);
  442. }
  443.  
  444. function decodeUtf8(bytes, start, length) {
  445. let i = start, str = "";
  446. length += start;
  447. while (i < length) {
  448. let c = bytes[i++];
  449. if (c > 127) {
  450. if (c > 191 && c < 224) {
  451. if (i >= length)
  452. throw new Error("UTF-8 decode: incomplete 2-byte sequence");
  453. c = (c & 31) << 6 | bytes[i++] & 63;
  454. }
  455. else if (c > 223 && c < 240) {
  456. if (i + 1 >= length)
  457. throw new Error("UTF-8 decode: incomplete 3-byte sequence");
  458. c = (c & 15) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63;
  459. }
  460. else if (c > 239 && c < 248) {
  461. if (i + 2 >= length)
  462. throw new Error("UTF-8 decode: incomplete 4-byte sequence");
  463. c = (c & 7) << 18 | (bytes[i++] & 63) << 12 | (bytes[i++] & 63) << 6 | bytes[i++] & 63;
  464. }
  465. else throw new Error("UTF-8 decode: unknown multibyte start 0x" + c.toString(16) + " at index " + (i - 1));
  466. }
  467. if (c <= 0xffff) str += String.fromCharCode(c);
  468. else if (c <= 0x10ffff) {
  469. c -= 0x10000;
  470. str += String.fromCharCode(c >> 10 | 0xd800)
  471. str += String.fromCharCode(c & 0x3FF | 0xdc00)
  472. }
  473. else throw new Error("UTF-8 decode: code point 0x" + c.toString(16) + " exceeds UTF-16 reach");
  474. }
  475. return str;
  476. }
  477.  
  478. let msgpack = {
  479. serialize: serialize,
  480. deserialize: deserialize,
  481.  
  482. encode: serialize,
  483. decode: deserialize
  484. };
  485.  
  486. if (typeof module === "object" && module && typeof module.exports === "object") {
  487. module.exports = msgpack;
  488. }
  489. else {
  490. window[window.msgpackJsName || "msgpack"] = msgpack;
  491. }
  492.  
  493. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址