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.

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