pack.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package protopack enables manual encoding and decoding of protobuf wire data.
  5. //
  6. // This package is intended for use in debugging and/or creation of test data.
  7. // Proper usage of this package requires knowledge of the wire format.
  8. //
  9. // See https://protobuf.dev/programming-guides/encoding.
  10. package protopack
  11. import (
  12. "fmt"
  13. "io"
  14. "math"
  15. "path"
  16. "reflect"
  17. "strconv"
  18. "strings"
  19. "unicode"
  20. "unicode/utf8"
  21. "google.golang.org/protobuf/encoding/protowire"
  22. "google.golang.org/protobuf/reflect/protoreflect"
  23. )
  24. // Number is the field number; aliased from the [protowire] package for convenience.
  25. type Number = protowire.Number
  26. // Number type constants; copied from the [protowire] package for convenience.
  27. const (
  28. MinValidNumber Number = protowire.MinValidNumber
  29. FirstReservedNumber Number = protowire.FirstReservedNumber
  30. LastReservedNumber Number = protowire.LastReservedNumber
  31. MaxValidNumber Number = protowire.MaxValidNumber
  32. )
  33. // Type is the wire type; aliased from the [protowire] package for convenience.
  34. type Type = protowire.Type
  35. // Wire type constants; copied from the [protowire] package for convenience.
  36. const (
  37. VarintType Type = protowire.VarintType
  38. Fixed32Type Type = protowire.Fixed32Type
  39. Fixed64Type Type = protowire.Fixed64Type
  40. BytesType Type = protowire.BytesType
  41. StartGroupType Type = protowire.StartGroupType
  42. EndGroupType Type = protowire.EndGroupType
  43. )
  44. type (
  45. // Token is any other type (e.g., [Message], [Tag], [Varint], [Float32], etc).
  46. Token token
  47. // Message is an ordered sequence of [Token] values, where certain tokens may
  48. // contain other tokens. It is functionally a concrete syntax tree that
  49. // losslessly represents any arbitrary wire data (including invalid input).
  50. Message []Token
  51. // Tag is a tuple of the field number and the wire type.
  52. Tag struct {
  53. Number Number
  54. Type Type
  55. }
  56. // Bool is a boolean.
  57. Bool bool
  58. // Varint is a signed varint using 64-bit two's complement encoding.
  59. Varint int64
  60. // Svarint is a signed varint using zig-zag encoding.
  61. Svarint int64
  62. // Uvarint is a unsigned varint.
  63. Uvarint uint64
  64. // Int32 is a signed 32-bit fixed-width integer.
  65. Int32 int32
  66. // Uint32 is an unsigned 32-bit fixed-width integer.
  67. Uint32 uint32
  68. // Float32 is a 32-bit fixed-width floating point number.
  69. Float32 float32
  70. // Int64 is a signed 64-bit fixed-width integer.
  71. Int64 int64
  72. // Uint64 is an unsigned 64-bit fixed-width integer.
  73. Uint64 uint64
  74. // Float64 is a 64-bit fixed-width floating point number.
  75. Float64 float64
  76. // String is a length-prefixed string.
  77. String string
  78. // Bytes is a length-prefixed bytes.
  79. Bytes []byte
  80. // LengthPrefix is a length-prefixed message.
  81. LengthPrefix Message
  82. // Denormalized is a denormalized varint value, where a varint is encoded
  83. // using more bytes than is strictly necessary. The number of extra bytes
  84. // alone is sufficient to losslessly represent the denormalized varint.
  85. //
  86. // The value may be one of [Tag], [Bool], [Varint], [Svarint], or [Uvarint],
  87. // where the varint representation of each token is denormalized.
  88. //
  89. // Alternatively, the value may be one of [String], [Bytes], or [LengthPrefix],
  90. // where the varint representation of the length-prefix is denormalized.
  91. Denormalized struct {
  92. Count uint // number of extra bytes
  93. Value Token
  94. }
  95. // Raw are bytes directly appended to output.
  96. Raw []byte
  97. )
  98. type token interface {
  99. isToken()
  100. }
  101. func (Message) isToken() {}
  102. func (Tag) isToken() {}
  103. func (Bool) isToken() {}
  104. func (Varint) isToken() {}
  105. func (Svarint) isToken() {}
  106. func (Uvarint) isToken() {}
  107. func (Int32) isToken() {}
  108. func (Uint32) isToken() {}
  109. func (Float32) isToken() {}
  110. func (Int64) isToken() {}
  111. func (Uint64) isToken() {}
  112. func (Float64) isToken() {}
  113. func (String) isToken() {}
  114. func (Bytes) isToken() {}
  115. func (LengthPrefix) isToken() {}
  116. func (Denormalized) isToken() {}
  117. func (Raw) isToken() {}
  118. // Size reports the size in bytes of the marshaled message.
  119. func (m Message) Size() int {
  120. var n int
  121. for _, v := range m {
  122. switch v := v.(type) {
  123. case Message:
  124. n += v.Size()
  125. case Tag:
  126. n += protowire.SizeTag(v.Number)
  127. case Bool:
  128. n += protowire.SizeVarint(protowire.EncodeBool(false))
  129. case Varint:
  130. n += protowire.SizeVarint(uint64(v))
  131. case Svarint:
  132. n += protowire.SizeVarint(protowire.EncodeZigZag(int64(v)))
  133. case Uvarint:
  134. n += protowire.SizeVarint(uint64(v))
  135. case Int32, Uint32, Float32:
  136. n += protowire.SizeFixed32()
  137. case Int64, Uint64, Float64:
  138. n += protowire.SizeFixed64()
  139. case String:
  140. n += protowire.SizeBytes(len(v))
  141. case Bytes:
  142. n += protowire.SizeBytes(len(v))
  143. case LengthPrefix:
  144. n += protowire.SizeBytes(Message(v).Size())
  145. case Denormalized:
  146. n += int(v.Count) + Message{v.Value}.Size()
  147. case Raw:
  148. n += len(v)
  149. default:
  150. panic(fmt.Sprintf("unknown type: %T", v))
  151. }
  152. }
  153. return n
  154. }
  155. // Marshal encodes a syntax tree into the protobuf wire format.
  156. //
  157. // Example message definition:
  158. //
  159. // message MyMessage {
  160. // string field1 = 1;
  161. // int64 field2 = 2;
  162. // repeated float32 field3 = 3;
  163. // }
  164. //
  165. // Example encoded message:
  166. //
  167. // b := Message{
  168. // Tag{1, BytesType}, String("Hello, world!"),
  169. // Tag{2, VarintType}, Varint(-10),
  170. // Tag{3, BytesType}, LengthPrefix{
  171. // Float32(1.1), Float32(2.2), Float32(3.3),
  172. // },
  173. // }.Marshal()
  174. //
  175. // Resulting wire data:
  176. //
  177. // 0x0000 0a 0d 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 10 |..Hello, world!.|
  178. // 0x0010 f6 ff ff ff ff ff ff ff ff 01 1a 0c cd cc 8c 3f |...............?|
  179. // 0x0020 cd cc 0c 40 33 33 53 40 |...@33S@|
  180. func (m Message) Marshal() []byte {
  181. var out []byte
  182. for _, v := range m {
  183. switch v := v.(type) {
  184. case Message:
  185. out = append(out, v.Marshal()...)
  186. case Tag:
  187. out = protowire.AppendTag(out, v.Number, v.Type)
  188. case Bool:
  189. out = protowire.AppendVarint(out, protowire.EncodeBool(bool(v)))
  190. case Varint:
  191. out = protowire.AppendVarint(out, uint64(v))
  192. case Svarint:
  193. out = protowire.AppendVarint(out, protowire.EncodeZigZag(int64(v)))
  194. case Uvarint:
  195. out = protowire.AppendVarint(out, uint64(v))
  196. case Int32:
  197. out = protowire.AppendFixed32(out, uint32(v))
  198. case Uint32:
  199. out = protowire.AppendFixed32(out, uint32(v))
  200. case Float32:
  201. out = protowire.AppendFixed32(out, math.Float32bits(float32(v)))
  202. case Int64:
  203. out = protowire.AppendFixed64(out, uint64(v))
  204. case Uint64:
  205. out = protowire.AppendFixed64(out, uint64(v))
  206. case Float64:
  207. out = protowire.AppendFixed64(out, math.Float64bits(float64(v)))
  208. case String:
  209. out = protowire.AppendBytes(out, []byte(v))
  210. case Bytes:
  211. out = protowire.AppendBytes(out, []byte(v))
  212. case LengthPrefix:
  213. out = protowire.AppendBytes(out, Message(v).Marshal())
  214. case Denormalized:
  215. b := Message{v.Value}.Marshal()
  216. _, n := protowire.ConsumeVarint(b)
  217. out = append(out, b[:n]...)
  218. for i := uint(0); i < v.Count; i++ {
  219. out[len(out)-1] |= 0x80 // set continuation bit on previous
  220. out = append(out, 0)
  221. }
  222. out = append(out, b[n:]...)
  223. case Raw:
  224. return append(out, v...)
  225. default:
  226. panic(fmt.Sprintf("unknown type: %T", v))
  227. }
  228. }
  229. return out
  230. }
  231. // Unmarshal parses the input protobuf wire data as a syntax tree.
  232. // Any parsing error results in the remainder of the input being
  233. // concatenated to the message as a [Raw] type.
  234. //
  235. // Each tag (a tuple of the field number and wire type) encountered is
  236. // inserted into the syntax tree as a [Tag].
  237. //
  238. // The contents of each wire type is mapped to the following Go types:
  239. //
  240. // - [VarintType] ⇒ [Uvarint]
  241. // - [Fixed32Type] ⇒ [Uint32]
  242. // - [Fixed64Type] ⇒ [Uint64]
  243. // - [BytesType] ⇒ [Bytes]
  244. // - [StartGroupType] and [StartGroupType] ⇒ [Message]
  245. //
  246. // Since the wire format is not self-describing, this function cannot parse
  247. // sub-messages and will leave them as the [Bytes] type. Further manual parsing
  248. // can be performed as such:
  249. //
  250. // var m, m1, m2 Message
  251. // m.Unmarshal(b)
  252. // m1.Unmarshal(m[3].(Bytes))
  253. // m[3] = LengthPrefix(m1)
  254. // m2.Unmarshal(m[3].(LengthPrefix)[1].(Bytes))
  255. // m[3].(LengthPrefix)[1] = LengthPrefix(m2)
  256. //
  257. // Unmarshal is useful for debugging the protobuf wire format.
  258. func (m *Message) Unmarshal(in []byte) {
  259. m.unmarshal(in, nil, false)
  260. }
  261. // UnmarshalDescriptor parses the input protobuf wire data as a syntax tree
  262. // using the provided message descriptor for more accurate parsing of fields.
  263. // It operates like [Message.Unmarshal], but may use a wider range of Go types to
  264. // represent the wire data.
  265. //
  266. // The contents of each wire type is mapped to one of the following Go types:
  267. //
  268. // - [VarintType] ⇒ [Bool], [Varint], [Svarint], [Uvarint]
  269. // - [Fixed32Type] ⇒ [Int32], [Uint32], [Float32]
  270. // - [Fixed64Type] ⇒ [Uint32], [Uint64], [Float64]
  271. // - [BytesType] ⇒ [String], [Bytes], [LengthPrefix]
  272. // - [StartGroupType] and [StartGroupType] ⇒ [Message]
  273. //
  274. // If the field is unknown, it uses the same mapping as [Message.Unmarshal].
  275. // Known sub-messages are parsed as a Message and packed repeated fields are
  276. // parsed as a [LengthPrefix].
  277. func (m *Message) UnmarshalDescriptor(in []byte, desc protoreflect.MessageDescriptor) {
  278. m.unmarshal(in, desc, false)
  279. }
  280. // UnmarshalAbductive is like [Message.UnmarshalDescriptor], but infers abductively
  281. // whether any unknown bytes values is a message based on whether it is
  282. // a syntactically well-formed message.
  283. //
  284. // Note that the protobuf wire format is not fully self-describing,
  285. // so abductive inference may attempt to expand a bytes value as a message
  286. // that is not actually a message. It is a best-effort guess.
  287. func (m *Message) UnmarshalAbductive(in []byte, desc protoreflect.MessageDescriptor) {
  288. m.unmarshal(in, desc, true)
  289. }
  290. func (m *Message) unmarshal(in []byte, desc protoreflect.MessageDescriptor, inferMessage bool) {
  291. p := parser{in: in, out: *m}
  292. p.parseMessage(desc, false, inferMessage)
  293. *m = p.out
  294. }
  295. type parser struct {
  296. in []byte
  297. out []Token
  298. invalid bool
  299. }
  300. func (p *parser) parseMessage(msgDesc protoreflect.MessageDescriptor, group, inferMessage bool) {
  301. for len(p.in) > 0 {
  302. v, n := protowire.ConsumeVarint(p.in)
  303. num, typ := protowire.DecodeTag(v)
  304. if n < 0 || num <= 0 || v > math.MaxUint32 {
  305. p.out, p.in = append(p.out, Raw(p.in)), nil
  306. p.invalid = true
  307. return
  308. }
  309. if typ == EndGroupType && group {
  310. return // if inside a group, then stop
  311. }
  312. p.out, p.in = append(p.out, Tag{num, typ}), p.in[n:]
  313. if m := n - protowire.SizeVarint(v); m > 0 {
  314. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  315. }
  316. // If descriptor is available, use it for more accurate parsing.
  317. var isPacked bool
  318. var kind protoreflect.Kind
  319. var subDesc protoreflect.MessageDescriptor
  320. if msgDesc != nil && !msgDesc.IsPlaceholder() {
  321. if fieldDesc := msgDesc.Fields().ByNumber(num); fieldDesc != nil {
  322. isPacked = fieldDesc.IsPacked()
  323. kind = fieldDesc.Kind()
  324. switch kind {
  325. case protoreflect.MessageKind, protoreflect.GroupKind:
  326. subDesc = fieldDesc.Message()
  327. if subDesc == nil || subDesc.IsPlaceholder() {
  328. kind = 0
  329. }
  330. }
  331. }
  332. }
  333. switch typ {
  334. case VarintType:
  335. p.parseVarint(kind)
  336. case Fixed32Type:
  337. p.parseFixed32(kind)
  338. case Fixed64Type:
  339. p.parseFixed64(kind)
  340. case BytesType:
  341. p.parseBytes(isPacked, kind, subDesc, inferMessage)
  342. case StartGroupType:
  343. p.parseGroup(num, subDesc, inferMessage)
  344. case EndGroupType:
  345. // Handled by p.parseGroup.
  346. default:
  347. p.out, p.in = append(p.out, Raw(p.in)), nil
  348. p.invalid = true
  349. }
  350. }
  351. }
  352. func (p *parser) parseVarint(kind protoreflect.Kind) {
  353. v, n := protowire.ConsumeVarint(p.in)
  354. if n < 0 {
  355. p.out, p.in = append(p.out, Raw(p.in)), nil
  356. p.invalid = true
  357. return
  358. }
  359. switch kind {
  360. case protoreflect.BoolKind:
  361. switch v {
  362. case 0:
  363. p.out, p.in = append(p.out, Bool(false)), p.in[n:]
  364. case 1:
  365. p.out, p.in = append(p.out, Bool(true)), p.in[n:]
  366. default:
  367. p.out, p.in = append(p.out, Uvarint(v)), p.in[n:]
  368. }
  369. case protoreflect.Int32Kind, protoreflect.Int64Kind:
  370. p.out, p.in = append(p.out, Varint(v)), p.in[n:]
  371. case protoreflect.Sint32Kind, protoreflect.Sint64Kind:
  372. p.out, p.in = append(p.out, Svarint(protowire.DecodeZigZag(v))), p.in[n:]
  373. default:
  374. p.out, p.in = append(p.out, Uvarint(v)), p.in[n:]
  375. }
  376. if m := n - protowire.SizeVarint(v); m > 0 {
  377. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  378. }
  379. }
  380. func (p *parser) parseFixed32(kind protoreflect.Kind) {
  381. v, n := protowire.ConsumeFixed32(p.in)
  382. if n < 0 {
  383. p.out, p.in = append(p.out, Raw(p.in)), nil
  384. p.invalid = true
  385. return
  386. }
  387. switch kind {
  388. case protoreflect.FloatKind:
  389. p.out, p.in = append(p.out, Float32(math.Float32frombits(v))), p.in[n:]
  390. case protoreflect.Sfixed32Kind:
  391. p.out, p.in = append(p.out, Int32(v)), p.in[n:]
  392. default:
  393. p.out, p.in = append(p.out, Uint32(v)), p.in[n:]
  394. }
  395. }
  396. func (p *parser) parseFixed64(kind protoreflect.Kind) {
  397. v, n := protowire.ConsumeFixed64(p.in)
  398. if n < 0 {
  399. p.out, p.in = append(p.out, Raw(p.in)), nil
  400. p.invalid = true
  401. return
  402. }
  403. switch kind {
  404. case protoreflect.DoubleKind:
  405. p.out, p.in = append(p.out, Float64(math.Float64frombits(v))), p.in[n:]
  406. case protoreflect.Sfixed64Kind:
  407. p.out, p.in = append(p.out, Int64(v)), p.in[n:]
  408. default:
  409. p.out, p.in = append(p.out, Uint64(v)), p.in[n:]
  410. }
  411. }
  412. func (p *parser) parseBytes(isPacked bool, kind protoreflect.Kind, desc protoreflect.MessageDescriptor, inferMessage bool) {
  413. v, n := protowire.ConsumeVarint(p.in)
  414. if n < 0 {
  415. p.out, p.in = append(p.out, Raw(p.in)), nil
  416. p.invalid = true
  417. return
  418. }
  419. p.out, p.in = append(p.out, Uvarint(v)), p.in[n:]
  420. if m := n - protowire.SizeVarint(v); m > 0 {
  421. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  422. }
  423. if v > uint64(len(p.in)) {
  424. p.out, p.in = append(p.out, Raw(p.in)), nil
  425. p.invalid = true
  426. return
  427. }
  428. p.out = p.out[:len(p.out)-1] // subsequent tokens contain prefix-length
  429. if isPacked {
  430. p.parsePacked(int(v), kind)
  431. } else {
  432. switch kind {
  433. case protoreflect.MessageKind:
  434. p2 := parser{in: p.in[:v]}
  435. p2.parseMessage(desc, false, inferMessage)
  436. p.out, p.in = append(p.out, LengthPrefix(p2.out)), p.in[v:]
  437. case protoreflect.StringKind:
  438. p.out, p.in = append(p.out, String(p.in[:v])), p.in[v:]
  439. case protoreflect.BytesKind:
  440. p.out, p.in = append(p.out, Bytes(p.in[:v])), p.in[v:]
  441. default:
  442. if inferMessage {
  443. // Check whether this is a syntactically valid message.
  444. p2 := parser{in: p.in[:v]}
  445. p2.parseMessage(nil, false, inferMessage)
  446. if !p2.invalid {
  447. p.out, p.in = append(p.out, LengthPrefix(p2.out)), p.in[v:]
  448. break
  449. }
  450. }
  451. p.out, p.in = append(p.out, Bytes(p.in[:v])), p.in[v:]
  452. }
  453. }
  454. if m := n - protowire.SizeVarint(v); m > 0 {
  455. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  456. }
  457. }
  458. func (p *parser) parsePacked(n int, kind protoreflect.Kind) {
  459. p2 := parser{in: p.in[:n]}
  460. for len(p2.in) > 0 {
  461. switch kind {
  462. case protoreflect.BoolKind, protoreflect.EnumKind,
  463. protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Uint32Kind,
  464. protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind:
  465. p2.parseVarint(kind)
  466. case protoreflect.Fixed32Kind, protoreflect.Sfixed32Kind, protoreflect.FloatKind:
  467. p2.parseFixed32(kind)
  468. case protoreflect.Fixed64Kind, protoreflect.Sfixed64Kind, protoreflect.DoubleKind:
  469. p2.parseFixed64(kind)
  470. default:
  471. panic(fmt.Sprintf("invalid packed kind: %v", kind))
  472. }
  473. }
  474. p.out, p.in = append(p.out, LengthPrefix(p2.out)), p.in[n:]
  475. }
  476. func (p *parser) parseGroup(startNum protowire.Number, desc protoreflect.MessageDescriptor, inferMessage bool) {
  477. p2 := parser{in: p.in}
  478. p2.parseMessage(desc, true, inferMessage)
  479. if len(p2.out) > 0 {
  480. p.out = append(p.out, Message(p2.out))
  481. }
  482. p.in = p2.in
  483. // Append the trailing end group.
  484. v, n := protowire.ConsumeVarint(p.in)
  485. if endNum, typ := protowire.DecodeTag(v); typ == EndGroupType {
  486. if startNum != endNum {
  487. p.invalid = true
  488. }
  489. p.out, p.in = append(p.out, Tag{endNum, typ}), p.in[n:]
  490. if m := n - protowire.SizeVarint(v); m > 0 {
  491. p.out[len(p.out)-1] = Denormalized{uint(m), p.out[len(p.out)-1]}
  492. }
  493. }
  494. }
  495. // Format implements a custom formatter to visualize the syntax tree.
  496. // Using "%#v" formats the Message in Go source code.
  497. func (m Message) Format(s fmt.State, r rune) {
  498. switch r {
  499. case 'x':
  500. io.WriteString(s, fmt.Sprintf("%x", m.Marshal()))
  501. case 'X':
  502. io.WriteString(s, fmt.Sprintf("%X", m.Marshal()))
  503. case 'v':
  504. switch {
  505. case s.Flag('#'):
  506. io.WriteString(s, m.format(true, true))
  507. case s.Flag('+'):
  508. io.WriteString(s, m.format(false, true))
  509. default:
  510. io.WriteString(s, m.format(false, false))
  511. }
  512. default:
  513. panic("invalid verb: " + string(r))
  514. }
  515. }
  516. // format formats the message.
  517. // If source is enabled, this emits valid Go source.
  518. // If multi is enabled, the output may span multiple lines.
  519. func (m Message) format(source, multi bool) string {
  520. var ss []string
  521. var prefix, nextPrefix string
  522. for _, v := range m {
  523. // Ensure certain tokens have preceding or succeeding newlines.
  524. prefix, nextPrefix = nextPrefix, " "
  525. if multi {
  526. switch v := v.(type) {
  527. case Tag: // only has preceding newline
  528. prefix = "\n"
  529. case Denormalized: // only has preceding newline
  530. if _, ok := v.Value.(Tag); ok {
  531. prefix = "\n"
  532. }
  533. case Message, Raw: // has preceding and succeeding newlines
  534. prefix, nextPrefix = "\n", "\n"
  535. }
  536. }
  537. s := formatToken(v, source, multi)
  538. ss = append(ss, prefix+s+",")
  539. }
  540. var s string
  541. if len(ss) > 0 {
  542. s = strings.TrimSpace(strings.Join(ss, ""))
  543. if multi {
  544. s = "\n\t" + strings.Join(strings.Split(s, "\n"), "\n\t") + "\n"
  545. } else {
  546. s = strings.TrimSuffix(s, ",")
  547. }
  548. }
  549. s = fmt.Sprintf("%T{%s}", m, s)
  550. if !source {
  551. s = trimPackage(s)
  552. }
  553. return s
  554. }
  555. // formatToken formats a single token.
  556. func formatToken(t Token, source, multi bool) (s string) {
  557. switch v := t.(type) {
  558. case Message:
  559. s = v.format(source, multi)
  560. case LengthPrefix:
  561. s = formatPacked(v, source, multi)
  562. if s == "" {
  563. ms := Message(v).format(source, multi)
  564. s = fmt.Sprintf("%T(%s)", v, ms)
  565. }
  566. case Tag:
  567. s = fmt.Sprintf("%T{%d, %s}", v, v.Number, formatType(v.Type, source))
  568. case Bool, Varint, Svarint, Uvarint, Int32, Uint32, Float32, Int64, Uint64, Float64:
  569. if source {
  570. // Print floats in a way that preserves exact precision.
  571. if f, _ := v.(Float32); math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) {
  572. switch {
  573. case f > 0:
  574. s = fmt.Sprintf("%T(math.Inf(+1))", v)
  575. case f < 0:
  576. s = fmt.Sprintf("%T(math.Inf(-1))", v)
  577. case math.Float32bits(float32(math.NaN())) == math.Float32bits(float32(f)):
  578. s = fmt.Sprintf("%T(math.NaN())", v)
  579. default:
  580. s = fmt.Sprintf("%T(math.Float32frombits(0x%08x))", v, math.Float32bits(float32(f)))
  581. }
  582. break
  583. }
  584. if f, _ := v.(Float64); math.IsNaN(float64(f)) || math.IsInf(float64(f), 0) {
  585. switch {
  586. case f > 0:
  587. s = fmt.Sprintf("%T(math.Inf(+1))", v)
  588. case f < 0:
  589. s = fmt.Sprintf("%T(math.Inf(-1))", v)
  590. case math.Float64bits(float64(math.NaN())) == math.Float64bits(float64(f)):
  591. s = fmt.Sprintf("%T(math.NaN())", v)
  592. default:
  593. s = fmt.Sprintf("%T(math.Float64frombits(0x%016x))", v, math.Float64bits(float64(f)))
  594. }
  595. break
  596. }
  597. }
  598. s = fmt.Sprintf("%T(%v)", v, v)
  599. case String, Bytes, Raw:
  600. s = fmt.Sprintf("%s", v)
  601. s = fmt.Sprintf("%T(%s)", v, formatString(s))
  602. case Denormalized:
  603. s = fmt.Sprintf("%T{+%d, %v}", v, v.Count, formatToken(v.Value, source, multi))
  604. default:
  605. panic(fmt.Sprintf("unknown type: %T", v))
  606. }
  607. if !source {
  608. s = trimPackage(s)
  609. }
  610. return s
  611. }
  612. // formatPacked returns a non-empty string if LengthPrefix looks like a packed
  613. // repeated field of primitives.
  614. func formatPacked(v LengthPrefix, source, multi bool) string {
  615. var ss []string
  616. for _, v := range v {
  617. switch v.(type) {
  618. case Bool, Varint, Svarint, Uvarint, Int32, Uint32, Float32, Int64, Uint64, Float64, Denormalized, Raw:
  619. if v, ok := v.(Denormalized); ok {
  620. switch v.Value.(type) {
  621. case Bool, Varint, Svarint, Uvarint:
  622. default:
  623. return ""
  624. }
  625. }
  626. ss = append(ss, formatToken(v, source, multi))
  627. default:
  628. return ""
  629. }
  630. }
  631. s := fmt.Sprintf("%T{%s}", v, strings.Join(ss, ", "))
  632. if !source {
  633. s = trimPackage(s)
  634. }
  635. return s
  636. }
  637. // formatType returns the name for Type.
  638. func formatType(t Type, source bool) (s string) {
  639. switch t {
  640. case VarintType:
  641. s = pkg + ".VarintType"
  642. case Fixed32Type:
  643. s = pkg + ".Fixed32Type"
  644. case Fixed64Type:
  645. s = pkg + ".Fixed64Type"
  646. case BytesType:
  647. s = pkg + ".BytesType"
  648. case StartGroupType:
  649. s = pkg + ".StartGroupType"
  650. case EndGroupType:
  651. s = pkg + ".EndGroupType"
  652. default:
  653. s = fmt.Sprintf("Type(%d)", t)
  654. }
  655. if !source {
  656. s = strings.TrimSuffix(trimPackage(s), "Type")
  657. }
  658. return s
  659. }
  660. // formatString returns a quoted string for s.
  661. func formatString(s string) string {
  662. // Use quoted string if it the same length as a raw string literal.
  663. // Otherwise, attempt to use the raw string form.
  664. qs := strconv.Quote(s)
  665. if len(qs) == 1+len(s)+1 {
  666. return qs
  667. }
  668. // Disallow newlines to ensure output is a single line.
  669. // Disallow non-printable runes for readability purposes.
  670. rawInvalid := func(r rune) bool {
  671. return r == '`' || r == '\n' || r == utf8.RuneError || !unicode.IsPrint(r)
  672. }
  673. if strings.IndexFunc(s, rawInvalid) < 0 {
  674. return "`" + s + "`"
  675. }
  676. return qs
  677. }
  678. var pkg = path.Base(reflect.TypeOf(Tag{}).PkgPath())
  679. func trimPackage(s string) string {
  680. return strings.TrimPrefix(strings.TrimPrefix(s, pkg), ".")
  681. }