Monday, August 16, 2010

How to create Patch on Liunx?

In this post I will explain how to create a patch file and how to apply patch.
I only know the basic stuff. Therefore, if you want to know more then please use command man diff at command prompt to explore other possible options.

How to Create a Patch File?
Patch file is a readable text file which is created using diff command.
To create a patch of entire source folder including its sub directories, do as follows:

 $ diff -rauB  old_dir   new_dir   >  patchfile_name

  options -rauB mean:
  r  -> recursive (multiple levels dir)
  a ->  treat all files as text
  u -> Output NUM (default 3) lines of unified context
  B -> B is to ignore Blank Lines

Blank lines are usually useless in the patch file.  Therefore I used B option, however you are free to tune your patch file according to your choice.
old_dir is the original source code directory,  new_dir is the new version of source code and patchfile_name is the name of the patch file.  You can give any name to your patch file.

How to Apply Patch to your Source Code?
Suppose you have got a patch file and you want to apply that patch to the version of your source code.
It is not necessary but I recommend to do a dry-run test first.

To do a dry run:
$ patch --dry-run -p1 -i patchfile_name

-i : Read the patch from patchfile.
-pnum : Strip  the  smallest prefix containing num leading slashes from each
          file name found in the patch file.  A sequence of one or more  adjacent
          slashes  is counted as a single slash.  This controls how file
          names found in the patch file are treated, in  case  you  keep  your
          files  in  a  different  directory  than the person who sent out the
          patch.

If there is no failure then you can apply patch to your source code:
patch -p1 -i patchfile_name

I hope this post will help you to generate patch from your source code and apply patch to your source code.

Saturday, August 14, 2010

How to Build Lobo browser on Ubuntu 10.04

