-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsafearray.go
423 lines (378 loc) · 11.2 KB
/
safearray.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package safearray
import (
"errors"
"reflect"
"unsafe"
"github.com/go-ole/com"
"github.com/go-ole/idispatch"
"github.com/go-ole/iunknown"
)
var (
NotSliceError = errors.New("must be a slice")
VariantNotImplementedError = errors.New("variant type is not implemented")
VariantNotSupportedError = errors.New("variant type is not supported")
SafeArrayVectorFailedError = errors.New("could not convert slice to SafeArray")
)
// COMArray is how COM handles arrays.
type COMArray struct {
Dimensions uint16
FeaturesFlag uint16
ElementsSize uint32
LocksAmount uint32
Data uint32
// This must hold the bytes for two Bounds objects. Use binary.Read() to
// get the contents.
Bounds [16]byte
}
// Bounds defines the array boundaries.
type Bounds struct {
Elements uint32
LowerBound int32
}
// Array is wrapper for COMArray with helpers.
//
// It is recommended that you use this type instead of the COMArray, because
// the bounds is a pointer to the SafeArrayBounds and not referenced directly.
type Array struct {
Array *COMArray
// bounds contains a mirror of the COMArray.Bounds in the Go type.
bounds [2]Bounds
}
// Destroy SafeArray object.
func (sa *Array) Destroy() error {
return Destroy(sa.Array)
}
// DestroyData removes safe array data.
func (sa *Array) DestroyData() error {
return DestroyData(sa.Array)
}
// DestroyDescriptor removes safe array descriptor.
func (sa *Array) DestroyDescriptor() error {
return DestroyDescriptor(sa.Array)
}
// Duplicate SafeArray to another SafeArray.
//
// This copies the underlying COMArray object into another Array object.
func (sa *Array) Duplicate() (*Array, error) {
saCopy, err := Duplicate(sa.Array)
return &Array{saCopy}, err
}
// DuplicateDataTo takes current SafeArray Data and copies to given SafeArray.
//
// This copies the underlying COMArray data into another SafeArray
// COMArray object.
func (sa *Array) DuplicateDataTo(duplicate *Array) error {
return DuplicateData(sa.Array, duplicate.Array)
}
// Dimensions is the total number of array of arrays.
//
// For example is dimensions returns 3, then you have:
//
// array[0][]
// array[1][]
// array[2][]
//
// And so on for other lengths.
func (sa *Array) Dimensions() (uint32, error) {
return GetDimensions(sa.Array)
}
// ResetDimensions resets the bounds of the SafeArray.
//
// If the bounds is less than the current, then memory will be automatically
// freed. If the bounds is more than the current, then memory will be
// automatically allocated.
func (sa *Array) ResetDimensions(bounds []Bounds) error {
sa.bounds = bounds
return ResetDimensions(sa.Array, &sa.bounds[0])
}
// ElementSize is the type's size.
func (sa *Array) ElementSize() (uint32, error) {
return GetElementSize(sa.Array)
}
// Length returns total elements for SafeArray.
func (sa *Array) Length() (totalElements int64, err error) {
totalElements = 0
dimensions, err := sa.Dimensions()
if err != nil {
return
}
for dimension := uint32(1); dimension <= dimensions; dimension++ {
length, err := sa.DimensionLength(dimension)
if err != nil {
return
}
totalElements += length
}
return
}
// DimensionLength returns total elements for given dimension.
//
// Dimensions start at 1, this will only be corrected if you enter '0'.
func (sa *Array) DimensionLength(dimension uint32) (totalElements int64, err error) {
if dimension < 1 {
dimension = 1
}
// Get array bounds
var LowerBounds int64
var UpperBounds int64
LowerBounds, err = GetLowerBound(sa.Array, dimension)
if err != nil {
return
}
UpperBounds, err = GetUpperBound(sa.Array, dimension)
if err != nil {
return
}
totalElements = UpperBounds - LowerBounds + 1
return
}
// SetElementAt with element value at index.
//
// XXX: Index must be defined on how it works with multidimensional arrays.
func (sa *Array) SetElementAt(index int64, element interface{}) error {
return PutElement(sa.Array, index, &element)
}
// ElementAt returns element at index.
//
// Returned value will need to be converted to the type you require, because it
// is an interface{}.
//
// XXX: Index must be defined on how it works with multidimensional arrays.
func (sa *Array) ElementAt(index int64) (interface{}, error) {
return GetElement(sa.Array, index)
}
// ElementDirect puts element value into given element.
//
// You do not need to convert element. It will be typed to the interface. This
// is an unsafe operation. Element must be passed by reference.
//
// XXX: Index must be defined on how it works with multidimensional arrays.
func (sa *Array) ElementDirect(index int64, element interface{}) error {
return GetElementDirect(sa.Array, index, &element)
}
// SetInterfaceID sets the IID for the COM array.
//
// This is only used when serving COM arrays to clients.
func (sa *Array) SetInterfaceID(interfaceID *com.GUID) error {
return SetInterfaceID(sa.Array, &interfaceID)
}
// InterfaceID may return the IID, if the array type is a COM object.
func (sa *Array) InterfaceID() (*com.GUID, error) {
return GetInterfaceID(sa.Array)
}
// VariantType returns the variant type, if there is one available.
//
// Flag com.HasVariantSafeArrayMask must be set.
func (sa *Array) VariantType() (varType com.VariantType, err error) {
vt, err := GetVariantType(sa.Array)
varType = com.VariantType(vt)
return
}
// Lock SafeArray for modification.
func (sa *Array) Lock() error {
return Lock(sa.Array)
}
// Unlock SafeArray for reading.
func (sa *Array) Unlock() error {
return Unlock(sa.Array)
}
// RecordInfo retrieves IRecordInfo for SafeArray.
//
// XXX: Must implement IRecordInfo interface for this to return.
func (sa *Array) RecordInfo() (interface{}, error) {
return GetRecordInfo(sa.Array)
}
// SetRecordInfo sets IRecordInfo for SafeArray.
//
// XXX: Must implement IRecordInfo interface for this to return.
func (sa *Array) SetRecordInfo(info interface{}) error {
return SetRecordInfo(sa.Array, info)
}
// ToArrayDirect converts SafeArray data in to arbitrary type slice.
//
// This works on both single dimensional and multidimensional arrays. It will
// convert multidimensional to single dimensional arrays. This will not change
// in the future. A separate method exists for returning a multidimensional
// array.
func (sa *Array) ToArrayDirect(slice interface{}) (err error) {
if !IsSlice(slice) {
err = NotSliceError
return
}
dimensions, err := GetDimensions(sa.Array)
if err != nil {
return
}
length, err := sa.Length()
if err != nil {
return
}
kind := reflect.ValueOf(slice).Kind()
if dimensions == 1 && kind != reflect.String {
err = MarshalArray(sa.Array, length, &slice)
return
}
t := reflect.TypeOf(slice)
for i := int64(0); i < length; i++ {
if kind != string {
element := reflect.New(t).Interface()
err = GetElementDirect(sa.Array, i, &element)
if err != nil {
return
}
*slice = append(slice, element)
} else {
element, err := GetElementString(sa.Array, i)
if err != nil {
return
}
*slice = append(slice, element)
}
}
}
// ToArray returns array slice based on the supported variant type.
//
// If there is no variant type, then an error will be returned.
//
// You must also convert the returned value to whatever slice type it should be.
//
// raw, err := array.ToArray()
// slice, ok := raw.([]byte)
//
// This must be done because the returned type is a slice of interface{}.
func (sa *Array) ToArray() (slice interface{}, err error) {
vt, err := sa.VariantType()
if err != nil {
return
}
// Must not have VT_ARRAY and VT_BYREF flags set.
// Must not be VT_EMPTY and VT_NULL.
switch vt {
case com.Float32VariantType:
slice = make([]float32, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.Float64VariantType:
slice = make([]float64, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.CurrencyVariantType:
slice = make([]*com.Currency, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.DateVariantType:
err = VariantNotImplementedError
case com.BinaryStringVariantType, com.ClassIDVariantType:
slice = make([]string, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.IDispatchVariantType:
slice = make([]*idispatch.Dispatch, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.ErrorVariantType:
err = VariantNotImplementedError
case com.BoolVariantType:
slice = make([]uint16, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.VariantVariantType:
slice = make([]*com.Variant, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.IUnknownVariantType:
slice = make([]*iunknown.Unknown, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.DecimalVariantType:
slice = make([]*com.Decimal, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.Integer8VariantType:
slice = make([]int8, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.UInteger8VariantType:
slice = make([]uint8, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.Integer16VariantType:
slice = make([]int16, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.UInteger16VariantType:
slice = make([]uint16, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.Integer32VariantType:
slice = make([]int32, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.UInteger32VariantType:
slice = make([]uint32, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.Integer64VariantType:
slice = make([]int64, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.UInteger64VariantType:
slice = make([]uint64, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.IntegerVariantType:
// Warning: This must match the architecture of the application you wish
// to access.
slice = make([]int, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.UIntegerVariantType:
// Warning: This must match the architecture of the application you wish
// to access.
slice = make([]uint, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.HResultVariantType:
// Warning: This must match the architecture of the application you wish
// to access.
slice = make([]uintptr, sa.Length())
err = sa.ToArrayDirect(&slice)
// TODO: Need to turn HResult into OleError.
return
case com.PointerVariantType:
slice = make([]unsafe.Pointer, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.SafeArrayVariantType:
slice = make([]*COMArray, sa.Length())
err = sa.ToArrayDirect(&slice)
// Need to turn into Array objects
return
case com.CArrayVariantType:
// TODO: Complete
err = VariantNotImplementedError
case com.ANSIStringVariantType:
// TODO: Complete
err = VariantNotImplementedError
case com.UnicodeStringVariantType:
// TODO: Complete
err = VariantNotImplementedError
case com.RecordVariantType:
// TODO: Complete
err = VariantNotImplementedError
case com.IntegerPointerVariantType, com.UIntegerPointerVariantType:
slice = make([]uintptr, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.FileTimeVariantType:
slice = make([]*com.FileTime, sa.Length())
err = sa.ToArrayDirect(&slice)
return
case com.ClipboardFormatVariantType:
// TODO: Complete
err = VariantNotImplementedError
default:
err = VariantNotSupportedError
}
return
}