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.

359 lines
6.9 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
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
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
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
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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. package binary
  2. import (
  3. "io"
  4. "encoding/binary"
  5. )
  6. type Byte byte
  7. type Int8 int8
  8. type UInt8 uint8
  9. type Int16 int16
  10. type UInt16 uint16
  11. type Int32 int32
  12. type UInt32 uint32
  13. type Int64 int64
  14. type UInt64 uint64
  15. type Int int
  16. type UInt uint
  17. // Byte
  18. func (self Byte) Equals(other Binary) bool {
  19. return self == other
  20. }
  21. func (self Byte) Less(other Binary) bool {
  22. if o, ok := other.(Byte); ok {
  23. return self < o
  24. } else {
  25. panic("Cannot compare unequal types")
  26. }
  27. }
  28. func (self Byte) ByteSize() int {
  29. return 1
  30. }
  31. func (self Byte) WriteTo(w io.Writer) (int64, error) {
  32. n, err := w.Write([]byte{byte(self)})
  33. return int64(n), err
  34. }
  35. func ReadByte(r io.Reader) Byte {
  36. buf := [1]byte{0}
  37. _, err := io.ReadFull(r, buf[:])
  38. if err != nil { panic(err) }
  39. return Byte(buf[0])
  40. }
  41. // Int8
  42. func (self Int8) Equals(other Binary) bool {
  43. return self == other
  44. }
  45. func (self Int8) Less(other Binary) bool {
  46. if o, ok := other.(Int8); ok {
  47. return self < o
  48. } else {
  49. panic("Cannot compare unequal types")
  50. }
  51. }
  52. func (self Int8) ByteSize() int {
  53. return 1
  54. }
  55. func (self Int8) WriteTo(w io.Writer) (int64, error) {
  56. n, err := w.Write([]byte{byte(self)})
  57. return int64(n), err
  58. }
  59. func ReadInt8(r io.Reader) Int8 {
  60. buf := [1]byte{0}
  61. _, err := io.ReadFull(r, buf[:])
  62. if err != nil { panic(err) }
  63. return Int8(buf[0])
  64. }
  65. // UInt8
  66. func (self UInt8) Equals(other Binary) bool {
  67. return self == other
  68. }
  69. func (self UInt8) Less(other Binary) bool {
  70. if o, ok := other.(UInt8); ok {
  71. return self < o
  72. } else {
  73. panic("Cannot compare unequal types")
  74. }
  75. }
  76. func (self UInt8) ByteSize() int {
  77. return 1
  78. }
  79. func (self UInt8) WriteTo(w io.Writer) (int64, error) {
  80. n, err := w.Write([]byte{byte(self)})
  81. return int64(n), err
  82. }
  83. func ReadUInt8(r io.Reader) UInt8 {
  84. buf := [1]byte{0}
  85. _, err := io.ReadFull(r, buf[:])
  86. if err != nil { panic(err) }
  87. return UInt8(buf[0])
  88. }
  89. // Int16
  90. func (self Int16) Equals(other Binary) bool {
  91. return self == other
  92. }
  93. func (self Int16) Less(other Binary) bool {
  94. if o, ok := other.(Int16); ok {
  95. return self < o
  96. } else {
  97. panic("Cannot compare unequal types")
  98. }
  99. }
  100. func (self Int16) ByteSize() int {
  101. return 2
  102. }
  103. func (self Int16) WriteTo(w io.Writer) (int64, error) {
  104. err := binary.Write(w, binary.LittleEndian, int16(self))
  105. return 2, err
  106. }
  107. func ReadInt16(r io.Reader) Int16 {
  108. buf := [2]byte{0}
  109. _, err := io.ReadFull(r, buf[:])
  110. if err != nil { panic(err) }
  111. return Int16(binary.LittleEndian.Uint16(buf[:]))
  112. }
  113. // UInt16
  114. func (self UInt16) Equals(other Binary) bool {
  115. return self == other
  116. }
  117. func (self UInt16) Less(other Binary) bool {
  118. if o, ok := other.(UInt16); ok {
  119. return self < o
  120. } else {
  121. panic("Cannot compare unequal types")
  122. }
  123. }
  124. func (self UInt16) ByteSize() int {
  125. return 2
  126. }
  127. func (self UInt16) WriteTo(w io.Writer) (int64, error) {
  128. err := binary.Write(w, binary.LittleEndian, uint16(self))
  129. return 2, err
  130. }
  131. func ReadUInt16(r io.Reader) UInt16 {
  132. buf := [2]byte{0}
  133. _, err := io.ReadFull(r, buf[:])
  134. if err != nil { panic(err) }
  135. return UInt16(binary.LittleEndian.Uint16(buf[:]))
  136. }
  137. // Int32
  138. func (self Int32) Equals(other Binary) bool {
  139. return self == other
  140. }
  141. func (self Int32) Less(other Binary) bool {
  142. if o, ok := other.(Int32); ok {
  143. return self < o
  144. } else {
  145. panic("Cannot compare unequal types")
  146. }
  147. }
  148. func (self Int32) ByteSize() int {
  149. return 4
  150. }
  151. func (self Int32) WriteTo(w io.Writer) (int64, error) {
  152. err := binary.Write(w, binary.LittleEndian, int32(self))
  153. return 4, err
  154. }
  155. func ReadInt32(r io.Reader) Int32 {
  156. buf := [4]byte{0}
  157. _, err := io.ReadFull(r, buf[:])
  158. if err != nil { panic(err) }
  159. return Int32(binary.LittleEndian.Uint32(buf[:]))
  160. }
  161. // UInt32
  162. func (self UInt32) Equals(other Binary) bool {
  163. return self == other
  164. }
  165. func (self UInt32) Less(other Binary) bool {
  166. if o, ok := other.(UInt32); ok {
  167. return self < o
  168. } else {
  169. panic("Cannot compare unequal types")
  170. }
  171. }
  172. func (self UInt32) ByteSize() int {
  173. return 4
  174. }
  175. func (self UInt32) WriteTo(w io.Writer) (int64, error) {
  176. err := binary.Write(w, binary.LittleEndian, uint32(self))
  177. return 4, err
  178. }
  179. func ReadUInt32(r io.Reader) UInt32 {
  180. buf := [4]byte{0}
  181. _, err := io.ReadFull(r, buf[:])
  182. if err != nil { panic(err) }
  183. return UInt32(binary.LittleEndian.Uint32(buf[:]))
  184. }
  185. // Int64
  186. func (self Int64) Equals(other Binary) bool {
  187. return self == other
  188. }
  189. func (self Int64) Less(other Binary) bool {
  190. if o, ok := other.(Int64); ok {
  191. return self < o
  192. } else {
  193. panic("Cannot compare unequal types")
  194. }
  195. }
  196. func (self Int64) ByteSize() int {
  197. return 8
  198. }
  199. func (self Int64) WriteTo(w io.Writer) (int64, error) {
  200. err := binary.Write(w, binary.LittleEndian, int64(self))
  201. return 8, err
  202. }
  203. func ReadInt64(r io.Reader) Int64 {
  204. buf := [8]byte{0}
  205. _, err := io.ReadFull(r, buf[:])
  206. if err != nil { panic(err) }
  207. return Int64(binary.LittleEndian.Uint64(buf[:]))
  208. }
  209. // UInt64
  210. func (self UInt64) Equals(other Binary) bool {
  211. return self == other
  212. }
  213. func (self UInt64) Less(other Binary) bool {
  214. if o, ok := other.(UInt64); ok {
  215. return self < o
  216. } else {
  217. panic("Cannot compare unequal types")
  218. }
  219. }
  220. func (self UInt64) ByteSize() int {
  221. return 8
  222. }
  223. func (self UInt64) WriteTo(w io.Writer) (int64, error) {
  224. err := binary.Write(w, binary.LittleEndian, uint64(self))
  225. return 8, err
  226. }
  227. func ReadUInt64(r io.Reader) UInt64 {
  228. buf := [8]byte{0}
  229. _, err := io.ReadFull(r, buf[:])
  230. if err != nil { panic(err) }
  231. return UInt64(binary.LittleEndian.Uint64(buf[:]))
  232. }
  233. // Int
  234. func (self Int) Equals(other Binary) bool {
  235. return self == other
  236. }
  237. func (self Int) Less(other Binary) bool {
  238. if o, ok := other.(Int); ok {
  239. return self < o
  240. } else {
  241. panic("Cannot compare unequal types")
  242. }
  243. }
  244. func (self Int) ByteSize() int {
  245. return 8
  246. }
  247. func (self Int) WriteTo(w io.Writer) (int64, error) {
  248. err := binary.Write(w, binary.LittleEndian, int64(self))
  249. return 8, err
  250. }
  251. func ReadInt(r io.Reader) Int {
  252. buf := [8]byte{0}
  253. _, err := io.ReadFull(r, buf[:])
  254. if err != nil { panic(err) }
  255. return Int(binary.LittleEndian.Uint64(buf[:]))
  256. }
  257. // UInt
  258. func (self UInt) Equals(other Binary) bool {
  259. return self == other
  260. }
  261. func (self UInt) Less(other Binary) bool {
  262. if o, ok := other.(UInt); ok {
  263. return self < o
  264. } else {
  265. panic("Cannot compare unequal types")
  266. }
  267. }
  268. func (self UInt) ByteSize() int {
  269. return 8
  270. }
  271. func (self UInt) WriteTo(w io.Writer) (int64, error) {
  272. err := binary.Write(w, binary.LittleEndian, uint64(self))
  273. return 8, err
  274. }
  275. func ReadUInt(r io.Reader) UInt {
  276. buf := [8]byte{0}
  277. _, err := io.ReadFull(r, buf[:])
  278. if err != nil { panic(err) }
  279. return UInt(binary.LittleEndian.Uint64(buf[:]))
  280. }