read GpgmeData

Florian Lindner mailinglists at xgm.de
Tue Apr 29 19:10:02 CEST 2003


Am Dienstag, 29. April 2003 15:23 schrieb Marcus Brinkmann:
> On Mon, Apr 28, 2003 at 11:00:24PM +0200, Florian Lindner wrote:
> > Hello,
> > I'm rather a C++ programmer than C, therefor I've little excperience with
> > malloc, free and char[].
>
> Writing a C++ wrapper for gpgme could be an interesting project, though.

I'm currently trying to do that.

> Still, you should know about new and delete, and arrays, even if you
> started out with C++.  malloc and free allocate and release memory
> explicitely as the C++ operators new and delete do.
>
> > How can I get the result of gpgme_data_release_and_get_mem into a
> > std::string object?
> >
> > 	GpgmeData data, output;
> > 	gpgme_data_new(&data);
> > 	gpgme_data_new_from_mem(&data, m_text.c_str(), m_text.size(), 1);
> > 	gpgme_op_encrypt(m_ctx, rec, data, output);
> >
> > 	int length;
> > 	gpgme_data_release_and_get_mem(output, &length);  // get the return
> > value
>
> You need to add a binary 0 to the data object:
>
> gpgme_data_write (output, "", 1);
>
> and then the return value of gpgme_data_release_and_get_mem is a standard C
> string (if the data is in armor format, of course, and does not contain
> binary zeroes).
>
> From there on, you can use the standard constructor (and implicit
> conversion rules) to convert a C string to a std::string.  Stroustrup
> covers this in his book.
>
> After conversion, you must free the result.

I've one other problem at the moment:

std::string GPG::Text::Encrypt(bool sign)
{
	if (m_rlist.size() == 0)
		throw ErrNoRecipientKey(1, "No recipient key was given. Please provide one 
before encrypting.", "GPG::Text::Encrypt");

	GpgmeRecipients rec;
	if (gpgme_recipients_new(&rec)==GPGME_Out_Of_Core)
		throw ErrOutOfMemory(2, "Out of memory.", "GPG::Text::Encrypt, while 
creating the new recipients list");
	for (Keylist::iterator i = m_rlist.begin(); i != m_rlist.end(); i++)
		gpgme_recipients_add_name(rec, (*i)->GetUserID(0).c_str());

	GpgmeData data, output;
	gpgme_data_new(&data);
	gpgme_data_new_from_mem(&data, m_text.c_str(), m_text.size(), 1);
	gpgme_op_encrypt(m_ctx, rec, data, output);

	gpgme_data_release(output);
	gpgme_data_release(data);
	gpgme_recipients_release(rec);
	return "";
}

m_rlist is a  std::vector<GPG::Key*>.
GPG::Key has a member function GetUserID.

std::string GPG::Key::GetUserID(int index) {
	return gpgme_key_get_string_attr(m_key, GPGME_ATTR_USERID, 0, index);
}

CTX is a valid session context., m_rlist contains on Key. running this piece 
of code produces:

gpg: data.c:704: _gpgme_data_append: Assertion `!dh->private_buffer' failed.
Aborted

Any idea why?
Thx,
Florian




More information about the Gnupg-devel mailing list