...
Run Format

Source file src/encoding/gob/dec_helpers.go

Documentation: encoding/gob

     1  // Code generated by go run decgen.go -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 := state.b.Bytes()
   331  		if len(data) < n {
   332  			errorf("invalid string length %d: exceeds input size %d", n, len(data))
   333  		}
   334  		slice[i] = string(data[:n])
   335  		state.b.Drop(n)
   336  	}
   337  	return true
   338  }
   339  
   340  func decUintArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   341  	// Can only slice if it is addressable.
   342  	if !v.CanAddr() {
   343  		return false
   344  	}
   345  	return decUintSlice(state, v.Slice(0, v.Len()), length, ovfl)
   346  }
   347  
   348  func decUintSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   349  	slice, ok := v.Interface().([]uint)
   350  	if !ok {
   351  		// It is kind uint but not type uint. TODO: We can handle this unsafely.
   352  		return false
   353  	}
   354  	for i := 0; i < length; i++ {
   355  		if state.b.Len() == 0 {
   356  			errorf("decoding uint array or slice: length exceeds input size (%d elements)", length)
   357  		}
   358  		x := state.decodeUint()
   359  		/*TODO if math.MaxUint32 < x {
   360  			error_(ovfl)
   361  		}*/
   362  		slice[i] = uint(x)
   363  	}
   364  	return true
   365  }
   366  
   367  func decUint16Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   368  	// Can only slice if it is addressable.
   369  	if !v.CanAddr() {
   370  		return false
   371  	}
   372  	return decUint16Slice(state, v.Slice(0, v.Len()), length, ovfl)
   373  }
   374  
   375  func decUint16Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   376  	slice, ok := v.Interface().([]uint16)
   377  	if !ok {
   378  		// It is kind uint16 but not type uint16. TODO: We can handle this unsafely.
   379  		return false
   380  	}
   381  	for i := 0; i < length; i++ {
   382  		if state.b.Len() == 0 {
   383  			errorf("decoding uint16 array or slice: length exceeds input size (%d elements)", length)
   384  		}
   385  		x := state.decodeUint()
   386  		if math.MaxUint16 < x {
   387  			error_(ovfl)
   388  		}
   389  		slice[i] = uint16(x)
   390  	}
   391  	return true
   392  }
   393  
   394  func decUint32Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   395  	// Can only slice if it is addressable.
   396  	if !v.CanAddr() {
   397  		return false
   398  	}
   399  	return decUint32Slice(state, v.Slice(0, v.Len()), length, ovfl)
   400  }
   401  
   402  func decUint32Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   403  	slice, ok := v.Interface().([]uint32)
   404  	if !ok {
   405  		// It is kind uint32 but not type uint32. TODO: We can handle this unsafely.
   406  		return false
   407  	}
   408  	for i := 0; i < length; i++ {
   409  		if state.b.Len() == 0 {
   410  			errorf("decoding uint32 array or slice: length exceeds input size (%d elements)", length)
   411  		}
   412  		x := state.decodeUint()
   413  		if math.MaxUint32 < x {
   414  			error_(ovfl)
   415  		}
   416  		slice[i] = uint32(x)
   417  	}
   418  	return true
   419  }
   420  
   421  func decUint64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   422  	// Can only slice if it is addressable.
   423  	if !v.CanAddr() {
   424  		return false
   425  	}
   426  	return decUint64Slice(state, v.Slice(0, v.Len()), length, ovfl)
   427  }
   428  
   429  func decUint64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   430  	slice, ok := v.Interface().([]uint64)
   431  	if !ok {
   432  		// It is kind uint64 but not type uint64. TODO: We can handle this unsafely.
   433  		return false
   434  	}
   435  	for i := 0; i < length; i++ {
   436  		if state.b.Len() == 0 {
   437  			errorf("decoding uint64 array or slice: length exceeds input size (%d elements)", length)
   438  		}
   439  		slice[i] = state.decodeUint()
   440  	}
   441  	return true
   442  }
   443  
   444  func decUintptrArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   445  	// Can only slice if it is addressable.
   446  	if !v.CanAddr() {
   447  		return false
   448  	}
   449  	return decUintptrSlice(state, v.Slice(0, v.Len()), length, ovfl)
   450  }
   451  
   452  func decUintptrSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   453  	slice, ok := v.Interface().([]uintptr)
   454  	if !ok {
   455  		// It is kind uintptr but not type uintptr. TODO: We can handle this unsafely.
   456  		return false
   457  	}
   458  	for i := 0; i < length; i++ {
   459  		if state.b.Len() == 0 {
   460  			errorf("decoding uintptr array or slice: length exceeds input size (%d elements)", length)
   461  		}
   462  		x := state.decodeUint()
   463  		if uint64(^uintptr(0)) < x {
   464  			error_(ovfl)
   465  		}
   466  		slice[i] = uintptr(x)
   467  	}
   468  	return true
   469  }
   470  

View as plain text