@basmilius/apple-encoding 0.0.80 → 0.0.81

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +515 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1 +1,515 @@
1
- var D=Object.defineProperty;var g=(e,t)=>{for(var r in t)D(e,r,{get:t[r],enumerable:!0,configurable:!0,set:(f)=>t[r]=()=>f})};var I={};g(I,{parts:()=>d,ns:()=>F,now:()=>N,encode:()=>S,decode:()=>$});var O=0x83AA7E80n;function N(){let e=F()/1000n,t=e/1000000n,r=e-t*1000000n;return t+O<<32n|(r<<32n)/1000000n}function F(){return process.hrtime.bigint()}function d(e){return[Number(e>>32n),Number(e&0xFFFFFFFFn)]}function $(e){return{proto:e.readUInt8(0),type:e.readUInt8(1),seqno:e.readUInt16BE(2),padding:e.readUInt32BE(4),reftime_sec:e.readUInt32BE(8),reftime_frac:e.readUInt32BE(12),recvtime_sec:e.readUInt32BE(16),recvtime_frac:e.readUInt32BE(20),sendtime_sec:e.readUInt32BE(24),sendtime_frac:e.readUInt32BE(28)}}function S(e){let t=Buffer.alloc(32);return t.writeUInt8(e.proto,0),t.writeUInt8(e.type,1),t.writeUInt16BE(e.seqno,2),t.writeUInt32BE(e.padding,4),t.writeUInt32BE(e.reftime_sec,8),t.writeUInt32BE(e.reftime_frac,12),t.writeUInt32BE(e.recvtime_sec,16),t.writeUInt32BE(e.recvtime_frac,20),t.writeUInt32BE(e.sendtime_sec,24),t.writeUInt32BE(e.sendtime_frac,28),t}var k={};g(k,{sizedInt:()=>_,int:()=>z,float:()=>V,encode:()=>R,decode:()=>q});class E extends Number{size;constructor(e,t){super(e);this.size=t}}class A{value;constructor(e){this.value=e}}class v{value;constructor(e){this.value=e}}function _(e,t){return new E(e,t)}function V(e){return new A(e)}function z(e){return new v(e)}function c(e){let t=e.reduce((n,s)=>n+s.length,0),r=new Uint8Array(t),f=0;for(let n of e)r.set(n,f),f+=n.length;return r}function u(e){return Uint8Array.of(e)}function x(e,t){let r=new Uint8Array(t),f=BigInt(e);for(let n=0;n<t;n++)r[n]=Number(f&0xffn),f>>=8n;return r}function m(e){let t=e.reduce((n,s)=>n+s.length,0),r=new Uint8Array(t),f=0;for(let n of e)r.set(n,f),f+=n.length;return r}function R(e){return U(e,[])}function U(e,t){let r=null;if(e===null||e===void 0)r=u(4);else if(typeof e==="boolean")r=u(e?1:2);else if(e instanceof A){let n=new ArrayBuffer(8);new DataView(n).setFloat64(0,e.value,!0),r=c([u(54),new Uint8Array(n)])}else if(e instanceof v){let n=e.value;if(n<40)r=u(8+n);else if(n<=255)r=m([u(48),x(n,1)]);else if(n<=65535)r=m([u(49),x(n,2)]);else if(n<=4294967295)r=m([u(50),x(n,4)]);else r=m([u(51),x(n,8)])}else if(typeof e==="number")if(!Number.isInteger(e)){let n=new ArrayBuffer(8);new DataView(n).setFloat64(0,e,!0),r=c([u(54),new Uint8Array(n)])}else if(e<40)r=u(8+e);else if(e<=255)r=c([u(48),x(e,1)]);else if(e<=65535)r=c([u(49),x(e,2)]);else if(e<=4294967295)r=c([u(50),x(e,4)]);else r=c([u(51),x(e,8)]);else if(e instanceof E)r=c([u(48+Math.log2(e.size)),x(e.valueOf(),e.size)]);else if(typeof e==="string"){let n=new TextEncoder().encode(e),s=n.length;if(s<=32)r=c([u(64+s),n]);else if(s<=255)r=c([u(97),x(s,1),n]);else if(s<=65535)r=c([u(98),x(s,2),n]);else if(s<=16777215)r=c([u(99),x(s,3),n]);else r=c([u(100),x(s,4),n])}else if(e instanceof Uint8Array||Buffer.isBuffer(e)){let n=e instanceof Uint8Array?e:new Uint8Array(e),s=n.length;if(s<=32)r=c([u(112+s),n]);else if(s<=255)r=c([u(145),x(s,1),n]);else if(s<=65535)r=c([u(146),x(s,2),n]);else r=c([u(147),x(s,4),n])}else if(Array.isArray(e)){let n=c(e.map((o)=>U(o,t))),s=e.length;if(s<=15){if(r=c([u(208+s),n]),s>=15)r=c([r,u(3)])}else r=c([u(223),n,u(3)])}else if(typeof e==="object"){let n=Object.keys(e),s=n.length,o=[];for(let l of n)o.push(U(l,t)),o.push(U(e[l],t));let i;if(s<=15)i=u(224+s);else i=u(239);if(r=m([i,m(o)]),s>=15||t.some((l)=>l===r))r=m([r,u(129)])}else throw TypeError(typeof e);let f=t.findIndex((n)=>n.length===r.length&&n.every((s,o)=>s===r[o]));if(f>=0)if(f<33)r=u(160+f);else if(f<=255)r=c([u(193),x(f,1)]);else if(f<=65535)r=c([u(194),x(f,2)]);else if(f<=4294967295)r=c([u(195),x(f,4)]);else r=c([u(196),x(f,8)]);else if(r.length>1)t.push(r);return r}function q(e){let[t]=p(e,[]);return t}function H(e,t){if(e.length<t)throw TypeError(`Not enough data: need ${t} bytes, have ${e.length}`)}function h(e,t,r){H(e.subarray(t),r);let f=0n;for(let n=r-1;n>=0;n--)f=f<<8n|BigInt(e[t+n]);return Number(f)}function p(e,t){if(e.length===0)throw TypeError("No data to unpack");let r=e[0],f=!0,n,s;if(r===1)n=!0,s=e.subarray(1);else if(r===2)n=!1,s=e.subarray(1);else if(r===4)n=null,s=e.subarray(1);else if(r===5)n=e.subarray(1,17),s=e.subarray(17);else if(r===6)n=h(e,1,8),s=e.subarray(9);else if(r>=8&&r<=47)n=r-8,s=e.subarray(1);else if(r===53)n=new DataView(e.buffer,e.byteOffset+1,4).getFloat32(0,!0),s=e.subarray(5);else if(r===54)n=new DataView(e.buffer,e.byteOffset+1,8).getFloat64(0,!0),s=e.subarray(9);else if((r&240)===48){let o=2**(r&15),i=h(e,1,o);n=_(i,o),s=e.subarray(1+o)}else if(r>=64&&r<=96){let o=r-64;n=new TextDecoder().decode(e.subarray(1,1+o)),s=e.subarray(1+o)}else if(r>=97&&r<=100){let o=r&15,i=h(e,1,o);n=new TextDecoder().decode(e.subarray(1+o,1+o+i)),s=e.subarray(1+o+i)}else if(r>=112&&r<=144){let o=r-112;n=e.subarray(1,1+o),s=e.subarray(1+o)}else if(r>=145&&r<=148){let o=1<<(r&15)-1,i=h(e,1,o),l=1+o;n=e.subarray(l,l+i),s=e.subarray(l+i)}else if((r&240)===208){let o=r&15,i=e.subarray(1),l=[];if(o===15){while(i[0]!==3){let[a,y]=p(i,t);l.push(a),i=y}i=i.subarray(1)}else for(let a=0;a<o;a++){let[y,w]=p(i,t);l.push(y),i=w}n=l,s=i,f=!1}else if((r&224)===224){let o=r&15,i=e.subarray(1),l={};if(o===15){while(i[0]!==3){let[a,y]=p(i,t),[w,B]=p(y,t);l[a]=w,i=B}i=i.subarray(1)}else for(let a=0;a<o;a++){let[y,w]=p(i,t),[B,T]=p(w,t);l[y]=B,i=T}n=l,s=i,f=!1}else if(r>=160&&r<=192){let o=r-160;if(o>=t.length)throw TypeError(`Reference index ${o} out of range`);n=t[o],s=e.subarray(1),f=!1}else if(r>=193&&r<=196){let o=r-192,i=h(e,1,o);if(i>=t.length)throw TypeError(`UID ${i} out of range`);n=t[i],s=e.subarray(1+o),f=!1}else throw TypeError(`Unknown tag 0x${r.toString(16)}`);if(f)t.push(n);return[n,s]}var P={};g(P,{serialize:()=>W,parse:()=>K});import{parse as K}from"@plist/binary.parse";import{serialize as W}from"@plist/binary.serialize";var M={};g(M,{encode:()=>Y,decode:()=>Z,bail:()=>X,Value:()=>b,State:()=>Q,Method:()=>J,Flags:()=>G,ErrorCode:()=>C});var G={TransientPairing:16},C={Unknown:1,Authentication:2,BackOff:3,MaxPeers:4,MaxTries:5,Unavailable:6,Busy:7},J={PairSetup:0,PairSetupWithAuth:1,PairVerify:2,AddPairing:3,RemovePairing:4,ListPairing:5},Q={M1:1,M2:2,M3:3,M4:4,M5:5,M6:6},b={Method:0,Identifier:1,Salt:2,PublicKey:3,Proof:4,EncryptedData:5,State:6,Error:7,BackOff:8,Certificate:9,Signature:10,Permissions:11,FragmentData:12,FragmentLast:13,Name:17,Flags:19};function X(e){if(e.has(b.BackOff)){let t=e.get(b.BackOff),r=t.readUintLE(0,t.length);throw Error(`Device is busy, try again in ${r} seconds.`)}if(e.has(b.Error)){let r=Object.entries(C).find(([f,n])=>n===e.get(b.Error).readUint8());if(!r)throw Error(`Device returned an unknown error code: ${e.get(b.Error).readUint8()}`);throw Error(`Device returned an error code: ${r[0]}`)}throw Error("Invalid response")}function Y(e){let t=[];for(let[r,f]of e){let n;if(typeof f==="number")n=Buffer.from([f]);else n=f;let s=0;do{let o=Math.min(n.length-s,255);if(t.push(r,o),o>0)for(let i=0;i<o;i++)t.push(n[s+i]);s+=o}while(s<n.length)}return Buffer.from(t)}function Z(e){let t=new Map,r=0;while(r<e.length){let f=e[r++],n=e[r++],s=new Uint8Array(e).slice(r,r+n);r+=n;let o=t.get(f);if(o)t.set(f,Buffer.concat([o,s]));else t.set(f,Buffer.from(s))}return t}export{M as TLV8,P as Plist,k as OPack,I as NTP};
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, {
5
+ get: all[name],
6
+ enumerable: true,
7
+ configurable: true,
8
+ set: (newValue) => all[name] = () => newValue
9
+ });
10
+ };
11
+
12
+ // src/ntp.ts
13
+ var exports_ntp = {};
14
+ __export(exports_ntp, {
15
+ parts: () => parts,
16
+ ns: () => ns,
17
+ now: () => now,
18
+ encode: () => encode,
19
+ decode: () => decode
20
+ });
21
+ var EPOCH = 0x83AA7E80n;
22
+ function now() {
23
+ const now2 = ns() / 1000n;
24
+ const seconds = now2 / 1000000n;
25
+ const frac = now2 - seconds * 1000000n;
26
+ return seconds + EPOCH << 32n | (frac << 32n) / 1000000n;
27
+ }
28
+ function ns() {
29
+ return process.hrtime.bigint();
30
+ }
31
+ function parts(ntp) {
32
+ return [
33
+ Number(ntp >> 32n),
34
+ Number(ntp & 0xFFFFFFFFn)
35
+ ];
36
+ }
37
+ function decode(buffer) {
38
+ return {
39
+ proto: buffer.readUInt8(0),
40
+ type: buffer.readUInt8(1),
41
+ seqno: buffer.readUInt16BE(2),
42
+ padding: buffer.readUInt32BE(4),
43
+ reftime_sec: buffer.readUInt32BE(8),
44
+ reftime_frac: buffer.readUInt32BE(12),
45
+ recvtime_sec: buffer.readUInt32BE(16),
46
+ recvtime_frac: buffer.readUInt32BE(20),
47
+ sendtime_sec: buffer.readUInt32BE(24),
48
+ sendtime_frac: buffer.readUInt32BE(28)
49
+ };
50
+ }
51
+ function encode(fields) {
52
+ const buffer = Buffer.alloc(32);
53
+ buffer.writeUInt8(fields.proto, 0);
54
+ buffer.writeUInt8(fields.type, 1);
55
+ buffer.writeUInt16BE(fields.seqno, 2);
56
+ buffer.writeUInt32BE(fields.padding, 4);
57
+ buffer.writeUInt32BE(fields.reftime_sec, 8);
58
+ buffer.writeUInt32BE(fields.reftime_frac, 12);
59
+ buffer.writeUInt32BE(fields.recvtime_sec, 16);
60
+ buffer.writeUInt32BE(fields.recvtime_frac, 20);
61
+ buffer.writeUInt32BE(fields.sendtime_sec, 24);
62
+ buffer.writeUInt32BE(fields.sendtime_frac, 28);
63
+ return buffer;
64
+ }
65
+ // src/opack.ts
66
+ var exports_opack = {};
67
+ __export(exports_opack, {
68
+ sizedInt: () => sizedInt,
69
+ int: () => int,
70
+ float: () => float,
71
+ encode: () => encode2,
72
+ decode: () => decode2
73
+ });
74
+
75
+ class OPackSizedInt extends Number {
76
+ size;
77
+ constructor(value, size) {
78
+ super(value);
79
+ this.size = size;
80
+ }
81
+ }
82
+
83
+ class OPackFloat {
84
+ value;
85
+ constructor(value) {
86
+ this.value = value;
87
+ }
88
+ }
89
+
90
+ class OPackInteger {
91
+ value;
92
+ constructor(value) {
93
+ this.value = value;
94
+ }
95
+ }
96
+ function sizedInt(value, size) {
97
+ return new OPackSizedInt(value, size);
98
+ }
99
+ function float(value) {
100
+ return new OPackFloat(value);
101
+ }
102
+ function int(value) {
103
+ return new OPackInteger(value);
104
+ }
105
+ function concat(arr) {
106
+ const total = arr.reduce((s, a) => s + a.length, 0);
107
+ const out = new Uint8Array(total);
108
+ let off = 0;
109
+ for (const a of arr) {
110
+ out.set(a, off);
111
+ off += a.length;
112
+ }
113
+ return out;
114
+ }
115
+ function u8(b) {
116
+ return Uint8Array.of(b);
117
+ }
118
+ function uintToLEBytes(value, byteLen) {
119
+ const out = new Uint8Array(byteLen);
120
+ let v = BigInt(value);
121
+ for (let i = 0;i < byteLen; i++) {
122
+ out[i] = Number(v & 0xffn);
123
+ v >>= 8n;
124
+ }
125
+ return out;
126
+ }
127
+ function concatUint8Arrays(arrays) {
128
+ const total = arrays.reduce((sum, a) => sum + a.length, 0);
129
+ const out = new Uint8Array(total);
130
+ let offset = 0;
131
+ for (const a of arrays) {
132
+ out.set(a, offset);
133
+ offset += a.length;
134
+ }
135
+ return out;
136
+ }
137
+ function encode2(data) {
138
+ return _pack(data, []);
139
+ }
140
+ function _pack(data, objectList) {
141
+ let packed = null;
142
+ if (data === null || data === undefined)
143
+ packed = u8(4);
144
+ else if (typeof data === "boolean")
145
+ packed = u8(data ? 1 : 2);
146
+ else if (data instanceof OPackFloat) {
147
+ const buf = new ArrayBuffer(8);
148
+ new DataView(buf).setFloat64(0, data.value, true);
149
+ packed = concat([u8(54), new Uint8Array(buf)]);
150
+ } else if (data instanceof OPackInteger) {
151
+ const val = data.value;
152
+ if (val < 40)
153
+ packed = u8(8 + val);
154
+ else if (val <= 255)
155
+ packed = concatUint8Arrays([u8(48), uintToLEBytes(val, 1)]);
156
+ else if (val <= 65535)
157
+ packed = concatUint8Arrays([u8(49), uintToLEBytes(val, 2)]);
158
+ else if (val <= 4294967295)
159
+ packed = concatUint8Arrays([u8(50), uintToLEBytes(val, 4)]);
160
+ else
161
+ packed = concatUint8Arrays([u8(51), uintToLEBytes(val, 8)]);
162
+ } else if (typeof data === "number") {
163
+ if (!Number.isInteger(data)) {
164
+ const buf = new ArrayBuffer(8);
165
+ new DataView(buf).setFloat64(0, data, true);
166
+ packed = concat([u8(54), new Uint8Array(buf)]);
167
+ } else {
168
+ if (data < 40)
169
+ packed = u8(8 + data);
170
+ else if (data <= 255)
171
+ packed = concat([u8(48), uintToLEBytes(data, 1)]);
172
+ else if (data <= 65535)
173
+ packed = concat([u8(49), uintToLEBytes(data, 2)]);
174
+ else if (data <= 4294967295)
175
+ packed = concat([u8(50), uintToLEBytes(data, 4)]);
176
+ else
177
+ packed = concat([u8(51), uintToLEBytes(data, 8)]);
178
+ }
179
+ } else if (data instanceof OPackSizedInt) {
180
+ packed = concat([u8(48 + Math.log2(data.size)), uintToLEBytes(data.valueOf(), data.size)]);
181
+ } else if (typeof data === "string") {
182
+ const b = new TextEncoder().encode(data);
183
+ const len = b.length;
184
+ if (len <= 32)
185
+ packed = concat([u8(64 + len), b]);
186
+ else if (len <= 255)
187
+ packed = concat([u8(97), uintToLEBytes(len, 1), b]);
188
+ else if (len <= 65535)
189
+ packed = concat([u8(98), uintToLEBytes(len, 2), b]);
190
+ else if (len <= 16777215)
191
+ packed = concat([u8(99), uintToLEBytes(len, 3), b]);
192
+ else
193
+ packed = concat([u8(100), uintToLEBytes(len, 4), b]);
194
+ } else if (data instanceof Uint8Array || Buffer.isBuffer(data)) {
195
+ const bytes = data instanceof Uint8Array ? data : new Uint8Array(data);
196
+ const len = bytes.length;
197
+ if (len <= 32)
198
+ packed = concat([u8(112 + len), bytes]);
199
+ else if (len <= 255)
200
+ packed = concat([u8(145), uintToLEBytes(len, 1), bytes]);
201
+ else if (len <= 65535)
202
+ packed = concat([u8(146), uintToLEBytes(len, 2), bytes]);
203
+ else
204
+ packed = concat([u8(147), uintToLEBytes(len, 4), bytes]);
205
+ } else if (Array.isArray(data)) {
206
+ const body = concat(data.map((d) => _pack(d, objectList)));
207
+ const len = data.length;
208
+ if (len <= 15) {
209
+ packed = concat([u8(208 + len), body]);
210
+ if (len >= 15)
211
+ packed = concat([packed, u8(3)]);
212
+ } else
213
+ packed = concat([u8(223), body, u8(3)]);
214
+ } else if (typeof data === "object") {
215
+ const keys = Object.keys(data);
216
+ const len = keys.length;
217
+ const pairs = [];
218
+ for (const k of keys) {
219
+ pairs.push(_pack(k, objectList));
220
+ pairs.push(_pack(data[k], objectList));
221
+ }
222
+ let header;
223
+ if (len <= 15) {
224
+ header = u8(224 + len);
225
+ } else {
226
+ header = u8(239);
227
+ }
228
+ packed = concatUint8Arrays([header, concatUint8Arrays(pairs)]);
229
+ if (len >= 15 || objectList.some((v) => v === packed)) {
230
+ packed = concatUint8Arrays([packed, u8(129)]);
231
+ }
232
+ } else
233
+ throw new TypeError(typeof data + "");
234
+ const idx = objectList.findIndex((v) => v.length === packed.length && v.every((x, i) => x === packed[i]));
235
+ if (idx >= 0) {
236
+ if (idx < 33)
237
+ packed = u8(160 + idx);
238
+ else if (idx <= 255)
239
+ packed = concat([u8(193), uintToLEBytes(idx, 1)]);
240
+ else if (idx <= 65535)
241
+ packed = concat([u8(194), uintToLEBytes(idx, 2)]);
242
+ else if (idx <= 4294967295)
243
+ packed = concat([u8(195), uintToLEBytes(idx, 4)]);
244
+ else
245
+ packed = concat([u8(196), uintToLEBytes(idx, 8)]);
246
+ } else if (packed.length > 1)
247
+ objectList.push(packed);
248
+ return packed;
249
+ }
250
+ function decode2(data) {
251
+ const [value] = _unpack(data, []);
252
+ return value;
253
+ }
254
+ function ensureAvailable(buf, need) {
255
+ if (buf.length < need)
256
+ throw new TypeError(`Not enough data: need ${need} bytes, have ${buf.length}`);
257
+ }
258
+ function readLittleEndian(buf, offset, len) {
259
+ ensureAvailable(buf.subarray(offset), len);
260
+ let v = 0n;
261
+ for (let i = len - 1;i >= 0; i--)
262
+ v = v << 8n | BigInt(buf[offset + i]);
263
+ return Number(v);
264
+ }
265
+ function _unpack(data, objectList) {
266
+ if (data.length === 0)
267
+ throw new TypeError("No data to unpack");
268
+ const tag = data[0];
269
+ let addToObjectList = true;
270
+ let value;
271
+ let rest;
272
+ if (tag === 1) {
273
+ value = true;
274
+ rest = data.subarray(1);
275
+ } else if (tag === 2) {
276
+ value = false;
277
+ rest = data.subarray(1);
278
+ } else if (tag === 4) {
279
+ value = null;
280
+ rest = data.subarray(1);
281
+ } else if (tag === 5) {
282
+ value = data.subarray(1, 17);
283
+ rest = data.subarray(17);
284
+ } else if (tag === 6) {
285
+ value = readLittleEndian(data, 1, 8);
286
+ rest = data.subarray(9);
287
+ } else if (tag >= 8 && tag <= 47) {
288
+ value = tag - 8;
289
+ rest = data.subarray(1);
290
+ } else if (tag === 53) {
291
+ const view = new DataView(data.buffer, data.byteOffset + 1, 4);
292
+ value = view.getFloat32(0, true);
293
+ rest = data.subarray(5);
294
+ } else if (tag === 54) {
295
+ const view = new DataView(data.buffer, data.byteOffset + 1, 8);
296
+ value = view.getFloat64(0, true);
297
+ rest = data.subarray(9);
298
+ } else if ((tag & 240) === 48) {
299
+ const noOfBytes = 2 ** (tag & 15);
300
+ const val = readLittleEndian(data, 1, noOfBytes);
301
+ value = sizedInt(val, noOfBytes);
302
+ rest = data.subarray(1 + noOfBytes);
303
+ } else if (tag >= 64 && tag <= 96) {
304
+ const length = tag - 64;
305
+ value = new TextDecoder().decode(data.subarray(1, 1 + length));
306
+ rest = data.subarray(1 + length);
307
+ } else if (tag >= 97 && tag <= 100) {
308
+ const lenBytes = tag & 15;
309
+ const length = readLittleEndian(data, 1, lenBytes);
310
+ value = new TextDecoder().decode(data.subarray(1 + lenBytes, 1 + lenBytes + length));
311
+ rest = data.subarray(1 + lenBytes + length);
312
+ } else if (tag >= 112 && tag <= 144) {
313
+ const length = tag - 112;
314
+ value = data.subarray(1, 1 + length);
315
+ rest = data.subarray(1 + length);
316
+ } else if (tag >= 145 && tag <= 148) {
317
+ const noOfBytes = 1 << (tag & 15) - 1;
318
+ const length = readLittleEndian(data, 1, noOfBytes);
319
+ const start = 1 + noOfBytes;
320
+ value = data.subarray(start, start + length);
321
+ rest = data.subarray(start + length);
322
+ } else if ((tag & 240) === 208) {
323
+ const count = tag & 15;
324
+ let ptr = data.subarray(1);
325
+ const arr = [];
326
+ if (count === 15) {
327
+ while (ptr[0] !== 3) {
328
+ const [v, r] = _unpack(ptr, objectList);
329
+ arr.push(v);
330
+ ptr = r;
331
+ }
332
+ ptr = ptr.subarray(1);
333
+ } else {
334
+ for (let i = 0;i < count; i++) {
335
+ const [v, r] = _unpack(ptr, objectList);
336
+ arr.push(v);
337
+ ptr = r;
338
+ }
339
+ }
340
+ value = arr;
341
+ rest = ptr;
342
+ addToObjectList = false;
343
+ } else if ((tag & 224) === 224) {
344
+ const count = tag & 15;
345
+ let ptr = data.subarray(1);
346
+ const obj = {};
347
+ if (count === 15) {
348
+ while (ptr[0] !== 3) {
349
+ const [k, r1] = _unpack(ptr, objectList);
350
+ const [v, r2] = _unpack(r1, objectList);
351
+ obj[k] = v;
352
+ ptr = r2;
353
+ }
354
+ ptr = ptr.subarray(1);
355
+ } else {
356
+ for (let i = 0;i < count; i++) {
357
+ const [k, r1] = _unpack(ptr, objectList);
358
+ const [v, r2] = _unpack(r1, objectList);
359
+ obj[k] = v;
360
+ ptr = r2;
361
+ }
362
+ }
363
+ value = obj;
364
+ rest = ptr;
365
+ addToObjectList = false;
366
+ } else if (tag >= 160 && tag <= 192) {
367
+ const idx = tag - 160;
368
+ if (idx >= objectList.length)
369
+ throw new TypeError(`Reference index ${idx} out of range`);
370
+ value = objectList[idx];
371
+ rest = data.subarray(1);
372
+ addToObjectList = false;
373
+ } else if (tag >= 193 && tag <= 196) {
374
+ const len = tag - 192;
375
+ const uid = readLittleEndian(data, 1, len);
376
+ if (uid >= objectList.length)
377
+ throw new TypeError(`UID ${uid} out of range`);
378
+ value = objectList[uid];
379
+ rest = data.subarray(1 + len);
380
+ addToObjectList = false;
381
+ } else {
382
+ throw new TypeError(`Unknown tag 0x${tag.toString(16)}`);
383
+ }
384
+ if (addToObjectList)
385
+ objectList.push(value);
386
+ return [value, rest];
387
+ }
388
+ // src/plist.ts
389
+ var exports_plist = {};
390
+ __export(exports_plist, {
391
+ serialize: () => serialize,
392
+ parse: () => parse
393
+ });
394
+ import { parse } from "@plist/binary.parse";
395
+ import { serialize } from "@plist/binary.serialize";
396
+ // src/tlv8.ts
397
+ var exports_tlv8 = {};
398
+ __export(exports_tlv8, {
399
+ encode: () => encode3,
400
+ decode: () => decode3,
401
+ bail: () => bail,
402
+ Value: () => Value,
403
+ State: () => State,
404
+ Method: () => Method,
405
+ Flags: () => Flags,
406
+ ErrorCode: () => ErrorCode
407
+ });
408
+ var Flags = {
409
+ TransientPairing: 16
410
+ };
411
+ var ErrorCode = {
412
+ Unknown: 1,
413
+ Authentication: 2,
414
+ BackOff: 3,
415
+ MaxPeers: 4,
416
+ MaxTries: 5,
417
+ Unavailable: 6,
418
+ Busy: 7
419
+ };
420
+ var Method = {
421
+ PairSetup: 0,
422
+ PairSetupWithAuth: 1,
423
+ PairVerify: 2,
424
+ AddPairing: 3,
425
+ RemovePairing: 4,
426
+ ListPairing: 5
427
+ };
428
+ var State = {
429
+ M1: 1,
430
+ M2: 2,
431
+ M3: 3,
432
+ M4: 4,
433
+ M5: 5,
434
+ M6: 6
435
+ };
436
+ var Value = {
437
+ Method: 0,
438
+ Identifier: 1,
439
+ Salt: 2,
440
+ PublicKey: 3,
441
+ Proof: 4,
442
+ EncryptedData: 5,
443
+ State: 6,
444
+ Error: 7,
445
+ BackOff: 8,
446
+ Certificate: 9,
447
+ Signature: 10,
448
+ Permissions: 11,
449
+ FragmentData: 12,
450
+ FragmentLast: 13,
451
+ Name: 17,
452
+ Flags: 19
453
+ };
454
+ function bail(data) {
455
+ if (data.has(Value.BackOff)) {
456
+ const buffer = data.get(Value.BackOff);
457
+ const time = buffer.readUintLE(0, buffer.length);
458
+ throw new Error(`Device is busy, try again in ${time} seconds.`);
459
+ }
460
+ if (data.has(Value.Error)) {
461
+ const errorCodeEntries = Object.entries(ErrorCode);
462
+ const errorCode = errorCodeEntries.find(([_, code]) => code === data.get(Value.Error).readUint8());
463
+ if (!errorCode) {
464
+ throw new Error(`Device returned an unknown error code: ${data.get(Value.Error).readUint8()}`);
465
+ }
466
+ throw new Error(`Device returned an error code: ${errorCode[0]}`);
467
+ }
468
+ throw new Error("Invalid response");
469
+ }
470
+ function encode3(entries) {
471
+ const chunks = [];
472
+ for (const [type, valueRaw] of entries) {
473
+ let value;
474
+ if (typeof valueRaw === "number") {
475
+ value = Buffer.from([valueRaw]);
476
+ } else {
477
+ value = valueRaw;
478
+ }
479
+ let offset = 0;
480
+ do {
481
+ const len = Math.min(value.length - offset, 255);
482
+ chunks.push(type, len);
483
+ if (len > 0) {
484
+ for (let i = 0;i < len; i++) {
485
+ chunks.push(value[offset + i]);
486
+ }
487
+ }
488
+ offset += len;
489
+ } while (offset < value.length);
490
+ }
491
+ return Buffer.from(chunks);
492
+ }
493
+ function decode3(buf) {
494
+ const map = new Map;
495
+ let i = 0;
496
+ while (i < buf.length) {
497
+ const type = buf[i++];
498
+ const len = buf[i++];
499
+ const value = new Uint8Array(buf).slice(i, i + len);
500
+ i += len;
501
+ const existing = map.get(type);
502
+ if (existing) {
503
+ map.set(type, Buffer.concat([existing, value]));
504
+ } else {
505
+ map.set(type, Buffer.from(value));
506
+ }
507
+ }
508
+ return map;
509
+ }
510
+ export {
511
+ exports_tlv8 as TLV8,
512
+ exports_plist as Plist,
513
+ exports_opack as OPack,
514
+ exports_ntp as NTP
515
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@basmilius/apple-encoding",
3
3
  "description": "Common encoding utilities for Apple Protocols.",
4
- "version": "0.0.80",
4
+ "version": "0.0.81",
5
5
  "type": "module",
6
6
  "license": "MIT",
7
7
  "author": {