decode_other.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Copyright 2016 The Snappy-Go Authors. All rights reserved.
  2. // Copyright (c) 2019 Klaus Post. 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. //go:build (!amd64 && !arm64) || appengine || !gc || noasm
  6. // +build !amd64,!arm64 appengine !gc noasm
  7. package s2
  8. import (
  9. "fmt"
  10. "strconv"
  11. )
  12. // decode writes the decoding of src to dst. It assumes that the varint-encoded
  13. // length of the decompressed bytes has already been read, and that len(dst)
  14. // equals that length.
  15. //
  16. // It returns 0 on success or a decodeErrCodeXxx error code on failure.
  17. func s2Decode(dst, src []byte) int {
  18. const debug = false
  19. if debug {
  20. fmt.Println("Starting decode, dst len:", len(dst))
  21. }
  22. var d, s, length int
  23. offset := 0
  24. // As long as we can read at least 5 bytes...
  25. for s < len(src)-5 {
  26. // Removing bounds checks is SLOWER, when if doing
  27. // in := src[s:s+5]
  28. // Checked on Go 1.18
  29. switch src[s] & 0x03 {
  30. case tagLiteral:
  31. x := uint32(src[s] >> 2)
  32. switch {
  33. case x < 60:
  34. s++
  35. case x == 60:
  36. s += 2
  37. x = uint32(src[s-1])
  38. case x == 61:
  39. in := src[s : s+3]
  40. x = uint32(in[1]) | uint32(in[2])<<8
  41. s += 3
  42. case x == 62:
  43. in := src[s : s+4]
  44. // Load as 32 bit and shift down.
  45. x = uint32(in[0]) | uint32(in[1])<<8 | uint32(in[2])<<16 | uint32(in[3])<<24
  46. x >>= 8
  47. s += 4
  48. case x == 63:
  49. in := src[s : s+5]
  50. x = uint32(in[1]) | uint32(in[2])<<8 | uint32(in[3])<<16 | uint32(in[4])<<24
  51. s += 5
  52. }
  53. length = int(x) + 1
  54. if length > len(dst)-d || length > len(src)-s || (strconv.IntSize == 32 && length <= 0) {
  55. if debug {
  56. fmt.Println("corrupt: lit size", length)
  57. }
  58. return decodeErrCodeCorrupt
  59. }
  60. if debug {
  61. fmt.Println("literals, length:", length, "d-after:", d+length)
  62. }
  63. copy(dst[d:], src[s:s+length])
  64. d += length
  65. s += length
  66. continue
  67. case tagCopy1:
  68. s += 2
  69. toffset := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
  70. length = int(src[s-2]) >> 2 & 0x7
  71. if toffset == 0 {
  72. if debug {
  73. fmt.Print("(repeat) ")
  74. }
  75. // keep last offset
  76. switch length {
  77. case 5:
  78. length = int(src[s]) + 4
  79. s += 1
  80. case 6:
  81. in := src[s : s+2]
  82. length = int(uint32(in[0])|(uint32(in[1])<<8)) + (1 << 8)
  83. s += 2
  84. case 7:
  85. in := src[s : s+3]
  86. length = int((uint32(in[2])<<16)|(uint32(in[1])<<8)|uint32(in[0])) + (1 << 16)
  87. s += 3
  88. default: // 0-> 4
  89. }
  90. } else {
  91. offset = toffset
  92. }
  93. length += 4
  94. case tagCopy2:
  95. in := src[s : s+3]
  96. offset = int(uint32(in[1]) | uint32(in[2])<<8)
  97. length = 1 + int(in[0])>>2
  98. s += 3
  99. case tagCopy4:
  100. in := src[s : s+5]
  101. offset = int(uint32(in[1]) | uint32(in[2])<<8 | uint32(in[3])<<16 | uint32(in[4])<<24)
  102. length = 1 + int(in[0])>>2
  103. s += 5
  104. }
  105. if offset <= 0 || d < offset || length > len(dst)-d {
  106. if debug {
  107. fmt.Println("corrupt: match, length", length, "offset:", offset, "dst avail:", len(dst)-d, "dst pos:", d)
  108. }
  109. return decodeErrCodeCorrupt
  110. }
  111. if debug {
  112. fmt.Println("copy, length:", length, "offset:", offset, "d-after:", d+length)
  113. }
  114. // Copy from an earlier sub-slice of dst to a later sub-slice.
  115. // If no overlap, use the built-in copy:
  116. if offset > length {
  117. copy(dst[d:d+length], dst[d-offset:])
  118. d += length
  119. continue
  120. }
  121. // Unlike the built-in copy function, this byte-by-byte copy always runs
  122. // forwards, even if the slices overlap. Conceptually, this is:
  123. //
  124. // d += forwardCopy(dst[d:d+length], dst[d-offset:])
  125. //
  126. // We align the slices into a and b and show the compiler they are the same size.
  127. // This allows the loop to run without bounds checks.
  128. a := dst[d : d+length]
  129. b := dst[d-offset:]
  130. b = b[:len(a)]
  131. for i := range a {
  132. a[i] = b[i]
  133. }
  134. d += length
  135. }
  136. // Remaining with extra checks...
  137. for s < len(src) {
  138. switch src[s] & 0x03 {
  139. case tagLiteral:
  140. x := uint32(src[s] >> 2)
  141. switch {
  142. case x < 60:
  143. s++
  144. case x == 60:
  145. s += 2
  146. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  147. return decodeErrCodeCorrupt
  148. }
  149. x = uint32(src[s-1])
  150. case x == 61:
  151. s += 3
  152. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  153. return decodeErrCodeCorrupt
  154. }
  155. x = uint32(src[s-2]) | uint32(src[s-1])<<8
  156. case x == 62:
  157. s += 4
  158. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  159. return decodeErrCodeCorrupt
  160. }
  161. x = uint32(src[s-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16
  162. case x == 63:
  163. s += 5
  164. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  165. return decodeErrCodeCorrupt
  166. }
  167. x = uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24
  168. }
  169. length = int(x) + 1
  170. if length > len(dst)-d || length > len(src)-s || (strconv.IntSize == 32 && length <= 0) {
  171. if debug {
  172. fmt.Println("corrupt: lit size", length)
  173. }
  174. return decodeErrCodeCorrupt
  175. }
  176. if debug {
  177. fmt.Println("literals, length:", length, "d-after:", d+length)
  178. }
  179. copy(dst[d:], src[s:s+length])
  180. d += length
  181. s += length
  182. continue
  183. case tagCopy1:
  184. s += 2
  185. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  186. return decodeErrCodeCorrupt
  187. }
  188. length = int(src[s-2]) >> 2 & 0x7
  189. toffset := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
  190. if toffset == 0 {
  191. if debug {
  192. fmt.Print("(repeat) ")
  193. }
  194. // keep last offset
  195. switch length {
  196. case 5:
  197. s += 1
  198. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  199. return decodeErrCodeCorrupt
  200. }
  201. length = int(uint32(src[s-1])) + 4
  202. case 6:
  203. s += 2
  204. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  205. return decodeErrCodeCorrupt
  206. }
  207. length = int(uint32(src[s-2])|(uint32(src[s-1])<<8)) + (1 << 8)
  208. case 7:
  209. s += 3
  210. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  211. return decodeErrCodeCorrupt
  212. }
  213. length = int(uint32(src[s-3])|(uint32(src[s-2])<<8)|(uint32(src[s-1])<<16)) + (1 << 16)
  214. default: // 0-> 4
  215. }
  216. } else {
  217. offset = toffset
  218. }
  219. length += 4
  220. case tagCopy2:
  221. s += 3
  222. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  223. return decodeErrCodeCorrupt
  224. }
  225. length = 1 + int(src[s-3])>>2
  226. offset = int(uint32(src[s-2]) | uint32(src[s-1])<<8)
  227. case tagCopy4:
  228. s += 5
  229. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  230. return decodeErrCodeCorrupt
  231. }
  232. length = 1 + int(src[s-5])>>2
  233. offset = int(uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24)
  234. }
  235. if offset <= 0 || d < offset || length > len(dst)-d {
  236. if debug {
  237. fmt.Println("corrupt: match, length", length, "offset:", offset, "dst avail:", len(dst)-d, "dst pos:", d)
  238. }
  239. return decodeErrCodeCorrupt
  240. }
  241. if debug {
  242. fmt.Println("copy, length:", length, "offset:", offset, "d-after:", d+length)
  243. }
  244. // Copy from an earlier sub-slice of dst to a later sub-slice.
  245. // If no overlap, use the built-in copy:
  246. if offset > length {
  247. copy(dst[d:d+length], dst[d-offset:])
  248. d += length
  249. continue
  250. }
  251. // Unlike the built-in copy function, this byte-by-byte copy always runs
  252. // forwards, even if the slices overlap. Conceptually, this is:
  253. //
  254. // d += forwardCopy(dst[d:d+length], dst[d-offset:])
  255. //
  256. // We align the slices into a and b and show the compiler they are the same size.
  257. // This allows the loop to run without bounds checks.
  258. a := dst[d : d+length]
  259. b := dst[d-offset:]
  260. b = b[:len(a)]
  261. for i := range a {
  262. a[i] = b[i]
  263. }
  264. d += length
  265. }
  266. if d != len(dst) {
  267. return decodeErrCodeCorrupt
  268. }
  269. return 0
  270. }