|
| 1 | +/** |
| 2 | + * @author brun0ne [[email protected]] |
| 3 | + * @copyright Crown Copyright 2023 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import Operation from "../Operation.mjs"; |
| 8 | +import OperationError from "../errors/OperationError.mjs"; |
| 9 | + |
| 10 | +/** |
| 11 | + * PHP Serialize operation |
| 12 | + */ |
| 13 | +class PHPSerialize extends Operation { |
| 14 | + |
| 15 | + /** |
| 16 | + * PHPSerialize constructor |
| 17 | + */ |
| 18 | + constructor() { |
| 19 | + super(); |
| 20 | + |
| 21 | + this.name = "PHP Serialize"; |
| 22 | + this.module = "Default"; |
| 23 | + this.description = "Performs PHP serialization on JSON data.<br><br>This function does not support <code>object</code> tags.<br><br>Since PHP doesn't distinguish dicts and arrays, this operation is not always symmetric to <code>PHP Deserialize</code>.<br><br>Example:<br><code>[5,"abc",true]</code><br>becomes<br><code>a:3:{i:0;i:5;i:1;s:3:"abc";i:2;b:1;}<code>"; |
| 24 | + this.infoURL = "https://www.phpinternalsbook.com/php5/classes_objects/serialization.html"; |
| 25 | + this.inputType = "JSON"; |
| 26 | + this.outputType = "string"; |
| 27 | + this.args = []; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param {JSON} input |
| 32 | + * @param {Object[]} args |
| 33 | + * @returns {string} |
| 34 | + */ |
| 35 | + run(input, args) { |
| 36 | + /** |
| 37 | + * Determines if a number is an integer |
| 38 | + * @param {number} value |
| 39 | + * @returns {boolean} |
| 40 | + */ |
| 41 | + function isInteger(value) { |
| 42 | + return typeof value === "number" && parseInt(value.toString(), 10) === value; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Serialize basic types |
| 47 | + * @param {string | number | boolean} content |
| 48 | + * @returns {string} |
| 49 | + */ |
| 50 | + function serializeBasicTypes(content) { |
| 51 | + const basicTypes = { |
| 52 | + "string": "s", |
| 53 | + "integer": "i", |
| 54 | + "float": "d", |
| 55 | + "boolean": "b" |
| 56 | + }; |
| 57 | + /** |
| 58 | + * Booleans |
| 59 | + * cast to 0 or 1 |
| 60 | + */ |
| 61 | + if (typeof content === "boolean") { |
| 62 | + return `${basicTypes.boolean}:${content ? 1 : 0}`; |
| 63 | + } |
| 64 | + /* Numbers */ |
| 65 | + if (typeof content === "number") { |
| 66 | + if (isInteger(content)) { |
| 67 | + return `${basicTypes.integer}:${content.toString()}`; |
| 68 | + } else { |
| 69 | + return `${basicTypes.float}:${content.toString()}`; |
| 70 | + } |
| 71 | + } |
| 72 | + /* Strings */ |
| 73 | + if (typeof content === "string") |
| 74 | + return `${basicTypes.string}:${content.length}:"${content}"`; |
| 75 | + |
| 76 | + /** This should be unreachable */ |
| 77 | + throw new OperationError(`Encountered a non-implemented type: ${typeof content}`); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Recursively serialize |
| 82 | + * @param {*} object |
| 83 | + * @returns {string} |
| 84 | + */ |
| 85 | + function serialize(object) { |
| 86 | + /* Null */ |
| 87 | + if (object == null) { |
| 88 | + return `N;`; |
| 89 | + } |
| 90 | + |
| 91 | + if (typeof object !== "object") { |
| 92 | + /* Basic types */ |
| 93 | + return `${serializeBasicTypes(object)};`; |
| 94 | + } else if (object instanceof Array) { |
| 95 | + /* Arrays */ |
| 96 | + const serializedElements = []; |
| 97 | + |
| 98 | + for (let i = 0; i < object.length; i++) { |
| 99 | + serializedElements.push(`${serialize(i)}${serialize(object[i])}`); |
| 100 | + } |
| 101 | + |
| 102 | + return `a:${object.length}:{${serializedElements.join("")}}`; |
| 103 | + } else if (object instanceof Object) { |
| 104 | + /** |
| 105 | + * Objects |
| 106 | + * Note: the output cannot be guaranteed to be in the same order as the input |
| 107 | + */ |
| 108 | + const serializedElements = []; |
| 109 | + const keys = Object.keys(object); |
| 110 | + |
| 111 | + for (const key of keys) { |
| 112 | + serializedElements.push(`${serialize(key)}${serialize(object[key])}`); |
| 113 | + } |
| 114 | + |
| 115 | + return `a:${keys.length}:{${serializedElements.join("")}}`; |
| 116 | + } |
| 117 | + |
| 118 | + /** This should be unreachable */ |
| 119 | + throw new OperationError(`Encountered a non-implemented type: ${typeof object}`); |
| 120 | + } |
| 121 | + |
| 122 | + return serialize(input); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +export default PHPSerialize; |
0 commit comments