Skip to content

Instantly share code, notes, and snippets.

@leif
Created December 2, 2013 04:18
Show Gist options
  • Save leif/7745013 to your computer and use it in GitHub Desktop.
Save leif/7745013 to your computer and use it in GitHub Desktop.
mostly written by mischief on 2013-11-25
package main
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"os"
)
func EncHammerTime(in chan []byte, out chan []byte) error {
var data []byte
crap := make([]byte, 127)
for i := range crap {
crap[i] = 0xaa
}
for {
var framesize uint8
select {
case buf := <-in:
data = append(data, buf...)
framesize = uint8(math.Min(float64(len(data)), float64(127)))
for len(data) > 0 {
encoded := new(bytes.Buffer)
binary.Write(encoded, binary.LittleEndian, framesize)
encoded.Write(data[:framesize])
data = data[framesize:]
framesize = uint8(len(data))
out <- encoded.Bytes()
}
default:
framesize = 255
encoded := new(bytes.Buffer)
binary.Write(encoded, binary.LittleEndian, framesize)
encoded.Write(crap)
out <- encoded.Bytes()
}
}
return nil
}
func main() {
in := make(chan []byte, 1)
out := make(chan []byte, 1)
go EncHammerTime(in, out)
// read stdin
go func() {
buf := make([]byte, 127)
for {
_, err := os.Stdin.Read(buf)
if err != nil {
fmt.Fprintf(os.Stderr, "EOF")
os.Exit(1)
}
in <- buf
}
}()
func() {
for {
encoded := <-out
os.Stdout.Write(encoded)
}
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment