-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbplist.js
844 lines (745 loc) · 25.4 KB
/
bplist.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
'use strict';
var util = require('util'),
BPLIST_TIME_START = 978307200000,
//BPLIST_NULL = 0x00,
BPLIST_BOOL = 0x00,
BPLIST_FALSE = 0x08,
BPLIST_TRUE = 0x09,
BPLIST_FILL = 0x0F,
/* will be used for length grabbing */
BPLIST_UINT = 0x10,
BPLIST_REAL = 0x20,
BPLIST_DATE = 0x30,
BPLIST_DATA = 0x40,
BPLIST_STRING = 0x50,
BPLIST_UNICODE = 0x60,
BPLIST_UID = 0x70,
BPLIST_ARRAY = 0xA0,
BPLIST_SET = 0xC0,
BPLIST_DICT = 0xD0,
BPLIST_MASK = 0xF0;
function error(str) {
throw new Error(str || "Error");
}
// {{{ BPlistNode
// {{{ private
function hashEncode(str) {
return String(str)
.replace("\\", "\\\\")
.replace(",", "\\,")
.replace(":", "\\:");
}
// }}}
function BPlistPlainNode(type, value) { // BPlistPlainNode {{{
this.type = type;
this.value = value;
}
BPlistPlainNode.prototype.hash = function() {
//hash value type:value
return [(this.type >> 4).toString(16), hashEncode(this.valueHash())].join(":");
};
BPlistPlainNode.prototype.valueHash = function() {
switch (this.type) {
case BPLIST_BOOL:
case BPLIST_UINT:
case BPLIST_REAL:
case BPLIST_STRING:
case BPLIST_UNICODE:
//默认情况下,直接String化Hash值
return String(this.value);
default:
error("Not a plain node:" + this.type);
}
}; // }}}
function BPlistDateNode(value) { // BPlistDateNode {{{
BPlistPlainNode.call(this, BPLIST_DATE, value);
}
util.inherits(BPlistDateNode, BPlistPlainNode);
BPlistDateNode.prototype.valueHash = function() {
return this.value.getTime();
}; // }}}
function BPlistArrayNode() { // BPlistArrayNode {{{
BPlistPlainNode.call(this, BPLIST_ARRAY, []);
this.indexs = null;
}
util.inherits(BPlistArrayNode, BPlistPlainNode);
BPlistArrayNode.prototype.push = function(value) {
this.value.push(value);
};
BPlistArrayNode.prototype.valueHash = function() {
var tmp = [],
count = this.value.length;
while (count--) {
tmp.unshift(hashEncode(this.value[count].hash()));
}
return tmp.join(",");
}; // }}}
function BPlistDictNode() { // BPlistDictNode {{{
BPlistPlainNode.call(this, BPLIST_DICT, []);
this.indexs = null;
}
util.inherits(BPlistDictNode, BPlistPlainNode);
BPlistDictNode.prototype.push = function(key, value) {
this.value.push([key, value]);
};
BPlistDictNode.prototype.valueHash = function() {
var tmp = [],
count = this.value.length,
item;
while (count--) {
item = this.value[count];
tmp.unshift([hashEncode(item[0].value), hashEncode(item[1].hash())].join(":"));
}
return tmp.join(",");
}; // }}}
function BPlistDataNode(buffer) { // BPlistDataNode {{{
BPlistPlainNode.call(this, BPLIST_DATA, buffer);
}
util.inherits(BPlistDataNode, BPlistPlainNode);
BPlistDataNode.prototype.valueHash = function() {
return this.value.toString("base64");
}; // }}}
// }}}
// {{{ BPlistBuffer
var BPLIST_MAGIC = "bplist",
BPLIST_MAGIC_SIZE = 6,
BPLIST_VERSION = "00",
BPLIST_VERSION_SIZE = 2,
//BPLIST_TRL_SIZE = 26,
//BPLIST_TRL_OFFSIZE_IDX = 0,
//BPLIST_TRL_PARMSIZE_IDX = 1,
//BPLIST_TRL_NUMOBJ_IDX = 2,
//BPLIST_TRL_ROOTOBJ_IDX = 10,
//BPLIST_TRL_OFFTAB_IDX = 18,
//
BPLIST_TRL_SIZE = 32,
BPLIST_TRL_OFFSIZE_IDX = 6,
BPLIST_TRL_PARMSIZE_IDX = 7,
BPLIST_TRL_NUMOBJ_IDX = 8,
BPLIST_TRL_ROOTOBJ_IDX = 16,
BPLIST_TRL_OFFTAB_IDX = 24,
/* default buffer size */
BPLIST_BUFFER_MIN_SIZE = 256;
function BPlistBuffer(buffer, start, end) { // 构造函数 {{{
if (Buffer.isBuffer(buffer)) {
this.start = start;
this.end = end;
this.buffer = buffer;
} else {
this.start = 0;
this.end = Math.max((buffer || 0) << 1, BPLIST_BUFFER_MIN_SIZE);
this.buffer = new Buffer(this.end);
this.buffer.fill(0x00);
}
this.offset = this.start;
this.offsetSize = 0; //节点偏移地址字段大小
this.dictParamSize = 0; //索引字段大小
this.numObjects = 0; //节点总数
this.rootObject = 0; //根节点索引
this.offsetTableIndex = 0; //索引表偏移地址
this.objectIndex = 0;
this.type = null; //BPLIST_NULL;
this.size = 0; //BPLIST_NULL;
this.inited = false;
} // }}}
BPlistBuffer.prototype.initParam = function() { // 解析参数 {{{
if (this.inited) return;
var length = this.end - this.start,
offset = this.start,
trailer = this.end - BPLIST_TRL_SIZE;
if (length < BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE) {
error("Error size of plist.");
}
if (this.buffer.toString("ascii", offset, offset + BPLIST_MAGIC_SIZE) !== BPLIST_MAGIC) {
error("Not a plist format.");
}
offset += BPLIST_MAGIC_SIZE;
if (this.buffer.toString("ascii", offset, offset + BPLIST_VERSION_SIZE) !== BPLIST_VERSION) {
error("No support version.");
}
this.offsetSize = this.buffer[trailer + BPLIST_TRL_OFFSIZE_IDX];
this.dictParamSize = this.buffer[trailer + BPLIST_TRL_PARMSIZE_IDX];
this.numObjects = this.getUint(trailer + BPLIST_TRL_NUMOBJ_IDX, 8);
this.rootObject = this.getUint(trailer + BPLIST_TRL_ROOTOBJ_IDX, 8);
this.offsetTableIndex = this.getUint(trailer + BPLIST_TRL_OFFTAB_IDX, 8);
}; // }}}
BPlistBuffer.prototype.getUint = function(offset, size) { // 读取无符号整型 {{{
switch (size) {
case 1:
return this.buffer[offset];
case 2:
return this.buffer.readUInt16BE(offset);
case 4:
return this.buffer.readUInt32BE(offset);
case 8:
return this.buffer.readUInt32BE(offset) * 0x100000000 + this.buffer.readUInt32BE(offset + 4);
default:
error("Unknow uint size " + size);
}
}; // }}}
BPlistBuffer.prototype.next = function() { // 解析下一个节点 {{{
var tmp;
if (this.offset < this.start || this.offset >= this.end) return false;
tmp = this.buffer[this.offset];
this.type = tmp & BPLIST_MASK;
this.size = tmp & BPLIST_FILL;
this.offset++;
return true;
}; // }}}
BPlistBuffer.prototype.readSize = function() { // 得到扩展Size(大于15的) {{{
var size = this.size;
if (size == BPLIST_FILL) {
if (this.next() && this.type == BPLIST_UINT) {
size = this.readUint();
} else {
error("Can't get next size.");
}
}
return size;
}; // }}}
BPlistBuffer.prototype.readBoolean = function() { // 得到布尔类型 true false {{{
switch (this.size) {
case BPLIST_TRUE:
return true;
case BPLIST_FALSE:
return false;
default:
error("Error boolean value.");
}
}; // }}}
BPlistBuffer.prototype.readUint = function() { // 得到无符号整型 {{{
var offset = this.offset,
size = 1 << this.size;
this.offset += size;
return this.getUint(offset, size);
}; // }}}
BPlistBuffer.prototype.readReal = function() { // 得到 real 类型 {{{
var offset = this.offset,
size = 1 << this.size;
this.offset += size;
switch (size) {
case 4:
return this.buffer.readFloatBE(offset);
case 8:
return this.buffer.readDoubleBE(offset);
default:
error("Unknow real size " + size);
}
}; // }}}
BPlistBuffer.prototype.readDate = function() { // 得到 Date 类型 {{{
if (this.size != 3) error("Unknow date size " + this.size);
return new Date(this.readReal() * 1000 + BPLIST_TIME_START);
}; // }}}
BPlistBuffer.prototype.readData = function() { // 得到Data类型 {{{
var size = this.readSize(),
start = this.offset;
this.offset += size;
return this.buffer.slice(start, start + size);
}; // }}}
BPlistBuffer.prototype.readString = function() { // 得到字符串 {{{
var size = this.readSize(),
start = this.offset;
this.offset += size;
return this.buffer.toString("ascii", start, this.offset);
}; // }}}
BPlistBuffer.prototype.readUnicode = function() { // 得到Unicode字符串 {{{
var size = this.readSize(),
tmp, start, index;
size <<= 1;
tmp = new Buffer(size);
start = this.offset;
this.offset += size;
for (index = 0; index < size; index += 2) {
tmp.writeUInt16LE(this.buffer.readUInt16BE(start + index), index);
}
return tmp.toString("utf16le");
}; // }}}
BPlistBuffer.prototype.readList = function(size) { // 得到索引类型 {{{
var offset = this.offset,
ret = [];
while (size--) {
ret.push(this.getUint(offset, this.dictParamSize));
offset += this.dictParamSize;
}
this.offset = offset;
return ret;
};
// }}}
BPlistBuffer.prototype.readNode = function() { // 得到节点 {{{
if (!this.next()) return false;
var node;
switch (this.type) {
case BPLIST_BOOL:
return new BPlistPlainNode(BPLIST_BOOL, this.readBoolean());
case BPLIST_UINT:
return new BPlistPlainNode(BPLIST_UINT, this.readUint());
case BPLIST_REAL:
return new BPlistPlainNode(BPLIST_REAL, this.readReal());
case BPLIST_DATE:
return new BPlistDateNode(this.readDate());
case BPLIST_DATA:
return new BPlistDataNode(this.readData());
case BPLIST_STRING:
return new BPlistPlainNode(BPLIST_STRING, this.readString());
case BPLIST_UNICODE:
return new BPlistPlainNode(BPLIST_UNICODE, this.readUnicode());
case BPLIST_UID:
case BPLIST_ARRAY:
node = new BPlistArrayNode();
node.indexs = this.readList(this.readSize());
return node;
case BPLIST_SET:
case BPLIST_DICT:
node = new BPlistDictNode();
node.indexs = this.readList(this.readSize() << 1);
return node;
default:
error("Unknow type " + this.type);
}
}; // }}}
BPlistBuffer.prototype.nextNode = function() { // 得到下一个节点 {{{
if (this.objectIndex >= this.numObjects) return false;
var offsetTable = this.start + this.offsetTableIndex;
this.offset = this.start + this.getUint(offsetTable + this.objectIndex * this.offsetSize, this.offsetSize);
this.objectIndex++;
return this.readNode();
}; // }}}
BPlistBuffer.prototype.readPlist = function() { // 解析整个树并得到跟节点 {{{
var nodeslist, node, index, count;
this.initParam();
nodeslist = [];
while (node = this.nextNode()) {
nodeslist.push(node);
}
nodeslist.forEach(function(node) {
switch (node.type) {
case BPLIST_ARRAY:
for (index = 0, count = node.indexs.length; index < count; index++) {
node.push(nodeslist[node.indexs[index]]);
}
break;
case BPLIST_DICT:
for (index = 0, count = node.indexs.length >> 1; index < count; index++) {
node.push(
nodeslist[node.indexs[index]],
nodeslist[node.indexs[index + count]]);
}
break;
}
});
return nodeslist[this.rootObject] || false;
}; // }}}
BPlistBuffer.prototype.malloc = function(size) { // 申请空间 {{{
var buffer, used, length;
if (this.end - this.offset < size) {
used = this.offset - this.start;
length = (used + size) << 1;
buffer = new Buffer(length);
buffer.fill(0x00);
this.buffer.copy(buffer, 0, this.start, this.end);
this.start = 0;
this.end = length;
this.buffer = buffer;
this.offset = used;
}
}; // }}}
BPlistBuffer.prototype.getUintSize = function(value) { // 得到正整数Size {{{
return (value < 1 << 8 ? 1 : (value < 1 << 16 ? 2 : (value < 1 << 32 ? 4 : 8)));
}; // }}}
BPlistBuffer.prototype.setUint = function(offset, value, size) { // {{{ 写入一个整型
switch (size) {
case 1:
this.buffer[offset] = value;
break;
case 2:
this.buffer.writeUInt16BE(value, offset);
break;
case 4:
this.buffer.writeUInt32BE(value, offset);
break;
case 8:
this.buffer.writeUInt32BE(value / 0x100000000 | 0, offset);
this.buffer.writeUInt32BE(value | 0, offset + 4);
break;
default:
error("Error size of uint");
break;
}
return size;
}; // }}}
BPlistBuffer.prototype.writeHeader = function() { // 写入头部标识 {{{
this.malloc(BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE);
this.buffer.write(BPLIST_MAGIC, this.offset, BPLIST_MAGIC_SIZE, "ascii");
this.offset += BPLIST_MAGIC_SIZE;
this.buffer.write(BPLIST_VERSION, this.offset, BPLIST_VERSION_SIZE, "ascii");
this.offset += BPLIST_VERSION_SIZE;
}; // }}}
BPlistBuffer.prototype.writeOffsetTable = function(table) { // 写入索引表 {{{
var length = table.length,
size = this.offsetSize,
index = 0,
offset;
this.malloc(size * length);
offset = this.offset;
for (index = 0; index < length; index++) {
this.setUint(offset, table[index], size);
offset += size;
}
this.offset = offset;
}; // }}}
BPlistBuffer.prototype.writeTrailer = function() { // 写入尾部数据 {{{
var offset;
offset = this.offset;
this.malloc(BPLIST_TRL_SIZE);
this.setUint(offset + BPLIST_TRL_OFFSIZE_IDX, this.offsetSize, 1);
this.setUint(offset + BPLIST_TRL_PARMSIZE_IDX, this.dictParamSize, 1);
this.setUint(offset + BPLIST_TRL_NUMOBJ_IDX, this.numObjects, 8);
this.setUint(offset + BPLIST_TRL_ROOTOBJ_IDX, this.rootObject, 8);
this.setUint(offset + BPLIST_TRL_OFFTAB_IDX, this.offsetTableIndex, 8);
this.offset = offset + BPLIST_TRL_SIZE;
}; // }}}
BPlistBuffer.prototype.writeType = function(type) { // 写入type {{{
this.malloc(1);
type = (type & BPLIST_MASK) | (this.buffer[this.offset] & BPLIST_FILL);
this.buffer[this.offset] = type;
}; // }}}
BPlistBuffer.prototype.writeSize = function(size) { // 写入size {{{
this.malloc(1);
var offset = this.offset;
if (size < BPLIST_FILL) {
size = (size & BPLIST_FILL) | (this.buffer[offset] & BPLIST_MASK);
this.buffer[offset] = size;
this.offset++;
} else {
this.buffer[offset] = BPLIST_FILL | (this.buffer[offset] & BPLIST_MASK);
this.offset++;
this.writeType(BPLIST_UINT);
this.writeUint(size);
}
}; // }}}
BPlistBuffer.prototype.writeBoolean = function(value) { // 写入布尔节点 {{{
this.writeSize(value ? BPLIST_TRUE : BPLIST_FALSE);
}; // }}}
BPlistBuffer.prototype.writeUint = function(value) { // 写入正整数 {{{
var size, index;
size = this.getUintSize(value);
index = (size == 1 ? 0 : (size == 2 ? 1 : (size == 4 ? 2 : 3)));
this.writeSize(index);
this.malloc(size);
this.setUint(this.offset, value, size);
this.offset += size;
}; // }}}
BPlistBuffer.prototype.writeReal = function(value) { // 写入Real数 {{{
var //index = (value < 3.4028234663852886e+38 && value > -3.4028234663852886e+38) ? 2 : 3,
//TODO 修复判断逻辑
index = 3,
size = 1 << index;
this.writeSize(index);
this.malloc(size);
switch (size) {
case 4:
this.buffer.writeFloatBE(value, this.offset);
break;
case 8:
this.buffer.writeDoubleBE(value, this.offset);
break;
default:
error("Error size of real value.");
}
this.offset += size;
}; // }}}
BPlistBuffer.prototype.writeDate = function(value) { // 写入Date节点 {{{
var time = +value;
this.writeReal((time - BPLIST_TIME_START) / 1000);
}; // }}}
BPlistBuffer.prototype.writeData = function(value) { // 写入Data {{{
var length = value.length;
this.writeSize(length);
this.malloc(length);
value.copy(this.buffer, this.offset);
this.offset += length;
}; // }}}
BPlistBuffer.prototype.writeString = function(value) { // 写入String {{{
var length = value.length;
this.writeSize(length);
this.malloc(length);
this.buffer.write(value, this.offset, length, "ascii");
this.offset += length;
}; // }}}
BPlistBuffer.prototype.writeUnicode = function(value) { // 写入Unicode {{{
var length = value.length,
size = length << 1,
offset, index;
this.writeSize(length);
this.malloc(size);
offset = this.offset;
this.buffer.write(value, offset, size, "utf16le");
for (index = 0; index < length; index++, offset += 2) {
this.buffer.writeUInt16BE(this.buffer.readUInt16LE(offset), offset);
}
this.offset += size;
}; // }}}
BPlistBuffer.prototype.writeList = function(value) { // 写入列表 {{{
var count, size, index, length, offset;
count = value.length;
size = this.dictParamSize;
length = count * size;
this.malloc(length);
offset = this.offset;
for (index = 0; index < count; index++) {
this.setUint(offset, value[index], size);
offset += size;
}
this.offset = offset;
}; // }}}
BPlistBuffer.prototype.writeNode = function(node) { // 写入节点 {{{
this.writeType(node.type);
switch (node.type) {
case BPLIST_BOOL:
this.writeBoolean(node.value);
break;
case BPLIST_UINT:
this.writeUint(node.value);
break;
case BPLIST_REAL:
this.writeReal(node.value);
break;
case BPLIST_DATE:
this.writeDate(node.value);
break;
case BPLIST_DATA:
this.writeData(node.value);
break;
case BPLIST_STRING:
this.writeString(node.value);
break;
case BPLIST_UNICODE:
this.writeUnicode(node.value);
break;
case BPLIST_UID:
case BPLIST_ARRAY:
this.writeSize(node.indexs.length);
this.writeList(node.indexs);
break;
case BPLIST_SET:
case BPLIST_DICT:
this.writeSize(node.indexs.length >> 1);
this.writeList(node.indexs);
break;
default:
error("Unknow node type.");
}
}; // }}}
BPlistBuffer.prototype.serializeNode = function(node, array, set) { // 找到所有节点,并且存储在数组中 {{{
var hash = node.hash(),
index, list, keys, values, count, i, kv;
set = set || {}; //用于储存节点索引
if (set.hasOwnProperty(hash)) return set[hash];
index = array.push(node) - 1;
set[hash] = index;
switch (node.type) {
case BPLIST_BOOL:
case BPLIST_UINT:
case BPLIST_REAL:
case BPLIST_DATE:
case BPLIST_DATA:
case BPLIST_STRING:
case BPLIST_UNICODE:
break;
case BPLIST_UID:
case BPLIST_ARRAY:
count = node.value.length;
list = [];
for (i = 0; i < count; i++) {
list.push(this.serializeNode(node.value[i], array, set));
}
node.indexs = list;
break;
case BPLIST_SET:
case BPLIST_DICT:
count = node.value.length;
keys = [];
values = [];
for (i = 0; i < count; i++) {
kv = node.value[i];
keys.push(this.serializeNode(kv[0], array, set));
values.push(this.serializeNode(kv[1], array, set));
}
node.indexs = keys.concat(values);
break;
default:
error("Unknow node type.");
}
return index;
}; // }}}
BPlistBuffer.prototype.writePlist = function(rootNode) { // 写入整个Plist结构 {{{
var nodeslist = [],
offsetTable = [],
count, index;
this.rootObject = this.serializeNode(rootNode, nodeslist);
this.numObjects = count = nodeslist.length;
this.dictParamSize = this.getUintSize(count);
this.writeHeader();
for (index = 0; index < count; index++) {
offsetTable.push(this.offset - this.start);
this.writeNode(nodeslist[index]);
}
this.offsetTableIndex = this.offset - this.start;
this.offsetSize = this.getUintSize(this.offsetTableIndex);
this.writeOffsetTable(offsetTable);
this.writeTrailer();
return this.buffer.slice(this.start, this.offset);
}; // }}}
// }}}
module.exports = (function() {
var toString = Object.prototype.toString;
function fromBuffer(data, start, end) { // 解析bplist {{{
var buffer, root;
if (start === undefined) {
start = 0;
}
if (end === undefined) {
end = start + data.length;
}
buffer = new BPlistBuffer(data, start, end);
root = buffer.readPlist();
return root;
} // }}}
function toBuffer(rootNode) { // 构建 bplist {{{
var buffer = new BPlistBuffer(),
data = buffer.writePlist(rootNode);
return data;
} // }}}
function parsePString(str) { // 解析PString类型 {{{
var i = str.indexOf(":"),
parser, type, value;
if (i < 1) error("Error PString:\"" + str + "\"");
type = str.substring(0, i);
value = str.substring(i + 1);
switch (type) {
case "bool":
return new BPlistPlainNode(BPLIST_BOOL, value == "true");
case "uint":
return new BPlistPlainNode(BPLIST_UINT, parseInt(value));
case "real":
return new BPlistPlainNode(BPLIST_REAL, parseFloat(value));
case "date":
return new BPlistDateNode(new Date(parseInt(value)));
case "data":
return new BPlistDataNode(new Buffer(value, "base64"));
case "string":
return new BPlistPlainNode(BPLIST_STRING, value);
case "unicode":
return new BPlistPlainNode(BPLIST_UNICODE, value);
default:
error("Unknow type \"" + type + "\"");
}
} // }}}
function fromPObject(obj) { // {{{ 解析PObject类型
var type, ret, key;
type = toString.call(obj);
switch (type) {
case "[object String]":
return parsePString(obj);
case "[object Array]":
ret = new BPlistArrayNode();
obj.forEach(function(item) {
ret.push(fromPObject(item));
});
return ret;
case "[object Object]":
ret = new BPlistDictNode();
for (key in obj) {
ret.push(new BPlistPlainNode(BPLIST_STRING, key), fromPObject(obj[key]));
}
return ret;
default:
error("Unknow PObject type: " + type);
}
}
// }}}
function toPObject(node) { // 转化为PObject {{{
var ret, count,
index, item, key, value;
switch (node.type) {
case BPLIST_BOOL:
return "bool:" + String(node.value);
case BPLIST_UINT:
return "uint:" + String(node.value);
case BPLIST_REAL:
return "real:" + String(node.value);
case BPLIST_DATE:
return "date:" + String(node.value.getTime());
case BPLIST_DATA:
return "data:" + node.value.toString("base64");
case BPLIST_STRING:
return "string:" + String(node.value);
case BPLIST_UNICODE:
return "unicode:" + String(node.value);
case BPLIST_UID:
case BPLIST_ARRAY:
ret = [];
count = node.value.length;
while (count--) {
ret.unshift(toPObject(node.value[count]));
}
return ret;
case BPLIST_SET:
case BPLIST_DICT:
ret = {};
count = node.value.length;
for (index = 0; index < count; index++) {
item = node.value[index];
key = item[0].value;
value = toPObject(item[1]);
ret[key] = value;
}
return ret;
default:
error();
}
} // }}}
function toObject(node) { // {{{ 转化为Object
var ret, count,
index, item, key, value;
switch (node.type) {
case BPLIST_BOOL:
case BPLIST_UINT:
case BPLIST_REAL:
case BPLIST_DATE:
case BPLIST_DATA:
case BPLIST_STRING:
case BPLIST_UNICODE:
return node.value;
case BPLIST_UID:
case BPLIST_ARRAY:
ret = [];
count = node.value.length;
while (count--) {
ret.unshift(toObject(node.value[count]));
}
return ret;
case BPLIST_SET:
case BPLIST_DICT:
ret = {};
count = node.value.length;
for (index = 0; index < count; index++) {
item = node.value[index];
key = item[0].value;
value = toObject(item[1]);
ret[key] = value;
}
return ret;
default:
error();
}
} // }}}
return {
fromBuffer: fromBuffer,
toBuffer: toBuffer,
fromPObject: fromPObject,
toPObject: toPObject,
toObject: toObject
};
})();
// vim600: sw=4 ts=4 fdm=marker syn=javascript