예제 #1
0
// decode from bytes.
// Return bytes decoded or 0 if we could not decode a complete object.
//
func decode(data *C.pn_data_t, bytes []byte) (int, error) {
	if len(bytes) == 0 {
		return 0, nil
	}
	n := int(C.pn_data_decode(data, cPtr(bytes), cLen(bytes)))
	if n == int(C.PN_UNDERFLOW) {
		C.pn_error_clear(C.pn_data_error(data))
		return 0, nil
	} else if n <= 0 {
		return 0, fmt.Errorf("unmarshal %s", PnErrorCode(n))
	}
	return n, nil
}
예제 #2
0
// decode from bytes. Return bytes decoded and errEOS if we run out of data.
func (pd pnDecoder) decode(bytes []byte) (n int, err error) {
	C.pn_data_clear(pd.data)
	if len(bytes) == 0 {
		return 0, errEOS
	}
	cBuf := (*C.char)(unsafe.Pointer(&bytes[0]))
	result := int(C.pn_data_decode(pd.data, cBuf, C.size_t(len(bytes))))
	if result < 0 {
		return 0, errorCode(result)
	} else {
		return result, nil
	}
}
예제 #3
0
// decode from bytes.
// Return bytes decoded or 0 if we could not decode a complete object.
//
func decode(data *C.pn_data_t, bytes []byte) int {
	if len(bytes) == 0 {
		return 0
	}
	n := int(C.pn_data_decode(data, cPtr(bytes), cLen(bytes)))
	if n == int(C.PN_UNDERFLOW) {
		C.pn_error_clear(C.pn_data_error(data))
		return 0
	} else if n <= 0 {
		panic(internal.Errorf("unmarshal %s", internal.PnErrorCode(n)))
	}
	return n
}