12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package codex
- import (
- "fmt"
- "strconv"
- )
- type CodexBitmap struct {
- id string
- fields []Codex
- }
- func (c *CodexBitmap) init(cfg map[string]interface{}) {
- if v, ok := cfg["id"].(string); ok {
- c.id = v
- } else {
- panic(fmt.Sprintf("Invalid CodexBitmap.ID %+v", cfg))
- }
- if v, ok := cfg["fields"].([]interface{}); ok {
- c.fields = make([]Codex, len(v))
- for i,f := range v {
- if fm, ok := f.(map[string]interface{}); ok {
- c.fields[i] = parseType(fm)
- } else {
- panic(fmt.Sprintf("Invalid field %d %+v", i, f))
- }
- }
- } else {
- panic(fmt.Sprintf("Invalid CodexBitmap.Fields %+v", cfg))
- }
- }
- func (c *CodexBitmap) ID() string {
- return c.id
- }
- func (c *CodexBitmap) Decode(data []byte, pos int, value interface{}) (int, error) {
- ppos := pos + 16
- if len(data) < ppos {
- return pos, fmt.Errorf("incomplete packet %d %d", len(data), ppos)
- }
- var err error
- bi := 0
- var bitmap [128]bool
- for i := pos; i<ppos; i++ {
- bbi, err := strconv.ParseInt(string(data[i:i+1]), 16, 32)
- if err != nil {
- panic(err)
- }
- ba := int64(8)
- for j := 1; j <= 4; j++ {
- bitmap[bi] = (bbi & ba) != 0
- ba >>= 1
- bi++
- }
- }
- if bitmap[0] {
- ppos1 := ppos
- ppos = ppos + 16
- if len(data) < ppos {
- return pos, fmt.Errorf("incomplete packet %d %d", len(data), ppos)
- }
- for i := ppos1; i<ppos; i++ {
- bbi, err := strconv.ParseInt(string(data[i:i+1]), 16, 32)
- if err != nil {
- panic(err)
- }
- ba := int64(8)
- for j := 1; j <= 4; j++ {
- bitmap[bi] = (bbi & ba) != 0
- ba >>= 1
- bi++
- }
- }
- }
- for i := 0; i < 127; i++ {
- if bitmap[i+1] {
- ppos, err = c.fields[i].Decode(data, ppos, value)
- if err != nil {
- return pos, err
- }
- }
- }
- return ppos, nil
- }
|