github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/encoding/gob/dec_helpers.go (about)

     1  // Created by decgen --output dec_helpers.go; DO NOT EDIT
     2  
     3  // Copyright 2014 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package gob
     8  
     9  import (
    10  	"math"
    11  	"reflect"
    12  )
    13  
    14  var decArrayHelper = map[reflect.Kind]decHelper{
    15  	reflect.Bool:       decBoolArray,
    16  	reflect.Complex64:  decComplex64Array,
    17  	reflect.Complex128: decComplex128Array,
    18  	reflect.Float32:    decFloat32Array,
    19  	reflect.Float64:    decFloat64Array,
    20  	reflect.Int:        decIntArray,
    21  	reflect.Int16:      decInt16Array,
    22  	reflect.Int32:      decInt32Array,
    23  	reflect.Int64:      decInt64Array,
    24  	reflect.Int8:       decInt8Array,
    25  	reflect.String:     decStringArray,
    26  	reflect.Uint:       decUintArray,
    27  	reflect.Uint16:     decUint16Array,
    28  	reflect.Uint32:     decUint32Array,
    29  	reflect.Uint64:     decUint64Array,
    30  	reflect.Uintptr:    decUintptrArray,
    31  }
    32  
    33  var decSliceHelper = map[reflect.Kind]decHelper{
    34  	reflect.Bool:       decBoolSlice,
    35  	reflect.Complex64:  decComplex64Slice,
    36  	reflect.Complex128: decComplex128Slice,
    37  	reflect.Float32:    decFloat32Slice,
    38  	reflect.Float64:    decFloat64Slice,
    39  	reflect.Int:        decIntSlice,
    40  	reflect.Int16:      decInt16Slice,
    41  	reflect.Int32:      decInt32Slice,
    42  	reflect.Int64:      decInt64Slice,
    43  	reflect.Int8:       decInt8Slice,
    44  	reflect.String:     decStringSlice,
    45  	reflect.Uint:       decUintSlice,
    46  	reflect.Uint16:     decUint16Slice,
    47  	reflect.Uint32:     decUint32Slice,
    48  	reflect.Uint64:     decUint64Slice,
    49  	reflect.Uintptr:    decUintptrSlice,
    50  }
    51  
    52  func decBoolArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
    53  	// Can only slice if it is addressable.
    54  	if !v.CanAddr() {
    55  		return false
    56  	}
    57  	return decBoolSlice(state, v.Slice(0, v.Len()), length, ovfl)
    58  }
    59  
    60  func decBoolSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
    61  	slice, ok := v.Interface().([]bool)
    62  	if !ok {
    63  		// It is kind bool but not type bool. TODO: We can handle this unsafely.
    64  		return false
    65  	}
    66  	for i := 0; i < length; i++ {
    67  		if state.b.Len() == 0 {
    68  			errorf("decoding bool array or slice: length exceeds input size (%d elements)", length)
    69  		}
    70  		slice[i] = state.decodeUint() != 0
    71  	}
    72  	return true
    73  }
    74  
    75  func decComplex64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
    76  	// Can only slice if it is addressable.
    77  	if !v.CanAddr() {
    78  		return false
    79  	}
    80  	return decComplex64Slice(state, v.Slice(0, v.Len()), length, ovfl)
    81  }
    82  
    83  func decComplex64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
    84  	slice, ok := v.Interface().([]complex64)
    85  	if !ok {
    86  		// It is kind complex64 but not type complex64. TODO: We can handle this unsafely.
    87  		return false
    88  	}
    89  	for i := 0; i < length; i++ {
    90  		if state.b.Len() == 0 {
    91  			errorf("decoding complex64 array or slice: length exceeds input size (%d elements)", length)
    92  		}
    93  		real := float32FromBits(state.decodeUint(), ovfl)
    94  		imag := float32FromBits(state.decodeUint(), ovfl)
    95  		slice[i] = complex(float32(real), float32(imag))
    96  	}
    97  	return true
    98  }
    99  
   100  func decComplex128Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   101  	// Can only slice if it is addressable.
   102  	if !v.CanAddr() {
   103  		return false
   104  	}
   105  	return decComplex128Slice(state, v.Slice(0, v.Len()), length, ovfl)
   106  }
   107  
   108  func decComplex128Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   109  	slice, ok := v.Interface().([]complex128)
   110  	if !ok {
   111  		// It is kind complex128 but not type complex128. TODO: We can handle this unsafely.
   112  		return false
   113  	}
   114  	for i := 0; i < length; i++ {
   115  		if state.b.Len() == 0 {
   116  			errorf("decoding complex128 array or slice: length exceeds input size (%d elements)", length)
   117  		}
   118  		real := float64FromBits(state.decodeUint())
   119  		imag := float64FromBits(state.decodeUint())
   120  		slice[i] = complex(real, imag)
   121  	}
   122  	return true
   123  }
   124  
   125  func decFloat32Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   126  	// Can only slice if it is addressable.
   127  	if !v.CanAddr() {
   128  		return false
   129  	}
   130  	return decFloat32Slice(state, v.Slice(0, v.Len()), length, ovfl)
   131  }
   132  
   133  func decFloat32Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   134  	slice, ok := v.Interface().([]float32)
   135  	if !ok {
   136  		// It is kind float32 but not type float32. TODO: We can handle this unsafely.
   137  		return false
   138  	}
   139  	for i := 0; i < length; i++ {
   140  		if state.b.Len() == 0 {
   141  			errorf("decoding float32 array or slice: length exceeds input size (%d elements)", length)
   142  		}
   143  		slice[i] = float32(float32FromBits(state.decodeUint(), ovfl))
   144  	}
   145  	return true
   146  }
   147  
   148  func decFloat64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   149  	// Can only slice if it is addressable.
   150  	if !v.CanAddr() {
   151  		return false
   152  	}
   153  	return decFloat64Slice(state, v.Slice(0, v.Len()), length, ovfl)
   154  }
   155  
   156  func decFloat64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   157  	slice, ok := v.Interface().([]float64)
   158  	if !ok {
   159  		// It is kind float64 but not type float64. TODO: We can handle this unsafely.
   160  		return false
   161  	}
   162  	for i := 0; i < length; i++ {
   163  		if state.b.Len() == 0 {
   164  			errorf("decoding float64 array or slice: length exceeds input size (%d elements)", length)
   165  		}
   166  		slice[i] = float64FromBits(state.decodeUint())
   167  	}
   168  	return true
   169  }
   170  
   171  func decIntArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   172  	// Can only slice if it is addressable.
   173  	if !v.CanAddr() {
   174  		return false
   175  	}
   176  	return decIntSlice(state, v.Slice(0, v.Len()), length, ovfl)
   177  }
   178  
   179  func decIntSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   180  	slice, ok := v.Interface().([]int)
   181  	if !ok {
   182  		// It is kind int but not type int. TODO: We can handle this unsafely.
   183  		return false
   184  	}
   185  	for i := 0; i < length; i++ {
   186  		if state.b.Len() == 0 {
   187  			errorf("decoding int array or slice: length exceeds input size (%d elements)", length)
   188  		}
   189  		x := state.decodeInt()
   190  		// MinInt and MaxInt
   191  		if x < ^int64(^uint(0)>>1) || int64(^uint(0)>>1) < x {
   192  			error_(ovfl)
   193  		}
   194  		slice[i] = int(x)
   195  	}
   196  	return true
   197  }
   198  
   199  func decInt16Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   200  	// Can only slice if it is addressable.
   201  	if !v.CanAddr() {
   202  		return false
   203  	}
   204  	return decInt16Slice(state, v.Slice(0, v.Len()), length, ovfl)
   205  }
   206  
   207  func decInt16Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   208  	slice, ok := v.Interface().([]int16)
   209  	if !ok {
   210  		// It is kind int16 but not type int16. TODO: We can handle this unsafely.
   211  		return false
   212  	}
   213  	for i := 0; i < length; i++ {
   214  		if state.b.Len() == 0 {
   215  			errorf("decoding int16 array or slice: length exceeds input size (%d elements)", length)
   216  		}
   217  		x := state.decodeInt()
   218  		if x < math.MinInt16 || math.MaxInt16 < x {
   219  			error_(ovfl)
   220  		}
   221  		slice[i] = int16(x)
   222  	}
   223  	return true
   224  }
   225  
   226  func decInt32Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   227  	// Can only slice if it is addressable.
   228  	if !v.CanAddr() {
   229  		return false
   230  	}
   231  	return decInt32Slice(state, v.Slice(0, v.Len()), length, ovfl)
   232  }
   233  
   234  func decInt32Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   235  	slice, ok := v.Interface().([]int32)
   236  	if !ok {
   237  		// It is kind int32 but not type int32. TODO: We can handle this unsafely.
   238  		return false
   239  	}
   240  	for i := 0; i < length; i++ {
   241  		if state.b.Len() == 0 {
   242  			errorf("decoding int32 array or slice: length exceeds input size (%d elements)", length)
   243  		}
   244  		x := state.decodeInt()
   245  		if x < math.MinInt32 || math.MaxInt32 < x {
   246  			error_(ovfl)
   247  		}
   248  		slice[i] = int32(x)
   249  	}
   250  	return true
   251  }
   252  
   253  func decInt64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   254  	// Can only slice if it is addressable.
   255  	if !v.CanAddr() {
   256  		return false
   257  	}
   258  	return decInt64Slice(state, v.Slice(0, v.Len()), length, ovfl)
   259  }
   260  
   261  func decInt64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   262  	slice, ok := v.Interface().([]int64)
   263  	if !ok {
   264  		// It is kind int64 but not type int64. TODO: We can handle this unsafely.
   265  		return false
   266  	}
   267  	for i := 0; i < length; i++ {
   268  		if state.b.Len() == 0 {
   269  			errorf("decoding int64 array or slice: length exceeds input size (%d elements)", length)
   270  		}
   271  		slice[i] = state.decodeInt()
   272  	}
   273  	return true
   274  }
   275  
   276  func decInt8Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   277  	// Can only slice if it is addressable.
   278  	if !v.CanAddr() {
   279  		return false
   280  	}
   281  	return decInt8Slice(state, v.Slice(0, v.Len()), length, ovfl)
   282  }
   283  
   284  func decInt8Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   285  	slice, ok := v.Interface().([]int8)
   286  	if !ok {
   287  		// It is kind int8 but not type int8. TODO: We can handle this unsafely.
   288  		return false
   289  	}
   290  	for i := 0; i < length; i++ {
   291  		if state.b.Len() == 0 {
   292  			errorf("decoding int8 array or slice: length exceeds input size (%d elements)", length)
   293  		}
   294  		x := state.decodeInt()
   295  		if x < math.MinInt8 || math.MaxInt8 < x {
   296  			error_(ovfl)
   297  		}
   298  		slice[i] = int8(x)
   299  	}
   300  	return true
   301  }
   302  
   303  func decStringArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   304  	// Can only slice if it is addressable.
   305  	if !v.CanAddr() {
   306  		return false
   307  	}
   308  	return decStringSlice(state, v.Slice(0, v.Len()), length, ovfl)
   309  }
   310  
   311  func decStringSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   312  	slice, ok := v.Interface().([]string)
   313  	if !ok {
   314  		// It is kind string but not type string. TODO: We can handle this unsafely.
   315  		return false
   316  	}
   317  	for i := 0; i < length; i++ {
   318  		if state.b.Len() == 0 {
   319  			errorf("decoding string array or slice: length exceeds input size (%d elements)", length)
   320  		}
   321  		u := state.decodeUint()
   322  		n := int(u)
   323  		if n < 0 || uint64(n) != u || n > state.b.Len() {
   324  			errorf("length of string exceeds input size (%d bytes)", u)
   325  		}
   326  		if n > state.b.Len() {
   327  			errorf("string data too long for buffer: %d", n)
   328  		}
   329  		// Read the data.
   330  		data := make([]byte, n)
   331  		if _, err := state.b.Read(data); err != nil {
   332  			errorf("error decoding string: %s", err)
   333  		}
   334  		slice[i] = string(data)
   335  	}
   336  	return true
   337  }
   338  
   339  func decUintArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   340  	// Can only slice if it is addressable.
   341  	if !v.CanAddr() {
   342  		return false
   343  	}
   344  	return decUintSlice(state, v.Slice(0, v.Len()), length, ovfl)
   345  }
   346  
   347  func decUintSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   348  	slice, ok := v.Interface().([]uint)
   349  	if !ok {
   350  		// It is kind uint but not type uint. TODO: We can handle this unsafely.
   351  		return false
   352  	}
   353  	for i := 0; i < length; i++ {
   354  		if state.b.Len() == 0 {
   355  			errorf("decoding uint array or slice: length exceeds input size (%d elements)", length)
   356  		}
   357  		x := state.decodeUint()
   358  		/*TODO if math.MaxUint32 < x {
   359  			error_(ovfl)
   360  		}*/
   361  		slice[i] = uint(x)
   362  	}
   363  	return true
   364  }
   365  
   366  func decUint16Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   367  	// Can only slice if it is addressable.
   368  	if !v.CanAddr() {
   369  		return false
   370  	}
   371  	return decUint16Slice(state, v.Slice(0, v.Len()), length, ovfl)
   372  }
   373  
   374  func decUint16Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   375  	slice, ok := v.Interface().([]uint16)
   376  	if !ok {
   377  		// It is kind uint16 but not type uint16. TODO: We can handle this unsafely.
   378  		return false
   379  	}
   380  	for i := 0; i < length; i++ {
   381  		if state.b.Len() == 0 {
   382  			errorf("decoding uint16 array or slice: length exceeds input size (%d elements)", length)
   383  		}
   384  		x := state.decodeUint()
   385  		if math.MaxUint16 < x {
   386  			error_(ovfl)
   387  		}
   388  		slice[i] = uint16(x)
   389  	}
   390  	return true
   391  }
   392  
   393  func decUint32Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   394  	// Can only slice if it is addressable.
   395  	if !v.CanAddr() {
   396  		return false
   397  	}
   398  	return decUint32Slice(state, v.Slice(0, v.Len()), length, ovfl)
   399  }
   400  
   401  func decUint32Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   402  	slice, ok := v.Interface().([]uint32)
   403  	if !ok {
   404  		// It is kind uint32 but not type uint32. TODO: We can handle this unsafely.
   405  		return false
   406  	}
   407  	for i := 0; i < length; i++ {
   408  		if state.b.Len() == 0 {
   409  			errorf("decoding uint32 array or slice: length exceeds input size (%d elements)", length)
   410  		}
   411  		x := state.decodeUint()
   412  		if math.MaxUint32 < x {
   413  			error_(ovfl)
   414  		}
   415  		slice[i] = uint32(x)
   416  	}
   417  	return true
   418  }
   419  
   420  func decUint64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   421  	// Can only slice if it is addressable.
   422  	if !v.CanAddr() {
   423  		return false
   424  	}
   425  	return decUint64Slice(state, v.Slice(0, v.Len()), length, ovfl)
   426  }
   427  
   428  func decUint64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   429  	slice, ok := v.Interface().([]uint64)
   430  	if !ok {
   431  		// It is kind uint64 but not type uint64. TODO: We can handle this unsafely.
   432  		return false
   433  	}
   434  	for i := 0; i < length; i++ {
   435  		if state.b.Len() == 0 {
   436  			errorf("decoding uint64 array or slice: length exceeds input size (%d elements)", length)
   437  		}
   438  		slice[i] = state.decodeUint()
   439  	}
   440  	return true
   441  }
   442  
   443  func decUintptrArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   444  	// Can only slice if it is addressable.
   445  	if !v.CanAddr() {
   446  		return false
   447  	}
   448  	return decUintptrSlice(state, v.Slice(0, v.Len()), length, ovfl)
   449  }
   450  
   451  func decUintptrSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   452  	slice, ok := v.Interface().([]uintptr)
   453  	if !ok {
   454  		// It is kind uintptr but not type uintptr. TODO: We can handle this unsafely.
   455  		return false
   456  	}
   457  	for i := 0; i < length; i++ {
   458  		if state.b.Len() == 0 {
   459  			errorf("decoding uintptr array or slice: length exceeds input size (%d elements)", length)
   460  		}
   461  		x := state.decodeUint()
   462  		if uint64(^uintptr(0)) < x {
   463  			error_(ovfl)
   464  		}
   465  		slice[i] = uintptr(x)
   466  	}
   467  	return true
   468  }