-
Notifications
You must be signed in to change notification settings - Fork 2
/
plist.js
162 lines (149 loc) · 4.56 KB
/
plist.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
function Parser(data) {
this.data = data;
this.ofs = 0;
this.length = data.length;
this.pos = function() { return this.ofs }
this.get_int = function(intsize) {
value = 0;
while(intsize--)
value = (value<<8) + this.data.charCodeAt(this.ofs++);
return value;
}
this.get_str = function() {
length = this.get_int(2);
if(length==0xffff)
return '';
value = this.data.substr(this.ofs,length)
this.ofs += length;
return value;
}
this.skip_str = function(numstrs) {
while(numstrs--) {
length = this.get_int(2);
if(length<0xffff)
this.ofs += length;
}
}
this.skip = function(size) {
this.ofs += size;
}
this.bytes = function(size) {
value = this.data.substr(this.ofs,size);
this.ofs += size;
return value;
}
this.octet = function(size) {
value = this.bytes(size);
value = value.split('')
value = $.map(value, function(x) { return x.charCodeAt(0) })
while(value.length < size) {
value.push(0)
}
return value;
}
this.varint = function() {
segments = [];
count = 0
msb = 255;
while((msb & 128) == 128 && count < 10) {
count = count + 1;
msb = this.get_int(1);
segments.push(msb)
//break;
}
i = $.map(segments, function(t) { return $.strPad(t.toString(2),8,0).substr(1,7) }).join('')
return parseInt(i,2);
}
this.more = function() {
return this.ofs < this.length;
}
this.seek = function(ofs) {
this.ofs = ofs;
}
}
read_column = function(x, payload) {
if(x==0) { return '' }
if(x==1) { return payload.get_int(1) }
if(x==2) { return payload.get_int(2) }
if(x==3) { return payload.get_int(3) }
if(x==4) { return payload.get_int(4) }
if(x==7) { return PACK.Unpack('d', payload.octet(8), 0) }
if(x > 11 && x%2==0) { return payload.bytes((x-12)/2) }
if(x > 12 && x%2==1) { return payload.bytes((x-13)/2) }
console.log('unknown column of type ' + x)
}
function load_loc_db(reader) {
return function(e) {
var data = new Parser(reader.result);
lilog('db is '+data.length+' bytes');
var magic = data.bytes(15);
lilog('magic is '+magic);
if(("SQLite format 3"!=magic)||(0!=data.bytes(1).charCodeAt(0)))
throw "location db has bad magic ("+magic+")";
var page_size = data.get_int(2)*256;
lilog(page_size);
data.seek(100);
}
}
function load_mbdx(reader,files,target) {
return function(e) {
var data = new Parser(reader.result);
if("mbdx" != data.bytes(4))
throw "manifest MBDX has bad magic";
data.skip(6);
var output = [];
var file = null;
while(data.more()) {
var fileID = "";
for(var i=0; i<20; i++) {
var hex = data.bytes(1).charCodeAt(0).toString(16);
while(hex.length < 2)
hex = '0'+hex;
fileID += hex;
}
if(target == data.get_int(4)+6) {
lilog('location db is in '+fileID);
file = files[fileID];
break;
}
data.skip(2);
}
if(file == null)
throw "Cannot resolve Library/Caches/locationd/consolidated.db";
db = new FileReader();
db.onload = SQR.loadfile(db);
db.onerror = function(e) { alert(e) }
db.readAsBinaryString(file);
lilog('extracting location info; please wait...')
}
}
function load_mbdb(reader,files) {
return function(evt) {
var data = new Parser(evt.target.result);
lilog('manifest is '+ data.length+' bytes');
if("mbdb" != data.bytes(4))
throw "manifest MBDB has bad magic";
data.skip(2);
var locationd;
while(data.more()) {
var start = data.ofs;
data.skip_str(1);
if("Library/Caches/locationd/consolidated.db" == data.get_str()) {
locationd = start;
break;
}
data.skip_str(3);
data.skip(4*9+3);
var num_props = data.get_int(1);
for(var i=0; i<num_props; i++) {
data.skip_str(2);
}
}
if(locationd == null)
throw "Library/Caches/locationd/consolidated.db is not in index"
var mbdx = new FileReader();
mbdx.onloadend = load_mbdx(mbdx,files,locationd);
mbdx.onerror = function(e) { alert(e) }
mbdx.readAsBinaryString(files["Manifest.mbdx"]);
}
}