Merge pull request #3 from arkadiyt/atetelman--updates

Handle 10-character long proto filenames
This commit is contained in:
Arkadiy Tetelman authored and GitHub committed 2023-01-01 09:58:28 -08:00
commit 25f5d1df8f
3 files changed
+23 -14

No files matched your search

+1 -1
View File
@@ -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 ./...
+3 -2
View File
@@ -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
+19 -11
View File
@@ -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 "<file>.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:]