How to generate call-graphs for given javascript? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have seen "https://stackoverflow.com/questions/1385335/how-to-generate-function-call-graphs-for-javascript", and tried it. It works well, if you want to get an abstract syntax tree.
Unfortunately Closure Compiler only seems to offer --print_tree, --print_ast and --print_pass_graph. None of them are useful for me.
I want to see a chart of which function calls which other functions.

code2flow does exactly this. Full disclosure, I started this project
To run
$ code2flow source1.js source2.js -o out.gv
Then, open out.gv with graphviz
Edit: For now, this project is unmaintained. I would suggest trying out a different solution before using code2flow.

If you filter the output of closure --print_tree you get what you want.
For example take the following file:
var fib = function(n) {
if (n < 2) {
return n;
} else {
return fib(n - 1) + fib(n - 2);
}
};
console.log(fib(fib(5)));
Filter the output of closure --print_tree
NAME fib 1
FUNCTION 1
CALL 5
NAME fib 5
SUB 5
NAME a 5
NUMBER 1.0 5
CALL 5
NAME fib 5
SUB 5
NAME a 5
NUMBER 2.0 5
EXPR_RESULT 9
CALL 9
GETPROP 9
NAME console 9
STRING log 9
CALL 9
CALL 9
NAME fib 9
CALL 9
CALL 9
NAME fib 9
NUMBER 5.0 9
And you can see all the call statements.
I wrote the following scripts to do this.
./call_tree
#! /usr/bin/env sh
function make_tree() {
closure --print_tree $1 | grep $1
}
function parse_tree() {
gawk -f parse_tree.awk
}
if [[ "$1" = "--tree" ]]; then
make_tree $2
else
make_tree $1 | parse_tree
fi
parse_tree.awk
BEGIN {
lines_c = 0
indent_width = 4
indent_offset = 0
string_offset = ""
calling = 0
call_indent = 0
}
{
sub(/\[source_file.*$/, "")
sub(/\[free_call.*$/, "")
}
/SCRIPT/ {
indent_offset = calculate_indent($0)
root_indent = indent_offset - 1
}
/FUNCTION/ {
pl = get_previous_line()
if (calculate_indent(pl) < calculate_indent($0))
print pl
print
}
{
lines_v[lines_c] = $0
lines_c += 1
}
{
indent = calculate_indent($0)
if (indent <= call_indent) {
calling = 0
}
if (calling) {
print
}
}
/CALL/ {
calling = 1
call_indent = calculate_indent($0)
print
}
/EXPR/{
line_indent = calculate_indent($0)
if (line_indent == root_indent) {
if ($0 !~ /(FUNCTION)/) {
print
}
}
}
function calculate_indent(line) {
match(line, /^ */)
return int(RLENGTH / indent_width) - indent_offset
}
function get_previous_line() {
return lines_v[lines_c - 1]
}

I finally managed this using UglifyJS2 and Dot/GraphViz, in a sort of combination of the above answer and the answers to the linked question.
The missing part, for me, was how to filter the parsed AST. It turns out that UglifyJS has the TreeWalker object, which basically applys a function to each node of the AST. This is the code I have so far:
//to be run using nodejs
var UglifyJS = require('uglify-js')
var fs = require('fs');
var util = require('util');
var file = 'path/to/file...';
//read in the code
var code = fs.readFileSync(file, "utf8");
//parse it to AST
var toplevel = UglifyJS.parse(code);
//open the output DOT file
var out = fs.openSync('path/to/output/file...', 'w');
//output the start of a directed graph in DOT notation
fs.writeSync(out, 'digraph test{\n');
//use a tree walker to examine each node
var walker = new UglifyJS.TreeWalker(function(node){
//check for function calls
if (node instanceof UglifyJS.AST_Call) {
if(node.expression.name !== undefined)
{
//find where the calling function is defined
var p = walker.find_parent(UglifyJS.AST_Defun);
if(p !== undefined)
{
//filter out unneccessary stuff, eg calls to external libraries or constructors
if(node.expression.name == "$" || node.expression.name == "Number" || node.expression.name =="Date")
{
//NOTE: $ is from jquery, and causes problems if it's in the DOT file.
//It's also very frequent, so even replacing it with a safe string
//results in a very cluttered graph
}
else
{
fs.writeSync(out, p.name.name);
fs.writeSync(out, " -> ");
fs.writeSync(out, node.expression.name);
fs.writeSync(out, "\n");
}
}
else
{
//it's a top level function
fs.writeSync(out, node.expression.name);
fs.writeSync(out, "\n");
}
}
}
if(node instanceof UglifyJS.AST_Defun)
{
//defined but not called
fs.writeSync(out, node.name.name);
fs.writeSync(out, "\n");
}
});
//analyse the AST
toplevel.walk(walker);
//finally, write out the closing bracket
fs.writeSync(out, '}');
I run it with node, and then put the output through
dot -Tpng -o graph_name.png dot_file_name.dot
Notes:
It gives a pretty basic graph - only black and white and no formatting.
It doesn't catch ajax at all, and presumably not stuff like eval or with either, as others have mentioned.
Also, as it stands it includes in the graph: functions called by other functions (and consequently functions that call other functions), functions that are called independantly, AND functions that are defined but not called.
As a result of all this, it may miss things that are relevant, or include things that are not. It's a start though, and appears to accomplish what I was after, and what led me to this question in the first place.

https://github.com/mishoo/UglifyJS
gives access to an ast in javascript.
ast.coffee
util = require 'util'
jsp = require('uglify-js').parser
orig_code = """
var a = function (x) {
return x * x;
};
function b (x) {
return a(x)
}
console.log(a(5));
console.log(b(5));
"""
ast = jsp.parse(orig_code)
console.log util.inspect ast, true, null, true

Related

Unable to Get Output From While Loop in Javascript

I'm working on my final project of the Winter 2017 quarter to demonstrate how to use Regular Expressions in both C# and JavaScript code behind pages. I've got the C# version of my demonstration program done, but the JavaScript version is making me pull what little hair I have left on my head out (no small achievement since I got a fresh buzz cut this morning!). The problem involves not getting any output after applying a Regular Expression in a While loop to get each instance of the expression and printing it out.
On my HTML page I have an input textarea, seven radio buttons, an output textarea, and two buttons underneath (one button is to move the output text to the input area to perform multiple iterations of applying expressions, and the other button to clear all textareas for starting from scratch). Each radio button links to a function that applies a regular expression to the text in the input area. Five of my seven functions work; the sixth is the one I can't figure out, and the seventh is essentially the same but with a slightly different RegEx pattern, so if I fix the sixth function, the seventh function will be a snap.
(I tried to insert/upload a JPG of the front end, but the photo upload doesn't seem to be working. Hopefully you get the drift of what I've set up.)
Here are my problem children from my JS code behind:
// RegEx_Demo_JS.js - code behind for RegEx_Demo_JS
var inputString; // Global variable for the input from the input text box.
var pattern; // Global variable for the regular expression.
var result; // Global variable for the result of applying the regular expression to the user input.
// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder()
{
var strings = [];
this.append = function (string)
{
string = verify(string);
if (string.length > 0) strings[strings.length] = string;
}
this.appendLine = function (string)
{
string = verify(string);
if (this.isEmpty())
{
if (string.length > 0) strings[strings.length] = string;
else return;
}
else strings[strings.length] = string.length > 0 ? "\r\n" + string : "\r\n";
}
this.clear = function () { strings = []; };
this.isEmpty = function () { return strings.length == 0; };
this.toString = function () { return strings.join(""); };
var verify = function (string)
{
if (!defined(string)) return "";
if (getType(string) != getType(new String())) return String(string);
return string;
}
var defined = function (el)
{
// Changed per Ryan O'Hara's comment:
return el != null && typeof(el) != "undefined";
}
var getType = function (instance)
{
if (!defined(instance.constructor)) throw Error("Unexpected object type");
var type = String(instance.constructor).match(/function\s+(\w+)/);
return defined(type) ? type[1] : "undefined";
}
}
Within the code of the second radio button (which will be the seventh and last function to complete), I tested the ScriptBuilder with data in a local variable, and it ran successfully and produced output into the output textarea. But I get no output from this next function that invokes a While loop:
function RegEx_Match_TheOnly_AllInstances()
{
inputString = document.getElementById("txtUserInput").value;
pattern = /(\s+the\s+)/ig; // Using an Flag (/i) to select either lowercase or uppercase version. Finds first occurrence either as a standalone word or inside a word.
//result = pattern.exec(inputString); // Finds the first index location
var arrResult; // Array for the results of the search.
var sb = getStringBuilder(); // Variable to hold iterations of the result and the text
while ((arrResult = pattern.exec(inputString)) !==null)
{
sb.appendLine = "Match: " + arrResult[0] ;
}
document.getElementById("txtRegExOutput").value = sb.toString();
/* Original code from C# version:
// string pattern = #"\s+(?i)the\s+"; // Same as above, but using Option construct for case insensitive search.
string pattern = #"(^|\s+)(?i)the(\W|\s+)";
MatchCollection matches = Regex.Matches(userTextInput, pattern);
StringBuilder outputString = new StringBuilder();
foreach (Match match in matches)
{
string outputRegExs = "Match: " + "\"" + match.Value + "\"" + " at index [" + match.Index + ","
+ (match.Index + match.Length) + "]" + "\n";
outputString.Append(outputRegExs);
}
txtRegExOutput.Text = outputString.ToString();
*/
} // End RegEx_Match_The_AllInstances
I left the commented code in to show what I had used in the C# code behind version to illustrate what I'm trying to accomplish.
The test input/string I used for this function is:
Don’t go there. If you want to be the Man, you have to beat The Man.
That should return two hits. Ideally, I want it to show the word that it found and the index where it found the word, but at this point I'd be happy to just get some output showing every instance it found, and then build on that with the index and possibly the lastIndex.
So, is my problem in my While loop, the way I'm applying the StringBuilder, or a combination of the two? I know the StringBuilder code works, at least when not being used in a loop and using some test data from the site I found that code. And the code for simply finding the first instance of "the" as a standalone or inside another word does work and returns output, but that doesn't use a loop.
I've looked through Stack Overflow and several other JavaScript websites for inspiration, but nothing I've tried so far has worked. I appreciate any help anyone can provide! (If you need me to post any other code, please advise and I'll be happy to oblige.)

Number formatting in template strings (Javascript - ES6)

I was wondering if it is possible to format numbers in Javascript template strings, for example something like:
var n = 5.1234;
console.log(`This is a number: $.2d{n}`);
// -> 5.12
Or possibly
var n = 5.1234;
console.log(`This is a number: ${n.toString('.2d')}`);
// -> 5.12
That syntax obviously doesn't work, it is just an illustration of the type of thing I'm looking for.
I am aware of tools like sprintf from underscore.string, but this seems like something that JS should be able to do out the box, especially given the power of template strings.
EDIT
As stated above, I am already aware of 3rd party tools (e.g. sprintf) and customised functions to do this. Similar questions (e.g. JavaScript equivalent to printf/String.Format) don't mention template strings at all, probably because they were asked before the ES6 template strings were around. My question is specific to ES6, and is independent of implementation. I am quite happy to accept an answer of "No, this is not possible" if that is case, but what would be great is either info about a new ES6 feature that provides this, or some insight into whether such a feature is on its way.
No, ES6 does not introduce any new number formatting functions, you will have to live with the existing .toExponential(fractionDigits), .toFixed(fractionDigits), .toPrecision(precision), .toString([radix]) and toLocaleString(…) (which has been updated to optionally support the ECMA-402 Standard, though).
Template strings have nothing to do with number formatting, they just desugar to a function call (if tagged) or string concatenation (default).
If those Number methods are not sufficient for you, you will have to roll your own. You can of course write your formatting function as a template string tag if you wish to do so.
You should be able to use the toFixed() method of a number:
var num = 5.1234;
var n = num.toFixed(2);
If you want to use ES6 tag functions here's how such a tag function would look,
function d2(pieces) {
var result = pieces[0];
var substitutions = [].slice.call(arguments, 1);
for (var i = 0; i < substitutions.length; ++i) {
var n = substitutions[i];
if (Number(n) == n) {
result += Number(substitutions[i]).toFixed(2);
} else {
result += substitutions[i];
}
result += pieces[i + 1];
}
return result;
}
which can then be applied to a template string thusly,
d2`${some_float} (you can interpolate as many floats as you want) of ${some_string}`;
that will format the float and leave the string alone.
Here's a fully ES6 version of Filip Allberg's solution above, using ES6 "rest" params. The only thing missing is being able to vary the precision; that could be done by making a factory function. Left as an exercise for the reader.
function d2(strs, ...args) {
var result = strs[0];
for (var i = 0; i < args.length; ++i) {
var n = args[i];
if (Number(n) == n) {
result += Number(args[i]).toFixed(2);
} else {
result += args[i];
}
result += strs[i+1];
}
return result;
}
f=1.2345678;
s="a string";
console.log(d2`template: ${f} ${f*100} and ${s} (literal:${9.0001})`);
While template-string interpolation formatting is not available as a built-in, you can get equivalent behavior with Intl.NumberFormat:
const format = (num, fraction = 2) => new Intl.NumberFormat([], {
minimumFractionDigits: fraction,
maximumFractionDigits: fraction,
}).format(num);
format(5.1234); // -> '5.12'
Note that regardless of your implementation of choice, you might get bitten by rounding errors:
(9.999).toFixed(2) // -> '10.00'
new Intl.NumberFormat([], {
minimumFractionDigits: 2,
maximumFractionDigits: 2, // <- implicit rounding!
}).format(9.999) // -> '10.00'
based on ES6 Tagged Templates (credit to https://stackoverflow.com/a/51680250/711085), this will emulate typical template string syntax in other languages (this is loosely based on python f-strings; I avoid calling it f in case of name overlaps):
Demo:
> F`${(Math.sqrt(2))**2}{.0f}` // normally 2.0000000000000004
"2"
> F`${1/3}{%} ~ ${1/3}{.2%} ~ ${1/3}{d} ~ ${1/3}{.2f} ~ ${1/3}"
"33% ~ 33.33% ~ 0 ~ 0.33 ~ 0.3333333333333333"
> F`${[1/3,1/3]}{.2f} ~ ${{a:1/3, b:1/3}}{.2f} ~ ${"someStr"}`
"[0.33,0.33] ~ {\"a\":\"0.33\",\"b\":\"0.33\"} ~ someStr
Fairly simple code using :
var FORMATTER = function(obj,fmt) {
/* implements things using (Number).toFixed:
${1/3}{.2f} -> 0.33
${1/3}{.0f} -> 1
${1/3}{%} -> 33%
${1/3}{.3%} -> 33.333%
${1/3}{d} -> 0
${{a:1/3,b:1/3}}{.2f} -> {"a":0.33, "b":0.33}
${{a:1/3,b:1/3}}{*:'.2f',b:'%'} -> {"a":0.33, "b":'33%'} //TODO not implemented
${[1/3,1/3]}{.2f} -> [0.33, 0.33]
${someObj} -> if the object/class defines a method [Symbol.FTemplate](){...},
it will be evaluated; alternatively if a method [Symbol.FTemplateKey](key){...}
that can be evaluated to a fmt string; alternatively in the future
once decorators exist, metadata may be appended to object properties to derive
formats //TODO not implemented
*/
try {
let fracDigits=0,percent;
if (fmt===undefined) {
if (typeof obj === 'string')
return obj;
else
return JSON.stringify(obj);
} else if (obj instanceof Array)
return '['+obj.map(x=> FORMATTER(x,fmt))+']'
else if (typeof obj==='object' && obj!==null /*&&!Array.isArray(obj)*/)
return JSON.stringify(Object.fromEntries(Object.entries(obj).map(([k,v])=> [k,FORMATTER(v,fmt)])));
else if (matches = fmt.match(/^\.(\d+)f$/))
[_,fracDigits] = matches;
else if (matches = fmt.match(/^(?:\.(\d+))?(%)$/))
[_,fracDigits,percent] = matches;
else if (matches = fmt.match(/^d$/))
fracDigits = 0;
else
throw 'format not recognized';
if (obj===null)
return 'null';
if (obj===undefined) {
// one might extend the above syntax to
// allow for example for .3f? -> "undefined"|"0.123"
return 'undefined';
}
if (percent)
obj *= 100;
fracDigits = parseFloat(fracDigits);
return obj.toFixed(fracDigits) + (percent? '%':'');
} catch(err) {
throw `error executing F\`$\{${someObj}\}{${fmt}}\` specification: ${err}`
}
}
function F(strs, ...args) {
/* usage: F`Demo: 1+1.5 = ${1+1.5}{.2f}`
--> "Demo: 1+1.5 = 2.50"
*/
let R = strs[0];
args.forEach((arg,i)=> {
let [_,fmt,str] = strs[i+1].match(/(?:\{(.*)(?<!\\)\})?(.*)/);
R += FORMATTER(arg,fmt) + str;
});
return R;
}
sidenote: The core of the code is as follows. The heavy lifting is done by the formatter. The negative lookbehind is somewhat optional, and to let one escape actual curly braces.
let R = strs[0];
args.forEach((arg,i)=> {
let [_,fmt,str] = strs[i+1].match(/(?:\{(.*)(?<!\\)\})?(.*)/);
R += FORMATTER(arg,fmt) + str;
});
You can use es6 tag functions. I don't know ready for use of that.
It might look like this:
num`This is a number: $.2d{n}`
Learn more:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
https://developers.google.com/web/updates/2015/01/ES6-Template-Strings

How to deobfuscate Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need help deobfuscating some Javascript. I've been trying to decode it for hours but I've gotten nowhere.
function divulge() {
eval(function (p, a, c, k, e, r) {
e = function (c) {
return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36))
};
if (!''.replace(/^/, String)) {
while (c--) r[e(c)] = k[c] || e(c);
k = [
function (e) {
return r[e]
}
];
e = function () {
return '\\w+'
};
c = 1
};
while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]);
return p
}('19 k=["\\N\\U\\O\\V\\P\\F\\a\\W\\a\\Q\\a\\t\\a\\o\\a\\g\\a\\X\\a\\x\\a\\u\\a\\y\\a\\l\\a\\t\\a\\p\\a\\u\\a\\g\\a\\o\\a\\l\\a\\p\\a\\m\\a\\f\\a\\o\\a\\g\\a\\o\\a\\p\\a\\v\\a\\f\\a\\g\\a\\o\\a\\f\\a\\y\\a\\m\\a\\f\\a\\l\\a\\g\\a\\x\\a\\u\\a\\Y\\a\\f\\a\\m\\a\\g\\a\\o\\a\\p\\a\\v\\a\\f\\a\\g\\a\\n\\a\\m\\a\\Z\\a\\t\\a\\l\\a\\m\\a\\n\\a\\m\\a\\c\\a\\g\\a\\r\\a\\n\\a\\m\\a\\t\\a\\n\\a\\Z\\a\\z\\a\\f\\a\\g\\a\\1a\\a\\Q\\a\\p\\a\\o\\a\\f\\a\\g\\a\\u\\a\\n\\a\\v\\a\\f\\a\\g\\a\\1b\\a\\g\\a\\o\\a\\Q\\a\\n\\a\\z\\a\\z\\a\\g\\a\\u\\a\\p\\a\\l\\a\\g\\a\\m\\a\\f\\a\\r\\a\\f\\a\\n\\a\\z\\a\\1c\\F\\1d\\F\\a\\d\\a\\e\\a\\1e\\a\\d\\a\\h\\a\\G\\a\\d\\a\\h\\a\\G\\a\\d\\a\\h\\a\\H\\a\\d\\a\\i\\a\\10\\a\\d\\a\\s\\a\\A\\a\\d\\a\\s\\a\\A\\a\\d\\a\\h\\a\\i\\a\\d\\a\\e\\a\\w\\a\\d\\a\\e\\a\\h\\a\\d\\a\\e\\a\\B\\a\\d\\a\\e\\a\\w\\a\\d\\a\\e\\a\\B\\a\\d\\a\\s\\a\\B\\a\\d\\a\\e\\a\\i\\a\\d\\a\\e\\a\\11\\a\\d\\a\\e\\a\\12\\a\\d\\a\\h\\a\\i\\a\\d\\a\\e\\a\\i\\a\\d\\a\\h\\a\\i\\a\\d\\a\\e\\a\\i\\a\\d\\a\\s\\a\\B\\a\\d\\a\\e\\a\\A\\a\\d\\a\\h\\a\\s\\a\\d\\a\\e\\a\\h\\a\\d\\a\\s\\a\\A\\a\\d\\a\\h\\a\\i\\a\\d\\a\\e\\a\\C\\a\\d\\a\\e\\a\\i\\a\\d\\a\\h\\a\\s\\a\\d\\a\\e\\a\\C\\a\\d\\a\\h\\a\\G\\a\\d\\a\\h\\a\\i\\F\\R\\S\\1f\\13\\O\\P\\I\\R\\14\\S\\N\\U\\1g\\V\\1h\\13\\O\\P\\D\\R\\14\\S","\\b","\\w\\y\\v\\B\\C","\\b\\b\\c\\d\\h\\b\\c\\e\\f\\b\\c\\d\\I\\b\\c\\e\\i\\b\\c\\f\\h\\b\\c\\e\\e\\b\\c\\i\\e\\b\\c\\i\\d\\b\\c\\f\\D\\b\\c\\e\\d\\b\\c\\f\\12\\b\\c\\f\\11\\b\\c\\i\\g\\b\\c\\f\\m\\b\\c\\f\\H\\b\\c\\g\\f\\b\\c\\g\\h\\b\\1i\\I\\c\\d\\p\\f\\t\\b\\c\\f\\o\\b\\c\\f\\10\\b\\c\\e\\g\\b\\X\\n\\r\\b\\c\\i\\h\\b\\c\\f\\e\\b\\c\\f\\d\\b\\c\\i\\f\\b\\c\\e\\m\\b\\c\\e\\h\\b\\c\\h\\g\\b\\c\\f\\f\\b\\c\\f\\g\\b\\c\\i\\m\\b\\c\\i\\i\\b\\c\\g\\m\\b\\c\\d\\D\\b\\c\\e\\o\\b\\c\\e\\I\\b\\c\\g\\D\\b\\c\\e\\D\\b\\c\\g\\g\\b\\n\\v\\l\\r\\C\\b\\w\\l\\p\\r\\l\\C\\x\\w\\b\\W\\N\\l\\w\\p\\n\\y\\l","","\\t\\r\\x\\G\\H\\A\\n\\r\\H\\x\\u\\l","\\r\\l\\y\\v\\n\\p\\l","\\a\\Y\\1j","\\a\\s","\\z"];1k(J(K,L,j,E,q,T){q=J(j){M(j<L?k[4]:q(1l(j/L)))+((j=j%L)>1m?15[k[5]](j+1n):j.1o(1p))};16(!k[4][k[6]](/^/,15)){17(j--){T[q(j)]=E[j]||q(j)};E=[J(q){M T[q]}];q=J(){M k[7]};j=1};17(j--){16(E[j]){K=K[k[6]](1q 1r(k[8]+q(j)+k[8],k[9]),E[j])}};M K}(k[0],18,18,k[3][k[2]](k[1]),0,{}));', 62, 90, '||||||||||x5C|x7C|x78|x32|x33|x36|x34|x35|x37|_0xd4b0x3|_0x6159|x65|x39|x61|x38|x63|_0xd4b0x5|x72|x62|x66|x64|x6C|x73|x6F|x70|x67|x68|x69|x74|x31|_0xd4b0x4|x22|x6D|x43|x30|function|_0xd4b0x1|_0xd4b0x2|return|x6E|x6A|x5B|x6B|x5D|x3B|_0xd4b0x6|x20|x3D|x75|x76|x77|x71|x44|x45|x46|x28|x29|String|if|while|45|var|x79|x7A|x41|x2C|x42|x47|x48|x49|x5F|x2B|eval|parseInt|35|29|toString|36|new|RegExp'.split('|'), 0, {
}))
}
I ran it in JSBeautifier and got
function divulge() {
var _0x6159 = ["\x6E\x20\x6A\x3D\x5B\x22\x5C\x75\x5C\x6B\x5C\x66\x5C\x38\x5C\x34\x5C\x76\x5C\x6F\x5C\x64\x5C\x70\x5C\x65\x5C\x66\x5C\x63\x5C\x64\x5C\x34\x5C\x38\x5C\x65\x5C\x63\x5C\x39\x5C\x36\x5C\x38\x5C\x34\x5C\x38\x5C\x63\x5C\x6C\x5C\x36\x5C\x34\x5C\x38\x5C\x36\x5C\x70\x5C\x39\x5C\x36\x5C\x65\x5C\x34\x5C\x6F\x5C\x64\x5C\x77\x5C\x36\x5C\x39\x5C\x34\x5C\x38\x5C\x63\x5C\x6C\x5C\x36\x5C\x34\x5C\x61\x5C\x39\x5C\x71\x5C\x66\x5C\x65\x5C\x39\x5C\x61\x5C\x39\x5C\x78\x5C\x34\x5C\x72\x5C\x61\x5C\x39\x5C\x66\x5C\x61\x5C\x71\x5C\x67\x5C\x36\x5C\x34\x5C\x79\x5C\x6B\x5C\x63\x5C\x38\x5C\x36\x5C\x34\x5C\x64\x5C\x61\x5C\x6C\x5C\x36\x5C\x34\x5C\x7A\x5C\x34\x5C\x38\x5C\x6B\x5C\x61\x5C\x67\x5C\x67\x5C\x34\x5C\x64\x5C\x63\x5C\x65\x5C\x34\x5C\x39\x5C\x36\x5C\x72\x5C\x36\x5C\x61\x5C\x67\x5C\x41\x22\x2C\x22\x5C\x32\x5C\x33\x5C\x42\x5C\x32\x5C\x35\x5C\x6D\x5C\x32\x5C\x35\x5C\x6D\x5C\x32\x5C\x35\x5C\x43\x5C\x32\x5C\x37\x5C\x44\x5C\x32\x5C\x62\x5C\x68\x5C\x32\x5C\x62\x5C\x68\x5C\x32\x5C\x35\x5C\x37\x5C\x32\x5C\x33\x5C\x73\x5C\x32\x5C\x33\x5C\x35\x5C\x32\x5C\x33\x5C\x69\x5C\x32\x5C\x33\x5C\x73\x5C\x32\x5C\x33\x5C\x69\x5C\x32\x5C\x62\x5C\x69\x5C\x32\x5C\x33\x5C\x37\x5C\x32\x5C\x33\x5C\x45\x5C\x32\x5C\x33\x5C\x46\x5C\x32\x5C\x35\x5C\x37\x5C\x32\x5C\x33\x5C\x37\x5C\x32\x5C\x35\x5C\x37\x5C\x32\x5C\x33\x5C\x37\x5C\x32\x5C\x62\x5C\x69\x5C\x32\x5C\x33\x5C\x68\x5C\x32\x5C\x35\x5C\x62\x5C\x32\x5C\x33\x5C\x35\x5C\x32\x5C\x62\x5C\x68\x5C\x32\x5C\x35\x5C\x37\x5C\x32\x5C\x33\x5C\x74\x5C\x32\x5C\x33\x5C\x37\x5C\x32\x5C\x35\x5C\x62\x5C\x32\x5C\x33\x5C\x74\x5C\x32\x5C\x35\x5C\x6D\x5C\x32\x5C\x35\x5C\x37\x22\x5D\x3B\x47\x28\x6A\x5B\x30\x5D\x29\x3B\x6E\x20\x48\x3D\x49\x28\x6A\x5B\x31\x5D\x29\x3B", "\x7C", "\x73\x70\x6C\x69\x74", "\x7C\x7C\x78\x32\x35\x7C\x78\x33\x36\x7C\x78\x32\x30\x7C\x78\x33\x37\x7C\x78\x36\x35\x7C\x78\x33\x33\x7C\x78\x37\x33\x7C\x78\x37\x32\x7C\x78\x36\x31\x7C\x78\x33\x32\x7C\x78\x36\x46\x7C\x78\x36\x45\x7C\x78\x37\x34\x7C\x78\x36\x39\x7C\x78\x36\x43\x7C\x78\x34\x36\x7C\x78\x34\x35\x7C\x5F\x30\x78\x32\x63\x36\x66\x7C\x78\x36\x38\x7C\x78\x36\x44\x7C\x78\x33\x34\x7C\x76\x61\x72\x7C\x78\x37\x35\x7C\x78\x36\x33\x7C\x78\x36\x32\x7C\x78\x37\x36\x7C\x78\x33\x39\x7C\x78\x33\x35\x7C\x78\x35\x34\x7C\x78\x36\x36\x7C\x78\x36\x34\x7C\x78\x37\x39\x7C\x78\x37\x37\x7C\x78\x34\x39\x7C\x78\x32\x31\x7C\x78\x33\x38\x7C\x78\x33\x30\x7C\x78\x34\x31\x7C\x78\x33\x31\x7C\x78\x34\x34\x7C\x61\x6C\x65\x72\x74\x7C\x73\x65\x63\x72\x65\x74\x6F\x73\x7C\x75\x6E\x65\x73\x63\x61\x70\x65", "", "\x66\x72\x6F\x6D\x43\x68\x61\x72\x43\x6F\x64\x65", "\x72\x65\x70\x6C\x61\x63\x65", "\x5C\x77\x2B", "\x5C\x62", "\x67"];
eval(function (_0xd4b0x1, _0xd4b0x2, _0xd4b0x3, _0xd4b0x4, _0xd4b0x5, _0xd4b0x6) {
_0xd4b0x5 = function (_0xd4b0x3) {
return (_0xd4b0x3 < _0xd4b0x2 ? _0x6159[4] : _0xd4b0x5(parseInt(_0xd4b0x3 / _0xd4b0x2))) + ((_0xd4b0x3 = _0xd4b0x3 % _0xd4b0x2) > 35 ? String[_0x6159[5]](_0xd4b0x3 + 29) : _0xd4b0x3.toString(36))
};
if (!_0x6159[4][_0x6159[6]](/^/, String)) {
while (_0xd4b0x3--) {
_0xd4b0x6[_0xd4b0x5(_0xd4b0x3)] = _0xd4b0x4[_0xd4b0x3] || _0xd4b0x5(_0xd4b0x3)
};
_0xd4b0x4 = [
function (_0xd4b0x5) {
return _0xd4b0x6[_0xd4b0x5]
}];
_0xd4b0x5 = function () {
return _0x6159[7]
};
_0xd4b0x3 = 1
};
while (_0xd4b0x3--) {
if (_0xd4b0x4[_0xd4b0x3]) {
_0xd4b0x1 = _0xd4b0x1[_0x6159[6]](new RegExp(_0x6159[8] + _0xd4b0x5(_0xd4b0x3) + _0x6159[8], _0x6159[9]), _0xd4b0x4[_0xd4b0x3])
}
};
return _0xd4b0x1
}(_0x6159[0], 45, 45, _0x6159[3][_0x6159[2]](_0x6159[1]), 0, {}));
}
I'm pretty lost at this point. Is the first code not obfuscated? I don't have much experience in programming, it's a part of a computer science challenge I've been trying to do. I also tried replacing function in line 1 with alert and it said there's a missing semicolon but I'm not sure where.
Here's a quick tutorial on deobfuscating it. I will not go into too much detail because this was a challenge you are supposed to solve afterall.
Take the original code and look at its structure. It basically is of the form
function divulge() {
eval(function(...) {
/* nobody cares what happens here */
// now this is interesting, because p will contain the string that eval() will execute!
return p;
}(...));
}
It should be fairly obvious now how to learn what this code will execute rather than having it actually execute: p will get evaled, so just intercept it. This will show that it's just more code (obviously).
So let's start over! Looking at the new code, after beautifying it, we will see that the structure is basically the same. So someone is trying to be sneaky by obfuscating the code several times. Too bad we already deobfuscated it once, so we just repeat the entire procedure.
After the second time, we get code of the structure
var _0x2c6f = ["..."];
alert(_0x2c6f[0]);
var secretos = unescape(_0x2c6f[1]);
This will alert the following text:
This function stores some secret under some arbitrary variable whose name I shall not reveal!
And finally, secretos contains a link. However, I will censor the link here for multiple reasons:
http://signin.**********.org/secrets

