2018-11-11 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / encoding / gob / dec_helpers.go
blob26eb9e4cd1c41704af11935fb751af08650f8e45
1 // Code generated by go run decgen.go -output dec_helpers.go; DO NOT EDIT.
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.
7 package gob
9 import (
10 "math"
11 "reflect"
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,
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,
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
57 return decBoolSlice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
70 slice[i] = state.decodeUint() != 0
72 return true
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
80 return decComplex64Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
93 real := float32FromBits(state.decodeUint(), ovfl)
94 imag := float32FromBits(state.decodeUint(), ovfl)
95 slice[i] = complex(float32(real), float32(imag))
97 return true
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
105 return decComplex128Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
118 real := float64FromBits(state.decodeUint())
119 imag := float64FromBits(state.decodeUint())
120 slice[i] = complex(real, imag)
122 return true
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
130 return decFloat32Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
143 slice[i] = float32(float32FromBits(state.decodeUint(), ovfl))
145 return true
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
153 return decFloat64Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
166 slice[i] = float64FromBits(state.decodeUint())
168 return true
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
176 return decIntSlice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
189 x := state.decodeInt()
190 // MinInt and MaxInt
191 if x < ^int64(^uint(0)>>1) || int64(^uint(0)>>1) < x {
192 error_(ovfl)
194 slice[i] = int(x)
196 return true
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
204 return decInt16Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
217 x := state.decodeInt()
218 if x < math.MinInt16 || math.MaxInt16 < x {
219 error_(ovfl)
221 slice[i] = int16(x)
223 return true
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
231 return decInt32Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
244 x := state.decodeInt()
245 if x < math.MinInt32 || math.MaxInt32 < x {
246 error_(ovfl)
248 slice[i] = int32(x)
250 return true
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
258 return decInt64Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
271 slice[i] = state.decodeInt()
273 return true
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
281 return decInt8Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
294 x := state.decodeInt()
295 if x < math.MinInt8 || math.MaxInt8 < x {
296 error_(ovfl)
298 slice[i] = int8(x)
300 return true
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
308 return decStringSlice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
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)
326 if n > state.b.Len() {
327 errorf("string data too long for buffer: %d", n)
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))
334 slice[i] = string(data[:n])
335 state.b.Drop(n)
337 return true
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
345 return decUintSlice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
358 x := state.decodeUint()
359 /*TODO if math.MaxUint32 < x {
360 error_(ovfl)
362 slice[i] = uint(x)
364 return true
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
372 return decUint16Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
385 x := state.decodeUint()
386 if math.MaxUint16 < x {
387 error_(ovfl)
389 slice[i] = uint16(x)
391 return true
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
399 return decUint32Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
412 x := state.decodeUint()
413 if math.MaxUint32 < x {
414 error_(ovfl)
416 slice[i] = uint32(x)
418 return true
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
426 return decUint64Slice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
439 slice[i] = state.decodeUint()
441 return true
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
449 return decUintptrSlice(state, v.Slice(0, v.Len()), length, ovfl)
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
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)
462 x := state.decodeUint()
463 if uint64(^uintptr(0)) < x {
464 error_(ovfl)
466 slice[i] = uintptr(x)
468 return true