uint64 fixed is a thing

types
Jordan Orelli 8 years ago
parent 701d08ec7c
commit 96cf379192

@ -61,62 +61,42 @@ func ReadUBitVarFP(r Reader) uint64 {
// in little-endian order. The most-significant bit of each byte represents a // in little-endian order. The most-significant bit of each byte represents a
// continuation bit. // continuation bit.
func ReadVarInt(r Reader) uint64 { func ReadVarInt(r Reader) uint64 {
var ( var x uint64
x uint64 var s uint
b uint64 for i := 0; ; i++ {
shift uint b := r.ReadBits(8)
)
for ; shift < 64; shift += 7 {
b = r.ReadBits(8)
if r.Err() != nil { if r.Err() != nil {
return 0 return 0
} }
x |= b & 0x7f << shift if b < 0x80 {
if b&0x80 == 0 { if i > 9 || i == 9 && b > 1 {
return x panic("varint overflow")
}
return x | b<<s
} }
x |= b & 0x7f << s
s += 7
} }
return x
} }
// reads a 32bit varint // reads a 32bit varint
func ReadVarInt32(r Reader) uint32 { func ReadVarInt32(r Reader) uint32 {
var ( var x uint32
x uint64 var s uint
b uint64 for i := 0; ; i++ {
shift uint b := r.ReadBits(8)
)
for ; shift < 32; shift += 7 {
b = r.ReadBits(8)
if r.Err() != nil { if r.Err() != nil {
return 0 return 0
} }
x |= b & 0x7f << shift if b < 0x80 {
if b&0x80 == 0 { if i > 4 || i == 4 && b > 0xf {
return uint32(x) panic("varint32 overflow")
}
}
return uint32(x)
}
// reads a 32bit varint
func ReadVarInt16(r Reader) uint16 {
var (
x uint64
b uint64
shift uint
)
for ; shift < 16; shift += 7 {
b = r.ReadBits(8)
if r.Err() != nil {
return 0
} }
x |= b & 0x7f << shift return x | uint32(b)<<s
if b&0x80 == 0 {
return uint16(x)
} }
x |= uint32(b&0x7f) << s
s += 7
} }
return uint16(x)
} }
func ReadBool(r Reader) bool { func ReadBool(r Reader) bool {

@ -20,10 +20,10 @@ func arrayType(spec *typeSpec, env *Env) tÿpe {
elemSpec := *spec elemSpec := *spec
elemSpec.typeName = elemName elemSpec.typeName = elemName
elemType := parseTypeSpec(&elemSpec, env) elemType := parseTypeSpec(&elemSpec, env)
return &array_t{elemType, count} return &array_t{elem: elemType, count: count}
} }
func parseArrayName(s string) (string, int) { func parseArrayName(s string) (string, uint) {
runes := []rune(s) runes := []rune(s)
if runes[len(runes)-1] != ']' { if runes[len(runes)-1] != ']' {
panic("invalid array type name: " + s) panic("invalid array type name: " + s)
@ -38,7 +38,7 @@ func parseArrayName(s string) (string, int) {
panic("invalid array type name: " + err.Error()) panic("invalid array type name: " + err.Error())
} }
} }
return strings.TrimSpace(string(runes[:i])), n return strings.TrimSpace(string(runes[:i])), uint(n)
} }
} }
panic("invalid array type name: " + s) panic("invalid array type name: " + s)
@ -46,7 +46,15 @@ func parseArrayName(s string) (string, int) {
type array_t struct { type array_t struct {
elem tÿpe elem tÿpe
count int count uint
bits uint
}
func (t *array_t) sizeBits() uint {
if t.bits == 0 {
t.bits = bit.Length(t.count)
}
return t.bits
} }
func (t *array_t) nü() value { return array{t: t, slots: make([]value, t.count)} } func (t *array_t) nü() value { return array{t: t, slots: make([]value, t.count)} }
@ -60,7 +68,9 @@ type array struct {
func (a array) tÿpe() tÿpe { return a.t } func (a array) tÿpe() tÿpe { return a.t }
func (a array) read(r bit.Reader) error { func (a array) read(r bit.Reader) error {
for i := range a.slots { n := r.ReadBits(a.t.bits)
Debug.Printf("reading %d array elements", n)
for i := uint64(0); i < n; i++ {
if a.slots[i] == nil { if a.slots[i] == nil {
a.slots[i] = a.t.elem.nü() a.slots[i] = a.t.elem.nü()
} }

@ -48,7 +48,7 @@ type uint8_v uint8
func (v uint8_v) tÿpe() tÿpe { return uint8_t } func (v uint8_v) tÿpe() tÿpe { return uint8_t }
func (v *uint8_v) read(r bit.Reader) error { func (v *uint8_v) read(r bit.Reader) error {
*v = uint8_v(r.ReadBits(8)) *v = uint8_v(bit.ReadVarInt32(r))
return r.Err() return r.Err()
} }
@ -71,7 +71,7 @@ type uint16_v uint16
func (v uint16_v) tÿpe() tÿpe { return uint16_t } func (v uint16_v) tÿpe() tÿpe { return uint16_t }
func (v *uint16_v) read(r bit.Reader) error { func (v *uint16_v) read(r bit.Reader) error {
*v = uint16_v(bit.ReadVarInt16(r)) *v = uint16_v(bit.ReadVarInt32(r))
return r.Err() return r.Err()
} }
@ -94,11 +94,7 @@ type uint32_v uint32
func (v uint32_v) tÿpe() tÿpe { return uint32_t } func (v uint32_v) tÿpe() tÿpe { return uint32_t }
func (v *uint32_v) read(r bit.Reader) error { func (v *uint32_v) read(r bit.Reader) error {
u := bit.ReadVarInt(r) *v = uint32_v(bit.ReadVarInt32(r))
if u > 1<<32-1 {
return fmt.Errorf("uint32 overflow: %d", u)
}
*v = uint32_v(u)
return r.Err() return r.Err()
} }
@ -121,7 +117,7 @@ type uint64_v uint64
func (v uint64_v) tÿpe() tÿpe { return uint64_t } func (v uint64_v) tÿpe() tÿpe { return uint64_t }
func (v *uint64_v) read(r bit.Reader) error { func (v *uint64_v) read(r bit.Reader) error {
*v = uint64_v(bit.ReadVarInt(r)) *v = uint64_v(bit.ReadVarInt32(r))
return r.Err() return r.Err()
} }
@ -129,6 +125,31 @@ func (v uint64_v) String() string {
return strconv.FormatUint(uint64(v), 10) return strconv.FormatUint(uint64(v), 10)
} }
// ------------------------------------------------------------------------------
// uint64fixed
//
// (a uint64 value that is always represented on the wire with 64 bits)
// ------------------------------------------------------------------------------
var uint64fixed_t = &typeLiteral{
name: "uint64fixed",
newFn: func() value {
return new(uint64fixed_v)
},
}
type uint64fixed_v uint64
func (v uint64fixed_v) tÿpe() tÿpe { return uint64fixed_t }
func (v *uint64fixed_v) read(r bit.Reader) error {
*v = uint64fixed_v(r.ReadBits(64))
return r.Err()
}
func (v uint64fixed_v) String() string {
return strconv.FormatUint(uint64(v), 10)
}
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int8 // int8
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@ -153,6 +174,30 @@ func (v int8_v) String() string {
return strconv.FormatInt(int64(v), 10) return strconv.FormatInt(int64(v), 10)
} }
// ------------------------------------------------------------------------------
// int16
// ------------------------------------------------------------------------------
var int16_t = &typeLiteral{
name: "int16",
newFn: func() value {
return new(int16_v)
},
}
type int16_v int16
func (v int16_v) tÿpe() tÿpe { return int16_t }
func (v *int16_v) read(r bit.Reader) error {
// TODO: bounds check here?
*v = int16_v(bit.ReadZigZag32(r))
return r.Err()
}
func (v int16_v) String() string {
return strconv.FormatInt(int64(v), 10)
}
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// int32 // int32
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@ -176,6 +221,29 @@ func (v int32_v) String() string {
return strconv.FormatInt(int64(v), 10) return strconv.FormatInt(int64(v), 10)
} }
// ------------------------------------------------------------------------------
// int64
// ------------------------------------------------------------------------------
var int64_t = &typeLiteral{
name: "int64",
newFn: func() value {
return new(int64_v)
},
}
type int64_v int64
func (v int64_v) tÿpe() tÿpe { return int64_t }
func (v *int64_v) read(r bit.Reader) error {
*v = int64_v(bit.ReadZigZag(r))
return r.Err()
}
func (v int64_v) String() string {
return strconv.FormatInt(int64(v), 10)
}
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
// CUtlStringToken // CUtlStringToken
// //
@ -229,24 +297,70 @@ func (c color) String() string {
return fmt.Sprintf("#%x%x%x%x", c.r, c.g, c.b, c.a) return fmt.Sprintf("#%x%x%x%x", c.r, c.g, c.b, c.a)
} }
// ------------------------------------------------------------------------------
// CUtlSymbolLarge
// ------------------------------------------------------------------------------
var cutl_string_t = typeLiteral{
name: "CUtlSymbolLarge",
newFn: func() value {
return new(cutl_string_v)
},
}
type cutl_string_v string
func (v cutl_string_v) tÿpe() tÿpe { return cutl_string_t }
func (v cutl_string_v) String() string { return string(v) }
func (v *cutl_string_v) read(r bit.Reader) error {
*v = cutl_string_v(bit.ReadString(r))
return r.Err()
}
var atom_types = []tÿpe{ var atom_types = []tÿpe{
bool_t, bool_t,
uint8_t, uint8_t,
uint16_t, uint16_t,
uint32_t, uint32_t,
uint64_t,
int8_t, int8_t,
int16_t,
int32_t, int32_t,
int64_t,
stringToken_t, stringToken_t,
color_t, color_t,
cutl_string_t,
} }
func atomType(spec *typeSpec, env *Env) tÿpe { func atomType(spec *typeSpec, env *Env) tÿpe {
for _, t := range atom_types { for _, t := range atom_types {
if t.typeName() == spec.typeName { if t.typeName() == spec.typeName {
Debug.Printf(" atom type: %s", t.typeName()) Debug.Printf(" atom type: %s", t.typeName())
if spec.bits != 0 {
return typeError("spec can't be atom type: has bit specification: %v", spec)
}
if spec.encoder != "" {
return typeError("spec can't be atom type: has encoder specification: %v", spec)
}
if spec.flags != 0 {
return typeError("spec can't be atom type: has flags: %v", spec)
}
if spec.high != 0 {
return typeError("spec can't be atom type: has high value constraint: %v", spec)
}
if spec.low != 0 {
return typeError("spec can't be atom type: has low value constraint: %v", spec)
}
if spec.serializer != "" {
return typeError("spec can't be atom type: has serializer: %v", spec)
}
return t return t
} }
} }
if spec.typeName == "uint64" {
if spec.encoder == "fixed64" {
return uint64fixed_t
}
return uint64_t
}
return nil return nil
} }

@ -63,6 +63,9 @@ func (e *Env) mergeSendTables(m *dota.CDemoSendTables) error {
if err != nil { if err != nil {
return wrap(err, "unable to get serializers in sendtables") return wrap(err, "unable to get serializers in sendtables")
} }
for i, s := range flat.GetSymbols() {
Debug.Printf("symbol %d: %s", i, s)
}
e.symbols = symbolTable(flat.GetSymbols()) e.symbols = symbolTable(flat.GetSymbols())
e.stubClasses(flat) e.stubClasses(flat)
if err := e.parseFields(flat); err != nil { if err := e.parseFields(flat); err != nil {

@ -42,10 +42,10 @@ func genericName(name string) ([2]string, error) {
for i, r := range runes { for i, r := range runes {
if r == '<' { if r == '<' {
if depth == 0 { if depth == 0 {
b_start = i
out[0] = strings.TrimSpace(string(runes[0:i])) out[0] = strings.TrimSpace(string(runes[0:i]))
} }
depth++ depth++
b_start = i
} }
if r == '>' { if r == '>' {
depth-- depth--
@ -89,6 +89,7 @@ func (v *cutl_vector) tÿpe() tÿpe { return v.t }
func (v *cutl_vector) read(r bit.Reader) error { func (v *cutl_vector) read(r bit.Reader) error {
count := bit.ReadVarInt32(r) count := bit.ReadVarInt32(r)
Debug.Printf("allocating cutl_vector of size %d with element type %s", count, v.t.elem.typeName())
v.slots = make([]value, count) v.slots = make([]value, count)
return r.Err() return r.Err()
} }

@ -30,11 +30,12 @@ func (s selection) fillSlotsIter(offset int, dest slotted, path string, r bit.Re
} }
switch s.count - offset { switch s.count - offset {
case 1: case 1:
old := dest.getSlotValue(slot)
v := dest.slotType(slot).nü() v := dest.slotType(slot).nü()
Debug.Printf("%v %s.%s (%s) %v read", s, path, dest.slotName(slot), dest.slotType(slot).typeName(), old)
if err := v.read(r); err != nil { if err := v.read(r); err != nil {
return fmt.Errorf("unable to fill selection %v for %s.%s (%s): %v", s, path, dest.slotName(slot), dest.slotType(slot).typeName(), err) return fmt.Errorf("unable to fill selection %v for %s.%s (%s): %v", s, path, dest.slotName(slot), dest.slotType(slot).typeName(), err)
} }
old := dest.getSlotValue(slot)
dest.setSlotValue(slot, v) dest.setSlotValue(slot, v)
Debug.Printf("%v %s.%s (%s) %v -> %v", s, path, dest.slotName(slot), dest.slotType(slot).typeName(), old, v) Debug.Printf("%v %s.%s (%s) %v -> %v", s, path, dest.slotName(slot), dest.slotType(slot).typeName(), old, v)
return nil return nil

Loading…
Cancel
Save