You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
5.0 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package binary
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "io"
  6. )
  7. // Byte
  8. func WriteByte(b byte, w io.Writer, n *int64, err *error) {
  9. WriteTo([]byte{b}, w, n, err)
  10. }
  11. func ReadByte(r io.Reader, n *int64, err *error) byte {
  12. buf := make([]byte, 1)
  13. ReadFull(buf, r, n, err)
  14. return buf[0]
  15. }
  16. // Int8
  17. func WriteInt8(i int8, w io.Writer, n *int64, err *error) {
  18. WriteByte(byte(i), w, n, err)
  19. }
  20. func ReadInt8(r io.Reader, n *int64, err *error) int8 {
  21. return int8(ReadByte(r, n, err))
  22. }
  23. // Uint8
  24. func WriteUint8(i uint8, w io.Writer, n *int64, err *error) {
  25. WriteByte(byte(i), w, n, err)
  26. }
  27. func ReadUint8(r io.Reader, n *int64, err *error) uint8 {
  28. return uint8(ReadByte(r, n, err))
  29. }
  30. // Int16
  31. func WriteInt16(i int16, w io.Writer, n *int64, err *error) {
  32. buf := make([]byte, 2)
  33. binary.BigEndian.PutUint16(buf, uint16(i))
  34. *n += 2
  35. WriteTo(buf, w, n, err)
  36. }
  37. func ReadInt16(r io.Reader, n *int64, err *error) int16 {
  38. buf := make([]byte, 2)
  39. ReadFull(buf, r, n, err)
  40. return int16(binary.BigEndian.Uint16(buf))
  41. }
  42. // Uint16
  43. func WriteUint16(i uint16, w io.Writer, n *int64, err *error) {
  44. buf := make([]byte, 2)
  45. binary.BigEndian.PutUint16(buf, uint16(i))
  46. *n += 2
  47. WriteTo(buf, w, n, err)
  48. }
  49. func ReadUint16(r io.Reader, n *int64, err *error) uint16 {
  50. buf := make([]byte, 2)
  51. ReadFull(buf, r, n, err)
  52. return uint16(binary.BigEndian.Uint16(buf))
  53. }
  54. // []Uint16
  55. func WriteUint16s(iz []uint16, w io.Writer, n *int64, err *error) {
  56. WriteUint32(uint32(len(iz)), w, n, err)
  57. for _, i := range iz {
  58. WriteUint16(i, w, n, err)
  59. if *err != nil {
  60. return
  61. }
  62. }
  63. }
  64. func ReadUint16s(r io.Reader, n *int64, err *error) []uint16 {
  65. length := ReadUint32(r, n, err)
  66. if *err != nil {
  67. return nil
  68. }
  69. iz := make([]uint16, length)
  70. for j := uint32(0); j < length; j++ {
  71. ii := ReadUint16(r, n, err)
  72. if *err != nil {
  73. return nil
  74. }
  75. iz[j] = ii
  76. }
  77. return iz
  78. }
  79. // Int32
  80. func WriteInt32(i int32, w io.Writer, n *int64, err *error) {
  81. buf := make([]byte, 4)
  82. binary.BigEndian.PutUint32(buf, uint32(i))
  83. *n += 4
  84. WriteTo(buf, w, n, err)
  85. }
  86. func ReadInt32(r io.Reader, n *int64, err *error) int32 {
  87. buf := make([]byte, 4)
  88. ReadFull(buf, r, n, err)
  89. return int32(binary.BigEndian.Uint32(buf))
  90. }
  91. // Uint32
  92. func WriteUint32(i uint32, w io.Writer, n *int64, err *error) {
  93. buf := make([]byte, 4)
  94. binary.BigEndian.PutUint32(buf, uint32(i))
  95. *n += 4
  96. WriteTo(buf, w, n, err)
  97. }
  98. func ReadUint32(r io.Reader, n *int64, err *error) uint32 {
  99. buf := make([]byte, 4)
  100. ReadFull(buf, r, n, err)
  101. return uint32(binary.BigEndian.Uint32(buf))
  102. }
  103. // Int64
  104. func WriteInt64(i int64, w io.Writer, n *int64, err *error) {
  105. buf := make([]byte, 8)
  106. binary.BigEndian.PutUint64(buf, uint64(i))
  107. *n += 8
  108. WriteTo(buf, w, n, err)
  109. }
  110. func ReadInt64(r io.Reader, n *int64, err *error) int64 {
  111. buf := make([]byte, 8)
  112. ReadFull(buf, r, n, err)
  113. return int64(binary.BigEndian.Uint64(buf))
  114. }
  115. // Uint64
  116. func WriteUint64(i uint64, w io.Writer, n *int64, err *error) {
  117. buf := make([]byte, 8)
  118. binary.BigEndian.PutUint64(buf, uint64(i))
  119. *n += 8
  120. WriteTo(buf, w, n, err)
  121. }
  122. func ReadUint64(r io.Reader, n *int64, err *error) uint64 {
  123. buf := make([]byte, 8)
  124. ReadFull(buf, r, n, err)
  125. return uint64(binary.BigEndian.Uint64(buf))
  126. }
  127. // Varint
  128. func uvarIntSize(i uint) int {
  129. if i < 1<<8 {
  130. return 1
  131. }
  132. if i < 1<<16 {
  133. return 2
  134. }
  135. if i < 1<<24 {
  136. return 3
  137. }
  138. if i < 1<<32 {
  139. return 4
  140. }
  141. if i < 1<<40 {
  142. return 5
  143. }
  144. if i < 1<<48 {
  145. return 6
  146. }
  147. if i < 1<<56 {
  148. return 7
  149. }
  150. return 8
  151. }
  152. func WriteVarint(i int, w io.Writer, n *int64, err *error) {
  153. var negate = false
  154. if i < 0 {
  155. negate = true
  156. i = -i
  157. }
  158. var size = uvarIntSize(uint(i))
  159. if negate {
  160. // e.g. 0xF1 for a single negative byte
  161. WriteUint8(uint8(size+0xF0), w, n, err)
  162. } else {
  163. WriteUint8(uint8(size), w, n, err)
  164. }
  165. buf := make([]byte, 8)
  166. binary.BigEndian.PutUint64(buf, uint64(i))
  167. WriteTo(buf[(8-size):], w, n, err)
  168. *n += int64(1 + size)
  169. }
  170. func ReadVarint(r io.Reader, n *int64, err *error) int {
  171. var size = ReadUint8(r, n, err)
  172. var negate = false
  173. if (size >> 4) == 0xF {
  174. negate = true
  175. size = size & 0x0F
  176. }
  177. if size > 8 {
  178. setFirstErr(err, errors.New("Varint overflow"))
  179. return 0
  180. }
  181. if size == 0 {
  182. setFirstErr(err, errors.New("Varint underflow"))
  183. return 0
  184. }
  185. buf := make([]byte, 8)
  186. ReadFull(buf[(8-size):], r, n, err)
  187. *n += int64(1 + size)
  188. var i = int(binary.BigEndian.Uint64(buf))
  189. if negate {
  190. return -i
  191. } else {
  192. return i
  193. }
  194. }
  195. // Uvarint
  196. func WriteUvarint(i uint, w io.Writer, n *int64, err *error) {
  197. var size = uvarIntSize(i)
  198. WriteUint8(uint8(size), w, n, err)
  199. buf := make([]byte, 8)
  200. binary.BigEndian.PutUint64(buf, uint64(i))
  201. WriteTo(buf[(8-size):], w, n, err)
  202. *n += int64(1 + size)
  203. }
  204. func ReadUvarint(r io.Reader, n *int64, err *error) uint {
  205. var size = ReadUint8(r, n, err)
  206. if size > 8 {
  207. setFirstErr(err, errors.New("Uvarint overflow"))
  208. return 0
  209. }
  210. if size == 0 {
  211. setFirstErr(err, errors.New("Uvarint underflow"))
  212. return 0
  213. }
  214. buf := make([]byte, 8)
  215. ReadFull(buf[(8-size):], r, n, err)
  216. *n += int64(1 + size)
  217. return uint(binary.BigEndian.Uint64(buf))
  218. }
  219. func setFirstErr(err *error, newErr error) {
  220. if *err == nil && newErr != nil {
  221. *err = newErr
  222. }
  223. }