decode eval function in javascript? - javascript

I got this a function eval in javascript - http://pastebin.com/E1PXQeKj
but i don't know how read it? how does this generate or decode the string?
or simply how is this code?
thanks for help!

Paste it in http://jsbeautifier.org/ and it reveals:
var secret = 'dDGSUW1QU01JaVZNTWFFN0pWcm2RZkE6MD';
To do it manually, find the end of the eval function and replace eval by alert or whatever debugging function you use. Line 2 can be handled in that way.
Line 3 formatted looks like:
var _9581
;
var _8438 = '196E84D180D984E928C920F980E928D988C652F768E652F680B924F808E924A724E916A872C1000A720A848E872F868D980E864C796E940A724B912F724B732B724B924C876C956D836C912C864A804E1008E840C868A800A740A832E848B680D760F';
var _5668 = /[\x41\x42\x43\x44\x45\x46]/;
var _9413 = 2;
var _9565 = _8438.charAt(_8438.length - 1);
var _5032;
var _9978 = _8438.split(_5668);
var _4678 = [String.fromCharCode, isNaN, parseInt, String];
_9978[1] = _4678[_9413 + 1](_4678[_9413](_9978[1]) / 21);
var _6432 = (_9413 == 7) ? String : eval;
_5032 = '';
_11 = _4678[_9413](_9978[0]) / _4678[_9413](_9978[1]);
for (_9581 = 3; _9581 < _11; _9581++) _5032 += (_4678[_9413 - 2]((_4678[_9413](_9978[_9581]) + _4678[_9413](_9978[2]) + _4678[_9413](_9978[1])) / _4678[_9413](_9978[1]) - _4678[_9413](_9978[2]) + _4678[_9413](_9978[1]) - 1));
_6432(_5032);
Now analyse it yourself. Care should be taken when something is executeable. Constructs like foo(bar).
Decoding in progress:
var _9581;
var _8438 = '196E84D180D984E928C920F980E928D988C652F768E652F680B924F808E924A724E916A872C1000A720A848E872F868D980E864C796E940A724B912F724B732B724B924C876C956D836C912C864A804E1008E840C868A800A740A832E848B680D760F';
var _5668 = /[ABCDEF]/;
var _9413 = 2;
var _9565 = "F";
var _5032;
var _9978 = [196, 84, 180, 984, 928, 920, 980, 928, 988, 652, 768, 652, 680, 924, 808, 924, 724, 916, 872, 1000, 720, 848, 872, 868, 980, 864, 796, 940, 724, 912, 724, 732, 724, 924, 876, 956, 836, 912, 864, 804, 1008, 840, 868, 800, 740, 832, 848, 680, 760, ];
var _4678 = [String.fromCharCode, isNaN, parseInt, String];
_9978[1] = "4"; //String(parseInt(84) / 21)
var _6432 = eval;
_5032 = '';
_11 = 49; //parseInt(196) / parseInt(4);
for (_9581 = 3; _9581 < _11; _9581++) {
//_5032 += (String.fromCharCode((parseInt(_9978[_9581]) + parseInt(180) + parseInt(4)) / parseInt(4) - parseInt(180) + parseInt(4) - 1));
_5032 += String.fromCharCode((parseInt(_9978[_9581]) + 184) / 4 - 177);
}
// so from here one, we can safely assume that the code is NOT executable
//_6432(_5032);
console.log(_5032);
Yields:
secret = 'dGd3bWw1QWVrUDh2a412dXl2aUFyOVE6MQ';
Conclusion:
The first packer is to confuse people who manage to decode it.
The second code actually changes the secret variable

Here is unpacker for code encoded in such a way http://www.strictly-software.com/unpacker. It seems the only thing this code do is:
ver secret = 'dGd3bWw1QWVrUDh1a242dXlNaUFyOVE6MQ';

Related

I want to get the array count on based of few conditions

