...
1
2
3
4
5 package packet
6
7 import (
8 "bytes"
9 "image"
10 "image/jpeg"
11 "io"
12 )
13
14 const UserAttrImageSubpacket = 1
15
16
17
18
19
20 type UserAttribute struct {
21 Contents []*OpaqueSubpacket
22 }
23
24
25
26 func NewUserAttributePhoto(photos ...image.Image) (uat *UserAttribute, err error) {
27 uat = new(UserAttribute)
28 for _, photo := range photos {
29 var buf bytes.Buffer
30
31 data := []byte{
32 0x10, 0x00,
33 0x01,
34 0x01,
35 0, 0, 0, 0,
36 0, 0, 0, 0,
37 0, 0, 0, 0}
38 if _, err = buf.Write(data); err != nil {
39 return
40 }
41 if err = jpeg.Encode(&buf, photo, nil); err != nil {
42 return
43 }
44 uat.Contents = append(uat.Contents, &OpaqueSubpacket{
45 SubType: UserAttrImageSubpacket,
46 Contents: buf.Bytes()})
47 }
48 return
49 }
50
51
52 func NewUserAttribute(contents ...*OpaqueSubpacket) *UserAttribute {
53 return &UserAttribute{Contents: contents}
54 }
55
56 func (uat *UserAttribute) parse(r io.Reader) (err error) {
57
58 b, err := io.ReadAll(r)
59 if err != nil {
60 return
61 }
62 uat.Contents, err = OpaqueSubpackets(b)
63 return
64 }
65
66
67
68 func (uat *UserAttribute) Serialize(w io.Writer) (err error) {
69 var buf bytes.Buffer
70 for _, sp := range uat.Contents {
71 sp.Serialize(&buf)
72 }
73 if err = serializeHeader(w, packetTypeUserAttribute, buf.Len()); err != nil {
74 return err
75 }
76 _, err = w.Write(buf.Bytes())
77 return
78 }
79
80
81
82
83 func (uat *UserAttribute) ImageData() (imageData [][]byte) {
84 for _, sp := range uat.Contents {
85 if sp.SubType == UserAttrImageSubpacket && len(sp.Contents) > 16 {
86 imageData = append(imageData, sp.Contents[16:])
87 }
88 }
89 return
90 }
91
View as plain text