|
| 1 | +import { asciiBytes } from './byte-strings'; |
| 2 | + |
| 3 | +export class BinaryPlistData { |
| 4 | + constructor(bytes) { |
| 5 | + this.bytes = bytes; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +export class BinaryPlistUid { |
| 10 | + constructor(value) { |
| 11 | + this.value = value; |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +function concatBytes(parts) { |
| 16 | + const length = parts.reduce((sum, part) => sum + part.length, 0); |
| 17 | + const bytes = new Uint8Array(length); |
| 18 | + let offset = 0; |
| 19 | + |
| 20 | + parts.forEach((part) => { |
| 21 | + bytes.set(part, offset); |
| 22 | + offset += part.length; |
| 23 | + }); |
| 24 | + |
| 25 | + return bytes; |
| 26 | +} |
| 27 | + |
| 28 | +function minimumByteSize(value) { |
| 29 | + if (value <= 0xff) { |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + if (value <= 0xffff) { |
| 34 | + return 2; |
| 35 | + } |
| 36 | + |
| 37 | + if (value <= 0xffffffff) { |
| 38 | + return 4; |
| 39 | + } |
| 40 | + |
| 41 | + return 8; |
| 42 | +} |
| 43 | + |
| 44 | +function integerBytes(value, byteSize = minimumByteSize(value)) { |
| 45 | + const bytes = new Uint8Array(byteSize); |
| 46 | + let remaining = BigInt(value); |
| 47 | + |
| 48 | + for (let index = byteSize - 1; index >= 0; index -= 1) { |
| 49 | + bytes[index] = Number(remaining & 0xffn); |
| 50 | + remaining >>= 8n; |
| 51 | + } |
| 52 | + |
| 53 | + return bytes; |
| 54 | +} |
| 55 | + |
| 56 | +function markerWithLength(marker, length) { |
| 57 | + if (length < 15) { |
| 58 | + return new Uint8Array([marker | length]); |
| 59 | + } |
| 60 | + |
| 61 | + const lengthBytes = integerBytes(length); |
| 62 | + const intMarker = 0x10 | Math.log2(lengthBytes.length); |
| 63 | + |
| 64 | + return concatBytes([ |
| 65 | + new Uint8Array([marker | 0x0f, intMarker]), |
| 66 | + lengthBytes, |
| 67 | + ]); |
| 68 | +} |
| 69 | + |
| 70 | +function isScalar(value) { |
| 71 | + return value === null || |
| 72 | + typeof value === 'string' || |
| 73 | + typeof value === 'number' || |
| 74 | + value instanceof BinaryPlistData || |
| 75 | + value instanceof BinaryPlistUid; |
| 76 | +} |
| 77 | + |
| 78 | +function flattenObject(value, objects) { |
| 79 | + const index = objects.length; |
| 80 | + objects.push(value); |
| 81 | + |
| 82 | + if (isScalar(value)) { |
| 83 | + return index; |
| 84 | + } |
| 85 | + |
| 86 | + if (Array.isArray(value)) { |
| 87 | + value.forEach((item) => flattenObject(item, objects)); |
| 88 | + return index; |
| 89 | + } |
| 90 | + |
| 91 | + Object.keys(value).forEach((key) => { |
| 92 | + flattenObject(key, objects); |
| 93 | + flattenObject(value[key], objects); |
| 94 | + }); |
| 95 | + |
| 96 | + return index; |
| 97 | +} |
| 98 | + |
| 99 | +function objectReference(index, objectRefSize) { |
| 100 | + return integerBytes(index, objectRefSize); |
| 101 | +} |
| 102 | + |
| 103 | +function encodeScalar(value) { |
| 104 | + if (value === null) { |
| 105 | + return new Uint8Array([0x00]); |
| 106 | + } |
| 107 | + |
| 108 | + if (typeof value === 'number') { |
| 109 | + const bytes = integerBytes(value); |
| 110 | + return concatBytes([new Uint8Array([0x10 | Math.log2(bytes.length)]), bytes]); |
| 111 | + } |
| 112 | + |
| 113 | + if (typeof value === 'string') { |
| 114 | + const bytes = asciiBytes(value); |
| 115 | + return concatBytes([markerWithLength(0x50, bytes.length), bytes]); |
| 116 | + } |
| 117 | + |
| 118 | + if (value instanceof BinaryPlistData) { |
| 119 | + return concatBytes([markerWithLength(0x40, value.bytes.length), value.bytes]); |
| 120 | + } |
| 121 | + |
| 122 | + const bytes = integerBytes(value.value); |
| 123 | + return concatBytes([new Uint8Array([0x80 | (bytes.length - 1)]), bytes]); |
| 124 | +} |
| 125 | + |
| 126 | +function encodeObject(value, objectIndexes, objectRefSize) { |
| 127 | + if (isScalar(value)) { |
| 128 | + return encodeScalar(value); |
| 129 | + } |
| 130 | + |
| 131 | + if (Array.isArray(value)) { |
| 132 | + const references = value.map((item) => objectReference(objectIndexes.get(item), objectRefSize)); |
| 133 | + |
| 134 | + return concatBytes([markerWithLength(0xa0, value.length), ...references]); |
| 135 | + } |
| 136 | + |
| 137 | + const keys = Object.keys(value); |
| 138 | + const keyReferences = keys.map((key) => objectReference(objectIndexes.get(key), objectRefSize)); |
| 139 | + const valueReferences = keys.map((key) => objectReference(objectIndexes.get(value[key]), objectRefSize)); |
| 140 | + |
| 141 | + return concatBytes([markerWithLength(0xd0, keys.length), ...keyReferences, ...valueReferences]); |
| 142 | +} |
| 143 | + |
| 144 | +function buildObjectIndexMap(objects) { |
| 145 | + const objectIndexes = new Map(); |
| 146 | + |
| 147 | + objects.forEach((object, index) => { |
| 148 | + objectIndexes.set(object, index); |
| 149 | + }); |
| 150 | + |
| 151 | + return objectIndexes; |
| 152 | +} |
| 153 | + |
| 154 | +function trailer(offsetIntSize, objectRefSize, objectCount, topObjectIndex, offsetTableOffset) { |
| 155 | + return concatBytes([ |
| 156 | + new Uint8Array(6), |
| 157 | + new Uint8Array([offsetIntSize, objectRefSize]), |
| 158 | + integerBytes(objectCount, 8), |
| 159 | + integerBytes(topObjectIndex, 8), |
| 160 | + integerBytes(offsetTableOffset, 8), |
| 161 | + ]); |
| 162 | +} |
| 163 | + |
| 164 | +export function encodeBinaryPlist(value) { |
| 165 | + const objects = []; |
| 166 | + const topObjectIndex = flattenObject(value, objects); |
| 167 | + const objectRefSize = minimumByteSize(objects.length - 1); |
| 168 | + const objectIndexes = buildObjectIndexMap(objects); |
| 169 | + const header = asciiBytes('bplist00'); |
| 170 | + const offsets = []; |
| 171 | + const encodedObjects = []; |
| 172 | + let offset = header.length; |
| 173 | + |
| 174 | + objects.forEach((object) => { |
| 175 | + const encodedObject = encodeObject(object, objectIndexes, objectRefSize); |
| 176 | + |
| 177 | + offsets.push(offset); |
| 178 | + encodedObjects.push(encodedObject); |
| 179 | + offset += encodedObject.length; |
| 180 | + }); |
| 181 | + |
| 182 | + const offsetIntSize = minimumByteSize(offset); |
| 183 | + const offsetTableOffset = offset; |
| 184 | + const offsetTable = concatBytes(offsets.map((item) => integerBytes(item, offsetIntSize))); |
| 185 | + |
| 186 | + return concatBytes([ |
| 187 | + header, |
| 188 | + ...encodedObjects, |
| 189 | + offsetTable, |
| 190 | + trailer(offsetIntSize, objectRefSize, objects.length, topObjectIndex, offsetTableOffset), |
| 191 | + ]); |
| 192 | +} |
0 commit comments