skip_gogo.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2013, Vastech SA (PTY) LTD. All rights reserved.
  2. // http://github.com/gogo/protobuf/gogoproto
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //
  15. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. package proto
  27. import (
  28. "fmt"
  29. "io"
  30. )
  31. func Skip(data []byte) (n int, err error) {
  32. l := len(data)
  33. index := 0
  34. for index < l {
  35. var wire uint64
  36. for shift := uint(0); ; shift += 7 {
  37. if index >= l {
  38. return 0, io.ErrUnexpectedEOF
  39. }
  40. b := data[index]
  41. index++
  42. wire |= (uint64(b) & 0x7F) << shift
  43. if b < 0x80 {
  44. break
  45. }
  46. }
  47. wireType := int(wire & 0x7)
  48. switch wireType {
  49. case 0:
  50. for {
  51. if index >= l {
  52. return 0, io.ErrUnexpectedEOF
  53. }
  54. index++
  55. if data[index-1] < 0x80 {
  56. break
  57. }
  58. }
  59. return index, nil
  60. case 1:
  61. index += 8
  62. return index, nil
  63. case 2:
  64. var length int
  65. for shift := uint(0); ; shift += 7 {
  66. if index >= l {
  67. return 0, io.ErrUnexpectedEOF
  68. }
  69. b := data[index]
  70. index++
  71. length |= (int(b) & 0x7F) << shift
  72. if b < 0x80 {
  73. break
  74. }
  75. }
  76. index += length
  77. return index, nil
  78. case 3:
  79. for {
  80. var innerWire uint64
  81. var start int = index
  82. for shift := uint(0); ; shift += 7 {
  83. if index >= l {
  84. return 0, io.ErrUnexpectedEOF
  85. }
  86. b := data[index]
  87. index++
  88. innerWire |= (uint64(b) & 0x7F) << shift
  89. if b < 0x80 {
  90. break
  91. }
  92. }
  93. innerWireType := int(innerWire & 0x7)
  94. if innerWireType == 4 {
  95. break
  96. }
  97. next, err := Skip(data[start:])
  98. if err != nil {
  99. return 0, err
  100. }
  101. index = start + next
  102. }
  103. return index, nil
  104. case 4:
  105. return index, nil
  106. case 5:
  107. index += 4
  108. return index, nil
  109. default:
  110. return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
  111. }
  112. }
  113. panic("unreachable")
  114. }