decode_other.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // +build !amd64 appengine !gc noasm
  6. package s2
  7. import "fmt"
  8. // decode writes the decoding of src to dst. It assumes that the varint-encoded
  9. // length of the decompressed bytes has already been read, and that len(dst)
  10. // equals that length.
  11. //
  12. // It returns 0 on success or a decodeErrCodeXxx error code on failure.
  13. func s2Decode(dst, src []byte) int {
  14. const debug = false
  15. if debug {
  16. fmt.Println("Starting decode, dst len:", len(dst))
  17. }
  18. var d, s, length int
  19. offset := 0
  20. for s < len(src) {
  21. switch src[s] & 0x03 {
  22. case tagLiteral:
  23. x := uint32(src[s] >> 2)
  24. switch {
  25. case x < 60:
  26. s++
  27. case x == 60:
  28. s += 2
  29. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  30. return decodeErrCodeCorrupt
  31. }
  32. x = uint32(src[s-1])
  33. case x == 61:
  34. s += 3
  35. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  36. return decodeErrCodeCorrupt
  37. }
  38. x = uint32(src[s-2]) | uint32(src[s-1])<<8
  39. case x == 62:
  40. s += 4
  41. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  42. return decodeErrCodeCorrupt
  43. }
  44. x = uint32(src[s-3]) | uint32(src[s-2])<<8 | uint32(src[s-1])<<16
  45. case x == 63:
  46. s += 5
  47. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  48. return decodeErrCodeCorrupt
  49. }
  50. x = uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24
  51. }
  52. length = int(x) + 1
  53. if length <= 0 {
  54. return decodeErrCodeUnsupportedLiteralLength
  55. }
  56. if length > len(dst)-d || length > len(src)-s {
  57. return decodeErrCodeCorrupt
  58. }
  59. if debug {
  60. fmt.Println("literals, length:", length, "d-after:", d+length)
  61. }
  62. copy(dst[d:], src[s:s+length])
  63. d += length
  64. s += length
  65. continue
  66. case tagCopy1:
  67. s += 2
  68. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  69. return decodeErrCodeCorrupt
  70. }
  71. length = int(src[s-2]) >> 2 & 0x7
  72. toffset := int(uint32(src[s-2])&0xe0<<3 | uint32(src[s-1]))
  73. if toffset == 0 {
  74. if debug {
  75. fmt.Print("(repeat) ")
  76. }
  77. // keep last offset
  78. switch length {
  79. case 5:
  80. s += 1
  81. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  82. return decodeErrCodeCorrupt
  83. }
  84. length = int(uint32(src[s-1])) + 4
  85. case 6:
  86. s += 2
  87. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  88. return decodeErrCodeCorrupt
  89. }
  90. length = int(uint32(src[s-2])|(uint32(src[s-1])<<8)) + (1 << 8)
  91. case 7:
  92. s += 3
  93. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  94. return decodeErrCodeCorrupt
  95. }
  96. length = int(uint32(src[s-3])|(uint32(src[s-2])<<8)|(uint32(src[s-1])<<16)) + (1 << 16)
  97. default: // 0-> 4
  98. }
  99. } else {
  100. offset = toffset
  101. }
  102. length += 4
  103. case tagCopy2:
  104. s += 3
  105. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  106. return decodeErrCodeCorrupt
  107. }
  108. length = 1 + int(src[s-3])>>2
  109. offset = int(uint32(src[s-2]) | uint32(src[s-1])<<8)
  110. case tagCopy4:
  111. s += 5
  112. if uint(s) > uint(len(src)) { // The uint conversions catch overflow from the previous line.
  113. return decodeErrCodeCorrupt
  114. }
  115. length = 1 + int(src[s-5])>>2
  116. offset = int(uint32(src[s-4]) | uint32(src[s-3])<<8 | uint32(src[s-2])<<16 | uint32(src[s-1])<<24)
  117. }
  118. if offset <= 0 || d < offset || length > len(dst)-d {
  119. return decodeErrCodeCorrupt
  120. }
  121. if debug {
  122. fmt.Println("copy, length:", length, "offset:", offset, "d-after:", d+length)
  123. }
  124. // Copy from an earlier sub-slice of dst to a later sub-slice.
  125. // If no overlap, use the built-in copy:
  126. if offset > length {
  127. copy(dst[d:d+length], dst[d-offset:])
  128. d += length
  129. continue
  130. }
  131. // Unlike the built-in copy function, this byte-by-byte copy always runs
  132. // forwards, even if the slices overlap. Conceptually, this is:
  133. //
  134. // d += forwardCopy(dst[d:d+length], dst[d-offset:])
  135. //
  136. // We align the slices into a and b and show the compiler they are the same size.
  137. // This allows the loop to run without bounds checks.
  138. a := dst[d : d+length]
  139. b := dst[d-offset:]
  140. b = b[:len(a)]
  141. for i := range a {
  142. a[i] = b[i]
  143. }
  144. d += length
  145. }
  146. if d != len(dst) {
  147. return decodeErrCodeCorrupt
  148. }
  149. return 0
  150. }