Reformatted PHP deserialization.

This commit is contained in:
Jarmo van Lenthe 2017-11-12 22:11:16 -05:00
parent f596fe8404
commit 50a32e90d9

View File

@ -19,6 +19,12 @@ const PhpSerialization = {
*/ */
OUTPUT_VALID_JSON: true, OUTPUT_VALID_JSON: true,
/**
* Deserializes a PHP serialized input
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
PhpDeserialize: function (input, args) { PhpDeserialize: function (input, args) {
function handleInput() { function handleInput() {
function read(length) { function read(length) {
@ -39,8 +45,7 @@ const PhpSerialization = {
let char = read(1); let char = read(1);
if (char === until) { if (char === until) {
break; break;
} } else {
else {
result += char; result += char;
} }
} }
@ -57,28 +62,27 @@ const PhpSerialization = {
} }
function handleArray() { function handleArray() {
let items = parseInt(readUntil(':')) * 2; let items = parseInt(readUntil(":"), 10) * 2;
expect('{'); expect("{");
let result = []; let result = [];
let isKey = true; let isKey = true;
let last_item = null; let lastItem = null;
for (let idx = 0; idx < items; idx++) { for (let idx = 0; idx < items; idx++) {
let item = handleInput(); let item = handleInput();
if (isKey) { if (isKey) {
last_item = item; lastItem = item;
isKey = false; isKey = false;
} else { } else {
let numberCheck = last_item.match(/[0-9]+/); let numberCheck = lastItem.match(/[0-9]+/);
if (args[0] && numberCheck && numberCheck[0].length === last_item.length) if (args[0] && numberCheck && numberCheck[0].length === lastItem.length) {
{ result.push("\"" + lastItem + "\": " + item);
result.push('"' + last_item + '": ' + item);
} else { } else {
result.push(last_item + ': ' + item); result.push(lastItem + ": " + item);
} }
isKey = true; isKey = true;
} }
} }
expect('}'); expect("}");
return result; return result;
} }
@ -86,41 +90,44 @@ const PhpSerialization = {
let kind = read(1).toLowerCase(); let kind = read(1).toLowerCase();
switch (kind) { switch (kind) {
case 'n': case "n":
expect(';'); expect(";");
return ''; return "";
case 'i': case "i":
case 'd': case "d":
case 'b': case "b": {
expect(':'); expect(":");
let data = readUntil(';'); let data = readUntil(";");
if (kind === 'b') if (kind === "b") {
return (parseInt(data) !== 0); return (parseInt(data, 10) !== 0);
}
return data; return data;
}
case "a":
expect(":");
return "{" + handleArray() + "}";
case 'a': case "s": {
expect(':'); expect(":");
return '{' + handleArray() + '}'; let length = readUntil(":");
expect("\"");
case 's':
expect(':');
let length = readUntil(':');
expect('"');
let value = read(length); let value = read(length);
expect('";'); expect("\";");
if (args[0]) if (args[0]) {
return '"' + value.replace(/"/g, '\\"') + '"'; return "\"" + value.replace(/"/g, "\\\"") + "\"";
else } else {
return '"' + value + '"'; return "\"" + value + "\"";
}
}
default: default:
throw "Unknown type: " + kind; throw "Unknown type: " + kind;
} }
} }
let inputPart = input.split(''); let inputPart = input.split("");
return handleInput(); return handleInput();
} }
}; };