From 824a2d9f587ca017fc71b84d835e72f54f9c87c4 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 7 Nov 2018 18:02:36 +0100 Subject: Began rewrite --- node_modules/locutus/php/var/unserialize.js | 204 ++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 node_modules/locutus/php/var/unserialize.js (limited to 'node_modules/locutus/php/var/unserialize.js') diff --git a/node_modules/locutus/php/var/unserialize.js b/node_modules/locutus/php/var/unserialize.js new file mode 100644 index 0000000..048a133 --- /dev/null +++ b/node_modules/locutus/php/var/unserialize.js @@ -0,0 +1,204 @@ +'use strict'; + +module.exports = function unserialize(data) { + // discuss at: http://locutus.io/php/unserialize/ + // original by: Arpad Ray (mailto:arpad@php.net) + // improved by: Pedro Tainha (http://www.pedrotainha.com) + // improved by: Kevin van Zonneveld (http://kvz.io) + // improved by: Kevin van Zonneveld (http://kvz.io) + // improved by: Chris + // improved by: James + // improved by: Le Torbi + // improved by: Eli Skeggs + // bugfixed by: dptr1988 + // bugfixed by: Kevin van Zonneveld (http://kvz.io) + // bugfixed by: Brett Zamir (http://brett-zamir.me) + // bugfixed by: philippsimon (https://github.com/philippsimon/) + // revised by: d3x + // input by: Brett Zamir (http://brett-zamir.me) + // input by: Martin (http://www.erlenwiese.de/) + // input by: kilops + // input by: Jaroslaw Czarniak + // input by: lovasoa (https://github.com/lovasoa/) + // note 1: We feel the main purpose of this function should be + // note 1: to ease the transport of data between php & js + // note 1: Aiming for PHP-compatibility, we have to translate objects to arrays + // example 1: unserialize('a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}') + // returns 1: ['Kevin', 'van', 'Zonneveld'] + // example 2: unserialize('a:2:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";}') + // returns 2: {firstName: 'Kevin', midName: 'van'} + // example 3: unserialize('a:3:{s:2:"ü";s:2:"ü";s:3:"四";s:3:"四";s:4:"𠜎";s:4:"𠜎";}') + // returns 3: {'ü': 'ü', '四': '四', '𠜎': '𠜎'} + + var $global = typeof window !== 'undefined' ? window : global; + + var utf8Overhead = function utf8Overhead(str) { + var s = str.length; + for (var i = str.length - 1; i >= 0; i--) { + var code = str.charCodeAt(i); + if (code > 0x7f && code <= 0x7ff) { + s++; + } else if (code > 0x7ff && code <= 0xffff) { + s += 2; + } + // trail surrogate + if (code >= 0xDC00 && code <= 0xDFFF) { + i--; + } + } + return s - 1; + }; + var error = function error(type, msg, filename, line) { + throw new $global[type](msg, filename, line); + }; + var readUntil = function readUntil(data, offset, stopchr) { + var i = 2; + var buf = []; + var chr = data.slice(offset, offset + 1); + + while (chr !== stopchr) { + if (i + offset > data.length) { + error('Error', 'Invalid'); + } + buf.push(chr); + chr = data.slice(offset + (i - 1), offset + i); + i += 1; + } + return [buf.length, buf.join('')]; + }; + var readChrs = function readChrs(data, offset, length) { + var i, chr, buf; + + buf = []; + for (i = 0; i < length; i++) { + chr = data.slice(offset + (i - 1), offset + i); + buf.push(chr); + length -= utf8Overhead(chr); + } + return [buf.length, buf.join('')]; + }; + function _unserialize(data, offset) { + var dtype; + var dataoffset; + var keyandchrs; + var keys; + var contig; + var length; + var array; + var readdata; + var readData; + var ccount; + var stringlength; + var i; + var key; + var kprops; + var kchrs; + var vprops; + var vchrs; + var value; + var chrs = 0; + var typeconvert = function typeconvert(x) { + return x; + }; + + if (!offset) { + offset = 0; + } + dtype = data.slice(offset, offset + 1).toLowerCase(); + + dataoffset = offset + 2; + + switch (dtype) { + case 'i': + typeconvert = function typeconvert(x) { + return parseInt(x, 10); + }; + readData = readUntil(data, dataoffset, ';'); + chrs = readData[0]; + readdata = readData[1]; + dataoffset += chrs + 1; + break; + case 'b': + typeconvert = function typeconvert(x) { + return parseInt(x, 10) !== 0; + }; + readData = readUntil(data, dataoffset, ';'); + chrs = readData[0]; + readdata = readData[1]; + dataoffset += chrs + 1; + break; + case 'd': + typeconvert = function typeconvert(x) { + return parseFloat(x); + }; + readData = readUntil(data, dataoffset, ';'); + chrs = readData[0]; + readdata = readData[1]; + dataoffset += chrs + 1; + break; + case 'n': + readdata = null; + break; + case 's': + ccount = readUntil(data, dataoffset, ':'); + chrs = ccount[0]; + stringlength = ccount[1]; + dataoffset += chrs + 2; + + readData = readChrs(data, dataoffset + 1, parseInt(stringlength, 10)); + chrs = readData[0]; + readdata = readData[1]; + dataoffset += chrs + 2; + if (chrs !== parseInt(stringlength, 10) && chrs !== readdata.length) { + error('SyntaxError', 'String length mismatch'); + } + break; + case 'a': + readdata = {}; + + keyandchrs = readUntil(data, dataoffset, ':'); + chrs = keyandchrs[0]; + keys = keyandchrs[1]; + dataoffset += chrs + 2; + + length = parseInt(keys, 10); + contig = true; + + for (i = 0; i < length; i++) { + kprops = _unserialize(data, dataoffset); + kchrs = kprops[1]; + key = kprops[2]; + dataoffset += kchrs; + + vprops = _unserialize(data, dataoffset); + vchrs = vprops[1]; + value = vprops[2]; + dataoffset += vchrs; + + if (key !== i) { + contig = false; + } + + readdata[key] = value; + } + + if (contig) { + array = new Array(length); + for (i = 0; i < length; i++) { + array[i] = readdata[i]; + } + readdata = array; + } + + dataoffset += 1; + break; + default: + error('SyntaxError', 'Unknown / Unhandled data type(s): ' + dtype); + break; + } + return [dtype, dataoffset - offset, typeconvert(readdata)]; + } + + return _unserialize(data + '', 0)[2]; +}; +//# sourceMappingURL=unserialize.js.map \ No newline at end of file -- cgit v1.2.3