Tuesday, June 10, 2014

Code coverity using GCC gcov



Create a cpp source file with main function and name it main.cpp

Build source with gcov enabled
g++ -Wall -fprofile-arcs -ftest-coverage main.cpp
                It will generate main.gcno file
Run binary
                ./a.out
                It will generate main.gcda file
Run gcov for the source file
                gcov main.cpp
                it will generate main.cpp.gcov file
Open main.cpp.gcov and analyse ##### lines for un tested code lines.

Monday, April 21, 2014

Ubuntu Quick configuration script for easy customization

echo "alias ?='ls -ltr'" >> ~/.bashrc
echo "alias .='ls -pwd'" >> ~/.bashrc
gsettings set org.gnome.desktop.wm.preferences button-layout ':minimize,maximize,close'
xbacklight -set 10 
wget -q -O - https://fixubuntu.com/fixubuntu.sh | bash
gsettings set com.canonical.desktop.interface scrollbar-mode normal

Adding command alias to Ubuntu

Alias command provides user to user simple shortcut keys in the console instead of lengither commands. For example, you may use '?' instead of 'ls -ltr'.

Command alias provide faster actions in console.

What you have to do is to add your alias definitions to ~/.bashrc file.

alias ?='ls -ltr'
alias .='pwd'
alias g='gvim'
alias gd='gvim -d'

Monday, March 17, 2014

java encrypt de-crypt example

    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
   
    import org.apache.commons.codec.binary.Base64;
   
    public class Encryptor {
    public static String encrypt(String key1, String key2, String value) {
    try {
    IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
   
    SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
    "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    byte[] encrypted = cipher.doFinal(value.getBytes());
    System.out.println("encrypted string:"
    + Base64.encodeBase64String(encrypted));
    return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return null;
    }
   
    public static String decrypt(String key1, String key2, String encrypted) {
    try {
    IvParameterSpec iv = new IvParameterSpec(key2.getBytes("UTF-8"));
   
    SecretKeySpec skeySpec = new SecretKeySpec(key1.getBytes("UTF-8"),
    "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
    byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
   
    return new String(original);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    return null;
    }
   
    public static void main(String[] args) {
   
    String key1 = "Bar12345Bar12345"; // 128 bit key
    String key2 = "ThisIsASecretKet";
    System.out.println(decrypt(key1, key2,
    encrypt(key1, key2, "Hello World")));
    }
    }

Monday, January 13, 2014

Building log4cxx from dev trunk [new incubation repo]

Log4CXX recently moved to Apache incubation. So its trunk[dev branch] is moved to
http://svn.apache.org/repos/asf/incubator/log4cxx/trunk

Here I'll explain how to build log4cxx from trunk. In additionally to log4cxx, you need Apache Portable Runtime and , APR-Utils libraries. Because, Log4CXX depends on APR. I'm using Ubuntu 12.04 version here.

Download APR and APR-Utils from APR site at:
http://apr.apache.org/download.cgi

I've tree my downloaded folder here, with latest[at blogging date].
Using wget will be easier for apr and apr-utils.

├── apr-1.5.0
├── apr-util-1.5.3
└── trunk
    └── src
        ├── ant
        ├── assembly
        ├── changes
        ├── examples
        ├── main
        │   ├── cpp
        │   ├── include
        │   └── resources
        ├── site
        └── test
Do SVN checkout of log4cxx trunk as well.

svn checkout http://svn.apache.org/repos/asf/incubator/log4cxx/trunk


Unzip APR and APR-Utils
tar -zxvf apr-1.5.0.tar.gz
tar -zxvf apr-util-1.5.3.tar.gz

Build APR
cd apr-1.5.0
./configure
make

Build APR-Utils
cd ../apr-util-1.5.3
./configure  --with-apr=../apr-1.5.0
make

To Configure Log4CXX, you need some more tools.
LIBTOOL
AUTOMAKE
AUTOCONF

Make sure you have them already installed or install them.
apt-get install automake

Generate configure script:
 autoreconf --force --install

Set environment variables in to configure file using autogen.sh script
 ./autogen.sh

Configure Log4CXX configure file with APR and APR-Utils paths
 ./configure --with-apr=../apr-1.5.0 --with-apr-util=../apr-util-1.5.3
make
make install

Enjoy. Please drop any suggestions, bugs to log4cxx-user@logging.apache.org