skip_gogo.go 3.0 KB

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