Skip to content

Commit

Permalink
Expose errors variables (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilpa authored and glaslos committed Jul 2, 2018
1 parent 40ecfd9 commit c17959d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
18 changes: 13 additions & 5 deletions score.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package ssdeep

import (
"errors"
"math"
"strings"
"strconv"
"errors"
"strings"
)

var (
// ErrEmptyHash is returned when no hash string is provided for scoring.
ErrEmptyHash = errors.New("empty string")

// ErrInvalidFormat is returned when a hash string is malformed.
ErrInvalidFormat = errors.New("invalid ssdeep format")
)

// Distance computes the match score between two fuzzy hash signatures.
Expand Down Expand Up @@ -44,19 +52,19 @@ func Distance(hash1, hash2 string) (score int, err error) {

func splitSsdeep(hash string) (blockSize int, hashString1, hashString2 string, err error) {
if hash == "" {
err = errors.New("empty string")
err = ErrEmptyHash
return
}

parts := strings.Split(hash, ":")
if len(parts) != 3 {
err = errors.New("invalid ssdeep format")
err = ErrInvalidFormat
return
}

blockSize, err = strconv.Atoi(parts[0])
if err != nil {
err = errors.New("invalid ssdeep format")
err = ErrInvalidFormat
return
}

Expand Down
12 changes: 10 additions & 2 deletions ssdeep.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import (
"os"
)

var (
// ErrFileTooSmall is returned when a file contains too few bytes.
ErrFileTooSmall = errors.New("did not process files large enough to produce meaningful results")

// ErrBlockSizeTooSmall is returned when a file can't produce a large enough block size.
ErrBlockSizeTooSmall = errors.New("unable to establish a sufficient block size")
)

const (
rollingWindow uint32 = 7
blockMin = 3
Expand Down Expand Up @@ -131,7 +139,7 @@ func (state *ssdeepState) process(r *bufio.Reader) {
// Returns an error when ssdeep could not be computed on the Reader.
func FuzzyReader(f Reader, fileSize int) (out string, err error) {
if fileSize < minFileSize {
err = errors.New("did not process files large enough to produce meaningful results")
err = ErrFileTooSmall
if !Force {
return
}
Expand All @@ -145,7 +153,7 @@ func FuzzyReader(f Reader, fileSize int) (out string, err error) {
r := bufio.NewReader(f)
state.process(r)
if state.blockSize < blockMin {
err = errors.New("unable to establish a sufficient block size")
err = ErrBlockSizeTooSmall
if !Force {
return
}
Expand Down

0 comments on commit c17959d

Please sign in to comment.