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.

458 lines
8.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
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. "encoding/binary"
  4. "io"
  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 ReadByteSafe(r io.Reader) (Byte, error) {
  36. buf := [1]byte{0}
  37. _, err := io.ReadFull(r, buf[:])
  38. if err != nil {
  39. return 0, err
  40. }
  41. return Byte(buf[0]), nil
  42. }
  43. func ReadByte(r io.Reader) Byte {
  44. b, err := ReadByteSafe(r)
  45. if err != nil {
  46. panic(err)
  47. }
  48. return b
  49. }
  50. // Int8
  51. func (self Int8) Equals(other Binary) bool {
  52. return self == other
  53. }
  54. func (self Int8) Less(other Binary) bool {
  55. if o, ok := other.(Int8); ok {
  56. return self < o
  57. } else {
  58. panic("Cannot compare unequal types")
  59. }
  60. }
  61. func (self Int8) ByteSize() int {
  62. return 1
  63. }
  64. func (self Int8) WriteTo(w io.Writer) (int64, error) {
  65. n, err := w.Write([]byte{byte(self)})
  66. return int64(n), err
  67. }
  68. func ReadInt8Safe(r io.Reader) (Int8, error) {
  69. buf := [1]byte{0}
  70. _, err := io.ReadFull(r, buf[:])
  71. if err != nil {
  72. return Int8(0), err
  73. }
  74. return Int8(buf[0]), nil
  75. }
  76. func ReadInt8(r io.Reader) Int8 {
  77. b, err := ReadInt8Safe(r)
  78. if err != nil {
  79. panic(err)
  80. }
  81. return b
  82. }
  83. // UInt8
  84. func (self UInt8) Equals(other Binary) bool {
  85. return self == other
  86. }
  87. func (self UInt8) Less(other Binary) bool {
  88. if o, ok := other.(UInt8); ok {
  89. return self < o
  90. } else {
  91. panic("Cannot compare unequal types")
  92. }
  93. }
  94. func (self UInt8) ByteSize() int {
  95. return 1
  96. }
  97. func (self UInt8) WriteTo(w io.Writer) (int64, error) {
  98. n, err := w.Write([]byte{byte(self)})
  99. return int64(n), err
  100. }
  101. func ReadUInt8Safe(r io.Reader) (UInt8, error) {
  102. buf := [1]byte{0}
  103. _, err := io.ReadFull(r, buf[:])
  104. if err != nil {
  105. return UInt8(0), err
  106. }
  107. return UInt8(buf[0]), nil
  108. }
  109. func ReadUInt8(r io.Reader) UInt8 {
  110. b, err := ReadUInt8Safe(r)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return b
  115. }
  116. // Int16
  117. func (self Int16) Equals(other Binary) bool {
  118. return self == other
  119. }
  120. func (self Int16) Less(other Binary) bool {
  121. if o, ok := other.(Int16); ok {
  122. return self < o
  123. } else {
  124. panic("Cannot compare unequal types")
  125. }
  126. }
  127. func (self Int16) ByteSize() int {
  128. return 2
  129. }
  130. func (self Int16) WriteTo(w io.Writer) (int64, error) {
  131. buf := []byte{0, 0}
  132. binary.LittleEndian.PutUint16(buf, uint16(self))
  133. n, err := w.Write(buf)
  134. return int64(n), err
  135. }
  136. func ReadInt16Safe(r io.Reader) (Int16, error) {
  137. buf := [2]byte{0}
  138. _, err := io.ReadFull(r, buf[:])
  139. if err != nil {
  140. return Int16(0), err
  141. }
  142. return Int16(binary.LittleEndian.Uint16(buf[:])), nil
  143. }
  144. func ReadInt16(r io.Reader) Int16 {
  145. b, err := ReadInt16Safe(r)
  146. if err != nil {
  147. panic(err)
  148. }
  149. return b
  150. }
  151. // UInt16
  152. func (self UInt16) Equals(other Binary) bool {
  153. return self == other
  154. }
  155. func (self UInt16) Less(other Binary) bool {
  156. if o, ok := other.(UInt16); ok {
  157. return self < o
  158. } else {
  159. panic("Cannot compare unequal types")
  160. }
  161. }
  162. func (self UInt16) ByteSize() int {
  163. return 2
  164. }
  165. func (self UInt16) WriteTo(w io.Writer) (int64, error) {
  166. buf := []byte{0, 0}
  167. binary.LittleEndian.PutUint16(buf, uint16(self))
  168. n, err := w.Write(buf)
  169. return int64(n), err
  170. }
  171. func ReadUInt16Safe(r io.Reader) (UInt16, error) {
  172. buf := [2]byte{0}
  173. _, err := io.ReadFull(r, buf[:])
  174. if err != nil {
  175. return UInt16(0), err
  176. }
  177. return UInt16(binary.LittleEndian.Uint16(buf[:])), nil
  178. }
  179. func ReadUInt16(r io.Reader) UInt16 {
  180. b, err := ReadUInt16Safe(r)
  181. if err != nil {
  182. panic(err)
  183. }
  184. return b
  185. }
  186. // Int32
  187. func (self Int32) Equals(other Binary) bool {
  188. return self == other
  189. }
  190. func (self Int32) Less(other Binary) bool {
  191. if o, ok := other.(Int32); ok {
  192. return self < o
  193. } else {
  194. panic("Cannot compare unequal types")
  195. }
  196. }
  197. func (self Int32) ByteSize() int {
  198. return 4
  199. }
  200. func (self Int32) WriteTo(w io.Writer) (int64, error) {
  201. buf := []byte{0, 0, 0, 0}
  202. binary.LittleEndian.PutUint32(buf, uint32(self))
  203. n, err := w.Write(buf)
  204. return int64(n), err
  205. }
  206. func ReadInt32Safe(r io.Reader) (Int32, error) {
  207. buf := [4]byte{0}
  208. _, err := io.ReadFull(r, buf[:])
  209. if err != nil {
  210. return Int32(0), err
  211. }
  212. return Int32(binary.LittleEndian.Uint32(buf[:])), nil
  213. }
  214. func ReadInt32(r io.Reader) Int32 {
  215. b, err := ReadInt32Safe(r)
  216. if err != nil {
  217. panic(err)
  218. }
  219. return b
  220. }
  221. // UInt32
  222. func (self UInt32) Equals(other Binary) bool {
  223. return self == other
  224. }
  225. func (self UInt32) Less(other Binary) bool {
  226. if o, ok := other.(UInt32); ok {
  227. return self < o
  228. } else {
  229. panic("Cannot compare unequal types")
  230. }
  231. }
  232. func (self UInt32) ByteSize() int {
  233. return 4
  234. }
  235. func (self UInt32) WriteTo(w io.Writer) (int64, error) {
  236. buf := []byte{0, 0, 0, 0}
  237. binary.LittleEndian.PutUint32(buf, uint32(self))
  238. n, err := w.Write(buf)
  239. return int64(n), err
  240. }
  241. func ReadUInt32Safe(r io.Reader) (UInt32, error) {
  242. buf := [4]byte{0}
  243. _, err := io.ReadFull(r, buf[:])
  244. if err != nil {
  245. return UInt32(0), err
  246. }
  247. return UInt32(binary.LittleEndian.Uint32(buf[:])), nil
  248. }
  249. func ReadUInt32(r io.Reader) UInt32 {
  250. b, err := ReadUInt32Safe(r)
  251. if err != nil {
  252. panic(err)
  253. }
  254. return b
  255. }
  256. // Int64
  257. func (self Int64) Equals(other Binary) bool {
  258. return self == other
  259. }
  260. func (self Int64) Less(other Binary) bool {
  261. if o, ok := other.(Int64); ok {
  262. return self < o
  263. } else {
  264. panic("Cannot compare unequal types")
  265. }
  266. }
  267. func (self Int64) ByteSize() int {
  268. return 8
  269. }
  270. func (self Int64) WriteTo(w io.Writer) (int64, error) {
  271. buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
  272. binary.LittleEndian.PutUint64(buf, uint64(self))
  273. n, err := w.Write(buf)
  274. return int64(n), err
  275. }
  276. func ReadInt64Safe(r io.Reader) (Int64, error) {
  277. buf := [8]byte{0}
  278. _, err := io.ReadFull(r, buf[:])
  279. if err != nil {
  280. return Int64(0), err
  281. }
  282. return Int64(binary.LittleEndian.Uint64(buf[:])), nil
  283. }
  284. func ReadInt64(r io.Reader) Int64 {
  285. b, err := ReadInt64Safe(r)
  286. if err != nil {
  287. panic(err)
  288. }
  289. return b
  290. }
  291. // UInt64
  292. func (self UInt64) Equals(other Binary) bool {
  293. return self == other
  294. }
  295. func (self UInt64) Less(other Binary) bool {
  296. if o, ok := other.(UInt64); ok {
  297. return self < o
  298. } else {
  299. panic("Cannot compare unequal types")
  300. }
  301. }
  302. func (self UInt64) ByteSize() int {
  303. return 8
  304. }
  305. func (self UInt64) WriteTo(w io.Writer) (int64, error) {
  306. buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
  307. binary.LittleEndian.PutUint64(buf, uint64(self))
  308. n, err := w.Write(buf)
  309. return int64(n), err
  310. }
  311. func ReadUInt64Safe(r io.Reader) (UInt64, error) {
  312. buf := [8]byte{0}
  313. _, err := io.ReadFull(r, buf[:])
  314. if err != nil {
  315. return UInt64(0), err
  316. }
  317. return UInt64(binary.LittleEndian.Uint64(buf[:])), nil
  318. }
  319. func ReadUInt64(r io.Reader) UInt64 {
  320. b, err := ReadUInt64Safe(r)
  321. if err != nil {
  322. panic(err)
  323. }
  324. return b
  325. }
  326. // Int
  327. func (self Int) Equals(other Binary) bool {
  328. return self == other
  329. }
  330. func (self Int) Less(other Binary) bool {
  331. if o, ok := other.(Int); ok {
  332. return self < o
  333. } else {
  334. panic("Cannot compare unequal types")
  335. }
  336. }
  337. func (self Int) ByteSize() int {
  338. return 8
  339. }
  340. func (self Int) WriteTo(w io.Writer) (int64, error) {
  341. buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
  342. binary.LittleEndian.PutUint64(buf, uint64(self))
  343. n, err := w.Write(buf)
  344. return int64(n), err
  345. }
  346. func ReadInt(r io.Reader) Int {
  347. buf := [8]byte{0}
  348. _, err := io.ReadFull(r, buf[:])
  349. if err != nil {
  350. panic(err)
  351. }
  352. return Int(binary.LittleEndian.Uint64(buf[:]))
  353. }
  354. // UInt
  355. func (self UInt) Equals(other Binary) bool {
  356. return self == other
  357. }
  358. func (self UInt) Less(other Binary) bool {
  359. if o, ok := other.(UInt); ok {
  360. return self < o
  361. } else {
  362. panic("Cannot compare unequal types")
  363. }
  364. }
  365. func (self UInt) ByteSize() int {
  366. return 8
  367. }
  368. func (self UInt) WriteTo(w io.Writer) (int64, error) {
  369. buf := []byte{0, 0, 0, 0, 0, 0, 0, 0}
  370. binary.LittleEndian.PutUint64(buf, uint64(self))
  371. n, err := w.Write(buf)
  372. return int64(n), err
  373. }
  374. func ReadUInt(r io.Reader) UInt {
  375. buf := [8]byte{0}
  376. _, err := io.ReadFull(r, buf[:])
  377. if err != nil {
  378. panic(err)
  379. }
  380. return UInt(binary.LittleEndian.Uint64(buf[:]))
  381. }