main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. )
  32. func init() {
  33. flag.Usage = func() {
  34. fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "[-noascii] [-hex <data>] [filename]")
  35. flag.PrintDefaults()
  36. fmt.Fprintln(os.Stderr, `
  37. Dumps RLP data from the given file in readable form.
  38. If the filename is omitted, data is read from stdin.`)
  39. }
  40. }
  41. func main() {
  42. flag.Parse()
  43. var r io.Reader
  44. switch {
  45. case *hexMode != "":
  46. data, err := hex.DecodeString(*hexMode)
  47. if err != nil {
  48. die(err)
  49. }
  50. r = bytes.NewReader(data)
  51. case flag.NArg() == 0:
  52. r = os.Stdin
  53. case flag.NArg() == 1:
  54. fd, err := os.Open(flag.Arg(0))
  55. if err != nil {
  56. die(err)
  57. }
  58. defer fd.Close()
  59. r = fd
  60. default:
  61. fmt.Fprintln(os.Stderr, "Error: too many arguments")
  62. flag.Usage()
  63. os.Exit(2)
  64. }
  65. s := rlp.NewStream(r, 0)
  66. for {
  67. if err := dump(s, 0); err != nil {
  68. if err != io.EOF {
  69. die(err)
  70. }
  71. break
  72. }
  73. fmt.Println()
  74. }
  75. }
  76. func dump(s *rlp.Stream, depth int) error {
  77. kind, size, err := s.Kind()
  78. if err != nil {
  79. return err
  80. }
  81. switch kind {
  82. case rlp.Byte, rlp.String:
  83. str, err := s.Bytes()
  84. if err != nil {
  85. return err
  86. }
  87. if len(str) == 0 || !*noASCII && isASCII(str) {
  88. fmt.Printf("%s%q", ws(depth), str)
  89. } else {
  90. fmt.Printf("%s%x", ws(depth), str)
  91. }
  92. case rlp.List:
  93. s.List()
  94. defer s.ListEnd()
  95. if size == 0 {
  96. fmt.Print(ws(depth) + "[]")
  97. } else {
  98. fmt.Println(ws(depth) + "[")
  99. for i := 0; ; i++ {
  100. if i > 0 {
  101. fmt.Print(",\n")
  102. }
  103. if err := dump(s, depth+1); err == rlp.EOL {
  104. break
  105. } else if err != nil {
  106. return err
  107. }
  108. }
  109. fmt.Print(ws(depth) + "]")
  110. }
  111. }
  112. return nil
  113. }
  114. func isASCII(b []byte) bool {
  115. for _, c := range b {
  116. if c < 32 || c > 126 {
  117. return false
  118. }
  119. }
  120. return true
  121. }
  122. func ws(n int) string {
  123. return strings.Repeat(" ", n)
  124. }
  125. func die(args ...interface{}) {
  126. fmt.Fprintln(os.Stderr, args...)
  127. os.Exit(1)
  128. }