Revert "Make gobencode() optional in SetPriv(), return errors instead of integers"

This reverts commit 6a57e8c3fc.
This commit is contained in:
Dolf Schimmel (Freeaqingme)
2015-11-25 20:42:41 +01:00
parent 01964e5391
commit c60841ef06

View File

@@ -28,7 +28,6 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"encoding/gob" "encoding/gob"
"errors"
"fmt" "fmt"
"log" "log"
"net" "net"
@@ -358,24 +357,14 @@ func GetSymVal(ctx uintptr, symname string) string {
return C.GoString(cval) return C.GoString(cval)
} }
// Beware that if your struct is too large it will be discarded // See also: http://bit.ly/1HVWA9I
// without storing. You may want to simply provide a reference func SetPriv(ctx uintptr, privatedata interface{}) int {
// to something you keep in a map or similar structure yourself.
func SetPriv(ctx uintptr, privatedata interface{}) error {
// privatedata seems to work for any data type // privatedata seems to work for any data type
// Structs must have exported fields // Structs must have exported fields
// Serialize Go privatedata into a byte slice // Serialize Go privatedata into a byte slice
bytedata, err := GobEncode(privatedata) bytedata, _ := GobEncode(privatedata)
if err != nil {
return err
}
return SetPrivBytes(ctx, bytedata)
}
// See also: http://bit.ly/1HVWA9I
func SetPrivBytes(ctx uintptr, bytedata []byte) error {
// length and size // length and size
// length is a uint32 (usually 4 bytes) // length is a uint32 (usually 4 bytes)
// the length will be stored in front of the byte sequence // the length will be stored in front of the byte sequence
@@ -384,7 +373,7 @@ func SetPrivBytes(ctx uintptr, bytedata []byte) error {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, length) err := binary.Write(buf, binary.BigEndian, length)
if err != nil { if err != nil {
return errors.New("Could not write binary data into buffer: " + err.Error()) return -1
} }
lengthbytes := buf.Bytes() lengthbytes := buf.Bytes()
@@ -410,29 +399,10 @@ func SetPrivBytes(ctx uintptr, bytedata []byte) error {
// Call libmilter smfi_setpriv // Call libmilter smfi_setpriv
type CtxPtr *C.struct_smfi_str type CtxPtr *C.struct_smfi_str
res := int(C.smfi_setpriv(int2ctx(ctx), unsafe.Pointer(lenStart))) return int(C.smfi_setpriv(int2ctx(ctx), unsafe.Pointer(lenStart)))
if res != int(C.MI_SUCCESS) {
return errors.New("smfi_setpriv() returned a failure")
}
return nil
} }
func GetPriv(ctx uintptr, privatedata interface{}) error { func GetPriv(ctx uintptr, privatedata interface{}) int {
databytes, err := GetPrivBytes(ctx)
if err != nil {
return err
}
err = GobDecode(databytes, privatedata)
if err != nil {
return err
}
return nil
}
func GetPrivBytes(ctx uintptr) ([]byte, error) {
/* Retrieve the private data stored by the milter /* Retrieve the private data stored by the milter
Retrieving the data will release the memory allocated for it Retrieving the data will release the memory allocated for it
Don't try to retrieve it again unless you call SetPriv first Don't try to retrieve it again unless you call SetPriv first
@@ -443,7 +413,7 @@ func GetPrivBytes(ctx uintptr) ([]byte, error) {
// Make sure data has been set with a previous call to SetPriv // Make sure data has been set with a previous call to SetPriv
if CArray == nil { if CArray == nil {
return nil, errors.New("smfi_getpriv() call failed") return -1
} }
// Read uint32 size bytes from the start of the pointer // Read uint32 size bytes from the start of the pointer
@@ -463,7 +433,7 @@ func GetPrivBytes(ctx uintptr) ([]byte, error) {
buf := bytes.NewBuffer(lengthbytes) buf := bytes.NewBuffer(lengthbytes)
err := binary.Read(buf, binary.BigEndian, &length) err := binary.Read(buf, binary.BigEndian, &length)
if err != nil { if err != nil {
return nil, errors.New("Could not parse binary data") return -1
} }
// Read byte sequence of data // Read byte sequence of data
@@ -478,7 +448,12 @@ func GetPrivBytes(ctx uintptr) ([]byte, error) {
C.smfi_setpriv(int2ctx(ctx), nil) C.smfi_setpriv(int2ctx(ctx), nil)
// Unserialize the data bytes back into a data structure // Unserialize the data bytes back into a data structure
return databytes, nil err = GobDecode(databytes, privatedata)
if err != nil {
return -1
}
return 0
} }
func SetReply(ctx uintptr, rcode, xcode, message string) int { func SetReply(ctx uintptr, rcode, xcode, message string) int {