Source file src/encoding/gob/dec_helpers.go

     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  		if i >= len(slice) {
    71  			// This is a slice that we only partially allocated.
    72  			growSlice(v, &slice, length)
    73  		}
    74  		slice[i] = state.decodeUint() != 0
    75  	}
    76  	return true
    77  }
    78  
    79  func decComplex64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
    80  	// Can only slice if it is addressable.
    81  	if !v.CanAddr() {
    82  		return false
    83  	}
    84  	return decComplex64Slice(state, v.Slice(0, v.Len()), length, ovfl)
    85  }
    86  
    87  func decComplex64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
    88  	slice, ok := v.Interface().([]complex64)
    89  	if !ok {
    90  		// It is kind complex64 but not type complex64. TODO: We can handle this unsafely.
    91  		return false
    92  	}
    93  	for i := 0; i < length; i++ {
    94  		if state.b.Len() == 0 {
    95  			errorf("decoding complex64 array or slice: length exceeds input size (%d elements)", length)
    96  		}
    97  		if i >= len(slice) {
    98  			// This is a slice that we only partially allocated.
    99  			growSlice(v, &slice, length)
   100  		}
   101  		real := float32FromBits(state.decodeUint(), ovfl)
   102  		imag := float32FromBits(state.decodeUint(), ovfl)
   103  		slice[i] = complex(float32(real), float32(imag))
   104  	}
   105  	return true
   106  }
   107  
   108  func decComplex128Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   109  	// Can only slice if it is addressable.
   110  	if !v.CanAddr() {
   111  		return false
   112  	}
   113  	return decComplex128Slice(state, v.Slice(0, v.Len()), length, ovfl)
   114  }
   115  
   116  func decComplex128Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   117  	slice, ok := v.Interface().([]complex128)
   118  	if !ok {
   119  		// It is kind complex128 but not type complex128. TODO: We can handle this unsafely.
   120  		return false
   121  	}
   122  	for i := 0; i < length; i++ {
   123  		if state.b.Len() == 0 {
   124  			errorf("decoding complex128 array or slice: length exceeds input size (%d elements)", length)
   125  		}
   126  		if i >= len(slice) {
   127  			// This is a slice that we only partially allocated.
   128  			growSlice(v, &slice, length)
   129  		}
   130  		real := float64FromBits(state.decodeUint())
   131  		imag := float64FromBits(state.decodeUint())
   132  		slice[i] = complex(real, imag)
   133  	}
   134  	return true
   135  }
   136  
   137  func decFloat32Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   138  	// Can only slice if it is addressable.
   139  	if !v.CanAddr() {
   140  		return false
   141  	}
   142  	return decFloat32Slice(state, v.Slice(0, v.Len()), length, ovfl)
   143  }
   144  
   145  func decFloat32Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   146  	slice, ok := v.Interface().([]float32)
   147  	if !ok {
   148  		// It is kind float32 but not type float32. TODO: We can handle this unsafely.
   149  		return false
   150  	}
   151  	for i := 0; i < length; i++ {
   152  		if state.b.Len() == 0 {
   153  			errorf("decoding float32 array or slice: length exceeds input size (%d elements)", length)
   154  		}
   155  		if i >= len(slice) {
   156  			// This is a slice that we only partially allocated.
   157  			growSlice(v, &slice, length)
   158  		}
   159  		slice[i] = float32(float32FromBits(state.decodeUint(), ovfl))
   160  	}
   161  	return true
   162  }
   163  
   164  func decFloat64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   165  	// Can only slice if it is addressable.
   166  	if !v.CanAddr() {
   167  		return false
   168  	}
   169  	return decFloat64Slice(state, v.Slice(0, v.Len()), length, ovfl)
   170  }
   171  
   172  func decFloat64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   173  	slice, ok := v.Interface().([]float64)
   174  	if !ok {
   175  		// It is kind float64 but not type float64. TODO: We can handle this unsafely.
   176  		return false
   177  	}
   178  	for i := 0; i < length; i++ {
   179  		if state.b.Len() == 0 {
   180  			errorf("decoding float64 array or slice: length exceeds input size (%d elements)", length)
   181  		}
   182  		if i >= len(slice) {
   183  			// This is a slice that we only partially allocated.
   184  			growSlice(v, &slice, length)
   185  		}
   186  		slice[i] = float64FromBits(state.decodeUint())
   187  	}
   188  	return true
   189  }
   190  
   191  func decIntArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   192  	// Can only slice if it is addressable.
   193  	if !v.CanAddr() {
   194  		return false
   195  	}
   196  	return decIntSlice(state, v.Slice(0, v.Len()), length, ovfl)
   197  }
   198  
   199  func decIntSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   200  	slice, ok := v.Interface().([]int)
   201  	if !ok {
   202  		// It is kind int but not type int. TODO: We can handle this unsafely.
   203  		return false
   204  	}
   205  	for i := 0; i < length; i++ {
   206  		if state.b.Len() == 0 {
   207  			errorf("decoding int array or slice: length exceeds input size (%d elements)", length)
   208  		}
   209  		if i >= len(slice) {
   210  			// This is a slice that we only partially allocated.
   211  			growSlice(v, &slice, length)
   212  		}
   213  		x := state.decodeInt()
   214  		// MinInt and MaxInt
   215  		if x < ^int64(^uint(0)>>1) || int64(^uint(0)>>1) < x {
   216  			error_(ovfl)
   217  		}
   218  		slice[i] = int(x)
   219  	}
   220  	return true
   221  }
   222  
   223  func decInt16Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   224  	// Can only slice if it is addressable.
   225  	if !v.CanAddr() {
   226  		return false
   227  	}
   228  	return decInt16Slice(state, v.Slice(0, v.Len()), length, ovfl)
   229  }
   230  
   231  func decInt16Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   232  	slice, ok := v.Interface().([]int16)
   233  	if !ok {
   234  		// It is kind int16 but not type int16. TODO: We can handle this unsafely.
   235  		return false
   236  	}
   237  	for i := 0; i < length; i++ {
   238  		if state.b.Len() == 0 {
   239  			errorf("decoding int16 array or slice: length exceeds input size (%d elements)", length)
   240  		}
   241  		if i >= len(slice) {
   242  			// This is a slice that we only partially allocated.
   243  			growSlice(v, &slice, length)
   244  		}
   245  		x := state.decodeInt()
   246  		if x < math.MinInt16 || math.MaxInt16 < x {
   247  			error_(ovfl)
   248  		}
   249  		slice[i] = int16(x)
   250  	}
   251  	return true
   252  }
   253  
   254  func decInt32Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   255  	// Can only slice if it is addressable.
   256  	if !v.CanAddr() {
   257  		return false
   258  	}
   259  	return decInt32Slice(state, v.Slice(0, v.Len()), length, ovfl)
   260  }
   261  
   262  func decInt32Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   263  	slice, ok := v.Interface().([]int32)
   264  	if !ok {
   265  		// It is kind int32 but not type int32. TODO: We can handle this unsafely.
   266  		return false
   267  	}
   268  	for i := 0; i < length; i++ {
   269  		if state.b.Len() == 0 {
   270  			errorf("decoding int32 array or slice: length exceeds input size (%d elements)", length)
   271  		}
   272  		if i >= len(slice) {
   273  			// This is a slice that we only partially allocated.
   274  			growSlice(v, &slice, length)
   275  		}
   276  		x := state.decodeInt()
   277  		if x < math.MinInt32 || math.MaxInt32 < x {
   278  			error_(ovfl)
   279  		}
   280  		slice[i] = int32(x)
   281  	}
   282  	return true
   283  }
   284  
   285  func decInt64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   286  	// Can only slice if it is addressable.
   287  	if !v.CanAddr() {
   288  		return false
   289  	}
   290  	return decInt64Slice(state, v.Slice(0, v.Len()), length, ovfl)
   291  }
   292  
   293  func decInt64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   294  	slice, ok := v.Interface().([]int64)
   295  	if !ok {
   296  		// It is kind int64 but not type int64. TODO: We can handle this unsafely.
   297  		return false
   298  	}
   299  	for i := 0; i < length; i++ {
   300  		if state.b.Len() == 0 {
   301  			errorf("decoding int64 array or slice: length exceeds input size (%d elements)", length)
   302  		}
   303  		if i >= len(slice) {
   304  			// This is a slice that we only partially allocated.
   305  			growSlice(v, &slice, length)
   306  		}
   307  		slice[i] = state.decodeInt()
   308  	}
   309  	return true
   310  }
   311  
   312  func decInt8Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   313  	// Can only slice if it is addressable.
   314  	if !v.CanAddr() {
   315  		return false
   316  	}
   317  	return decInt8Slice(state, v.Slice(0, v.Len()), length, ovfl)
   318  }
   319  
   320  func decInt8Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   321  	slice, ok := v.Interface().([]int8)
   322  	if !ok {
   323  		// It is kind int8 but not type int8. TODO: We can handle this unsafely.
   324  		return false
   325  	}
   326  	for i := 0; i < length; i++ {
   327  		if state.b.Len() == 0 {
   328  			errorf("decoding int8 array or slice: length exceeds input size (%d elements)", length)
   329  		}
   330  		if i >= len(slice) {
   331  			// This is a slice that we only partially allocated.
   332  			growSlice(v, &slice, length)
   333  		}
   334  		x := state.decodeInt()
   335  		if x < math.MinInt8 || math.MaxInt8 < x {
   336  			error_(ovfl)
   337  		}
   338  		slice[i] = int8(x)
   339  	}
   340  	return true
   341  }
   342  
   343  func decStringArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   344  	// Can only slice if it is addressable.
   345  	if !v.CanAddr() {
   346  		return false
   347  	}
   348  	return decStringSlice(state, v.Slice(0, v.Len()), length, ovfl)
   349  }
   350  
   351  func decStringSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   352  	slice, ok := v.Interface().([]string)
   353  	if !ok {
   354  		// It is kind string but not type string. TODO: We can handle this unsafely.
   355  		return false
   356  	}
   357  	for i := 0; i < length; i++ {
   358  		if state.b.Len() == 0 {
   359  			errorf("decoding string array or slice: length exceeds input size (%d elements)", length)
   360  		}
   361  		if i >= len(slice) {
   362  			// This is a slice that we only partially allocated.
   363  			growSlice(v, &slice, length)
   364  		}
   365  		u := state.decodeUint()
   366  		n := int(u)
   367  		if n < 0 || uint64(n) != u || n > state.b.Len() {
   368  			errorf("length of string exceeds input size (%d bytes)", u)
   369  		}
   370  		if n > state.b.Len() {
   371  			errorf("string data too long for buffer: %d", n)
   372  		}
   373  		// Read the data.
   374  		data := state.b.Bytes()
   375  		if len(data) < n {
   376  			errorf("invalid string length %d: exceeds input size %d", n, len(data))
   377  		}
   378  		slice[i] = string(data[:n])
   379  		state.b.Drop(n)
   380  	}
   381  	return true
   382  }
   383  
   384  func decUintArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   385  	// Can only slice if it is addressable.
   386  	if !v.CanAddr() {
   387  		return false
   388  	}
   389  	return decUintSlice(state, v.Slice(0, v.Len()), length, ovfl)
   390  }
   391  
   392  func decUintSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   393  	slice, ok := v.Interface().([]uint)
   394  	if !ok {
   395  		// It is kind uint but not type uint. TODO: We can handle this unsafely.
   396  		return false
   397  	}
   398  	for i := 0; i < length; i++ {
   399  		if state.b.Len() == 0 {
   400  			errorf("decoding uint array or slice: length exceeds input size (%d elements)", length)
   401  		}
   402  		if i >= len(slice) {
   403  			// This is a slice that we only partially allocated.
   404  			growSlice(v, &slice, length)
   405  		}
   406  		x := state.decodeUint()
   407  		/*TODO if math.MaxUint32 < x {
   408  			error_(ovfl)
   409  		}*/
   410  		slice[i] = uint(x)
   411  	}
   412  	return true
   413  }
   414  
   415  func decUint16Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   416  	// Can only slice if it is addressable.
   417  	if !v.CanAddr() {
   418  		return false
   419  	}
   420  	return decUint16Slice(state, v.Slice(0, v.Len()), length, ovfl)
   421  }
   422  
   423  func decUint16Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   424  	slice, ok := v.Interface().([]uint16)
   425  	if !ok {
   426  		// It is kind uint16 but not type uint16. TODO: We can handle this unsafely.
   427  		return false
   428  	}
   429  	for i := 0; i < length; i++ {
   430  		if state.b.Len() == 0 {
   431  			errorf("decoding uint16 array or slice: length exceeds input size (%d elements)", length)
   432  		}
   433  		if i >= len(slice) {
   434  			// This is a slice that we only partially allocated.
   435  			growSlice(v, &slice, length)
   436  		}
   437  		x := state.decodeUint()
   438  		if math.MaxUint16 < x {
   439  			error_(ovfl)
   440  		}
   441  		slice[i] = uint16(x)
   442  	}
   443  	return true
   444  }
   445  
   446  func decUint32Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   447  	// Can only slice if it is addressable.
   448  	if !v.CanAddr() {
   449  		return false
   450  	}
   451  	return decUint32Slice(state, v.Slice(0, v.Len()), length, ovfl)
   452  }
   453  
   454  func decUint32Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   455  	slice, ok := v.Interface().([]uint32)
   456  	if !ok {
   457  		// It is kind uint32 but not type uint32. TODO: We can handle this unsafely.
   458  		return false
   459  	}
   460  	for i := 0; i < length; i++ {
   461  		if state.b.Len() == 0 {
   462  			errorf("decoding uint32 array or slice: length exceeds input size (%d elements)", length)
   463  		}
   464  		if i >= len(slice) {
   465  			// This is a slice that we only partially allocated.
   466  			growSlice(v, &slice, length)
   467  		}
   468  		x := state.decodeUint()
   469  		if math.MaxUint32 < x {
   470  			error_(ovfl)
   471  		}
   472  		slice[i] = uint32(x)
   473  	}
   474  	return true
   475  }
   476  
   477  func decUint64Array(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   478  	// Can only slice if it is addressable.
   479  	if !v.CanAddr() {
   480  		return false
   481  	}
   482  	return decUint64Slice(state, v.Slice(0, v.Len()), length, ovfl)
   483  }
   484  
   485  func decUint64Slice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   486  	slice, ok := v.Interface().([]uint64)
   487  	if !ok {
   488  		// It is kind uint64 but not type uint64. TODO: We can handle this unsafely.
   489  		return false
   490  	}
   491  	for i := 0; i < length; i++ {
   492  		if state.b.Len() == 0 {
   493  			errorf("decoding uint64 array or slice: length exceeds input size (%d elements)", length)
   494  		}
   495  		if i >= len(slice) {
   496  			// This is a slice that we only partially allocated.
   497  			growSlice(v, &slice, length)
   498  		}
   499  		slice[i] = state.decodeUint()
   500  	}
   501  	return true
   502  }
   503  
   504  func decUintptrArray(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   505  	// Can only slice if it is addressable.
   506  	if !v.CanAddr() {
   507  		return false
   508  	}
   509  	return decUintptrSlice(state, v.Slice(0, v.Len()), length, ovfl)
   510  }
   511  
   512  func decUintptrSlice(state *decoderState, v reflect.Value, length int, ovfl error) bool {
   513  	slice, ok := v.Interface().([]uintptr)
   514  	if !ok {
   515  		// It is kind uintptr but not type uintptr. TODO: We can handle this unsafely.
   516  		return false
   517  	}
   518  	for i := 0; i < length; i++ {
   519  		if state.b.Len() == 0 {
   520  			errorf("decoding uintptr array or slice: length exceeds input size (%d elements)", length)
   521  		}
   522  		if i >= len(slice) {
   523  			// This is a slice that we only partially allocated.
   524  			growSlice(v, &slice, length)
   525  		}
   526  		x := state.decodeUint()
   527  		if uint64(^uintptr(0)) < x {
   528  			error_(ovfl)
   529  		}
   530  		slice[i] = uintptr(x)
   531  	}
   532  	return true
   533  }
   534  
   535  // growSlice is called for a slice that we only partially allocated,
   536  // to grow it up to length.
   537  func growSlice[E any](v reflect.Value, ps *[]E, length int) {
   538  	var zero E
   539  	s := *ps
   540  	s = append(s, zero)
   541  	cp := cap(s)
   542  	if cp > length {
   543  		cp = length
   544  	}
   545  	s = s[:cp]
   546  	v.Set(reflect.ValueOf(s))
   547  	*ps = s
   548  }
   549  

View as plain text