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.

462 lines
8.2 KiB

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