Skip to content

Commit

Permalink
Drop (Un)MarshalBinary() methods
Browse files Browse the repository at this point in the history
In hindsight they're not really needed. Tweak the examples accordingly.

Also improve the README.
  • Loading branch information
bodgit committed Jul 27, 2020
1 parent 2fbc947 commit 3350279
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@

windows
=======

A collection of types native to Windows but are useful on non-Windows platforms.

The `FILETIME`-comparable type is the sole export which is a 1:1 copy of the one from `golang.org/x/sys/windows`. However, that package isn't available for all platforms and this particular type gets used in other protocols and file types such as NTLMv2 and 7-Zip.
25 changes: 2 additions & 23 deletions filetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
// are useful on non-Windows platforms.
package windows

import (
"bytes"
"encoding/binary"
)

// Taken from golang.org/x/sys/windows

const offset int64 = 116444736000000000
Expand All @@ -15,23 +10,13 @@ const offset int64 = 116444736000000000
// as the number of 100-nanosecond intervals that have elapsed since
// 00:00:00 UTC, January 1, 1601. This code is taken from the
// golang.org/x/sys/windows package where it's not available for non-Windows
// platforms however various file formats and protocols pass variations of
// this type about so it's useful to have it available for interoperability
// purposes. It implements the encoding.BinaryMarshaler and
// encoding.BinaryUnmarshaler interfaces.
// platforms however various file formats and protocols pass this structure
// about so it's useful to have it available for interoperability purposes.
type Filetime struct {
LowDateTime uint32
HighDateTime uint32
}

// MarshalBinary encodes Filetime ft into a binary form and returns the
// result.
func (ft *Filetime) MarshalBinary() ([]byte, error) {
w := new(bytes.Buffer)
_ = binary.Write(w, binary.LittleEndian, ft)
return w.Bytes(), nil
}

// Nanoseconds returns Filetime ft in nanoseconds
// since Epoch (00:00:00 UTC, January 1, 1970).
func (ft *Filetime) Nanoseconds() int64 {
Expand All @@ -44,12 +29,6 @@ func (ft *Filetime) Nanoseconds() int64 {
return nsec
}

// UnmarshalBinary decodes Filetime ft from binary form.
func (ft *Filetime) UnmarshalBinary(b []byte) error {
r := bytes.NewBuffer(b)
return binary.Read(r, binary.LittleEndian, ft)
}

// NsecToFiletime converts nanoseconds to the equivalent Filetime type.
func NsecToFiletime(nsec int64) (ft Filetime) {
// convert into 100-nanosecond
Expand Down
28 changes: 11 additions & 17 deletions filetime_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
package windows

import (
"encoding/hex"
"fmt"
"time"
)

func ExampleFiletime_MarshalBinary() {
ft := NsecToFiletime(1595265310000000000) // time.Date(2020, 7, 20, 17, 15, 10, 0, time.UTC).UnixNano()
func ExampleNsecToFiletime() {
ft := NsecToFiletime(time.Date(2020, 7, 20, 17, 15, 10, 0, time.UTC).UnixNano())

b, err := ft.MarshalBinary()
if err != nil {
panic(err)
}

fmt.Print(hex.Dump(b))
// Output: 00000000 00 a3 7e 52 b9 5e d6 01 |..~R.^..|
fmt.Println(ft)
// Output: {1384030976 30826169}
}

func ExampleFiletime_UnmarshalBinary() {
ft := new(Filetime)

if err := ft.UnmarshalBinary([]byte{0x00, 0xa3, 0x7e, 0x52, 0xb9, 0x5e, 0xd6, 0x01}); err != nil {
panic(err)
func ExampleFiletime_Nanoseconds() {
ft := Filetime{
1384030976,
30826169,
}

fmt.Println(ft.Nanoseconds())
// Output: 1595265310000000000
fmt.Println(time.Unix(0, ft.Nanoseconds()).UTC())
// Output: 2020-07-20 17:15:10 +0000 UTC
}

0 comments on commit 3350279

Please sign in to comment.