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.

242 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
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. // NOTE: may end up advancing the reader upon error.
  17. func PeekByte(r Unreader, n *int64, err *error) byte {
  18. byte_ := ReadByte(r, n, err)
  19. if *err != nil {
  20. return 0
  21. }
  22. *err = r.UnreadByte()
  23. return byte_
  24. }
  25. // Int8
  26. func WriteInt8(i int8, w io.Writer, n *int64, err *error) {
  27. WriteByte(byte(i), w, n, err)
  28. }
  29. func ReadInt8(r io.Reader, n *int64, err *error) int8 {
  30. return int8(ReadByte(r, n, err))
  31. }
  32. // Uint8
  33. func WriteUint8(i uint8, w io.Writer, n *int64, err *error) {
  34. WriteByte(byte(i), w, n, err)
  35. }
  36. func ReadUint8(r io.Reader, n *int64, err *error) uint8 {
  37. return uint8(ReadByte(r, n, err))
  38. }
  39. // Int16
  40. func WriteInt16(i int16, w io.Writer, n *int64, err *error) {
  41. buf := make([]byte, 2)
  42. binary.LittleEndian.PutUint16(buf, uint16(i))
  43. *n += 2
  44. WriteTo(buf, w, n, err)
  45. }
  46. func ReadInt16(r io.Reader, n *int64, err *error) int16 {
  47. buf := make([]byte, 2)
  48. ReadFull(buf, r, n, err)
  49. return int16(binary.LittleEndian.Uint16(buf))
  50. }
  51. // Uint16
  52. func WriteUint16(i uint16, w io.Writer, n *int64, err *error) {
  53. buf := make([]byte, 2)
  54. binary.LittleEndian.PutUint16(buf, uint16(i))
  55. *n += 2
  56. WriteTo(buf, w, n, err)
  57. }
  58. func ReadUint16(r io.Reader, n *int64, err *error) uint16 {
  59. buf := make([]byte, 2)
  60. ReadFull(buf, r, n, err)
  61. return uint16(binary.LittleEndian.Uint16(buf))
  62. }
  63. // []Uint16
  64. func WriteUint16s(iz []uint16, w io.Writer, n *int64, err *error) {
  65. WriteUint32(uint32(len(iz)), w, n, err)
  66. for _, i := range iz {
  67. WriteUint16(i, w, n, err)
  68. if *err != nil {
  69. return
  70. }
  71. }
  72. }
  73. func ReadUint16s(r io.Reader, n *int64, err *error) []uint16 {
  74. length := ReadUint32(r, n, err)
  75. if *err != nil {
  76. return nil
  77. }
  78. iz := make([]uint16, length)
  79. for j := uint32(0); j < length; j++ {
  80. ii := ReadUint16(r, n, err)
  81. if *err != nil {
  82. return nil
  83. }
  84. iz[j] = ii
  85. }
  86. return iz
  87. }
  88. // Int32
  89. func WriteInt32(i int32, w io.Writer, n *int64, err *error) {
  90. buf := make([]byte, 4)
  91. binary.LittleEndian.PutUint32(buf, uint32(i))
  92. *n += 4
  93. WriteTo(buf, w, n, err)
  94. }
  95. func ReadInt32(r io.Reader, n *int64, err *error) int32 {
  96. buf := make([]byte, 4)
  97. ReadFull(buf, r, n, err)
  98. return int32(binary.LittleEndian.Uint32(buf))
  99. }
  100. // Uint32
  101. func WriteUint32(i uint32, w io.Writer, n *int64, err *error) {
  102. buf := make([]byte, 4)
  103. binary.LittleEndian.PutUint32(buf, uint32(i))
  104. *n += 4
  105. WriteTo(buf, w, n, err)
  106. }
  107. func ReadUint32(r io.Reader, n *int64, err *error) uint32 {
  108. buf := make([]byte, 4)
  109. ReadFull(buf, r, n, err)
  110. return uint32(binary.LittleEndian.Uint32(buf))
  111. }
  112. // Int64
  113. func WriteInt64(i int64, w io.Writer, n *int64, err *error) {
  114. buf := make([]byte, 8)
  115. binary.LittleEndian.PutUint64(buf, uint64(i))
  116. *n += 8
  117. WriteTo(buf, w, n, err)
  118. }
  119. func ReadInt64(r io.Reader, n *int64, err *error) int64 {
  120. buf := make([]byte, 8)
  121. ReadFull(buf, r, n, err)
  122. return int64(binary.LittleEndian.Uint64(buf))
  123. }
  124. // Uint64
  125. func WriteUint64(i uint64, w io.Writer, n *int64, err *error) {
  126. buf := make([]byte, 8)
  127. binary.LittleEndian.PutUint64(buf, uint64(i))
  128. *n += 8
  129. WriteTo(buf, w, n, err)
  130. }
  131. func ReadUint64(r io.Reader, n *int64, err *error) uint64 {
  132. buf := make([]byte, 8)
  133. ReadFull(buf, r, n, err)
  134. return uint64(binary.LittleEndian.Uint64(buf))
  135. }
  136. // Varint
  137. func WriteVarint(i int, w io.Writer, n *int64, err *error) {
  138. buf := make([]byte, binary.MaxVarintLen64)
  139. n_ := int64(binary.PutVarint(buf, int64(i)))
  140. *n += n_
  141. WriteTo(buf[:n_], w, n, err)
  142. }
  143. func ReadVarint(r io.Reader, n *int64, err *error) int {
  144. res, n_, err_ := readVarint(r)
  145. *n += n_
  146. *err = err_
  147. return int(res)
  148. }
  149. // Uvarint
  150. func WriteUvarint(i uint, w io.Writer, n *int64, err *error) {
  151. buf := make([]byte, binary.MaxVarintLen64)
  152. n_ := int64(binary.PutUvarint(buf, uint64(i)))
  153. *n += n_
  154. WriteTo(buf[:n_], w, n, err)
  155. }
  156. func ReadUvarint(r io.Reader, n *int64, err *error) uint {
  157. res, n_, err_ := readUvarint(r)
  158. *n += n_
  159. *err = err_
  160. return uint(res)
  161. }
  162. //-----------------------------------------------------------------------------
  163. var overflow = errors.New("binary: varint overflows a 64-bit integer")
  164. // Modified to return number of bytes read, from
  165. // http://golang.org/src/pkg/encoding/binary/varint.go?s=3652:3699#L116
  166. func readUvarint(r io.Reader) (uint64, int64, error) {
  167. var x uint64
  168. var s uint
  169. var buf = make([]byte, 1)
  170. for i := 0; ; i++ {
  171. for {
  172. n, err := r.Read(buf)
  173. if err != nil {
  174. return x, int64(i), err
  175. }
  176. if n > 0 {
  177. break
  178. }
  179. }
  180. b := buf[0]
  181. if b < 0x80 {
  182. if i > 9 || i == 9 && b > 1 {
  183. return x, int64(i), overflow
  184. }
  185. return x | uint64(b)<<s, int64(i), nil
  186. }
  187. x |= uint64(b&0x7f) << s
  188. s += 7
  189. }
  190. }
  191. // Modified to return number of bytes read, from
  192. // http://golang.org/src/pkg/encoding/binary/varint.go?s=3652:3699#L116
  193. func readVarint(r io.Reader) (int64, int64, error) {
  194. ux, n, err := readUvarint(r) // ok to continue in presence of error
  195. x := int64(ux >> 1)
  196. if ux&1 != 0 {
  197. x = ^x
  198. }
  199. return x, n, err
  200. }