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.

373 lines
7.2 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
10 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
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. func ReadUInt8Safe(r io.Reader) (UInt8, error) {
  96. buf := [1]byte{0}
  97. _, err := io.ReadFull(r, buf[:])
  98. if err != nil { return UInt8(0), err }
  99. return UInt8(buf[0]), nil
  100. }
  101. // Int16
  102. func (self Int16) Equals(other Binary) bool {
  103. return self == other
  104. }
  105. func (self Int16) Less(other Binary) bool {
  106. if o, ok := other.(Int16); ok {
  107. return self < o
  108. } else {
  109. panic("Cannot compare unequal types")
  110. }
  111. }
  112. func (self Int16) ByteSize() int {
  113. return 2
  114. }
  115. func (self Int16) WriteTo(w io.Writer) (int64, error) {
  116. err := binary.Write(w, binary.LittleEndian, int16(self))
  117. return 2, err
  118. }
  119. func ReadInt16(r io.Reader) Int16 {
  120. buf := [2]byte{0}
  121. _, err := io.ReadFull(r, buf[:])
  122. if err != nil { panic(err) }
  123. return Int16(binary.LittleEndian.Uint16(buf[:]))
  124. }
  125. // UInt16
  126. func (self UInt16) Equals(other Binary) bool {
  127. return self == other
  128. }
  129. func (self UInt16) Less(other Binary) bool {
  130. if o, ok := other.(UInt16); ok {
  131. return self < o
  132. } else {
  133. panic("Cannot compare unequal types")
  134. }
  135. }
  136. func (self UInt16) ByteSize() int {
  137. return 2
  138. }
  139. func (self UInt16) WriteTo(w io.Writer) (int64, error) {
  140. err := binary.Write(w, binary.LittleEndian, uint16(self))
  141. return 2, err
  142. }
  143. func ReadUInt16(r io.Reader) UInt16 {
  144. buf := [2]byte{0}
  145. _, err := io.ReadFull(r, buf[:])
  146. if err != nil { panic(err) }
  147. return UInt16(binary.LittleEndian.Uint16(buf[:]))
  148. }
  149. // Int32
  150. func (self Int32) Equals(other Binary) bool {
  151. return self == other
  152. }
  153. func (self Int32) Less(other Binary) bool {
  154. if o, ok := other.(Int32); ok {
  155. return self < o
  156. } else {
  157. panic("Cannot compare unequal types")
  158. }
  159. }
  160. func (self Int32) ByteSize() int {
  161. return 4
  162. }
  163. func (self Int32) WriteTo(w io.Writer) (int64, error) {
  164. err := binary.Write(w, binary.LittleEndian, int32(self))
  165. return 4, err
  166. }
  167. func ReadInt32(r io.Reader) Int32 {
  168. buf := [4]byte{0}
  169. _, err := io.ReadFull(r, buf[:])
  170. if err != nil { panic(err) }
  171. return Int32(binary.LittleEndian.Uint32(buf[:]))
  172. }
  173. // UInt32
  174. func (self UInt32) Equals(other Binary) bool {
  175. return self == other
  176. }
  177. func (self UInt32) Less(other Binary) bool {
  178. if o, ok := other.(UInt32); ok {
  179. return self < o
  180. } else {
  181. panic("Cannot compare unequal types")
  182. }
  183. }
  184. func (self UInt32) ByteSize() int {
  185. return 4
  186. }
  187. func (self UInt32) WriteTo(w io.Writer) (int64, error) {
  188. err := binary.Write(w, binary.LittleEndian, uint32(self))
  189. return 4, err
  190. }
  191. func ReadUInt32(r io.Reader) UInt32 {
  192. buf := [4]byte{0}
  193. _, err := io.ReadFull(r, buf[:])
  194. if err != nil { panic(err) }
  195. return UInt32(binary.LittleEndian.Uint32(buf[:]))
  196. }
  197. // Int64
  198. func (self Int64) Equals(other Binary) bool {
  199. return self == other
  200. }
  201. func (self Int64) Less(other Binary) bool {
  202. if o, ok := other.(Int64); ok {
  203. return self < o
  204. } else {
  205. panic("Cannot compare unequal types")
  206. }
  207. }
  208. func (self Int64) ByteSize() int {
  209. return 8
  210. }
  211. func (self Int64) WriteTo(w io.Writer) (int64, error) {
  212. err := binary.Write(w, binary.LittleEndian, int64(self))
  213. return 8, err
  214. }
  215. func ReadInt64(r io.Reader) Int64 {
  216. buf := [8]byte{0}
  217. _, err := io.ReadFull(r, buf[:])
  218. if err != nil { panic(err) }
  219. return Int64(binary.LittleEndian.Uint64(buf[:]))
  220. }
  221. // UInt64
  222. func (self UInt64) Equals(other Binary) bool {
  223. return self == other
  224. }
  225. func (self UInt64) Less(other Binary) bool {
  226. if o, ok := other.(UInt64); ok {
  227. return self < o
  228. } else {
  229. panic("Cannot compare unequal types")
  230. }
  231. }
  232. func (self UInt64) ByteSize() int {
  233. return 8
  234. }
  235. func (self UInt64) WriteTo(w io.Writer) (int64, error) {
  236. err := binary.Write(w, binary.LittleEndian, uint64(self))
  237. return 8, err
  238. }
  239. func ReadUInt64(r io.Reader) UInt64 {
  240. buf := [8]byte{0}
  241. _, err := io.ReadFull(r, buf[:])
  242. if err != nil { panic(err) }
  243. return UInt64(binary.LittleEndian.Uint64(buf[:]))
  244. }
  245. // Int
  246. func (self Int) Equals(other Binary) bool {
  247. return self == other
  248. }
  249. func (self Int) Less(other Binary) bool {
  250. if o, ok := other.(Int); ok {
  251. return self < o
  252. } else {
  253. panic("Cannot compare unequal types")
  254. }
  255. }
  256. func (self Int) ByteSize() int {
  257. return 8
  258. }
  259. func (self Int) WriteTo(w io.Writer) (int64, error) {
  260. err := binary.Write(w, binary.LittleEndian, int64(self))
  261. return 8, err
  262. }
  263. func ReadInt(r io.Reader) Int {
  264. buf := [8]byte{0}
  265. _, err := io.ReadFull(r, buf[:])
  266. if err != nil { panic(err) }
  267. return Int(binary.LittleEndian.Uint64(buf[:]))
  268. }
  269. // UInt
  270. func (self UInt) Equals(other Binary) bool {
  271. return self == other
  272. }
  273. func (self UInt) Less(other Binary) bool {
  274. if o, ok := other.(UInt); ok {
  275. return self < o
  276. } else {
  277. panic("Cannot compare unequal types")
  278. }
  279. }
  280. func (self UInt) ByteSize() int {
  281. return 8
  282. }
  283. func (self UInt) WriteTo(w io.Writer) (int64, error) {
  284. err := binary.Write(w, binary.LittleEndian, uint64(self))
  285. return 8, err
  286. }
  287. func ReadUInt(r io.Reader) UInt {
  288. buf := [8]byte{0}
  289. _, err := io.ReadFull(r, buf[:])
  290. if err != nil { panic(err) }
  291. return UInt(binary.LittleEndian.Uint64(buf[:]))
  292. }