/src/encoding/gob/dec_helpers.go

https://gitlab.com/OBSERVER-DLL/go · Go · 469 lines · 391 code · 37 blank · 41 comment · 98 complexity · dbb8c63e4500a7f465567dcc1964ca3d MD5 · raw file

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