Friday, 5 May 2017

Binary Files to C array Transformer

Python script converting binary files into C byte arrays 

#!/usr/bin/python
## bf2ca.py binary file to C array script
## author: Frank Rehberger
## License: Apache License V2
## Usage:
##    chmod +x bf2ca.py
##    ./bf2ca.py file1.bin  file2.bin file3.bin
import sys
import os
from array import array

def getvarname(datafile):
   return datafile[:datafile.rfind(".")]

def printAsEmbeddedArray(datafile):
   if not os.path.exists(datafile):
      sys.stderr.write("ERROR: Datafile %r was not found!" % (datafile,))
      return 1

   with open(datafile, "rb") as f:
      ar = ["(char)0x{:02x}".format(ord(c)) for c in f.read()]
      varname = getvarname(datafile)
      print "static char", varname, "[] = {", ", ".join(ar), "};"
      print

def main(argv):
   if len(argv) < 2:
      sys.stderr.write("Usage: %s +" % (argv[0],))
      return 1
   filelist = argv[1:]
   for datafile in filelist:
       printAsEmbeddedArray(datafile)

   varnamelist = ["{:s}".format(getvarname(f)) for f in filelist] + ["NULL"]
   print "static char* BUF[] = {", ", ".join(varnamelist), "};"
  
   varnamelist = ["sizeof({:s})".format(getvarname(f)) for f in filelist] + ["0"]
   print "static size_t BUFLEN[] = {", ", ".join(varnamelist), "};"

if __name__ == "__main__":
    sys.exit(main(sys.argv))

Sunday, 25 September 2016

Rust lang - initialize HashSet HashMap with constant members

Rust programming language is short of examples, how to initialize HashSet and HashMap variables with constant members. The following example demonstrates usage of seq macro rule for variables set, map1 and map2. Last th expression for the HashMap variable map3 demonstrates initialization with  constant elements without usage of macro 'seq!'.


/////////// main.rs ////////////////////
use std::collections::{HashMap,HashSet};

// from https://github.com/rust-lang/rust/issues/14726
macro_rules! seq {
    ($($x:expr),+) => {
        [$($x,)+].iter().map(|&x| x).collect()
    }
}

fn main() {
    let set: HashSet = seq!(1, 2, 3);
    let map1: HashMap = seq!((1, 10), (2, 20), (3, 30));
    let map2: HashMap<&'static str, &'static str> =
    seq!(("A", "1"),
        ("B", "2"),
        ("C", "3"));

    // demonstrate without macro
    let map3: HashMap<&'static str, &'static str> =
    [ ("A", "1"),
      ("B", "2"),
      ("C", "3") ]
      .iter().map(|&x| x).collect();
  
    println!("Set: {:?}", set);
    println!("Map: {:?}", map1);
    println!("Map: {:?}", map2);
    println!("Map: {:?}", map3);
}

Saturday, 3 September 2016

Automate ssh execution with password auth

Warning: The following command should be used only, operating on hosts not shared with other users, for example in internal test-labs. Otherwise the following command line might leak the password via ps process table to other users. 

In case  to automate ssh commands for example in test-lab and using password authentication instead of public-private authentication the following command options could be used with ssh (sshpass and -o options will work similar for scp).

sshpass -p${PASSWORD} ssh -c arcfour  -o StrictHostKeyChecking=no  -o UserKnownHostsFile=/dev/null -o ControlMaster=no -o LogLevel=ERROR -l ${USER} ${HOST_IP} ${COMMAND}

Explaining a few elements:
  • sshpass is a command feeding the password into ssh via stdin, emulating terminal input.
  • StrictHostKeyChecking=no will disable the lookup of host-key of the target host in local whitelist. In case of freshly added machines to test-lab, the ssh command will not wait for manual confirmation to trust the new remote host.

Using Ubuntu sshpass can be installed as:

sudo apt-get install sshpass

Sunday, 20 March 2016

Lenovo X230 wifi stops - re-activate

I'm am using Ubuntu 15.10 on Lenovo X230. Sometimes the wifi does not work anymore, after waking the system up from suspend. The wif is available, but does not find any hotspots around.

After reboot the wifi is back again. But it is sufficient to unload and reload the kernel-module rtl8192ce

sudo rmmod -f rtl8192ce
sudo modprobe rtl8192ce


Afterwards the hotspots around will be detected again.

Monday, 21 December 2015

Installing Haskell IDE leksah in Ubuntu 15.10

On fresh Ubuntu-15.10 (Desktop) you must install the following packages to compile the Haskell IDE leksah.

$ sudo apt-get-install ghc ghc-testsuite cabal-install libgtk-3-dev libgtksourceview-3.0-dev alex happy libwebkitgtk-3.0-devlibghc-quickcheck-instances-dev doctest libghc-doctest-dev

https://wiki.haskell.org/Gtk2Hs/Installation
https://github.com/leksah/leksah/wiki/Getting-started-with-Leksah
$ mkdir -p ${HOME}/build/leksah
$ cd build/leksah
$ cabal sandbox init
$ cabal update
$ cabal install gtk2hs-buildtools
$ cabal install leksah


In case of cabal error messages please execute cabal in verbose mode to get to know the reason for the error
$ cabal install -v leksah

Finally leksah has been installed to the user specific sandbox  "${HOME}/build/leksah/.cabal-sandbox"  

Leksah is being started using the following command:

$ PATH="$PATH:${HOME}/build/leksah/.cabal-sandbox/bin" ${HOME}/build/leksah/.cabal-sandbox/bin/leksah
Starting up first time, Leksah will download and pre-compile some packages of Haskell. This may take some minutes.

Tuesday, 3 March 2015

Anniversary coming close: 
Almost 5 years ago I deployed  Android on the SmartQ7 tablet first time :)

https://skednet.wordpress.com/2010/04/09/qdroid-2-0-on-smartq7/


Wednesday, 11 June 2014

Lock/Unlock Icons for Encrypted Content/Devices

Hi, I am wondering why a mechanical old fashioned lock is used as icon to illustrate encrypted content. Thinking about this the following icons came into my mind, representing locked/protected data or unlocked/accessible data within a container. Maybe this icon will find its way into future software ;)