-
Notifications
You must be signed in to change notification settings - Fork 10
/
util.js
206 lines (160 loc) · 5.12 KB
/
util.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
var base64 = require('base64'),
xml2js = require('xml2js'),
gzip = require('gzip');
module.exports.crypto = {
key: undefined,
crypto: require("crypto"),
init: function(key) {
this.key = key;
if(typeof(this.key) != 'string') {
this.key = this.key.toString();
}
},
cipher: function(data, key, saltSize, compressCb) {
if (saltSize === undefined) saltSize = 32 * 6;
var salt = module.exports.randomString(saltSize),
cipher = this.crypto.createCipher('aes-256-cbc', salt + '$' + ((typeof key === 'string') ? key : this.key));
if(compressCb) {
gzip(data, function(err, buf) {
compressCb(buf.length + ":" + salt + ":" + base64.encode(new Buffer(cipher.update(buf, 'binary', 'binary') + cipher.final('binary'), 'binary')));
});
} else {
var buf = new Buffer(data, 'utf8');
return buf.length + ":" + salt + ":" + base64.encode(new Buffer(cipher.update(buf, 'binary', 'binary') + cipher.final('binary'), 'binary'));
}
},
decipher: function(data, key) {
var sd = data.split(':'),
len = parseInt(sd[0]),
target = new Buffer(len),
salt = sd[1],
decipher = this.crypto.createDecipher('aes-256-cbc', salt + '$' + ((typeof key === 'string') ? key : this.key)),
first = new Buffer(decipher.update(base64.decode(sd[2]), 'binary', 'binary'), 'binary'),
end = new Buffer(decipher.final('binary'), 'binary');
first.copy(target);
end.copy(target, first.length, 0, len - first.length);
return target.toString('utf8');
}
};
module.exports.randomString = function(bits) {
var rand, i;
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/';
var ret = '';
// in v8, Math.random() yields 32 pseudo-random bits (in spidermonkey it gives 53)
while(bits > 0) {
rand = Math.floor(Math.random()*0x100000000); // 32-bit integer
// base 64 means 6 bits per character, so we use the top 30 bits from rand to give 30/6=5 characters.
for(i = 26; i > 0 && bits > 0; i -= 6, bits -= 6) {
ret += chars[0x3F & rand >>> i];
}
}
return ret;
};
var XmlStream = module.exports.XmlStream = function() {
this.good = "";
this.remaining = "";
this.ptr = 0;
this.state = "text";
this.nesting = 0;
this.tag = /<(\/?)\s*([^ >/]+)[^>]*?(\/?)>/g;
this.utf = "";
};
XmlStream.prototype = new process.EventEmitter();
XmlStream.prototype.update = function(buf, offset, len) {
if(typeof offset == 'undefined') offset = 0;
if(typeof len == 'undefined') len = buf.length - offset;
if(len <= 0) return;
var nextUtf = "";
if((buf[offset+len-1] & 0x80) != 0) {
// oh oh, the last character may be an incomplete UTF8 character.
var origLen = len;
var utfCharStart = offset + (--len);
while(utfCharStart >= offset && (buf[utfCharStart] & 0xC0) == 0x80) {
utfCharStart--;
len--;
}
if(utfCharStart < offset) {
this.utf += buf.slice(offset, origLen).toString('binary');
return;
} else {
nextUtf = buf.slice(utfCharStart, origLen).toString('binary');
}
}
if(this.utf.length == 0) {
this.remaining += buf.slice(offset, offset + len).toString('utf8');
} else {
var newBuf = new Buffer(this.utf.length + len);
newBuf.write(this.utf, 0, 'binary');
buf.copy(newBuf, this.utf.length, offset, offset + len);
this.remaining += newBuf.toString('utf8');
}
this.utf = nextUtf;
var m;
var begin = 0;
var last = 0;
var self = this;
while ((m = this.tag.exec(this.remaining)) != null) {
last = this.tag.lastIndex;
if(m[2] == 'stream:stream') {
self.emit('data', {'stream:stream': {}});
begin = last;
continue;
} if(m[1] == '/') {
this.nesting--;
} else if(m[3] != '/') {
this.nesting++;
}
if(this.nesting == 0) {
var parser = new xml2js.Parser();
parser.addListener('end', function(result) {
self.emit('data', result);
});
parser.parseString('<x>' + this.good + this.remaining.substring(begin, last) + '</x>');
begin = last;
this.good = '';
}
}
this.good += this.remaining.substring(begin, last);
this.remaining = this.remaining.substring(last);
};
module.exports.json
module.exports.xmlify = function(tag, json) {
if(typeof(json) == 'object' && json.length) {
var xml = "";
for(var i = 0; i < json.length; i++) {
xml += module.exports.xmlify(tag, json[i]);
}
return xml;
} else if(tag == '#') {
return module.exports.xmlEscape(json);
} else if(!json) {
return '';
} else if(typeof(json) == 'string') {
return "<" + tag + ">" + module.exports.xmlEscape(json) + "</" + tag + ">";
}
var xml = "<" + tag;
if(json['@']) {
for(var key in json['@']) {
if(!json['@'].hasOwnProperty(key)) continue;
xml += " " + key + "='" + module.exports.xmlEscape(json['@'][key]) + "'";
}
}
var children = "";
for(var key in json) {
if(key == '@' || !json.hasOwnProperty(key)) continue;
children += module.exports.xmlify(key, json[key]);
}
if(children.length == 0) {
xml += '/>';
} else {
xml += '>' + children + '</' + tag + '>';
}
return xml;
};
module.exports.xmlEscape = function(str) {
return str.replace(/&/g, '&')
.replace(/'/g, ''')
.replace(/"/g, '"')
.replace(/</g, '<')
.replace(/>/g, '>');
}