Piping a node.js script's stdout to another's stdin doesn't work

In case you are not familiar with CoffeeScript, here's the JavaScript version of p1.coffee and p2.coffee mentioned below.
Piping a node.js script's stdout to another's stdin doesn't seem to work. I have p1.coffee which outputs numbers to stdout as fast as it can:
i = 0
(->
i += 1
process.stdout.write "#{i} "
)() while true
I now have p2.coffee which does exactly what's like a cat:
process.stdin.on 'end', -> console.log "EOF"
process.stdin.pipe(process.stdout)
Now if I pipeline them together, it displays only the first number and "blocks" there:
> coffee p1.coffee | coffee p2.coffee
1
I'm using node v0.10.31 on Windows if that matters.
That might be a Windows specific issue. I tried the following with Node.js v0.10.31 on OS X and it worked fine:
// cat.js
process.stdin.once('end', function () {
console.log('EOF');
});
process.stdin.pipe(process.stdout);
// count.js
var i = 0;
while (true) {
process.stdout.write(i++ + ' ');
}
and piped the output of count.js to cat.js:
node count.js | node cat.js
Also note that your CoffeeScript compiles to:
var i;
i = 0;
while (true) {
(function() {
i += 1;
return process.stdout.write("" + i + " ");
})();
}
Creating functions within loops makes your code slower. You could do the following instead:
i = 0
loop process.stdout.write("#{i += 1} ")

