This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
tamper.js
196 lines (185 loc) · 5.72 KB
/
tamper.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
/* ---- begin test harness ---- */
if (typeof module !== 'undefined' && module.exports) {
var atob = require("../test/node_modules/atob"),
_ = require("../test/node_modules/underscore");
}
/* ---- end test harness ---- */
Tamper = {}
Tamper.biterate = function(encoded){
var binary = atob(encoded),
length = binary.length,
i = 0,
output = [];
while(i < length){
var b = binary.charCodeAt(i),
j = 0;
while(j < 8 ){
output.push((b >> (7-j)) & 1);
j++;
}
i++;
}
return output;
}
Tamper.unpackExistence = function(element,default_attrs){
if(typeof(callback) === "undefined"){callback = false}
var bitArray = Tamper.biterate(element.pack),
length = bitArray.length,
output = [],
counter = 0,
consumeCC = function(array){
if(array.length < 2){throw "Improperly formatted bit array"}
var cc = array.splice(0,8);
cc = parseInt(cc.join(""),2)
return cc;
},
consumeNum = function(n,array){
var num = parseInt(array.splice(0,n).join(""),2);
return num;
},
consumeChunk = function(bytes,bits,array){
var num_bits = bytes*8 + bits,
chunk = array.splice(0,bytes*8 + bits),
i = 0;
while(i < num_bits){
if (!! chunk[i]){
default_attrs = _.clone(default_attrs);
output.push(_.extend(default_attrs,{guid: counter}));
}
counter++;
i++;
}
},
processBitArray = function(array){
if(array.length === 0){
return output;
}
var cc = consumeCC(array);
if (cc == 0){
var bytes_to_consume = consumeNum(32,array),
bits_to_consume = consumeNum(8,array);
consumeChunk(bytes_to_consume,bits_to_consume,array);
if(bits_to_consume > 0){
array.splice(0,8 - bits_to_consume)
}
return processBitArray(array);
} else if (cc == 1){
var num_to_skip = consumeNum(32,array);
counter += num_to_skip;
return processBitArray(array);
} else if (cc == 2){
var num_to_run = consumeNum(32,array);
for (var i = 0;i < num_to_run;i++){
default_attrs = _.clone(default_attrs);
output.push(_.extend(default_attrs,{guid: counter}));
counter = counter + 1;
}
return processBitArray(array);
} else {
throw "Unrecognized control code: " + cc;
}
};
var ba = processBitArray(bitArray);
return ba;
}
Tamper.unpackIntegerEncoding = function(element, num_items) {
if(typeof(callback) === "undefined"){callback = false}
var consumeNum = function(array,n){
var num = parseInt(array.splice(0,n).join(""),2);
return num;
}
var bitArray = Tamper.biterate(element.pack),
bit_window_width = element.bit_window_width,
item_window_width = element.item_window_width,
item_chunks = item_window_width/bit_window_width;
bytes_to_consume = consumeNum(bitArray,32),
bits_to_consume = consumeNum(bitArray,8);
var getPossibility = function(i){
if(i === 0){
return null
} else {
return element.possibilities[i - 1]
}
}
var i = 0,output = [];
while(i < num_items) {
bit_window = bitArray.slice(i * item_window_width, (i * item_window_width) + item_window_width);
var j = 0;
while(j < item_chunks){
var choice = bit_window.slice(j*bit_window_width,(j * bit_window_width) + bit_window_width);
possibility_id = parseInt(choice.join(''), 2);
if (! output[i]) {output[i] = []}
var result = getPossibility(possibility_id);
if(item_chunks == 1){
output[i] = result
} else if(result) {
output[i].push(result);
}
j++;
}
i++;
}
return output;
}
Tamper.unpackBitmapEncoding = function(element){
var consumeNum = function(array,n){
var num = parseInt(array.splice(0,n).join(""),2);
return num;
}
var bitArray = Tamper.biterate(element.pack),
item_window_width = element.item_window_width,
chunks = bitArray.length / item_window_width,
bytes_to_consume = consumeNum(bitArray,32),
bits_to_consum = consumeNum(bitArray,8);
var i = 0,output = [];
while(i < chunks) {
bit_window = bitArray.slice(i * item_window_width, (i * item_window_width) + item_window_width);
if (! output[i]) {output[i] = []}
j = 0;
while (j < item_window_width){
if(bit_window[j] == "1"){
output[i].push(element.possibilities[j]);
}
j++;
}
i++;
}
return output;
}
Tamper.unpackData = function(data,default_attrs){
if(typeof(default_attrs) === "undefined"){var default_attrs = {}}
if(data.existence){
var exists = Tamper.unpackExistence(data.existence,default_attrs),collection;
_(data.attributes).each(function(a){
switch(a.encoding) {
case 'bitmap':
var attr_array = Tamper.unpackBitmapEncoding(a);
break;
case 'integer':
var attr_array = Tamper.unpackIntegerEncoding(a, exists.length);
break;
default: // attr does not contain a pack or is an unknown encoding
return;
}
_(exists).each(function(s,i){s[a.attr_name] = attr_array[i]});
})
return exists;
} else {
seeds = _(data.collection).map(function(i){var default_attrs = _.clone(default_attrs);return _(default_attrs).extend(i)});
return seeds;
}
}
if(typeof exports !== "undefined"){
exports.Tamper = function(){
return Tamper;
}
} else {
if (typeof define === 'function' && define.amd) {
define(['underscore'], function(_) {
return Tamper;
});
}
else {
window.Tamper = Tamper;
}
}