Tuesday, December 14, 2010

String matching in a file using (sed/grep) commands in LINUX

SED command using & for match string:
The special character "&" corresponds to the pattern found.
Suppose the filename ch.txt contains following lines.  And we want for each function name to add (* ) brackets .
xpcom_ProcessNextEvent(int state); 
loc_wer_test(int state);
Following command converts content in ch.txt into following format and stored output in ch1.txt file.
$sed 's/[a-zA-Z]*_*[a-zA-Z]*_*[a-zA-Z]*(/int (*&)/' < ch.txt >  ch1.txt
The Output of above command in ch1.txt file is as per follows:
int (*xpcom_ProcessNextEvent()int state); 
int (*loc_wer_test()int state);


Now we want to swap "()" bracket with ")(" from the output of above command. Following commands does that job.
$sed 's/()/)(/'  < ch1.txt > ch2.txt
int (*xpcom_ProcessNextEvent)(int state); 
int (*loc_wer_test)(int state);

You could also double a pattern, e.g. the first number of a line:
$ echo "123 abc" | sed 's/[0-9]*/& &/'
123 123 abc
In above command "123 abc" was input and output was "123 123 abc".
You can have any number of "&" in the replacement string.

GREP command to copy a matching string line from a file to another file.
Suppose input_filename contains following:
 //Escape() Function hook
  int js_js_str_fun (...)
 {
    .....
  }
int js_str_fun2 (...)
{
    .....
 }

Using following command we can extract a line with function name:
$grep "int .*(.*)"  input_filename > output_filename

Above commands stores following output in output_filename file:
  int js_js_str_fun (...)
int js_str_fun2 (...)

Wednesday, December 8, 2010

Dynamic Lib using dlopen and dlsym

dlopen system call loads dynamic library and returns handle to dynamic library.  If dlopen() fails it returns NULL
dlsym takes the library handle returnd by dlopen and symbol name as input, and  returns the address where that symbol is loaded. If the symbol is not found, dlsym returns NULL
dlerror returns NULL if no errors have occurred since initialization or since it was last called. (Calling dlerror() twice consecutively, will always result in the second call returning NULL.)

#include <stdio.h>
#include <dlfcn.h>

typedef void *(*func_t)(hooks *);

int main()
{
    // declare handle variable for dynamic library
    void *handle;
  
    double (*cosine)(double);

    // open the dynamic library
    handle = dlopen ("libm.so", RTLD_LAZY);
    if (!handle) {
        fprintf (stderr, "%s\n", dlerror());
        exit(1);
    }

    func_t initializer_fn = (func_t)dlsym(handle, "initializer");
    if (initializer_fn) {
           // call function
 (*initializer_fn)(this);
    }

    // Initialize cosine with function pointer of "cos".
   cosine = dlsym(handle, "cos");
   // call "cos" function using function pointer
   fprintf (stderr, "%f\n", (*cosine)(2.0));

   dlclose(handle);
    return 0;
} // end of main

// functions defined in library file
void initializer(hooks *libifc)
{
}

Monday, December 6, 2010

Firefox about: urls

Firefox can be tweak using about:config url, that allows to change user preferences.
There are other about: urls listed below. I tested them on FF 3.5.3.

about:blank  : shows a blank web page.
about:buildconfig  : shows mozilla build options
about:cache
about:cache?device=memory  : Lists memory cache entries
about:config  : modifies user preferences.
about:credits  : Shows an alphabetical list of mozilla contributors
about:logo  : shows mozilla logo
about:plugins  : shows all installed plugins
about:crashes  : shows Firefox crash reports if any

Other about: URLs showing some messages:
about:mozilla
about:robots
about:privatebrowsing
about:sessionrestore

Friday, December 3, 2010

Firefox Extension and Web Page Script Execution

To access Web page Document from Extension
  webpageDocument = top.window.content.document;

To Execute Web Page defined function from Firefox Extension

 var wm = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator);

    var currentWindow = wm.getMostRecentWindow('navigator:browser').getBrowser().contentWindow;
    var sandbox = new Components.utils.Sandbox(SandboxURL);
    // __.proto__  hack removes need of using window.a, window.alert()
    sandbox.__proto__ = currentWindow.wrappedJSObject;
    //  assume web page script defines variable "a"
    var pageResult = Components.utils.evalInSandbox("alert(a);", sandbox);
    dump("\n\n Web  page Function execution Result = " + pageResult);

In FF Extension to execute a Script in a Sandbox and allowing Extension function to be accessed by Sandbox object
Following code I also added to MDN [1].

 // Chrome (FF Extension) custom Functions:
function sandboxAlert(msg){
    dump("\n Inside sandboxAlert");
    msg = XPCSafeJSObjectWrapper(msg);
    dump("\n msg = " + msg);
}

// variable declared in Chrome code
var tempval = 10;

function sandboxFun1(){
    return tempval;
}

    // Chrome (Firefox Extension) code

    // create sandbox enviornment to execute script.
    // SandboxURL is required for XHR requests. Same domain are allowed.
    var sandbox = new Components.utils.Sandbox(SandboxURL);

    sandbox.y = 5;  // insert property 'y' with value 5 into global scope.
    var scriptText = "var x = 2 + sandboxFun1(); var k = y + 15; sandboxAlert('Testing'); x + 3";

    // // include chrome function into sandbox context.
    sandbox.importFunction(sandboxFun1);
    sandbox.importFunction(sandboxAlert);

    // // execute script into sandbox object
    var sandboxResult = Components.utils.evalInSandbox(scriptText, sandbox);


References:
1. Mozilla FF Extension evalInSandbox