/
dump.go
242 lines (188 loc) · 4.98 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package protoss
import (
"fmt"
"io"
"os"
"path/filepath"
"slices"
"strings"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/descriptorpb"
)
type DumpOptions struct {
// The directory to output decompiled definitions to
Output string
// The Go package path to prepend to Protocol Buffers packages
// when generating "go_package" attribute
GoPrefix string
// If true, output additional .json files to assist in ensuring accurate .proto decompilation
JSON bool
// If true, don't output descriptors that are in the google/ path
IgnoreGoogle bool
}
type filedumper struct {
options *DumpOptions
output_file *os.File
file_descriptor *descriptorpb.FileDescriptorProto
syntax string
import_scope string
package_scope string
line_indentation int
}
func DumpBinary(binary_path string, options *DumpOptions) (err error) {
var pb_reader *Reader
filedumper := new(filedumper)
filedumper.options = options
pb_reader, err = OpenBinary(binary_path)
if err != nil {
return
}
defer pb_reader.Close()
for {
filedumper.file_descriptor, err = pb_reader.ReadDescriptor()
if err != nil {
if err == io.EOF {
return nil
}
return
}
err = filedumper.dump()
if err != nil {
return
}
}
return
}
func (fd *filedumper) printf(fomt string, args ...any) {
fmt.Fprintf(fd.output_file, fomt, args...)
}
func (fd *filedumper) indent() {
for i := 0; i < fd.line_indentation; i++ {
fmt.Fprintf(fd.output_file, " ")
}
}
// returns the directory containing a proto definition (forward slash only)
func directory_path(fd_name string) string {
elements := strings.Split(fd_name, "/")
if len(elements) == 1 {
return ""
}
joined := strings.Join(elements[:len(elements)-1], "/")
if !strings.HasSuffix(joined, "/") {
joined += "/"
}
return joined
}
func (fd *filedumper) dump() (err error) {
fd_name := fd.file_descriptor.GetName()
if fd.options.IgnoreGoogle {
if strings.HasPrefix(fd_name, "google/") {
return nil
}
}
if fd_name == "" {
err = fmt.Errorf("protoss: dump(): file descriptor has no name")
return
}
if !(strings.HasSuffix(fd_name, ".proto")) {
err = fmt.Errorf("protoss: dump(): incorrect file descriptor name: %s ", fd_name)
return
}
fd.import_scope = directory_path(fd_name)
fd.package_scope = fd.file_descriptor.GetPackage()
output_path := filepath.Join(fd.options.Output, fd_name)
output_path_directory := filepath.Dir(output_path)
fmt.Println("mkdir", output_path_directory)
if err = os.MkdirAll(output_path_directory, 0700); err != nil {
return
}
if fd.options.JSON {
js := protojson.Format(fd.file_descriptor)
if err = os.WriteFile(output_path+".json", []byte(js), 0700); err != nil {
return
}
}
fd.output_file, err = os.OpenFile(output_path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0700)
if err != nil {
return
}
fd.syntax = "proto2"
if fd.file_descriptor.Syntax != nil {
fd.syntax = fd.file_descriptor.GetSyntax()
}
fd.printf("// DO NOT EDIT: this file was auto-generated by Gophercraft/protoss\n\n")
fd.printf("syntax = \"%s\";\n\n", fd.syntax)
if fd.file_descriptor.Package != nil {
fd.printf("package %s;\n\n", fd.file_descriptor.GetPackage())
go_prefix := fd.options.GoPrefix
if !strings.HasSuffix(go_prefix, "/") {
go_prefix += "/"
}
fd.printf("option go_package = \"%s%s\";\n\n", go_prefix, strings.ReplaceAll(fd.file_descriptor.GetPackage(), ".", "/"))
}
for i, dep := range fd.file_descriptor.Dependency {
index := int32(i)
prefix := ""
for _, idx := range fd.file_descriptor.WeakDependency {
if index == idx {
prefix = " weak"
break
}
}
for _, idx := range fd.file_descriptor.PublicDependency {
if index == idx {
prefix = " public"
break
}
}
fmt.Println(dep, "=>", fd.import_scope)
// if strings.HasPrefix(dep, fd.import_scope) {
// dep = dep[len(fd.import_scope):]
// }
fd.printf("import%s \"%s\";\n", prefix, dep)
}
if len(fd.file_descriptor.Dependency) > 0 {
fd.printf("\n")
}
fd.printf("\n// Enums\n\n")
for _, enum_type := range fd.file_descriptor.EnumType {
if err = fd.dump_enum_definition(enum_type); err != nil {
return
}
}
fd.printf("\n// Messages\n\n")
for i, message := range fd.file_descriptor.MessageType {
if i > 0 {
fd.printf("\n")
}
if err = fd.dump_message_definition(message); err != nil {
return
}
}
fd.printf("\n// Services\n\n")
for i, service := range fd.file_descriptor.Service {
if i > 0 {
fd.printf("\n")
}
if err = fd.dump_service_definition(service); err != nil {
return
}
}
fd.printf("// Extensions \n\n")
// look for extendees
var extendees []string
for _, extension := range fd.file_descriptor.Extension {
if !slices.Contains(extendees, extension.GetExtendee()) {
extendees = append(extendees, extension.GetExtendee())
}
}
for i, extendee := range extendees {
if i > 0 {
fd.printf("\n")
}
if err = fd.dump_extension(extendee); err != nil {
return
}
}
return fd.output_file.Close()
}