decode_other.go 7.6 KB

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