I have the following result from an API
data: {
"total_slots": int,
"occupied_count": int,
"occupied_slots" : [int] e.g.[452,453,459] (here each int in the array signfies an occupied slot),
"slots_down" : [int] e.g.[460,462] (here each int in the array signfies a down slot)
}
I want the following conditions
VAR OCCUPIED, length of the list which are occupied minus length of common slots in occupied and slots_down
VAR TOTAL_SLOTS = total slots (which are 31 i think) - slots which are down
VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
The slots are 31 that are fixed.
var ALL_SLOTS = [452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523];
How can I meet the OR/AND conditions like VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
This is my CURRENT CODE
s = JSON.parse(s);
console.log(s);
SLOTS_DOWN = s.slots_down;
var total_slots = s.total_slots;
SLOTS_DOWN = s.slots_down.length;
console.log('down slots are ' + SLOTS_DOWN);
total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN);
var occupied = s.occupied_count;
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN);
According to these:
VAR OCCUPIED, length of the list which are occupied minus length of common
slots in occupied and slots_down
VAR TOTAL_SLOTS = total slots (which are 31 i think) - slots which are down
VAR AVAILABLE = (31 - length(slots_down)) - length( slots occupied AND not down)
you could try this:
var OCCUPIED = data.occupied_count -
data.occupied_slots.filter(function(elem){
return data.slots_down.indexOf(elem)>-1;
}).length;
var TOTAL_SLOTS = data.total_slots.length - data.slots_down.length;
var AVAILABLE = ((data.total_slots.length - data.slots_down.length)) -
data.occupied_slots.filter(function(elem){
return data.slots_down.indexOf(elem)==-1;
}).length
This should work:
data = {
"total_slots": 124,
"occupied_count": 3,
"occupied_slots" : [245, 326, 256],
"slots_down" : [245, 136]
}
var down_but_not_occ = 0;
data.slots_down.map(function(v, i){
if(data.occupied_slots.indexOf(v) === -1){
down_but_not_occ ++;
};
})
var available_slots = parseInt(data.total_slots) - data.occupied_slots.length - down_but_not_occ;
console.log(available_slots);
Where you were wrong,
total_slots = parseInt(total_slots) - parseInt(SLOTS_DOWN); // slots down subtracted
var occupied = s.occupied_count;
var available = parseInt(total_slots) - parseInt(occupied) - parseInt(SLOTS_DOWN); // slots down AGAIN subtracted

Redis - Iterate through bitmap to get set bits

I'm trying to iterate through a Redis bitmap and get the offset of all set bits.
I tried several methods but all of them have failed, here is a snipped I got from Redis Essentials book by Maxwell. I went through the code several times, still unable to figure out whats going wrong.
var bitmap = 'test';
redisClient.get(bitmap, function(err, bitmapValue){
var userIds = [];
var data = bitmapValue.toJSON().data;
data.forEach(function(byte, byteIndex){
for(var bitIndex = 7; bitIndex >= 0; bitIndex--) {
var visited = byte >> bitIndex & 1;
}
if(visited === 1) {
var userId = byteIndex * 8 + (7 - bitIndex);
userIds.push(userId);
}
})
console.log(userIds);
});
Most of the time I get empty result, usually when the bitmap is small. When I try large bitmaps I get array full of multiples of 8. A sample result I got is shown below, which is incorrect, the bitcount of that bitmap is 690, while I got only 91.
[8,
16,
32,
144,
176,
256,
320,
440,
456,
520,
584,
592,
624,
640,
648,
680,
696,
704,
712,
720,
752,
760,
848,
1056,
1088,
1104,
1112,
1120,
1136,
1144,
1160,
1168,
1224,
1248,
1264,
1280,
1312,
1328,
1424,
1480,
1496,
1544,
1600,
1680,
1792,
1824,
1840,
1904,
1976,
2080,
2112,
2144,
2224,
2240,
2296,
2392,
2400,
2504,
2512,
2584,
2616,
2712,
2856,
2880,
2912,
2976,
3048,
3288,
3328,
3352,
3360,
3368,
3376,
3432,
3440,
3496,
3600,
3616,
3624,
3632,
3704,
3752,
3776,
3832,
3880,
3936,
4056,
4264,
4296,
4352,
4376]
I don't have the bitmap you've stored under the key test, so testing is difficult. However, reading the code it looks like the if statement is placed wrongly - it should be:
var bitmap = 'test';
redisClient.get(bitmap, function(err, bitmapValue){
var userIds = [];
var data = bitmapValue.toJSON().data;
data.forEach(function(byte, byteIndex){
for(var bitIndex = 7; bitIndex >= 0; bitIndex--) {
var visited = byte >> bitIndex & 1;
if(visited === 1) {
var userId = byteIndex * 8 + (7 - bitIndex);
userIds.push(userId);
}
}
})
console.log(userIds);
});

Read a Int64 in an ArrayBuffer using DataView

I am using Web Sockets.
I am passing an ArrayBuffer to my JavaScript page.
I have this in C# code:
byte[] packet = new byte[2];
packet[0] = (byte)1;
packet[1] = (byte)0;
byte[] tickArray = BitConverter.GetBytes( 635744635349556838 );
byte[] packet2 = new byte[2 + tickArray.Length];
Buffer.BlockCopy(packet, 0, packet2, 0, packet.Length);
Buffer.BlockCopy(tickArray, 0, packet2, packet.Length, tickArray.Length);
In my JavaScript Client I have this:
var dv = new DataView(e.data);
var marker = dv.getInt8(0);
var tripped = dv.getInt8(1);
var x = dv.getInt8(2);
I get the results:
1
0
-29
if change to this:
var x= dv.getInt16(1);
-7389
How do I get my value of 635744635349556838?
Thanks
I wrote a code. Would be it helpful?
function getUint64(bytes, littleEndian)
{
var low = 4, high = 0;
if (littleEndian)
{
low = 0;
high = 4;
}
var dv = new DataView(Uint8Array.from(bytes ).buffer);
return (dv.getUint32(high, littleEndian) << 32) |
dv.getUint32(low, littleEndian);
}
var bytes = [ 124, 22, 124, 22, 124, 22, 124, 22];
var value = getUint64(bytes, false);
console.log(value);

