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.

270 lines
5.1 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
10 years ago
10 years ago
10 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 uint64) int {
  129. if i == 0 {
  130. return 0
  131. }
  132. if i < 1<<8 {
  133. return 1
  134. }
  135. if i < 1<<16 {
  136. return 2
  137. }
  138. if i < 1<<24 {
  139. return 3
  140. }
  141. if i < 1<<32 {
  142. return 4
  143. }
  144. if i < 1<<40 {
  145. return 5
  146. }
  147. if i < 1<<48 {
  148. return 6
  149. }
  150. if i < 1<<56 {
  151. return 7
  152. }
  153. return 8
  154. }
  155. func WriteVarint(i int, w io.Writer, n *int64, err *error) {
  156. var negate = false
  157. if i < 0 {
  158. negate = true
  159. i = -i
  160. }
  161. var size = uvarintSize(uint64(i))
  162. if negate {
  163. // e.g. 0xF1 for a single negative byte
  164. WriteUint8(uint8(size+0xF0), w, n, err)
  165. } else {
  166. WriteUint8(uint8(size), w, n, err)
  167. }
  168. if size > 0 {
  169. buf := make([]byte, 8)
  170. binary.BigEndian.PutUint64(buf, uint64(i))
  171. WriteTo(buf[(8-size):], w, n, err)
  172. }
  173. *n += int64(1 + size)
  174. }
  175. func ReadVarint(r io.Reader, n *int64, err *error) int {
  176. var size = ReadUint8(r, n, err)
  177. var negate = false
  178. if (size >> 4) == 0xF {
  179. negate = true
  180. size = size & 0x0F
  181. }
  182. if size > 8 {
  183. setFirstErr(err, errors.New("Varint overflow"))
  184. return 0
  185. }
  186. if size == 0 {
  187. if negate {
  188. setFirstErr(err, errors.New("Varint does not allow negative zero"))
  189. }
  190. return 0
  191. }
  192. buf := make([]byte, 8)
  193. ReadFull(buf[(8-size):], r, n, err)
  194. *n += int64(1 + size)
  195. var i = int(binary.BigEndian.Uint64(buf))
  196. if negate {
  197. return -i
  198. } else {
  199. return i
  200. }
  201. }
  202. // Uvarint
  203. func WriteUvarint(i uint, w io.Writer, n *int64, err *error) {
  204. var size = uvarintSize(uint64(i))
  205. WriteUint8(uint8(size), w, n, err)
  206. if size > 0 {
  207. buf := make([]byte, 8)
  208. binary.BigEndian.PutUint64(buf, uint64(i))
  209. WriteTo(buf[(8-size):], w, n, err)
  210. }
  211. *n += int64(1 + size)
  212. }
  213. func ReadUvarint(r io.Reader, n *int64, err *error) uint {
  214. var size = ReadUint8(r, n, err)
  215. if size > 8 {
  216. setFirstErr(err, errors.New("Uvarint overflow"))
  217. return 0
  218. }
  219. if size == 0 {
  220. return 0
  221. }
  222. buf := make([]byte, 8)
  223. ReadFull(buf[(8-size):], r, n, err)
  224. *n += int64(1 + size)
  225. return uint(binary.BigEndian.Uint64(buf))
  226. }
  227. func setFirstErr(err *error, newErr error) {
  228. if *err == nil && newErr != nil {
  229. *err = newErr
  230. }
  231. }