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.
16 const uint64Size
= unsafe
.Sizeof(uint64(0))
18 // The global execution state of an instance of the encoder.
19 // Field numbers are delta encoded and always increase. The field
20 // number is initialized to -1 so 0 comes out as delta(1). A delta of
21 // 0 terminates the structure.
22 type encoderState
struct {
25 sendZero
bool // encoding an array element or map key/value pair; send zero values
26 fieldnum
int // the last field number written.
27 buf
[1 + uint64Size
]byte // buffer used by the encoder; here to avoid allocation.
30 func newEncoderState(enc
*Encoder
, b
*bytes
.Buffer
) *encoderState
{
31 return &encoderState
{enc
: enc
, b
: b
}
34 // Unsigned integers have a two-state encoding. If the number is less
35 // than 128 (0 through 0x7F), its value is written directly.
36 // Otherwise the value is written in big-endian byte order preceded
37 // by the byte length, negated.
39 // encodeUint writes an encoded unsigned integer to state.b.
40 func (state
*encoderState
) encodeUint(x
uint64) {
42 err
:= state
.b
.WriteByte(uint8(x
))
50 for n
= 1; x
> 0; n
++ {
51 state
.buf
[m
] = uint8(x
& 0xFF)
55 state
.buf
[m
] = uint8(-(n
- 1))
56 n
, err
:= state
.b
.Write(state
.buf
[m
: uint64Size
+1])
62 // encodeInt writes an encoded signed integer to state.w.
63 // The low bit of the encoding says whether to bit complement the (other bits of the)
64 // uint to recover the int.
65 func (state
*encoderState
) encodeInt(i
int64) {
72 state
.encodeUint(uint64(x
))
75 type encOp
func(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
)
77 // The 'instructions' of the encoding machine
78 type encInstr
struct {
80 field
int // field number
81 indir
int // how many pointer indirections to reach the value in the struct
82 offset
uintptr // offset in the structure of the field to encode
85 // Emit a field number and update the state to record its value for delta encoding.
86 // If the instruction pointer is nil, do nothing
87 func (state
*encoderState
) update(instr
*encInstr
) {
89 state
.encodeUint(uint64(instr
.field
- state
.fieldnum
))
90 state
.fieldnum
= instr
.field
94 // Each encoder is responsible for handling any indirections associated
95 // with the data structure. If any pointer so reached is nil, no bytes are written.
96 // If the data item is zero, no bytes are written.
97 // Otherwise, the output (for a scalar) is the field number, as an encoded integer,
98 // followed by the field data in its appropriate format.
100 func encIndirect(p unsafe
.Pointer
, indir
int) unsafe
.Pointer
{
101 for ; indir
> 0; indir
-- {
102 p
= *(*unsafe
.Pointer
)(p
)
104 return unsafe
.Pointer(nil)
110 func encBool(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
112 if b || state
.sendZero
{
122 func encInt(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
123 v
:= int64(*(*int)(p
))
124 if v
!= 0 || state
.sendZero
{
130 func encUint(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
131 v
:= uint64(*(*uint)(p
))
132 if v
!= 0 || state
.sendZero
{
138 func encInt8(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
139 v
:= int64(*(*int8)(p
))
140 if v
!= 0 || state
.sendZero
{
146 func encUint8(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
147 v
:= uint64(*(*uint8)(p
))
148 if v
!= 0 || state
.sendZero
{
154 func encInt16(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
155 v
:= int64(*(*int16)(p
))
156 if v
!= 0 || state
.sendZero
{
162 func encUint16(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
163 v
:= uint64(*(*uint16)(p
))
164 if v
!= 0 || state
.sendZero
{
170 func encInt32(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
171 v
:= int64(*(*int32)(p
))
172 if v
!= 0 || state
.sendZero
{
178 func encUint32(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
179 v
:= uint64(*(*uint32)(p
))
180 if v
!= 0 || state
.sendZero
{
186 func encInt64(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
188 if v
!= 0 || state
.sendZero
{
194 func encUint64(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
196 if v
!= 0 || state
.sendZero
{
202 func encUintptr(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
203 v
:= uint64(*(*uintptr)(p
))
204 if v
!= 0 || state
.sendZero
{
210 // Floating-point numbers are transmitted as uint64s holding the bits
211 // of the underlying representation. They are sent byte-reversed, with
212 // the exponent end coming out first, so integer floating point numbers
213 // (for example) transmit more compactly. This routine does the
215 func floatBits(f
float64) uint64 {
216 u
:= math
.Float64bits(f
)
218 for i
:= 0; i
< 8; i
++ {
226 func encFloat32(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
228 if f
!= 0 || state
.sendZero
{
229 v
:= floatBits(float64(f
))
235 func encFloat64(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
237 if f
!= 0 || state
.sendZero
{
244 // Complex numbers are just a pair of floating-point numbers, real part first.
245 func encComplex64(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
246 c
:= *(*complex64
)(p
)
247 if c
!= 0+0i || state
.sendZero
{
248 rpart
:= floatBits(float64(real(c
)))
249 ipart
:= floatBits(float64(imag(c
)))
251 state
.encodeUint(rpart
)
252 state
.encodeUint(ipart
)
256 func encComplex128(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
257 c
:= *(*complex128
)(p
)
258 if c
!= 0+0i || state
.sendZero
{
259 rpart
:= floatBits(real(c
))
260 ipart
:= floatBits(imag(c
))
262 state
.encodeUint(rpart
)
263 state
.encodeUint(ipart
)
267 func encNoOp(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
270 // Byte arrays are encoded as an unsigned count followed by the raw bytes.
271 func encUint8Array(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
273 if len(b
) > 0 || state
.sendZero
{
275 state
.encodeUint(uint64(len(b
)))
280 // Strings are encoded as an unsigned count followed by the raw bytes.
281 func encString(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
283 if len(s
) > 0 || state
.sendZero
{
285 state
.encodeUint(uint64(len(s
)))
286 io
.WriteString(state
.b
, s
)
290 // The end of a struct is marked by a delta field number of 0.
291 func encStructTerminator(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
297 // The encoder engine is an array of instructions indexed by field number of the encoding
298 // data, typically a struct. It is executed top to bottom, walking the struct.
299 type encEngine
struct {
303 const singletonField
= 0
305 func (enc
*Encoder
) encodeSingle(b
*bytes
.Buffer
, engine
*encEngine
, basep
uintptr) {
306 state
:= newEncoderState(enc
, b
)
307 state
.fieldnum
= singletonField
308 // There is no surrounding struct to frame the transmission, so we must
309 // generate data even if the item is zero. To do this, set sendZero.
310 state
.sendZero
= true
311 instr
:= &engine
.instr
[singletonField
]
312 p
:= unsafe
.Pointer(basep
) // offset will be zero
314 if p
= encIndirect(p
, instr
.indir
); p
== nil {
318 instr
.op(instr
, state
, p
)
321 func (enc
*Encoder
) encodeStruct(b
*bytes
.Buffer
, engine
*encEngine
, basep
uintptr) {
322 state
:= newEncoderState(enc
, b
)
324 for i
:= 0; i
< len(engine
.instr
); i
++ {
325 instr
:= &engine
.instr
[i
]
326 p
:= unsafe
.Pointer(basep
+ instr
.offset
)
328 if p
= encIndirect(p
, instr
.indir
); p
== nil {
332 instr
.op(instr
, state
, p
)
336 func (enc
*Encoder
) encodeArray(b
*bytes
.Buffer
, p
uintptr, op encOp
, elemWid
uintptr, elemIndir
int, length
int) {
337 state
:= newEncoderState(enc
, b
)
339 state
.sendZero
= true
340 state
.encodeUint(uint64(length
))
341 for i
:= 0; i
< length
; i
++ {
343 up
:= unsafe
.Pointer(elemp
)
345 if up
= encIndirect(up
, elemIndir
); up
== nil {
346 errorf("gob: encodeArray: nil element")
350 op(nil, state
, unsafe
.Pointer(elemp
))
351 p
+= uintptr(elemWid
)
355 func encodeReflectValue(state
*encoderState
, v reflect
.Value
, op encOp
, indir
int) {
356 for i
:= 0; i
< indir
&& v
!= nil; i
++ {
357 v
= reflect
.Indirect(v
)
360 errorf("gob: encodeReflectValue: nil element")
362 op(nil, state
, unsafe
.Pointer(v
.Addr()))
365 func (enc
*Encoder
) encodeMap(b
*bytes
.Buffer
, mv
*reflect
.MapValue
, keyOp
, elemOp encOp
, keyIndir
, elemIndir
int) {
366 state
:= newEncoderState(enc
, b
)
368 state
.sendZero
= true
370 state
.encodeUint(uint64(len(keys
)))
371 for _
, key
:= range keys
{
372 encodeReflectValue(state
, key
, keyOp
, keyIndir
)
373 encodeReflectValue(state
, mv
.Elem(key
), elemOp
, elemIndir
)
377 // To send an interface, we send a string identifying the concrete type, followed
378 // by the type identifier (which might require defining that type right now), followed
379 // by the concrete value. A nil value gets sent as the empty string for the name,
380 // followed by no value.
381 func (enc
*Encoder
) encodeInterface(b
*bytes
.Buffer
, iv
*reflect
.InterfaceValue
) {
382 state
:= newEncoderState(enc
, b
)
384 state
.sendZero
= true
390 typ
, _
:= indirect(iv
.Elem().Type())
391 name
, ok
:= concreteTypeToName
[typ
]
393 errorf("gob: type not registered for interface: %s", typ
)
396 state
.encodeUint(uint64(len(name
)))
397 _
, err
:= io
.WriteString(state
.b
, name
)
401 // Send (and maybe first define) the type id.
402 enc
.sendTypeDescriptor(typ
)
403 // Encode the value into a new buffer.
404 data
:= new(bytes
.Buffer
)
405 err
= enc
.encode(data
, iv
.Elem())
409 state
.encodeUint(uint64(data
.Len()))
410 _
, err
= state
.b
.Write(data
.Bytes())
416 var encOpMap
= []encOp
{
417 reflect
.Bool
: encBool
,
419 reflect
.Int8
: encInt8
,
420 reflect
.Int16
: encInt16
,
421 reflect
.Int32
: encInt32
,
422 reflect
.Int64
: encInt64
,
423 reflect
.Uint
: encUint
,
424 reflect
.Uint8
: encUint8
,
425 reflect
.Uint16
: encUint16
,
426 reflect
.Uint32
: encUint32
,
427 reflect
.Uint64
: encUint64
,
428 reflect
.Uintptr
: encUintptr
,
429 reflect
.Float32
: encFloat32
,
430 reflect
.Float64
: encFloat64
,
431 reflect
.Complex64
: encComplex64
,
432 reflect
.Complex128
: encComplex128
,
433 reflect
.String
: encString
,
436 // Return the encoding op for the base type under rt and
437 // the indirection count to reach it.
438 func (enc
*Encoder
) encOpFor(rt reflect
.Type
) (encOp
, int) {
439 typ
, indir
:= indirect(rt
)
442 if int(k
) < len(encOpMap
) {
447 switch t
:= typ
.(type) {
448 case *reflect
.SliceType
:
449 if t
.Elem().Kind() == reflect
.Uint8
{
453 // Slices have a header; we decode it to find the underlying array.
454 elemOp
, indir
:= enc
.encOpFor(t
.Elem())
455 op
= func(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
456 slice
:= (*reflect
.SliceHeader
)(p
)
457 if !state
.sendZero
&& slice
.Len
== 0 {
461 state
.enc
.encodeArray(state
.b
, slice
.Data
, elemOp
, t
.Elem().Size(), indir
, int(slice
.Len
))
463 case *reflect
.ArrayType
:
464 // True arrays have size in the type.
465 elemOp
, indir
:= enc
.encOpFor(t
.Elem())
466 op
= func(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
468 state
.enc
.encodeArray(state
.b
, uintptr(p
), elemOp
, t
.Elem().Size(), indir
, t
.Len())
470 case *reflect
.MapType
:
471 keyOp
, keyIndir
:= enc
.encOpFor(t
.Key())
472 elemOp
, elemIndir
:= enc
.encOpFor(t
.Elem())
473 op
= func(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
474 // Maps cannot be accessed by moving addresses around the way
475 // that slices etc. can. We must recover a full reflection value for
477 v
:= reflect
.NewValue(unsafe
.Unreflect(t
, unsafe
.Pointer((p
))))
478 mv
:= reflect
.Indirect(v
).(*reflect
.MapValue
)
479 if !state
.sendZero
&& mv
.Len() == 0 {
483 state
.enc
.encodeMap(state
.b
, mv
, keyOp
, elemOp
, keyIndir
, elemIndir
)
485 case *reflect
.StructType
:
486 // Generate a closure that calls out to the engine for the nested type.
487 enc
.getEncEngine(typ
)
488 info
:= mustGetTypeInfo(typ
)
489 op
= func(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
491 // indirect through info to delay evaluation for recursive structs
492 state
.enc
.encodeStruct(state
.b
, info
.encoder
, uintptr(p
))
494 case *reflect
.InterfaceType
:
495 op
= func(i
*encInstr
, state
*encoderState
, p unsafe
.Pointer
) {
496 // Interfaces transmit the name and contents of the concrete
497 // value they contain.
498 v
:= reflect
.NewValue(unsafe
.Unreflect(t
, unsafe
.Pointer((p
))))
499 iv
:= reflect
.Indirect(v
).(*reflect
.InterfaceValue
)
500 if !state
.sendZero
&& (iv
== nil || iv
.IsNil()) {
504 state
.enc
.encodeInterface(state
.b
, iv
)
509 errorf("gob enc: can't happen: encode type %s", rt
.String())
514 // The local Type was compiled from the actual value, so we know it's compatible.
515 func (enc
*Encoder
) compileEnc(rt reflect
.Type
) *encEngine
{
516 srt
, isStruct
:= rt
.(*reflect
.StructType
)
517 engine
:= new(encEngine
)
519 engine
.instr
= make([]encInstr
, srt
.NumField()+1) // +1 for terminator
520 for fieldnum
:= 0; fieldnum
< srt
.NumField(); fieldnum
++ {
521 f
:= srt
.Field(fieldnum
)
522 op
, indir
:= enc
.encOpFor(f
.Type
)
523 if !isExported(f
.Name
) {
526 engine
.instr
[fieldnum
] = encInstr
{op
, fieldnum
, indir
, uintptr(f
.Offset
)}
528 engine
.instr
[srt
.NumField()] = encInstr
{encStructTerminator
, 0, 0, 0}
530 engine
.instr
= make([]encInstr
, 1)
531 op
, indir
:= enc
.encOpFor(rt
)
532 engine
.instr
[0] = encInstr
{op
, singletonField
, indir
, 0} // offset is zero
537 // typeLock must be held (or we're in initialization and guaranteed single-threaded).
538 // The reflection type must have all its indirections processed out.
539 func (enc
*Encoder
) getEncEngine(rt reflect
.Type
) *encEngine
{
540 info
, err1
:= getTypeInfo(rt
)
544 if info
.encoder
== nil {
545 // mark this engine as underway before compiling to handle recursive types.
546 info
.encoder
= new(encEngine
)
547 info
.encoder
= enc
.compileEnc(rt
)
552 // Put this in a function so we can hold the lock only while compiling, not when encoding.
553 func (enc
*Encoder
) lockAndGetEncEngine(rt reflect
.Type
) *encEngine
{
555 defer typeLock
.Unlock()
556 return enc
.getEncEngine(rt
)
559 func (enc
*Encoder
) encode(b
*bytes
.Buffer
, value reflect
.Value
) (err os
.Error
) {
560 defer catchError(&err
)
561 // Dereference down to the underlying object.
562 rt
, indir
:= indirect(value
.Type())
563 for i
:= 0; i
< indir
; i
++ {
564 value
= reflect
.Indirect(value
)
566 engine
:= enc
.lockAndGetEncEngine(rt
)
567 if value
.Type().Kind() == reflect
.Struct
{
568 enc
.encodeStruct(b
, engine
, value
.Addr())
570 enc
.encodeSingle(b
, engine
, value
.Addr())