SVG path convert into JSON

I have this path:
<path id="secnb1l1" class="lungon"
fill="none" stroke="black" stroke-width="1"
d="M 93.00,444.00
C 93.00,444.00 114.00,506.00 114.00,506.00
102.30,512.28 100.00,518.71 100.00,531.00
100.00,531.00 86.00,534.00 86.00,534.00
86.00,534.00 68.95,485.00 68.95,485.00
68.95,485.00 58.00,452.00 58.00,452.00
58.00,452.00 93.00,444.00 93.00,444.00 Z
M 75.00,458.00
C 75.00,458.00 79.00,458.00 79.00,458.00
78.99,466.29 79.26,463.93 76.00,471.00
76.00,471.00 86.00,471.00 86.00,471.00
82.12,462.60 83.00,464.37 83.00,455.00
83.00,455.00 75.00,458.00 75.00,458.00 Z" />
And I want to convert it into JSON. Just like on this LINK
quick example of SVG converted into JSON:
(SVG):
<path d=" M 10 25
L 10 75
L 60 75
L 10 25"
stroke="red" stroke-width="2" fill="none" />
(JSON):
var lineData = [ { "x": 1, "y": 5}, { "x": 20, "y": 20},
{ "x": 40, "y": 10}, { "x": 60, "y": 40},
{ "x": 80, "y": 5}, { "x": 100, "y": 60}];
I'm afraid that there's no generator to make my works easier. It's a pain to convert it manually.
Thanks in advance for your help :)
EDIT
I'm using d3.js for zoom and pan.
If you do not want to fiddle around with string operations, you can also use the DOM Interface.
Interface
PathSegList
From there you can iterate over the path elements path segments list and create a json string from that.
var path = document.getElementById(<pathid>),
seglist = path.pathSegList,
length = seglist.numberOfItems, i = 0, seg,
out = [], data, type;
for (; i < length; i++) {
seg = seglist.getItem(i);
type = seg.pathSegTypeAsLetter;
data = { type: seg.pathSegTypeAsLetter };
switch (type) {
case 'M':
case 'm':
case 'l' :
case 'L' :
data.x = seg.x;
data.y = seg.y;
break;
case 'C':
case 'c':
data.x = seg.x;
data.y = seg.x;
data.x1 = seg.x1;
data.y1 = seg.y1;
data.x2 = seg.x2;
data.y2 = seg.y2;
break;
/*
* to
* be
* continued
*/
}
out.push(data);
}
return JSON.stringify(out);
I have not test the code above, it should outline the basic process of iterating the Paths' segments. Since there are quite a couple of different types of segments, you can, as outlines above, »switch« over the type and create suitable data from there. Or, instead of path.pathSegList you could use path.normalizedPathSegList what return a list of segments, where each segment is converted to the type of cubic Bezier, but when tried it the last time, I got an »Not yet implemented Error«.
I have created a fiddle showing that the code works!
Given that you haven't specified things such as resolution etc. for your path around curves, I've just created a JSON with an extra property for the path type from that co-ordinate to the next one.
var d = document.getElementById('secnb1l1').getAttribute('d');
d = d.replace(/\s{2,}/g, ' '); // Remove multiple spaces
d = d.replace(/([a-zA-Z])\s[0-9]/g, '$1,'); // Add letters to coords group
d = d.split(" "); // Split on space
var coords = [];
for (var i = 0; i < d.length; i++) {
var coordString = d[i];
var coordArray = coordString.split(",");
var coord = {
x: coordArray[coordArray.length - 2],
y: coordArray[coordArray.length - 1]
};
if (coordArray.length > 2) {
coord.path = coordArray[0];
}
coords.push(coord);
}
You can see it working in this fiddle.
You can head over here, we have both node packages and API, as well as a simple web interface.
Github: https://github.com/Rexfont/svgJson
The API instruction is given in there as well
example:
svgjson({data: 'filepath/string', web: false, file: true, remove: true, output: true, filename: 'example.txt'})
You can use the API if you wish to use it multiple times on web or use the npm for node

Trouble with Javascript new keyword while using fabricjs

