Friday, March 20, 2009

Security via Java Presentation

On Fri 2008-01-16 I made a presentation for my work parteners in Arx ICT about Security and its usage in Java.

The presentation starts by giving a very briefed and comprehensive introduction to General Security Concepts (Theory).

The presentation contains many common code snippets. These code snippets are very useful, they will help you in most security aware programs.

You can download the presentation here.

Thursday, January 1, 2009

Gaza and Egypt’s National Security

In 1979 Egypt and Israel had signed a peace treaty (Egyptian–Israeli Peace Treaty). After that both Israel and Egypt’s government are trying to normalize the relationships between the two neighbors. So, for us, Israel is supposed to be a friend state.

The Egyptian–Israeli Peace Treaty does not mean that Israel is not still the biggest threat to Egypt’s National Security. I have no problems with Jews, but I have a big problem with a neighbor who have nuclear weapons near our borders and ruled by the rightist extremists in the world.

In brief, Israel has the best weapons in the middle east, we and Israel are in an endless ideological conflict and Israel is ruled by extremists and Israeli people themselves are extremists and support their government’s politics.

So, Israel is the biggest threat to Egypt’s National Security however Egypt and Israel have signed a Peace Treaty.

As a consequence of being a thread, Egypt should support Israel’s enemies. We should do our best to make Israel weaker. At least, secretly Egypt should give Hamas Physical and Logistic support. Not because we and Hamas are friends, but because Hamas is Israel’s biggest enemy.

Egypt should always respect the peace treaty, we never call for war, but that does not mean that Egypt and Israel are in a cold war.

Thursday, May 8, 2008

My first attempt to build Mozilla Firefox

Today, 2008-05-08, I tried to build Mozilla Firefox. I started by downloading Mozilla build system. Then I checked all Mozilla trunk out. I followed the instructions on how to build it. It was so easy.

The build process went ok with no problems. Now, I use Mozilla Firefox that I have built.

Mozilla Firefox build process is much easier and straightforward than WebKit  build process. Few days ago, I tried to build WebKit. The build was successful but after more than one attempt.

Now, I can start hacking Mozilla codebase. After that, I will try to submit patches for review.

I wish I become a Mozilla committer someday.

Tuesday, May 6, 2008

How to make a non-rectangular window?

In this post I will show how to make a non-rectangular window on Windows 2000 and later (including Windows XP and Vista) . I will use C programming language and Win32 API.

coolwnd

 

There are two ways to make a non-rectangular window in Win32:

  1. Using SetWindowRgn.
  2. Using Layered Windows.

1. Using SetWindowRgn:

This way works on earlier versions of Windows including Windows 98/ME. To make a non-rectangular window:

  1. Prepare an HRGN object with the desired shape.
  2. Call SetWindowRgn function on the HWND of your window passing it the HRGN.

This method has the advantage of running on older versions of windows. But, it's more complex than the second method, and if you try to create a region from an image it will be a little complex.

2. Using Layered Windows:

This way requires Windows 2000 or later. It's the recommended way. It's more efficient, flexible and easier.

Layered windows improves the performance of windows that have complex shapes. You can specify a color to be the transparent color of your window. You can also specify the alpha blending value of the entire window, so you can make semi-transparent windows.

To make a non-rectangular windows using Layered Windows do the following:

  1. Prepare a bitmap to represent the window shape using any photo editor. In this bitmap choose a color to be the transparent color in your window. For example, you can make a bitmap with white background (if you won't use the white color in your window) and then draw your window on that background. If you want to make holes in your window, make this holes in the background color.
  2. Specify the WS_EX_LAYERED extended window style when calling the CreateWindowEx function, or call the SetWindowLong function to set WS_EX_LAYERED after the window has been created.
    hWnd = CreateWindowEx(
    WS_EX_LAYERED,
    MAIN_WND_CLASS_NAME,
    TEXT("Cool Window!"),
    WS_POPUP | WS_MINIMIZEBOX,
    CW_USEDEFAULT, CW_USEDEFAULT,
    600, 300,
    NULL,
    NULL,
    hInstance,
    NULL);



  3. Call SetLayeredWindowAttributes on your window to specify the transparent color. Use the same color you used as the background color of the bitmap.

    SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), 255, LWA_COLORKEY)



  4. Handle WM_ERASEBKGND and BitBlt the bitmap on the given HDC.

    BOOL OnEraseBackground(HDC hdc)
    {
    HBITMAP hBitmap;
    static HDC hMemDc = NULL;

    if (NULL == hMemDc)
    {
    hBitmap = LoadBitmap(
    GetModuleHandle(NULL),
    MAKEINTRESOURCE(IDB_SKIN));

    if (!hBitmap)
    return FALSE;

    hMemDc = CreateCompatibleDC(hdc);
    if(!hMemDc)
    return FALSE;

    SelectObject(hMemDc, hBitmap);
    }
    BitBlt(hdc,
    0, 0, 600, 300,
    hMemDc,
    0, 0, SRCCOPY);

    return TRUE;
    }

    Now any pixel in the background color (the color you specified using SetLayeredWindowAttributes) will not be considered inside the window.



Sample Code:



I made a simple sample you can download here.



Note: If you can't download the sample, send me an e-mail at bahaazaid@yahoo.com, I will send you the sample code.



Conclusion:



Windows has so many powerful features that is impossible to make use of it without returning to its Win32 API.



In the future posts I will show how to make amazing window effects (e.g. Animations, Fadeout Effects...). I will try also to write Java classes that make use of these features to make that features available to Java programmers.

Thursday, April 3, 2008

"Pure Java" is not Always an Advantage

Many software products are proud of being "Pure Java". A "Pure Java" Software is the software that is %100 written in Java Code, it uses only Java Standard Library or another "Pure Java" Library.

Being "Pure Java" has many advantages, for example:

  • You guarantee that your software will run on any OS without recompilation
  • The code is more clean, No JNI or native libraries.

But, writing every thing in Java is not always good. For example, Swing is written in "Pure Java", so Swing and Swing Applications can be used on any Platform without modifications (at least in theory). But, I can't remember the name of a single real-life, serious successful Swing Application.

Why? Ok, Some pieces of software need to be fast, responsive and use memory efficiently for example, GUI Rendering, DBMS Engines and their SQL Optimizers/Evaluators, 2D/3D Rendering/Composing Algorithms...etc.

That explains why Eclipse SWT is more successful in real-life than Java's Swing.

So, why some (many) people try to write a "Pure Java" DBMS (e.g. Derby/Java DB, HBSQL, Berkeley DB Java Edition, ...)? Why not to write a JDBC drivers for Native DBMS's? It is done already, and it is used seriously in many great real-life applications.

At the end I want to say "If Java Core Services, Back-Ends, Processing Intensive Code, Memory Exhaustive Code... etc is written/rewritten in Native Code and Exposed to Java as Thin Java Layer over that code. Java Applications will perform better (I think much better)".

Wednesday, April 2, 2008

Developers Benefit from Hardware Upgrades More Than Users

I worked on two PCs at two different dates:

1. The old one configurations is PIII 733 MHz Processor and 128 MB RAM in 2002.

2. My PC at work configurations is P4 Core 2 Due 3.00 MHz Processor and 2.00 GB RAM in 2008.

Suppose I want to run a web application in 2002, I used Microsoft IIS 6, and ASP.NET. The server and the application take less 5 seconds to run.

To run the same application in 2008, I used JBoss Application Server and JSP. The server and the application take more than 10 seconds.

Another Example, eclipse platform on 2008 hardware is slower than Microsoft Visual Studio 2002 .NET on 2002 Hardware.

I don't want to say PIII is faster than P4 Core 2 Due. I want to say software (e.g. IIS) was written using more efficient but harder tools (e.g. C++).

Today's Software is slower than Yesterday's Software without any new great added value to the user.

Now software Development is easier but on the cost of user's hardware.

I think from 2000 to now we didn't provide any fully new features to the end users. We just provide easier tools for the developer.

Before 2000 we were introducing new fully new features to the users when the hardware allow that (e.g Switching from Console UI to GUI, Video Playback, 3D Games ... etc).

Users don't take full benefits of his/her Hardware, Developers take that benefits.