| 1 |
package zipextra |
| 2 |
|
| 3 |
import ( |
| 4 |
"time" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
const ExtraFieldNTFS uint16 = 0x000a |
| 9 |
|
| 10 |
|
| 11 |
type NTFS struct { |
| 12 |
Attributes []NTFSAttribute |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
type NTFSAttribute interface { |
| 18 |
Tag() uint16 |
| 19 |
Data() []byte |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
type NTFSRawAttribute struct { |
| 24 |
RawTag uint16 |
| 25 |
RawData []byte |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
func (a NTFSRawAttribute) Tag() uint16 { |
| 30 |
return a.RawTag |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func (a NTFSRawAttribute) Data() []byte { |
| 35 |
return a.RawData |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
type NTFSTimeAttribute struct { |
| 41 |
MTime time.Time |
| 42 |
ATime time.Time |
| 43 |
CTime time.Time |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func (a NTFSTimeAttribute) Tag() uint16 { |
| 48 |
return 1 |
| 49 |
} |
| 50 |
|
| 51 |
|
| 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 |
|
| 64 |
func NewNTFS(attributes ...NTFSAttribute) NTFS { |
| 65 |
return NTFS{ |
| 66 |
Attributes: attributes, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (field NTFS) Encode() []byte { |
| 72 |
buf := NewBuffer([]byte{}) |
| 73 |
defer buf.WriteHeader(ExtraFieldNTFS)() |
| 74 |
|
| 75 |
buf.Write32(0) |
| 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 |
|
| 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() |
| 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 |
} |