-
Notifications
You must be signed in to change notification settings - Fork 84
/
smali2java.go
83 lines (65 loc) · 1.43 KB
/
smali2java.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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"sync"
"github.com/alexeysoshin/smali2java/parser"
)
const smaliExtension = ".smali"
func main() {
pathToSmali := flag.String("path_to_smali", "./test_data/s.smali", "Path to your smali files")
flag.Parse()
parseSmaliFiles(*pathToSmali)
}
func parseSmaliFiles(path string) {
var wg = sync.WaitGroup{}
filepath.Walk(path, func(path string, f os.FileInfo, err error) error {
if err != nil {
log.Printf("Error occurred while walking path %q: %v", path, err)
return err
}
if f.IsDir() {
//log.Printf("Skipping directory: %s\n", path)
} else if filepath.Ext(path) != smaliExtension {
//log.Printf("Not a smali file, skipping: %s\n", path)
} else {
wg.Add(1)
go convertSmali(path, &wg)
}
return nil
})
wg.Wait()
}
var debug = func() bool {
return strings.ToLower(os.Getenv("debug")) == "true"
}()
func convertSmali(path string, wg *sync.WaitGroup) {
if debug {
fmt.Printf("Processing %s\n", path)
}
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
javaFile := parser.JavaFile{}
scanner := bufio.NewScanner(file)
buf := make([]byte, 0, 64*1024)
scanner.Buffer(buf, 1024*1024)
for scanner.Scan() {
javaFile.ParseLine(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
if debug {
javaFile.Print()
}
javaFile.Save(filepath.Dir(path))
wg.Done()
}