Previous: File Based Data Buffers, Up: Creating Data Buffers
If neither memory nor file based data objects are a good fit for your application, you can implement the functions a data object provides yourself and create a data object from these callback functions.
The
gpgme_data_read_cb_t
type is the type of functions which GPGME calls if it wants to read data from a user-implemented data object. The function should read up to size bytes from the current read position into the space starting at buffer. The handle is provided by the user at data object creation time.The function should return the number of bytes read, 0 on EOF, and -1 on error. If an error occurs, errno should be set to describe the type of the error.
The
gpgme_data_write_cb_t
type is the type of functions which GPGME calls if it wants to write data to a user-implemented data object. The function should write up to size bytes to the current write position from the space starting at buffer. The handle is provided by the user at data object creation time.The function should return the number of bytes written, and -1 on error. If an error occurs, errno should be set to describe the type of the error.
The
gpgme_data_seek_cb_t
type is the type of functions which GPGME calls if it wants to change the current read/write position in a user-implemented data object, just like thelseek
function.The function should return the new read/write position, and -1 on error. If an error occurs, errno should be set to describe the type of the error.
The
gpgme_data_release_cb_t
type is the type of functions which GPGME calls if it wants to destroy a user-implemented data object. The handle is provided by the user at data object creation time.
This structure is used to store the data callback interface functions described above. It has the following members:
gpgme_data_read_cb_t read
- This is the function called by GPGME to read data from the data object. It is only required for input data object.
gpgme_data_write_cb_t write
- This is the function called by GPGME to write data to the data object. It is only required for output data object.
gpgme_data_seek_cb_t seek
- This is the function called by GPGME to change the current read/write pointer in the data object (if available). It is optional.
gpgme_data_release_cb_t release
- This is the function called by GPGME to release a data object. It is optional.
The function
gpgme_data_new_from_cbs
creates a newgpgme_data_t
object and uses the user-provided callback functions to operate on the data object.The handle handle is passed as first argument to the callback functions. This can be used to identify this data object.
The function returns the error code
GPG_ERR_NO_ERROR
if the data object was successfully created, andGPG_ERR_ENOMEM
if not enough memory is available.
The following interface is deprecated and only provided for backward compatibility. Don't use it. It will be removed in a future version of GPGME.
The function
gpgme_data_new_with_read_cb
creates a newgpgme_data_t
object and uses the callback function readfunc to retrieve the data on demand. As the callback function can supply the data in any way it wants, this is the most flexible data type GPGME provides. However, it can not be used to write data.The callback function receives hook_value as its first argument whenever it is invoked. It should return up to count bytes in buffer, and return the number of bytes actually read in nread. It may return
0
in nread if no data is currently available. To indicateEOF
the function should return with an error code of-1
and set nread to0
. The callback function may support to reset its internal read pointer if it is invoked with buffer and nread beingNULL
and count being0
.The function returns the error code
GPG_ERR_NO_ERROR
if the data object was successfully created,GPG_ERR_INV_VALUE
if dh or readfunc is not a valid pointer, andGPG_ERR_ENOMEM
if not enough memory is available.