-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcobhan.go
More file actions
486 lines (400 loc) · 10 KB
/
cobhan.go
File metadata and controls
486 lines (400 loc) · 10 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package cobhan
import (
"C"
"encoding/json"
"math"
"os"
"unsafe"
)
const ERR_NONE = 0
// ERR_NULL_PTR One of the provided pointers is NULL / nil / 0
const ERR_NULL_PTR = -1
// ERR_BUFFER_TOO_LARGE One of the provided buffer lengths is too large
const ERR_BUFFER_TOO_LARGE = -2
// ERR_BUFFER_TOO_SMALL One of the provided buffers was too small
const ERR_BUFFER_TOO_SMALL = -3
// ERR_COPY_FAILED Failed to copy into the buffer (copy length != expected length)
const ERR_COPY_FAILED = -4
// ERR_JSON_DECODE_FAILED Failed to decode a JSON buffer
const ERR_JSON_DECODE_FAILED = -5
// ERR_JSON_ENCODE_FAILED Failed to encode to JSON buffer
const ERR_JSON_ENCODE_FAILED = -6
// ERR_INVALID_UTF8 Buffer contains invalid utf-8
const ERR_INVALID_UTF8 = -7
const ERR_READ_TEMP_FILE_FAILED = -8
const ERR_WRITE_TEMP_FILE_FAILED = -9
// Reusable functions to facilitate FFI
const BUFFER_HEADER_SIZE = (64 / 8) // 64 bit buffer header provides 8 byte alignment for data pointers
const DefaultBufferMaximum = math.MaxInt32
var bufferMaximum = math.MaxInt32
var allowTempFileBuffers = true
var copyBuffers = false
func SetDefaultBufferMaximum(max int) {
bufferMaximum = max
}
func AllowTempFileBuffers(flag bool) {
allowTempFileBuffers = flag
}
func CopyBuffers(flag bool) {
copyBuffers = flag
}
func CPtr(buf *[]byte) *C.char {
return (*C.char)(Ptr(buf))
}
func Ptr(buf *[]byte) unsafe.Pointer {
return unsafe.Pointer(&(*buf)[0])
}
func AllocateBuffer(length int) []byte {
//Allocation
buf := make([]byte, length+BUFFER_HEADER_SIZE)
updateBufferPtrLength(Ptr(&buf), length)
return buf
}
func AllocateStringBuffer(str string) ([]byte, int32) {
//Allocation
buf := AllocateBuffer(len(str))
result := StringToBufferSafe(str, &buf)
if result != ERR_NONE {
return nil, result
}
return buf, ERR_NONE
}
func AllocateBytesBuffer(bytes []byte) ([]byte, int32) {
//Allocation
buf := AllocateBuffer(len(bytes))
result := BytesToBufferSafe(bytes, &buf)
if result != ERR_NONE {
return nil, result
}
return buf, ERR_NONE
}
func bufferPtrToLength(bufferPtr unsafe.Pointer) C.int {
return C.int(*(*int32)(bufferPtr))
}
func bufferPtrToDataPtr(bufferPtr unsafe.Pointer) unsafe.Pointer {
return unsafe.Pointer(uintptr(bufferPtr) + BUFFER_HEADER_SIZE)
}
func bufferPtrToString(bufferPtr unsafe.Pointer, length C.int) string {
dataPtr := bufferPtrToDataPtr(bufferPtr)
//Allocation
return C.GoStringN((*C.char)(dataPtr), length)
}
func bufferPtrToBytes(bufferPtr unsafe.Pointer, length C.int) ([]byte, int32) {
src := unsafe.Slice((*byte)(bufferPtrToDataPtr(bufferPtr)), length)
if copyBuffers {
dst := make([]byte, length)
copy(dst, src)
return dst, ERR_NONE
}
return src, ERR_NONE
}
func updateBufferPtrLength(bufferPtr unsafe.Pointer, length int) {
*(*int32)(bufferPtr) = int32(length)
}
func tempToBytes(ptr unsafe.Pointer, length C.int) ([]byte, int32) {
if !allowTempFileBuffers {
return nil, ERR_READ_TEMP_FILE_FAILED
}
length = 0 - length
if bufferMaximum < int(length) {
return nil, ERR_BUFFER_TOO_LARGE
}
fileName := bufferPtrToString(ptr, length)
fileData, err := os.ReadFile(fileName)
if err != nil {
return nil, ERR_READ_TEMP_FILE_FAILED //TODO: Temp file read error
}
os.Remove(fileName) // Ignore delete error
return fileData, ERR_NONE
}
func Int64ToBuffer(value int64, dstPtr unsafe.Pointer) int32 {
if dstPtr == nil {
return ERR_NULL_PTR
}
intPtr := (*int64)(dstPtr)
*intPtr = value
return 0
}
func Int64ToBufferSafe(value int64, dst *[]byte) int32 {
if dst == nil {
return ERR_NULL_PTR
}
Int64ToBuffer(value, Ptr(dst))
return 0
}
func BufferToInt64Safe(src *[]byte) (int64, int32) {
if src == nil {
return 0, ERR_NULL_PTR
}
return BufferToInt64(Ptr(src))
}
func BufferToInt64(srcPtr unsafe.Pointer) (int64, int32) {
if srcPtr == nil {
return 0, ERR_NULL_PTR
}
return *(*int64)(srcPtr), 0
}
func Int32ToBuffer(value int32, dstPtr unsafe.Pointer) int32 {
if dstPtr == nil {
return ERR_NULL_PTR
}
intPtr := (*int32)(dstPtr)
*intPtr = value
return 0
}
func BufferToInt32Safe(src *[]byte) (int32, int32) {
if src == nil {
return 0, ERR_NULL_PTR
}
return BufferToInt32(Ptr(src))
}
func BufferToInt32(srcPtr unsafe.Pointer) (int32, int32) {
if srcPtr == nil {
return 0, ERR_NULL_PTR
}
return *(*int32)(srcPtr), 0
}
func Int32ToBufferSafe(value int32, dst *[]byte) int32 {
if dst == nil {
return ERR_NULL_PTR
}
Int32ToBuffer(value, Ptr(dst))
return 0
}
func BufferLength(srcPtr unsafe.Pointer) int32 {
if srcPtr == nil {
return 0
}
return int32(bufferPtrToLength(srcPtr))
}
func BufferLengthSafe(src *[]byte) int32 {
if src == nil {
return 0
}
return BufferLength(Ptr(src))
}
func IsBufferAllNulls(srcPtr unsafe.Pointer) bool {
if srcPtr == nil {
return false
}
length := bufferPtrToLength(srcPtr)
if length <= 0 {
return false
}
checkLen := int(length)
if checkLen > 64 {
checkLen = 64
}
src := unsafe.Slice((*byte)(bufferPtrToDataPtr(srcPtr)), checkLen)
for _, b := range src {
if b != 0 {
return false
}
}
return true
}
func IsBufferAllNullsSafe(src *[]byte) bool {
if src == nil {
return false
}
return IsBufferAllNulls(Ptr(src))
}
func BufferToBytesSafe(src *[]byte) ([]byte, int32) {
if src == nil {
return nil, ERR_NULL_PTR
}
return BufferToBytes(Ptr(src))
}
func BufferToBytes(srcPtr unsafe.Pointer) ([]byte, int32) {
if srcPtr == nil {
return nil, ERR_NULL_PTR
}
length := bufferPtrToLength(srcPtr)
if bufferMaximum < int(length) {
return nil, ERR_BUFFER_TOO_LARGE
}
if length >= 0 {
return bufferPtrToBytes(srcPtr, length)
} else {
return tempToBytes(srcPtr, length)
}
}
func BufferToStringSafe(src *[]byte) (string, int32) {
if src == nil {
return "", ERR_NULL_PTR
}
return BufferToString(Ptr(src))
}
func BufferToString(srcPtr unsafe.Pointer) (string, int32) {
if srcPtr == nil {
return "", ERR_NULL_PTR
}
length := bufferPtrToLength(srcPtr)
if bufferMaximum < int(length) {
return "", ERR_BUFFER_TOO_LARGE
}
if length >= 0 {
return bufferPtrToString(srcPtr, length), ERR_NONE
} else {
bytes, result := tempToBytes(srcPtr, length)
if result < 0 {
return "", result
}
return string(bytes), ERR_NONE
}
}
func BufferToJsonSafe(src *[]byte) (map[string]interface{}, int32) {
if src == nil {
return nil, ERR_NULL_PTR
}
return BufferToJson(Ptr(src))
}
func BufferToJson(srcPtr unsafe.Pointer) (map[string]interface{}, int32) {
if srcPtr == nil {
return nil, ERR_NULL_PTR
}
bytes, result := BufferToBytes(srcPtr)
if result < 0 {
return nil, result
}
var loadedJson interface{}
err := json.Unmarshal(bytes, &loadedJson)
if err != nil {
return nil, ERR_JSON_DECODE_FAILED
}
return loadedJson.(map[string]interface{}), ERR_NONE
}
func BufferToJsonStruct(srcPtr unsafe.Pointer, dst interface{}) int32 {
if srcPtr == nil {
return ERR_NULL_PTR
}
bytes, result := BufferToBytes(srcPtr)
if result < 0 {
return result
}
err := json.Unmarshal(bytes, dst)
if err != nil {
return ERR_JSON_DECODE_FAILED
}
return ERR_NONE
}
func BufferToJsonStructSafe(src *[]byte, dst interface{}) int32 {
if src == nil {
return ERR_NULL_PTR
}
return BufferToJsonStruct(Ptr(src), dst)
}
func StringToBufferSafe(str string, dst *[]byte) int32 {
if dst == nil {
return ERR_NULL_PTR
}
return StringToBuffer(str, Ptr(dst))
}
func StringToBuffer(str string, dstPtr unsafe.Pointer) int32 {
if dstPtr == nil {
return ERR_NULL_PTR
}
return BytesToBuffer([]byte(str), dstPtr)
}
func JsonToBufferSafe(v interface{}, dst *[]byte) int32 {
if dst == nil {
return ERR_NULL_PTR
}
return JsonToBuffer(v, Ptr(dst))
}
func JsonToBuffer(v interface{}, dstPtr unsafe.Pointer) int32 {
if dstPtr == nil {
return ERR_NULL_PTR
}
outputBytes, err := json.Marshal(v)
if err != nil {
return ERR_JSON_ENCODE_FAILED
}
return BytesToBuffer(outputBytes, dstPtr)
}
func BytesToBufferSafe(bytes []byte, dst *[]byte) int32 {
if dst == nil {
return ERR_NULL_PTR
}
return BytesToBuffer(bytes, Ptr(dst))
}
func BytesToBuffer(bytes []byte, dstPtr unsafe.Pointer) int32 {
if dstPtr == nil {
return ERR_NULL_PTR
}
//Get the destination capacity from the Buffer
dstCap := bufferPtrToLength(dstPtr)
dstCapInt := int(dstCap)
bytesLen := len(bytes)
// Construct a byte slice out of the unsafe pointers
var dst []byte = unsafe.Slice((*byte)(bufferPtrToDataPtr(dstPtr)), dstCapInt)
var result int
if dstCapInt < bytesLen {
// Output will not fit in supplied buffer
if !allowTempFileBuffers {
return ERR_BUFFER_TOO_SMALL
}
// Write the data to a temp file and copy the temp file name into the buffer
file, err := os.CreateTemp("", "cobhan-*")
if err != nil {
return ERR_WRITE_TEMP_FILE_FAILED
}
fileName := file.Name()
if len(fileName) > dstCapInt {
// Even the file path won't fit in the output buffer, we're completely out of luck now
file.Close()
os.Remove(fileName)
return ERR_BUFFER_TOO_SMALL
}
_, err = file.Write(bytes)
if err != nil {
file.Close()
os.Remove(fileName)
return ERR_WRITE_TEMP_FILE_FAILED
}
// Explicit rather than defer
file.Close()
fileNameBytes := ([]byte)(fileName)
result = copy(dst, fileNameBytes)
if result != len(fileNameBytes) {
os.Remove(fileName)
return ERR_COPY_FAILED
}
// Convert result to temp file name length
result = 0 - result
} else {
result = copy(dst, bytes)
if result != bytesLen {
return ERR_COPY_FAILED
}
}
//Update the output buffer length
updateBufferPtrLength(dstPtr, result)
return ERR_NONE
}
func CobhanErrorToString(cobhanError int32) string {
switch cobhanError {
case ERR_NONE:
return "ERR_NONE"
case ERR_BUFFER_TOO_SMALL:
return "ERR_BUFFER_TOO_SMALL"
case ERR_BUFFER_TOO_LARGE:
return "ERR_BUFFER_TOO_LARGE"
case ERR_COPY_FAILED:
return "ERR_COPY_FAILED"
case ERR_INVALID_UTF8:
return "ERR_INVALID_UTF8"
case ERR_JSON_DECODE_FAILED:
return "ERR_JSON_DECODE_FAILED"
case ERR_JSON_ENCODE_FAILED:
return "ERR_JSON_ENCODE_FAILED"
case ERR_READ_TEMP_FILE_FAILED:
return "ERR_READ_TEMP_FILE_FAILED"
case ERR_WRITE_TEMP_FILE_FAILED:
return "ERR_WRITE_TEMP_FILE_FAILED"
case ERR_NULL_PTR:
return "ERR_NULL_PTR"
default:
return "UNKNOWN"
}
}