add blocksCount to LoadCommandStream

This commit is contained in:
Andrea Franz 2019-03-06 10:44:42 +01:00
parent 53689712e6
commit 10c8e16065
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
1 changed files with 13 additions and 4 deletions

View File

@ -4,6 +4,7 @@ import (
"archive/zip" "archive/zip"
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"math"
"os" "os"
"strings" "strings"
@ -15,12 +16,15 @@ var internalFiles = []string{
"Method", "StaticField", "Export", "ConstantPool", "RefLocation", "Method", "StaticField", "Export", "ConstantPool", "RefLocation",
} }
const blockSize = 247 // 255 - 8 bytes for MAC
// LoadCommandStream implement a struct that generates multiple Load commands used to load files to smartcards. // LoadCommandStream implement a struct that generates multiple Load commands used to load files to smartcards.
type LoadCommandStream struct { type LoadCommandStream struct {
data *bytes.Reader data *bytes.Reader
currentIndex uint8 currentIndex uint8
currentData []byte currentData []byte
p1 uint8 p1 uint8
blocksCount int
} }
// NewLoadCommandStream returns a new LoadCommandStream to load the specified file. // NewLoadCommandStream returns a new LoadCommandStream to load the specified file.
@ -36,19 +40,24 @@ func NewLoadCommandStream(file *os.File) (*LoadCommandStream, error) {
} }
return &LoadCommandStream{ return &LoadCommandStream{
data: bytes.NewReader(data), data: bytes.NewReader(data),
p1: P1LoadMoreBlocks, p1: P1LoadMoreBlocks,
blocksCount: int(math.Ceil(float64(len(data)) / float64(blockSize))),
}, nil }, nil
} }
// BlocksCount returns the total number of blocks based on data length and blockSize
func (lcs *LoadCommandStream) BlocksCount() int {
return lcs.blocksCount
}
// Next returns initialize the data for the next Load command. // Next returns initialize the data for the next Load command.
// TODO:@pilu update blockSize when using encrypted data // TODO:@gravityblast update blockSize when using encrypted data
func (lcs *LoadCommandStream) Next() bool { func (lcs *LoadCommandStream) Next() bool {
if lcs.data.Len() == 0 { if lcs.data.Len() == 0 {
return false return false
} }
blockSize := 247 // 255 - 8 bytes for MAC
buf := make([]byte, blockSize) buf := make([]byte, blockSize)
n, err := lcs.data.Read(buf) n, err := lcs.data.Read(buf)
if err != nil { if err != nil {