本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.5KB

  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package packet
  5. import (
  6. "bytes"
  7. "image"
  8. "image/jpeg"
  9. "io"
  10. "io/ioutil"
  11. )
  12. const UserAttrImageSubpacket = 1
  13. // UserAttribute is capable of storing other types of data about a user
  14. // beyond name, email and a text comment. In practice, user attributes are typically used
  15. // to store a signed thumbnail photo JPEG image of the user.
  16. // See RFC 4880, section 5.12.
  17. type UserAttribute struct {
  18. Contents []*OpaqueSubpacket
  19. }
  20. // NewUserAttributePhoto creates a user attribute packet
  21. // containing the given images.
  22. func NewUserAttributePhoto(photos ...image.Image) (uat *UserAttribute, err error) {
  23. uat = new(UserAttribute)
  24. for _, photo := range photos {
  25. var buf bytes.Buffer
  26. // RFC 4880, Section 5.12.1.
  27. data := []byte{
  28. 0x10, 0x00, // Little-endian image header length (16 bytes)
  29. 0x01, // Image header version 1
  30. 0x01, // JPEG
  31. 0, 0, 0, 0, // 12 reserved octets, must be all zero.
  32. 0, 0, 0, 0,
  33. 0, 0, 0, 0}
  34. if _, err = buf.Write(data); err != nil {
  35. return
  36. }
  37. if err = jpeg.Encode(&buf, photo, nil); err != nil {
  38. return
  39. }
  40. uat.Contents = append(uat.Contents, &OpaqueSubpacket{
  41. SubType: UserAttrImageSubpacket,
  42. Contents: buf.Bytes()})
  43. }
  44. return
  45. }
  46. // NewUserAttribute creates a new user attribute packet containing the given subpackets.
  47. func NewUserAttribute(contents ...*OpaqueSubpacket) *UserAttribute {
  48. return &UserAttribute{Contents: contents}
  49. }
  50. func (uat *UserAttribute) parse(r io.Reader) (err error) {
  51. // RFC 4880, section 5.13
  52. b, err := ioutil.ReadAll(r)
  53. if err != nil {
  54. return
  55. }
  56. uat.Contents, err = OpaqueSubpackets(b)
  57. return
  58. }
  59. // Serialize marshals the user attribute to w in the form of an OpenPGP packet, including
  60. // header.
  61. func (uat *UserAttribute) Serialize(w io.Writer) (err error) {
  62. var buf bytes.Buffer
  63. for _, sp := range uat.Contents {
  64. sp.Serialize(&buf)
  65. }
  66. if err = serializeHeader(w, packetTypeUserAttribute, buf.Len()); err != nil {
  67. return err
  68. }
  69. _, err = w.Write(buf.Bytes())
  70. return
  71. }
  72. // ImageData returns zero or more byte slices, each containing
  73. // JPEG File Interchange Format (JFIF), for each photo in the
  74. // user attribute packet.
  75. func (uat *UserAttribute) ImageData() (imageData [][]byte) {
  76. for _, sp := range uat.Contents {
  77. if sp.SubType == UserAttrImageSubpacket && len(sp.Contents) > 16 {
  78. imageData = append(imageData, sp.Contents[16:])
  79. }
  80. }
  81. return
  82. }
上海开阖软件有限公司 沪ICP备12045867号-1