Debian Codegraph @

x000a.go

raw symbols SCIP
1 package zipextra
2
3 import (
4 "time"
5 )
6
7 // NTFS Extra Field identifier
8 const ExtraFieldNTFS uint16 = 0x000a
9
10 // NTFS is the NTFS Extra Field structure for holding NTFS attributes.
11 type NTFS struct {
12 Attributes []NTFSAttribute
13 }
14
15 // NTFSAttribute is an NTFS attribute interface. Each attribute consists of a
16 // tag and data. The tag is an identifier for the type of data.
17 type NTFSAttribute interface {
18 Tag() uint16
19 Data() []byte
20 }
21
22 // NTFSRawAttribute represents an unrecognized NTFS attribute.
23 type NTFSRawAttribute struct {
24 RawTag uint16
25 RawData []byte
26 }
27
28 // Tag returns the unrecognized NTFS attribute's tag.
29 func (a NTFSRawAttribute) Tag() uint16 {
30 return a.RawTag
31 }
32
33 // Tag returns the unrecognized NTFS attribute's data.
34 func (a NTFSRawAttribute) Data() []byte {
35 return a.RawData
36 }
37
38 // NTFSTimeAttribute is an NTFS attribute for storing modified, access and
39 // creation file times.
40 type NTFSTimeAttribute struct {
41 MTime time.Time
42 ATime time.Time
43 CTime time.Time
44 }
45
46 // Tag returns the NTFS Time attribute's tag. It is always 1.
47 func (a NTFSTimeAttribute) Tag() uint16 {
48 return 1
49 }
50
51 // Data returns the NTFS Time attribute's encoded data.
52 func (a NTFSTimeAttribute) Data() []byte {
53 buf := NewBuffer(make([]byte, 0, 8*3))
54 for _, t := range []time.Time{a.MTime, a.ATime, a.CTime} {
55 l, h := timeToFiletime(t)
56 buf.Write32(l)
57 buf.Write32(h)
58 }
59
60 return buf.Bytes()
61 }
62
63 // NewNTFS returns a new NTFS extra field structure.
64 func NewNTFS(attributes ...NTFSAttribute) NTFS {
65 return NTFS{
66 Attributes: attributes,
67 }
68 }
69
70 // Encode encodes the NTFS extra field.
71 func (field NTFS) Encode() []byte {
72 buf := NewBuffer([]byte{})
73 defer buf.WriteHeader(ExtraFieldNTFS)()
74
75 buf.Write32(0) // reserved
76
77 for _, attribute := range field.Attributes {
78 buf.Write16(attribute.Tag())
79 buf.Write16(uint16(len(attribute.Data())))
80 buf.WriteBytes(attribute.Data())
81 }
82
83 return buf.Bytes()
84 }
85
86 // NTFS returns the decoded NTFS extra field.
87 func (ef ExtraField) NTFS() (field NTFS, err error) {
88 buf := NewBuffer(ef)
89 if buf.Available() < 4 {
90 return field, ErrInvalidExtraFieldFormat
91 }
92
93 buf.Read32() // reserved
94
95 for buf.Available() >= 2 {
96 tag := buf.Read16()
97 size := buf.Read16()
98
99 if buf.Available() < int(size) {
100 return field, ErrInvalidExtraFieldFormat
101 }
102
103 switch tag {
104 case 1:
105 if size < 24 {
106 return field, ErrInvalidExtraFieldFormat
107 }
108
109 field.Attributes = append(field.Attributes, NTFSTimeAttribute{
110 MTime: filetimeToTime(buf.Read32(), buf.Read32()),
111 ATime: filetimeToTime(buf.Read32(), buf.Read32()),
112 CTime: filetimeToTime(buf.Read32(), buf.Read32()),
113 })
114
115 default:
116 field.Attributes = append(field.Attributes, NTFSRawAttribute{
117 RawTag: tag,
118 RawData: buf.ReadBytes(int(size)),
119 })
120 }
121 }
122 return
123 }