main.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. // rlpdump is a pretty-printer for RLP data.
  17. package main
  18. import (
  19. "bytes"
  20. "encoding/hex"
  21. "flag"
  22. "fmt"
  23. "io"
  24. "os"
  25. "strings"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. )
  28. var (
  29. hexMode = flag.String("hex", "", "dump given hex data")
  30. noASCII = flag.Bool("noascii", false, "don't print ASCII strings readably")
  31. single = flag.Bool("single", false, "print only the first element, discard the rest")
  32. )
  33. func init() {
  34. flag.Usage = func() {
  35. fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "[-noascii] [-hex <data>] [filename]")
  36. flag.PrintDefaults()
  37. fmt.Fprintln(os.Stderr, `
  38. Dumps RLP data from the given file in readable form.
  39. If the filename is omitted, data is read from stdin.`)
  40. }
  41. }
  42. func main() {
  43. flag.Parse()
  44. var r io.Reader
  45. switch {
  46. case *hexMode != "":
  47. data, err := hex.DecodeString(strings.TrimPrefix(*hexMode, "0x"))
  48. if err != nil {
  49. die(err)
  50. }
  51. r = bytes.NewReader(data)
  52. case flag.NArg() == 0:
  53. r = os.Stdin
  54. case flag.NArg() == 1:
  55. fd, err := os.Open(flag.Arg(0))
  56. if err != nil {
  57. die(err)
  58. }
  59. defer fd.Close()
  60. r = fd
  61. default:
  62. fmt.Fprintln(os.Stderr, "Error: too many arguments")
  63. flag.Usage()
  64. os.Exit(2)
  65. }
  66. s := rlp.NewStream(r, 0)
  67. for {
  68. if err := dump(s, 0); err != nil {
  69. if err != io.EOF {
  70. die(err)
  71. }
  72. break
  73. }
  74. fmt.Println()
  75. if *single {
  76. break
  77. }
  78. }
  79. }
  80. func dump(s *rlp.Stream, depth int) error {
  81. kind, size, err := s.Kind()
  82. if err != nil {
  83. return err
  84. }
  85. switch kind {
  86. case rlp.Byte, rlp.String:
  87. str, err := s.Bytes()
  88. if err != nil {
  89. return err
  90. }
  91. if len(str) == 0 || !*noASCII && isASCII(str) {
  92. fmt.Printf("%s%q", ws(depth), str)
  93. } else {
  94. fmt.Printf("%s%x", ws(depth), str)
  95. }
  96. case rlp.List:
  97. s.List()
  98. defer s.ListEnd()
  99. if size == 0 {
  100. fmt.Print(ws(depth) + "[]")
  101. } else {
  102. fmt.Println(ws(depth) + "[")
  103. for i := 0; ; i++ {
  104. if i > 0 {
  105. fmt.Print(",\n")
  106. }
  107. if err := dump(s, depth+1); err == rlp.EOL {
  108. break
  109. } else if err != nil {
  110. return err
  111. }
  112. }
  113. fmt.Print(ws(depth) + "]")
  114. }
  115. }
  116. return nil
  117. }
  118. func isASCII(b []byte) bool {
  119. for _, c := range b {
  120. if c < 32 || c > 126 {
  121. return false
  122. }
  123. }
  124. return true
  125. }
  126. func ws(n int) string {
  127. return strings.Repeat(" ", n)
  128. }
  129. func die(args ...interface{}) {
  130. fmt.Fprintln(os.Stderr, args...)
  131. os.Exit(1)
  132. }