Skip to content

Commit

Permalink
Add DecodeFlags header function
Browse files Browse the repository at this point in the history
allows to rewrite flags from .Data bytes into header
  • Loading branch information
wykwit committed Feb 20, 2020
1 parent 9e7cd5e commit eeb9142
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/mslnk/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@ func (h *header) Update() {
}
}

// writing flags from .Data bytes into header
func (h *header) DecodeFlags() {
var byteIndex, bitIndex int8
byteIndex = -1
bitIndex = 8
for _, k := range LinkFlags {
if bitIndex >= 8 {
byteIndex += 1
bitIndex = 0
}
h.LinkFlags[k] = bool(h.Data.LinkFlags[byteIndex]&(1<<bitIndex) > 0)
bitIndex += 1
}
byteIndex = -1
bitIndex = 8
for _, k := range FileAttributesFlags {
if bitIndex >= 8 {
byteIndex += 1
bitIndex = 0
}
h.FileAttributes[k] = bool(h.Data.FileAttributes[byteIndex]&(1<<bitIndex) > 0)
bitIndex += 1
}
}

func (h *header) Bytes() []byte {
var buffer bytes.Buffer
binary.Write(&buffer, binary.LittleEndian, h.Data)
Expand Down
35 changes: 35 additions & 0 deletions pkg/mslnk/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,38 @@ func TestHeader(t *testing.T) {
t.Errorf("Initialized header\n%v\ndoesn't match template\n%v\n", h.Bytes(), expected)
}
}

func TestHeader_DecodeFlags(t *testing.T) {
println("Testing Header DecodeFlags...")
h := mslnk.Header()
h.Data.LinkFlags = [4]byte{155, 0, 8, 0}
h.Data.FileAttributes = [4]byte{32, 0, 0, 0}
h.DecodeFlags()
// list of expected flags should be in order
expected := []string{"HasLinkTargetIDList", "HasLinkInfo", "HasRelativePath", "HasWorkingDir", "IsUnicode", "EnableTargetMetadata", "FILE_ATTRIBUTE_ARCHIVE", ""}
var i int = 0
for _, k := range mslnk.LinkFlags {
if h.LinkFlags[k] {
if expected[i] == k {
i += 1
} else {
t.Errorf("Flag %s set when it shouldn't be. Expecting %s first.\n", k, expected[i])
}
} else if expected[i] == k {
t.Errorf("Flag %s not set when it should be.", k)
i += 1
}
}
for _, k := range mslnk.FileAttributesFlags {
if h.FileAttributes[k] {
if expected[i] == k {
i += 1
} else {
t.Errorf("Flag %s set when it shouldn't be. Expecting %s first.\n", k, expected[i])
}
} else if expected[i] == k {
t.Errorf("Flag %s not set when it should be.", k)
i += 1
}
}
}

0 comments on commit eeb9142

Please sign in to comment.