the packet types are all wrong fuck

master
Jordan Orelli 8 years ago
parent 9b4075c881
commit 96a32e861a

@ -1,6 +1,7 @@
package main
import (
"fmt"
"io"
)
@ -49,3 +50,68 @@ func (b *bitBuffer) readBits(bits uint) uint64 {
b.bits -= bits
return n
}
func (b *bitBuffer) readByte() (out byte) {
if b.bits == 0 {
if b.index >= len(b.source) {
b.err = io.ErrUnexpectedEOF
return
}
out = b.source[b.index]
b.index += 1
return
}
return byte(b.readBits(8))
}
func (b *bitBuffer) readBytes(n int) []byte {
if b.bits == 0 {
b.index += n
if b.index > len(b.source) {
b.err = io.ErrUnexpectedEOF
return []byte{}
}
return b.source[b.index-n : b.index]
}
buf := make([]byte, n)
for i := 0; i < n; i++ {
buf[i] = byte(b.readBits(8))
}
return buf
}
// readVarUint reads a variable-length uint32, encoded with some scheme that I
// can't find a standard for. first two bits are a length prefix, followed by a
// 4, 8, 12, or 32-bit wide uint.
func (b *bitBuffer) readVarUint() uint32 {
switch b.readBits(2) {
case 0:
return uint32(b.readBits(4))
case 1:
return uint32(b.readBits(4) | b.readBits(4)<<4)
case 2:
return uint32(b.readBits(4) | b.readBits(8)<<4)
case 3:
return uint32(b.readBits(4) | b.readBits(28)<<4)
default:
// this switch is already exhaustive, the compiler just can't tell.
panic(fmt.Sprintf("invalid varuint prefix"))
}
}
// readVarInt reads a varint-encoded value off of the front of the buffer. This
// is the varint encoding used in protobuf. That is: each byte utilizes a 7-bit
// group. the msb of each byte indicates whether there are more bytes to
// follow.
func (b *bitBuffer) readVarInt() uint64 {
var x, n uint64
for shift := uint(0); shift < 64; shift += 7 {
n = b.readBits(8)
if n < 0x80 {
return x | n<<shift
}
x |= n &^ 0x80 << shift
}
b.err = fmt.Errorf("readVarInt never saw the end of varint")
return 0
}

@ -34,4 +34,50 @@ func TestBits(t *testing.T) {
if bb.err != nil {
t.Errorf("fuck")
}
buf = []byte{0x4}
bb = newBitBuffer(buf)
u := bb.readVarUint()
if u != 1 {
t.Errorf("feck. wanted %v, got %v", 1, u)
}
if bb.readBits(2); bb.err != nil {
t.Errorf("shouldn't have an error yet")
}
if bb.readBits(1); bb.err == nil {
t.Errorf("we should be at EOF now")
}
buf = []byte{0x3c}
bb = newBitBuffer(buf)
u = bb.readVarUint()
if u != 15 {
t.Errorf("feck. wanted %v, got %v", 15, u)
}
if bb.readBits(2); bb.err != nil {
t.Errorf("shouldn't have an error yet")
}
if bb.readBits(1); bb.err == nil {
t.Errorf("we should be at EOF now")
}
buf = []byte{0x48, 0x10}
// 0100 1000 0001 0000
// 01 - prefix bits. indicates length 12
// 00 10 - least significant four
// 00 0001 00 - most significant eight
// 00 - not read.
//
// 0000 0100 0010 - actual value (0x42, or 66)
bb = newBitBuffer(buf)
u = bb.readVarUint()
if u != 66 {
t.Errorf("feck. wanted %v, got %v", 66, u)
}
if bb.readBits(2); bb.err != nil {
t.Errorf("shouldn't have an error yet")
}
if bb.readBits(1); bb.err == nil {
t.Errorf("we should be at EOF now")
}
}

@ -40,8 +40,20 @@ func (m *message) check(dump bool) error {
if err := proto.Unmarshal(m.body, packet); err != nil {
return wrap(err, "onPacket unable to unmarshal message body")
}
if dump {
fmt.Printf("{in: %d out: %d data: %x}\n", packet.GetSequenceIn(), packet.GetSequenceOutAck(), packet.GetData()[:8])
shit := packet.GetData()[:4]
bb := newBitBuffer(packet.GetData())
type T struct {
t int32
}
var v T
v.t = int32(bb.readVarUint())
if bb.err != nil {
fmt.Printf("packet error: %v\n", bb.err)
} else {
fmt.Printf("{in: %d out: %d data: %v shit: %x}\n", packet.GetSequenceIn(), packet.GetSequenceOutAck(), v, shit)
}
}
return nil
}

@ -12,16 +12,13 @@ type parser struct {
// the source of replay bytes. Must NOT be compressed.
source *bufio.Reader
// re-useable scratch buffer. Contents never guaranteed to be clean.
scratch []byte
dumpMessages bool
dumpPackets bool
}
func newParser(r io.Reader) *parser {
br := bufio.NewReaderSize(r, 1<<16)
return &parser{source: br, scratch: make([]byte, 1<<10)}
return &parser{source: br}
}
func (p *parser) start() error {
@ -56,13 +53,6 @@ func (p *parser) run() error {
}
}
// grows the scratch buffer until it is at least n bytes wide
func (p *parser) growScratch(n int) {
for len(p.scratch) < n {
p.scratch = make([]byte, 2*len(p.scratch))
}
}
// DecodeVarint reads a varint-encoded integer from the source reader.
// It returns the value as a uin64 and any errors encountered. The reader will
// be advanced by the number of bytes needed to consume this value. On error,
@ -102,8 +92,7 @@ func (p *parser) decodeVarint() (uint64, error) {
// the beginning of the scratch buffer. it will be corrupted on the next call
// to readn or the next operation that utilizes the scratch buffer.
func (p *parser) readn(n int) ([]byte, error) {
p.growScratch(n)
buf := p.scratch[:n]
buf := make([]byte, n)
if _, err := io.ReadFull(p.source, buf); err != nil {
return nil, wrap(err, "error reading %d bytes", n)
}

Loading…
Cancel
Save