Debian Codegraph @

x6375.go

raw symbols SCIP
1 package zipextra
2
3 import "hash/crc32"
4
5 // ExtraFieldUCom is the Info-ZIP's Unicode Comment Extra Field identifier.
6 const ExtraFieldUCom uint16 = 0x6375
7
8 // InfoZIPUnicodeComment is the Unicode Comment Extra Field structure for
9 // holding NTFS file comments.
10 type InfoZIPUnicodeComment struct {
11 Version uint8
12 Crc32 uint32
13 Comment string
14 }
15
16 // NewInfoZIPUnicodeComment returns a new InfoZIPUnicodeComment extra field
17 // structure.
18 func NewInfoZIPUnicodeComment(comment string) InfoZIPUnicodeComment {
19 return InfoZIPUnicodeComment{
20 Version: 1,
21 Crc32: crc32.ChecksumIEEE([]byte(comment)),
22 Comment: comment,
23 }
24 }
25
26 // Encode encodes the InfoZIPUnicodeComment extra field.
27 func (field InfoZIPUnicodeComment) Encode() []byte {
28 buf := NewBuffer([]byte{})
29 defer buf.WriteHeader(ExtraFieldUCom)()
30
31 buf.Write8(field.Version)
32 buf.Write32(field.Crc32)
33 buf.WriteBytes([]byte(field.Comment))
34
35 return buf.Bytes()
36 }
37
38 // InfoZIPUnicodeComment returns the decoded InfoZIPUnicodeComment extra field.
39 func (ef ExtraField) InfoZIPUnicodeComment() (field InfoZIPUnicodeComment, err error) {
40 buf := NewBuffer(ef)
41 if buf.Available() < 5 {
42 return field, ErrInvalidExtraFieldFormat
43 }
44
45 field.Version = buf.Read8()
46 field.Crc32 = buf.Read32()
47 field.Comment = string(buf.ReadBytes(buf.Available()))
48
49 return
50 }