aboutsummaryrefslogtreecommitdiff
path: root/librsync/librsync_callback.go
blob: 45a874c1101e566b3a2a9dde6f1a93227cb801f3 (plain)
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
package librsync // import "code.laria.me/golibrsync/librsync"

/*
#include <stdio.h>
#include <librsync.h>
#include <stdlib.h>
*/
import "C"

import (
	"io"
	"unsafe"
)

//export patchCallbackGo
func patchCallbackGo(_patcher uintptr, pos C.rs_long_t, buflen *C.size_t, buf *unsafe.Pointer) C.rs_result {
	patcher := getPatcher(_patcher)

	if patcher.buf != nil {
		C.free(patcher.buf)
	}
	patcher.buf = C.malloc(*buflen)
	// https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
	s := (*[1 << 30]byte)(patcher.buf)[:*buflen:*buflen]
	n, err := patcher.basis.ReadAt(s, int64(pos))
	if n < int(*buflen) {
		if err != io.EOF {
			panic(jobInternalPanic{err})
		} else {
			return C.RS_INPUT_ENDED
		}
	}
	*buflen = C.size_t(n)
	*buf = patcher.buf

	return C.RS_DONE
}