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 encodeUint(state *encoderState, 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 encodeInt(state *encoderState, i int64) {
72 encodeUint(state, 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 encodeUint(state, 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 encFloat(i *encInstr, state *encoderState, p unsafe.Pointer) {
228 if f != 0 || state.sendZero {
229 v := floatBits(float64(f))
235 func encFloat32(i *encInstr, state *encoderState, p unsafe.Pointer) {
237 if f != 0 || state.sendZero {
238 v := floatBits(float64(f))
244 func encFloat64(i *encInstr, state *encoderState, p unsafe.Pointer) {
246 if f != 0 || state.sendZero {
253 // Complex numbers are just a pair of floating-point numbers, real part first.
254 func encComplex(i *encInstr, state *encoderState, p unsafe.Pointer) {
256 if c != 0+0i || state.sendZero {
257 rpart := floatBits(float64(real(c)))
258 ipart := floatBits(float64(imag(c)))
260 encodeUint(state, rpart)
261 encodeUint(state, ipart)
265 func encComplex64(i *encInstr, state *encoderState, p unsafe.Pointer) {
266 c := *(*complex64)(p)
267 if c != 0+0i || state.sendZero {
268 rpart := floatBits(float64(real(c)))
269 ipart := floatBits(float64(imag(c)))
271 encodeUint(state, rpart)
272 encodeUint(state, ipart)
276 func encComplex128(i *encInstr, state *encoderState, p unsafe.Pointer) {
277 c := *(*complex128)(p)
278 if c != 0+0i || state.sendZero {
279 rpart := floatBits(real(c))
280 ipart := floatBits(imag(c))
282 encodeUint(state, rpart)
283 encodeUint(state, ipart)
287 // Byte arrays are encoded as an unsigned count followed by the raw bytes.
288 func encUint8Array(i *encInstr, state *encoderState, p unsafe.Pointer) {
290 if len(b) > 0 || state.sendZero {
292 encodeUint(state, uint64(len(b)))
297 // Strings are encoded as an unsigned count followed by the raw bytes.
298 func encString(i *encInstr, state *encoderState, p unsafe.Pointer) {
300 if len(s) > 0 || state.sendZero {
302 encodeUint(state, uint64(len(s)))
303 io.WriteString(state.b, s)
307 // The end of a struct is marked by a delta field number of 0.
308 func encStructTerminator(i *encInstr, state *encoderState, p unsafe.Pointer) {
314 // The encoder engine is an array of instructions indexed by field number of the encoding
315 // data, typically a struct. It is executed top to bottom, walking the struct.
316 type encEngine struct {
320 const singletonField = 0
322 func (enc *Encoder) encodeSingle(b *bytes.Buffer, engine *encEngine, basep uintptr) {
323 state := newEncoderState(enc, b)
324 state.fieldnum = singletonField
325 // There is no surrounding struct to frame the transmission, so we must
326 // generate data even if the item is zero. To do this, set sendZero.
327 state.sendZero = true
328 instr := &engine.instr[singletonField]
329 p := unsafe.Pointer(basep) // offset will be zero
331 if p = encIndirect(p, instr.indir); p == nil {
335 instr.op(instr, state, p)
338 func (enc *Encoder) encodeStruct(b *bytes.Buffer, engine *encEngine, basep uintptr) {
339 state := newEncoderState(enc, b)
341 for i := 0; i < len(engine.instr); i++ {
342 instr := &engine.instr[i]
343 p := unsafe.Pointer(basep + instr.offset)
345 if p = encIndirect(p, instr.indir); p == nil {
349 instr.op(instr, state, p)
353 func (enc *Encoder) encodeArray(b *bytes.Buffer, p uintptr, op encOp, elemWid uintptr, elemIndir int, length int) {
354 state := newEncoderState(enc, b)
356 state.sendZero = true
357 encodeUint(state, uint64(length))
358 for i := 0; i < length; i++ {
360 up := unsafe.Pointer(elemp)
362 if up = encIndirect(up, elemIndir); up == nil {
363 errorf("gob: encodeArray: nil element")
367 op(nil, state, unsafe.Pointer(elemp))
368 p += uintptr(elemWid)
372 func encodeReflectValue(state *encoderState, v reflect.Value, op encOp, indir int) {
373 for i := 0; i < indir && v != nil; i++ {
374 v = reflect.Indirect(v)
377 errorf("gob: encodeReflectValue: nil element")
379 op(nil, state, unsafe.Pointer(v.Addr()))
382 func (enc *Encoder) encodeMap(b *bytes.Buffer, mv *reflect.MapValue, keyOp, elemOp encOp, keyIndir, elemIndir int) {
383 state := newEncoderState(enc, b)
385 state.sendZero = true
387 encodeUint(state, uint64(len(keys)))
388 for _, key := range keys {
389 encodeReflectValue(state, key, keyOp, keyIndir)
390 encodeReflectValue(state, mv.Elem(key), elemOp, elemIndir)
394 // To send an interface, we send a string identifying the concrete type, followed
395 // by the type identifier (which might require defining that type right now), followed
396 // by the concrete value. A nil value gets sent as the empty string for the name,
397 // followed by no value.
398 func (enc *Encoder) encodeInterface(b *bytes.Buffer, iv *reflect.InterfaceValue) {
399 state := newEncoderState(enc, b)
401 state.sendZero = true
407 typ, _ := indirect(iv.Elem().Type())
408 name, ok := concreteTypeToName[typ]
410 errorf("gob: type not registered for interface: %s", typ)
413 encodeUint(state, uint64(len(name)))
414 _, err := io.WriteString(state.b, name)
418 // Send (and maybe first define) the type id.
419 enc.sendTypeDescriptor(typ)
420 // Encode the value into a new buffer.
421 data := new(bytes.Buffer)
422 err = enc.encode(data, iv.Elem())
426 encodeUint(state, uint64(data.Len()))
427 _, err = state.b.Write(data.Bytes())
433 var encOpMap = []encOp{
434 reflect.Bool: encBool,
436 reflect.Int8: encInt8,
437 reflect.Int16: encInt16,
438 reflect.Int32: encInt32,
439 reflect.Int64: encInt64,
440 reflect.Uint: encUint,
441 reflect.Uint8: encUint8,
442 reflect.Uint16: encUint16,
443 reflect.Uint32: encUint32,
444 reflect.Uint64: encUint64,
445 reflect.Uintptr: encUintptr,
446 reflect.Float: encFloat,
447 reflect.Float32: encFloat32,
448 reflect.Float64: encFloat64,
449 reflect.Complex: encComplex,
450 reflect.Complex64: encComplex64,
451 reflect.Complex128: encComplex128,
452 reflect.String: encString,
455 // Return the encoding op for the base type under rt and
456 // the indirection count to reach it.
457 func (enc *Encoder) encOpFor(rt reflect.Type) (encOp, int) {
458 typ, indir := indirect(rt)
461 if int(k) < len(encOpMap) {
466 switch t := typ.(type) {
467 case *reflect.SliceType:
468 if t.Elem().Kind() == reflect.Uint8 {
472 // Slices have a header; we decode it to find the underlying array.
473 elemOp, indir := enc.encOpFor(t.Elem())
474 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
475 slice := (*reflect.SliceHeader)(p)
480 state.enc.encodeArray(state.b, slice.Data, elemOp, t.Elem().Size(), indir, int(slice.Len))
482 case *reflect.ArrayType:
483 // True arrays have size in the type.
484 elemOp, indir := enc.encOpFor(t.Elem())
485 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
487 state.enc.encodeArray(state.b, uintptr(p), elemOp, t.Elem().Size(), indir, t.Len())
489 case *reflect.MapType:
490 keyOp, keyIndir := enc.encOpFor(t.Key())
491 elemOp, elemIndir := enc.encOpFor(t.Elem())
492 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
493 // Maps cannot be accessed by moving addresses around the way
494 // that slices etc. can. We must recover a full reflection value for
496 v := reflect.NewValue(unsafe.Unreflect(t, unsafe.Pointer((p))))
497 mv := reflect.Indirect(v).(*reflect.MapValue)
502 state.enc.encodeMap(state.b, mv, keyOp, elemOp, keyIndir, elemIndir)
504 case *reflect.StructType:
505 // Generate a closure that calls out to the engine for the nested type.
506 enc.getEncEngine(typ)
507 info := mustGetTypeInfo(typ)
508 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
510 // indirect through info to delay evaluation for recursive structs
511 state.enc.encodeStruct(state.b, info.encoder, uintptr(p))
513 case *reflect.InterfaceType:
514 op = func(i *encInstr, state *encoderState, p unsafe.Pointer) {
515 // Interfaces transmit the name and contents of the concrete
516 // value they contain.
517 v := reflect.NewValue(unsafe.Unreflect(t, unsafe.Pointer((p))))
518 iv := reflect.Indirect(v).(*reflect.InterfaceValue)
519 if !state.sendZero && (iv == nil || iv.IsNil()) {
523 state.enc.encodeInterface(state.b, iv)
528 errorf("gob enc: can't happen: encode type %s", rt.String())
533 // The local Type was compiled from the actual value, so we know it's compatible.
534 func (enc *Encoder) compileEnc(rt reflect.Type) *encEngine {
535 srt, isStruct := rt.(*reflect.StructType)
536 engine := new(encEngine)
538 engine.instr = make([]encInstr, srt.NumField()+1) // +1 for terminator
539 for fieldnum := 0; fieldnum < srt.NumField(); fieldnum++ {
540 f := srt.Field(fieldnum)
541 op, indir := enc.encOpFor(f.Type)
542 engine.instr[fieldnum] = encInstr{op, fieldnum, indir, uintptr(f.Offset)}
544 engine.instr[srt.NumField()] = encInstr{encStructTerminator, 0, 0, 0}
546 engine.instr = make([]encInstr, 1)
547 op, indir := enc.encOpFor(rt)
548 engine.instr[0] = encInstr{op, singletonField, indir, 0} // offset is zero
553 // typeLock must be held (or we're in initialization and guaranteed single-threaded).
554 // The reflection type must have all its indirections processed out.
555 func (enc *Encoder) getEncEngine(rt reflect.Type) *encEngine {
556 info, err1 := getTypeInfo(rt)
560 if info.encoder == nil {
561 // mark this engine as underway before compiling to handle recursive types.
562 info.encoder = new(encEngine)
563 info.encoder = enc.compileEnc(rt)
568 // Put this in a function so we can hold the lock only while compiling, not when encoding.
569 func (enc *Encoder) lockAndGetEncEngine(rt reflect.Type) *encEngine {
571 defer typeLock.Unlock()
572 return enc.getEncEngine(rt)
575 func (enc *Encoder) encode(b *bytes.Buffer, value reflect.Value) (err os.Error) {
576 defer catchError(&err)
577 // Dereference down to the underlying object.
578 rt, indir := indirect(value.Type())
579 for i := 0; i < indir; i++ {
580 value = reflect.Indirect(value)
582 engine := enc.lockAndGetEncEngine(rt)
583 if value.Type().Kind() == reflect.Struct {
584 enc.encodeStruct(b, engine, value.Addr())
586 enc.encodeSingle(b, engine, value.Addr())