1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package binary
23
24 import (
25 "errors"
26 "io"
27 "math"
28 "reflect"
29 "sync"
30 )
31
32
33
34
35
36 type ByteOrder interface {
37 Uint16([]byte) uint16
38 Uint32([]byte) uint32
39 Uint64([]byte) uint64
40 PutUint16([]byte, uint16)
41 PutUint32([]byte, uint32)
42 PutUint64([]byte, uint64)
43 String() string
44 }
45
46
47
48
49
50 type AppendByteOrder interface {
51 AppendUint16([]byte, uint16) []byte
52 AppendUint32([]byte, uint32) []byte
53 AppendUint64([]byte, uint64) []byte
54 String() string
55 }
56
57
58 var LittleEndian littleEndian
59
60
61 var BigEndian bigEndian
62
63 type littleEndian struct{}
64
65 func (littleEndian) Uint16(b []byte) uint16 {
66 _ = b[1]
67 return uint16(b[0]) | uint16(b[1])<<8
68 }
69
70 func (littleEndian) PutUint16(b []byte, v uint16) {
71 _ = b[1]
72 b[0] = byte(v)
73 b[1] = byte(v >> 8)
74 }
75
76 func (littleEndian) AppendUint16(b []byte, v uint16) []byte {
77 return append(b,
78 byte(v),
79 byte(v>>8),
80 )
81 }
82
83 func (littleEndian) Uint32(b []byte) uint32 {
84 _ = b[3]
85 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
86 }
87
88 func (littleEndian) PutUint32(b []byte, v uint32) {
89 _ = b[3]
90 b[0] = byte(v)
91 b[1] = byte(v >> 8)
92 b[2] = byte(v >> 16)
93 b[3] = byte(v >> 24)
94 }
95
96 func (littleEndian) AppendUint32(b []byte, v uint32) []byte {
97 return append(b,
98 byte(v),
99 byte(v>>8),
100 byte(v>>16),
101 byte(v>>24),
102 )
103 }
104
105 func (littleEndian) Uint64(b []byte) uint64 {
106 _ = b[7]
107 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
108 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
109 }
110
111 func (littleEndian) PutUint64(b []byte, v uint64) {
112 _ = b[7]
113 b[0] = byte(v)
114 b[1] = byte(v >> 8)
115 b[2] = byte(v >> 16)
116 b[3] = byte(v >> 24)
117 b[4] = byte(v >> 32)
118 b[5] = byte(v >> 40)
119 b[6] = byte(v >> 48)
120 b[7] = byte(v >> 56)
121 }
122
123 func (littleEndian) AppendUint64(b []byte, v uint64) []byte {
124 return append(b,
125 byte(v),
126 byte(v>>8),
127 byte(v>>16),
128 byte(v>>24),
129 byte(v>>32),
130 byte(v>>40),
131 byte(v>>48),
132 byte(v>>56),
133 )
134 }
135
136 func (littleEndian) String() string { return "LittleEndian" }
137
138 func (littleEndian) GoString() string { return "binary.LittleEndian" }
139
140 type bigEndian struct{}
141
142 func (bigEndian) Uint16(b []byte) uint16 {
143 _ = b[1]
144 return uint16(b[1]) | uint16(b[0])<<8
145 }
146
147 func (bigEndian) PutUint16(b []byte, v uint16) {
148 _ = b[1]
149 b[0] = byte(v >> 8)
150 b[1] = byte(v)
151 }
152
153 func (bigEndian) AppendUint16(b []byte, v uint16) []byte {
154 return append(b,
155 byte(v>>8),
156 byte(v),
157 )
158 }
159
160 func (bigEndian) Uint32(b []byte) uint32 {
161 _ = b[3]
162 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
163 }
164
165 func (bigEndian) PutUint32(b []byte, v uint32) {
166 _ = b[3]
167 b[0] = byte(v >> 24)
168 b[1] = byte(v >> 16)
169 b[2] = byte(v >> 8)
170 b[3] = byte(v)
171 }
172
173 func (bigEndian) AppendUint32(b []byte, v uint32) []byte {
174 return append(b,
175 byte(v>>24),
176 byte(v>>16),
177 byte(v>>8),
178 byte(v),
179 )
180 }
181
182 func (bigEndian) Uint64(b []byte) uint64 {
183 _ = b[7]
184 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
185 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
186 }
187
188 func (bigEndian) PutUint64(b []byte, v uint64) {
189 _ = b[7]
190 b[0] = byte(v >> 56)
191 b[1] = byte(v >> 48)
192 b[2] = byte(v >> 40)
193 b[3] = byte(v >> 32)
194 b[4] = byte(v >> 24)
195 b[5] = byte(v >> 16)
196 b[6] = byte(v >> 8)
197 b[7] = byte(v)
198 }
199
200 func (bigEndian) AppendUint64(b []byte, v uint64) []byte {
201 return append(b,
202 byte(v>>56),
203 byte(v>>48),
204 byte(v>>40),
205 byte(v>>32),
206 byte(v>>24),
207 byte(v>>16),
208 byte(v>>8),
209 byte(v),
210 )
211 }
212
213 func (bigEndian) String() string { return "BigEndian" }
214
215 func (bigEndian) GoString() string { return "binary.BigEndian" }
216
217 func (nativeEndian) String() string { return "NativeEndian" }
218
219 func (nativeEndian) GoString() string { return "binary.NativeEndian" }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 func Read(r io.Reader, order ByteOrder, data any) error {
238
239 if n := intDataSize(data); n != 0 {
240 bs := make([]byte, n)
241 if _, err := io.ReadFull(r, bs); err != nil {
242 return err
243 }
244 switch data := data.(type) {
245 case *bool:
246 *data = bs[0] != 0
247 case *int8:
248 *data = int8(bs[0])
249 case *uint8:
250 *data = bs[0]
251 case *int16:
252 *data = int16(order.Uint16(bs))
253 case *uint16:
254 *data = order.Uint16(bs)
255 case *int32:
256 *data = int32(order.Uint32(bs))
257 case *uint32:
258 *data = order.Uint32(bs)
259 case *int64:
260 *data = int64(order.Uint64(bs))
261 case *uint64:
262 *data = order.Uint64(bs)
263 case *float32:
264 *data = math.Float32frombits(order.Uint32(bs))
265 case *float64:
266 *data = math.Float64frombits(order.Uint64(bs))
267 case []bool:
268 for i, x := range bs {
269 data[i] = x != 0
270 }
271 case []int8:
272 for i, x := range bs {
273 data[i] = int8(x)
274 }
275 case []uint8:
276 copy(data, bs)
277 case []int16:
278 for i := range data {
279 data[i] = int16(order.Uint16(bs[2*i:]))
280 }
281 case []uint16:
282 for i := range data {
283 data[i] = order.Uint16(bs[2*i:])
284 }
285 case []int32:
286 for i := range data {
287 data[i] = int32(order.Uint32(bs[4*i:]))
288 }
289 case []uint32:
290 for i := range data {
291 data[i] = order.Uint32(bs[4*i:])
292 }
293 case []int64:
294 for i := range data {
295 data[i] = int64(order.Uint64(bs[8*i:]))
296 }
297 case []uint64:
298 for i := range data {
299 data[i] = order.Uint64(bs[8*i:])
300 }
301 case []float32:
302 for i := range data {
303 data[i] = math.Float32frombits(order.Uint32(bs[4*i:]))
304 }
305 case []float64:
306 for i := range data {
307 data[i] = math.Float64frombits(order.Uint64(bs[8*i:]))
308 }
309 default:
310 n = 0
311 }
312 if n != 0 {
313 return nil
314 }
315 }
316
317
318 v := reflect.ValueOf(data)
319 size := -1
320 switch v.Kind() {
321 case reflect.Pointer:
322 v = v.Elem()
323 size = dataSize(v)
324 case reflect.Slice:
325 size = dataSize(v)
326 }
327 if size < 0 {
328 return errors.New("binary.Read: invalid type " + reflect.TypeOf(data).String())
329 }
330 d := &decoder{order: order, buf: make([]byte, size)}
331 if _, err := io.ReadFull(r, d.buf); err != nil {
332 return err
333 }
334 d.value(v)
335 return nil
336 }
337
338
339
340
341
342
343
344
345
346 func Write(w io.Writer, order ByteOrder, data any) error {
347
348 if n := intDataSize(data); n != 0 {
349 bs := make([]byte, n)
350 switch v := data.(type) {
351 case *bool:
352 if *v {
353 bs[0] = 1
354 } else {
355 bs[0] = 0
356 }
357 case bool:
358 if v {
359 bs[0] = 1
360 } else {
361 bs[0] = 0
362 }
363 case []bool:
364 for i, x := range v {
365 if x {
366 bs[i] = 1
367 } else {
368 bs[i] = 0
369 }
370 }
371 case *int8:
372 bs[0] = byte(*v)
373 case int8:
374 bs[0] = byte(v)
375 case []int8:
376 for i, x := range v {
377 bs[i] = byte(x)
378 }
379 case *uint8:
380 bs[0] = *v
381 case uint8:
382 bs[0] = v
383 case []uint8:
384 bs = v
385 case *int16:
386 order.PutUint16(bs, uint16(*v))
387 case int16:
388 order.PutUint16(bs, uint16(v))
389 case []int16:
390 for i, x := range v {
391 order.PutUint16(bs[2*i:], uint16(x))
392 }
393 case *uint16:
394 order.PutUint16(bs, *v)
395 case uint16:
396 order.PutUint16(bs, v)
397 case []uint16:
398 for i, x := range v {
399 order.PutUint16(bs[2*i:], x)
400 }
401 case *int32:
402 order.PutUint32(bs, uint32(*v))
403 case int32:
404 order.PutUint32(bs, uint32(v))
405 case []int32:
406 for i, x := range v {
407 order.PutUint32(bs[4*i:], uint32(x))
408 }
409 case *uint32:
410 order.PutUint32(bs, *v)
411 case uint32:
412 order.PutUint32(bs, v)
413 case []uint32:
414 for i, x := range v {
415 order.PutUint32(bs[4*i:], x)
416 }
417 case *int64:
418 order.PutUint64(bs, uint64(*v))
419 case int64:
420 order.PutUint64(bs, uint64(v))
421 case []int64:
422 for i, x := range v {
423 order.PutUint64(bs[8*i:], uint64(x))
424 }
425 case *uint64:
426 order.PutUint64(bs, *v)
427 case uint64:
428 order.PutUint64(bs, v)
429 case []uint64:
430 for i, x := range v {
431 order.PutUint64(bs[8*i:], x)
432 }
433 case *float32:
434 order.PutUint32(bs, math.Float32bits(*v))
435 case float32:
436 order.PutUint32(bs, math.Float32bits(v))
437 case []float32:
438 for i, x := range v {
439 order.PutUint32(bs[4*i:], math.Float32bits(x))
440 }
441 case *float64:
442 order.PutUint64(bs, math.Float64bits(*v))
443 case float64:
444 order.PutUint64(bs, math.Float64bits(v))
445 case []float64:
446 for i, x := range v {
447 order.PutUint64(bs[8*i:], math.Float64bits(x))
448 }
449 }
450 _, err := w.Write(bs)
451 return err
452 }
453
454
455 v := reflect.Indirect(reflect.ValueOf(data))
456 size := dataSize(v)
457 if size < 0 {
458 return errors.New("binary.Write: some values are not fixed-sized in type " + reflect.TypeOf(data).String())
459 }
460 buf := make([]byte, size)
461 e := &encoder{order: order, buf: buf}
462 e.value(v)
463 _, err := w.Write(buf)
464 return err
465 }
466
467
468
469
470 func Size(v any) int {
471 return dataSize(reflect.Indirect(reflect.ValueOf(v)))
472 }
473
474 var structSize sync.Map
475
476
477
478
479
480 func dataSize(v reflect.Value) int {
481 switch v.Kind() {
482 case reflect.Slice:
483 if s := sizeof(v.Type().Elem()); s >= 0 {
484 return s * v.Len()
485 }
486
487 case reflect.Struct:
488 t := v.Type()
489 if size, ok := structSize.Load(t); ok {
490 return size.(int)
491 }
492 size := sizeof(t)
493 structSize.Store(t, size)
494 return size
495
496 default:
497 if v.IsValid() {
498 return sizeof(v.Type())
499 }
500 }
501
502 return -1
503 }
504
505
506 func sizeof(t reflect.Type) int {
507 switch t.Kind() {
508 case reflect.Array:
509 if s := sizeof(t.Elem()); s >= 0 {
510 return s * t.Len()
511 }
512
513 case reflect.Struct:
514 sum := 0
515 for i, n := 0, t.NumField(); i < n; i++ {
516 s := sizeof(t.Field(i).Type)
517 if s < 0 {
518 return -1
519 }
520 sum += s
521 }
522 return sum
523
524 case reflect.Bool,
525 reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
526 reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
527 reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
528 return int(t.Size())
529 }
530
531 return -1
532 }
533
534 type coder struct {
535 order ByteOrder
536 buf []byte
537 offset int
538 }
539
540 type decoder coder
541 type encoder coder
542
543 func (d *decoder) bool() bool {
544 x := d.buf[d.offset]
545 d.offset++
546 return x != 0
547 }
548
549 func (e *encoder) bool(x bool) {
550 if x {
551 e.buf[e.offset] = 1
552 } else {
553 e.buf[e.offset] = 0
554 }
555 e.offset++
556 }
557
558 func (d *decoder) uint8() uint8 {
559 x := d.buf[d.offset]
560 d.offset++
561 return x
562 }
563
564 func (e *encoder) uint8(x uint8) {
565 e.buf[e.offset] = x
566 e.offset++
567 }
568
569 func (d *decoder) uint16() uint16 {
570 x := d.order.Uint16(d.buf[d.offset : d.offset+2])
571 d.offset += 2
572 return x
573 }
574
575 func (e *encoder) uint16(x uint16) {
576 e.order.PutUint16(e.buf[e.offset:e.offset+2], x)
577 e.offset += 2
578 }
579
580 func (d *decoder) uint32() uint32 {
581 x := d.order.Uint32(d.buf[d.offset : d.offset+4])
582 d.offset += 4
583 return x
584 }
585
586 func (e *encoder) uint32(x uint32) {
587 e.order.PutUint32(e.buf[e.offset:e.offset+4], x)
588 e.offset += 4
589 }
590
591 func (d *decoder) uint64() uint64 {
592 x := d.order.Uint64(d.buf[d.offset : d.offset+8])
593 d.offset += 8
594 return x
595 }
596
597 func (e *encoder) uint64(x uint64) {
598 e.order.PutUint64(e.buf[e.offset:e.offset+8], x)
599 e.offset += 8
600 }
601
602 func (d *decoder) int8() int8 { return int8(d.uint8()) }
603
604 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
605
606 func (d *decoder) int16() int16 { return int16(d.uint16()) }
607
608 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
609
610 func (d *decoder) int32() int32 { return int32(d.uint32()) }
611
612 func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
613
614 func (d *decoder) int64() int64 { return int64(d.uint64()) }
615
616 func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
617
618 func (d *decoder) value(v reflect.Value) {
619 switch v.Kind() {
620 case reflect.Array:
621 l := v.Len()
622 for i := 0; i < l; i++ {
623 d.value(v.Index(i))
624 }
625
626 case reflect.Struct:
627 t := v.Type()
628 l := v.NumField()
629 for i := 0; i < l; i++ {
630
631
632
633
634
635 if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
636 d.value(v)
637 } else {
638 d.skip(v)
639 }
640 }
641
642 case reflect.Slice:
643 l := v.Len()
644 for i := 0; i < l; i++ {
645 d.value(v.Index(i))
646 }
647
648 case reflect.Bool:
649 v.SetBool(d.bool())
650
651 case reflect.Int8:
652 v.SetInt(int64(d.int8()))
653 case reflect.Int16:
654 v.SetInt(int64(d.int16()))
655 case reflect.Int32:
656 v.SetInt(int64(d.int32()))
657 case reflect.Int64:
658 v.SetInt(d.int64())
659
660 case reflect.Uint8:
661 v.SetUint(uint64(d.uint8()))
662 case reflect.Uint16:
663 v.SetUint(uint64(d.uint16()))
664 case reflect.Uint32:
665 v.SetUint(uint64(d.uint32()))
666 case reflect.Uint64:
667 v.SetUint(d.uint64())
668
669 case reflect.Float32:
670 v.SetFloat(float64(math.Float32frombits(d.uint32())))
671 case reflect.Float64:
672 v.SetFloat(math.Float64frombits(d.uint64()))
673
674 case reflect.Complex64:
675 v.SetComplex(complex(
676 float64(math.Float32frombits(d.uint32())),
677 float64(math.Float32frombits(d.uint32())),
678 ))
679 case reflect.Complex128:
680 v.SetComplex(complex(
681 math.Float64frombits(d.uint64()),
682 math.Float64frombits(d.uint64()),
683 ))
684 }
685 }
686
687 func (e *encoder) value(v reflect.Value) {
688 switch v.Kind() {
689 case reflect.Array:
690 l := v.Len()
691 for i := 0; i < l; i++ {
692 e.value(v.Index(i))
693 }
694
695 case reflect.Struct:
696 t := v.Type()
697 l := v.NumField()
698 for i := 0; i < l; i++ {
699
700 if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" {
701 e.value(v)
702 } else {
703 e.skip(v)
704 }
705 }
706
707 case reflect.Slice:
708 l := v.Len()
709 for i := 0; i < l; i++ {
710 e.value(v.Index(i))
711 }
712
713 case reflect.Bool:
714 e.bool(v.Bool())
715
716 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
717 switch v.Type().Kind() {
718 case reflect.Int8:
719 e.int8(int8(v.Int()))
720 case reflect.Int16:
721 e.int16(int16(v.Int()))
722 case reflect.Int32:
723 e.int32(int32(v.Int()))
724 case reflect.Int64:
725 e.int64(v.Int())
726 }
727
728 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
729 switch v.Type().Kind() {
730 case reflect.Uint8:
731 e.uint8(uint8(v.Uint()))
732 case reflect.Uint16:
733 e.uint16(uint16(v.Uint()))
734 case reflect.Uint32:
735 e.uint32(uint32(v.Uint()))
736 case reflect.Uint64:
737 e.uint64(v.Uint())
738 }
739
740 case reflect.Float32, reflect.Float64:
741 switch v.Type().Kind() {
742 case reflect.Float32:
743 e.uint32(math.Float32bits(float32(v.Float())))
744 case reflect.Float64:
745 e.uint64(math.Float64bits(v.Float()))
746 }
747
748 case reflect.Complex64, reflect.Complex128:
749 switch v.Type().Kind() {
750 case reflect.Complex64:
751 x := v.Complex()
752 e.uint32(math.Float32bits(float32(real(x))))
753 e.uint32(math.Float32bits(float32(imag(x))))
754 case reflect.Complex128:
755 x := v.Complex()
756 e.uint64(math.Float64bits(real(x)))
757 e.uint64(math.Float64bits(imag(x)))
758 }
759 }
760 }
761
762 func (d *decoder) skip(v reflect.Value) {
763 d.offset += dataSize(v)
764 }
765
766 func (e *encoder) skip(v reflect.Value) {
767 n := dataSize(v)
768 zero := e.buf[e.offset : e.offset+n]
769 for i := range zero {
770 zero[i] = 0
771 }
772 e.offset += n
773 }
774
775
776
777 func intDataSize(data any) int {
778 switch data := data.(type) {
779 case bool, int8, uint8, *bool, *int8, *uint8:
780 return 1
781 case []bool:
782 return len(data)
783 case []int8:
784 return len(data)
785 case []uint8:
786 return len(data)
787 case int16, uint16, *int16, *uint16:
788 return 2
789 case []int16:
790 return 2 * len(data)
791 case []uint16:
792 return 2 * len(data)
793 case int32, uint32, *int32, *uint32:
794 return 4
795 case []int32:
796 return 4 * len(data)
797 case []uint32:
798 return 4 * len(data)
799 case int64, uint64, *int64, *uint64:
800 return 8
801 case []int64:
802 return 8 * len(data)
803 case []uint64:
804 return 8 * len(data)
805 case float32, *float32:
806 return 4
807 case float64, *float64:
808 return 8
809 case []float32:
810 return 4 * len(data)
811 case []float64:
812 return 8 * len(data)
813 }
814 return 0
815 }
816
View as plain text