...
1
2
3
4
5 package encoding
6
7 import (
8 "io"
9
10 "github.com/ProtonMail/go-crypto/openpgp/errors"
11 )
12
13
14
15 type OID struct {
16 bytes []byte
17 }
18
19 const (
20
21 maxOID = 254
22
23
24 reservedOIDLength1 = 0
25 reservedOIDLength2 = 0xff
26 )
27
28
29 func NewOID(bytes []byte) *OID {
30 switch len(bytes) {
31 case reservedOIDLength1, reservedOIDLength2:
32 panic("encoding: NewOID argument length is reserved")
33 default:
34 if len(bytes) > maxOID {
35 panic("encoding: NewOID argument too large")
36 }
37 }
38
39 return &OID{
40 bytes: bytes,
41 }
42 }
43
44
45 func (o *OID) Bytes() []byte {
46 return o.bytes
47 }
48
49
50 func (o *OID) BitLength() uint16 {
51 return uint16(len(o.bytes) * 8)
52 }
53
54
55 func (o *OID) EncodedBytes() []byte {
56 return append([]byte{byte(len(o.bytes))}, o.bytes...)
57 }
58
59
60 func (o *OID) EncodedLength() uint16 {
61 return uint16(1 + len(o.bytes))
62 }
63
64
65 func (o *OID) ReadFrom(r io.Reader) (int64, error) {
66 var buf [1]byte
67 n, err := io.ReadFull(r, buf[:])
68 if err != nil {
69 if err == io.EOF {
70 err = io.ErrUnexpectedEOF
71 }
72 return int64(n), err
73 }
74
75 switch buf[0] {
76 case reservedOIDLength1, reservedOIDLength2:
77 return int64(n), errors.UnsupportedError("reserved for future extensions")
78 }
79
80 o.bytes = make([]byte, buf[0])
81
82 nn, err := io.ReadFull(r, o.bytes)
83 if err == io.EOF {
84 err = io.ErrUnexpectedEOF
85 }
86
87 return int64(n) + int64(nn), err
88 }
89
View as plain text