Source file src/pkg/encoding/binary/binary.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package binary implements translation between numbers and byte sequences
6 // and encoding and decoding of varints.
7 //
8 // Numbers are translated by reading and writing fixed-size values.
9 // A fixed-size value is either a fixed-size arithmetic
10 // type (int8, uint8, int16, float32, complex64, ...)
11 // or an array or struct containing only fixed-size values.
12 //
13 // Varints are a method of encoding integers using one or more bytes;
14 // numbers with smaller absolute value take a smaller number of bytes.
15 // For a specification, see http://code.google.com/apis/protocolbuffers/docs/encoding.html.
16 package binary
17
18 import (
19 "errors"
20 "io"
21 "math"
22 "reflect"
23 )
24
25 // A ByteOrder specifies how to convert byte sequences into
26 // 16-, 32-, or 64-bit unsigned integers.
27 type ByteOrder interface {
28 Uint16([]byte) uint16
29 Uint32([]byte) uint32
30 Uint64([]byte) uint64
31 PutUint16([]byte, uint16)
32 PutUint32([]byte, uint32)
33 PutUint64([]byte, uint64)
34 String() string
35 }
36
37 // LittleEndian is the little-endian implementation of ByteOrder.
38 var LittleEndian littleEndian
39
40 // BigEndian is the big-endian implementation of ByteOrder.
41 var BigEndian bigEndian
42
43 type littleEndian struct{}
44
45 func (littleEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 }
46
47 func (littleEndian) PutUint16(b []byte, v uint16) {
48 b[0] = byte(v)
49 b[1] = byte(v >> 8)
50 }
51
52 func (littleEndian) Uint32(b []byte) uint32 {
53 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
54 }
55
56 func (littleEndian) PutUint32(b []byte, v uint32) {
57 b[0] = byte(v)
58 b[1] = byte(v >> 8)
59 b[2] = byte(v >> 16)
60 b[3] = byte(v >> 24)
61 }
62
63 func (littleEndian) Uint64(b []byte) uint64 {
64 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
65 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
66 }
67
68 func (littleEndian) PutUint64(b []byte, v uint64) {
69 b[0] = byte(v)
70 b[1] = byte(v >> 8)
71 b[2] = byte(v >> 16)
72 b[3] = byte(v >> 24)
73 b[4] = byte(v >> 32)
74 b[5] = byte(v >> 40)
75 b[6] = byte(v >> 48)
76 b[7] = byte(v >> 56)
77 }
78
79 func (littleEndian) String() string { return "LittleEndian" }
80
81 func (littleEndian) GoString() string { return "binary.LittleEndian" }
82
83 type bigEndian struct{}
84
85 func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[1]) | uint16(b[0])<<8 }
86
87 func (bigEndian) PutUint16(b []byte, v uint16) {
88 b[0] = byte(v >> 8)
89 b[1] = byte(v)
90 }
91
92 func (bigEndian) Uint32(b []byte) uint32 {
93 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24
94 }
95
96 func (bigEndian) PutUint32(b []byte, v uint32) {
97 b[0] = byte(v >> 24)
98 b[1] = byte(v >> 16)
99 b[2] = byte(v >> 8)
100 b[3] = byte(v)
101 }
102
103 func (bigEndian) Uint64(b []byte) uint64 {
104 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 |
105 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56
106 }
107
108 func (bigEndian) PutUint64(b []byte, v uint64) {
109 b[0] = byte(v >> 56)
110 b[1] = byte(v >> 48)
111 b[2] = byte(v >> 40)
112 b[3] = byte(v >> 32)
113 b[4] = byte(v >> 24)
114 b[5] = byte(v >> 16)
115 b[6] = byte(v >> 8)
116 b[7] = byte(v)
117 }
118
119 func (bigEndian) String() string { return "BigEndian" }
120
121 func (bigEndian) GoString() string { return "binary.BigEndian" }
122
123 // Read reads structured binary data from r into data.
124 // Data must be a pointer to a fixed-size value or a slice
125 // of fixed-size values.
126 // Bytes read from r are decoded using the specified byte order
127 // and written to successive fields of the data.
128 func Read(r io.Reader, order ByteOrder, data interface{}) error {
129 // Fast path for basic types.
130 if n := intDestSize(data); n != 0 {
131 var b [8]byte
132 bs := b[:n]
133 if _, err := io.ReadFull(r, bs); err != nil {
134 return err
135 }
136 switch v := data.(type) {
137 case *int8:
138 *v = int8(b[0])
139 case *uint8:
140 *v = b[0]
141 case *int16:
142 *v = int16(order.Uint16(bs))
143 case *uint16:
144 *v = order.Uint16(bs)
145 case *int32:
146 *v = int32(order.Uint32(bs))
147 case *uint32:
148 *v = order.Uint32(bs)
149 case *int64:
150 *v = int64(order.Uint64(bs))
151 case *uint64:
152 *v = order.Uint64(bs)
153 }
154 return nil
155 }
156
157 // Fallback to reflect-based.
158 var v reflect.Value
159 switch d := reflect.ValueOf(data); d.Kind() {
160 case reflect.Ptr:
161 v = d.Elem()
162 case reflect.Slice:
163 v = d
164 default:
165 return errors.New("binary.Read: invalid type " + d.Type().String())
166 }
167 size := dataSize(v)
168 if size < 0 {
169 return errors.New("binary.Read: invalid type " + v.Type().String())
170 }
171 d := &decoder{order: order, buf: make([]byte, size)}
172 if _, err := io.ReadFull(r, d.buf); err != nil {
173 return err
174 }
175 d.value(v)
176 return nil
177 }
178
179 // Write writes the binary representation of data into w.
180 // Data must be a fixed-size value or a slice of fixed-size
181 // values, or a pointer to such data.
182 // Bytes written to w are encoded using the specified byte order
183 // and read from successive fields of the data.
184 func Write(w io.Writer, order ByteOrder, data interface{}) error {
185 // Fast path for basic types.
186 var b [8]byte
187 var bs []byte
188 switch v := data.(type) {
189 case *int8:
190 bs = b[:1]
191 b[0] = byte(*v)
192 case int8:
193 bs = b[:1]
194 b[0] = byte(v)
195 case *uint8:
196 bs = b[:1]
197 b[0] = *v
198 case uint8:
199 bs = b[:1]
200 b[0] = byte(v)
201 case *int16:
202 bs = b[:2]
203 order.PutUint16(bs, uint16(*v))
204 case int16:
205 bs = b[:2]
206 order.PutUint16(bs, uint16(v))
207 case *uint16:
208 bs = b[:2]
209 order.PutUint16(bs, *v)
210 case uint16:
211 bs = b[:2]
212 order.PutUint16(bs, v)
213 case *int32:
214 bs = b[:4]
215 order.PutUint32(bs, uint32(*v))
216 case int32:
217 bs = b[:4]
218 order.PutUint32(bs, uint32(v))
219 case *uint32:
220 bs = b[:4]
221 order.PutUint32(bs, *v)
222 case uint32:
223 bs = b[:4]
224 order.PutUint32(bs, v)
225 case *int64:
226 bs = b[:8]
227 order.PutUint64(bs, uint64(*v))
228 case int64:
229 bs = b[:8]
230 order.PutUint64(bs, uint64(v))
231 case *uint64:
232 bs = b[:8]
233 order.PutUint64(bs, *v)
234 case uint64:
235 bs = b[:8]
236 order.PutUint64(bs, v)
237 }
238 if bs != nil {
239 _, err := w.Write(bs)
240 return err
241 }
242 v := reflect.Indirect(reflect.ValueOf(data))
243 size := dataSize(v)
244 if size < 0 {
245 return errors.New("binary.Write: invalid type " + v.Type().String())
246 }
247 buf := make([]byte, size)
248 e := &encoder{order: order, buf: buf}
249 e.value(v)
250 _, err := w.Write(buf)
251 return err
252 }
253
254 // Size returns how many bytes Write would generate to encode the value v, which
255 // must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.
256 func Size(v interface{}) int {
257 return dataSize(reflect.Indirect(reflect.ValueOf(v)))
258 }
259
260 // dataSize returns the number of bytes the actual data represented by v occupies in memory.
261 // For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice
262 // it returns the length of the slice times the element size and does not count the memory
263 // occupied by the header.
264 func dataSize(v reflect.Value) int {
265 if v.Kind() == reflect.Slice {
266 elem := sizeof(v.Type().Elem())
267 if elem < 0 {
268 return -1
269 }
270 return v.Len() * elem
271 }
272 return sizeof(v.Type())
273 }
274
275 func sizeof(t reflect.Type) int {
276 switch t.Kind() {
277 case reflect.Array:
278 n := sizeof(t.Elem())
279 if n < 0 {
280 return -1
281 }
282 return t.Len() * n
283
284 case reflect.Struct:
285 sum := 0
286 for i, n := 0, t.NumField(); i < n; i++ {
287 s := sizeof(t.Field(i).Type)
288 if s < 0 {
289 return -1
290 }
291 sum += s
292 }
293 return sum
294
295 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
296 reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
297 reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
298 return int(t.Size())
299 }
300 return -1
301 }
302
303 type decoder struct {
304 order ByteOrder
305 buf []byte
306 }
307
308 type encoder struct {
309 order ByteOrder
310 buf []byte
311 }
312
313 func (d *decoder) uint8() uint8 {
314 x := d.buf[0]
315 d.buf = d.buf[1:]
316 return x
317 }
318
319 func (e *encoder) uint8(x uint8) {
320 e.buf[0] = x
321 e.buf = e.buf[1:]
322 }
323
324 func (d *decoder) uint16() uint16 {
325 x := d.order.Uint16(d.buf[0:2])
326 d.buf = d.buf[2:]
327 return x
328 }
329
330 func (e *encoder) uint16(x uint16) {
331 e.order.PutUint16(e.buf[0:2], x)
332 e.buf = e.buf[2:]
333 }
334
335 func (d *decoder) uint32() uint32 {
336 x := d.order.Uint32(d.buf[0:4])
337 d.buf = d.buf[4:]
338 return x
339 }
340
341 func (e *encoder) uint32(x uint32) {
342 e.order.PutUint32(e.buf[0:4], x)
343 e.buf = e.buf[4:]
344 }
345
346 func (d *decoder) uint64() uint64 {
347 x := d.order.Uint64(d.buf[0:8])
348 d.buf = d.buf[8:]
349 return x
350 }
351
352 func (e *encoder) uint64(x uint64) {
353 e.order.PutUint64(e.buf[0:8], x)
354 e.buf = e.buf[8:]
355 }
356
357 func (d *decoder) int8() int8 { return int8(d.uint8()) }
358
359 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) }
360
361 func (d *decoder) int16() int16 { return int16(d.uint16()) }
362
363 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) }
364
365 func (d *decoder) int32() int32 { return int32(d.uint32()) }
366
367 func (e *encoder) int32(x int32) { e.uint32(uint32(x)) }
368
369 func (d *decoder) int64() int64 { return int64(d.uint64()) }
370
371 func (e *encoder) int64(x int64) { e.uint64(uint64(x)) }
372
373 func (d *decoder) value(v reflect.Value) {
374 switch v.Kind() {
375 case reflect.Array:
376 l := v.Len()
377 for i := 0; i < l; i++ {
378 d.value(v.Index(i))
379 }
380
381 case reflect.Struct:
382 l := v.NumField()
383 for i := 0; i < l; i++ {
384 d.value(v.Field(i))
385 }
386
387 case reflect.Slice:
388 l := v.Len()
389 for i := 0; i < l; i++ {
390 d.value(v.Index(i))
391 }
392
393 case reflect.Int8:
394 v.SetInt(int64(d.int8()))
395 case reflect.Int16:
396 v.SetInt(int64(d.int16()))
397 case reflect.Int32:
398 v.SetInt(int64(d.int32()))
399 case reflect.Int64:
400 v.SetInt(d.int64())
401
402 case reflect.Uint8:
403 v.SetUint(uint64(d.uint8()))
404 case reflect.Uint16:
405 v.SetUint(uint64(d.uint16()))
406 case reflect.Uint32:
407 v.SetUint(uint64(d.uint32()))
408 case reflect.Uint64:
409 v.SetUint(d.uint64())
410
411 case reflect.Float32:
412 v.SetFloat(float64(math.Float32frombits(d.uint32())))
413 case reflect.Float64:
414 v.SetFloat(math.Float64frombits(d.uint64()))
415
416 case reflect.Complex64:
417 v.SetComplex(complex(
418 float64(math.Float32frombits(d.uint32())),
419 float64(math.Float32frombits(d.uint32())),
420 ))
421 case reflect.Complex128:
422 v.SetComplex(complex(
423 math.Float64frombits(d.uint64()),
424 math.Float64frombits(d.uint64()),
425 ))
426 }
427 }
428
429 func (e *encoder) value(v reflect.Value) {
430 switch v.Kind() {
431 case reflect.Array:
432 l := v.Len()
433 for i := 0; i < l; i++ {
434 e.value(v.Index(i))
435 }
436
437 case reflect.Struct:
438 l := v.NumField()
439 for i := 0; i < l; i++ {
440 e.value(v.Field(i))
441 }
442
443 case reflect.Slice:
444 l := v.Len()
445 for i := 0; i < l; i++ {
446 e.value(v.Index(i))
447 }
448
449 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
450 switch v.Type().Kind() {
451 case reflect.Int8:
452 e.int8(int8(v.Int()))
453 case reflect.Int16:
454 e.int16(int16(v.Int()))
455 case reflect.Int32:
456 e.int32(int32(v.Int()))
457 case reflect.Int64:
458 e.int64(v.Int())
459 }
460
461 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
462 switch v.Type().Kind() {
463 case reflect.Uint8:
464 e.uint8(uint8(v.Uint()))
465 case reflect.Uint16:
466 e.uint16(uint16(v.Uint()))
467 case reflect.Uint32:
468 e.uint32(uint32(v.Uint()))
469 case reflect.Uint64:
470 e.uint64(v.Uint())
471 }
472
473 case reflect.Float32, reflect.Float64:
474 switch v.Type().Kind() {
475 case reflect.Float32:
476 e.uint32(math.Float32bits(float32(v.Float())))
477 case reflect.Float64:
478 e.uint64(math.Float64bits(v.Float()))
479 }
480
481 case reflect.Complex64, reflect.Complex128:
482 switch v.Type().Kind() {
483 case reflect.Complex64:
484 x := v.Complex()
485 e.uint32(math.Float32bits(float32(real(x))))
486 e.uint32(math.Float32bits(float32(imag(x))))
487 case reflect.Complex128:
488 x := v.Complex()
489 e.uint64(math.Float64bits(real(x)))
490 e.uint64(math.Float64bits(imag(x)))
491 }
492 }
493 }
494
495 // intDestSize returns the size of the integer that ptrType points to,
496 // or 0 if the type is not supported.
497 func intDestSize(ptrType interface{}) int {
498 switch ptrType.(type) {
499 case *int8, *uint8:
500 return 1
501 case *int16, *uint16:
502 return 2
503 case *int32, *uint32:
504 return 4
505 case *int64, *uint64:
506 return 8
507 }
508 return 0
509 }