il linguaggio Lua: parte 3

segue dalla seconda parte Che fai tu luna in ciel ? : le funzioni Fino a che scriviamo script di poche righe, possiamo inserire le istruzioni nel programma principale, ma aumentando la complessità diventa necessario organizzare il codice in pezzi indipendenti e riutilizzabili; come in tutti gli altri linguaggi, in Lua è possibile definire funzioni; vediamo un esempio piuttosto classico: function fattoriale(n) local f=1 -- variabile locale alla funzione for i=2,n do f=f*i end return f Abbiamo definito la funzione fattoriale, che da ora in avanti possiamo richiamare nel nostro codice: ...

March 20, 2017 · Andrea Manzini

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