...

Source file src/google.golang.org/protobuf/internal/cmd/pbdump/pbdump.go

Documentation: google.golang.org/protobuf/internal/cmd/pbdump

     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  
     5  // pbdump is a tool for decoding the wire format for protocol buffer messages.
     6  package main
     7  
     8  import (
     9  	"bytes"
    10  	"flag"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"log"
    14  	"os"
    15  	"path/filepath"
    16  	"sort"
    17  	"strconv"
    18  	"strings"
    19  
    20  	"google.golang.org/protobuf/encoding/protowire"
    21  	"google.golang.org/protobuf/internal/errors"
    22  	"google.golang.org/protobuf/proto"
    23  	"google.golang.org/protobuf/reflect/protodesc"
    24  	"google.golang.org/protobuf/reflect/protoreflect"
    25  	"google.golang.org/protobuf/testing/protopack"
    26  
    27  	"google.golang.org/protobuf/types/descriptorpb"
    28  )
    29  
    30  func main() {
    31  	log.SetFlags(0)
    32  	log.SetOutput(os.Stderr)
    33  
    34  	var fs fields
    35  	var flagUsages []string
    36  	flagVar := func(value flag.Value, name, usage string) {
    37  		flagUsages = append(flagUsages, fmt.Sprintf("  -%-16v  %v", name+" "+value.String(), usage))
    38  		flag.Var(value, name, usage)
    39  	}
    40  	flagBool := func(name, usage string) *bool {
    41  		flagUsages = append(flagUsages, fmt.Sprintf("  -%-16v  %v", name, usage))
    42  		return flag.Bool(name, false, usage)
    43  	}
    44  	flagVar(fieldsFlag{&fs, protoreflect.BoolKind}, "bools", "List of bool fields")
    45  	flagVar(fieldsFlag{&fs, protoreflect.Int64Kind}, "ints", "List of int32 or int64 fields")
    46  	flagVar(fieldsFlag{&fs, protoreflect.Sint64Kind}, "sints", "List of sint32 or sint64 fields")
    47  	flagVar(fieldsFlag{&fs, protoreflect.Uint64Kind}, "uints", "List of enum, uint32, or uint64 fields")
    48  	flagVar(fieldsFlag{&fs, protoreflect.Fixed32Kind}, "uint32s", "List of fixed32 fields")
    49  	flagVar(fieldsFlag{&fs, protoreflect.Sfixed32Kind}, "int32s", "List of sfixed32 fields")
    50  	flagVar(fieldsFlag{&fs, protoreflect.FloatKind}, "float32s", "List of float fields")
    51  	flagVar(fieldsFlag{&fs, protoreflect.Fixed64Kind}, "uint64s", "List of fixed64 fields")
    52  	flagVar(fieldsFlag{&fs, protoreflect.Sfixed64Kind}, "int64s", "List of sfixed64 fields")
    53  	flagVar(fieldsFlag{&fs, protoreflect.DoubleKind}, "float64s", "List of double fields")
    54  	flagVar(fieldsFlag{&fs, protoreflect.StringKind}, "strings", "List of string fields")
    55  	flagVar(fieldsFlag{&fs, protoreflect.BytesKind}, "bytes", "List of bytes fields")
    56  	flagVar(fieldsFlag{&fs, protoreflect.MessageKind}, "messages", "List of message fields")
    57  	flagVar(fieldsFlag{&fs, protoreflect.GroupKind}, "groups", "List of group fields")
    58  	printDesc := flagBool("print_descriptor", "Print the message descriptor")
    59  	printSource := flagBool("print_source", "Print the output in valid Go syntax")
    60  	flag.Usage = func() {
    61  		fmt.Printf("Usage: %s [OPTIONS]... [INPUTS]...\n\n%s\n", filepath.Base(os.Args[0]), strings.Join(append([]string{
    62  			"Print structured representations of encoded protocol buffer messages.",
    63  			"Since the protobuf wire format is not fully self-describing, type information",
    64  			"about the proto message can be provided using flags (e.g., -messages).",
    65  			"Each field list is a comma-separated list of field identifiers,",
    66  			"where each field identifier is a dot-separated list of field numbers,",
    67  			"identifying each field relative to the root message.",
    68  			"",
    69  			"For example, \"-messages 1,3,3.1 -float32s 1.2 -bools 3.1.2\" represents:",
    70  			"",
    71  			"    message M {",
    72  			"        optional M1 f1 = 1;           // -messages 1",
    73  			"        message M1 {",
    74  			"            repeated float f2 = 2;    // -float32s 1.2",
    75  			"        }",
    76  			"        optional M3 f3 = 3;           // -messages 3",
    77  			"        message M3 {",
    78  			"            optional M1 f1 = 1;       // -messages 3.1",
    79  			"            message M1 {",
    80  			"                repeated bool f2 = 2; // -bools 3.1.2",
    81  			"            }",
    82  			"        }",
    83  			"    }",
    84  			"",
    85  			"Arbitrarily complex message schemas can be represented using these flags.",
    86  			"Scalar field types are marked as repeated so that pbdump can decode",
    87  			"the packed representations of such field types.",
    88  			"",
    89  			"If no inputs are specified, the wire data is read in from stdin, otherwise",
    90  			"the contents of each specified input file is concatenated and",
    91  			"treated as one large message.",
    92  			"",
    93  			"Options:",
    94  		}, flagUsages...), "\n"))
    95  	}
    96  	flag.Parse()
    97  
    98  	// Create message types.
    99  	var desc protoreflect.MessageDescriptor
   100  	if len(fs) > 0 {
   101  		var err error
   102  		desc, err = fs.Descriptor()
   103  		if err != nil {
   104  			log.Fatalf("Descriptor error: %v", err)
   105  		}
   106  		if *printDesc {
   107  			fmt.Printf("%#v\n", desc)
   108  		}
   109  	}
   110  
   111  	// Read message input.
   112  	var buf []byte
   113  	if flag.NArg() == 0 {
   114  		b, err := ioutil.ReadAll(os.Stdin)
   115  		if err != nil {
   116  			log.Fatalf("ReadAll error: %v", err)
   117  		}
   118  		buf = b
   119  	}
   120  	for _, f := range flag.Args() {
   121  		b, err := ioutil.ReadFile(f)
   122  		if err != nil {
   123  			log.Fatalf("ReadFile error: %v", err)
   124  		}
   125  		buf = append(buf, b...)
   126  	}
   127  
   128  	// Parse and print message structure.
   129  	defer log.Printf("fatal input: %q", buf) // debug printout if panic occurs
   130  	var m protopack.Message
   131  	m.UnmarshalAbductive(buf, desc)
   132  	if *printSource {
   133  		fmt.Printf("%#v\n", m)
   134  	} else {
   135  		fmt.Printf("%+v\n", m)
   136  	}
   137  	if !bytes.Equal(buf, m.Marshal()) || len(buf) != m.Size() {
   138  		log.Fatalf("roundtrip mismatch:\n\tgot:  %d %x\n\twant: %d %x", m.Size(), m, len(buf), buf)
   139  	}
   140  	os.Exit(0) // exit cleanly, avoid debug printout
   141  }
   142  
   143  // fields is a tree of fields, keyed by a field number.
   144  // Fields representing messages or groups have sub-fields.
   145  type fields map[protowire.Number]*field
   146  type field struct {
   147  	kind protoreflect.Kind
   148  	sub  fields // only for MessageKind or GroupKind
   149  }
   150  
   151  // Set parses s as a comma-separated list (see the help above for the format)
   152  // and treats each field identifier as the specified kind.
   153  func (fs *fields) Set(s string, k protoreflect.Kind) error {
   154  	if *fs == nil {
   155  		*fs = make(fields)
   156  	}
   157  	for _, s := range strings.Split(s, ",") {
   158  		if err := fs.set("", strings.TrimSpace(s), k); err != nil {
   159  			return err
   160  		}
   161  	}
   162  	return nil
   163  }
   164  func (fs fields) set(prefix, s string, k protoreflect.Kind) error {
   165  	if s == "" {
   166  		return nil
   167  	}
   168  
   169  	// Parse next field number.
   170  	i := strings.IndexByte(s, '.')
   171  	if i < 0 {
   172  		i = len(s)
   173  	}
   174  	prefix = strings.TrimPrefix(prefix+"."+s[:i], ".")
   175  	n, _ := strconv.ParseInt(s[:i], 10, 32)
   176  	num := protowire.Number(n)
   177  	if num < protowire.MinValidNumber || protowire.MaxValidNumber < num {
   178  		return errors.New("invalid field: %v", prefix)
   179  	}
   180  	s = strings.TrimPrefix(s[i:], ".")
   181  
   182  	// Handle the current field.
   183  	if fs[num] == nil {
   184  		fs[num] = &field{0, make(fields)}
   185  	}
   186  	if len(s) == 0 {
   187  		if fs[num].kind.IsValid() {
   188  			return errors.New("field %v already set as %v type", prefix, fs[num].kind)
   189  		}
   190  		fs[num].kind = k
   191  	}
   192  	if err := fs[num].sub.set(prefix, s, k); err != nil {
   193  		return err
   194  	}
   195  
   196  	// Verify that only messages or groups can have sub-fields.
   197  	k2 := fs[num].kind
   198  	if k2 > 0 && k2 != protoreflect.MessageKind && k2 != protoreflect.GroupKind && len(fs[num].sub) > 0 {
   199  		return errors.New("field %v of %v type cannot have sub-fields", prefix, k2)
   200  	}
   201  	return nil
   202  }
   203  
   204  // Descriptor returns the field tree as a message descriptor.
   205  func (fs fields) Descriptor() (protoreflect.MessageDescriptor, error) {
   206  	fd, err := protodesc.NewFile(&descriptorpb.FileDescriptorProto{
   207  		Name:        proto.String("dump.proto"),
   208  		Syntax:      proto.String("proto2"),
   209  		MessageType: []*descriptorpb.DescriptorProto{fs.messageDescriptor("X")},
   210  	}, nil)
   211  	if err != nil {
   212  		return nil, err
   213  	}
   214  	return fd.Messages().Get(0), nil
   215  }
   216  func (fs fields) messageDescriptor(name protoreflect.FullName) *descriptorpb.DescriptorProto {
   217  	m := &descriptorpb.DescriptorProto{Name: proto.String(string(name.Name()))}
   218  	for _, n := range fs.sortedNums() {
   219  		k := fs[n].kind
   220  		if !k.IsValid() {
   221  			k = protoreflect.MessageKind
   222  		}
   223  		f := &descriptorpb.FieldDescriptorProto{
   224  			Name:   proto.String(fmt.Sprintf("x%d", n)),
   225  			Number: proto.Int32(int32(n)),
   226  			Label:  descriptorpb.FieldDescriptorProto_LABEL_OPTIONAL.Enum(),
   227  			Type:   descriptorpb.FieldDescriptorProto_Type(k).Enum(),
   228  		}
   229  		switch k {
   230  		case protoreflect.BoolKind, protoreflect.EnumKind,
   231  			protoreflect.Int32Kind, protoreflect.Sint32Kind, protoreflect.Uint32Kind,
   232  			protoreflect.Int64Kind, protoreflect.Sint64Kind, protoreflect.Uint64Kind,
   233  			protoreflect.Sfixed32Kind, protoreflect.Fixed32Kind, protoreflect.FloatKind,
   234  			protoreflect.Sfixed64Kind, protoreflect.Fixed64Kind, protoreflect.DoubleKind:
   235  			f.Label = descriptorpb.FieldDescriptorProto_LABEL_REPEATED.Enum()
   236  			f.Options = &descriptorpb.FieldOptions{Packed: proto.Bool(true)}
   237  		case protoreflect.MessageKind, protoreflect.GroupKind:
   238  			s := name.Append(protoreflect.Name(fmt.Sprintf("X%d", n)))
   239  			f.TypeName = proto.String(string("." + s))
   240  			m.NestedType = append(m.NestedType, fs[n].sub.messageDescriptor(s))
   241  		}
   242  		m.Field = append(m.Field, f)
   243  	}
   244  	return m
   245  }
   246  
   247  func (fs fields) sortedNums() (ns []protowire.Number) {
   248  	for n := range fs {
   249  		ns = append(ns, n)
   250  	}
   251  	sort.Slice(ns, func(i, j int) bool { return ns[i] < ns[j] })
   252  	return ns
   253  }
   254  
   255  // fieldsFlag is an implementation of flag.Value that is keyed a specific kind.
   256  type fieldsFlag struct {
   257  	f *fields
   258  	k protoreflect.Kind
   259  }
   260  
   261  func (fs fieldsFlag) String() string     { return "FIELDS" }
   262  func (fs fieldsFlag) Set(s string) error { return fs.f.Set(s, fs.k) }
   263  

View as plain text