From 84b50ef1bf07792b9e5e24b492397018ee63271c Mon Sep 17 00:00:00 2001 From: Arkadiy Tetelman Date: Sun, 1 Jan 2023 08:26:21 -0800 Subject: [PATCH] Handle 10-character long proto filenames --- Makefile | 2 +- pkg/protodump/proto.go | 5 +++-- pkg/protodump/scan.go | 30 +++++++++++++++++++----------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 8724135..28d433f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ build: - go build -o protodump cmd/protodump/main.go + go build -o bin/protodump cmd/protodump/main.go fmt: go fmt ./... diff --git a/pkg/protodump/proto.go b/pkg/protodump/proto.go index 6b51889..3395926 100644 --- a/pkg/protodump/proto.go +++ b/pkg/protodump/proto.go @@ -250,9 +250,10 @@ func (pd *ProtoDefinition) writeFileOptions() { elemPtr := options.FieldByName(option.FieldName) if !elemPtr.IsNil() { elem := elemPtr.Elem() - if elem.Kind() == reflect.String { + kind := elem.Kind() + if kind == reflect.String { pd.writeStringFileOptions(option.OptionName, elem.String()) - } else if elem.Kind() == reflect.Bool { + } else if kind == reflect.Bool { pd.writeBoolFileOptions(option.OptionName, elem.Bool()) } printedOption = true diff --git a/pkg/protodump/scan.go b/pkg/protodump/scan.go index 9e8aba7..42c819d 100644 --- a/pkg/protodump/scan.go +++ b/pkg/protodump/scan.go @@ -8,13 +8,8 @@ import ( "google.golang.org/protobuf/encoding/protowire" ) -func ScanFile(path string) ([][]byte, error) { - data, err := os.ReadFile(path) - if err != nil { - return nil, fmt.Errorf("Couldn't open file: %w", err) - } - return Scan(data), nil -} +const scan = ".proto" +const magicByte = 0xa func consumeBytes(data []byte, position int) int { start := position @@ -29,26 +24,39 @@ func consumeBytes(data []byte, position int) int { } } +func ScanFile(path string) ([][]byte, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("Couldn't open file: %w", err) + } + return Scan(data), nil +} + func Scan(data []byte) [][]byte { results := make([][]byte, 0) for { - index := bytes.Index(data, []byte(".proto")) + index := bytes.Index(data, []byte(scan)) if index == -1 { break } // Look for a 0xa byte: 0 0001 010 // ^ no more bytes for varint - // ^ field 1 (file name) + // ^ field 1 (file name) // ^ type 2 (LEN) - start := bytes.LastIndexByte(data[:index], 0xa) + start := bytes.LastIndexByte(data[:index], magicByte) if start == -1 { - // fmt.Printf("Warn: couldn't find start to proto definition at offset:0x%s\n", strconv.FormatInt(int64(index), 16)) data = data[index+1:] continue } + // If the ".proto" filename is 10 characters long then the 0xa byte we found is actually + // the string length and not the start of the protobuf record, so go back 1 more + if index-start == (magicByte-len(scan)+1) && start > 0 && data[start-1] == magicByte { + start -= 1 + } + length := consumeBytes(data, start) results = append(results, data[start:start+length]) data = data[start+length:]