decode_other.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. package s2
  7. import (
  8. "fmt"
  9. "strconv"
  10. "github.com/klauspost/compress/internal/le"
  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. x = uint32(src[s+1])
  37. s += 2
  38. case x == 61:
  39. x = uint32(le.Load16(src, s+1))
  40. s += 3
  41. case x == 62:
  42. // Load as 32 bit and shift down.
  43. x = le.Load32(src, s)
  44. x >>= 8
  45. s += 4
  46. case x == 63:
  47. x = le.Load32(src, s+1)
  48. s += 5
  49. }
  50. length = int(x) + 1
  51. if length > len(dst)-d || length > len(src)-s || (strconv.IntSize == 32 && length <= 0) {
  52. if debug {
  53. fmt.Println("corrupt: lit size", length)
  54. }
  55. return decodeErrCodeCorrupt
  56. }
  57. if debug {
  58. fmt.Println("literals, length:", length, "d-after:", d+length)
  59. }
  60. copy(dst[d:], src[s:s+length])
  61. d += length
  62. s += length
  63. continue
  64. case tagCopy1:
  65. s += 2
  66. toffset := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
  67. length = int(src[s-2]) >> 2 & 0x7
  68. if toffset == 0 {
  69. if debug {
  70. fmt.Print("(repeat) ")
  71. }
  72. // keep last offset
  73. switch length {
  74. case 5:
  75. length = int(src[s]) + 4
  76. s += 1
  77. case 6:
  78. length = int(le.Load16(src, s)) + 1<<8
  79. s += 2
  80. case 7:
  81. in := src[s : s+3]
  82. length = int((uint32(in[2])<<16)|(uint32(in[1])<<8)|uint32(in[0])) + (1 << 16)
  83. s += 3
  84. default: // 0-> 4
  85. }
  86. } else {
  87. offset = toffset
  88. }
  89. length += 4
  90. case tagCopy2:
  91. offset = int(le.Load16(src, s+1))
  92. length = 1 + int(src[s])>>2
  93. s += 3
  94. case tagCopy4:
  95. offset = int(le.Load32(src, s+1))
  96. length = 1 + int(src[s])>>2
  97. s += 5
  98. }
  99. if offset <= 0 || d < offset || length > len(dst)-d {
  100. if debug {
  101. fmt.Println("corrupt: match, length", length, "offset:", offset, "dst avail:", len(dst)-d, "dst pos:", d)
  102. }
  103. return decodeErrCodeCorrupt
  104. }
  105. if debug {
  106. fmt.Println("copy, length:", length, "offset:", offset, "d-after:", d+length)
  107. }
  108. // Copy from an earlier sub-slice of dst to a later sub-slice.
  109. // If no overlap, use the built-in copy:
  110. if offset > length {
  111. copy(dst[d:d+length], dst[d-offset:])
  112. d += length
  113. continue
  114. }
  115. // Unlike the built-in copy function, this byte-by-byte copy always runs
  116. // forwards, even if the slices overlap. Conceptually, this is:
  117. //
  118. // d += forwardCopy(dst[d:d+length], dst[d-offset:])
  119. //
  120. // We align the slices into a and b and show the compiler they are the same size.
  121. // This allows the loop to run without bounds checks.
  122. a := dst[d : d+length]
  123. b := dst[d-offset:]
  124. b = b[:len(a)]
  125. for i := range a {
  126. a[i] = b[i]
  127. }
  128. d += length
  129. }
  130. // Remaining with extra checks...
  131. for s < len(src) {
  132. switch src[s] & 0x03 {
  133. case tagLiteral:
  134. x := uint32(src[s] >> 2)
  135. switch {
  136. case x < 60:
  137. s++
  138. case x == 60:
  139. s += 2
  140. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  141. return decodeErrCodeCorrupt
  142. }
  143. x = uint32(src[s-1])
  144. case x == 61:
  145. s += 3
  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-2]) | uint32(src[s-1])<<8
  150. case x == 62:
  151. s += 4
  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-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16
  156. case x == 63:
  157. s += 5
  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-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24
  162. }
  163. length = int(x) + 1
  164. if length > len(dst)-d || length > len(src)-s || (strconv.IntSize == 32 && length <= 0) {
  165. if debug {
  166. fmt.Println("corrupt: lit size", length)
  167. }
  168. return decodeErrCodeCorrupt
  169. }
  170. if debug {
  171. fmt.Println("literals, length:", length, "d-after:", d+length)
  172. }
  173. copy(dst[d:], src[s:s+length])
  174. d += length
  175. s += length
  176. continue
  177. case tagCopy1:
  178. s += 2
  179. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  180. return decodeErrCodeCorrupt
  181. }
  182. length = int(src[s-2]) >> 2 & 0x7
  183. toffset := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
  184. if toffset == 0 {
  185. if debug {
  186. fmt.Print("(repeat) ")
  187. }
  188. // keep last offset
  189. switch length {
  190. case 5:
  191. s += 1
  192. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  193. return decodeErrCodeCorrupt
  194. }
  195. length = int(uint32(src[s-1])) + 4
  196. case 6:
  197. s += 2
  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-2])|(uint32(src[s-1])<<8)) + (1 << 8)
  202. case 7:
  203. s += 3
  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-3])|(uint32(src[s-2])<<8)|(uint32(src[s-1])<<16)) + (1 << 16)
  208. default: // 0-> 4
  209. }
  210. } else {
  211. offset = toffset
  212. }
  213. length += 4
  214. case tagCopy2:
  215. s += 3
  216. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  217. return decodeErrCodeCorrupt
  218. }
  219. length = 1 + int(src[s-3])>>2
  220. offset = int(uint32(src[s-2]) | uint32(src[s-1])<<8)
  221. case tagCopy4:
  222. s += 5
  223. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  224. return decodeErrCodeCorrupt
  225. }
  226. length = 1 + int(src[s-5])>>2
  227. offset = int(uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24)
  228. }
  229. if offset <= 0 || d < offset || length > len(dst)-d {
  230. if debug {
  231. fmt.Println("corrupt: match, length", length, "offset:", offset, "dst avail:", len(dst)-d, "dst pos:", d)
  232. }
  233. return decodeErrCodeCorrupt
  234. }
  235. if debug {
  236. fmt.Println("copy, length:", length, "offset:", offset, "d-after:", d+length)
  237. }
  238. // Copy from an earlier sub-slice of dst to a later sub-slice.
  239. // If no overlap, use the built-in copy:
  240. if offset > length {
  241. copy(dst[d:d+length], dst[d-offset:])
  242. d += length
  243. continue
  244. }
  245. // Unlike the built-in copy function, this byte-by-byte copy always runs
  246. // forwards, even if the slices overlap. Conceptually, this is:
  247. //
  248. // d += forwardCopy(dst[d:d+length], dst[d-offset:])
  249. //
  250. // We align the slices into a and b and show the compiler they are the same size.
  251. // This allows the loop to run without bounds checks.
  252. a := dst[d : d+length]
  253. b := dst[d-offset:]
  254. b = b[:len(a)]
  255. for i := range a {
  256. a[i] = b[i]
  257. }
  258. d += length
  259. }
  260. if d != len(dst) {
  261. return decodeErrCodeCorrupt
  262. }
  263. return 0
  264. }