codex_bitmap.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package codex
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. type CodexBitmap struct {
  7. id string
  8. fields []Codex
  9. }
  10. func (c *CodexBitmap) init(cfg map[string]interface{}) {
  11. if v, ok := cfg["id"].(string); ok {
  12. c.id = v
  13. } else {
  14. panic(fmt.Sprintf("Invalid CodexBitmap.ID %+v", cfg))
  15. }
  16. if v, ok := cfg["fields"].([]interface{}); ok {
  17. c.fields = make([]Codex, len(v))
  18. for i,f := range v {
  19. if fm, ok := f.(map[string]interface{}); ok {
  20. c.fields[i] = parseType(fm)
  21. } else {
  22. panic(fmt.Sprintf("Invalid field %d %+v", i, f))
  23. }
  24. }
  25. } else {
  26. panic(fmt.Sprintf("Invalid CodexBitmap.Fields %+v", cfg))
  27. }
  28. }
  29. func (c *CodexBitmap) ID() string {
  30. return c.id
  31. }
  32. func (c *CodexBitmap) Decode(data []byte, pos int, value interface{}) (int, error) {
  33. ppos := pos + 16
  34. if len(data) < ppos {
  35. return pos, fmt.Errorf("incomplete packet %d %d", len(data), ppos)
  36. }
  37. var err error
  38. bi := 0
  39. var bitmap [128]bool
  40. for i := pos; i<ppos; i++ {
  41. bbi, err := strconv.ParseInt(string(data[i:i+1]), 16, 32)
  42. if err != nil {
  43. panic(err)
  44. }
  45. ba := int64(8)
  46. for j := 1; j <= 4; j++ {
  47. bitmap[bi] = (bbi & ba) != 0
  48. ba >>= 1
  49. bi++
  50. }
  51. }
  52. if bitmap[0] {
  53. ppos1 := ppos
  54. ppos = ppos + 16
  55. if len(data) < ppos {
  56. return pos, fmt.Errorf("incomplete packet %d %d", len(data), ppos)
  57. }
  58. for i := ppos1; i<ppos; i++ {
  59. bbi, err := strconv.ParseInt(string(data[i:i+1]), 16, 32)
  60. if err != nil {
  61. panic(err)
  62. }
  63. ba := int64(8)
  64. for j := 1; j <= 4; j++ {
  65. bitmap[bi] = (bbi & ba) != 0
  66. ba >>= 1
  67. bi++
  68. }
  69. }
  70. }
  71. for i := 0; i < 127; i++ {
  72. if bitmap[i+1] {
  73. ppos, err = c.fields[i].Decode(data, ppos, value)
  74. if err != nil {
  75. return pos, err
  76. }
  77. }
  78. }
  79. return ppos, nil
  80. }
  81. func (c *CodexBitmap) Encode(data []byte, pos int, value interface{}) (int, error) {
  82. if vm, ok := value.(map[string]interface{}); ok {
  83. ppos := pos
  84. var bitmap [128]bool
  85. for i, f := range c.fields {
  86. bitmap[i+1] = vm[f.ID()] != nil
  87. }
  88. for i := 64; i<128; i++ {
  89. if bitmap[i] {
  90. bitmap[0] = true
  91. break
  92. }
  93. }
  94. bi := 0
  95. for i := 0; i<16; i++ {
  96. b := byte(0)
  97. ba := byte(8)
  98. for j := 1; j <= 4; j++ {
  99. if bitmap[bi] {
  100. b |= ba
  101. }
  102. ba >>= 1
  103. bi += 1
  104. }
  105. if b > 9 {
  106. data[ppos] = 38 + b
  107. } else {
  108. data[ppos] = 48 + b
  109. }
  110. ppos +=1
  111. }
  112. if bitmap[0] {
  113. for i := 0; i<16; i++ {
  114. b := byte(0)
  115. ba := byte(8)
  116. for j := 1; j <= 4; j++ {
  117. if bitmap[bi] {
  118. b |= ba
  119. }
  120. ba >>= 1
  121. bi += 1
  122. }
  123. if b > 9 {
  124. data[ppos] = 38 + b
  125. } else {
  126. data[ppos] = 48 + b
  127. }
  128. ppos +=1
  129. }
  130. }
  131. var err error
  132. for i, f := range c.fields {
  133. if bitmap[i+1] {
  134. ppos, err = f.Encode(data, ppos, value)
  135. if err != nil {
  136. return pos, err
  137. }
  138. }
  139. }
  140. return ppos, nil
  141. }
  142. panic(fmt.Sprintf("Invalid value type [%s] %+v", c.id, value))
  143. }