Prerequisites:
 1. Eclipse SDK for Java.
 2. IzPack (http://izpack.org/). Download cross platform installation jar file.

Steps:
1. Create a sudirectory "XAMJ_Project" in /opt folder.  In linux, /opt belongs to root user so you may need to change the owner of /opt folder, so that write operation will be allowed.

2. Install IzPack in "/opt/IzPack". for installation of IzPack at command prompt run the command: java -jar IzPack-install-x.x.x.jar.

3. Open Eclipse and change workspace path to "/opt/XAMJ_Project".

4. Now choose the Project Explorer (Window->Show View -> Project Explorer).

5. Right click in project explorer and select New -> Project -> CVS  - > Project from CVS.
6. When prompted for server details fill out the server details as given in step 3 of Lobo Eclipse build instruction page.   
Host: xamj.cvs.sourceforge.net
Repository path: /cvsroot/xamj
User: anonymous
Password: [leave blank]
Connection type: pserver

  7.  Next, select "Use an existing module" option.

  8.  Then select 8 modules from the XAMJ_Project folder for basic web browser build.  You can choose all modules.  eight modules I choose were as follows:    

  • Common
  • cssparser
  • HTML_Renderer
  • Platform_Public_API
  • Platform_Core
  • Primary_Extension
  • XAMJ_Build
  • IzPack_RegistryPanel
 9. Next select the version of repository and check out.

 10.  Now right click on "IzPack_RegistryPanel" select "Properties" . Then select "Java Build Path".  select the "Libraries" tab. 

  11. If you see cross for some libraries then its bcoz of the wrong path. As we have already installed IzPack, search for files in /opt/Izpack/lib or /opt/IzPack/bin/Panel, and correct the paths of library files. 

  12. Now configure "Run Configuration" for your Java Application project. Please see the README.txt file in Platform_Core for instructions, and set VM Variable and Program Variable in the Arguments tab according to the README.txt file.  Also set the  Project name and Main class name in the Main tab.  The name of the main class is org.lobobrowser.main.EntryPoint .

13. Now apply your setting and run the project. It should work. If it doesn't then right click on IzPack_Registry Panel and add libraies: xpp3-1.1.3.4.M.jar, and cobra-no-commons.jar.zip. You can search and download these libraries if they are not present.

Friday, July 30, 2010

What is Really Simple Syndication( RSS)?

Really Simple Syndication (RSS) is a way to subscribe to a source of information, such as a Web site.  
RSS works by having the website author maintain a list of notifications on their website in a standard way. This list of notifications is called an "RSS Feed". Many Blog services automatically create RSS Feeds. For Websites, RSS Feeds can be created manually or with software(such as Software Garden, Inc.'s ListGarden)
The RSS Feed that is created is an XML file that lives on a Webserver. Once RSS feed is ready on web server, RSS Feeds waits for an RSS Reader to subscribe to them. The RSS Feed Reader reads the RSS Feed file and displays it. That is, the RSS Reader displays only new items from the RSS Feed. An RSS Feed Reader reads the RSS feed file, finds what is new, converts it to HTML, and displays it.

Monday, May 24, 2010

Spider-monkey Internals



SpiderMonkey Internals 
The JavaScript engine compiles and executes scripts containing JavaScript statements and functions. The engine handles memory allocation for the objects needed to execute scripts, and it cleans up—garbage collects—objects it no longer needs.

The word JavaScript may bring to mind features such as event handlers (like onclick), DOMobjects, window.open, and XMLHttpRequest. But in Mozilla, all of these features are actually provided by other components, not the SpiderMonkey engine itself. SpiderMonkey provides a few core JavaScript data types—numbers, strings, Arrays, Objects, and so on—and a few methods, such as Array.push. It also makes it easy for each application to expose some of its own objects and functions to JavaScript code. Browsers expose DOM objects. 


Spidermonkey includes/implemented JavaScript functions such as eval(), charAt(), escape(), unescape(), encodeURI(), decodeURI(), etc. 


JavaScript Event Handlers (like onClick) are implemented in  the content/event/ module in Firefox. The file use to handle events is content/event/src/nsEventStateManager.cpp.


Window.open is implemented in the dom/ module. The file  which implemented native function for window.open is  dom/src/base/nsGlobalWindow::OpenJS() in Firefox. window object is not standardized in DOM specifications, therefore, it has vendor specific implementation of it. 


XMLHttpRequest() is implemented in the content/base/ module in Firefox. The file is content/base/src/nsXMLHttpRequest.cpp implementing XHR request.  

C/C++ code accesses SpiderMonkey via the JSAPI, by including the header "jsapi.h". The JSAPI provides functions for setting up the JavaScript runtime, compiling and executing scripts, creating and examining JavaScript data structures, handling errors, enabling security checks, and debugging scripts.

In order to run any JavaScript code in SpiderMonkey, an application must have three key elements: a JSRuntime, a JSContext, and a global object.

Runtimes. A JSRuntime, or runtime, is the space in which the JavaScript variables, objects, scripts, and contexts used by your application are allocated. Every JSContext and every object in an application lives within a JSRuntime. They cannot travel to other runtimes or be shared across runtimes. Most applications only need one runtime.

A program typically has only one JSRuntime, even if it has many threads.

Contexts. A JSContext, or context, is like a little machine that can do many things involving JavaScript code and objects. It can compile and execute scripts, get and set object properties, call JavaScript functions, convert JavaScript data from one type to another, create objects, and so on. Almost all JSAPI functions require a JSContext * as the first argument.

Global objects. Lastly, the global object contains all the classes, functions, and variables that are available for JavaScript code to use. Whenever JavaScript code does something like window.open("http://www.mozilla.org/"), it is accessing a global property, in this case window. JSAPI applications have full control over what global properties scripts can see. The application starts out by creating an object and populating it with the standard JavaScript classes, like Array and Object. Then it adds whatever custom classes, functions, and variables (like window) the application wants to provide; see Custom objects below. Each time the application runs a JS script (using, for example, JS_EvaluateScript), it provides the global object for that script to use. As the script runs, it can create global functions and variables of its own. All of these functions, classes, and variables are stored as properties of the global object.

References:




Wednesday, May 5, 2010

V8 Build Error

Hi,
I downloaded v8 and followed the build instruction given at http://code.google.com/apis/v8/build.html#build .

However, When I executed the command "scons" (without quotes) it shown following errors on my machine.


cc1plus: error: dereferencing pointer 'dest' does break strict-aliasing rules
cc1plus: error: dereferencing pointer 'dest' does break strict-aliasing rules
cc1plus: error: dereferencing pointer 'dest' does break strict-aliasing rules
src/api.cc:3495: note: initialized from here
scons: *** [obj/release/api.o] Error 1
scons: building terminated because of errors.

This is bcoz warnings are considered as errors by the compiler.
To solve this problem,
open "SConstruct" file in the source directory of v8 and look for "V8_EXTRA_FLAGS"
Then comment the line (use # to comment line) '-Werror' in
gcc:{ 
    all:{ 
       WARNINGFLAGS: [
                                    
                                      ]
  },

After doing this, I was able to build it. However, I was not able to embed v8 in C++ application.
The reason was, I was running ubuntu x64 bit OS and V8 build is of 32-bit.

I ran following command to build 64-bit libaray of V8.
$ scons mode=debug arch=x64

Where mode=debug to build DEBUG version instead of by default release version. And arch=x64 to build 64-bit version instead of by-default 32-bit version.



Sunday, April 11, 2010

Measure CPU usage time by Thread

    On Linux there are two commands you can use to measure CPU usage time of a thread in a multi threaded application.  Those commands are getrusage() and clock_gettime().  You can sue same commands to measure CPU time consumed by process.

Sample program to measure CPU time in a thread is given below:



#include
#include
#include


    int kResult = 0;
    struct rusage start, end;
    struct timeval timeS, timeE;
    double t= 0.0, time= 0.0;

    struct timespec st, endt;
    double t1= 0.0, time1= 0.0;

    // measure CPU time
      kResult = getrusage(RUSAGE_THREAD, &start);

        if (kResult == -1)
            fprintf(stderr, "\n\n Error in getrusage command");

        timeS = start.ru_stime; // system time
        t = (double)timeS.tv_sec + (double) timeS.tv_usec / 1000000.0;
        timeS = start.ru_utime; // user time
        t = t + (double)timeS.tv_sec + (double) timeS.tv_usec / 1000000.0;

        kResult = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &st);
        if (kResult == -1)
            fprintf(stderr, "\n\n Error in clock_gettime command");
      
        t1 = (double) (st.tv_sec * 1000000.0 )+ (double) st.tv_nsec / 1000.0 ;
  


   // Do some operation to consume CPU


  // measure CPU time

 kResult = getrusage(RUSAGE_THREAD, &end);

        if (kResult == -1)
            fprintf(stderr, "\n\n Error in getrusage command");

        timeE = end.ru_stime; // system time
        time = (double)timeE.tv_sec + (double) timeE.tv_usec / 1000000.0;
        timeE = end.ru_utime; // user time
        time = time + (double)timeE.tv_sec + (double) timeE.tv_usec / 1000000.0;

        time = time - t;
        fprintf(stderr,"\n\n Total CPU usage time using 'getrusage' = %.12lf\n\n", time);

        kResult = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &endt);
        if (kResult == -1)
            fprintf(stderr, "\n\n Error in clock_gettime command");
        
        time1 = (double) (endt.tv_sec * 1000000.0)+ (double) endt.tv_nsec / 1000.0 ;
        time1 = (time1 - t1) / 1000000.0;

        fprintf(stderr,"\n\n Total CPU usage time using 'clock_gettime' = %.12lf\n\n", time1);



Note: clock_gettime will generate error if you will not provide -lrt library to linker. 



  

Friday, March 19, 2010

To remove old kernel from Ubuntu

To remove old kernels from ubuntu OS:

First check the current kernel version:
$ uname -r

Do not remove current version.

To remove old kernel version use command:
$sudo apt-get purge linux-image-2.6.XX-XX-generic


Then remove header of that version:
$sudo apt-get purge linux-headers-2.6.XX-XX

Tuesday, March 16, 2010

Cannot Update Ubuntu

Recently, When I was trying to update ubuntu, I was getting following error message:
E: Could not get lock /var/lib/apt/lists/lock - open (11 Resource temporarily unavailable)
E: Unable to lock the list directory

After searching on Ubuntu forums I found the solution to solve this Problem.

Use following command to see is their any synaptic pkg manager running?
      $ ps -e | grep apt

If yes, then kill all those processes using 
  $ sudo kill -9 processID

Then remove lock file from ur system:
  $  sudo rm /var/lib/apt/lists/lock

Now try to update ur system. It should work. Atleast it worked for me.



DOT Graphs

DOT (filename.dot) is a file format to draw graphs including directed graphs.
DOT writes graphs in .ps, .pdf, .gif, .png formats.

For example,

$ dot -Tps src.dot -o dest.ps

$ dot -Tpdf src.dot -o dest.pdf

Dot file  format is as follows:

digraph graphName {
 "Node 1"  -> "Node 2"  ;
 "Node 1" -> "Node 2" -> "Node 3";
}

Each line is terminated by semicolon (;), and arrow (->) is used to show directed arc.

digraph means directed graph whereas graph means undirected graph.

Within a main graph a subgraph define a subset of graph.
For example,
digraph GraphName {
"Node 1" -> "Node 2";
 subgraph SubGraphName{
   "Node 3" -> "Node 1";
   "Node 4" -> Node 3";
  }
}

Tuesday, March 2, 2010

JavaScript Injection

JavaScript Injection is a technique that allows you alter the content of current web page without actually leaving the current web page.  It is extremely useful when you want to spoof the contents that are sent to server using Forms. 

Basics of JS Injection:
JS injection means inserting or executing a script. You can execute a script from the URL bar of the web page which you want to alter.  To execute JS code, you must first clear the URL bar (Note: Don't press enter yet), that is, no http:// or anything else. 
Javascript can be executed from URL using javascript: protocol.
Try following code in the URL bar of the web page to display your message. 

              javascript:alert("Hello World!");

If you saw a window pop-up and saying Hello World, then congrats, you successfully did a JS injection test.
Cookie Editing:
This time we will try penetrate one level deeper and we will try to modify server state. 
One of the mechanism used to represent server state is using Cookies. Server identifies client state and authorization using Cookies. Therefore, it is worth to learn cookie alteration using JS injection technique. 

To check the cookies set by web site, use following script at URL bar:
         javascript:alert(document.cookie);

Above script will show you cookies set by web site. To modify any key=value pair, use following syntax:
        javascript:void(document.cookie="Key=Value");

 Above command can either alter existing Key=Value pair or add new Key=Value pair if it doesn't exists.  To edit or alter information we use void( ) function of JavaScript.

 For example, server set Authorization=no in Cookie and you want to modify this Key=value pair.  Then you can use script given below:
        javascript:void(document.cookie="Autorization=yes");

It is also useful to try an alert(document.cookie); script at the end of the same line to see what effect your altering had.
    
Form Modifications:
One way to edit values sent to web server from client using a Form is to store a web page on a local disk and modify its Form field values with whatever values you want and then submit the form to the server.

For example:
  Following HTML code snippet shows that hidden field is submitted when a submit button is clicked on Form. If we want to modify email address to get data sent by email to webmaster.

 
<form action="/missions/basic/process.php" method="post">
<input type="hidden" name="to" value="webmaster@mywebsite.com" />
<input type="submit" value="Click to Submit" />
</form>

First, we need to store this web page on local disk, and then modify it as shown below.

<form action="http://mywebsite.com/missions/basic/process.php" method="post">
<input type="hidden" name="to" value="altered@emailaddress.com" />
<input type="submit" value="Click to Submit" />
</form>

However, sometimes the website checks to see if you actually submitted it from the website  or not. To get around this, we can just edit the form using from javascript Injection.

Every form on a given webpage (unless named otherwise) is stored in the forms[x] array... where "x" is the number, in order from top to bottom, of all the forms in a page. Note that the forms start at 0, so the first form on the page would actually be 0, and the second would be 1 and so on.

Lets consider our previous form example:
<form action="/missions/basic/process.php" method="post">
<input type="hidden" name="to" value="webmaster@mywebsite.com" />
<input type="submit" value="Click to Submit" />
</form>
Note:Since this is the first form on the page, it is forms[0].

To check the value using JS, use following command:
      javascript:alert(document.forms[0].to.value)

In this case, It would pop up an alert that says "webmaster@mywebsite.com"

So here's how to Inject your email into it. You can use the same technique as shown earlier in the cookies editing :
   javascript:void(document.forms[0].to.value="altered@emailaddress.com");

Above script would change email address to altered@emailaddress.com. You can use alert( ) JavaScript function to check your work.

These are the most basic things you need to know about JS injection and useful in many cases.