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.

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