main.go 2.9 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. /**
  17. * @authors
  18. * Felix Lange <felix@ethdev.com>
  19. */
  20. // rlpdump is a pretty-printer for RLP data.
  21. package main
  22. import (
  23. "bytes"
  24. "encoding/hex"
  25. "flag"
  26. "fmt"
  27. "io"
  28. "os"
  29. "strings"
  30. "github.com/ethereum/go-ethereum/rlp"
  31. )
  32. var (
  33. hexMode = flag.String("hex", "", "dump given hex data")
  34. noASCII = flag.Bool("noascii", false, "don't print ASCII strings readably")
  35. )
  36. func init() {
  37. flag.Usage = func() {
  38. fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "[-noascii] [-hex <data>] [filename]")
  39. flag.PrintDefaults()
  40. fmt.Fprintln(os.Stderr, `
  41. Dumps RLP data from the given file in readable form.
  42. If the filename is omitted, data is read from stdin.`)
  43. }
  44. }
  45. func main() {
  46. flag.Parse()
  47. var r io.Reader
  48. switch {
  49. case *hexMode != "":
  50. data, err := hex.DecodeString(*hexMode)
  51. if err != nil {
  52. die(err)
  53. }
  54. r = bytes.NewReader(data)
  55. case flag.NArg() == 0:
  56. r = os.Stdin
  57. case flag.NArg() == 1:
  58. fd, err := os.Open(flag.Arg(0))
  59. if err != nil {
  60. die(err)
  61. }
  62. defer fd.Close()
  63. r = fd
  64. default:
  65. fmt.Fprintln(os.Stderr, "Error: too many arguments")
  66. flag.Usage()
  67. os.Exit(2)
  68. }
  69. s := rlp.NewStream(r, 0)
  70. for {
  71. if err := dump(s, 0); err != nil {
  72. if err != io.EOF {
  73. die(err)
  74. }
  75. break
  76. }
  77. fmt.Println()
  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. }