File: x6375.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 (50 lines) | stat: -rw-r--r-- 1,306 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
package zipextra

import "hash/crc32"

// ExtraFieldUCom is the Info-ZIP's Unicode Comment Extra Field identifier.
const ExtraFieldUCom uint16 = 0x6375

// InfoZIPUnicodeComment is the Unicode Comment Extra Field structure for
// holding NTFS file comments.
type InfoZIPUnicodeComment struct {
	Version uint8
	Crc32   uint32
	Comment string
}

// NewInfoZIPUnicodeComment returns a new InfoZIPUnicodeComment extra field
// structure.
func NewInfoZIPUnicodeComment(comment string) InfoZIPUnicodeComment {
	return InfoZIPUnicodeComment{
		Version: 1,
		Crc32:   crc32.ChecksumIEEE([]byte(comment)),
		Comment: comment,
	}
}

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

	buf.Write8(field.Version)
	buf.Write32(field.Crc32)
	buf.WriteBytes([]byte(field.Comment))

	return buf.Bytes()
}

// InfoZIPUnicodeComment returns the decoded InfoZIPUnicodeComment extra field.
func (ef ExtraField) InfoZIPUnicodeComment() (field InfoZIPUnicodeComment, err error) {
	buf := NewBuffer(ef)
	if buf.Available() < 5 {
		return field, ErrInvalidExtraFieldFormat
	}

	field.Version = buf.Read8()
	field.Crc32 = buf.Read32()
	field.Comment = string(buf.ReadBytes(buf.Available()))

	return
}