-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·231 lines (202 loc) · 5.01 KB
/
index.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
function isInt(n) {
return typeof n === 'number' && parseFloat(n) == parseInt(n, 10) && !isNaN(n);
}
function getstrlenght(str){
var l = 0;
var a = str.split("");
for (var i=0;i<a.length;i++) {
if (a[i].charCodeAt(0)<299) {
l++;
} else {
//TODO:WTF ? Why it is 3 for Chinese char
l+=3;
}
}
return l;
}
module.exports = {
serialize : function(){
if ( arguments.length != 1 ){
throw '.serialize() expects 1 parameter. '+ arguments.length +' given';
}
var item = arguments[0];
var val = null;
switch ( typeof(item) ) {
case 'object' :
if (item === null) {
val = 'N;';
} else if (item.prototype){
val = 'o:';
} else {
var isArray = (item instanceof Array);
var length = 0;
var objval = '{';
for (var key in item){
if (item.hasOwnProperty(key)){
length++;
if (isArray) key = parseInt(key, 10);
objval += this.serialize(key);
objval += this.serialize(item[key]);
}
}
objval += '}';
val = 'a:'+length+':'+objval;
}
break;
case 'string' :
val = 's:'+ getstrlenght(item)+':"'+item+'";';
break;
case 'number' :
if (isInt(item)){
//TODO create tests to deal with integer limits on JS and PHP
val = 'i:'+item+';';
} else {
val = 'd:'+item+';';
}
break;
case 'boolean' :
val = 'b:'+ ((item) ? 1 : 0) +';';
break;
case 'undefined' :
val = 'N;';
break;
}
return val;
},
deserialize: function(phpstr) {
if(!phpstr){
return null;
}
var idx = 0
, rstack = []
, ridx = 0
, readLength = function () {
var del = phpstr.indexOf(':', idx)
, val = phpstr.substring(idx, del);
idx = del + 2;
return parseInt(val);
} //end readLength
, parseAsInt = function () {
var del = phpstr.indexOf(';', idx)
, val = phpstr.substring(idx, del);
idx = del + 1;
return parseInt(val);
} //end parseAsInt
, parseAsFloat = function () {
var del = phpstr.indexOf(';', idx)
, val = phpstr.substring(idx, del);
idx = del + 1;
return parseFloat(val);
} //end parseAsFloat
, parseAsBoolean = function () {
var del = phpstr.indexOf(';', idx)
, val = phpstr.substring(idx, del);
idx = del + 1;
return ("1" === val)? true: false;
} //end parseAsBoolean
, parseAsString = function () {
var len = readLength()
, utfLen = 0
, bytes = 0
, ch
, val;
while (bytes < len) {
ch = phpstr.charCodeAt(idx + utfLen++);
if (ch <= 0x007F) {
bytes++;
} else if (ch > 0x07FF) {
bytes += 3;
} else {
bytes += 2;
}
}
val = phpstr.substring(idx, idx + utfLen);
idx += utfLen + 2;
return val;
} //end parseAsString
, parseAsArray = function () {
var len = readLength()
, resultArray = []
, resultHash = {}
, keep = resultArray
, lref = ridx++
, key
, val;
rstack[lref] = keep;
for (var i = 0; i < len; i++) {
key = parseNext();
val = parseNext();
if (keep === resultArray && parseInt(key) == i) {
// store in array version
resultArray.push(val);
} else {
if (keep !== resultHash) {
// found first non-sequential numeric key
// convert existing data to hash
for (var j = 0, alen = resultArray.length; j < alen; j++) {
resultHash[j] = resultArray[j];
}
keep = resultHash;
rstack[lref] = keep;
}
resultHash[key] = val;
} //end if
} //end for
idx++;
return keep;
} //end parseAsArray
, parseAsObject = function () {
var len = readLength()
, obj = {}
, lref = ridx++
, clazzname = phpstr.substring(idx, idx + len)
, re_strip = new RegExp("^\u0000(\\*|" + clazzname + ")\u0000")
, key
, val;
rstack[lref] = obj;
idx += len + 2;
len = readLength();
for (var i = 0; i < len; i++) {
key = parseNext();
// private members start with "\u0000CLASSNAME\u0000"
// protected members start with "\u0000*\u0000"
// we will strip these prefixes
key = key.replace(re_strip, '');
val = parseNext();
obj[key] = val;
}
idx++;
return obj;
} //end parseAsObject
, parseAsRef = function () {
var ref = parseAsInt();
// php's ref counter is 1-based; our stack is 0-based.
return rstack[ref - 1];
} //end parseAsRef
, readType = function () {
var type = phpstr.charAt(idx);
idx += 2;
return type;
} //end readType
, parseNext = function () {
var type = readType();
switch (type) {
case 'i': return parseAsInt();
case 'd': return parseAsFloat();
case 'b': return parseAsBoolean();
case 's': return parseAsString();
case 'a': return parseAsArray();
case 'O': return parseAsObject();
case 'r': return parseAsRef();
case 'R': return parseAsRef();
case 'N': return null;
default:
throw {
name: "Parse Error",
message: "Unknown type' " + type + " 'at postion " + (idx - 2)
};
} //end switch
}; //end parseNext
return parseNext();
}
};