github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/encoding/binary/binary.go (about) 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 // When reading into structs, the field data for fields with 129 // blank (_) field names is skipped; i.e., blank field names 130 // may be used for padding. 131 func Read(r io.Reader, order ByteOrder, data interface{}) error { 132 // Fast path for basic types. 133 if n := intDestSize(data); n != 0 { 134 var b [8]byte 135 bs := b[:n] 136 if _, err := io.ReadFull(r, bs); err != nil { 137 return err 138 } 139 switch v := data.(type) { 140 case *int8: 141 *v = int8(b[0]) 142 case *uint8: 143 *v = b[0] 144 case *int16: 145 *v = int16(order.Uint16(bs)) 146 case *uint16: 147 *v = order.Uint16(bs) 148 case *int32: 149 *v = int32(order.Uint32(bs)) 150 case *uint32: 151 *v = order.Uint32(bs) 152 case *int64: 153 *v = int64(order.Uint64(bs)) 154 case *uint64: 155 *v = order.Uint64(bs) 156 } 157 return nil 158 } 159 160 // Fallback to reflect-based decoding. 161 var v reflect.Value 162 switch d := reflect.ValueOf(data); d.Kind() { 163 case reflect.Ptr: 164 v = d.Elem() 165 case reflect.Slice: 166 v = d 167 default: 168 return errors.New("binary.Read: invalid type " + d.Type().String()) 169 } 170 size, err := dataSize(v) 171 if err != nil { 172 return errors.New("binary.Read: " + err.Error()) 173 } 174 d := &decoder{order: order, buf: make([]byte, size)} 175 if _, err := io.ReadFull(r, d.buf); err != nil { 176 return err 177 } 178 d.value(v) 179 return nil 180 } 181 182 // Write writes the binary representation of data into w. 183 // Data must be a fixed-size value or a slice of fixed-size 184 // values, or a pointer to such data. 185 // Bytes written to w are encoded using the specified byte order 186 // and read from successive fields of the data. 187 // When writing structs, zero values are written for fields 188 // with blank (_) field names. 189 func Write(w io.Writer, order ByteOrder, data interface{}) error { 190 // Fast path for basic types. 191 var b [8]byte 192 var bs []byte 193 switch v := data.(type) { 194 case *int8: 195 bs = b[:1] 196 b[0] = byte(*v) 197 case int8: 198 bs = b[:1] 199 b[0] = byte(v) 200 case *uint8: 201 bs = b[:1] 202 b[0] = *v 203 case uint8: 204 bs = b[:1] 205 b[0] = byte(v) 206 case *int16: 207 bs = b[:2] 208 order.PutUint16(bs, uint16(*v)) 209 case int16: 210 bs = b[:2] 211 order.PutUint16(bs, uint16(v)) 212 case *uint16: 213 bs = b[:2] 214 order.PutUint16(bs, *v) 215 case uint16: 216 bs = b[:2] 217 order.PutUint16(bs, v) 218 case *int32: 219 bs = b[:4] 220 order.PutUint32(bs, uint32(*v)) 221 case int32: 222 bs = b[:4] 223 order.PutUint32(bs, uint32(v)) 224 case *uint32: 225 bs = b[:4] 226 order.PutUint32(bs, *v) 227 case uint32: 228 bs = b[:4] 229 order.PutUint32(bs, v) 230 case *int64: 231 bs = b[:8] 232 order.PutUint64(bs, uint64(*v)) 233 case int64: 234 bs = b[:8] 235 order.PutUint64(bs, uint64(v)) 236 case *uint64: 237 bs = b[:8] 238 order.PutUint64(bs, *v) 239 case uint64: 240 bs = b[:8] 241 order.PutUint64(bs, v) 242 } 243 if bs != nil { 244 _, err := w.Write(bs) 245 return err 246 } 247 248 // Fallback to reflect-based encoding. 249 v := reflect.Indirect(reflect.ValueOf(data)) 250 size, err := dataSize(v) 251 if err != nil { 252 return errors.New("binary.Write: " + err.Error()) 253 } 254 buf := make([]byte, size) 255 e := &encoder{order: order, buf: buf} 256 e.value(v) 257 _, err = w.Write(buf) 258 return err 259 } 260 261 // Size returns how many bytes Write would generate to encode the value v, which 262 // must be a fixed-size value or a slice of fixed-size values, or a pointer to such data. 263 func Size(v interface{}) int { 264 n, err := dataSize(reflect.Indirect(reflect.ValueOf(v))) 265 if err != nil { 266 return -1 267 } 268 return n 269 } 270 271 // dataSize returns the number of bytes the actual data represented by v occupies in memory. 272 // For compound structures, it sums the sizes of the elements. Thus, for instance, for a slice 273 // it returns the length of the slice times the element size and does not count the memory 274 // occupied by the header. 275 func dataSize(v reflect.Value) (int, error) { 276 if v.Kind() == reflect.Slice { 277 elem, err := sizeof(v.Type().Elem()) 278 if err != nil { 279 return 0, err 280 } 281 return v.Len() * elem, nil 282 } 283 return sizeof(v.Type()) 284 } 285 286 func sizeof(t reflect.Type) (int, error) { 287 switch t.Kind() { 288 case reflect.Array: 289 n, err := sizeof(t.Elem()) 290 if err != nil { 291 return 0, err 292 } 293 return t.Len() * n, nil 294 295 case reflect.Struct: 296 sum := 0 297 for i, n := 0, t.NumField(); i < n; i++ { 298 s, err := sizeof(t.Field(i).Type) 299 if err != nil { 300 return 0, err 301 } 302 sum += s 303 } 304 return sum, nil 305 306 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, 307 reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, 308 reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128: 309 return int(t.Size()), nil 310 } 311 return 0, errors.New("invalid type " + t.String()) 312 } 313 314 type coder struct { 315 order ByteOrder 316 buf []byte 317 } 318 319 type decoder coder 320 type encoder coder 321 322 func (d *decoder) uint8() uint8 { 323 x := d.buf[0] 324 d.buf = d.buf[1:] 325 return x 326 } 327 328 func (e *encoder) uint8(x uint8) { 329 e.buf[0] = x 330 e.buf = e.buf[1:] 331 } 332 333 func (d *decoder) uint16() uint16 { 334 x := d.order.Uint16(d.buf[0:2]) 335 d.buf = d.buf[2:] 336 return x 337 } 338 339 func (e *encoder) uint16(x uint16) { 340 e.order.PutUint16(e.buf[0:2], x) 341 e.buf = e.buf[2:] 342 } 343 344 func (d *decoder) uint32() uint32 { 345 x := d.order.Uint32(d.buf[0:4]) 346 d.buf = d.buf[4:] 347 return x 348 } 349 350 func (e *encoder) uint32(x uint32) { 351 e.order.PutUint32(e.buf[0:4], x) 352 e.buf = e.buf[4:] 353 } 354 355 func (d *decoder) uint64() uint64 { 356 x := d.order.Uint64(d.buf[0:8]) 357 d.buf = d.buf[8:] 358 return x 359 } 360 361 func (e *encoder) uint64(x uint64) { 362 e.order.PutUint64(e.buf[0:8], x) 363 e.buf = e.buf[8:] 364 } 365 366 func (d *decoder) int8() int8 { return int8(d.uint8()) } 367 368 func (e *encoder) int8(x int8) { e.uint8(uint8(x)) } 369 370 func (d *decoder) int16() int16 { return int16(d.uint16()) } 371 372 func (e *encoder) int16(x int16) { e.uint16(uint16(x)) } 373 374 func (d *decoder) int32() int32 { return int32(d.uint32()) } 375 376 func (e *encoder) int32(x int32) { e.uint32(uint32(x)) } 377 378 func (d *decoder) int64() int64 { return int64(d.uint64()) } 379 380 func (e *encoder) int64(x int64) { e.uint64(uint64(x)) } 381 382 func (d *decoder) value(v reflect.Value) { 383 switch v.Kind() { 384 case reflect.Array: 385 l := v.Len() 386 for i := 0; i < l; i++ { 387 d.value(v.Index(i)) 388 } 389 390 case reflect.Struct: 391 t := v.Type() 392 l := v.NumField() 393 for i := 0; i < l; i++ { 394 // Note: Calling v.CanSet() below is an optimization. 395 // It would be sufficient to check the field name, 396 // but creating the StructField info for each field is 397 // costly (run "go test -bench=ReadStruct" and compare 398 // results when making changes to this code). 399 if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" { 400 d.value(v) 401 } else { 402 d.skip(v) 403 } 404 } 405 406 case reflect.Slice: 407 l := v.Len() 408 for i := 0; i < l; i++ { 409 d.value(v.Index(i)) 410 } 411 412 case reflect.Int8: 413 v.SetInt(int64(d.int8())) 414 case reflect.Int16: 415 v.SetInt(int64(d.int16())) 416 case reflect.Int32: 417 v.SetInt(int64(d.int32())) 418 case reflect.Int64: 419 v.SetInt(d.int64()) 420 421 case reflect.Uint8: 422 v.SetUint(uint64(d.uint8())) 423 case reflect.Uint16: 424 v.SetUint(uint64(d.uint16())) 425 case reflect.Uint32: 426 v.SetUint(uint64(d.uint32())) 427 case reflect.Uint64: 428 v.SetUint(d.uint64()) 429 430 case reflect.Float32: 431 v.SetFloat(float64(math.Float32frombits(d.uint32()))) 432 case reflect.Float64: 433 v.SetFloat(math.Float64frombits(d.uint64())) 434 435 case reflect.Complex64: 436 v.SetComplex(complex( 437 float64(math.Float32frombits(d.uint32())), 438 float64(math.Float32frombits(d.uint32())), 439 )) 440 case reflect.Complex128: 441 v.SetComplex(complex( 442 math.Float64frombits(d.uint64()), 443 math.Float64frombits(d.uint64()), 444 )) 445 } 446 } 447 448 func (e *encoder) value(v reflect.Value) { 449 switch v.Kind() { 450 case reflect.Array: 451 l := v.Len() 452 for i := 0; i < l; i++ { 453 e.value(v.Index(i)) 454 } 455 456 case reflect.Struct: 457 t := v.Type() 458 l := v.NumField() 459 for i := 0; i < l; i++ { 460 // see comment for corresponding code in decoder.value() 461 if v := v.Field(i); v.CanSet() || t.Field(i).Name != "_" { 462 e.value(v) 463 } else { 464 e.skip(v) 465 } 466 } 467 468 case reflect.Slice: 469 l := v.Len() 470 for i := 0; i < l; i++ { 471 e.value(v.Index(i)) 472 } 473 474 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 475 switch v.Type().Kind() { 476 case reflect.Int8: 477 e.int8(int8(v.Int())) 478 case reflect.Int16: 479 e.int16(int16(v.Int())) 480 case reflect.Int32: 481 e.int32(int32(v.Int())) 482 case reflect.Int64: 483 e.int64(v.Int()) 484 } 485 486 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 487 switch v.Type().Kind() { 488 case reflect.Uint8: 489 e.uint8(uint8(v.Uint())) 490 case reflect.Uint16: 491 e.uint16(uint16(v.Uint())) 492 case reflect.Uint32: 493 e.uint32(uint32(v.Uint())) 494 case reflect.Uint64: 495 e.uint64(v.Uint()) 496 } 497 498 case reflect.Float32, reflect.Float64: 499 switch v.Type().Kind() { 500 case reflect.Float32: 501 e.uint32(math.Float32bits(float32(v.Float()))) 502 case reflect.Float64: 503 e.uint64(math.Float64bits(v.Float())) 504 } 505 506 case reflect.Complex64, reflect.Complex128: 507 switch v.Type().Kind() { 508 case reflect.Complex64: 509 x := v.Complex() 510 e.uint32(math.Float32bits(float32(real(x)))) 511 e.uint32(math.Float32bits(float32(imag(x)))) 512 case reflect.Complex128: 513 x := v.Complex() 514 e.uint64(math.Float64bits(real(x))) 515 e.uint64(math.Float64bits(imag(x))) 516 } 517 } 518 } 519 520 func (d *decoder) skip(v reflect.Value) { 521 n, _ := dataSize(v) 522 d.buf = d.buf[n:] 523 } 524 525 func (e *encoder) skip(v reflect.Value) { 526 n, _ := dataSize(v) 527 for i := range e.buf[0:n] { 528 e.buf[i] = 0 529 } 530 e.buf = e.buf[n:] 531 } 532 533 // intDestSize returns the size of the integer that ptrType points to, 534 // or 0 if the type is not supported. 535 func intDestSize(ptrType interface{}) int { 536 switch ptrType.(type) { 537 case *int8, *uint8: 538 return 1 539 case *int16, *uint16: 540 return 2 541 case *int32, *uint32: 542 return 4 543 case *int64, *uint64: 544 return 8 545 } 546 return 0 547 }