-
Notifications
You must be signed in to change notification settings - Fork 8
/
property_reader.go
318 lines (257 loc) · 9.82 KB
/
property_reader.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// go-pst is a library for reading Personal Storage Table (.pst) files (written in Go/Golang).
//
// Copyright 2023 Marten Mooij
//
// 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 pst
import (
"encoding/binary"
"fmt"
"github.com/tinylib/msgp/msgp"
"math"
"time"
"github.com/rotisserie/eris"
"golang.org/x/text/encoding/ianaindex"
"golang.org/x/text/encoding/unicode"
)
// PropertyReader allows reading a property from the Property/Table Context.
// References [MS-OXCDATA]: 2.11.1 Property Data Types.
// Implements io.SectionReader.
type PropertyReader struct {
Property Property
HeapOnNodeReader *HeapOnNodeReader
LocalDescriptors []LocalDescriptor
File *File
}
// NewPropertyReader creates a new property reader.
func NewPropertyReader(property Property, heapOnNode *HeapOnNode, file *File, localDescriptors []LocalDescriptor) (PropertyReader, error) {
switch {
case property.HNID != 0 && len(property.Data) == 0:
heapOnNodeReader, err := file.GetHeapOnNodeReaderFromHNID(property.HNID, *heapOnNode.Reader, localDescriptors...)
if err != nil {
return PropertyReader{}, eris.Wrap(err, "failed to get Heap-on-Node reader")
}
return PropertyReader{property, heapOnNodeReader, localDescriptors, file}, nil
case len(property.Data) != 0:
return PropertyReader{property, nil, localDescriptors, file}, nil
default:
return PropertyReader{}, eris.Wrap(ErrPropertyNoData, fmt.Sprintf("Property ID: %x", property.ID))
}
}
// WriteMessagePackValue writes the Message Pack format of the property value.
// Used to populate struct fields.
func (propertyReader *PropertyReader) WriteMessagePackValue(writer *msgp.Writer) error {
key := fmt.Sprintf("%d%d", propertyReader.Property.ID, propertyReader.Property.Type)
switch propertyReader.Property.Type {
case PropertyTypeString:
value, err := propertyReader.GetString()
if err != nil {
return eris.Wrap(err, "failed to get string")
}
if err := writer.WriteString(key); err != nil {
return eris.Wrap(err, "failed to write key")
} else if err := writer.WriteString(value); err != nil {
return eris.Wrap(err, "failed to write value")
}
return nil
case PropertyTypeString8:
value, err := propertyReader.GetString8(65001) // TODO - Get from called, this is UTF-8 for now.
if err != nil {
return eris.Wrap(err, "failed to get string8")
}
if err := writer.WriteString(key); err != nil {
return eris.Wrap(err, "failed to write key")
} else if err := writer.WriteString(value); err != nil {
return eris.Wrap(err, "failed to write value")
}
return nil
case PropertyTypeTime:
value, err := propertyReader.GetDate()
if err != nil {
return eris.Wrap(err, "failed to get date")
}
if err := writer.WriteString(key); err != nil {
return eris.Wrap(err, "failed to write key")
} else if err := writer.WriteInt64(value); err != nil {
return eris.Wrap(err, "failed to write value")
}
return nil
case PropertyTypeInteger16:
value, err := propertyReader.GetInteger16()
if err != nil {
return eris.Wrap(err, "failed to get integer16")
}
if err := writer.WriteString(key); err != nil {
return eris.Wrap(err, "failed to write key")
} else if err := writer.WriteInt16(value); err != nil {
return eris.Wrap(err, "failed to write value")
}
return nil
case PropertyTypeInteger32:
value, err := propertyReader.GetInteger32()
if err != nil {
return eris.Wrap(err, "failed to write integer32")
}
if err := writer.WriteString(key); err != nil {
return eris.Wrap(err, "failed to write key")
} else if err := writer.WriteInt32(value); err != nil {
return eris.Wrap(err, "failed to write value")
}
return nil
case PropertyTypeInteger64:
value, err := propertyReader.GetInteger64()
if err != nil {
return eris.Wrap(err, "failed to get integer64")
}
if err := writer.WriteString(key); err != nil {
return eris.Wrap(err, "failed to write key")
} else if err := writer.WriteInt64(value); err != nil {
return eris.Wrap(err, "failed to write value")
}
return nil
case PropertyTypeBoolean:
value, err := propertyReader.GetBoolean()
if err != nil {
return eris.Wrap(err, "failed to get boolean")
}
if err := writer.WriteString(key); err != nil {
return eris.Wrap(err, "failed to write key")
} else if err := writer.WriteBool(value); err != nil {
return eris.Wrap(err, "failed to write value")
}
return nil
default:
// TODO - Write Nil?
return ErrPropertyNoData
}
}
// GetString returns the string value of the property.
func (propertyReader *PropertyReader) GetString() (string, error) {
if propertyReader.Property.Type != PropertyTypeString {
return "", ErrPropertyTypeMismatch
} else if propertyReader.HeapOnNodeReader == nil || propertyReader.Property.HNID == 0 {
return "", ErrPropertyNoData
}
data := make([]byte, propertyReader.Size())
if _, err := propertyReader.ReadAt(data, 0); err != nil {
return "", eris.Wrap(err, "failed to read data")
}
return propertyReader.DecodeString(data)
}
// GetString8 returns the string using the external encoding.
func (propertyReader *PropertyReader) GetString8(codepageIdentifier int) (string, error) {
if propertyReader.Property.Type != PropertyTypeString8 {
return "", ErrPropertyTypeMismatch
} else if propertyReader.HeapOnNodeReader == nil || propertyReader.Property.HNID == 0 {
return "", ErrPropertyNoData
}
data := make([]byte, propertyReader.Size())
if _, err := propertyReader.ReadAt(data, 0); err != nil {
return "", eris.Wrap(err, "failed to read data")
}
return propertyReader.DecodeString8(data, codepageIdentifier)
}
// GetInteger16 returns the 16-bit integer value of the property.
func (propertyReader *PropertyReader) GetInteger16() (int16, error) {
switch {
case propertyReader.Property.Type != PropertyTypeInteger16:
return 0, ErrPropertyTypeMismatch
case len(propertyReader.Property.Data) == 0:
return 0, ErrPropertyNoData
default:
return int16(binary.LittleEndian.Uint16(propertyReader.Property.Data)), nil
}
}
// GetInteger32 returns the 32-bit integer value of the property.
func (propertyReader *PropertyReader) GetInteger32() (int32, error) {
switch {
case propertyReader.Property.Type != PropertyTypeInteger32:
return 0, ErrPropertyTypeMismatch
case len(propertyReader.Property.Data) == 0:
return 0, ErrPropertyNoData
default:
return int32(binary.LittleEndian.Uint32(propertyReader.Property.Data)), nil
}
}
// GetInteger64 returns the 64-bit integer value of the property.
func (propertyReader *PropertyReader) GetInteger64() (int64, error) {
switch {
case propertyReader.Property.Type != PropertyTypeInteger64:
return 0, ErrPropertyTypeMismatch
case len(propertyReader.Property.Data) != 0:
return int64(binary.LittleEndian.Uint64(propertyReader.Property.Data)), nil
case propertyReader.Property.HNID != 0:
value := make([]byte, 8)
if _, err := propertyReader.ReadAt(value, 0); err != nil {
return 0, eris.Wrap(err, "failed to read integer64")
}
return int64(binary.LittleEndian.Uint64(value)), nil
default:
return 0, ErrPropertyNoData
}
}
// DecodeString decodes the PropertyTypeString using Unicode (UTF-16LE).
// References [MS-OXCDATA]: 2.11.1 Property Data Types.
func (propertyReader *PropertyReader) DecodeString(data []byte) (string, error) {
utf16Decoder := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM).NewDecoder()
return utf16Decoder.String(string(data))
}
// DecodeString8 decodes the PropertyTypeString8 using the code page identifier.
func (propertyReader *PropertyReader) DecodeString8(data []byte, codePageIdentifier int) (string, error) {
encoding, err := ianaindex.IANA.Encoding(CodePageIdentifierToEncoding[codePageIdentifier])
if err != nil {
return "", eris.Wrapf(err, "failed to find IANA encoding: %d", codePageIdentifier)
}
return encoding.NewDecoder().String(string(data))
}
// GetDate returns the date value (Unix Nano epoch) of the property context item.
func (propertyReader *PropertyReader) GetDate() (int64, error) {
if propertyReader.Property.Type != PropertyTypeTime {
return 0, ErrPropertyTypeMismatch
} else if propertyReader.Size() == 0 {
return 0, ErrPropertyNoData
}
outputBuffer := make([]byte, 8)
if _, err := propertyReader.ReadAt(outputBuffer, 0); err != nil {
return 0, eris.Wrap(err, "failed to read date")
}
// References https://stackoverflow.com/a/57903746
// The number of 100-nanosecond intervals since January 1, 1601.
input := int64(binary.LittleEndian.Uint64(outputBuffer))
maxd := time.Duration(math.MaxInt64).Truncate(100 * time.Nanosecond)
maxdUnits := int64(maxd / 100) // number of 100-ns units
t := time.Date(1601, 1, 1, 0, 0, 0, 0, time.UTC)
for input > maxdUnits {
t = t.Add(maxd)
input -= maxdUnits
}
if input != 0 {
t = t.Add(time.Duration(input * 100))
}
return t.UnixNano(), nil
}
// GetBoolean returns the boolean value of this property.
func (propertyReader *PropertyReader) GetBoolean() (bool, error) {
if propertyReader.Property.Type != PropertyTypeBoolean {
return false, ErrPropertyTypeMismatch
}
return propertyReader.Property.Data[0] == 1, nil
}
// ReadAt reads the underlying Heap-on-Node.
func (propertyReader *PropertyReader) ReadAt(outputBuffer []byte, offset int64) (int, error) {
return propertyReader.HeapOnNodeReader.ReadAt(outputBuffer, offset)
}
// Size returns the size of the Heap-on-Node.
func (propertyReader *PropertyReader) Size() int64 {
return propertyReader.HeapOnNodeReader.Size()
}