Need a basename function in Javascript

I need a short basename function (one-liner ?) for Javascript:
basename("/a/folder/file.a.ext") -> "file.a"
basename("/a/folder/file.ext") -> "file"
basename("/a/folder/file") -> "file"
That should strip the path and any extension.
Update:
For dot at the beginning would be nice to treat as "special" files
basename("/a/folder/.file.a.ext") -> ".file.a"
basename("/a/folder/.file.ext") -> ".file"
basename("/a/folder/.file") -> ".file" # empty is Ok
basename("/a/folder/.fil") -> ".fil" # empty is Ok
basename("/a/folder/.file..a..") -> # does'nt matter
function basename(path) {
return path.split('/').reverse()[0];
}
Breaks up the path into component directories and filename then returns the last piece (the filename) which is the last element of the array.
function baseName(str)
{
var base = new String(str).substring(str.lastIndexOf('/') + 1);
if(base.lastIndexOf(".") != -1)
base = base.substring(0, base.lastIndexOf("."));
return base;
}
If you can have both / and \ as separators, you have to change the code to add one more line
Any of the above works although they have no respect for speed/memory :-).
A faster/simpler implementation should uses as fewer functions/operations as possible. RegExp is a bad choice because it consumes a lot of resources when actually we can achieve the same result but easier.
An implementation when you want the filename including extension (which in fact this is the genuine definition of basename):
function basename(str, sep) {
return str.substr(str.lastIndexOf(sep) + 1);
}
If you need a custom basename implementation that has to strip also the extension I would recommend instead a specific extension-stripping function for that case which you can call it whenever you like.
function strip_extension(str) {
return str.substr(0,str.lastIndexOf('.'));
}
Usage example:
basename('file.txt','/'); // real basename
strip_extension(basename('file.txt','/')); // custom basename
They are separated such that you can combine them to obtain 3 different things: stripping the extention, getting the real-basename, getting your custom-basename. I regard it as a more elegant implementation than others approaches.
Maybe try to use existing packages if you can. http://nodejs.org/api/path.html
var path = require('path');
var str = '/path/to/file/test.html'
var fileNameStringWithoutExtention = path.basename(str, '.html');
// returns 'test'
// let path determine the extension
var fileNameStringWithoutExtention = path.basename(str, path.extname(str));
// returns 'test'
Other examples:
var pathString = path.dirname(str);
var fileNameStringWithExtention = path.basename(str);
var fullPathAndFileNameString = path.join(pathString, fileNameString);
basename = function(path) {
return path.replace(/.*\/|\.[^.]*$/g, '');
}
replace anything that ends with a slash .*\/ or dot - some non-dots - end \.[^.]*$ with nothing
Just like #3DFace has commented:
path.split(/[\\/]/).pop(); // works with both separators
Or if you like prototypes:
String.prototype.basename = function(sep) {
sep = sep || '\\/';
return this.split(new RegExp("["+sep+"]")).pop();
}
Testing:
var str = "http://stackoverflow.com/questions/3820381/need-a-basename-function-in-javascript";
alert(str.basename());
Will return "need-a-basename-function-in-javascript".
Enjoy!
Using modern (2020) js code:
function basename (path) {
return path.substring(path.lastIndexOf('/') + 1)
}
console.log(basename('/home/user/file.txt'))
Contrary to misinformation provided above, regular expressions are extremely efficient. The caveat is that, when possible, they should be in a position so that they are compiled exactly once in the life of the program.
Here is a solution that gives both dirname and basename.
const rx1 = /(.*)\/+([^/]*)$/; // (dir/) (optional_file)
const rx2 = /()(.*)$/; // () (file)
function dir_and_file(path) {
// result is array with
// [0] original string
// [1] dirname
// [2] filename
return rx1.exec(path) || rx2.exec(path);
}
// Single purpose versions.
function dirname(path) {
return (rx1.exec(path) || rx2.exec(path))[1];
}
function basename(path) {
return (rx1.exec(path) || rx2.exec(path))[2];
}
As for performance, I have not measured it, but I expect this solution to be in the same range as the fastest of the others on this page, but this solution does more. Helping the real-world performance is the fact that rx1 will match most actual paths, so rx2 is rarely executed.
Here is some test code.
function show_dir(parts) {
console.log("Original str :"+parts[0]);
console.log("Directory nm :"+parts[1]);
console.log("File nm :"+parts[2]);
console.log();
}
show_dir(dir_and_file('/absolute_dir/file.txt'));
show_dir(dir_and_file('relative_dir////file.txt'));
show_dir(dir_and_file('dir_no_file/'));
show_dir(dir_and_file('just_one_word'));
show_dir(dir_and_file('')); // empty string
show_dir(dir_and_file(null));
And here is what the test code yields:
# Original str :/absolute_dir/file.txt
# Directory nm :/absolute_dir
# File nm :file.txt
#
# Original str :relative_dir////file.txt
# Directory nm :relative_dir
# File nm :file.txt
#
# Original str :dir_no_file/
# Directory nm :dir_no_file
# File nm :
#
# Original str :just_one_word
# Directory nm :
# File nm :just_one_word
#
# Original str :
# Directory nm :
# File nm :
#
# Original str :null
# Directory nm :
# File nm :null
By the way, "node" has a built in module called "path" that has "dirname" and "basename". Node's "path.dirname()" function accurately imitates the behavior of the "bash" shell's "dirname," but is that good? Here's what it does:
Produces '.' (dot) when path=="" (empty string).
Produces '.' (dot) when path=="just_one_word".
Produces '.' (dot) when path=="dir_no_file/".
I prefer the results of the function defined above.
Another good solution:
function basename (path, suffix) {
// discuss at: http://locutus.io/php/basename/
// original by: Kevin van Zonneveld (http://kvz.io)
// improved by: Ash Searle (http://hexmen.com/blog/)
// improved by: Lincoln Ramsay
// improved by: djmix
// improved by: Dmitry Gorelenkov
// example 1: basename('/www/site/home.htm', '.htm')
// returns 1: 'home'
// example 2: basename('ecra.php?p=1')
// returns 2: 'ecra.php?p=1'
// example 3: basename('/some/path/')
// returns 3: 'path'
// example 4: basename('/some/path_ext.ext/','.ext')
// returns 4: 'path_ext'
var b = path
var lastChar = b.charAt(b.length - 1)
if (lastChar === '/' || lastChar === '\\') {
b = b.slice(0, -1)
}
b = b.replace(/^.*[\/\\]/g, '')
if (typeof suffix === 'string' && b.substr(b.length - suffix.length) === suffix) {
b = b.substr(0, b.length - suffix.length)
}
return b
}
from: http://locutus.io/php/filesystem/basename/
Fast without regular expressions, suitable for both path types '/' and '\'. Gets the extension also:
function baseName(str)
{
let li = Math.max(str.lastIndexOf('/'), str.lastIndexOf('\\'));
return new String(str).substring(li + 1);
}
This is my implementation which I use in my fundamental js file.
// BASENAME
Window.basename = function() {
var basename = window.location.pathname.split(/[\\/]/);
return basename.pop() || basename.pop();
}
JavaScript Functions for basename and also dirname:
function basename(path) {
return path.replace(/.*\//, '');
}
function dirname(path) {
return path.match(/.*\//);
}
Sample:
Input dirname() basename()
/folder/subfolder/file.ext /folder/subfolder/ file.ext
/folder/subfolder /folder/ subfolder
/file.ext file.ext /
file.ext file.ext null
See reference.
Defining a flexible basename implementation
Despite all the answers, I still had to produce my own solution which fits the following criteria:
Is fully portable and works in any environment (thus Node's path.basename won't do)
Works with both kinds of separators (/ and \)
Allows for mixing separators - e.g. a/b\c (this is different from Node's implementation which respects the underlying system's separator instead)
Does not return an empty path if path ends on separator (i.e. getBaseName('a/b/c/') === 'c')
Code
(make sure to open the console before running the Snippet)
/**
* Flexible `basename` implementation
* #see https://stackoverflow.com/a/59907288/2228771
*/
function getBasename(path) {
// make sure the basename is not empty, if string ends with separator
let end = path.length-1;
while (path[end] === '/' || path[end] === '\\') {
--end;
}
// support mixing of Win + Unix path separators
const i1 = path.lastIndexOf('/', end);
const i2 = path.lastIndexOf('\\', end);
let start;
if (i1 === -1) {
if (i2 === -1) {
// no separator in the whole thing
return path;
}
start = i2;
}
else if (i2 === -1) {
start = i1;
}
else {
start = Math.max(i1, i2);
}
return path.substring(start+1, end+1);
}
// tests
console.table([
['a/b/c', 'c'],
['a/b/c//', 'c'],
['a\\b\\c', 'c'],
['a\\b\\c\\', 'c'],
['a\\b\\c/', 'c'],
['a/b/c\\', 'c'],
['c', 'c']
].map(([input, expected]) => {
const result = getBasename(input);
return {
input,
result,
expected,
good: result === expected ? '✅' : '❌'
};
}));
A nice one line, using ES6 arrow functions:
var basename = name => /([^\/\\]*|\.[^\/\\]*)\..*$/gm.exec(name)[1];
// In response to #IAM_AL_X's comments, even shorter and it
// works with files that don't have extensions:
var basename = name => /([^\/\\\.]*)(\..*)?$/.exec(name)[1];
function basename(url){
return ((url=/(([^\/\\\.#\? ]+)(\.\w+)*)([?#].+)?$/.exec(url))!= null)? url[2]: '';
}
Fairly simple using regex:
function basename(input) {
return input.split(/\.[^.]+$/)[0];
}
Explanation:
Matches a single dot character, followed by any character except a dot ([^.]), one or more times (+), tied to the end of the string ($).
It then splits the string based on this matching criteria, and returns the first result (ie everything before the match).
[EDIT]
D'oh. Misread the question -- he wants to strip off the path as well. Oh well, this answers half the question anyway.
my_basename('http://www.example.com/dir/file.php?param1=blabla#cprod', '/', '?');
// returns: file.php
CODE:
function my_basename(str, DirSeparator, FileSeparator) { var x= str.split(DirSeparator); return x[x.length-1].split(FileSeparator)[0];}
UPDATE
An improved version which works with forward / and backslash \ single or double means either of the following
\\path\\to\\file
\path\to\file
//path//to//file
/path/to/file
http://url/path/file.ext
http://url/path/file
See a working demo below
let urlHelper = {};
urlHelper.basename = (path) => {
let isForwardSlash = path.match(/\/{1,2}/g) !== null;
let isBackSlash = path.match(/\\{1,2}/g) !== null;
if (isForwardSlash) {
return path.split('/').reverse().filter(function(el) {
return el !== '';
})[0];
} else if (isBackSlash) {
return path.split('\\').reverse().filter(function(el) {
return el !== '';
})[0];
}
return path;
};
$('em').each(function() {
var text = $(this).text();
$(this).after(' --> <strong>' + urlHelper.basename(text) + '</strong><br>');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<em>http://folder/subfolder/file.ext</em><br>
<em>http://folder/subfolder/subfolder2</em><br>
<em>/folder/subfolder</em><br>
<em>/file.ext</em><br>
<em>file.ext</em><br>
<em>/folder/subfolder/</em><br>
<em>//folder//subfolder//</em><br>
<em>//folder//subfolder</em><br>
<em>\\folder\\subfolder\\</em><br>
<em>\\folder\\subfolder\\file.ext</em><br>
<em>\folder\subfolder\</em><br>
<em>\\folder\\subfolder</em><br>
<em>\\folder\\subfolder\\file.ext</em><br>
<em>\folder\subfolder</em><br>
A more simpler solution could be
function basename(path) {
return path.replace(/\/+$/, "").replace( /.*\//, "" );
}
Input basename()
/folder/subfolder/file.ext --> file.ext
/folder/subfolder --> subfolder
/file.ext --> file.ext
file.ext --> file.ext
/folder/subfolder/ --> subfolder
Working example: https://jsfiddle.net/Smartik/2c20q0ak/1/
If your original string or text file contains a single backslash character, you could locate it by using '\\'.
In my circumstance, I am using JavaScript to find the index of "\N" from a text file. And str.indexOf('\\N'); helped me locate the \N from the original string, which is read from the source file.

Categories