-
Notifications
You must be signed in to change notification settings - Fork 4
/
abeid.go
249 lines (215 loc) · 5.88 KB
/
abeid.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copyright 2019 Kopano and its licensors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kcc
import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"io/ioutil"
)
// Version numbers as used by Kopano ABEID implementations.
const (
ABEIDV1VersionNumber = 1
)
// ABEID defines the public interface for Kopano AB EntryIDs.
type ABEID interface {
ABFlags() byte
GUID() [16]byte
Type() MAPIType
ID() uint32
ExID() []byte
String() string
Hex() string
}
// An abeidV1 defines an Kopano AB EntryID of version 1.
type abeidV1 struct {
header *abeidHeader
dataV1 *abeidV1Data
exID []byte
}
// ABFlags returns the first byte of the associated ABEIDs abflag data.
func (abeid *abeidV1) ABFlags() byte {
return abeid.header.ABFlags[0]
}
// GUID returns the associated ABEID GUID value.
func (abeid *abeidV1) GUID() [16]byte {
return abeid.header.GUID
}
// Type returns the associated ABEID Type.
func (abeid *abeidV1) Type() MAPIType {
return abeid.dataV1.Type
}
// ID returns the associated ABEID ID numeric field value.
func (abeid *abeidV1) ID() uint32 {
return abeid.dataV1.ID
}
// ExID returns the associated ABEID external ID field as byte value.
func (abeid *abeidV1) ExID() []byte {
return abeid.exID
}
func (abeid *abeidV1) String() string {
buf := new(bytes.Buffer)
enc1 := base64.NewEncoder(base64.StdEncoding, buf)
binary.Write(enc1, binary.LittleEndian, abeid.header)
binary.Write(enc1, binary.LittleEndian, abeid.dataV1)
enc2 := base64.NewEncoder(base64.StdEncoding, enc1)
enc2.Write(abeid.exID)
enc2.Close()
enc1.Close()
return buf.String()
}
func (abeid *abeidV1) Hex() string {
buf := new(bytes.Buffer)
enc1 := hex.NewEncoder(buf)
binary.Write(enc1, binary.LittleEndian, abeid.header)
binary.Write(enc1, binary.LittleEndian, abeid.dataV1)
enc2 := base64.NewEncoder(base64.StdEncoding, enc1)
enc2.Write(abeid.exID)
enc2.Close()
return buf.String()
}
// A abeidHeader is the byte representation of an AB EntryID start including
// version. See
// https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/entryid
// for the basic EntryID definition.
type abeidHeader struct {
ABFlags [4]byte
GUID [16]byte
Version uint32
}
// abeidV1Data define further values as defined in provider/include/kcore.hpp
// for version 1 ABEID structs.
type abeidV1Data struct {
Type MAPIType
ID uint32
/* Rest is exID of arbitrary size */
}
// NewABEIDFromBytes takes a byte value and returns the ABEID represented by
// those bytes.
func NewABEIDFromBytes(value []byte) (ABEID, error) {
reader := bytes.NewReader(value)
// Parse header into header struct.
var header abeidHeader
err := binary.Read(reader, binary.LittleEndian, &header)
if err != nil {
return nil, err
}
var abeid ABEID
switch header.Version {
case ABEIDV1VersionNumber:
// Parse fixed size V1 data into data struct.
var data abeidV1Data
err = binary.Read(reader, binary.LittleEndian, &data)
if err != nil {
break
}
// Read all the rest.
exIDRaw, readErr := ioutil.ReadAll(reader)
if readErr != nil {
err = readErr
break
}
// Remove padding.
exIDRaw = unpadBytesRightWithRune(exIDRaw, '\x00')
// Decode.
exID := make([]byte, base64.StdEncoding.DecodedLen(len(exIDRaw)))
n, decodeErr := base64.StdEncoding.Decode(exID, exIDRaw)
if decodeErr != nil {
err = decodeErr
break
}
// Construct with all the data.
abeid = &abeidV1{
header: &header,
dataV1: &data,
exID: exID[:n],
}
default:
err = fmt.Errorf("ABEID unsupported version %d", header.Version)
}
if err != nil {
return nil, err
}
return abeid, nil
}
// NewABEIDFromHex takes a hex encoded byte value and returns the ABEID
// represented by those bytes.
func NewABEIDFromHex(hexValue []byte) (ABEID, error) {
value := make([]byte, hex.DecodedLen(len(hexValue)))
if _, err := hex.Decode(value, hexValue); err != nil {
return nil, err
}
return NewABEIDFromBytes(value)
}
// NewABEIDFromBase64 takes a base64Std encoded byte value and returns the ABEID
// represented by those bytes.
func NewABEIDFromBase64(base64Value []byte) (ABEID, error) {
value := make([]byte, base64.StdEncoding.DecodedLen(len(base64Value)))
if _, err := base64.StdEncoding.Decode(value, base64Value); err != nil {
return nil, err
}
return NewABEIDFromBytes(value)
}
// NewABEIDV1 creates a new NewABEIDV1 from the provided values.
func NewABEIDV1(guid [16]byte, typE MAPIType, id uint32, exID []byte) (ABEID, error) {
abeid := &abeidV1{
header: &abeidHeader{
GUID: guid,
Version: ABEIDV1VersionNumber,
},
dataV1: &abeidV1Data{
Type: typE,
ID: id,
},
exID: exID,
}
return abeid, nil
}
// ABEIDEqual returns true if the provided btwo ABEID refer to the same entry
// considering all relevant fields, ignoring the not relevant (like ID).
func ABEIDEqual(first, second ABEID) bool {
switch a := first.(type) {
case *abeidV1:
b, ok := second.(*abeidV1)
if !ok {
return false
}
if a.header == nil || b.header == nil {
return false
}
if a.dataV1 == nil || b.dataV1 == nil {
return false
}
if a.header.Version != b.header.Version {
return false
}
if a.header.GUID != b.header.GUID {
return false
}
if a.dataV1.Type != b.dataV1.Type {
return false
}
return bytes.Equal(a.exID, b.exID)
}
return false
}
func unpadBytesRightWithRune(value []byte, p rune) []byte {
return bytes.TrimRightFunc(value, func(r rune) bool {
return r == p
})
}