I am using FabricJS to put SVG objects on Canvas element in the HTML.
But since the FabricJS uses new keyword to instantiate classes, I think the properties of that class are getting tied to the global namespace.
Below, is my code for reference
My JSON object that I am parsing
var defaultSceneObj = [
{
"eyes": "res/img/animals/cat/cat_part_eye.svg",
"skin": "res/img/animals/cat/cat_skin.svg",
"mouth": "res/img/animals/cat/cat_part_mouth_happy.svg",
"pos": {
"ground" : "right_back",
"sky" : "none", //other values ["none"]
"relative" : "none" //other values ["none", "top", "bottom"]
}
},
{
"eyes": "res/img/animals/cat/cat_part_eye.svg",
"skin": "res/img/animals/cat/cat_skin.svg",
"mouth": "res/img/animals/cat/cat_part_mouth_happy.svg",
"pos": {
"ground" : "left_back",
"sky" : "none", //other values ["none"]
"relative" : "none" //other values ["none", "top", "bottom"]
}
}
];
Which means there are 2 animals in my object, where each animal is composed of eye, skin and mouth svg files.
I am looping through them in my javascript code to render them
var renderObjOnCanvas = function(cObj, cDim){
// console.log("Object, Dimension:", cObj, cDim);
// var canvas = new fabric.Canvas('elem-frame-svg');
var canvas = this.__canvas = new fabric.Canvas('elem-frame-svg');
imgwidth = 200; //default image width
imgheight = 255; //default image height
imgScale = 0.6;
imgOffsetX = Math.floor(imgwidth*imgScale/2);
imgOffsetY = Math.floor(imgheight*imgScale/2);
canvaswidth = canvas.width;
canvasheight = canvas.height;
// console.log("render canvas dimensions:", canvaswidth, canvasheight);
if (cObj.length > 0){
for (var c =0; c < cObj.length; c++){
var noun = cObj[c]; //assign the noun object
if (noun.skin !== 'Undefined'){
var animalParts = ['skin', 'eyes', 'mouth'];
var pos = cDim.ground[noun.pos.ground];
for (var g = 0; g < animalParts.length; g++){
var part_top = canvasheight - (pos[1] + imgOffsetY);
var part_left = pos[0] - imgOffsetX;
console.log("part:", noun[animalParts[g]], "part_position: ", part_top, part_left);
var img = new fabric.Image.fromURL(noun[animalParts[g]], function(s){
this.top = part_top;
this.left = part_left;
// this.scale(imgScale);
s = this;
console.log("part:", part_top, part_left);
canvas.add();
});
}
}
}
}
};
The first console.log outputs the correct top and left coordinates, but the second one only outputs the last values assigned and hence all my objects are getting placed on the same position in canvas.
Output for the first console.log:
part: res/img/animals/cat/cat_skin.svg part_position: 282 574 main.js:126
part: res/img/animals/cat/cat_part_eye.svg part_position: 282 574 main.js:126
part: res/img/animals/cat/cat_part_mouth_happy.svg part_position: 282 574 main.js:126
part: res/img/animals/cat/cat_skin.svg part_position: 282 135 main.js:126
part: res/img/animals/cat/cat_part_eye.svg part_position: 282 135 main.js:126
part: res/img/animals/cat/cat_part_mouth_happy.svg part_position: 282 135
Output for the second console.log:
(6) part: 282 135
It's because the forEach manages the scope for the variables for you and whereas for does not. For example,
var arr = [1,2,3];
for (var i=0;i<arr.length;i++){
var r = 100;
}
console.log(r); //prints 100
arr.forEach(function(){
var w = 100;
});
console.log(w); //prints "w is not defined"
so in your case, the part_top, part_left variables exists outside the for loop and only the last assigned value will be taken up by the call back function as variables are passed by reference. Take a look this answer
scope of variables in JavaScript callback functions
Using forEach() method instead of for loop worked for me. Although I am not sure, why
Our closest explanation is that since forEach() accepts an anonymous function, it binds the scope of variables into a closure when the new operator is invoked.
...
if (noun.skin !== 'Undefined'){
var animalParts = ['skin', 'eyes', 'mouth'];
var pos = cDim.ground[noun.pos.ground];
animalParts.forEach(function(item, g){ // <-works
// for (var g = 0; g < animalParts.length; g++){
var part_top = canvasheight - (pos[1] + imgOffsetY);
var part_left = pos[0] - imgOffsetX;
console.log("part:", noun[animalParts[g]], "part_position: ", part_top, part_left);
var img = new fabric.Image.fromURL(noun[animalParts[g]], function(s){
s.top = part_top;
s.left = part_left;
s.scale(imgScale);
// console.log(s, s.top,s.left, part_top, part_left);
canvas.add(s);
});
});
}
...

Categories

Resources