File: x5455.go

package info (click to toggle)
golang-github-saracen-zipextra 0.0~git20250129.f1aa42d-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,205 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package zipextra

import "time"

// ExtraFieldExtTime is the Extended Timestamp Extra Field
// identifier.
const ExtraFieldExtTime uint16 = 0x5455

// ExtendedTime is the Extended Timestamp Extra Field structure for holding a
// file entry's last modification time.
type ExtendedTimestamp struct {
	ModTime time.Time
}

// NewExtendedTimestamp returns a new ExtendedTime extra field structure.
func NewExtendedTimestamp(modTime time.Time) ExtendedTimestamp {
	return ExtendedTimestamp{
		ModTime: modTime,
	}
}

// Encode encodes the ExtendedTimestamp extra field.
func (field ExtendedTimestamp) Encode() []byte {
	buf := NewBuffer([]byte{})
	defer buf.WriteHeader(ExtraFieldExtTime)()

	if field.ModTime.IsZero() {
		return buf.Bytes()
	}

	buf.Write8(1)
	buf.Write32(uint32(field.ModTime.Unix()))

	return buf.Bytes()
}

// ExtendedTime returns the decoded ExtendedTime extra field.
func (ef ExtraField) ExtendedTimestamp() (field ExtendedTimestamp, err error) {
	buf := NewBuffer(ef)
	if buf.Available() == 0 {
		return field, nil
	}

	if buf.Read8()&1 == 1 {
		if buf.Available() < 4 {
			return field, ErrInvalidExtraFieldFormat
		}

		field.ModTime = time.Unix(int64(buf.Read32()), 0)
	}

	return
}