php-serialize
Advanced tools
Comparing version 1.2.4 to 1.2.5
@@ -0,1 +1,6 @@ | ||
## 1.2.5 | ||
- Fixed support for multi-byte strings | ||
- Rewrote most of decode internals to work on Buffers instead of strings (external API still the same) | ||
## 1.2.4 | ||
@@ -2,0 +7,0 @@ |
213
lib/index.js
@@ -13,9 +13,2 @@ 'use strict'; | ||
var REGEX = { | ||
i: /i:([\d]+);/, | ||
d: /d:([\d.]+);/, | ||
C: /C:[\d]+:"([\S ]+?)":([\d]+):/, | ||
O: /O:[\d]+:"([\S ]+?)":([\d]+):/ | ||
}; | ||
function serialize(item) { | ||
@@ -76,94 +69,111 @@ var type = typeof item === 'undefined' ? 'undefined' : _typeof(item); | ||
function unserializeItem(item, scope, options) { | ||
var type = item.substr(0, 1); | ||
function unserializeItem(item, startIndex, scope, options) { | ||
var currentIndex = startIndex; | ||
var type = item.toString('utf8', currentIndex, currentIndex + 1); | ||
// Increment for the type and color (or semi-color for null) characters | ||
currentIndex += 2; | ||
if (type === 'N') { | ||
// Null | ||
return { index: currentIndex, value: null }; | ||
} | ||
if (type === 'i' || type === 'd') { | ||
var match = REGEX[type].exec(item); | ||
(0, _assert2.default)(Array.isArray(match), 'Syntax Error'); | ||
return { index: match[0].length, value: type === 'i' ? parseInt(match[1], 10) : parseFloat(match[1]) }; | ||
// Integer or Double (aka float) | ||
var valueEnd = item.indexOf(';', currentIndex); | ||
var _value3 = item.toString('utf8', currentIndex, valueEnd); | ||
// +1 because of extra semi-colon at the end | ||
currentIndex += _value3.length + 1; | ||
return { index: currentIndex, value: type === 'i' ? parseInt(_value3, 10) : parseFloat(_value3) }; | ||
} | ||
if (type === 'N') { | ||
return { index: 2, value: null }; | ||
} | ||
if (type === 'b') { | ||
return { index: 4, value: item.substr(2, 1) === '1' }; | ||
// Boolean | ||
var _value4 = item.toString('utf8', currentIndex, currentIndex + 1); | ||
// +2 for 1 digital value and a semi-colon | ||
currentIndex += 2; | ||
return { index: currentIndex, value: _value4 === '1' }; | ||
} | ||
if (type === 's') { | ||
var lengthEnd = item.indexOf(':', 2); | ||
var length = parseInt(item.slice(2, lengthEnd), 10) || 0; | ||
var startIndex = 4 + (lengthEnd - 2); | ||
var sliced = Buffer.from(item).slice(startIndex, startIndex + length).toString(); | ||
return { index: 4 + lengthEnd + length, value: sliced }; | ||
// String | ||
var lengthEnd = item.indexOf(':', currentIndex); | ||
var length = parseInt(item.slice(currentIndex, lengthEnd), 10) || 0; | ||
// +2 because of color and starting of inverted commas at start of string | ||
currentIndex = lengthEnd + 2; | ||
var _value5 = item.toString('utf8', currentIndex, currentIndex + length); | ||
// +2 because of closing of inverted commas at end of string, and extra semi-colon | ||
currentIndex += length + 2; | ||
return { index: currentIndex, value: _value5 }; | ||
} | ||
if (type === 'C') { | ||
var info = REGEX.C.exec(item); | ||
(0, _assert2.default)(Array.isArray(info), 'Syntax Error'); | ||
var className = info[1]; | ||
var contentLength = parseInt(info[2], 10); | ||
var contentOffset = info.index + info[0].length + 1; | ||
var classContent = item.slice(contentOffset, contentOffset + contentLength); | ||
var classReference = scope[className]; | ||
var container = void 0; | ||
if (!classReference) { | ||
if (options.strict) { | ||
(0, _assert2.default)(false, 'Class ' + className + ' not found in given scope'); | ||
} | ||
container = (0, _helpers.getIncompleteClass)(className); | ||
} else { | ||
(0, _assert2.default)(typeof scope[className].prototype.unserialize === 'function', className + '.prototype.unserialize is not a function'); | ||
container = new ((0, _helpers.getClass)(scope[className].prototype))(); | ||
// Serializable class | ||
var classNameLengthEnd = item.indexOf(':', currentIndex); | ||
var classNameLength = parseInt(item.toString('utf8', currentIndex, classNameLengthEnd), 10) || 0; | ||
// +2 for : and start of inverted commas for class name | ||
currentIndex = classNameLengthEnd + 2; | ||
var className = item.toString('utf8', currentIndex, currentIndex + classNameLength); | ||
// +2 for end of inverted commas and color before inner content length | ||
currentIndex += classNameLength + 2; | ||
var contentLengthEnd = item.indexOf(':', currentIndex); | ||
var contentLength = parseInt(item.toString('utf8', currentIndex, contentLengthEnd), 10) || 0; | ||
// +2 for : and { at start of inner content | ||
currentIndex = contentLengthEnd + 2; | ||
var classContent = item.toString('utf8', currentIndex, currentIndex + contentLength); | ||
// +1 for the } at end of inner content | ||
currentIndex += contentLength + 1; | ||
var container = getClassReference(className, scope, options.strict); | ||
if (container.constructor.name !== '__PHP_Incomplete_Class') { | ||
(0, _assert2.default)(typeof container.unserialize === 'function', container.constructor.name.toLowerCase() + '.unserialize is not a function'); | ||
// console.log('classContent', classContent) | ||
container.unserialize(classContent); | ||
} | ||
return { index: contentOffset + contentLength + 1, value: container }; | ||
return { index: currentIndex, value: container }; | ||
} | ||
if (type === 'a') { | ||
var _ret = function () { | ||
var first = true; | ||
var index = 0; | ||
var container = void 0; | ||
var lengthEnd = item.indexOf(':', 2); | ||
var length = parseInt(item.slice(2, lengthEnd), 10) || 0; | ||
if (length !== 0) { | ||
index = unserializeObject(length, item.slice(4 + (lengthEnd - 2)), scope, function (key, value) { | ||
if (first) { | ||
container = parseInt(key, 10) === 0 ? [] : {}; | ||
first = false; | ||
} | ||
if (container.constructor.name === 'Array') { | ||
container.push(value); | ||
} else container[key] = value; | ||
}, options); | ||
} else container = []; | ||
return { | ||
v: { index: 4 + (lengthEnd - 2) + index + 1, value: container } | ||
}; | ||
}(); | ||
// Array or Object | ||
var first = true; | ||
var _container = []; | ||
var _lengthEnd = item.indexOf(':', currentIndex); | ||
var _length = parseInt(item.toString('utf8', currentIndex, _lengthEnd), 10) || 0; | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
// +2 for ":{" before the start of object | ||
currentIndex = _lengthEnd + 2; | ||
currentIndex = unserializeObject(item, currentIndex, _length, scope, function (key, value) { | ||
if (first) { | ||
_container = parseInt(key, 10) === 0 ? [] : {}; | ||
first = false; | ||
} | ||
_container[key] = value; | ||
}, options); | ||
// +1 for the last } at the end of object | ||
currentIndex++; | ||
return { index: currentIndex, value: _container }; | ||
} | ||
if (type === 'O') { | ||
var _ret2 = function () { | ||
var info = REGEX.O.exec(item); | ||
(0, _assert2.default)(Array.isArray(info), 'Syntax Error'); | ||
var className = info[1]; | ||
var contentLength = parseInt(info[2], 10); | ||
var contentOffset = info.index + info[0].length + 1; | ||
var classReference = scope[className]; | ||
var container = void 0; | ||
if (!classReference) { | ||
if (options.strict) { | ||
(0, _assert2.default)(false, 'Class ' + className + ' not found in given scope'); | ||
} | ||
container = (0, _helpers.getIncompleteClass)(className); | ||
} else { | ||
container = new ((0, _helpers.getClass)(scope[className].prototype))(); | ||
} | ||
var index = unserializeObject(contentLength, item.slice(contentOffset), scope, function (key, value) { | ||
container[key] = value; | ||
}, options); | ||
return { | ||
v: { index: contentOffset + index + 1, value: container } | ||
}; | ||
}(); | ||
// Non-Serializable Class | ||
var _classNameLengthEnd = item.indexOf(':', currentIndex); | ||
var _classNameLength = parseInt(item.toString('utf8', currentIndex, _classNameLengthEnd), 10) || 0; | ||
if ((typeof _ret2 === 'undefined' ? 'undefined' : _typeof(_ret2)) === "object") return _ret2.v; | ||
// +2 for : and start of inverted commas for class name | ||
currentIndex = _classNameLengthEnd + 2; | ||
var _className = item.toString('utf8', currentIndex, currentIndex + _classNameLength); | ||
// +2 for end of inverted commas and color before inner content length | ||
currentIndex += _classNameLength + 2; | ||
var _contentLengthEnd = item.indexOf(':', currentIndex); | ||
var _contentLength = parseInt(item.toString('utf8', currentIndex, _contentLengthEnd), 10) || 0; | ||
// +2 for : and { at start of object | ||
currentIndex = _contentLengthEnd + 2; | ||
var _container2 = getClassReference(_className, scope, options.strict); | ||
currentIndex = unserializeObject(item, currentIndex, _contentLength, scope, function (key, value) { | ||
_container2[key] = value; | ||
}, options); | ||
// +1 for the last } at the end of object | ||
currentIndex += 1; | ||
return { index: currentIndex, value: _container2 }; | ||
} | ||
@@ -173,17 +183,32 @@ throw new SyntaxError(); | ||
function unserializeObject(count, content, scope, valueCallback, options) { | ||
var realCount = count * 2; | ||
var index = 0; | ||
function getClassReference(className, scope, strict) { | ||
var container = void 0; | ||
var classReference = scope[className]; | ||
if (!classReference) { | ||
if (strict) { | ||
(0, _assert2.default)(false, 'Class ' + className + ' not found in given scope'); | ||
} | ||
container = (0, _helpers.getIncompleteClass)(className); | ||
} else { | ||
container = new ((0, _helpers.getClass)(scope[className].prototype))(); | ||
} | ||
return container; | ||
} | ||
function unserializeObject(item, startIndex, length, scope, valueCallback, options) { | ||
var key = null; | ||
for (var i = 0; i < realCount; ++i) { | ||
var item = unserializeItem(content.slice(index), scope, options); | ||
var currentIndex = startIndex; | ||
for (var i = 0; i < length * 2; ++i) { | ||
var entry = unserializeItem(item, currentIndex, scope, options); | ||
if (key !== null) { | ||
valueCallback(key, item.value); | ||
valueCallback(key, entry.value); | ||
key = null; | ||
} else { | ||
key = item.value; | ||
key = entry.value; | ||
} | ||
index += item.index; | ||
currentIndex = entry.index; | ||
} | ||
return index; | ||
return currentIndex; | ||
} | ||
@@ -199,5 +224,5 @@ | ||
} | ||
return unserializeItem(item, scope, options).value; | ||
return unserializeItem(Buffer.from(item), 0, scope, options).value; | ||
} | ||
module.exports = { serialize: serialize, unserialize: unserialize }; |
{ | ||
"name": "php-serialize", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "PHP serialize/unserialize in Javascript", | ||
@@ -25,7 +25,7 @@ "main": "lib/index.js", | ||
"babel-cli": "^6.22.2", | ||
"babel-preset-steelbrain": "^4.0.0", | ||
"babel-preset-steelbrain": "^5.0.0", | ||
"eslint-config-steelbrain": "^1.0.1", | ||
"sb-exec": "^3.0.2", | ||
"flow-bin": "^0.37.4" | ||
"flow-bin": "^0.40.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14993
231