|  | 
|  | 1 | +package main | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"fmt" | 
|  | 5 | +	"io" | 
|  | 6 | +	"log" | 
|  | 7 | +	"os" | 
|  | 8 | + | 
|  | 9 | +	tcobs "github.com/rokath/tcobs/v1" | 
|  | 10 | +	"github.com/spf13/afero" | 
|  | 11 | +) | 
|  | 12 | + | 
|  | 13 | +var ( | 
|  | 14 | +	// do not initialize, goreleaser will handle that | 
|  | 15 | +	version string | 
|  | 16 | + | 
|  | 17 | +	// do not initialize, goreleaser will handle that | 
|  | 18 | +	commit string | 
|  | 19 | + | 
|  | 20 | +	// do not initialize, goreleaser will handle that | 
|  | 21 | +	date string | 
|  | 22 | +) | 
|  | 23 | + | 
|  | 24 | +// main is the entry point. | 
|  | 25 | +func main() { | 
|  | 26 | +	fSys := &afero.Afero{Fs: afero.NewOsFs()} // os.DirFS("") | 
|  | 27 | +	doit(os.Stdout, fSys) | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +func doit(w io.Writer, fSys *afero.Afero) { | 
|  | 31 | + | 
|  | 32 | +	if len(os.Args) != 1 { | 
|  | 33 | +		fmt.Fprintln(w, version, commit, date) | 
|  | 34 | +		fmt.Fprintln(w, "Feed with a space separated byte sequence to decode a TCOBSv1 sequence.") | 
|  | 35 | +		fmt.Fprintln(w, "Example: `echo 64 1 2 18 88 25 88 161 | TCOBSv1Decode` will return `0 0 1 2 2 2 2 88 88 88 88 88 88`") | 
|  | 36 | +		return | 
|  | 37 | +	} | 
|  | 38 | + | 
|  | 39 | +	var i []byte | 
|  | 40 | +	o := make([]byte, 16*1024) | 
|  | 41 | +	var pos int | 
|  | 42 | +	for { | 
|  | 43 | +		var n byte | 
|  | 44 | +		_, err := fmt.Scan(&n) | 
|  | 45 | +		if err == io.EOF { | 
|  | 46 | +			if len(i)+31/len(i) > len(o) { | 
|  | 47 | +				log.Fatal("len of internal buffer too small") | 
|  | 48 | +			} | 
|  | 49 | +			count := tcobs.CDecode(o, i) | 
|  | 50 | +			if count < 0 { | 
|  | 51 | +				log.Fatal("invalid input data, error code ", count) | 
|  | 52 | +			} | 
|  | 53 | +			o = o[len(o)-count:] | 
|  | 54 | +			for _, b := range o { | 
|  | 55 | +				fmt.Fprintf(w, "%d ", b) | 
|  | 56 | +			} | 
|  | 57 | +			return | 
|  | 58 | +		} | 
|  | 59 | +		if err != nil { | 
|  | 60 | +			log.Fatal(err, " at position ", pos) | 
|  | 61 | +		} | 
|  | 62 | +		i = append(i, n) | 
|  | 63 | +		pos++ | 
|  | 64 | +	} | 
|  | 65 | +} | 
0 commit comments