encode.go 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2010 The Go Authors. All rights reserved.
  4. // https://github.com/golang/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. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. /*
  33. * Routines for encoding data into the wire format for protocol buffers.
  34. */
  35. import (
  36. "errors"
  37. "fmt"
  38. "reflect"
  39. "sort"
  40. )
  41. // RequiredNotSetError is the error returned if Marshal is called with
  42. // a protocol buffer struct whose required fields have not
  43. // all been initialized. It is also the error returned if Unmarshal is
  44. // called with an encoded protocol buffer that does not include all the
  45. // required fields.
  46. //
  47. // When printed, RequiredNotSetError reports the first unset required field in a
  48. // message. If the field cannot be precisely determined, it is reported as
  49. // "{Unknown}".
  50. type RequiredNotSetError struct {
  51. field string
  52. }
  53. func (e *RequiredNotSetError) Error() string {
  54. return fmt.Sprintf("proto: required field %q not set", e.field)
  55. }
  56. var (
  57. // errRepeatedHasNil is the error returned if Marshal is called with
  58. // a struct with a repeated field containing a nil element.
  59. errRepeatedHasNil = errors.New("proto: repeated field has nil element")
  60. // ErrNil is the error returned if Marshal is called with nil.
  61. ErrNil = errors.New("proto: Marshal called with nil")
  62. )
  63. // The fundamental encoders that put bytes on the wire.
  64. // Those that take integer types all accept uint64 and are
  65. // therefore of type valueEncoder.
  66. const maxVarintBytes = 10 // maximum length of a varint
  67. // EncodeVarint returns the varint encoding of x.
  68. // This is the format for the
  69. // int32, int64, uint32, uint64, bool, and enum
  70. // protocol buffer types.
  71. // Not used by the package itself, but helpful to clients
  72. // wishing to use the same encoding.
  73. func EncodeVarint(x uint64) []byte {
  74. var buf [maxVarintBytes]byte
  75. var n int
  76. for n = 0; x > 127; n++ {
  77. buf[n] = 0x80 | uint8(x&0x7F)
  78. x >>= 7
  79. }
  80. buf[n] = uint8(x)
  81. n++
  82. return buf[0:n]
  83. }
  84. // EncodeVarint writes a varint-encoded integer to the Buffer.
  85. // This is the format for the
  86. // int32, int64, uint32, uint64, bool, and enum
  87. // protocol buffer types.
  88. func (p *Buffer) EncodeVarint(x uint64) error {
  89. for x >= 1<<7 {
  90. p.buf = append(p.buf, uint8(x&0x7f|0x80))
  91. x >>= 7
  92. }
  93. p.buf = append(p.buf, uint8(x))
  94. return nil
  95. }
  96. // SizeVarint returns the varint encoding size of an integer.
  97. func SizeVarint(x uint64) int {
  98. return sizeVarint(x)
  99. }
  100. func sizeVarint(x uint64) (n int) {
  101. for {
  102. n++
  103. x >>= 7
  104. if x == 0 {
  105. break
  106. }
  107. }
  108. return n
  109. }
  110. // EncodeFixed64 writes a 64-bit integer to the Buffer.
  111. // This is the format for the
  112. // fixed64, sfixed64, and double protocol buffer types.
  113. func (p *Buffer) EncodeFixed64(x uint64) error {
  114. p.buf = append(p.buf,
  115. uint8(x),
  116. uint8(x>>8),
  117. uint8(x>>16),
  118. uint8(x>>24),
  119. uint8(x>>32),
  120. uint8(x>>40),
  121. uint8(x>>48),
  122. uint8(x>>56))
  123. return nil
  124. }
  125. func sizeFixed64(x uint64) int {
  126. return 8
  127. }
  128. // EncodeFixed32 writes a 32-bit integer to the Buffer.
  129. // This is the format for the
  130. // fixed32, sfixed32, and float protocol buffer types.
  131. func (p *Buffer) EncodeFixed32(x uint64) error {
  132. p.buf = append(p.buf,
  133. uint8(x),
  134. uint8(x>>8),
  135. uint8(x>>16),
  136. uint8(x>>24))
  137. return nil
  138. }
  139. func sizeFixed32(x uint64) int {
  140. return 4
  141. }
  142. // EncodeZigzag64 writes a zigzag-encoded 64-bit integer
  143. // to the Buffer.
  144. // This is the format used for the sint64 protocol buffer type.
  145. func (p *Buffer) EncodeZigzag64(x uint64) error {
  146. // use signed number to get arithmetic right shift.
  147. return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  148. }
  149. func sizeZigzag64(x uint64) int {
  150. return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
  151. }
  152. // EncodeZigzag32 writes a zigzag-encoded 32-bit integer
  153. // to the Buffer.
  154. // This is the format used for the sint32 protocol buffer type.
  155. func (p *Buffer) EncodeZigzag32(x uint64) error {
  156. // use signed number to get arithmetic right shift.
  157. return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  158. }
  159. func sizeZigzag32(x uint64) int {
  160. return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
  161. }
  162. // EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
  163. // This is the format used for the bytes protocol buffer
  164. // type and for embedded messages.
  165. func (p *Buffer) EncodeRawBytes(b []byte) error {
  166. p.EncodeVarint(uint64(len(b)))
  167. p.buf = append(p.buf, b...)
  168. return nil
  169. }
  170. func sizeRawBytes(b []byte) int {
  171. return sizeVarint(uint64(len(b))) +
  172. len(b)
  173. }
  174. // EncodeStringBytes writes an encoded string to the Buffer.
  175. // This is the format used for the proto2 string type.
  176. func (p *Buffer) EncodeStringBytes(s string) error {
  177. p.EncodeVarint(uint64(len(s)))
  178. p.buf = append(p.buf, s...)
  179. return nil
  180. }
  181. func sizeStringBytes(s string) int {
  182. return sizeVarint(uint64(len(s))) +
  183. len(s)
  184. }
  185. // Marshaler is the interface representing objects that can marshal themselves.
  186. type Marshaler interface {
  187. Marshal() ([]byte, error)
  188. }
  189. // Marshal takes the protocol buffer
  190. // and encodes it into the wire format, returning the data.
  191. func Marshal(pb Message) ([]byte, error) {
  192. // Can the object marshal itself?
  193. if m, ok := pb.(Marshaler); ok {
  194. return m.Marshal()
  195. }
  196. p := NewBuffer(nil)
  197. err := p.Marshal(pb)
  198. var state errorState
  199. if err != nil && !state.shouldContinue(err, nil) {
  200. return nil, err
  201. }
  202. if p.buf == nil && err == nil {
  203. // Return a non-nil slice on success.
  204. return []byte{}, nil
  205. }
  206. return p.buf, err
  207. }
  208. // EncodeMessage writes the protocol buffer to the Buffer,
  209. // prefixed by a varint-encoded length.
  210. func (p *Buffer) EncodeMessage(pb Message) error {
  211. t, base, err := getbase(pb)
  212. if structPointer_IsNil(base) {
  213. return ErrNil
  214. }
  215. if err == nil {
  216. var state errorState
  217. err = p.enc_len_struct(GetProperties(t.Elem()), base, &state)
  218. }
  219. return err
  220. }
  221. // Marshal takes the protocol buffer
  222. // and encodes it into the wire format, writing the result to the
  223. // Buffer.
  224. func (p *Buffer) Marshal(pb Message) error {
  225. // Can the object marshal itself?
  226. if m, ok := pb.(Marshaler); ok {
  227. data, err := m.Marshal()
  228. if err != nil {
  229. return err
  230. }
  231. p.buf = append(p.buf, data...)
  232. return nil
  233. }
  234. t, base, err := getbase(pb)
  235. if structPointer_IsNil(base) {
  236. return ErrNil
  237. }
  238. if err == nil {
  239. err = p.enc_struct(GetProperties(t.Elem()), base)
  240. }
  241. if collectStats {
  242. stats.Encode++
  243. }
  244. return err
  245. }
  246. // Size returns the encoded size of a protocol buffer.
  247. func Size(pb Message) (n int) {
  248. // Can the object marshal itself? If so, Size is slow.
  249. // TODO: add Size to Marshaler, or add a Sizer interface.
  250. if m, ok := pb.(Marshaler); ok {
  251. b, _ := m.Marshal()
  252. return len(b)
  253. }
  254. t, base, err := getbase(pb)
  255. if structPointer_IsNil(base) {
  256. return 0
  257. }
  258. if err == nil {
  259. n = size_struct(GetProperties(t.Elem()), base)
  260. }
  261. if collectStats {
  262. stats.Size++
  263. }
  264. return
  265. }
  266. // Individual type encoders.
  267. // Encode a bool.
  268. func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
  269. v := *structPointer_Bool(base, p.field)
  270. if v == nil {
  271. return ErrNil
  272. }
  273. x := 0
  274. if *v {
  275. x = 1
  276. }
  277. o.buf = append(o.buf, p.tagcode...)
  278. p.valEnc(o, uint64(x))
  279. return nil
  280. }
  281. func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error {
  282. v := *structPointer_BoolVal(base, p.field)
  283. if !v {
  284. return ErrNil
  285. }
  286. o.buf = append(o.buf, p.tagcode...)
  287. p.valEnc(o, 1)
  288. return nil
  289. }
  290. func size_bool(p *Properties, base structPointer) int {
  291. v := *structPointer_Bool(base, p.field)
  292. if v == nil {
  293. return 0
  294. }
  295. return len(p.tagcode) + 1 // each bool takes exactly one byte
  296. }
  297. func size_proto3_bool(p *Properties, base structPointer) int {
  298. v := *structPointer_BoolVal(base, p.field)
  299. if !v && !p.oneof {
  300. return 0
  301. }
  302. return len(p.tagcode) + 1 // each bool takes exactly one byte
  303. }
  304. // Encode an int32.
  305. func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
  306. v := structPointer_Word32(base, p.field)
  307. if word32_IsNil(v) {
  308. return ErrNil
  309. }
  310. x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
  311. o.buf = append(o.buf, p.tagcode...)
  312. p.valEnc(o, uint64(x))
  313. return nil
  314. }
  315. func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error {
  316. v := structPointer_Word32Val(base, p.field)
  317. x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
  318. if x == 0 {
  319. return ErrNil
  320. }
  321. o.buf = append(o.buf, p.tagcode...)
  322. p.valEnc(o, uint64(x))
  323. return nil
  324. }
  325. func size_int32(p *Properties, base structPointer) (n int) {
  326. v := structPointer_Word32(base, p.field)
  327. if word32_IsNil(v) {
  328. return 0
  329. }
  330. x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
  331. n += len(p.tagcode)
  332. n += p.valSize(uint64(x))
  333. return
  334. }
  335. func size_proto3_int32(p *Properties, base structPointer) (n int) {
  336. v := structPointer_Word32Val(base, p.field)
  337. x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
  338. if x == 0 && !p.oneof {
  339. return 0
  340. }
  341. n += len(p.tagcode)
  342. n += p.valSize(uint64(x))
  343. return
  344. }
  345. // Encode a uint32.
  346. // Exactly the same as int32, except for no sign extension.
  347. func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
  348. v := structPointer_Word32(base, p.field)
  349. if word32_IsNil(v) {
  350. return ErrNil
  351. }
  352. x := word32_Get(v)
  353. o.buf = append(o.buf, p.tagcode...)
  354. p.valEnc(o, uint64(x))
  355. return nil
  356. }
  357. func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error {
  358. v := structPointer_Word32Val(base, p.field)
  359. x := word32Val_Get(v)
  360. if x == 0 {
  361. return ErrNil
  362. }
  363. o.buf = append(o.buf, p.tagcode...)
  364. p.valEnc(o, uint64(x))
  365. return nil
  366. }
  367. func size_uint32(p *Properties, base structPointer) (n int) {
  368. v := structPointer_Word32(base, p.field)
  369. if word32_IsNil(v) {
  370. return 0
  371. }
  372. x := word32_Get(v)
  373. n += len(p.tagcode)
  374. n += p.valSize(uint64(x))
  375. return
  376. }
  377. func size_proto3_uint32(p *Properties, base structPointer) (n int) {
  378. v := structPointer_Word32Val(base, p.field)
  379. x := word32Val_Get(v)
  380. if x == 0 && !p.oneof {
  381. return 0
  382. }
  383. n += len(p.tagcode)
  384. n += p.valSize(uint64(x))
  385. return
  386. }
  387. // Encode an int64.
  388. func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
  389. v := structPointer_Word64(base, p.field)
  390. if word64_IsNil(v) {
  391. return ErrNil
  392. }
  393. x := word64_Get(v)
  394. o.buf = append(o.buf, p.tagcode...)
  395. p.valEnc(o, x)
  396. return nil
  397. }
  398. func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error {
  399. v := structPointer_Word64Val(base, p.field)
  400. x := word64Val_Get(v)
  401. if x == 0 {
  402. return ErrNil
  403. }
  404. o.buf = append(o.buf, p.tagcode...)
  405. p.valEnc(o, x)
  406. return nil
  407. }
  408. func size_int64(p *Properties, base structPointer) (n int) {
  409. v := structPointer_Word64(base, p.field)
  410. if word64_IsNil(v) {
  411. return 0
  412. }
  413. x := word64_Get(v)
  414. n += len(p.tagcode)
  415. n += p.valSize(x)
  416. return
  417. }
  418. func size_proto3_int64(p *Properties, base structPointer) (n int) {
  419. v := structPointer_Word64Val(base, p.field)
  420. x := word64Val_Get(v)
  421. if x == 0 && !p.oneof {
  422. return 0
  423. }
  424. n += len(p.tagcode)
  425. n += p.valSize(x)
  426. return
  427. }
  428. // Encode a string.
  429. func (o *Buffer) enc_string(p *Properties, base structPointer) error {
  430. v := *structPointer_String(base, p.field)
  431. if v == nil {
  432. return ErrNil
  433. }
  434. x := *v
  435. o.buf = append(o.buf, p.tagcode...)
  436. o.EncodeStringBytes(x)
  437. return nil
  438. }
  439. func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error {
  440. v := *structPointer_StringVal(base, p.field)
  441. if v == "" {
  442. return ErrNil
  443. }
  444. o.buf = append(o.buf, p.tagcode...)
  445. o.EncodeStringBytes(v)
  446. return nil
  447. }
  448. func size_string(p *Properties, base structPointer) (n int) {
  449. v := *structPointer_String(base, p.field)
  450. if v == nil {
  451. return 0
  452. }
  453. x := *v
  454. n += len(p.tagcode)
  455. n += sizeStringBytes(x)
  456. return
  457. }
  458. func size_proto3_string(p *Properties, base structPointer) (n int) {
  459. v := *structPointer_StringVal(base, p.field)
  460. if v == "" && !p.oneof {
  461. return 0
  462. }
  463. n += len(p.tagcode)
  464. n += sizeStringBytes(v)
  465. return
  466. }
  467. // All protocol buffer fields are nillable, but be careful.
  468. func isNil(v reflect.Value) bool {
  469. switch v.Kind() {
  470. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  471. return v.IsNil()
  472. }
  473. return false
  474. }
  475. // Encode a message struct.
  476. func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
  477. var state errorState
  478. structp := structPointer_GetStructPointer(base, p.field)
  479. if structPointer_IsNil(structp) {
  480. return ErrNil
  481. }
  482. // Can the object marshal itself?
  483. if p.isMarshaler {
  484. m := structPointer_Interface(structp, p.stype).(Marshaler)
  485. data, err := m.Marshal()
  486. if err != nil && !state.shouldContinue(err, nil) {
  487. return err
  488. }
  489. o.buf = append(o.buf, p.tagcode...)
  490. o.EncodeRawBytes(data)
  491. return state.err
  492. }
  493. o.buf = append(o.buf, p.tagcode...)
  494. return o.enc_len_struct(p.sprop, structp, &state)
  495. }
  496. func size_struct_message(p *Properties, base structPointer) int {
  497. structp := structPointer_GetStructPointer(base, p.field)
  498. if structPointer_IsNil(structp) {
  499. return 0
  500. }
  501. // Can the object marshal itself?
  502. if p.isMarshaler {
  503. m := structPointer_Interface(structp, p.stype).(Marshaler)
  504. data, _ := m.Marshal()
  505. n0 := len(p.tagcode)
  506. n1 := sizeRawBytes(data)
  507. return n0 + n1
  508. }
  509. n0 := len(p.tagcode)
  510. n1 := size_struct(p.sprop, structp)
  511. n2 := sizeVarint(uint64(n1)) // size of encoded length
  512. return n0 + n1 + n2
  513. }
  514. // Encode a group struct.
  515. func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
  516. var state errorState
  517. b := structPointer_GetStructPointer(base, p.field)
  518. if structPointer_IsNil(b) {
  519. return ErrNil
  520. }
  521. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  522. err := o.enc_struct(p.sprop, b)
  523. if err != nil && !state.shouldContinue(err, nil) {
  524. return err
  525. }
  526. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  527. return state.err
  528. }
  529. func size_struct_group(p *Properties, base structPointer) (n int) {
  530. b := structPointer_GetStructPointer(base, p.field)
  531. if structPointer_IsNil(b) {
  532. return 0
  533. }
  534. n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
  535. n += size_struct(p.sprop, b)
  536. n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
  537. return
  538. }
  539. // Encode a slice of bools ([]bool).
  540. func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
  541. s := *structPointer_BoolSlice(base, p.field)
  542. l := len(s)
  543. if l == 0 {
  544. return ErrNil
  545. }
  546. for _, x := range s {
  547. o.buf = append(o.buf, p.tagcode...)
  548. v := uint64(0)
  549. if x {
  550. v = 1
  551. }
  552. p.valEnc(o, v)
  553. }
  554. return nil
  555. }
  556. func size_slice_bool(p *Properties, base structPointer) int {
  557. s := *structPointer_BoolSlice(base, p.field)
  558. l := len(s)
  559. if l == 0 {
  560. return 0
  561. }
  562. return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
  563. }
  564. // Encode a slice of bools ([]bool) in packed format.
  565. func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
  566. s := *structPointer_BoolSlice(base, p.field)
  567. l := len(s)
  568. if l == 0 {
  569. return ErrNil
  570. }
  571. o.buf = append(o.buf, p.tagcode...)
  572. o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
  573. for _, x := range s {
  574. v := uint64(0)
  575. if x {
  576. v = 1
  577. }
  578. p.valEnc(o, v)
  579. }
  580. return nil
  581. }
  582. func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
  583. s := *structPointer_BoolSlice(base, p.field)
  584. l := len(s)
  585. if l == 0 {
  586. return 0
  587. }
  588. n += len(p.tagcode)
  589. n += sizeVarint(uint64(l))
  590. n += l // each bool takes exactly one byte
  591. return
  592. }
  593. // Encode a slice of bytes ([]byte).
  594. func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
  595. s := *structPointer_Bytes(base, p.field)
  596. if s == nil {
  597. return ErrNil
  598. }
  599. o.buf = append(o.buf, p.tagcode...)
  600. o.EncodeRawBytes(s)
  601. return nil
  602. }
  603. func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error {
  604. s := *structPointer_Bytes(base, p.field)
  605. if len(s) == 0 {
  606. return ErrNil
  607. }
  608. o.buf = append(o.buf, p.tagcode...)
  609. o.EncodeRawBytes(s)
  610. return nil
  611. }
  612. func size_slice_byte(p *Properties, base structPointer) (n int) {
  613. s := *structPointer_Bytes(base, p.field)
  614. if s == nil && !p.oneof {
  615. return 0
  616. }
  617. n += len(p.tagcode)
  618. n += sizeRawBytes(s)
  619. return
  620. }
  621. func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
  622. s := *structPointer_Bytes(base, p.field)
  623. if len(s) == 0 && !p.oneof {
  624. return 0
  625. }
  626. n += len(p.tagcode)
  627. n += sizeRawBytes(s)
  628. return
  629. }
  630. // Encode a slice of int32s ([]int32).
  631. func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
  632. s := structPointer_Word32Slice(base, p.field)
  633. l := s.Len()
  634. if l == 0 {
  635. return ErrNil
  636. }
  637. for i := 0; i < l; i++ {
  638. o.buf = append(o.buf, p.tagcode...)
  639. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  640. p.valEnc(o, uint64(x))
  641. }
  642. return nil
  643. }
  644. func size_slice_int32(p *Properties, base structPointer) (n int) {
  645. s := structPointer_Word32Slice(base, p.field)
  646. l := s.Len()
  647. if l == 0 {
  648. return 0
  649. }
  650. for i := 0; i < l; i++ {
  651. n += len(p.tagcode)
  652. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  653. n += p.valSize(uint64(x))
  654. }
  655. return
  656. }
  657. // Encode a slice of int32s ([]int32) in packed format.
  658. func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
  659. s := structPointer_Word32Slice(base, p.field)
  660. l := s.Len()
  661. if l == 0 {
  662. return ErrNil
  663. }
  664. // TODO: Reuse a Buffer.
  665. buf := NewBuffer(nil)
  666. for i := 0; i < l; i++ {
  667. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  668. p.valEnc(buf, uint64(x))
  669. }
  670. o.buf = append(o.buf, p.tagcode...)
  671. o.EncodeVarint(uint64(len(buf.buf)))
  672. o.buf = append(o.buf, buf.buf...)
  673. return nil
  674. }
  675. func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
  676. s := structPointer_Word32Slice(base, p.field)
  677. l := s.Len()
  678. if l == 0 {
  679. return 0
  680. }
  681. var bufSize int
  682. for i := 0; i < l; i++ {
  683. x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
  684. bufSize += p.valSize(uint64(x))
  685. }
  686. n += len(p.tagcode)
  687. n += sizeVarint(uint64(bufSize))
  688. n += bufSize
  689. return
  690. }
  691. // Encode a slice of uint32s ([]uint32).
  692. // Exactly the same as int32, except for no sign extension.
  693. func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
  694. s := structPointer_Word32Slice(base, p.field)
  695. l := s.Len()
  696. if l == 0 {
  697. return ErrNil
  698. }
  699. for i := 0; i < l; i++ {
  700. o.buf = append(o.buf, p.tagcode...)
  701. x := s.Index(i)
  702. p.valEnc(o, uint64(x))
  703. }
  704. return nil
  705. }
  706. func size_slice_uint32(p *Properties, base structPointer) (n int) {
  707. s := structPointer_Word32Slice(base, p.field)
  708. l := s.Len()
  709. if l == 0 {
  710. return 0
  711. }
  712. for i := 0; i < l; i++ {
  713. n += len(p.tagcode)
  714. x := s.Index(i)
  715. n += p.valSize(uint64(x))
  716. }
  717. return
  718. }
  719. // Encode a slice of uint32s ([]uint32) in packed format.
  720. // Exactly the same as int32, except for no sign extension.
  721. func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error {
  722. s := structPointer_Word32Slice(base, p.field)
  723. l := s.Len()
  724. if l == 0 {
  725. return ErrNil
  726. }
  727. // TODO: Reuse a Buffer.
  728. buf := NewBuffer(nil)
  729. for i := 0; i < l; i++ {
  730. p.valEnc(buf, uint64(s.Index(i)))
  731. }
  732. o.buf = append(o.buf, p.tagcode...)
  733. o.EncodeVarint(uint64(len(buf.buf)))
  734. o.buf = append(o.buf, buf.buf...)
  735. return nil
  736. }
  737. func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
  738. s := structPointer_Word32Slice(base, p.field)
  739. l := s.Len()
  740. if l == 0 {
  741. return 0
  742. }
  743. var bufSize int
  744. for i := 0; i < l; i++ {
  745. bufSize += p.valSize(uint64(s.Index(i)))
  746. }
  747. n += len(p.tagcode)
  748. n += sizeVarint(uint64(bufSize))
  749. n += bufSize
  750. return
  751. }
  752. // Encode a slice of int64s ([]int64).
  753. func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
  754. s := structPointer_Word64Slice(base, p.field)
  755. l := s.Len()
  756. if l == 0 {
  757. return ErrNil
  758. }
  759. for i := 0; i < l; i++ {
  760. o.buf = append(o.buf, p.tagcode...)
  761. p.valEnc(o, s.Index(i))
  762. }
  763. return nil
  764. }
  765. func size_slice_int64(p *Properties, base structPointer) (n int) {
  766. s := structPointer_Word64Slice(base, p.field)
  767. l := s.Len()
  768. if l == 0 {
  769. return 0
  770. }
  771. for i := 0; i < l; i++ {
  772. n += len(p.tagcode)
  773. n += p.valSize(s.Index(i))
  774. }
  775. return
  776. }
  777. // Encode a slice of int64s ([]int64) in packed format.
  778. func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
  779. s := structPointer_Word64Slice(base, p.field)
  780. l := s.Len()
  781. if l == 0 {
  782. return ErrNil
  783. }
  784. // TODO: Reuse a Buffer.
  785. buf := NewBuffer(nil)
  786. for i := 0; i < l; i++ {
  787. p.valEnc(buf, s.Index(i))
  788. }
  789. o.buf = append(o.buf, p.tagcode...)
  790. o.EncodeVarint(uint64(len(buf.buf)))
  791. o.buf = append(o.buf, buf.buf...)
  792. return nil
  793. }
  794. func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
  795. s := structPointer_Word64Slice(base, p.field)
  796. l := s.Len()
  797. if l == 0 {
  798. return 0
  799. }
  800. var bufSize int
  801. for i := 0; i < l; i++ {
  802. bufSize += p.valSize(s.Index(i))
  803. }
  804. n += len(p.tagcode)
  805. n += sizeVarint(uint64(bufSize))
  806. n += bufSize
  807. return
  808. }
  809. // Encode a slice of slice of bytes ([][]byte).
  810. func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
  811. ss := *structPointer_BytesSlice(base, p.field)
  812. l := len(ss)
  813. if l == 0 {
  814. return ErrNil
  815. }
  816. for i := 0; i < l; i++ {
  817. o.buf = append(o.buf, p.tagcode...)
  818. o.EncodeRawBytes(ss[i])
  819. }
  820. return nil
  821. }
  822. func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
  823. ss := *structPointer_BytesSlice(base, p.field)
  824. l := len(ss)
  825. if l == 0 {
  826. return 0
  827. }
  828. n += l * len(p.tagcode)
  829. for i := 0; i < l; i++ {
  830. n += sizeRawBytes(ss[i])
  831. }
  832. return
  833. }
  834. // Encode a slice of strings ([]string).
  835. func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
  836. ss := *structPointer_StringSlice(base, p.field)
  837. l := len(ss)
  838. for i := 0; i < l; i++ {
  839. o.buf = append(o.buf, p.tagcode...)
  840. o.EncodeStringBytes(ss[i])
  841. }
  842. return nil
  843. }
  844. func size_slice_string(p *Properties, base structPointer) (n int) {
  845. ss := *structPointer_StringSlice(base, p.field)
  846. l := len(ss)
  847. n += l * len(p.tagcode)
  848. for i := 0; i < l; i++ {
  849. n += sizeStringBytes(ss[i])
  850. }
  851. return
  852. }
  853. // Encode a slice of message structs ([]*struct).
  854. func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
  855. var state errorState
  856. s := structPointer_StructPointerSlice(base, p.field)
  857. l := s.Len()
  858. for i := 0; i < l; i++ {
  859. structp := s.Index(i)
  860. if structPointer_IsNil(structp) {
  861. return errRepeatedHasNil
  862. }
  863. // Can the object marshal itself?
  864. if p.isMarshaler {
  865. m := structPointer_Interface(structp, p.stype).(Marshaler)
  866. data, err := m.Marshal()
  867. if err != nil && !state.shouldContinue(err, nil) {
  868. return err
  869. }
  870. o.buf = append(o.buf, p.tagcode...)
  871. o.EncodeRawBytes(data)
  872. continue
  873. }
  874. o.buf = append(o.buf, p.tagcode...)
  875. err := o.enc_len_struct(p.sprop, structp, &state)
  876. if err != nil && !state.shouldContinue(err, nil) {
  877. if err == ErrNil {
  878. return errRepeatedHasNil
  879. }
  880. return err
  881. }
  882. }
  883. return state.err
  884. }
  885. func size_slice_struct_message(p *Properties, base structPointer) (n int) {
  886. s := structPointer_StructPointerSlice(base, p.field)
  887. l := s.Len()
  888. n += l * len(p.tagcode)
  889. for i := 0; i < l; i++ {
  890. structp := s.Index(i)
  891. if structPointer_IsNil(structp) {
  892. return // return the size up to this point
  893. }
  894. // Can the object marshal itself?
  895. if p.isMarshaler {
  896. m := structPointer_Interface(structp, p.stype).(Marshaler)
  897. data, _ := m.Marshal()
  898. n += len(p.tagcode)
  899. n += sizeRawBytes(data)
  900. continue
  901. }
  902. n0 := size_struct(p.sprop, structp)
  903. n1 := sizeVarint(uint64(n0)) // size of encoded length
  904. n += n0 + n1
  905. }
  906. return
  907. }
  908. // Encode a slice of group structs ([]*struct).
  909. func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
  910. var state errorState
  911. s := structPointer_StructPointerSlice(base, p.field)
  912. l := s.Len()
  913. for i := 0; i < l; i++ {
  914. b := s.Index(i)
  915. if structPointer_IsNil(b) {
  916. return errRepeatedHasNil
  917. }
  918. o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
  919. err := o.enc_struct(p.sprop, b)
  920. if err != nil && !state.shouldContinue(err, nil) {
  921. if err == ErrNil {
  922. return errRepeatedHasNil
  923. }
  924. return err
  925. }
  926. o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
  927. }
  928. return state.err
  929. }
  930. func size_slice_struct_group(p *Properties, base structPointer) (n int) {
  931. s := structPointer_StructPointerSlice(base, p.field)
  932. l := s.Len()
  933. n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
  934. n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
  935. for i := 0; i < l; i++ {
  936. b := s.Index(i)
  937. if structPointer_IsNil(b) {
  938. return // return size up to this point
  939. }
  940. n += size_struct(p.sprop, b)
  941. }
  942. return
  943. }
  944. // Encode an extension map.
  945. func (o *Buffer) enc_map(p *Properties, base structPointer) error {
  946. v := *structPointer_ExtMap(base, p.field)
  947. if err := encodeExtensionMap(v); err != nil {
  948. return err
  949. }
  950. // Fast-path for common cases: zero or one extensions.
  951. if len(v) <= 1 {
  952. for _, e := range v {
  953. o.buf = append(o.buf, e.enc...)
  954. }
  955. return nil
  956. }
  957. // Sort keys to provide a deterministic encoding.
  958. keys := make([]int, 0, len(v))
  959. for k := range v {
  960. keys = append(keys, int(k))
  961. }
  962. sort.Ints(keys)
  963. for _, k := range keys {
  964. o.buf = append(o.buf, v[int32(k)].enc...)
  965. }
  966. return nil
  967. }
  968. func size_map(p *Properties, base structPointer) int {
  969. v := *structPointer_ExtMap(base, p.field)
  970. return sizeExtensionMap(v)
  971. }
  972. // Encode a map field.
  973. func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
  974. var state errorState // XXX: or do we need to plumb this through?
  975. /*
  976. A map defined as
  977. map<key_type, value_type> map_field = N;
  978. is encoded in the same way as
  979. message MapFieldEntry {
  980. key_type key = 1;
  981. value_type value = 2;
  982. }
  983. repeated MapFieldEntry map_field = N;
  984. */
  985. v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  986. if v.Len() == 0 {
  987. return nil
  988. }
  989. keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
  990. enc := func() error {
  991. if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
  992. return err
  993. }
  994. if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil {
  995. return err
  996. }
  997. return nil
  998. }
  999. // Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
  1000. for _, key := range v.MapKeys() {
  1001. val := v.MapIndex(key)
  1002. // The only illegal map entry values are nil message pointers.
  1003. if val.Kind() == reflect.Ptr && val.IsNil() {
  1004. return errors.New("proto: map has nil element")
  1005. }
  1006. keycopy.Set(key)
  1007. valcopy.Set(val)
  1008. o.buf = append(o.buf, p.tagcode...)
  1009. if err := o.enc_len_thing(enc, &state); err != nil {
  1010. return err
  1011. }
  1012. }
  1013. return nil
  1014. }
  1015. func size_new_map(p *Properties, base structPointer) int {
  1016. v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
  1017. keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
  1018. n := 0
  1019. for _, key := range v.MapKeys() {
  1020. val := v.MapIndex(key)
  1021. keycopy.Set(key)
  1022. valcopy.Set(val)
  1023. // Tag codes for key and val are the responsibility of the sub-sizer.
  1024. keysize := p.mkeyprop.size(p.mkeyprop, keybase)
  1025. valsize := p.mvalprop.size(p.mvalprop, valbase)
  1026. entry := keysize + valsize
  1027. // Add on tag code and length of map entry itself.
  1028. n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
  1029. }
  1030. return n
  1031. }
  1032. // mapEncodeScratch returns a new reflect.Value matching the map's value type,
  1033. // and a structPointer suitable for passing to an encoder or sizer.
  1034. func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
  1035. // Prepare addressable doubly-indirect placeholders for the key and value types.
  1036. // This is needed because the element-type encoders expect **T, but the map iteration produces T.
  1037. keycopy = reflect.New(mapType.Key()).Elem() // addressable K
  1038. keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
  1039. keyptr.Set(keycopy.Addr()) //
  1040. keybase = toStructPointer(keyptr.Addr()) // **K
  1041. // Value types are more varied and require special handling.
  1042. switch mapType.Elem().Kind() {
  1043. case reflect.Slice:
  1044. // []byte
  1045. var dummy []byte
  1046. valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
  1047. valbase = toStructPointer(valcopy.Addr())
  1048. case reflect.Ptr:
  1049. // message; the generated field type is map[K]*Msg (so V is *Msg),
  1050. // so we only need one level of indirection.
  1051. valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
  1052. valbase = toStructPointer(valcopy.Addr())
  1053. default:
  1054. // everything else
  1055. valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
  1056. valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
  1057. valptr.Set(valcopy.Addr()) //
  1058. valbase = toStructPointer(valptr.Addr()) // **V
  1059. }
  1060. return
  1061. }
  1062. // Encode a struct.
  1063. func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
  1064. var state errorState
  1065. // Encode fields in tag order so that decoders may use optimizations
  1066. // that depend on the ordering.
  1067. // https://developers.google.com/protocol-buffers/docs/encoding#order
  1068. for _, i := range prop.order {
  1069. p := prop.Prop[i]
  1070. if p.enc != nil {
  1071. err := p.enc(o, p, base)
  1072. if err != nil {
  1073. if err == ErrNil {
  1074. if p.Required && state.err == nil {
  1075. state.err = &RequiredNotSetError{p.Name}
  1076. }
  1077. } else if err == errRepeatedHasNil {
  1078. // Give more context to nil values in repeated fields.
  1079. return errors.New("repeated field " + p.OrigName + " has nil element")
  1080. } else if !state.shouldContinue(err, p) {
  1081. return err
  1082. }
  1083. }
  1084. }
  1085. }
  1086. // Do oneof fields.
  1087. if prop.oneofMarshaler != nil {
  1088. m := structPointer_Interface(base, prop.stype).(Message)
  1089. if err := prop.oneofMarshaler(m, o); err != nil {
  1090. return err
  1091. }
  1092. }
  1093. // Add unrecognized fields at the end.
  1094. if prop.unrecField.IsValid() {
  1095. v := *structPointer_Bytes(base, prop.unrecField)
  1096. if len(v) > 0 {
  1097. o.buf = append(o.buf, v...)
  1098. }
  1099. }
  1100. return state.err
  1101. }
  1102. func size_struct(prop *StructProperties, base structPointer) (n int) {
  1103. for _, i := range prop.order {
  1104. p := prop.Prop[i]
  1105. if p.size != nil {
  1106. n += p.size(p, base)
  1107. }
  1108. }
  1109. // Add unrecognized fields at the end.
  1110. if prop.unrecField.IsValid() {
  1111. v := *structPointer_Bytes(base, prop.unrecField)
  1112. n += len(v)
  1113. }
  1114. // Factor in any oneof fields.
  1115. if prop.oneofSizer != nil {
  1116. m := structPointer_Interface(base, prop.stype).(Message)
  1117. n += prop.oneofSizer(m)
  1118. }
  1119. return
  1120. }
  1121. var zeroes [20]byte // longer than any conceivable sizeVarint
  1122. // Encode a struct, preceded by its encoded length (as a varint).
  1123. func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
  1124. return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
  1125. }
  1126. // Encode something, preceded by its encoded length (as a varint).
  1127. func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
  1128. iLen := len(o.buf)
  1129. o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
  1130. iMsg := len(o.buf)
  1131. err := enc()
  1132. if err != nil && !state.shouldContinue(err, nil) {
  1133. return err
  1134. }
  1135. lMsg := len(o.buf) - iMsg
  1136. lLen := sizeVarint(uint64(lMsg))
  1137. switch x := lLen - (iMsg - iLen); {
  1138. case x > 0: // actual length is x bytes larger than the space we reserved
  1139. // Move msg x bytes right.
  1140. o.buf = append(o.buf, zeroes[:x]...)
  1141. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  1142. case x < 0: // actual length is x bytes smaller than the space we reserved
  1143. // Move msg x bytes left.
  1144. copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
  1145. o.buf = o.buf[:len(o.buf)+x] // x is negative
  1146. }
  1147. // Encode the length in the reserved space.
  1148. o.buf = o.buf[:iLen]
  1149. o.EncodeVarint(uint64(lMsg))
  1150. o.buf = o.buf[:len(o.buf)+lMsg]
  1151. return state.err
  1152. }
  1153. // errorState maintains the first error that occurs and updates that error
  1154. // with additional context.
  1155. type errorState struct {
  1156. err error
  1157. }
  1158. // shouldContinue reports whether encoding should continue upon encountering the
  1159. // given error. If the error is RequiredNotSetError, shouldContinue returns true
  1160. // and, if this is the first appearance of that error, remembers it for future
  1161. // reporting.
  1162. //
  1163. // If prop is not nil, it may update any error with additional context about the
  1164. // field with the error.
  1165. func (s *errorState) shouldContinue(err error, prop *Properties) bool {
  1166. // Ignore unset required fields.
  1167. reqNotSet, ok := err.(*RequiredNotSetError)
  1168. if !ok {
  1169. return false
  1170. }
  1171. if s.err == nil {
  1172. if prop != nil {
  1173. err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
  1174. }
  1175. s.err = err
  1176. }
  1177. return true
  1178. }