Skip to content

Commit

Permalink
windows fixes: rand bug, output formatting, file globbing
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Levinson committed Mar 6, 2018
1 parent 86a0caf commit 95feb4a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
21 changes: 14 additions & 7 deletions cmd/gscript/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"time"

"github.com/fatih/color"
"github.com/gen0cide/gscript"
"github.com/gen0cide/gscript/compiler"
"github.com/gen0cide/gscript/debugger"
Expand Down Expand Up @@ -44,6 +45,8 @@ func main() {
cli.AppHelpTemplate = fmt.Sprintf("%s\n\n%s", logging.AsciiLogo(), cli.AppHelpTemplate)
cli.CommandHelpTemplate = fmt.Sprintf("%s\n\n%s", logging.AsciiLogo(), cli.CommandHelpTemplate)
app := cli.NewApp()
app.Writer = color.Output
app.ErrWriter = color.Output
app.Name = "gscript"
app.Usage = "Command Line SDK for the Genesis Scripting Engine (GSE)"
app.Version = gscript.Version
Expand Down Expand Up @@ -135,12 +138,6 @@ func main() {
Usage: "Run a Genesis script (Careful, don't infect yourself!).",
Action: RunScript,
},
{
Name: "trace",
Aliases: []string{"a"},
Usage: "Show command line options",
Action: TraceScript,
},
}

sort.Sort(cli.FlagsByName(app.Flags))
Expand Down Expand Up @@ -209,7 +206,17 @@ func CompileScript(c *cli.Context) error {
if c.NArg() == 0 {
logger.Fatalf("You did not specify a genesis script!")
}
scriptFiles := c.Args()
scriptFiles := []string{}
for _, a := range c.Args() {
f, err := filepath.Glob(a)
if err != nil {
logger.Fatalf("Bad file glob: %s", err.Error())
}
for _, n := range f {
scriptFiles = append(scriptFiles, n)
}
}

if !outputSource && outputFile == "-" {
outputFile = filepath.Join(os.TempDir(), fmt.Sprintf("%d_genesis.bin", time.Now().Unix()))
}
Expand Down
1 change: 1 addition & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type StringDef struct {

// Compiler creates a skeleton structure to produce a compiled binary
type Compiler struct {
sync.RWMutex
OS string `json:"os"`
Arch string `json:"arch"`
OutputFile string `json:"output"`
Expand Down
11 changes: 7 additions & 4 deletions compiler/embedder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package compiler
import (
"bytes"
"compress/gzip"
"crypto/rand"
"fmt"
"io/ioutil"
"math/rand"
"math/big"
"path/filepath"
"strings"
"time"
)

type EmbeddedFile struct {
Expand Down Expand Up @@ -74,11 +74,14 @@ func CompressedToBytes(b []byte) []byte {
}

func RandUpperAlphaString(strlen int) string {
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
const chars = "abcdefghijklmnopqrstuvwxyz"
result := make([]byte, strlen)
for i := range result {
result[i] = chars[r.Intn(len(chars))]
val, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
if err != nil {
panic(err)
}
result[i] = chars[val.Int64()]
}
return strings.ToUpper(string(result))
}
3 changes: 2 additions & 1 deletion compiler/obfuscator.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,14 @@ func (c *Compiler) HairTangler(key rune, source string) string {
varDef = append(varDef, ch^key)
key ^= ch
}

c.Lock()
c.StringDefs = append(c.StringDefs, &StringDef{
ID: varName,
Value: source,
Key: key,
Data: varDef,
})
c.Unlock()
return cipher
}

Expand Down
24 changes: 14 additions & 10 deletions engine/core.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package engine

import (
"math/rand"
"crypto/rand"
"math/big"
"strings"
"time"
"unicode"
)

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

func init() {
rand.Seed(time.Now().UnixNano())
}

func XorBytes(a []byte, b []byte) []byte {
n := len(a)
if len(b) < n {
Expand Down Expand Up @@ -51,23 +47,31 @@ func ObfuscateString(Data string) string {
}

func RandString(strlen int) string {
var r = rand.New(rand.NewSource(time.Now().UnixNano()))
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := range result {
result[i] = chars[r.Intn(len(chars))]
val, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
if err != nil {
panic(err)
}
result[i] = chars[val.Int64()]
}
return string(result)
}

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

func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
val, err := rand.Int(rand.Reader, big.NewInt(int64(len(letterRunes))))
if err != nil {
panic(err)
}
b[i] = letterRunes[val.Int64()]
}
return string(b)
}
2 changes: 1 addition & 1 deletion gscript.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gscript

// Version defines the version of gscript
const Version = "v0.0.16"
const Version = "v0.0.17"

0 comments on commit 95feb4a

Please sign in to comment.