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.

232 lines
4.8 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
  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.LittleEndian.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.LittleEndian.Uint16(buf))
  41. }
  42. // Uint16
  43. func WriteUint16(i uint16, w io.Writer, n *int64, err *error) {
  44. buf := make([]byte, 2)
  45. binary.LittleEndian.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.LittleEndian.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.LittleEndian.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.LittleEndian.Uint32(buf))
  90. }
  91. // Uint32
  92. func WriteUint32(i uint32, w io.Writer, n *int64, err *error) {
  93. buf := make([]byte, 4)
  94. binary.LittleEndian.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.LittleEndian.Uint32(buf))
  102. }
  103. // Int64
  104. func WriteInt64(i int64, w io.Writer, n *int64, err *error) {
  105. buf := make([]byte, 8)
  106. binary.LittleEndian.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.LittleEndian.Uint64(buf))
  114. }
  115. // Uint64
  116. func WriteUint64(i uint64, w io.Writer, n *int64, err *error) {
  117. buf := make([]byte, 8)
  118. binary.LittleEndian.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.LittleEndian.Uint64(buf))
  126. }
  127. // Varint
  128. func WriteVarint(i int, w io.Writer, n *int64, err *error) {
  129. buf := make([]byte, binary.MaxVarintLen64)
  130. n_ := int64(binary.PutVarint(buf, int64(i)))
  131. *n += n_
  132. WriteTo(buf[:n_], w, n, err)
  133. }
  134. func ReadVarint(r io.Reader, n *int64, err *error) int {
  135. res, n_, err_ := readVarint(r)
  136. *n += n_
  137. *err = err_
  138. return int(res)
  139. }
  140. // Uvarint
  141. func WriteUvarint(i uint, w io.Writer, n *int64, err *error) {
  142. buf := make([]byte, binary.MaxVarintLen64)
  143. n_ := int64(binary.PutUvarint(buf, uint64(i)))
  144. *n += n_
  145. WriteTo(buf[:n_], w, n, err)
  146. }
  147. func ReadUvarint(r io.Reader, n *int64, err *error) uint {
  148. res, n_, err_ := readUvarint(r)
  149. *n += n_
  150. *err = err_
  151. return uint(res)
  152. }
  153. //-----------------------------------------------------------------------------
  154. var overflow = errors.New("binary: varint overflows a 64-bit integer")
  155. // Modified to return number of bytes read, from
  156. // http://golang.org/src/pkg/encoding/binary/varint.go?s=3652:3699#L116
  157. func readUvarint(r io.Reader) (uint64, int64, error) {
  158. var x uint64
  159. var s uint
  160. var buf = make([]byte, 1)
  161. for i := 0; ; i++ {
  162. for {
  163. n, err := r.Read(buf)
  164. if err != nil {
  165. return x, int64(i), err
  166. }
  167. if n > 0 {
  168. break
  169. }
  170. }
  171. b := buf[0]
  172. if b < 0x80 {
  173. if i > 9 || i == 9 && b > 1 {
  174. return x, int64(i), overflow
  175. }
  176. return x | uint64(b)<<s, int64(i), nil
  177. }
  178. x |= uint64(b&0x7f) << s
  179. s += 7
  180. }
  181. }
  182. // Modified to return number of bytes read, from
  183. // http://golang.org/src/pkg/encoding/binary/varint.go?s=3652:3699#L116
  184. func readVarint(r io.Reader) (int64, int64, error) {
  185. ux, n, err := readUvarint(r) // ok to continue in presence of error
  186. x := int64(ux >> 1)
  187. if ux&1 != 0 {
  188. x = ^x
  189. }
  190. return x, n, err
  191. }