Skip to content

Commit 95feb4a

Browse files
author
Alex Levinson
committed
windows fixes: rand bug, output formatting, file globbing
1 parent 86a0caf commit 95feb4a

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

cmd/gscript/main.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strings"
1818
"time"
1919

20+
"github.com/fatih/color"
2021
"github.com/gen0cide/gscript"
2122
"github.com/gen0cide/gscript/compiler"
2223
"github.com/gen0cide/gscript/debugger"
@@ -44,6 +45,8 @@ func main() {
4445
cli.AppHelpTemplate = fmt.Sprintf("%s\n\n%s", logging.AsciiLogo(), cli.AppHelpTemplate)
4546
cli.CommandHelpTemplate = fmt.Sprintf("%s\n\n%s", logging.AsciiLogo(), cli.CommandHelpTemplate)
4647
app := cli.NewApp()
48+
app.Writer = color.Output
49+
app.ErrWriter = color.Output
4750
app.Name = "gscript"
4851
app.Usage = "Command Line SDK for the Genesis Scripting Engine (GSE)"
4952
app.Version = gscript.Version
@@ -135,12 +138,6 @@ func main() {
135138
Usage: "Run a Genesis script (Careful, don't infect yourself!).",
136139
Action: RunScript,
137140
},
138-
{
139-
Name: "trace",
140-
Aliases: []string{"a"},
141-
Usage: "Show command line options",
142-
Action: TraceScript,
143-
},
144141
}
145142

146143
sort.Sort(cli.FlagsByName(app.Flags))
@@ -209,7 +206,17 @@ func CompileScript(c *cli.Context) error {
209206
if c.NArg() == 0 {
210207
logger.Fatalf("You did not specify a genesis script!")
211208
}
212-
scriptFiles := c.Args()
209+
scriptFiles := []string{}
210+
for _, a := range c.Args() {
211+
f, err := filepath.Glob(a)
212+
if err != nil {
213+
logger.Fatalf("Bad file glob: %s", err.Error())
214+
}
215+
for _, n := range f {
216+
scriptFiles = append(scriptFiles, n)
217+
}
218+
}
219+
213220
if !outputSource && outputFile == "-" {
214221
outputFile = filepath.Join(os.TempDir(), fmt.Sprintf("%d_genesis.bin", time.Now().Unix()))
215222
}

compiler/compiler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type StringDef struct {
4545

4646
// Compiler creates a skeleton structure to produce a compiled binary
4747
type Compiler struct {
48+
sync.RWMutex
4849
OS string `json:"os"`
4950
Arch string `json:"arch"`
5051
OutputFile string `json:"output"`

compiler/embedder.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package compiler
33
import (
44
"bytes"
55
"compress/gzip"
6+
"crypto/rand"
67
"fmt"
78
"io/ioutil"
8-
"math/rand"
9+
"math/big"
910
"path/filepath"
1011
"strings"
11-
"time"
1212
)
1313

1414
type EmbeddedFile struct {
@@ -74,11 +74,14 @@ func CompressedToBytes(b []byte) []byte {
7474
}
7575

7676
func RandUpperAlphaString(strlen int) string {
77-
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
7877
const chars = "abcdefghijklmnopqrstuvwxyz"
7978
result := make([]byte, strlen)
8079
for i := range result {
81-
result[i] = chars[r.Intn(len(chars))]
80+
val, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
81+
if err != nil {
82+
panic(err)
83+
}
84+
result[i] = chars[val.Int64()]
8285
}
8386
return strings.ToUpper(string(result))
8487
}

compiler/obfuscator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,14 @@ func (c *Compiler) HairTangler(key rune, source string) string {
258258
varDef = append(varDef, ch^key)
259259
key ^= ch
260260
}
261-
261+
c.Lock()
262262
c.StringDefs = append(c.StringDefs, &StringDef{
263263
ID: varName,
264264
Value: source,
265265
Key: key,
266266
Data: varDef,
267267
})
268+
c.Unlock()
268269
return cipher
269270
}
270271

engine/core.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
package engine
22

33
import (
4-
"math/rand"
4+
"crypto/rand"
5+
"math/big"
56
"strings"
6-
"time"
77
"unicode"
88
)
99

1010
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
1111

12-
func init() {
13-
rand.Seed(time.Now().UnixNano())
14-
}
15-
1612
func XorBytes(a []byte, b []byte) []byte {
1713
n := len(a)
1814
if len(b) < n {
@@ -51,23 +47,31 @@ func ObfuscateString(Data string) string {
5147
}
5248

5349
func RandString(strlen int) string {
54-
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
5550
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
5651
result := make([]byte, strlen)
5752
for i := range result {
58-
result[i] = chars[r.Intn(len(chars))]
53+
val, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
54+
if err != nil {
55+
panic(err)
56+
}
57+
result[i] = chars[val.Int64()]
5958
}
6059
return string(result)
6160
}
6261

6362
func RandomInt(min, max int) int {
64-
return rand.Intn(max-min) + min
63+
r, _ := rand.Int(rand.Reader, big.NewInt(int64(max-min)))
64+
return int(r.Int64()) + min
6565
}
6666

6767
func RandStringRunes(n int) string {
6868
b := make([]rune, n)
6969
for i := range b {
70-
b[i] = letterRunes[rand.Intn(len(letterRunes))]
70+
val, err := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes))))
71+
if err != nil {
72+
panic(err)
73+
}
74+
b[i] = letterRunes[val.Int64()]
7175
}
7276
return string(b)
7377
}

gscript.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package gscript
22

33
// Version defines the version of gscript
4-
const Version = "v0.0.16"
4+
const Version = "v0.0.17"

0 commit comments

Comments
 (0)