convert a binary file to ascii using hexdump

I have a binary file with data stored as two-byte big-endian 16-bit words. We need to extract the values in the file and print them in decimal ASCII format, so to obtain numbers in the 0-655535 range. let’s create the sample file: $ echo -en "\x01\x02\x03\x04\x05\x06\x07\x08" > file.bin and show its content in binary form: $ hexdump -C file.bin 00000000 01 02 03 04 05 06 07 08 |........| 00000008 to get the desired output we can use the powerful, but little documented format string option of hexdump: ...

October 20, 2016 · Andrea Manzini

playing with Crystal Programming Language

I’ve started experimenting with the Crystal Programming Language. It’s a nice and clean language with syntax similar to Ruby, but compiled to fast native code, and a lot of clever ideas, like union types and seamless C integration The project is still in early stages, but it’s promising. Just to see how easy, I ported a small python library to Crystal, you can find it on https://github.com/ilmanzo/spark. I hope to find the time to improve on it! ...

October 11, 2016 · Andrea Manzini

a simple HTTP rewriting proxy

This is an example of using goproxy, a fast and robust multithread proxy engine to develop an HTTP proxy that rewrites content on the fly, with multiple search and substitutions. It can be useful for debugging and other less noble (but useful) purposes … // rewriting_proxy project main.go package main import ( "bytes" "flag" "io/ioutil" "log" "net/http" "github.com/elazarl/goproxy" ) var replacements = []struct { from []byte to []byte }{ {[]byte("#e8ecec"), []byte("Red")}, // ugly colors!! {[]byte("Comic Sans MS"), []byte("Lucida Sans Unicode")}, // for eyes sanity {[]byte("Java "), []byte("Golang ")}, // just joking } func myHandler(resp *http.Response, ctx *goproxy.ProxyCtx) *http.Response { readBody, err := ioutil.ReadAll(resp.Body) if err != nil { //TODO handle read error gracefully return resp } resp.Body.Close() for _, elem := range replacements { readBody = bytes.Replace(readBody, elem.from, elem.to, -1) } resp.Body = ioutil.NopCloser(bytes.NewReader(readBody)) return resp } func main() { verbose := flag.Bool("v", true, "should every proxy request be logged to stdout") proxy := goproxy.NewProxyHttpServer() proxy.Verbose = *verbose proxy.OnResponse().DoFunc(myHandler) log.Fatal(http.ListenAndServe(":8081", proxy)) }

August 12, 2016 · Andrea Manzini

wrapping c plus plus classes in Python

This is a quick and dirty way to interface C++ code with Python, translating one or more C++ classes in Python objects. First, we need some c++ sample code: //myclass.h #ifndef MYCLASS_H #define MYCLASS_H #include <string> using namespace std; namespace pets { class Dog { public: Dog(string name, int age); virtual ~Dog(); string talk(); protected: string m_name; int m_age; }; } //myclass.cpp #include "myclass.h" #include <string> namespace pets { Dog::Dog(std::string name, int age): m_name(name),m_age(age) { } Dog::~Dog() { } std::string Dog::talk() { return "BARK! I am a DOG and my name is "+m_name; } } now, we can try a little test program just to exercise our class: ...

August 8, 2016 · Andrea Manzini